/* Matrix transposition benchmark. Designed to stress the memory hierarchy. Works on NxN integer arrays. Takes an optional command-line argument specifying what N is. Doesn't really compute anything of interest. compile with cc -O -o transpose transpose.c */ #include #include /* the following line is Mac CodeWarrior specific */ /* #include */ long N = 2048; void usage(char *prog) { fprintf(stderr, "Usage: %s [-n size] [-t iterations]\n", prog); exit(1); } int main (int argc, char **argv) { int *A, *B; int i, j; int t = 4; /* argc = ccommand(&argv); */ /* Mac CodeWarrior specific */ for (i = 1; i < argc; i++) { if (argv[i][0] != '-') usage(argv[0]); switch (argv[i][1]) { case 'n': i++; N = atoi(argv[i]); break; case 't': i++; t = atoi(argv[i]); break; default: usage(argv[0]); break; } } A = (int*) malloc(sizeof(int)*N*N); B = (int*) malloc(sizeof(int)*N*N); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { A[i*N+j] = rand(); } } for (; t; t--) { int *temp; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { B[i*N+j] = A[j*N+i]; } } temp = B; B = A; A = temp; } return 0; }