Big Temp
Big Temp

Reputation: 454

Is there a way to simulate cache locality when benchmarking?

I'm trying to figure out what would be the best approach to benchmark C++ programs and would like to simulate both the scenario when the data related to the benchmarked section is present in the cache and when it's cold.

Is there a reliable way to enforce good and bad cache locality on x86-64 machines as a form of preparation for a test run, assuming the data it will involve is known?

Upvotes: 2

Views: 631

Answers (1)

eerorika
eerorika

Reputation: 238441

Presumably you are benchmarking an algorithm that performs an operation over a range of objects, and you care about the locality of those objects in memory (and thus cache).

To "simulate" locality: Create locality. You can create a linked list with high locality as well as linked list with low locality:

Allocate the nodes in an array. To create a list with high locality, make sure that first element of the array points to the second, and so on. To create list with lower locality, create a random permutation of the order so that each node points to another in a random position of the array.

Make sure that number of elements at least a magnitude greater than the largest cache.

Upvotes: 3

Related Questions