Example Code: SIMD (Vector Processing)#

Speed-up of multiplication by using AVX (mul.cpp)#
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "StopWatch.hpp"

#include <immintrin.h>

#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>

#ifdef __APPLE__
// Macos hasn't implemented the C11 aligned_alloc as of the time 2019/8.
void * aligned_alloc(size_t alignment, size_t size)
{
    void * ptr;
    posix_memalign(&ptr, alignment, size);
    return ptr;
}
#endif

constexpr const size_t width = 8; // count of floating-point
constexpr const size_t repeat = 1024 * 1024;
constexpr const size_t nelem = width * repeat;

constexpr const size_t necount = 20;
constexpr const float error_tolerance = 2.e-7;

void multiply1_loop(float* a, float* b, float* r)
{
    for (size_t i=0; i<repeat*width; i+=width)
    {
        for (size_t j=i; j<i+width; ++j)
        {
            r[j] = a[j] * b[j];
        }
    }
}

void multiply1_simd(float* a, float* b, float* r)
{
    for (size_t i=0; i<repeat; ++i)
    {
        __m256 * ma = (__m256 *) (&a[i*width]);
        __m256 * mb = (__m256 *) (&b[i*width]);
        __m256 * mr = (__m256 *) (&r[i*width]);
        *mr = _mm256_mul_ps(*ma, *mb);
    }
}

void multiply3_loop(float* a, float* b, float* r)
{
    for (size_t i=0; i<repeat*width; i+=width)
    {
        for (size_t j=i; j<i+width; ++j)
        {
            r[j] = a[j] * a[j];
            r[j] *= b[j];
            r[j] *= b[j];
        }
    }
}

void multiply3_simd(float* a, float* b, float* r)
{
    for (size_t i=0; i<repeat; ++i)
    {
        __m256 * ma = (__m256 *) (&a[i*width]);
        __m256 * mb = (__m256 *) (&b[i*width]);
        __m256 * mr = (__m256 *) (&r[i*width]);
        *mr = _mm256_mul_ps(*ma, *ma);
        *mr = _mm256_mul_ps(*mr, *mb);
        *mr = _mm256_mul_ps(*mr, *mb);
    }
}

void multiply5_loop(float* a, float* b, float* r)
{
    for (size_t i=0; i<repeat*width; i+=width)
    {
        for (size_t j=i; j<i+width; ++j)
        {
            r[j] = a[j] * a[j];
            r[j] *= a[j];
            r[j] *= b[j];
            r[j] *= b[j];
            r[j] *= b[j];
        }
    }
}

void multiply5_simd(float* a, float* b, float* r)
{
    for (size_t i=0; i<repeat; ++i)
    {
        __m256 * ma = (__m256 *) (&a[i*width]);
        __m256 * mb = (__m256 *) (&b[i*width]);
        __m256 * mr = (__m256 *) (&r[i*width]);
        *mr = _mm256_mul_ps(*ma, *ma);
        *mr = _mm256_mul_ps(*mr, *ma);
        *mr = _mm256_mul_ps(*mr, *mb);
        *mr = _mm256_mul_ps(*mr, *mb);
        *mr = _mm256_mul_ps(*mr, *mb);
    }
}

double run(std::function<void(float*,float*,float*)> func, float * arr, float * brr, float * rrr)
{
    StopWatch sw;
    std::vector<double> ecv;

    for (size_t ecount=0; ecount<necount; ++ecount)
    {
        for (size_t i=0; i<nelem; ++i)
        {
            arr[i] = i+1;
            brr[i] = i+1;
        }
        sw.lap();
        func(arr, brr, rrr);
        ecv.push_back(sw.lap());
    }

    return *std::min_element(ecv.begin(), ecv.end());
}

void check(float * rrr1, float * rrr2)
{
    size_t mismatch_count = 0;
    size_t wrong_count = 0;
    for (size_t i=0; i<nelem; ++i)
    {
        if (rrr1[i] != rrr2[i])
        {
            ++mismatch_count;
            if (std::abs((rrr1[i] - rrr2[i])/rrr1[i]) > error_tolerance)
            {
                ++wrong_count;
            }
        }
    }
    if (mismatch_count)
    {
        std::cout << "Mismatch: " << mismatch_count;
        std::cout
            << ", wrong (relative error > " << error_tolerance << "): "
            << wrong_count;
        std::cout << " / " << nelem << std::endl;
    }
}

int main(int argc, char ** argv)
{
    float * arr = (float *) aligned_alloc(32, nelem * sizeof(float));
    float * brr = (float *) aligned_alloc(32, nelem * sizeof(float));
    float * rrr1 = (float *) aligned_alloc(32, nelem * sizeof(float));
    float * rrr2 = (float *) aligned_alloc(32, nelem * sizeof(float));
    double elapsed;

    std::cout << "width: " << width << std::endl;
    std::cout << "nelem: " << nelem << std::endl;
    std::cout << std::endl;

    std::cout << "arr: " << std::hex << arr << std::endl;
    std::cout << "brr: " << std::hex << brr << std::endl;
    std::cout << "rrr1: " << std::hex << rrr1 << std::endl;
    std::cout << "rrr2: " << std::hex << rrr2 << std::endl;
    std::cout << std::endl;
    std::cout << std::dec;

    std::cout
        << "Timing repeats for " << necount << " times and takes the minimum"
        << std::endl << std::endl;

    elapsed = run(multiply1_loop, arr, brr, rrr1);
    std::cout
        << "1 multiplication by loop takes: "
        << elapsed << " sec" << std::endl;
    elapsed = run(multiply1_simd, arr, brr, rrr2);
    std::cout
        << "1 multiplication by simd takes: "
        << elapsed << " sec" << std::endl;
    check(rrr1, rrr2);
    std::cout << std::endl;

    elapsed = run(multiply3_loop, arr, brr, rrr1);
    std::cout
        << "3 multiplication by loop takes: "
        << elapsed << " sec" << std::endl;
    elapsed = run(multiply3_simd, arr, brr, rrr2);
    std::cout
        << "3 multiplication by simd takes: "
        << elapsed << " sec" << std::endl;
    check(rrr1, rrr2);
    std::cout << std::endl;

    elapsed = run(multiply5_loop, arr, brr, rrr1);
    std::cout
        << "5 multiplication by loop takes: "
        << elapsed << " sec" << std::endl;
    elapsed = run(multiply5_simd, arr, brr, rrr2);
    std::cout
        << "5 multiplication by simd takes: "
        << elapsed << " sec" << std::endl;
    check(rrr1, rrr2);
    std::cout << std::endl;

    free(arr);
    free(brr);
    free(rrr1);
    free(rrr2);
}
OpenMP example (omp.cpp)#
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>

int main(int, char**) {
    #pragma omp parallel
    {
        printf
        (
            "Hello from thread %d, nthreads %d\n"
          , omp_get_thread_num()
          , omp_get_num_threads()
        );
    }
    return EXIT_SUCCESS;
}