0

For benchmarking reasons, I need to run the same algorithm over the same data multiple times. Now, I want to explore the case where all the runs over the same data are on cold caches.

I was thinking of adding a for loop reading every element of an array (LOAD/MOV instruction on every element) so the cache fill with the array elements. Eg.

vector<size_t> vec(CACHE_SIZE/sizeof(size_t));
//...
//...
size_t element;
for (size_t i = 0; i < vec.size(); ++i) {
    element = vec[i];
}

But when compiled with optimization, all of this will probably be removed.

So how do I do this, possibly with minimal overhead.

1 Answer 1

2

Declare element to be volatile. Accesses and assignments to a volatile variable are not allowed to be optimized away.

volatile size_t element;
Sign up to request clarification or add additional context in comments.

2 Comments

This makes sense. It should probably generate additional LOAD/MOV to memory instruction but that seems fine. I'll check it out.
vector<volatile size_t> vec might be better, but I'm not sure if this is allowed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.