/****************************************************************************** * FILE: mem01.c * DESCRIPTION: * Demonstrates effect of stride * LAST REVISED: 2/15/99 Blaise Barney ******************************************************************************/ #include #include #include #define ARRAY_SIZE 1000 #define SEED 1995 float a[ARRAY_SIZE][ARRAY_SIZE], aa[ARRAY_SIZE][ARRAY_SIZE]; float b[ARRAY_SIZE][ARRAY_SIZE], bb[ARRAY_SIZE][ARRAY_SIZE]; float c[ARRAY_SIZE][ARRAY_SIZE], cc[ARRAY_SIZE][ARRAY_SIZE]; struct timeval start_time, end_time; main() { int i, j; initialize_data(); /*****************************************************************************/ /* Stride 1000 Loop */ /*****************************************************************************/ gettimeofday(&start_time, (struct timeval*)0); for(j=0;j<1000;j++) for(i=0;i<1000;i++) cc[i][j] = cc[i][j] + aa[i][j] * bb[i][j]; gettimeofday(&end_time, (struct timeval*)0); /* after time */ printf("Execution time for the stride 1000 loop:\n"); print_execution_times(); /*****************************************************************************/ /* Stride 1 Loop */ /*****************************************************************************/ gettimeofday(&start_time, (struct timeval*)0); for(i=0;i<1000;i++) for(j=0;j<1000;j++) c[i][j] = c[i][j] + a[i][j] * b[i][j]; gettimeofday(&end_time, (struct timeval*)0); /* after time */ printf("Execution time for the stride 1 loop:\n"); print_execution_times(); finalize_data(); } initialize_data() { int i, j; srandom(SEED); for(i=0;i<1000;i++) for(j=0;j<1000;j++) { aa[i][j] = a[i][j] = (float) (random() % 100000); bb[i][j] = b[i][j] = (float) (random() % 100000); cc[i][j] = c[i][j] = (float) (random() % 100000); } } finalize_data() { int i, j; FILE *fp, *fopen(); fp = fopen("/dev/null", "w"); i = random() % ARRAY_SIZE; j = random() % ARRAY_SIZE; fprintf(fp, "%f\n", a[i][j]); fprintf(fp, "%f\n", aa[i][j]); fclose(fp); } print_execution_times() { int total_usecs; float total_time, total_flops; total_usecs = (end_time.tv_sec-start_time.tv_sec) * 1000000 + (end_time.tv_usec-start_time.tv_usec); printf("Total time was %.2f mSec and the ", ((float) total_usecs) / 1000.0); total_time = ((float) total_usecs) / 1000000.0; total_flops = (float) (ARRAY_SIZE * ARRAY_SIZE * 2); printf("total MFLOPS is %.2f\n\n", (total_flops / total_time) / 1000000.0); }