欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AVX2指令集浮點(diǎn)乘法性能分析

 更新時(shí)間:2022年05月18日 15:09:00   作者:concyclics  
這篇文章主要為大家介紹了AVX2指令集浮點(diǎn)乘法性能分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

一、AVX2指令集介紹

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

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位的內(nèi)存,并按32/64位一個(gè)浮點(diǎn)進(jìn)行乘法運(yùn)算。下附官網(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

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

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

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

為了比較結(jié)果,我們用1+1e-8填充。這里利用模版兼容不同數(shù)據(jù)類型。由于AVX2指令集一次要操作多個(gè)數(shù)據(jù),為了防止訪存越界,我們將大小擴(kuò)展到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. 普通連乘

為了比較性能差異,我們先實(shí)現(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指令集乘法:?jiǎn)尉雀↑c(diǎn)(float)

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

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指令集乘法:雙精度浮點(diǎn)(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];
}

三、性能測(cè)試

測(cè)試環(huán)境

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

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

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

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

測(cè)試內(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;

進(jìn)行性能測(cè)試

第一次測(cè)試

測(cè)試命令

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

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

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

第二次測(cè)試

測(cè)試命令

現(xiàn)在我們?cè)匍_(kāi)啟O2編譯優(yōu)化試一試:

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

測(cè)試結(jié)果

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

四、總結(jié)

經(jīng)過(guò)幾次測(cè)試,我們可以大概得出,AVX指令集在浮點(diǎn)的運(yùn)算上有比較高的性能,而整形運(yùn)算的提升則沒(méi)那么明顯,同時(shí)AVX2執(zhí)行一次運(yùn)算大致會(huì)消耗雙精度運(yùn)算2倍的時(shí)間,所以如果需要運(yùn)算的數(shù)據(jù)小于2個(gè),則用AVX2得不到提升。

個(gè)人猜測(cè)原因:

  • CPU內(nèi)部整形運(yùn)算器多于浮點(diǎn)運(yùn)算器,所以啟用優(yōu)化時(shí)整形普通運(yùn)算能得到更多提升。
  • AVX2指令集專門(mén)針對(duì)浮點(diǎn)型進(jìn)行過(guò)優(yōu)化。使得運(yùn)算邏輯門(mén)的關(guān)鍵路徑長(zhǎng)度小于普通浮點(diǎn)運(yùn)算。

以上就是AVX2指令集浮點(diǎn)乘法性能分析的詳細(xì)內(nèi)容,更多關(guān)于AVX2指令集浮點(diǎn)乘法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C++中const應(yīng)放在類型前還是后

    C++中const應(yīng)放在類型前還是后

    之前遇到小伙伴問(wèn)C++中const加在類型名前和變量名前的區(qū)別,今天給大家簡(jiǎn)單分析下。
    2016-05-05
  • C++實(shí)現(xiàn)多人聊天室

    C++實(shí)現(xiàn)多人聊天室

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)多人聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Qt中QMapIterator檢測(cè)是否為空的實(shí)現(xiàn)

    Qt中QMapIterator檢測(cè)是否為空的實(shí)現(xiàn)

    本文主要介紹了Qt中QMapIterator檢測(cè)是否為空的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-10-10
  • OpenCV中的cv::Mat函數(shù)將數(shù)據(jù)寫(xiě)入txt文件

    OpenCV中的cv::Mat函數(shù)將數(shù)據(jù)寫(xiě)入txt文件

    這篇文章主要介紹了OpenCVcv::Mat中的數(shù)據(jù)按行列寫(xiě)入txt文件中,需要的朋友可以參考下
    2018-05-05
  • Qt線程池QThreadPool的使用詳解

    Qt線程池QThreadPool的使用詳解

    本文主要介紹了Qt線程池QThreadPool的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • C語(yǔ)言實(shí)現(xiàn)通訊錄

    C語(yǔ)言實(shí)現(xiàn)通訊錄

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)通訊錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • QT基于TCP網(wǎng)絡(luò)聊天室

    QT基于TCP網(wǎng)絡(luò)聊天室

    這篇文章主要為大家詳細(xì)介紹了QT基于TCP網(wǎng)絡(luò)聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 基礎(chǔ)C語(yǔ)言編程時(shí)易犯錯(cuò)誤有哪些

    基礎(chǔ)C語(yǔ)言編程時(shí)易犯錯(cuò)誤有哪些

    基礎(chǔ)C語(yǔ)言編程時(shí)易犯錯(cuò)誤有哪些?這篇文章主要介紹了C語(yǔ)言編程時(shí)常見(jiàn)的錯(cuò)誤,感興趣的小伙伴們可以參考一下
    2016-11-11
  • C++在非面向?qū)ο蠓矫鎸?duì)C語(yǔ)言的擴(kuò)充

    C++在非面向?qū)ο蠓矫鎸?duì)C語(yǔ)言的擴(kuò)充

    C++是一種面向?qū)ο缶幊陶Z(yǔ)言,但它也可以作為C語(yǔ)言的擴(kuò)展語(yǔ)言。在C++中,我們可以使用非面向?qū)ο蠓矫娴奶匦詠?lái)擴(kuò)展C語(yǔ)言。在本文中,我們將討論C++在非面向?qū)ο蠓矫鎸?duì)C語(yǔ)言的擴(kuò)充
    2023-05-05
  • C++中的map使用方法詳解

    C++中的map使用方法詳解

    C++中的map是一種關(guān)聯(lián)容器,用于存儲(chǔ)鍵值對(duì)。它提供了一種非常高效的方法來(lái)快速查找特定的值,并且允許我們根據(jù)鍵來(lái)排序和遍歷數(shù)據(jù)。在本文中,我們將深入了解C++中的map以及如何使用它來(lái)提高程序的效率,感興趣的朋友可以參考下
    2023-05-05

最新評(píng)論