7

我试着做github std::simd上给出的例子,但我的矢量化版本最终慢了 2-3 倍。如何正确使用?

文档似乎缺乏,没有足够的例子。没有列出构造函数等。我确定我可能以错误的方式使用它,但是由于文档有限,我不知道如何继续。

g++ -o 测试 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;
}
4

1 回答 1

8

问题 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:在循环的每一轮,您使用运算符将native_simd<float>​​结果转换为 a 。这可能会产生可怕的后果——但编译器足够聪明,不会这样做,如上面的程序集所示。float[0]

问题 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;
}
于 2020-11-23T23:11:15.883 回答