Apologies about this simple question. Still struggling with some of the memory concepts here. Question is: Suppose I have a pre-computed array A that I want to access repeatedly. Is there a way to tell a C program to keep this array as close as possible to the CPU cache for fastest access? Thanks.
为这个简单的问题道歉。还在纠结于一些记忆的概念。问题是:假设我有一个预先计算好的数组a,我想要多次访问它。是否有一种方法可以告诉C程序使这个数组尽可能接近CPU缓存以获得最快的访问权限?谢谢。
1 个解决方案
#1
15
There is no way to force an array to L1/L2 cache on most architectures; it is not needed usually, if you access it frequently it is unlikely to be evicted from cache.
在大多数架构上,没有办法强制一个数组到L1/L2缓存;通常不需要它,如果您频繁访问它,它不太可能被从缓存中清除。
On some architectures there is a set of instructions that allows you to give the processor a hint that the memory location will soon be needed, so that it can start loading it to L1/L2 cache early - this is called prefetching, see _mm_prefetch instruction for example ( http://msdn.microsoft.com/en-us/library/84szxsww(v=vs.80).aspx ). Still this is unlikely to be needed if you're accessing a small array.
在某些架构中,有一组指令,允许您给处理器一个提示,提示内存位置将很快被需要,以便它可以开始将其加载到L1/L2缓存中——这称为预取,例如,参见_mm_prefetch指令(http://msdn.microsoft.com/en-us/library/84szxsww(v=vs.80).aspx)。不过,如果您访问的是一个小数组,则不太可能需要这样做。
The general advice is - make your data structures cache-efficient first (put related data together, pack data, etc.), try prefetching later if the profiler tells you that you're still spending time on cache misses and you can't improve the data layout any further.
一般的建议是——让您的数据结构具有缓存效率优先级(将相关数据放在一起,打包数据等等),如果profiler告诉您仍然在缓存遗漏上花费时间,并且您不能进一步改进数据布局,那么请稍后尝试预取。
#1
15
There is no way to force an array to L1/L2 cache on most architectures; it is not needed usually, if you access it frequently it is unlikely to be evicted from cache.
在大多数架构上,没有办法强制一个数组到L1/L2缓存;通常不需要它,如果您频繁访问它,它不太可能被从缓存中清除。
On some architectures there is a set of instructions that allows you to give the processor a hint that the memory location will soon be needed, so that it can start loading it to L1/L2 cache early - this is called prefetching, see _mm_prefetch instruction for example ( http://msdn.microsoft.com/en-us/library/84szxsww(v=vs.80).aspx ). Still this is unlikely to be needed if you're accessing a small array.
在某些架构中,有一组指令,允许您给处理器一个提示,提示内存位置将很快被需要,以便它可以开始将其加载到L1/L2缓存中——这称为预取,例如,参见_mm_prefetch指令(http://msdn.microsoft.com/en-us/library/84szxsww(v=vs.80).aspx)。不过,如果您访问的是一个小数组,则不太可能需要这样做。
The general advice is - make your data structures cache-efficient first (put related data together, pack data, etc.), try prefetching later if the profiler tells you that you're still spending time on cache misses and you can't improve the data layout any further.
一般的建议是——让您的数据结构具有缓存效率优先级(将相关数据放在一起,打包数据等等),如果profiler告诉您仍然在缓存遗漏上花费时间,并且您不能进一步改进数据布局,那么请稍后尝试预取。