Linux 预取数据以缓存 x86-64
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10323420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prefetching data to cache for x86-64
提问by pythonic
In my application, at one point I need to perform calculations on a large contiguous block of memory data (100s of MBs). What I was thinking was to keep prefetching the part of the block my program will touch in future, so that when I perform calculations on that portion, the data is already in the cache.
在我的应用程序中,有一次我需要对一大块连续的内存数据(数百 MB)执行计算。我的想法是继续预取我的程序将来会接触的那部分块,这样当我对该部分执行计算时,数据已经在缓存中了。
Can someone give me a simple example of how to achieve this with gcc? I read _mm_prefetch
somewhere, but don't know how to properly use it. Also note that I have a multicore system, but each core will be working on a different region of memory in parallel.
有人能给我一个简单的例子来说明如何使用 gcc 实现这一目标吗?我在_mm_prefetch
某处阅读,但不知道如何正确使用它。另请注意,我有一个多核系统,但每个核将并行处理不同的内存区域。
采纳答案by Jens Gustedt
gcc
uses builtin functions as an interface for lowlevel instructions. In particular for your case __builtin_prefetch
. But you only should see a measurable difference when using this in cases where the access pattern is not easy to predict automatically.
gcc
使用内置函数作为低级指令的接口。特别是对于您的情况__builtin_prefetch
。但是,只有在访问模式不容易自动预测的情况下使用它时,您才会看到可测量的差异。
回答by Paul R
Modern CPUs have pretty good automatic prefetch and you may well find that you do more harm than good if you try to initiate software prefetching. There is most likely a lot more "low hanging fruit" that you can focus on for optimisation if you find that you actually have a performance problem. Prefetch tends to be one of the last things that you might try, when you're desperate for a few more percent throughput.
现代 CPU 具有很好的自动预取功能,如果您尝试启动软件预取,您很可能会发现弊大于利。如果您发现确实存在性能问题,则很可能还有更多“悬而未决的果实”可以专注于优化。当您迫切需要更高的吞吐量时,预取往往是您可能尝试的最后一件事。