AVX, SSE amounts are slower than gcc autovectorization

I have a strange origin and cannot explain it. I am trying to write some numerical codes and thus check some implementations. I just wanted to compare some vector additions with SSE and AVX, as well as with the automatic gcc vector. To test this, I used and modified the code below.

The code:

#include <iostream>
#include <immintrin.h>

#include "../../time/timer.hpp"


void ser(double* a, double* b, double* res, int size){
 for(int i(0); i < size; i++ )
 {
    res[i] = a[i] + b[i];
 }
}

void sse(double* a, double* b, double* res, int size){
 for (int i(0); i < (size & ~0x1); i += 2 )
 {
    const __m128d kA2   = _mm_load_pd( &a[i] );
    const __m128d kB2   = _mm_load_pd( &b[i] );
    const __m128d kRes = _mm_add_pd( kA2, kB2 );
    _mm_store_pd( &res[i], kRes );
 }
}

void avx(double* a, double* b, double* res, int size){
for (int i(0); i < (size & ~0x3); i += 4 )
 {
    const __m256d kA4   = _mm256_load_pd( &a[i] );
    const __m256d kB4   = _mm256_load_pd( &b[i] );
    const __m256d kRes = _mm256_add_pd( kA4, kB4 );
    _mm256_store_pd( &res[i], kRes );
 }
}


#define N 1e7*64

int main(int argc, char const *argv[])
{ 


 double* a = (double*)_mm_malloc(N*sizeof(double), 64);
 double* b = (double*)_mm_malloc(N*sizeof(double), 64);
 double* res = (double*)_mm_malloc(N*sizeof(double), 64);

 Timer tm;

 tm.start();
 avx(a,b,res,N);
 tm.stop();
 std::cout<<"AVX\t"<<tm.elapsed()<<" ms\t"
          <<1e-6*N/tm.elapsed() <<" GFLOP/s"<<std::endl;

 tm.start();
 sse(a,b,res,N);
 tm.stop();
 std::cout<<"SSE\t"<<tm.elapsed()<<" ms\t"
          <<1e-6*N/tm.elapsed() <<" GFLOP/s"<<std::endl;

 tm.start();
 ser(a,b,res,N);
 tm.stop();
 std::cout<<"SER\t"<<tm.elapsed()<<" ms\t"
          <<1e-6*N/tm.elapsed() <<" GFLOP/s"<<std::endl;
 return 0;
}

For timings and computed GFLOP / S, I get:

./test3
AVX 1892 ms 0.338266 GFLOP/s
SSE 408  ms 1.56863 GFLOP/s
SER 396  ms 1.61616 GFLOP/s

which is clearly very slow compared to the peak performance of about 170 GFLOP / s of my i5 6600K.

Did I miss something important here? I know that adding vectors to the processor is not a good idea, but these results are very bad. Thanks for any hint.

+4
2

, . , , .

0

, ( , ?. i5-4200U 100000000 AVX, SSE, SER.

AVX 807 ms 0.123916 GFLOP/s SSE 215 ms 0.465116 GFLOP/s SER 287 ms 0.348432 GFLOP/s

SER, AVX, SSE,

SER 753 ms 0.132802 GFLOP/s AVX 225 ms 0.444444 GFLOP/s SSE 196 ms 0.510204 GFLOP/s

.

-2

Source: https://habr.com/ru/post/1667790/


All Articles