Flushing cache to prevent benchmarking fluctuations

I am running C ++ code in order to benchmark in a dataset. The problem is that often I get time for the first run, and these numbers change massively (i.e. from 28 seconds to 10 seconds) if I run the same code again. I assume that this is due to the automatic caching of the CPU. Is there a way to flush the cache or somehow prevent these fluctuations?

+4
source share
2 answers

Not the one that works "for everything, everywhere." Most processors have special instructions for clearing the cache, but they are often privileged instructions, so they must be done from within the OS kernel, and not from user mode code. And of course, these are completely different instructions for each processor architecture.

All current x86 processors have an instruction clflushthat flushes one cache line, but for this you need to have the address of the data (or code) that you want to flush. This is great for small and simple data structures, not so good if you have a binary tree that is everywhere. And, of course, not portable at all.

In most environments, reading and writing a large block of alternative data, for example. sort of:

// Global variables.
const size_t bigger_than_cachesize = 10 * 1024 * 1024;
long *p = new long[bigger_than_cachesize];
...
// When you want to "flush" cache. 
for(int i = 0; i < bigger_than_cachesize; i++)
{
   p[i] = rand();
}

rand , - /. , , () , .

- , , ( ) , . , ( perforamnce, , , , , , , )

" " - , , , , .

, " " , " ", . , , , ...

+4

:

  • 2x LLC, LLC ( ), , LLC 1.
  • memset : 1 .
  • "" -, ( volatile 100% ).
  • , 10 ( , (3)).

, , - x86-, .

  • ( 2), , -mapped page, , .
  • , LLC, , . LLC, , , : ( ), . 2 2x , .
  • , , , , .
  • , : , LLC Intel, , "" , LRU MRU, LRU - . , , , "" . .
  • LLC (a) , , LLC ( ) (b), , (c) , , -LRU, , , LRU, .

. , , . , , ( , 2 ). , : , - , 2.

, E () , , S ( ) , M () . , , : . , , / M, 1 , . , 4, , .

"", : , . , -M, -M- , : , , .


1 , LLC ( - ), , .

2 , "", .

+1

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


All Articles