Not. The cache is not addressed, so you cannot select objects in it.
It seems that you wanted to ask: by allocating space in virtual memory, can I guarantee that I always get caching?
This is a more complex question, and the answer is partially.
You can definitely avoid sending to disk using your OSβs memory management API (e.g. mlock() ) to mark the region as not printable. Or select from the "misunderstood pool" to start.
I do not believe that a similar API for connecting memory to the processor cache. Even if you can reserve a processor cache for this block, you cannot avoid cache misses. If another kernel is written to the memory, ownership will be transferred, and you will have a missed cache and the associated bus transfer (possibly to main memory, possibly to the cache of another kernel).
As Matthew mentions in his comment, you can also force skipping the cache in parallel with other useful work in the pipeline so that the data is in the cache when you need it.
source share