Skip to content

Instantly share code, notes, and snippets.

@thegedge
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thegedge/55dab0bfa87296926dc0 to your computer and use it in GitHub Desktop.
Save thegedge/55dab0bfa87296926dc0 to your computer and use it in GitHub Desktop.
Cache prefetch example
#include <array>
#include <chrono>
#include <iostream>
// Width of each dimension
constexpr size_t N = 200;
// Number of iterations for the test
constexpr size_t NUM_ITERATIONS = 100;
// Data type we'll use for testing
using DataType = short;
// 1D array index from a 3D coordinate
int index(int x, int y, int z) {
return N*(N*y + z) + x;
}
void test1() {
using namespace std::chrono;
const DataType data[N*N*N] = {0};
int sum = 0;
const auto start = high_resolution_clock::now();
for(int iteration = 0; iteration < NUM_ITERATIONS; ++iteration) {
for(int x = 0; x < N; ++x) {
for(int z = 0; z < N; ++z) {
for(int y = 0; y < N; ++y) {
if(x > 0 && x + 1 < N) {
sum += data[index(x - 1, y, z)] / 2;
sum += data[index(x, y, z)];
sum += data[index(x + 1, y, z)] / 2;
} else {
sum += data[index(x, y, z)];
}
}
}
}
}
const auto elapsed = high_resolution_clock::now() - start;
const auto ms = duration_cast<milliseconds>(elapsed).count();
std::cout << sum << " in " << ((1.0 * ms) / NUM_ITERATIONS) << "ms per loop\n";
}
void test2() {
using namespace std::chrono;
const DataType data[N*N*N] = {0};
int sum = 0;
const auto start = high_resolution_clock::now();
for(int iteration = 0; iteration < NUM_ITERATIONS; ++iteration) {
for(int x = 0; x < N; ++x) {
for(int z = 0; z < N; ++z) {
for(int y = 0; y < N; ++y) {
if(x > 0 && x + 1 < N) {
sum += data[index(x - 1, y, z)] / 2;
sum += data[index(x, y, z)];
sum += data[index(x + 1, y, z)] / 2;
} else {
sum += data[index(x, y, z)];
}
const int prefetch_index = index(x, y + 20, z);
__builtin_prefetch(data + prefetch_index, 0, 0);
}
}
}
}
const auto elapsed = high_resolution_clock::now() - start;
const auto ms = duration_cast<milliseconds>(elapsed).count();
std::cout << sum << " in " << ((1.0 * ms) / NUM_ITERATIONS) << "ms per loop\n";
}
int main(int argc, char **argv) {
test1();
test2();
return 0;
}
$ clang++ -O2 -std=c++1y main.cpp
$ ./a.out
0 in 145.29ms per loop
0 in 84.02ms per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment