gpt4 book ai didi

c++ - 为什么增加阵列对齐会降低性能?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:36 24 4
gpt4 key购买 nike

我正在尝试将综合测试中的阵列对齐从 16 增加到 32,性能从 ~4100ms 降低到 ~4600ms。更高的对齐度会对性能造成多大的损害?

下面是我用于测试的代码(我在这里尝试使用 avx 指令)。使用 g++ test.cpp -O2 -ftree-vectorize -mavx2 构建(我不支持 avx512)。

#include <chrono>
#include <iostream>
#include <memory>
#include <cassert>
#include <cstring>
#include <cstdlib>

using Time = std::chrono::time_point<std::chrono::system_clock>;
using Clock = std::chrono::system_clock;

template <typename Duration>
auto as_ms(Duration const& duration) {
return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
}

static const int repeats = 10000;

struct I {
static const int size = 524288;
int* pos;
I() : pos(new int[size]) { for (int i = 0; i != size; ++i) { pos[i] = i; } }
~I() { delete pos; }
};

static const int align = 16; // try to change here 16 (4100 ms) / 32 (4600 ms)

struct S {
static const int size = I::size;
alignas(align) float data[size];
S() { for (int i = 0; i != size; ++i) { data[i] = (i * 7 + 11) % 2; } }
};

void foo(const I& p, S& a, S& b) {
const int chunk = 32;
alignas(align) float aprev[chunk];
alignas(align) float anext[chunk];
alignas(align) float bprev[chunk];
alignas(align) float bnext[chunk];
const int N = S::size / chunk;
for (int j = 0; j != repeats; ++j) {
for (int i = 1; i != N-1; i++) {
int ind = p.pos[i] * chunk;
std::memcpy(aprev, &a.data[ind-1], sizeof(float) * chunk);
std::memcpy(anext, &a.data[ind+1], sizeof(float) * chunk);
std::memcpy(bprev, &b.data[ind-1], sizeof(float) * chunk);
std::memcpy(bnext, &b.data[ind+1], sizeof(float) * chunk);
for (int k = 0; k < chunk; ++k) {
int ind0 = ind + k;
a.data[ind0] = (b.data[ind0] - 1.0f) * aprev[k] * a.data[ind0] * bnext[k] + a.data[ind0] * anext[k] * (bprev[k] - 1.0f);
}
}
}
}

int main() {
S a, b;
I p;
Time start = Clock::now();
foo(p, a, b);
Time end = Clock::now();
std::cout << as_ms(end - start).count() << std::endl;
float sum = 0;
for (int i = 0; i != S::size; ++i) {
sum += a.data[i];
}
return sum;
}

检查缓存是否导致问题:

valgrind --tool=cachegrind ./a.out

对齐 = 16:

==4352== I   refs:      3,905,614,100
==4352== I1 misses: 1,626
==4352== LLi misses: 1,579
==4352== I1 miss rate: 0.00%
==4352== LLi miss rate: 0.00%
==4352==
==4352== D refs: 2,049,454,623 (1,393,712,296 rd + 655,742,327 wr)
==4352== D1 misses: 66,707,929 ( 66,606,998 rd + 100,931 wr)
==4352== LLd misses: 66,681,897 ( 66,581,942 rd + 99,955 wr)
==4352== D1 miss rate: 3.3% ( 4.8% + 0.0% )
==4352== LLd miss rate: 3.3% ( 4.8% + 0.0% )
==4352==
==4352== LL refs: 66,709,555 ( 66,608,624 rd + 100,931 wr)
==4352== LL misses: 66,683,476 ( 66,583,521 rd + 99,955 wr)
==4352== LL miss rate: 1.1% ( 1.3% + 0.0% )

对齐 = 32

==4426== I   refs:      2,857,165,049
==4426== I1 misses: 1,604
==4426== LLi misses: 1,560
==4426== I1 miss rate: 0.00%
==4426== LLi miss rate: 0.00%
==4426==
==4426== D refs: 1,558,058,149 (967,779,295 rd + 590,278,854 wr)
==4426== D1 misses: 66,706,930 ( 66,605,998 rd + 100,932 wr)
==4426== LLd misses: 66,680,898 ( 66,580,942 rd + 99,956 wr)
==4426== D1 miss rate: 4.3% ( 6.9% + 0.0% )
==4426== LLd miss rate: 4.3% ( 6.9% + 0.0% )
==4426==
==4426== LL refs: 66,708,534 ( 66,607,602 rd + 100,932 wr)
==4426== LL misses: 66,682,458 ( 66,582,502 rd + 99,956 wr)
==4426== LL miss rate: 1.5% ( 1.7% + 0.0% )

似乎问题不在缓存中。


检查该问题不在 Turbo Boost 中。

对齐:16 --> 32

启用 Turbo Boost :~4100ms --> ~4600ms

Turbo Boost 禁用:~5000ms --> ~5400ms

最佳答案

不是答案,但我在 Haswell E5-2680v3 上使用 GNU g++ 6.4.0 和 Intel icpc 18.0.1 进行了一些测量。所有时间都非常一致,只有几毫秒的偏差:

  1. g++ -O2 -mavx2 -ftree-vectorize align=16: 6.99 [s]
  2. g++ -O2 -mavx2 -ftree-vectorize align=32: 6.67 [s]
  3. g++ -O3 -mavx2 -ftree-vectorize align=16: 6.72 [s]
  4. g++ -O3 -mavx2 -ftree-vectorize align=32: 6.60 [s]
  5. g++ -O2 -march=haswell -ftree-vectorize align=16: 6.45 [s]
  6. g++ -O2 -march=haswell -ftree-vectorize align=32: 6.45 [s]
  7. g++ -O3 -march=haswell -ftree-vectorize align=16: 6.44 [s]
  8. g++ -O3 -march=haswell -ftree-vectorize align=32: 6.44 [s]
  9. icpc -O2 -xCORE-AVX2 align=16: 3.67 [s]
  10. icpc -O2 -xCORE-AVX2 align=32: 3.51 [s]
  11. icpc -O3 -xCORE-AVX2 align=16: 3.65 [s]
  12. icpc -O3 -xCORE-AVX2 align=32: 3.59 [s]

结论:

  • 对于 GCC,-march=haswell-mavx2 更快,对于 -march=haswell,实际上对优化级别和对齐方式。
  • ICC 提供比 GCC 低得多的运行时间/更高的性能。
  • 对于 ICC,正确对齐有帮助,但作用不大。
  • 有趣的是,具有更高优化级别的 ICC 提供了更长的运行时间,但这可能归因于更大的程序大小,因此更多的指令缓存未命中。

根据我的经验,ICC 通常比 GCC 好得多,至少在矢量化方面是这样。我没有研究生成的机器代码,但可以使用 Godbolt 等来了解为什么 ICC 在这种情况下如此出色。

关于c++ - 为什么增加阵列对齐会降低性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49403617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com