C++ 缓存感知编程
C++ 中有没有办法确定 CPU 的缓存大小?我有一个处理大量数据的算法,我想将这些数据分解成块,以便它们适合缓存.这可能吗?您能否就缓存大小进行编程(尤其是在多线程/多核数据处理方面)提供任何其他提示?
is there a way in C++ to determine the CPU's cache size? i have an algorithm that processes a lot of data and i'd like to break this data down into chunks such that they fit into the cache. Is this possible? Can you give me any other hints on programming with cache-size in mind (especially in regard to multithreaded/multicore data processing)?
谢谢!
推荐答案
根据每个程序员都应该知道的memory",由 Ulrich Drepper 编写,您可以在 Linux 上执行以下操作:
According to "What every programmer should know about memory", by Ulrich Drepper you can do the following on Linux:
一旦我们有了记忆的公式要求我们可以将其与缓存大小.如前所述,该缓存可能与多个共享其他核心.目前{那里肯定会很快成为一个更好的方法!} 获得的唯一方法没有硬编码的正确信息知识是通过/sys文件系统.在表 5.2 中我们看到内核发布的内容硬件.一个程序必须找到目录:
Once we have a formula for the memory requirement we can compare it with the cache size. As mentioned before, the cache might be shared with multiple other cores. Currently {There definitely will sometime soon be a better way!} the only way to get correct information without hardcoding knowledge is through the /sys filesystem. In Table 5.2 we have seen the what the kernel publishes about the hardware. A program has to find the directory:
/sys/devices/system/cpu/cpu*/cache
这在第 6 节:程序员可以做什么中列出.
他还在图 6.5 下方描述了一个简短的测试,如果您无法从操作系统获取 L1D 缓存大小,该测试可用于确定它.
He also describes a short test right under Figure 6.5 which can be used to determine L1D cache size if you can't get it from the OS.
我在他的论文中还遇到了一件事:sysconf(_SC_LEVEL2_CACHE_SIZE)
是 Linux 上的一个系统调用,它应该返回 L2 缓存大小,尽管它似乎不太好记录在案.
There is one more thing I ran across in his paper: sysconf(_SC_LEVEL2_CACHE_SIZE)
is a system call on Linux which is supposed to return the L2 cache size although it doesn't seem to be well documented.
相关文章