Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any workaround to "reserve" a cache fraction?

Assume I have to write a C or C++ computational intensive function that has 2 arrays as input and one array as output. If the computation uses the 2 input arrays more often than it updates the output array, I'll end up in a situation where the output array seldom gets cached because it's evicted in order to fetch the 2 input arrays.

I want to reserve one fraction of the cache for the output array and enforce somehow that those lines don't get evicted once they are fetched, in order to always write partial results in the cache.

Update1(output[]) // Output gets cached
DoCompute1(input1[]); // Input 1 gets cached
DoCompute2(input2[]); // Input 2 gets cached
Update2(output[]); // Output is not in the cache anymore and has to get cached again
...

I know there are mechanisms to help eviction: clflush, clevict, _mm_clevict, etc. Are there any mechanisms for the opposite?

I am thinking of 3 possible solutions:

  • Using _mm_prefetch from time to time to fetch the data back if it has been evicted. However this might generate unnecessary traffic plus that I need to be very careful to when to introduce them;
  • Trying to do processing on smaller chunks of data. However this would work only if the problem allows it;
  • Disabling hardware prefetchers where that's possible to reduce the rate of unwanted evictions.

Other than that, is there any elegant solution?

like image 352
VAndrei Avatar asked Apr 26 '15 18:04

VAndrei


People also ask

What is cache reserve and how does it work?

By pushing a single button in the dashboard, all of your website’s cacheable content will be written to Cache Reserve. In the same way that Tiered Cache builds a hierarchy of caches between your visitors and your origin, Cache Reserve serves as the ultimate upper-tier cache that will reserve storage space for your assets for as long as you want.

How does Cloudflare determine how long content in Cache reserve is fresh?

How long content in Cache Reserve will be considered “fresh” is determined by edge cache TTL setting or Cache-Control headers at your origin, if edge cache TTL is not set. After the retention period expires, Cloudflare will revalidate the asset when a subsequent request arrives in Cache Reserve for the asset.

What does a high cache ratio mean?

Having a high cache ratio means that more of a website’s content is served from a Cloudflare data center close to where a visitor is requesting the website.


1 Answers

Intel CPUs have something called No Eviction Mode (NEM) but I doubt this is what you need.

While you are attempting to optimise the second (unnecessary) fetch of output[], have you given thought to using SSE2/3/4 registers to store your intermediate output values, update them when necessary, and writing them back only when all updates related to that part of output[] are done? I have done something similar while computing FFTs (Fast Fourier Transforms) where part of the output is in registers and they are moved out (to memory) only when it is known they will not be accessed anymore. Until then, all updates happen to the registers. You'll need to introduce inline assembly to effectively use SSE* registers. Of course, such optimisations are highly dependent on the nature of the algorithm and data placement.

like image 200
pavan Avatar answered Nov 15 '22 17:11

pavan