AVX标量操作要快得多
我测试了以下简单的function
void mul(double *a, double *b) { for (int i = 0; i<N; i++) a[i] *= b[i]; }
具有非常大的数组,因此它是内存带宽限制。 我使用的测试代码如下。 当我用-O2
编译时需要1.7秒。 当我用-O2 -mavx
编译时,它只需要1.0秒。 非vex编码的标量操作慢了70%! 为什么是这样?
这是-O2
和-O2 -mavx
的程序集。 <img src="http://sofzh.miximages.com/c/otliN.png" alt=" -O2
和 -O2 -mavx
的vimddif”>
https://godbolt.org/g/w4p60f
系统:i7-6700HQ@2.60GHz(Skylake)32 GB内存,Ubuntu 16.10,GCC 6.3
测试代码
//gcc -O2 -fopenmp test.c //or //gcc -O2 -mavx -fopenmp test.c #include #include #include #include #define N 1000000 #define R 1000 void mul(double *a, double *b) { for (int i = 0; i<N; i++) a[i] *= b[i]; } int main() { double *a = (double*)_mm_malloc(sizeof *a * N, 32); double *b = (double*)_mm_malloc(sizeof *b * N, 32); //b must be initialized to get the correct bandwidth!!! memset(a, 1, sizeof *a * N); memset(b, 1, sizeof *b * N); double dtime; const double mem = 3*sizeof(double)*N*R/1024/1024/1024; const double maxbw = 34.1; dtime = -omp_get_wtime(); for(int i=0; i<R; i++) mul(a,b); dtime += omp_get_wtime(); printf("time %.2f s, %.1f GB/s, efficency %.1f%%\n", dtime, mem/dtime, 100*mem/dtime/maxbw); _mm_free(a), _mm_free(b); }
在调用omp_get_wtime()
之后,问题与AVX寄存器的脏半上半部分有关。 这对Skylake处理器来说尤其严重。
我第一次读到这个问题就在这里 。 从那时起,其他人就观察到了这个问题: 这里和这里 。
使用gdb
我发现omp_get_wtime()
调用了clock_gettime
。 我重写了我的代码以使用clock_gettime()
,我看到了同样的问题。
void fix_avx() { __asm__ __volatile__ ( "vzeroupper" : : : ); } void fix_sse() { } void (*fix)(); double get_wtime() { struct timespec time; clock_gettime(CLOCK_MONOTONIC, &time); #ifndef __AVX__ fix(); #endif return time.tv_sec + 1E-9*time.tv_nsec; } void dispatch() { fix = fix_sse; #if defined(__INTEL_COMPILER) if (_may_i_use_cpu_feature (_FEATURE_AVX)) fix = fix_avx; #else #if defined(__GNUC__) && !defined(__clang__) __builtin_cpu_init(); #endif if(__builtin_cpu_supports("avx")) fix = fix_avx; #endif }
使用gdb
_dl_runtime_resolve_avx()
代码我看到第一次调用clock_gettime
它调用了_dl_runtime_resolve_avx()
。 我认为问题出在此函数的基础上。 此函数似乎仅在第一次调用clock_gettime
调用。
使用GCC,问题就会消失//__asm__ __volatile__ ( "vzeroupper" : : : );
在使用clock_gettime
进行第一次调用之后,使用Clang(使用clang -O2 -fno-vectorize
因为Clang甚至在-O2
向量化),它只会在每次调用clock_gettime
后使用它消失。
这是我用来测试它的代码(使用GCC 6.3和Clang 3.8)
#include #include #include #include void fix_avx() { __asm__ __volatile__ ( "vzeroupper" : : : ); } void fix_sse() { } void (*fix)(); double get_wtime() { struct timespec time; clock_gettime(CLOCK_MONOTONIC, &time); #ifndef __AVX__ fix(); #endif return time.tv_sec + 1E-9*time.tv_nsec; } void dispatch() { fix = fix_sse; #if defined(__INTEL_COMPILER) if (_may_i_use_cpu_feature (_FEATURE_AVX)) fix = fix_avx; #else #if defined(__GNUC__) && !defined(__clang__) __builtin_cpu_init(); #endif if(__builtin_cpu_supports("avx")) fix = fix_avx; #endif } #define N 1000000 #define R 1000 void mul(double *a, double *b) { for (int i = 0; i
如果我-z now
用-z now
禁用懒惰函数调用解析(例如clang -O2 -fno-vectorize -z now foo.c
)那么Clang只需要__asm__ __volatile__ ( "vzeroupper" : : : );
在第一次调用clock_gettime
就像GCC一样。
我希望用-z now
我只需要__asm__ __volatile__ ( "vzeroupper" : : : );
在main()
但在第一次调用clock_gettime
之后我仍然需要它。