AVX2指令集浮點(diǎn)乘法性能分析_第1頁
AVX2指令集浮點(diǎn)乘法性能分析_第2頁
AVX2指令集浮點(diǎn)乘法性能分析_第3頁
AVX2指令集浮點(diǎn)乘法性能分析_第4頁
AVX2指令集浮點(diǎn)乘法性能分析_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第AVX2指令集浮點(diǎn)乘法性能分析目錄一、AVX2指令集介紹SynopsisDescriptionOperationPerformance二、代碼實(shí)現(xiàn)0.數(shù)據(jù)生成1.普通連乘2.AVX2指令集乘法:單精度浮點(diǎn)(float)3.AVX2指令集乘法:雙精度浮點(diǎn)(double)三、性能測試測試環(huán)境計(jì)時(shí)方式測試內(nèi)容進(jìn)行性能測試第一次測試第二次測試四、總結(jié)

一、AVX2指令集介紹

AVX2是SIMD(單指令多數(shù)據(jù)流)指令集,支持在一個(gè)指令周期內(nèi)同時(shí)對256位內(nèi)存進(jìn)行操作。包含乘法,加法,位運(yùn)算等功能。下附Intel官網(wǎng)使用文檔。

IntelIntrinsicsGuide

我們本次要用到的指令有**__m256_mm256_mul_ps(__m256a,__m256b),__m256d_mm256_mul_pd(__m256da,__m256db)**等,(p代表精度precision,s代表single,d代表double)

它們可以一次取256位的內(nèi)存,并按32/64位一個(gè)浮點(diǎn)進(jìn)行乘法運(yùn)算。下附官網(wǎng)描述。

Synopsis

__m256d_mm256_mul_pd(__m256da,__m256db)

Instruction:vmulpdymm,ymm,ymm

CPUIDFlags:AVX

Description

Multiplypackeddouble-precision(64-bit)floating-pointelementsinaandb,andstoretheresultsindst.

Operation

FORj:=0to3

i:=j*64

dst[i+63:i]:=a[i+63:i]*b[i+63:i]

ENDFOR

dst[MAX:256]:=0

Performance

ArchitectureLatencyThroughput(CPI)Icelake40.5Skylake40.5Broadwell30.5Haswell50.5IvyBridge51

二、代碼實(shí)現(xiàn)

0.數(shù)據(jù)生成

為了比較結(jié)果,我們用1+1e-8填充。這里利用模版兼容不同數(shù)據(jù)類型。由于AVX2指令集一次要操作多個(gè)數(shù)據(jù),為了防止訪存越界,我們將大小擴(kuò)展到256的整數(shù)倍位比特,也就是32字節(jié)的整數(shù)倍。

uint64_tlowbit(uint64_tx)

returnx(-x);

uint64_textTo2Power(uint64_tn,inti)//arraysizedatasize

while(lowbit(n)i)

n+=lowbit(n);

returnn;

templatetypenameT

T*getArray(uint64_tsize)

uint64_tExSize=extTo2Power(size,32/sizeof(T));

T*arr=newT[ExSize];

for(uint64_ti=0;isize;i++)

arr[i]=1.0+1e-8;

for(uint64_ti=size;iExSize;i++)

arr[i]=1.0;

returnarr;

1.普通連乘

為了比較性能差異,我們先實(shí)現(xiàn)一份普通連乘。這里也使用模版。

templatetypenameT

TsimpleProduct(T*arr,uint64_tsize)

Tproduct=1;

for(uint64_ti=0;isize;i++)

product*=arr[i];

returnproduct;

2.AVX2指令集乘法:單精度浮點(diǎn)(float)

這里我們預(yù)開一個(gè)avx2的整形變量,每次從數(shù)組中取8個(gè)32位浮點(diǎn),乘到這個(gè)變量上,最后在對這8個(gè)32位浮點(diǎn)進(jìn)行連乘。

floatavx2Product(float*arr,uint64_tsize)

floatproduct[8]={1};

__m256product256=_mm256_setr_ps(1,1,1,1,1,1,1,1);

__m256load256=_mm256_setzero_ps();

for(uint64_ti=0;isize;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];

returnproduct[0];

3.AVX2指令集乘法:雙精度浮點(diǎn)(double)

doubleavx2Product(double*arr,uint64_tsize)

doubleproduct[4]={1};

__m256dproduct256=_mm256_setr_pd(1,1,1,1);

__m256dload256=_mm256_setzero_pd();

for(uint64_ti=0;isize;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];

returnproduct[0];

三、性能測試

測試環(huán)境

DeviceDescriptionCPUIntelCorei9-9880H8-core2.3GHzMemoryDDR4-2400MHzDual-Channel32GBcomplierAppleClang-1300.0.29.30

計(jì)時(shí)方式

利用chrono庫獲取系統(tǒng)時(shí)鐘計(jì)算運(yùn)行時(shí)間,精確到毫秒級

uint64_tgetTime()

uint64_ttimems=std::chrono::duration_caststd::chrono::milliseconds(std::chrono::system_clock::now().time_since_epoch()).count();

returntimems;

測試內(nèi)容

uint64_tN=1e8;

//comparetheperformanceofsimpleProductandavx2Product

uint64_tstart,end;

//comparefloat

cout"comparefloatproduct"endl;

float*arr=getArrayfloat

start=getTime();

floatsimpleProductResult=simpleProduct(arr,N);

end=getTime();

cout"Simpleproduct:"simpleProductResultendl;

cout"Time:"end-start"ms"endl;

coutendl;

start=getTime();

floatavx2ProductResult=avx2Product(arr,N);

end=getTime();

cout"AVX2product:"avx2ProductResultendl;

cout"Time:"end-start"ms"endl;

coutendl;

delete[]arr;

//comparedouble

cout"comparedoubleproduct"endl;

double*arr2=getArraydouble

start=getTime();

doublesimpleProductResult2=simpleProduct(arr2,N);

end=getTime();

cout"Simpleproduct:"simpleProductResult2endl;

cout"Time:"end-start"ms"endl;

coutendl;

start=getTime();

doubleavx2ProductResult2=avx2Product(arr2,N);

end=getTime();

cout"AVX2product:"avx2ProductResult2endl;

cout"Time:"end-start"ms"endl;

coutendl;

delete[]arr2;

進(jìn)行性能測試

第一次測試

測試命令

g++-mavx2avx_product.cpp

./a.out

測試結(jié)果方法耗時(shí)(ms)AVX2乘法單精度57普通乘法單精度232AVX2乘法雙精度121普通乘法雙精度243

這里能看到單精度下已經(jīng)出現(xiàn)了比較明顯的誤差,同時(shí)由于CPU內(nèi)部沒有普通的單精度浮點(diǎn)運(yùn)算器,所以單精度運(yùn)算和雙精度耗時(shí)所差無幾。

第二次測試

測試命令

現(xiàn)在我們再開啟O2編譯優(yōu)化試一試:

g++-O2-mavx2avx_product.cpp

./a.out

測試結(jié)果

方法耗時(shí)(ms)AVX2乘法單精度19普通乘法單精度102AVX2乘法雙精度44普通乘法雙精度129

四、總結(jié)

經(jīng)過幾次測試,我們可以大概得出,AV

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論