#include #include /* struct timeval */ #include /* struct rusage */ #include "romberg.rts.h" float r1[10][10], /* orignial */ r2[10][10], /* ct spec */ r3[10][10]; /* rt spec */ /* time declarations */ struct rusage start, stop; extern void print_times(struct rusage, struct rusage, int); /* orig, ctspec, and rtspec calls to rt */ extern void romberg(float r[10][10], float, float, int); extern void romberg_2(float r[10][10], float, float); extern void romberg_4(float r[10][10], float, float); extern void romberg_6(float r[10][10], float, float); extern void romberg_8(float r[10][10], float, float); void (*ct_spec)(float r[10][10], float, float); void (*rt_spec)(float r[10][10], float, float); void check_array(float a[10][10], float b[10][10], int, int, int, int); void print_array(char *, float a[10][10], int, int, int, int); /* * e.g. bench -n 2 1000 */ main(int argc, char **argv) { int i; char *mode; /* benchmark mode */ int M; /* static value */ int iterate; /* number of times to iterate */ /**************************************************************************** * Check parameters ***************************************************************************/ /* correct number of parameters */ if (argc != 4) { printf("Usage: bench \n"); exit(0); } /* either normal or verbose mode */ mode = argv[1]; if (*mode++ == '-' && ( (*mode == 't') || (*mode == 'b'))) { printf(" : -n (normal)\n"); printf(" -v (verbose)\n"); exit(0); } /* M must be an "expected" value */ M = atoi(argv[2]); if ((M != 2) && (M != 4) && (M != 6) && (M != 8)) { printf(" : 2, 4, 6, or 8\n"); exit(0); } /* iterate can be any value */ iterate = atoi(argv[3]); /**************************************************************************** * Run benchmark ***************************************************************************/ if(*mode == 'v') { print_array("Original\n", r1, 0, 0, (M+1), (M+1)); print_array("CT Spec\n", r2, 0, 0, (M+1), (M+1)); print_array("RT Spec\n", r3, 0, 0, (M+1), (M+1)); } /* original function */ getrusage(RUSAGE_SELF, &start); for(i = 0; i < iterate; i++) romberg(r1, 1.0, 2.0, M); getrusage(RUSAGE_SELF, &stop); printf("Original(%d) ", M); print_times(start, stop, iterate); printf("\n"); /* compile_time specialized function */ switch (M) { case 2: ct_spec = romberg_2; break; case 4: ct_spec = romberg_4; break; case 6: ct_spec = romberg_6; break; case 8: ct_spec = romberg_8; break; } getrusage(RUSAGE_SELF, &start); for(i = 0; i < iterate; i++) ct_spec(r2, 1.0, 2.0); getrusage(RUSAGE_SELF, &stop); printf("CT Spec (%d) ", M); print_times(start, stop, iterate); check_array(r1, r2, 0, (M+1), 0, (M+1)); /* run_time specialize function */ getrusage(RUSAGE_SELF, &start); for(i = 0; i < iterate; i++) rt_spec = (void (*)(float r[10][10], float, float)) (rts_romberg_1(M)); getrusage(RUSAGE_SELF, &stop); printf("RT Gen (%d) ", M); print_times(start, stop, iterate); printf("\n"); /* run_time specialized function */ getrusage(RUSAGE_SELF, &start); for(i = 0; i < iterate; i++) (*rt_spec)(r3, 1.0, 2.0); getrusage(RUSAGE_SELF, &stop); printf("RT Spec (%d) ", M); print_times(start, stop, iterate); check_array(r1, r3, 0, (M+1), 0, (M+1)); printf("\n"); if(*mode == 'v') { print_array("Original\n", r1, 0, 0, (M+1), (M+1)); print_array("CT Spec\n", r2, 0, 0, (M+1), (M+1)); print_array("RT Spec\n", r3, 0, 0, (M+1), (M+1)); } } /* function used for integration */ float f(float x) { return (x * x) + (2.0 * x) + 1.0; } /* check the results */ void check_array(float a[10][10], float b[10][10], int x_start, int y_start, int x_stop, int y_stop) { int i, j, correct; correct = 1; for(i = x_start; i < x_stop; i++) for(j = y_start; j < y_stop; j++) correct = correct && (a[i][j] == b[i][j]); if(correct) printf("correct\n"); else printf("ERROR\n"); } /* print out the results (for debugging purposes) */ void print_array(char *str, float a[10][10], int x_start, int y_start, int x_stop, int y_stop) { int i, j; printf(str); for(i = x_start; i < x_stop; i++) { for(j = y_start; j < y_stop; j++) printf("%4.4f ", a[i][j]); printf("\n"); } }