如何使用std::experimental::simd?

7
我尝试按照 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;
}

我对此一无所知,但我觉得你应该使用“-march=native”来充分发挥其优势。 - walnut
6
为什么你要使用-O0呢?没有优化,你只是在测试所有本应被优化消除掉的开销。 - walnut
2
请查看scalar_product函数在不同优化级别下的编译结果:https://godbolt.org/z/JPGPlo - walnut
好的,但即使进行了优化和本地架构构建,总体速度仍会下降,但向量化版本仍然比原来慢2-3倍。 - infinitezero
1
你能否更新你的问题,以反映启用优化的基准测试方式?因为对于问题中的代码,整个“scalar_product”循环被优化掉了(因为结果从未被使用)。剩余的时间差异(以纳秒为单位)仅仅是“high_resolution_clock”调用顺序的问题:https://godbolt.org/z/2ViiF7 - walnut
1个回答

12

问题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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接