You probably do not need to do anything except replace the loop, because the caches are designed to use the locality of the link in the code by themselves, which means that it will cache the first element along with several of the following elements (spatial locality) from the array and for some time it will Store them in a cache (temporary locality).
However, some compilers allow you to control the caching, for example, gcc has __ builtin_prefetch , which allows you to control which data should be preprogrammed and whether it should be left in the cache or not.
- Built-in function: void __builtin_prefetch (const void * addr, rw, locality)
This feature is used to minimize cache latency by moving data to the cache before it is available. You can embed __builtin_prefetch calls in code for which you know the data addresses in memory that are likely to be available soon. If the target supports, they generate data prefetch instructions. If the prefetch made early enough before access, then the data will be in the cache at the time of access.
This guide provides an example:
for (i = 0; i < n; i++) { a[i] = a[i] + b[i]; __builtin_prefetch (&a[i+j], 1, 1); __builtin_prefetch (&b[i+j], 0, 1); }
source share