在c/c++中的CPU缓存中分配静态内存: 是否可以?
是否可以在 CPU 缓存中显式创建静态对象,以确保这些对象始终保留在缓存中,这样就不会因为一直到达 RAM 或上帝保佑的硬盘虚拟内存而对性能造成影响?
我对大型 L3 共享缓存特别感兴趣,而不是打算针对 L1、L2、指令或任何其他缓存,而只是最大的片内内存块。
只是为了澄清,以区别于我在发布此内容之前搜索的其他线程,我对私有化整个缓存不感兴趣,而只是私有化一个小的、几个类的区域。
Is it possible to explicitly create static objects in the CPU cache, sort of to make sure those objects always stay in the cache so no performance hit is ever taken from reaching all the way into RAM or god forbid - hdd virtual memory?
I am particular interested in targeting the large L3 shared cache, not intending to target L1, L2, instruction or any other cache, just the largest on-die chub of memory there is.
And just to clarify to differentiate from other threads I searched before posting this, I am not interested in privatizing the entire cache, just a small, few classes worth of region.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以。缓存是不可寻址的,因此您无法在其中分配对象。
您似乎想问的是:在虚拟内存中分配了空间,我可以确保我始终获得缓存命中吗?
这是一个更复杂的问题,答案是:部分。
通过使用操作系统的内存管理 API(例如
mlock()
)将该区域标记为不可分页,您绝对可以避免被换出到磁盘。或者从“非分页池”开始分配。我不相信有类似的 API 可以将内存固定到 CPU 缓存中。即使您可以为该块保留 CPU 缓存,也无法避免缓存未命中。如果另一个核心写入内存,所有权将被转移,并且您将遭受缓存未命中和相关的总线传输(可能到主内存,可能到另一个核心的缓存)。
正如 Mathew 在他的评论中提到的那样,您还可以强制缓存未命中与管道中的其他有用工作并行发生,以便在需要时数据位于缓存中。
No. Cache is not addressable, so you can't allocate objects in it.
What it seems like you meant to ask is: Having allocated space in virtual memory, can I ensure that I always get cache hits?
This is a more complicated question, and the answer is: partly.
You definitely can avoid being swapped out to disk, by using the memory management API of your OS (e.g.
mlock()
) to mark the region as non-pageable. Or allocate from "non-paged pool" to begin with.I don't believe there's a similar API to pin memory into CPU cache. Even if you could reserve CPU cache for that block, you can't avoid cache misses. If another core writes to the memory, ownership WILL be transferred, and you WILL suffer a cache miss and associated bus transfer (possibly to main memory, possibly to the cache of the other core).
As Mathew mentions in his comment, you can also force the cache miss to occur in parallel with other useful work in the pipeline, so that the data is in cache when you need it.
您可以运行另一个线程来循环数据并将其放入 L3 缓存中。
You could run another thread that loops over the data and brings it into the L3 cache.