问题描述
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 中我们看到内核发布的内容硬件.一个程序必须找到目录:
/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.
这篇关于C++ 缓存感知编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!