#include #include #include "timer.h" int adjust_counter (int loops, double time); void matmul (int loops, int ldim, int m, double *a, double *b, double *c); int main (int argc, char *argv[]) { int loops = 1000; int from = 10; int to = 1000; int step = 5; int seed = 1; double *a, *b, *c; int m, i, j; double time, timeg; unsigned long toul = (unsigned long) to; if ((a = (double *) malloc (sizeof (double) * toul * toul)) == NULL) { fprintf (stderr, "%s, %s, line %d: out of memory\n", argv[0], __FILE__, __LINE__); exit (1); } if ((b = (double *) malloc (sizeof (double) * toul * toul)) == NULL) { fprintf (stderr, "%s, %s, line %d: out of memory\n", argv[0], __FILE__, __LINE__); exit (1); } if ((c = (double *) malloc (sizeof (double) * toul * toul)) == NULL) { fprintf (stderr, "%s, %s, line %d: out of memory\n", argv[0], __FILE__, __LINE__); exit (1); } srand ((unsigned int) seed); double sc = 1. / ((double) RAND_MAX); for (j = 0; j < to; j++) { for (i = 0; i < to; i++) { a[i + j * to] = sc * rand () - 0.5; b[i + j * to] = sc * rand () - 0.5; } } fprintf (stdout, "#\n# "); for (i = 0; i < argc; i++) { fprintf (stdout, "%s ", argv[i]); } fprintf (stdout, "\n#\n"); fprintf (stdout, "# SIZE MFlops Time\n"); for (m = from; m <= to; m += step) { timer_start (); matmul (loops, to, m, a, b, c); time = timer_stop (); timeg = time / loops; fprintf (stdout, " %5d %10.2f %10.6f %10d\n", m, 2. * (double) m * (double) m * (double) m / timeg * 1.e-6, time, loops); fflush (stdout); /* * adjust loops such that time \sim 1-2 sec */ loops = adjust_counter (loops, time); } free (a); free (b); free (c); return 0; } /* * adjust the repetition count such that time \sim 1-2 sec */ int adjust_counter (int loops, double time) { if (time > 2.) { loops /= time; loops = loops < 1 ? 1 : loops; } else if (time < 1.) { loops /= time; } return loops; }