Skip to content

Commit

Permalink
Added cache testing to valgrind example
Browse files Browse the repository at this point in the history
  • Loading branch information
guyrt committed Feb 27, 2012
1 parent b0f970d commit 4dfb8a6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
41 changes: 39 additions & 2 deletions valgrind/Readme.rst
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,40 @@
Run this line to see errors in this code. Compile each program without optimization first.

For simpleTest.cc, run this line to see errors in this code.

::

valgrind --track-origins=yes --leak-check=full ./simpleTest 300 300


We also have a cache test line. Run this line to see the cache errors.

::

valgrind --tool=cachegrind ./a.out 0 1000 100000

There are two paths in this code. If the first input is 1, it runs a cache-sensitive version of the loop.
If it is 0, it runs a cache-insensitive version.

FYI: on the Trieste lab machines, this is what cache looks like:

::

guy ~>dmesg | grep cache
CPU: L1 I cache: 32K, L1 D cache: 32K
CPU: L2 cache: 6144K
CPU: L1 I cache: 32K, L1 D cache: 32K
CPU: L2 cache: 6144K

You can run the same command to see cache on your linux machine. Another way to see the exact cache setup that
valgrind found is the following:

::

cg_annotate --auto=yes cachegrind.out.21960

Note that your cachegrind.out will have a different number. This command is also handy because it shows which functions caused cache
misses.




valgrind --track-origins=yes --leak-check=full ./a.out
33 changes: 22 additions & 11 deletions valgrind/cacheTest.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,23 +7,34 @@ Tests cache misses.


int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc < 2){ if (argc < 4){
printf("Usage: cacheTest sizeI sizeJ\n"); printf("Usage: cacheTest fast sizeI sizeJ\nIn first input, use 1 for fast code (cache-smart) and anything else for slow (cache-poor) execution.\n");
return 1; return 1;
} }
int sI = atoi(argv[1]); int runFast = atoi(argv[1]);
int sJ = atoi(argv[2]); long sI = atoi(argv[2]);
int *arr = new int[sI*sJ]; // double array. long sJ = atoi(argv[3]);


printf("Operating on matrix of size %d by %d\n",sI,sJ);

long *arr = new long[sI*sJ]; // double array.

// El mejor!
//for (int i=0; i < sI*sJ; ++i) arr[i] = i; //for (int i=0; i < sI*sJ; ++i) arr[i] = i;


for (int i=0; i < sI; ++i) if (runFast == 1){
for (int j=0; j < sJ; ++j)
arr[(i * (sJ)) + j ] = i; for (long i=0; i < sI; ++i)
for (long j=0; j < sJ; ++j)
arr[(i * (sJ)) + j ] = i;


for (int i=0; i < sI; ++i) }else{
for (int j=0; j < sJ; ++j)
arr[(j * (sI)) + i ] = i; for (long i=0; i < sI; ++i)
for (long j=0; j < sJ; ++j)
arr[(j * (sI)) + i ] = i;

}


return 1; return 1;
} }

0 comments on commit 4dfb8a6

Please sign in to comment.