【发布时间】:2018-08-05 12:27:12
【问题描述】:
在优化内部循环的过程中,我遇到了奇怪的性能行为,我无法理解和纠正。
下面是精简版的代码;粗略地说有一个巨大的数组,它被分成 16 个单词块,我只是简单地将每个块中单词的前导零的数量相加。 (实际上,我使用的是来自Dan Luu 的popcnt 代码,但在这里我选择了一条具有类似性能特征的更简单指令来“简洁”。Dan Luu 的代码基于对this SO question 的回答,虽然它有非常相似的奇怪结果,这里似乎没有回答我的问题。)
// -*- compile-command: "gcc -O3 -march=native -Wall -Wextra -std=c99 -o clz-timing clz-timing.c" -*-
#include <stdint.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define ARRAY_LEN 16
// Return the sum of the leading zeros of each element of the ARRAY_LEN
// words starting at u.
static inline uint64_t clz_array(const uint64_t u[ARRAY_LEN]) {
uint64_t c0 = 0;
for (int i = 0; i < ARRAY_LEN; ++i) {
uint64_t t0;
__asm__ ("lzcnt %1, %0" : "=r"(t0) : "r"(u[i]));
c0 += t0;
}
return c0;
}
// For each of the narrays blocks of ARRAY_LEN words starting at
// arrays, put the result of clz_array(arrays + i*ARRAY_LEN) in
// counts[i]. Return the time taken in milliseconds.
double clz_arrays(uint32_t *counts, const uint64_t *arrays, int narrays) {
clock_t t = clock();
for (int i = 0; i < narrays; ++i, arrays += ARRAY_LEN)
counts[i] = clz_array(arrays);
t = clock() - t;
// Convert clock time to milliseconds
return t * 1e3 / (double)CLOCKS_PER_SEC;
}
void print_stats(double t_ms, long n, double total_MiB) {
double t_s = t_ms / 1e3, thru = (n/1e6) / t_s, band = total_MiB / t_s;
printf("Time: %7.2f ms, %7.2f x 1e6 clz/s, %8.1f MiB/s\n", t_ms, thru, band);
}
int main(int argc, char *argv[]) {
long n = 1 << 20;
if (argc > 1)
n = atol(argv[1]);
long total_bytes = n * ARRAY_LEN * sizeof(uint64_t);
uint64_t *buf = malloc(total_bytes);
uint32_t *counts = malloc(sizeof(uint32_t) * n);
double t_ms, total_MiB = total_bytes / (double)(1 << 20);
printf("Total size: %.1f MiB\n", total_MiB);
// Warm up
t_ms = clz_arrays(counts, buf, n);
//print_stats(t_ms, n, total_MiB); // (1)
// Run it
t_ms = clz_arrays(counts, buf, n); // (2)
print_stats(t_ms, n, total_MiB);
// Write something into buf
for (long i = 0; i < n*ARRAY_LEN; ++i)
buf[i] = i;
// And again...
(void) clz_arrays(counts, buf, n); // (3)
t_ms = clz_arrays(counts, buf, n); // (4)
print_stats(t_ms, n, total_MiB);
free(counts);
free(buf);
return 0;
}
上面的代码有点奇怪的是,我第一次和第二次调用clz_arrays 函数时它位于未初始化的内存中。
这是典型运行的结果(编译器命令在源代码的开头):
$ ./clz-timing 10000000
Total size: 1220.7 MiB
Time: 47.78 ms, 209.30 x 1e6 clz/s, 25548.9 MiB/s
Time: 77.41 ms, 129.19 x 1e6 clz/s, 15769.7 MiB/s
运行此程序的 CPU 是“Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz”,具有 3.5GHz 的涡轮增压。 lzcnt 指令的延迟为 3 个周期,但它的吞吐量为每秒 1 次操作(请参阅 Agner Fog's Skylake instruction tables),因此,在 3.5GHz 的 8 字节字(使用 uint64_t)下,峰值带宽应为 3.5e9 cycles/sec x 8 bytes/cycle = 28.0 GiB/s ,这与我们在第一个数字中看到的非常接近。即使在 2.6GHz 下,我们也应该接近 20.8 GiB/s。
我的主要问题是,
为什么调用 (4) 的带宽总是远低于调用 (2) 中获得的最佳值,在大多数情况下我可以做些什么来保证最佳性能?
关于我目前发现的几点:
- 根据与
perf的广泛分析,问题似乎是由慢速情况下的LLC缓存加载未命中引起的,而快速情况下没有出现.我的猜测是,我们正在执行计算的内存没有被初始化这一事实可能意味着编译器没有义务将任何特定值加载到内存中,但objdump -d的输出清楚地表明每次都运行相同的代码。就好像硬件预取器第一次激活但不是第二次激活,但在任何情况下,这个数组都应该是世界上最容易可靠地预取的东西。 - 在 (1) 和 (3) 处的“预热”调用始终与调用 (4) 对应的第二个打印带宽一样慢。
- 我在台式机上获得了几乎相同的结果(“Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz”)。
- GCC 4.9、7.0 和 Clang 4.0 的结果基本相同。所有测试都在 Debian 测试,内核 4.14 上运行。
- 所有这些结果和观察结果也可以通过 Dan Luu 帖子中的
clz_array替换为builtin_popcnt_unrolled_errata_manual来获得,并进行必要的修改。
任何帮助将不胜感激!
【问题讨论】:
-
popcnt对 Intel CPU 上的目标寄存器有错误的依赖性,但lzcnt没有(在 Skylake 上)。lzcnt/tzcnthave the same false dep aspopcnton Broadwell and earlier。你所有的测试结果实际上都是来自使用lzcnt而不是popcnt的代码吗? -
@PeterCordes 我在 Core i7-6700HQ 上进行了所有测试和分析,我所有的 cmets 都与此相关(因此 Skylake)。 Xeon E5-2620 的性能数据相似(诚然,我从未达到完整的 TurboBoost 带宽),但我没有像为 Core i7 那样深入研究为 Xeon 生产的组件。
-
对
lzcnt使用内联汇编通常比使用__builtin_clz更糟糕(使用-march=skylake进行编译,以确保它使用lzcnt而不是bsr,如果你关心的话。)@ 987654326@. -
在这个例子中,一些 -O3 优化也有点奇怪。好问题!
-
顺便说一句,如果您的真正问题是
popcnt内存中的一个大数组,AVX2vpshufb4 位 LUT 可以胜过 64 位popcnt(尤其是在 Intel CPU 上,其中popcnt吞吐量限制为每个时钟 1 个(相对于每个时钟 2 个负载),并且对结果求和需要标量代码而不是vpaddb,偶尔会出现vpsadbw/vpaddq)。请参阅 0x80.pl/articles/sse-popcount.html。差别更大with AVX512BW andvpernlogdfor Harley-Seal。
标签: c++ c performance caching assembly