AVX2指令集浮点乘法性能分析

这篇文章主要为大家介绍了AVX2指令集浮点乘法性能分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

一、AVX2指令集介绍

AVX2是SIMD(单指令多数据流)指令集,支持在一个指令周期内同时对256位内存进行操作。包含乘法,加法,位运算等功能。下附Intel官网使用文档。

Intel® Intrinsics Guide

我们本次要用到的指令有 **__m256 _mm256_mul_ps(__m256 a, __m256 b), __m256d_mm256_mul_pd(__m256d a, __m256d b)**等,(p代表精度precision,s代表single,d代表double)

它们可以一次取256位的内存,并按32/64位一个浮点进行乘法运算。下附官网描述。

Synopsis

__m256d _mm256_mul_pd (__m256d a, __m256d b)

#include

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

ArchitectureLatencyThroughput (CPI)
Icelake40.5
Skylake40.5
Broadwell30.5
Haswell50.5
Ivy Bridge51

二、代码实现

0. 数据生成

为了比较结果,我们用1+1e-8填充。这里利用模版兼容不同数据类型。由于AVX2指令集一次要操作多个数据,为了防止访存越界,我们将大小扩展到256的整数倍位比特,也就是32字节的整数倍。

uint64_t lowbit(uint64_t x) { return x & (-x); } uint64_t extTo2Power(uint64_t n, int i)//arraysize datasize { while(lowbit(n) 
template  T* getArray(uint64_t size) { uint64_t ExSize = extTo2Power(size, 32/sizeof(T)); T* arr = new T[ExSize]; for (uint64_t i = 0; i 

1. 普通连乘

为了比较性能差异,我们先实现一份普通连乘。这里也使用模版。

template  T simpleProduct(T* arr, uint64_t size) { T product = 1; for (uint64_t i = 0; i 

2. AVX2指令集乘法:单精度浮点(float)

这里我们预开一个avx2的整形变量,每次从数组中取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 

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 

三、性能测试

测试环境

DeviceDescription
CPUIntel Core i9-9880H 8-core 2.3GHz
MemoryDDR4-2400MHz Dual-Channel 32GB
complierApple Clang-1300.0.29.30

计时方式

利用chrono库获取系统时钟计算运行时间,精确到毫秒级

uint64_t getTime() { uint64_t timems = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); return timems; } 

测试内容

	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(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(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 

测试结果 方法耗时(ms)AVX2乘法 单精度57普通乘法 单精度232AVX2乘法 双精度121普通乘法 双精度243

这里能看到单精度下已经出现了比较明显的误差,同时由于CPU内部没有普通的单精度浮点运算器,所以单精度运算和双精度耗时所差无几。

第二次测试

测试命令

现在我们再开启O2编译优化试一试:

g++ -O2 -mavx2 avx_product.cpp ./a.out 

测试结果

方法耗时(ms)
AVX2乘法 单精度19
普通乘法 单精度102
AVX2乘法 双精度44
普通乘法 双精度129

四、总结

经过几次测试,我们可以大概得出,AVX指令集在浮点的运算上有比较高的性能,而整形运算的提升则没那么明显,同时AVX2执行一次运算大致会消耗双精度运算2倍的时间,所以如果需要运算的数据小于2个,则用AVX2得不到提升。

个人猜测原因:

  • CPU内部整形运算器多于浮点运算器,所以启用优化时整形普通运算能得到更多提升。
  • AVX2指令集专门针对浮点型进行过优化。使得运算逻辑门的关键路径长度小于普通浮点运算。

以上就是AVX2指令集浮点乘法性能分析的详细内容,更多关于AVX2指令集浮点乘法的资料请关注0133技术站其它相关文章!

以上就是AVX2指令集浮点乘法性能分析的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言