Force some data to cache L1

Sorry for this simple question. Still struggling with some memory concepts here. Question: Suppose I have a pre-computed array A that I want to access again. Is there a way to tell a C program to get this array as close to the CPU cache as possible for quick access? Thank you

+3
source share
1 answer

It is not possible to force an array to use the L1 / L2 cache on most architectures; this is usually not required if you frequently access it, it is unlikely that it will be taken out of the cache.

Some architectures have a set of instructions that allows the processor to give a hint that a fast memory location will be necessary, so that it can start loading into the L1 / L2 log earlier - this is called prefetching, see for example the _mm_prefetch ( http: / /msdn.microsoft.com/en-us/library/84szxsww(v=vs.80).aspx ). However, this is unlikely to be needed if you are accessing a small array.

General tip: first make your data structures cache-efficient (put related data together, pack data, etc.), try reprogramming later if the profiler tells you that you still spend time skipping the cache, and you can Further do not improve the data structure.

+15
source

Source: https://habr.com/ru/post/1782678/


All Articles