使用AVX指令的程序非常慢

时间:2013-06-05 21:45:00

标签: sse simd icc avx floating-point-exceptions

我正在尝试使用AVX内在函数编写几何平均值sqrt(a * b),但它比糖蜜运行速度慢!

int main()
{
  int count = 0;
  for (int i = 0; i < 100000000; ++i)
  {
    __m128i v8n_a = _mm_set1_epi16((++count) % 16),
            v8n_b = _mm_set1_epi16((++count) % 16);
    __m128i v8n_0 = _mm_set1_epi16(0);
    __m256i temp1, temp2;
    __m256 v8f_a = _mm256_cvtepi32_ps(temp1 = _mm256_insertf128_si256(_mm256_castsi128_si256(_mm_unpacklo_epi16(v8n_a, v8n_0)), _mm_unpackhi_epi16(v8n_a, v8n_0), 1)),
    v8f_b = _mm256_cvtepi32_ps(temp2 = _mm256_insertf128_si256(_mm256_castsi128_si256(_mm_unpacklo_epi16(v8n_b, v8n_0)), _mm_unpackhi_epi16(v8n_b, v8n_0), 1));
    __m256i v8n_meanInt32 = _mm256_cvtps_epi32(_mm256_sqrt_ps(_mm256_mul_ps(v8f_a, v8f_b)));
    __m128i v4n_meanLo = _mm256_castsi256_si128(v8n_meanInt32),
       v4n_meanHi = _mm256_extractf128_si256(v8n_meanInt32, 1);
    g_data[i % 8] = v4n_meanLo;
    g_data[(i + 1) % 8] = v4n_meanHi;
  }
  return 0;
}

这个谜团的关键是我正在使用英特尔ICC 11,并且在使用icc -O3 sqrt.cpp进行编译时速度很慢。如果我使用icc -O3 -xavx sqrt.cpp进行编译,那么它的运行速度要快10倍。

但是,由于我使用了性能计数器并且两个版本执行的指令数大致为4G,因此仿真发生并不明显:

 Performance counter stats for 'a.out':

  16867.119538 task-clock                #    0.999 CPUs utilized
            37 context-switches          #    0.000 M/sec
             8 CPU-migrations            #    0.000 M/sec
           281 page-faults               #    0.000 M/sec
35,463,758,996 cycles                    #    2.103 GHz
23,690,669,417 stalled-cycles-frontend   #   66.80% frontend cycles idle
20,846,452,415 stalled-cycles-backend    #   58.78% backend  cycles idle
 4,023,012,964 instructions              #    0.11  insns per cycle
                                         #    5.89  stalled cycles per insn
   304,385,109 branches                  #   18.046 M/sec
        42,636 branch-misses             #    0.01% of all branches

  16.891160582 seconds time elapsed

----------------------------------- with -xavx --------- -------------------------------

 Performance counter stats for 'a.out':

   1288.423505 task-clock                #    0.996 CPUs utilized
             3 context-switches          #    0.000 M/sec
             2 CPU-migrations            #    0.000 M/sec
           279 page-faults               #    0.000 M/sec
 2,708,906,702 cycles                    #    2.102 GHz
 1,608,134,568 stalled-cycles-frontend   #   59.36% frontend cycles idle
   798,177,722 stalled-cycles-backend    #   29.46% backend  cycles idle
 3,803,270,546 instructions              #    1.40  insns per cycle
                                         #    0.42  stalled cycles per insn
   300,601,809 branches                  #  233.310 M/sec
        15,167 branch-misses             #    0.01% of all branches

   1.293986790 seconds time elapsed

是否有某种处理器内部仿真正在进行?我知道对于非正规数字,加起来比正常情况慢64倍。

1 个答案:

答案 0 :(得分:1)

混合VEX和非VEX矢量指令时需要vzeroupper。否则,您将在英特尔硬件上获得巨大的停顿。