AVX2指令集浮點乘法性能分析
一、AVX2指令集介紹
AVX2是SIMD(單指令多數(shù)據(jù)流)指令集,支持在一個指令周期內(nèi)同時對256位內(nèi)存進行操作。包含乘法,加法,位運算等功能。下附Intel官網(wǎng)使用文檔。
我們本次要用到的指令有 **__m256 _mm256_mul_ps(__m256 a, __m256 b), __m256d_mm256_mul_pd(__m256d a, __m256d b)**等,(p代表精度precision,s代表single,d代表double)
它們可以一次取256位的內(nèi)存,并按32/64位一個浮點進行乘法運算。下附官網(wǎng)描述。
Synopsis
__m256d _mm256_mul_pd (__m256d a, __m256d b)
#include <immintrin.h>
Instruction: vmulpd ymm, ymm, ymm
CPUID Flags: AVX
Description
Multiply packed double-precision (64-bit) floating-point elements in a and b, and store the results in dst.
Operation
FOR j := 0 to 3 i := j*64 dst[i+63:i] := a[i+63:i] * b[i+63:i] ENDFOR dst[MAX:256] := 0
Performance
| Architecture | Latency | Throughput (CPI) |
|---|---|---|
| Icelake | 4 | 0.5 |
| Skylake | 4 | 0.5 |
| Broadwell | 3 | 0.5 |
| Haswell | 5 | 0.5 |
| Ivy Bridge | 5 | 1 |
二、代碼實現(xiàn)
0. 數(shù)據(jù)生成
為了比較結(jié)果,我們用1+1e-8填充。這里利用模版兼容不同數(shù)據(jù)類型。由于AVX2指令集一次要操作多個數(shù)據(jù),為了防止訪存越界,我們將大小擴展到256的整數(shù)倍位比特,也就是32字節(jié)的整數(shù)倍。
uint64_t lowbit(uint64_t x)
{
return x & (-x);
}
uint64_t extTo2Power(uint64_t n, int i)//arraysize datasize
{
while(lowbit(n) < i)
n += lowbit(n);
return n;
}
template <typename T>
T* getArray(uint64_t size)
{
uint64_t ExSize = extTo2Power(size, 32/sizeof(T));
T* arr = new T[ExSize];
for (uint64_t i = 0; i < size; i++)
arr[i] = 1.0+1e-8;
for (uint64_t i = size; i < ExSize; i++)
arr[i] = 1.0;
return arr;
}
}
1. 普通連乘
為了比較性能差異,我們先實現(xiàn)一份普通連乘。這里也使用模版。
template <typename T>
T simpleProduct(T* arr, uint64_t size)
{
T product = 1;
for (uint64_t i = 0; i < size; i++)
product *= arr[i];
return product;
}
2. AVX2指令集乘法:單精度浮點(float)
這里我們預開一個avx2的整形變量,每次從數(shù)組中取8個32位浮點,乘到這個變量上,最后在對這8個32位浮點進行連乘。
float avx2Product(float* arr, uint64_t size)
{
float product[8] = {1};
__m256 product256 = _mm256_setr_ps(1, 1, 1, 1, 1, 1, 1, 1);
__m256 load256 = _mm256_setzero_ps();
for (uint64_t i = 0; i < size; i += 8)
{
load256 = _mm256_loadu_ps(&arr[i]);
product256 = _mm256_mul_ps(product256, load256);
}
_mm256_storeu_ps(product, product256);
product[0] *= product[1] * product[2] * product[3] * product[4] * product[5] * product[6] * product[7];
return product[0];
}
3. AVX2指令集乘法:雙精度浮點(double)
double avx2Product(double* arr, uint64_t size)
{
double product[4] = {1};
__m256d product256 = _mm256_setr_pd(1, 1, 1, 1);
__m256d load256 = _mm256_setzero_pd();
for (uint64_t i = 0; i < size; i += 4)
{
load256 = _mm256_loadu_pd(&arr[i]);
product256 = _mm256_mul_pd(product256, load256);
}
_mm256_storeu_pd(product, product256);
product[0] *= product[1] * product[2] * product[3];
return product[0];
}
三、性能測試
測試環(huán)境
| Device | Description |
|---|---|
| CPU | Intel Core i9-9880H 8-core 2.3GHz |
| Memory | DDR4-2400MHz Dual-Channel 32GB |
| complier | Apple Clang-1300.0.29.30 |
計時方式
利用chrono庫獲取系統(tǒng)時鐘計算運行時間,精確到毫秒級
uint64_t getTime()
{
uint64_t timems = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
return timems;
}
測試內(nèi)容

uint64_t N = 1e8;
// compare the performance of simpleProduct and avx2Product
uint64_t start, end;
//compare float
cout << "compare float product" << endl;
float* arr = getArray<float>(N);
start = getTime();
float simpleProductResult = simpleProduct(arr, N);
end = getTime();
cout << "Simple product: " << simpleProductResult << endl;
cout << "Time: " << end - start << " ms" << endl;
cout << endl;
start = getTime();
float avx2ProductResult = avx2Product(arr, N);
end = getTime();
cout << "AVX2 product: " << avx2ProductResult << endl;
cout << "Time: " << end - start << " ms" << endl;
cout << endl;
delete[] arr;
//compare double
cout << "compare double product" << endl;
double* arr2 = getArray<double>(N);
start = getTime();
double simpleProductResult2 = simpleProduct(arr2, N);
end = getTime();
cout << "Simple product: " << simpleProductResult2 << endl;
cout << "Time: " << end - start << " ms" << endl;
cout << endl;
start = getTime();
double avx2ProductResult2 = avx2Product(arr2, N);
end = getTime();
cout << "AVX2 product: " << avx2ProductResult2 << endl;
cout << "Time: " << end - start << " ms" << endl;
cout << endl;
delete[] arr2;
進行性能測試
第一次測試
測試命令
g++ -mavx2 avx_product.cpp ./a.out
測試結(jié)果 方法耗時(ms)AVX2乘法 單精度57普通乘法 單精度232AVX2乘法 雙精度121普通乘法 雙精度243

這里能看到單精度下已經(jīng)出現(xiàn)了比較明顯的誤差,同時由于CPU內(nèi)部沒有普通的單精度浮點運算器,所以單精度運算和雙精度耗時所差無幾。
第二次測試
測試命令
現(xiàn)在我們再開啟O2編譯優(yōu)化試一試:
g++ -O2 -mavx2 avx_product.cpp ./a.out
測試結(jié)果
| 方法 | 耗時(ms) |
|---|---|
| AVX2乘法 單精度 | 19 |
| 普通乘法 單精度 | 102 |
| AVX2乘法 雙精度 | 44 |
| 普通乘法 雙精度 | 129 |

四、總結(jié)
經(jīng)過幾次測試,我們可以大概得出,AVX指令集在浮點的運算上有比較高的性能,而整形運算的提升則沒那么明顯,同時AVX2執(zhí)行一次運算大致會消耗雙精度運算2倍的時間,所以如果需要運算的數(shù)據(jù)小于2個,則用AVX2得不到提升。
個人猜測原因:
- CPU內(nèi)部整形運算器多于浮點運算器,所以啟用優(yōu)化時整形普通運算能得到更多提升。
- AVX2指令集專門針對浮點型進行過優(yōu)化。使得運算邏輯門的關鍵路徑長度小于普通浮點運算。
以上就是AVX2指令集浮點乘法性能分析的詳細內(nèi)容,更多關于AVX2指令集浮點乘法的資料請關注腳本之家其它相關文章!
相關文章
Qt中QMapIterator檢測是否為空的實現(xiàn)
本文主要介紹了Qt中QMapIterator檢測是否為空的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-10-10
OpenCV中的cv::Mat函數(shù)將數(shù)據(jù)寫入txt文件
這篇文章主要介紹了OpenCVcv::Mat中的數(shù)據(jù)按行列寫入txt文件中,需要的朋友可以參考下2018-05-05

