2013-06-05 28 views
1

我正在嘗試使用AVX intrinsics編寫幾何平均sqrt(a * b),但它的運行速度比糖蜜慢!使用AVX指令的程序非常慢

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倍。

+0

你測試哪個處理器? –

+0

Xeon E2658(Sandy Bridge) –

+3

看起來你有AVX-SSE轉換問題 –

回答

1

混合VEX和非VEX矢量指令時需要vzeroupper。否則,您會在英特爾硬件上出現巨大的停頓。

+2

根據Agner vzeroupper與Skylake沒有必要。事實上在騎士登陸它可能是壞的使用。請參閱https://software.intel.com/en-us/forums/intel-isa-extensions/topic/704023和http://www.agner.org/optimize/blog/read.php?i=761 –