Skip to content

Instantly share code, notes, and snippets.

@brly
Created February 28, 2018 06:08
Show Gist options
  • Save brly/fbc89eddf88966bdfa037b7aba64704c to your computer and use it in GitHub Desktop.
Save brly/fbc89eddf88966bdfa037b7aba64704c to your computer and use it in GitHub Desktop.
wepskam check cache prefetching
#include <cstdio>
#include <cstdlib>
#include <cstring>
double a[1002][1002];
double b[1002][1002];
double t[1002][1002];
double r[1002][1002];
const int N = 1000;
/*
行列を転置した方が処理は多いが sequential read となり hardware prefetch がうまくあたり
cache line を無駄なく使えるので早く処理される例
*/
void naive() {
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
for(int k=0;k<N;k++)
r[i][j] += a[i][k] * b[k][j];
}
void cache() {
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
t[i][j] = b[j][i];
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
for(int k=0;k<N;k++)
r[i][j] += a[i][k] * t[j][k];
}
int main(int argc, char**argv) {
// commonly initializing
memset(r, 0, sizeof(r));
srand(123);
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
a[i][j] = rand();
b[i][j] = rand();
}
}
if (argc == 1) {
naive();
}
else if (argc == 2) {
cache();
}
printf("%lf\n", r[0][0]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment