C++ cache aware programming(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.
这篇关于C++ 缓存感知编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 缓存感知编程
基础教程推荐
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01