Skip to content

Instantly share code, notes, and snippets.

@nixprime
Created September 1, 2012 17:45
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 nixprime/3581408 to your computer and use it in GitHub Desktop.
Save nixprime/3581408 to your computer and use it in GitHub Desktop.
Fixed elazartest
/**
* Inspired by Elazar Leibovich.
*
* g++ -O0 -g3 -funroll-loops -lrt -o elazartest elazertest.cpp
*/
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include <ctime>
#include <iostream>
#include <limits>
using namespace std;
const int kIterations = 32;
class WallClockTimer {
public:
struct timespec t1, t2;
WallClockTimer() :
t1(), t2() {
clock_gettime(CLOCK_REALTIME, &t1);
t2 = t1;
}
void reset() {
clock_gettime(CLOCK_REALTIME, &t1);
t2 = t1;
}
double elapsed() {
return ((t2.tv_sec - t1.tv_sec) * 1.0) +
((t2.tv_nsec - t1.tv_nsec) * 1.0e-9);
}
double split() {
clock_gettime(CLOCK_REALTIME, &t2);
return elapsed();
}
};
int totalsum(const int * data, const size_t length) {
int sum = 0;
for (size_t i=0; i<length;i++) sum+=data[i];
return sum;
}
template <int offset>
int sum(const int * data, const size_t length) {
int sum = 0;
for (size_t i=0; i<length;i+=offset) sum+=data[i];
return sum;
}
int test(const size_t N) {
int *a = new int[N];
for(size_t k = 0; k< N; ++k)
a[k] = k - 2 + k * k;
int fakecounter = 0;
cout<<"Buffer size = "<< N*sizeof(int) /(1024.0*1024.0)<<" MB "<<endl;
WallClockTimer t;
double besttime1 = numeric_limits<double>::max();
double besttime2 = numeric_limits<double>::max();
double besttime3 = numeric_limits<double>::max();
for(int k = 0; k<20;++k) {
t.reset();
for (int j = 0; j < kIterations; j++) {
fakecounter += totalsum(a,N);
}
double thistime1 = t.split();
if(thistime1 < besttime1) besttime1 = thistime1;
t.reset();
for (int j = 0; j < 2*kIterations; j++) {
fakecounter += sum<2>(a,N);
}
double thistime2 = t.split();
if(thistime2 < besttime2) besttime2 = thistime2;
t.reset();
for (int j = 0; j < 16*kIterations; j++) {
fakecounter += sum<16>(a,N);
}
double thistime3 = t.split();
if(thistime3 < besttime3) besttime3 = thistime3;
}
cout<<"Stream sum speed = "<<kIterations*N/(1000*1000*besttime1) <<" MI/s or "<< kIterations*N*sizeof(int)/(1024.0*1024.0*besttime1)<<" MB/s"<<endl;
cout<<"Stride-2 sum speed = "<<kIterations*N/(1000*1000*besttime2) <<" MI/s or "<< kIterations*N*sizeof(int)/(1024.0*1024.0*besttime2)<<" MB/s"<<endl;
cout<<"Stream/stride-2 = "<< (besttime2/besttime1) <<endl;
cout<<"Stride-16 sum speed = "<<kIterations*N/(1000*1000*besttime3) <<" MI/s or "<< kIterations*N*sizeof(int)/(1024.0*1024.0*besttime3)<<" MB/s"<<endl;
cout<<"Stream/stride-16 ratio = "<< (besttime3/besttime1) <<endl;
delete[] a;
return fakecounter;
}
int main() {
cout<<"L1 cache"<<endl;
test(16 * 1024 / sizeof(int));
cout << endl;
cout<<"L2 cache"<<endl;
test(128 * 1024 / sizeof(int));
cout << endl;
cout<<"L3 cache, single cache slice"<<endl;
test(768 * 1024 / sizeof(int));
cout << endl;
cout<<"L3 cache, multiple cache slices"<<endl;
test(4 * 1024 * 1024 / sizeof(int));
cout << endl;
cout<<"RAM cache"<<endl;
test(32 * 1024 * 1024 / sizeof(int));
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment