AVX标量操作要快得多

时间:2017-04-06 13:21:50

标签: c memory x86 sse avx

我测试以下简单函数

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的汇编。 vimddif of <code>-O2</code> and <code>-O2 -mavx</code>

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 <string.h>
#include <stdio.h>
#include <x86intrin.h>
#include <omp.h>

#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);
}

1 个答案:

答案 0 :(得分:5)

问题与调用omp_get_wtime()后AVX寄存器的上半部分有关。这对Skylake处理器来说尤其严重。

我第一次读到这个问题是here。从那时起,其他人就发现了这个问题:herehere

使用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单步执行代码我看到第一次调用clock_gettime时调用_dl_runtime_resolve_avx()。我认为问题出在这个基于this comment的函数中。此函数似乎仅在第一次调用clock_gettime时调用。

使用GCC后问题会在第一次使用//__asm__ __volatile__ ( "vzeroupper" : : : );之后使用clock_gettime消失;但是对于Clang(使用clang -O2 -fno-vectorize,因为即使在-O2时Clang矢量化)它也会消失每次致电clock_gettime后使用它。

以下是我用来测试它的代码(使用GCC 6.3和Clang 3.8)

#include <string.h>
#include <stdio.h>
#include <x86intrin.h>
#include <time.h>

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<N; i++) a[i] *= b[i];
}

int main() {
  dispatch();
  const double mem = 3*sizeof(double)*N*R/1024/1024/1024;
  const double maxbw = 34.1;

  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;
  //dtime = get_wtime(); // call once to fix GCC
  //printf("%f\n", dtime);
  //fix = fix_sse;

  dtime = -get_wtime();
  for(int i=0; i<R; i++) mul(a,b);
  dtime += 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);
}

如果我使用-z now禁用懒惰函数调用解析(例如clang -O2 -fno-vectorize -z now foo.c),则在第一次调用__asm__ __volatile__ ( "vzeroupper" : : : );后,Clang只需要clock_gettime,就像GCC一样。

我希望-z now __asm__ __volatile__ ( "vzeroupper" : : : );我只需main() clock_gettime,但在第一次调用patients后我仍然需要readtable