I have a strange occurence and can't really explain that. I am trying to write some numerical codes and thus benchmark some implementations. I just wanted to benchmark some vector additions with SSE and AVX as well as gcc auto vectorization. To test that, I have used and modified the code below.
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 the timings and calculated 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 really slow compared to the peak performance of about 170 GFLOP/s of my i5 6600K.
Am I missing anything important here? I know that vector addition on a CPU is not the best idea, but these results are really bad. Thanks for any clue.