![](/img/trans.png)
[英]Using OpenMP threads and std::(experimental::)simd to compute the Mandelbrot set
[英]How to use std::experimental::simd?
我嘗試執行github std::simd上給出的示例,但我的矢量化版本最終慢了 2-3 倍。 如何正確使用?
該紀錄片缺乏嚴肅的文檔和進一步的示例用法。 沒有列出構造函數等。我確定我可能以錯誤的方式使用它,但是由於文檔有限,我不知道如何繼續。
g++ -o test test.cpp --std=c++2a -O0
#include <array>
#include <chrono>
#include <cstdlib>
#include <experimental/simd>
#include <iostream>
#include <random>
using std::experimental::native_simd;
using Vec3D_v = std::array<native_simd<float>, 3>;
native_simd<float> scalar_product(const Vec3D_v& a, const Vec3D_v& b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
using Vec3D = std::array<float, 3>;
float scalar_product(const std::array<float, 3>& a, const std::array<float, 3>& b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
int main(){
constexpr std::size_t VECREG_SIZE = native_simd<float>::size();
std::array<Vec3D, VECREG_SIZE * 1000> arr;
std::array<Vec3D_v, VECREG_SIZE * 1000> arr_v;
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_real_distribution<float> distribution(0.f, 1.f);
for( std::size_t i = 0; i < arr.size(); ++i ){
arr[i] = {distribution(generator), distribution(generator), distribution(generator)};
arr_v[i] = {distribution(generator), distribution(generator), distribution(generator)};
}
float result = 0.f;
auto start = std::chrono::high_resolution_clock::now();
for( std::size_t i = 1; i < arr.size(); ++i ){
result += scalar_product(arr_v[i-1], arr_v[i])[0];
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = end - start;
std::cout << "VC: " << elapsed.count() << '\n' << std::endl;
result = 0;
start = std::chrono::high_resolution_clock::now();
for( std::size_t i = 1; i < arr.size(); ++i ){
result += scalar_product(arr[i-1], arr[i]);
}
end = std::chrono::high_resolution_clock::now();
elapsed = end - start;
std::cout << "notVC: " << elapsed.count() << '\n';
return EXIT_SUCCESS;
}
問題 1:使用 SIMD 指令時存在初始成本。 獲取您的代碼,並將其循環三遍(我使用-O3
編譯,並打印result
,否則大多數代碼將被刪除):
$ ./test
VC: 37240 (result: 5986.1)
notVC: 18668 (result: 5983.29)
VC: 26177 (result: 5986.1)
notVC: 18516 (result: 5983.29)
VC: 25895 (result: 5986.1)
notVC: 18083 (result: 5983.29)
_v
版本的主循環程序集現在顯示為:
1840: c5 fc 28 d5 vmovaps %ymm5,%ymm2
1844: c5 fc 28 28 vmovaps (%rax),%ymm5
1848: c5 fc 28 cc vmovaps %ymm4,%ymm1
184c: c5 fc 28 c3 vmovaps %ymm3,%ymm0
1850: c5 fc 28 60 20 vmovaps 0x20(%rax),%ymm4
1855: c5 fc 28 58 40 vmovaps 0x40(%rax),%ymm3
185a: 48 83 c0 60 add $0x60,%rax
185e: c5 d4 59 d2 vmulps %ymm2,%ymm5,%ymm2
1862: c4 e2 6d 98 cc vfmadd132ps %ymm4,%ymm2,%ymm1
1867: c4 e2 75 98 c3 vfmadd132ps %ymm3,%ymm1,%ymm0
186c: c5 ca 58 f0 vaddss %xmm0,%xmm6,%xmm6
1870: 48 39 d8 cmp %rbx,%rax
1873: 75 cb jne 1840 <main+0x6f0>
問題 2:在循環的每一輪,您使用[0]
運算符將native_simd<float>
結果轉換為float
。 這可能會產生可怕的后果——但編譯器足夠聰明,不會這樣做,如上面的程序集所示。
問題 3:正如我們所見, native
只是指示編譯器將值放入 SIMD 寄存器。 這樣做並沒有太大的收獲:這里的多數據方面在哪里? 您要做的是將 3D 向量打包到單個 SIMD 寄存器中,然后重寫循環以將標量積的每個維度累積在一個組件中。 最后,您將獲取所有組件的總和:
using std::experimental::fixed_size_simd;
using Vec3D_v = fixed_size_simd<float, 3>;
和
for( std::size_t i = 1; i < arr.size(); ++i ){
result_v += arr_v[i-1] * arr_v[i];
}
float result = std::experimental::reduce (result_v);
運行這個,我們有:
$ ./test
VC: 14958 (result: 2274.7)
notVC: 5279 (result: 2274.7)
VC: 4718 (result: 2274.7)
notVC: 5177 (result: 2274.7)
VC: 4720 (result: 2274.7)
notVC: 5132 (result: 2274.7)
主循環的組裝就是那個漂亮的部分:
1588: c5 f8 28 d0 vmovaps %xmm0,%xmm2
158c: c5 f8 28 00 vmovaps (%rax),%xmm0
1590: 48 83 c0 10 add $0x10,%rax
1594: c4 e2 79 b8 ca vfmadd231ps %xmm2,%xmm0,%xmm1
1599: 48 39 c3 cmp %rax,%rbx
159c: 75 ea jne 1588 <main+0x438>
在這里,每個%xmm
寄存器一次保存 3 個浮點值。 此外,編譯器對第二個循環進行了大量優化以使用 AVX 指令,因此增益並不是那么重要(但仍然存在)。
完整代碼:
#include <array>
#include <chrono>
#include <cstdlib>
#include <experimental/simd>
#include <iostream>
#include <random>
using std::experimental::fixed_size_simd;
using Vec3D_v = fixed_size_simd<float, 3>;
using Vec3D = std::array<float, 3>;
float scalar_product (const std::array<float, 3> &a, const std::array<float, 3> &b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
int main () {
constexpr std::size_t VECREG_SIZE = fixed_size_simd<float, 3>::size ();
std::array<Vec3D, VECREG_SIZE * 1000> arr;
std::array<Vec3D_v, VECREG_SIZE * 1000> arr_v;
std::random_device rd;
std::mt19937 generator (rd ());
std::uniform_real_distribution<float> distribution (0.f, 1.f);
for (std::size_t i = 0; i < arr.size (); ++i) {
arr[i] = {distribution (generator), distribution (generator), distribution (generator) };
for (int j = 0; j < 3; ++j)
arr_v[i][j] = arr[i][j];
}
Vec3D_v result_v;
for (int iter = 0; iter < 3; ++iter) {
for (int j = 0; j < 3; ++j)
result_v[j] = 0.f;
auto start = std::chrono::high_resolution_clock::now ();
for (std::size_t i = 1; i < arr.size (); ++i) {
result_v += arr_v[i - 1] * arr_v[i];
}
float result = std::experimental::reduce (result_v);
auto end = std::chrono::high_resolution_clock::now ();
auto elapsed = end - start;
std::cout << "VC: " << elapsed.count () << " (result: " << result << ")" << std::endl;
result = 0;
start = std::chrono::high_resolution_clock::now ();
for (std::size_t i = 1; i < arr.size (); ++i) {
result += scalar_product (arr[i - 1], arr[i]);
}
end = std::chrono::high_resolution_clock::now ();
elapsed = end - start;
std::cout << "notVC: " << elapsed.count () << " (result: " << result << ")" << std::endl;
}
return EXIT_SUCCESS;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.