1*5d28107eSBarry Smith 2*5d28107eSBarry Smith #include <sys/time.h> 3*5d28107eSBarry Smith /* int gettimeofday(struct timeval *tp, struct timezone *tzp); */ 4*5d28107eSBarry Smith 5*5d28107eSBarry Smith double second() 6*5d28107eSBarry Smith { 7*5d28107eSBarry Smith /* struct timeval { long tv_sec; 8*5d28107eSBarry Smith long tv_usec; }; 9*5d28107eSBarry Smith 10*5d28107eSBarry Smith struct timezone { int tz_minuteswest; 11*5d28107eSBarry Smith int tz_dsttime; }; */ 12*5d28107eSBarry Smith 13*5d28107eSBarry Smith struct timeval tp; 14*5d28107eSBarry Smith struct timezone tzp; 15*5d28107eSBarry Smith int i; 16*5d28107eSBarry Smith 17*5d28107eSBarry Smith i = gettimeofday(&tp,&tzp); 18*5d28107eSBarry Smith return ( (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6 ); 19*5d28107eSBarry Smith } 20*5d28107eSBarry Smith # include <stdio.h> 21*5d28107eSBarry Smith # include <math.h> 22*5d28107eSBarry Smith # include <float.h> 23*5d28107eSBarry Smith # include <limits.h> 24*5d28107eSBarry Smith # include <sys/time.h> 25*5d28107eSBarry Smith 26*5d28107eSBarry Smith /* 27*5d28107eSBarry Smith * Program: Stream 28*5d28107eSBarry Smith * Programmer: Joe R. Zagar 29*5d28107eSBarry Smith * Revision: 4.0-BETA, October 24, 1995 30*5d28107eSBarry Smith * Original code developed by John D. McCalpin 31*5d28107eSBarry Smith * 32*5d28107eSBarry Smith * This program measures memory transfer rates in MB/s for simple 33*5d28107eSBarry Smith * computational kernels coded in C. These numbers reveal the quality 34*5d28107eSBarry Smith * of code generation for simple uncacheable kernels as well as showing 35*5d28107eSBarry Smith * the cost of floating-point operations relative to memory accesses. 36*5d28107eSBarry Smith * 37*5d28107eSBarry Smith * INSTRUCTIONS: 38*5d28107eSBarry Smith * 39*5d28107eSBarry Smith * 1) Stream requires a good bit of memory to run. Adjust the 40*5d28107eSBarry Smith * value of 'N' (below) to give a 'timing calibration' of 41*5d28107eSBarry Smith * at least 20 clock-ticks. This will provide rate estimates 42*5d28107eSBarry Smith * that should be good to about 5% precision. 43*5d28107eSBarry Smith */ 44*5d28107eSBarry Smith 45*5d28107eSBarry Smith # define N 2000000 46*5d28107eSBarry Smith # define NTIMES 50 47*5d28107eSBarry Smith # define OFFSET 0 48*5d28107eSBarry Smith 49*5d28107eSBarry Smith /* 50*5d28107eSBarry Smith * 3) Compile the code with full optimization. Many compilers 51*5d28107eSBarry Smith * generate unreasonably bad code before the optimizer tightens 52*5d28107eSBarry Smith * things up. If the results are unreasonably good, on the 53*5d28107eSBarry Smith * other hand, the optimizer might be too smart for me! 54*5d28107eSBarry Smith * 55*5d28107eSBarry Smith * Try compiling with: 56*5d28107eSBarry Smith * cc -O stream_d.c second.c -o stream_d -lm 57*5d28107eSBarry Smith * 58*5d28107eSBarry Smith * This is known to work on Cray, SGI, IBM, and Sun machines. 59*5d28107eSBarry Smith * 60*5d28107eSBarry Smith * 61*5d28107eSBarry Smith * 4) Mail the results to mccalpin@cs.virginia.edu 62*5d28107eSBarry Smith * Be sure to include: 63*5d28107eSBarry Smith * a) computer hardware model number and software revision 64*5d28107eSBarry Smith * b) the compiler flags 65*5d28107eSBarry Smith * c) all of the output from the test case. 66*5d28107eSBarry Smith * Thanks! 67*5d28107eSBarry Smith * 68*5d28107eSBarry Smith */ 69*5d28107eSBarry Smith 70*5d28107eSBarry Smith # define HLINE "-------------------------------------------------------------\n" 71*5d28107eSBarry Smith 72*5d28107eSBarry Smith # ifndef MIN 73*5d28107eSBarry Smith # define MIN(x,y) ((x)<(y)?(x):(y)) 74*5d28107eSBarry Smith # endif 75*5d28107eSBarry Smith # ifndef MAX 76*5d28107eSBarry Smith # define MAX(x,y) ((x)>(y)?(x):(y)) 77*5d28107eSBarry Smith # endif 78*5d28107eSBarry Smith 79*5d28107eSBarry Smith static double a[N+OFFSET], 80*5d28107eSBarry Smith b[N+OFFSET], 81*5d28107eSBarry Smith c[N+OFFSET]; 82*5d28107eSBarry Smith /*double *a,*b,*c;*/ 83*5d28107eSBarry Smith 84*5d28107eSBarry Smith static double rmstime[4] = {0}, maxtime[4] = {0}, 85*5d28107eSBarry Smith mintime[4] = {FLT_MAX,FLT_MAX,FLT_MAX,FLT_MAX}; 86*5d28107eSBarry Smith 87*5d28107eSBarry Smith static char *label[4] = {"Copy: ", "Scale: ", 88*5d28107eSBarry Smith "Add: ", "Triad: "}; 89*5d28107eSBarry Smith 90*5d28107eSBarry Smith static double bytes[4] = { 91*5d28107eSBarry Smith 2 * sizeof(double) * N, 92*5d28107eSBarry Smith 2 * sizeof(double) * N, 93*5d28107eSBarry Smith 3 * sizeof(double) * N, 94*5d28107eSBarry Smith 3 * sizeof(double) * N 95*5d28107eSBarry Smith }; 96*5d28107eSBarry Smith 97*5d28107eSBarry Smith extern double second(); 98*5d28107eSBarry Smith 99*5d28107eSBarry Smith int 100*5d28107eSBarry Smith main() 101*5d28107eSBarry Smith { 102*5d28107eSBarry Smith int quantum, checktick(); 103*5d28107eSBarry Smith int BytesPerWord; 104*5d28107eSBarry Smith register int j, k; 105*5d28107eSBarry Smith double scalar, t, times[4][NTIMES]; 106*5d28107eSBarry Smith 107*5d28107eSBarry Smith /* --- SETUP --- determine precision and check timing --- */ 108*5d28107eSBarry Smith 109*5d28107eSBarry Smith printf(HLINE); 110*5d28107eSBarry Smith BytesPerWord = sizeof(double); 111*5d28107eSBarry Smith printf("This system uses %d bytes per DOUBLE PRECISION word.\n", 112*5d28107eSBarry Smith BytesPerWord); 113*5d28107eSBarry Smith 114*5d28107eSBarry Smith printf(HLINE); 115*5d28107eSBarry Smith printf("Array size = %d, Offset = %d\n" , N, OFFSET); 116*5d28107eSBarry Smith printf("Total memory required = %.1f MB.\n", 117*5d28107eSBarry Smith (3 * N * BytesPerWord) / 1048576.0); 118*5d28107eSBarry Smith printf("Each test is run %d times, but only\n", NTIMES); 119*5d28107eSBarry Smith printf("the *best* time for each is used.\n"); 120*5d28107eSBarry Smith 121*5d28107eSBarry Smith /* Get initial value for system clock. */ 122*5d28107eSBarry Smith 123*5d28107eSBarry Smith /* a = malloc(N*sizeof(double)); 124*5d28107eSBarry Smith b = malloc(N*sizeof(double)); 125*5d28107eSBarry Smith c = malloc(N*sizeof(double));*/ 126*5d28107eSBarry Smith for (j=0; j<N; j++) { 127*5d28107eSBarry Smith a[j] = 1.0; 128*5d28107eSBarry Smith b[j] = 2.0; 129*5d28107eSBarry Smith c[j] = 0.0; 130*5d28107eSBarry Smith } 131*5d28107eSBarry Smith 132*5d28107eSBarry Smith printf(HLINE); 133*5d28107eSBarry Smith 134*5d28107eSBarry Smith if ( (quantum = checktick()) >= 1) 135*5d28107eSBarry Smith printf("Your clock granularity/precision appears to be " 136*5d28107eSBarry Smith "%d microseconds.\n", quantum); 137*5d28107eSBarry Smith else 138*5d28107eSBarry Smith printf("Your clock granularity appears to be " 139*5d28107eSBarry Smith "less than one microsecond.\n"); 140*5d28107eSBarry Smith 141*5d28107eSBarry Smith t = second(); 142*5d28107eSBarry Smith for (j = 0; j < N; j++) 143*5d28107eSBarry Smith a[j] = 2.0E0 * a[j]; 144*5d28107eSBarry Smith t = 1.0E6 * (second() - t); 145*5d28107eSBarry Smith 146*5d28107eSBarry Smith printf("Each test below will take on the order" 147*5d28107eSBarry Smith " of %d microseconds.\n", (int) t ); 148*5d28107eSBarry Smith printf(" (= %d clock ticks)\n", (int) (t/quantum) ); 149*5d28107eSBarry Smith printf("Increase the size of the arrays if this shows that\n"); 150*5d28107eSBarry Smith printf("you are not getting at least 20 clock ticks per test.\n"); 151*5d28107eSBarry Smith 152*5d28107eSBarry Smith printf(HLINE); 153*5d28107eSBarry Smith 154*5d28107eSBarry Smith printf("WARNING -- The above is only a rough guideline.\n"); 155*5d28107eSBarry Smith printf("For best results, please be sure you know the\n"); 156*5d28107eSBarry Smith printf("precision of your system timer.\n"); 157*5d28107eSBarry Smith printf(HLINE); 158*5d28107eSBarry Smith 159*5d28107eSBarry Smith /* --- MAIN LOOP --- repeat test cases NTIMES times --- */ 160*5d28107eSBarry Smith 161*5d28107eSBarry Smith scalar = 3.0; 162*5d28107eSBarry Smith for (k=0; k<NTIMES; k++) 163*5d28107eSBarry Smith { 164*5d28107eSBarry Smith times[0][k] = second(); 165*5d28107eSBarry Smith for (j=0; j<N; j++) 166*5d28107eSBarry Smith c[j] = a[j]; 167*5d28107eSBarry Smith times[0][k] = second() - times[0][k]; 168*5d28107eSBarry Smith 169*5d28107eSBarry Smith times[1][k] = second(); 170*5d28107eSBarry Smith for (j=0; j<N; j++) 171*5d28107eSBarry Smith b[j] = scalar*c[j]; 172*5d28107eSBarry Smith times[1][k] = second() - times[1][k]; 173*5d28107eSBarry Smith 174*5d28107eSBarry Smith times[2][k] = second(); 175*5d28107eSBarry Smith for (j=0; j<N; j++) 176*5d28107eSBarry Smith c[j] = a[j]+b[j]; 177*5d28107eSBarry Smith times[2][k] = second() - times[2][k]; 178*5d28107eSBarry Smith 179*5d28107eSBarry Smith times[3][k] = second(); 180*5d28107eSBarry Smith for (j=0; j<N; j++) 181*5d28107eSBarry Smith a[j] = b[j]+scalar*c[j]; 182*5d28107eSBarry Smith times[3][k] = second() - times[3][k]; 183*5d28107eSBarry Smith } 184*5d28107eSBarry Smith 185*5d28107eSBarry Smith /* --- SUMMARY --- */ 186*5d28107eSBarry Smith 187*5d28107eSBarry Smith for (k=0; k<NTIMES; k++) 188*5d28107eSBarry Smith { 189*5d28107eSBarry Smith for (j=0; j<4; j++) 190*5d28107eSBarry Smith { 191*5d28107eSBarry Smith rmstime[j] = rmstime[j] + (times[j][k] * times[j][k]); 192*5d28107eSBarry Smith mintime[j] = MIN(mintime[j], times[j][k]); 193*5d28107eSBarry Smith maxtime[j] = MAX(maxtime[j], times[j][k]); 194*5d28107eSBarry Smith } 195*5d28107eSBarry Smith } 196*5d28107eSBarry Smith 197*5d28107eSBarry Smith printf("Function Rate (MB/s) RMS time Min time Max time\n"); 198*5d28107eSBarry Smith for (j=0; j<4; j++) { 199*5d28107eSBarry Smith rmstime[j] = sqrt(rmstime[j]/(double)NTIMES); 200*5d28107eSBarry Smith 201*5d28107eSBarry Smith printf("%s%11.4f %11.4f %11.4f %11.4f\n", label[j], 202*5d28107eSBarry Smith 1.0E-06 * bytes[j]/mintime[j], 203*5d28107eSBarry Smith rmstime[j], 204*5d28107eSBarry Smith mintime[j], 205*5d28107eSBarry Smith maxtime[j]); 206*5d28107eSBarry Smith } 207*5d28107eSBarry Smith return 0; 208*5d28107eSBarry Smith } 209*5d28107eSBarry Smith 210*5d28107eSBarry Smith # define M 20 211*5d28107eSBarry Smith 212*5d28107eSBarry Smith int 213*5d28107eSBarry Smith checktick() 214*5d28107eSBarry Smith { 215*5d28107eSBarry Smith int i, minDelta, Delta; 216*5d28107eSBarry Smith double t1, t2, timesfound[M]; 217*5d28107eSBarry Smith 218*5d28107eSBarry Smith /* Collect a sequence of M unique time values from the system. */ 219*5d28107eSBarry Smith 220*5d28107eSBarry Smith for (i = 0; i < M; i++) { 221*5d28107eSBarry Smith t1 = second(); 222*5d28107eSBarry Smith while( ((t2=second()) - t1) < 1.0E-6 ) 223*5d28107eSBarry Smith ; 224*5d28107eSBarry Smith timesfound[i] = t1 = t2; 225*5d28107eSBarry Smith } 226*5d28107eSBarry Smith 227*5d28107eSBarry Smith /* 228*5d28107eSBarry Smith * Determine the minimum difference between these M values. 229*5d28107eSBarry Smith * This result will be our estimate (in microseconds) for the 230*5d28107eSBarry Smith * clock granularity. 231*5d28107eSBarry Smith */ 232*5d28107eSBarry Smith 233*5d28107eSBarry Smith minDelta = 1000000; 234*5d28107eSBarry Smith for (i = 1; i < M; i++) { 235*5d28107eSBarry Smith Delta = (int)( 1.0E6 * (timesfound[i]-timesfound[i-1])); 236*5d28107eSBarry Smith minDelta = MIN(minDelta, MAX(Delta,0)); 237*5d28107eSBarry Smith } 238*5d28107eSBarry Smith 239*5d28107eSBarry Smith return(minDelta); 240*5d28107eSBarry Smith } 241*5d28107eSBarry Smith 242