gpt4 book ai didi

c++ - CPU 缓存的这种性能行为的解释

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:44 25 4
gpt4 key购买 nike

我正在尝试重现此处显示的结果 What Every programmer should know about memory ,具体结果如下图所示(论文中p20-21)

Impact of cache sizes

这基本上是不同工作大小的每个元素的周期图,图表中的突然上升是在工作集大小超过缓存大小的点。

为了完成这个我写了这个 code here .我看到所有数据都从内存中获取(通过每次使用 clflush 刷新缓存),性能是对于所有数据大小都是一样的(正如预期的那样),但是随着缓存的运行,我看到了一个完全相反趋势


Working Set: 16 Kb took 72.62 ticks per access
Working Set: 32 Kb took 46.31 ticks per access
Working Set: 64 Kb took 28.19 ticks per access
Working Set: 128 Kb took 23.70 ticks per access
Working Set: 256 Kb took 20.92 ticks per access
Working Set: 512 Kb took 35.07 ticks per access
Working Set: 1024 Kb took 24.59 ticks per access
Working Set: 2048 Kb took 24.44 ticks per access
Working Set: 3072 Kb took 24.70 ticks per access
Working Set: 4096 Kb took 22.17 ticks per access
Working Set: 5120 Kb took 21.90 ticks per access
Working Set: 6144 Kb took 23.29 ticks per access

有人可以解释这种行为吗?我相信“预取”在这里做得很好,在管道开始时将数据带到缓存中。

如果是这样,我如何重现图中显示的观察结果?我的缓存大小是 L1 (32 Kb)、L2 (256 Kb) 和 L3 (3072 Kb)。

谢谢

最佳答案

这是我修改后的代码。它每次按 STEP 字节步进,更新内存。我选择 STEP 来匹配处理器的缓存行大小(64 字节)。它重复填充循环 REPEAT 次。它向每个缓存行写入一个字节。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define ARRAYSIZE(arr) (sizeof(arr)/sizeof(arr[0]))

#define STEP (64)
#define REPEAT (1000)

inline void
clflush(volatile void *p)
{
asm volatile ("clflush (%0)" :: "r"(p));
}

inline uint64_t
rdtsc()
{
unsigned long a, d;
asm volatile ("cpuid; rdtsc" : "=a" (a), "=d" (d) : : "ebx", "ecx");
return a | ((uint64_t)d << 32);
}

//volatile int i;


volatile unsigned char data[1 << 26]; // 64MB


void sequentialAccess(int bytes)
{
for (int j = 0; j < REPEAT; j++)
for (int i = 0; i < bytes; i += STEP)
data[i] = i;

}

int rangeArr[] = {16, 32, 64, 128, 256, 512, 1024, 2048, 3072, 4096, 6144, 8192, 10240, 12*1024, 14*1024, 16*1024};

inline void test()
{
for (int i = 0; i < ARRAYSIZE(rangeArr); i++)
{
uint64_t start, end;
int kilobytes = rangeArr[i];
start = rdtsc();
sequentialAccess(kilobytes * 1024);
end = rdtsc();
double ticksPerAccess = 1.0 * (end - start) / (kilobytes * 1024 / STEP) / REPEAT;
printf("%d kB took %lf ticks per access\n", kilobytes, ticksPerAccess);
}
}

int
main(int ac, char **av)
{
test();
return 0;
}

在我的“AMD Phenom(tm) II X4 965 处理器”(来自 /proc/cpuinfo 的字符串)上,我得到了以下结果:

16 kB took 9.148758 ticks per access
32 kB took 8.855980 ticks per access
64 kB took 9.008148 ticks per access
128 kB took 17.197035 ticks per access
256 kB took 14.416313 ticks per access
512 kB took 15.845552 ticks per access
1024 kB took 21.394375 ticks per access
2048 kB took 21.379112 ticks per access
3072 kB took 21.399206 ticks per access
4096 kB took 21.630234 ticks per access
6144 kB took 23.907972 ticks per access
8192 kB took 46.306525 ticks per access
10240 kB took 49.292271 ticks per access
12288 kB took 49.575894 ticks per access
14336 kB took 49.758874 ticks per access
16384 kB took 49.660779 ticks per access

这看起来有点像乌尔里希曲线。


编辑:在仔细检查 Ulrich Drepper 的原始基准后,我意识到他在测量区域之外构建一个链表,然后测量追踪该链表的成本。它测量一个称为“加载使用延迟”的参数,这是从内存系统中提取的一个非常有用的参数。

我相信以下代码更接近最初的理想。它还显着增加了迭代次数,以确保我的处理器上的节能功能不会启动。

在下面的代码中,您可以调整 NPAD 以匹配处理器的缓存行大小。您可以调整 ACCESSES 以控制基准循环迭代的次数。总迭代次数完全独立于数据集大小。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define NPAD (64 - sizeof(void *))
#define ACCESSES (1 << 28)


inline void
clflush(volatile void *p)
{
asm volatile ("clflush (%0)" :: "r"(p));
}

inline uint64_t
rdtsc()
{
unsigned long a, d;
asm volatile ("cpuid; rdtsc" : "=a" (a), "=d" (d) : : "ebx", "ecx");
return a | ((uint64_t)d << 32);
}


struct l
{
l *next;
char pad[NPAD];
};


l array[ (1 << 26) / sizeof(l) ];


void init_sequential(int bytes)
{
int elems = bytes / sizeof(l);

for (int i = 1; i < elems; i++)
{
array[i - 1].next = &array[i];
}

array[elems - 1].next = &array[0];
}

void measure_baseline( int accesses )
{
volatile l *ptr = &array[0];

while (accesses-- > 0)
ptr = ptr->next;
}


int rangeArr[] = {16, 32, 64, 128, 256, 512, 1024, 2048, 3072, 4096, 6144, 8192, 10240, 12*1024, 14*1024, 16*1024};

inline void test()
{
for (int i = 0; i < sizeof(rangeArr) / sizeof(rangeArr[0]); i++)
{
uint64_t start, end;
int kilobytes = rangeArr[i];

init_sequential( kilobytes * 1024 );

start = rdtsc();
measure_baseline( ACCESSES );
end = rdtsc();
double ticksPerAccess = 1.0 * (end - start) / ACCESSES;
printf("%d kB took %lf ticks per access\n", kilobytes, ticksPerAccess);
}
}

int
main(int ac, char **av)
{
test();
return 0;
}

这是从我的处理器收集的数据:

16 kB took 3.062668 ticks per access
32 kB took 3.002012 ticks per access
64 kB took 3.001376 ticks per access
128 kB took 9.204764 ticks per access
256 kB took 9.284414 ticks per access
512 kB took 15.848642 ticks per access
1024 kB took 22.645605 ticks per access
2048 kB took 22.698062 ticks per access
3072 kB took 23.039498 ticks per access
4096 kB took 23.481494 ticks per access
6144 kB took 37.720315 ticks per access
8192 kB took 55.197783 ticks per access
10240 kB took 55.886692 ticks per access
12288 kB took 56.262199 ticks per access
14336 kB took 56.153559 ticks per access
16384 kB took 55.879395 ticks per access

这显示了在 L1D 中使用数据延迟的 3 个周期负载,这是我对该处理器(以及大多数主流高性能 PC 专用处理器)的预期。

关于c++ - CPU 缓存的这种性能行为的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20592813/

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