There are two cases that need to be distinguished. Do you need to know cache sizes at compile time or at runtime?
Determining cache size at compile time
For some applications, you know the exact architecture on which your code will run, for example, if you can compile the code directly on the host machine. In this case, simplifying the size search and hard coding is an option (it can be automated in the build system). On most machines today, the L1 cache line should be 64 bytes.
If you want to avoid this complexity or need compilation support on unknown architectures, you can use the C ++ 17 function std :: hardware_constructive_interference_size as a good fallback. This will provide an estimate of the compilation time for the cache line, but be aware of its limitations . Note that the compiler cannot guess exactly when it creates the binary, since the size of the cache line is typically architecture dependent.
Determining Runtime Cache Size
At run time, you have the advantage that you know the exact machine, but you need code for a specific platform to read information from the OS. A good starting point is the code snippet from this answer , which supports major platforms (Windows, Linux, MacOS). Similarly, you can also read the size of the L2 cache at runtime.
I would advise against trying to guess the cache line by running tests at startup and measuring which one works best. This may work well, but is also prone to errors if the processor is used by other processes.
Combination of both approaches
If you need to send one binary file and the machines on which it will work later will have different architectures with different cache sizes, you can create specialized pieces of code for each cache size, and then dynamically (when the application starts) choose the most suitable option. one.
Philipp Claßen Jun 04 '19 at 23:53 on 2019-06-04 23:53
source share