15d28107eSBarry Smith 25d28107eSBarry Smith #include <sys/time.h> 35d28107eSBarry Smith /* int gettimeofday(struct timeval *tp, struct timezone *tzp); */ 45d28107eSBarry Smith 55d28107eSBarry Smith double second() 65d28107eSBarry Smith { 75d28107eSBarry Smith /* struct timeval { long tv_sec; 85d28107eSBarry Smith long tv_usec; }; 95d28107eSBarry Smith 105d28107eSBarry Smith struct timezone { int tz_minuteswest; 115d28107eSBarry Smith int tz_dsttime; }; */ 125d28107eSBarry Smith 135d28107eSBarry Smith struct timeval tp; 145d28107eSBarry Smith struct timezone tzp; 155d28107eSBarry Smith int i; 165d28107eSBarry Smith 175d28107eSBarry Smith i = gettimeofday(&tp,&tzp); 185d28107eSBarry Smith return ( (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6 ); 195d28107eSBarry Smith } 205d28107eSBarry Smith # include <stdio.h> 215d28107eSBarry Smith # include <math.h> 225d28107eSBarry Smith # include <float.h> 235d28107eSBarry Smith # include <limits.h> 245d28107eSBarry Smith # include <sys/time.h> 255d28107eSBarry Smith 265d28107eSBarry Smith /* 275d28107eSBarry Smith * Program: Stream 285d28107eSBarry Smith * Programmer: Joe R. Zagar 295d28107eSBarry Smith * Revision: 4.0-BETA, October 24, 1995 305d28107eSBarry Smith * Original code developed by John D. McCalpin 315d28107eSBarry Smith * 325d28107eSBarry Smith * This program measures memory transfer rates in MB/s for simple 335d28107eSBarry Smith * computational kernels coded in C. These numbers reveal the quality 345d28107eSBarry Smith * of code generation for simple uncacheable kernels as well as showing 355d28107eSBarry Smith * the cost of floating-point operations relative to memory accesses. 365d28107eSBarry Smith * 375d28107eSBarry Smith * INSTRUCTIONS: 385d28107eSBarry Smith * 395d28107eSBarry Smith * 1) Stream requires a good bit of memory to run. Adjust the 405d28107eSBarry Smith * value of 'N' (below) to give a 'timing calibration' of 415d28107eSBarry Smith * at least 20 clock-ticks. This will provide rate estimates 425d28107eSBarry Smith * that should be good to about 5% precision. 435d28107eSBarry Smith */ 445d28107eSBarry Smith 455d28107eSBarry Smith # define N 2000000 465d28107eSBarry Smith # define NTIMES 50 475d28107eSBarry Smith # define OFFSET 0 485d28107eSBarry Smith 495d28107eSBarry Smith /* 505d28107eSBarry Smith * 3) Compile the code with full optimization. Many compilers 515d28107eSBarry Smith * generate unreasonably bad code before the optimizer tightens 525d28107eSBarry Smith * things up. If the results are unreasonably good, on the 535d28107eSBarry Smith * other hand, the optimizer might be too smart for me! 545d28107eSBarry Smith * 555d28107eSBarry Smith * Try compiling with: 565d28107eSBarry Smith * cc -O stream_d.c second.c -o stream_d -lm 575d28107eSBarry Smith * 585d28107eSBarry Smith * This is known to work on Cray, SGI, IBM, and Sun machines. 595d28107eSBarry Smith * 605d28107eSBarry Smith * 615d28107eSBarry Smith * 4) Mail the results to mccalpin@cs.virginia.edu 625d28107eSBarry Smith * Be sure to include: 635d28107eSBarry Smith * a) computer hardware model number and software revision 645d28107eSBarry Smith * b) the compiler flags 655d28107eSBarry Smith * c) all of the output from the test case. 665d28107eSBarry Smith * Thanks! 675d28107eSBarry Smith * 685d28107eSBarry Smith */ 695d28107eSBarry Smith 705d28107eSBarry Smith # define HLINE "-------------------------------------------------------------\n" 715d28107eSBarry Smith 725d28107eSBarry Smith # ifndef MIN 735d28107eSBarry Smith # define MIN(x,y) ((x)<(y)?(x):(y)) 745d28107eSBarry Smith # endif 755d28107eSBarry Smith # ifndef MAX 765d28107eSBarry Smith # define MAX(x,y) ((x)>(y)?(x):(y)) 775d28107eSBarry Smith # endif 785d28107eSBarry Smith 795d28107eSBarry Smith static double a[N+OFFSET], 805d28107eSBarry Smith b[N+OFFSET], 815d28107eSBarry Smith c[N+OFFSET]; 825d28107eSBarry Smith /*double *a,*b,*c;*/ 835d28107eSBarry Smith 845d28107eSBarry Smith static double rmstime[4] = {0}, maxtime[4] = {0}, 855d28107eSBarry Smith mintime[4] = {FLT_MAX,FLT_MAX,FLT_MAX,FLT_MAX}; 865d28107eSBarry Smith 875d28107eSBarry Smith static char *label[4] = {"Copy: ", "Scale: ", 885d28107eSBarry Smith "Add: ", "Triad: "}; 895d28107eSBarry Smith 905d28107eSBarry Smith static double bytes[4] = { 915d28107eSBarry Smith 2 * sizeof(double) * N, 925d28107eSBarry Smith 2 * sizeof(double) * N, 935d28107eSBarry Smith 3 * sizeof(double) * N, 945d28107eSBarry Smith 3 * sizeof(double) * N 955d28107eSBarry Smith }; 965d28107eSBarry Smith 975d28107eSBarry Smith extern double second(); 985d28107eSBarry Smith 99*01a79839SBarry Smith #include "mpi.h" 100*01a79839SBarry Smith 101*01a79839SBarry Smith int main(int argc,char **args) 1025d28107eSBarry Smith { 1035d28107eSBarry Smith int quantum, checktick(); 1045d28107eSBarry Smith int BytesPerWord; 1055d28107eSBarry Smith register int j, k; 1065d28107eSBarry Smith double scalar, t, times[4][NTIMES]; 1075d28107eSBarry Smith 108*01a79839SBarry Smith MPI_Init(&argc,&args); 1095d28107eSBarry Smith /* --- SETUP --- determine precision and check timing --- */ 1105d28107eSBarry Smith 1115d28107eSBarry Smith printf(HLINE); 1125d28107eSBarry Smith BytesPerWord = sizeof(double); 1135d28107eSBarry Smith printf("This system uses %d bytes per DOUBLE PRECISION word.\n", 1145d28107eSBarry Smith BytesPerWord); 1155d28107eSBarry Smith 1165d28107eSBarry Smith printf(HLINE); 1175d28107eSBarry Smith printf("Array size = %d, Offset = %d\n" , N, OFFSET); 1185d28107eSBarry Smith printf("Total memory required = %.1f MB.\n", 1195d28107eSBarry Smith (3 * N * BytesPerWord) / 1048576.0); 1205d28107eSBarry Smith printf("Each test is run %d times, but only\n", NTIMES); 1215d28107eSBarry Smith printf("the *best* time for each is used.\n"); 1225d28107eSBarry Smith 1235d28107eSBarry Smith /* Get initial value for system clock. */ 1245d28107eSBarry Smith 1255d28107eSBarry Smith /* a = malloc(N*sizeof(double)); 1265d28107eSBarry Smith b = malloc(N*sizeof(double)); 1275d28107eSBarry Smith c = malloc(N*sizeof(double));*/ 1285d28107eSBarry Smith for (j=0; j<N; j++) { 1295d28107eSBarry Smith a[j] = 1.0; 1305d28107eSBarry Smith b[j] = 2.0; 1315d28107eSBarry Smith c[j] = 0.0; 1325d28107eSBarry Smith } 1335d28107eSBarry Smith 1345d28107eSBarry Smith printf(HLINE); 1355d28107eSBarry Smith 1365d28107eSBarry Smith if ( (quantum = checktick()) >= 1) 1375d28107eSBarry Smith printf("Your clock granularity/precision appears to be " 1385d28107eSBarry Smith "%d microseconds.\n", quantum); 1395d28107eSBarry Smith else 1405d28107eSBarry Smith printf("Your clock granularity appears to be " 1415d28107eSBarry Smith "less than one microsecond.\n"); 1425d28107eSBarry Smith 1435d28107eSBarry Smith t = second(); 1445d28107eSBarry Smith for (j = 0; j < N; j++) 1455d28107eSBarry Smith a[j] = 2.0E0 * a[j]; 1465d28107eSBarry Smith t = 1.0E6 * (second() - t); 1475d28107eSBarry Smith 1485d28107eSBarry Smith printf("Each test below will take on the order" 1495d28107eSBarry Smith " of %d microseconds.\n", (int) t ); 1505d28107eSBarry Smith printf(" (= %d clock ticks)\n", (int) (t/quantum) ); 1515d28107eSBarry Smith printf("Increase the size of the arrays if this shows that\n"); 1525d28107eSBarry Smith printf("you are not getting at least 20 clock ticks per test.\n"); 1535d28107eSBarry Smith 1545d28107eSBarry Smith printf(HLINE); 1555d28107eSBarry Smith 1565d28107eSBarry Smith 1575d28107eSBarry Smith /* --- MAIN LOOP --- repeat test cases NTIMES times --- */ 1585d28107eSBarry Smith 1595d28107eSBarry Smith scalar = 3.0; 1605d28107eSBarry Smith for (k=0; k<NTIMES; k++) 1615d28107eSBarry Smith { 162*01a79839SBarry Smith MPI_Barrier(MPI_COMM_WORLD); 1635d28107eSBarry Smith times[0][k] = second(); 1645d28107eSBarry Smith for (j=0; j<N; j++) 1655d28107eSBarry Smith c[j] = a[j]; 1665d28107eSBarry Smith times[0][k] = second() - times[0][k]; 1675d28107eSBarry Smith 168*01a79839SBarry Smith MPI_Barrier(MPI_COMM_WORLD); 1695d28107eSBarry Smith times[1][k] = second(); 1705d28107eSBarry Smith for (j=0; j<N; j++) 1715d28107eSBarry Smith b[j] = scalar*c[j]; 1725d28107eSBarry Smith times[1][k] = second() - times[1][k]; 1735d28107eSBarry Smith 174*01a79839SBarry Smith MPI_Barrier(MPI_COMM_WORLD); 1755d28107eSBarry Smith times[2][k] = second(); 1765d28107eSBarry Smith for (j=0; j<N; j++) 1775d28107eSBarry Smith c[j] = a[j]+b[j]; 1785d28107eSBarry Smith times[2][k] = second() - times[2][k]; 1795d28107eSBarry Smith 180*01a79839SBarry Smith MPI_Barrier(MPI_COMM_WORLD); 1815d28107eSBarry Smith times[3][k] = second(); 1825d28107eSBarry Smith for (j=0; j<N; j++) 1835d28107eSBarry Smith a[j] = b[j]+scalar*c[j]; 1845d28107eSBarry Smith times[3][k] = second() - times[3][k]; 1855d28107eSBarry Smith } 1865d28107eSBarry Smith 1875d28107eSBarry Smith /* --- SUMMARY --- */ 1885d28107eSBarry Smith 1895d28107eSBarry Smith for (k=0; k<NTIMES; k++) 1905d28107eSBarry Smith { 1915d28107eSBarry Smith for (j=0; j<4; j++) 1925d28107eSBarry Smith { 1935d28107eSBarry Smith rmstime[j] = rmstime[j] + (times[j][k] * times[j][k]); 1945d28107eSBarry Smith mintime[j] = MIN(mintime[j], times[j][k]); 1955d28107eSBarry Smith maxtime[j] = MAX(maxtime[j], times[j][k]); 1965d28107eSBarry Smith } 1975d28107eSBarry Smith } 1985d28107eSBarry Smith 1995d28107eSBarry Smith printf("Function Rate (MB/s) RMS time Min time Max time\n"); 2005d28107eSBarry Smith for (j=0; j<4; j++) { 2015d28107eSBarry Smith rmstime[j] = sqrt(rmstime[j]/(double)NTIMES); 2025d28107eSBarry Smith 2035d28107eSBarry Smith printf("%s%11.4f %11.4f %11.4f %11.4f\n", label[j], 2045d28107eSBarry Smith 1.0E-06 * bytes[j]/mintime[j], 2055d28107eSBarry Smith rmstime[j], 2065d28107eSBarry Smith mintime[j], 2075d28107eSBarry Smith maxtime[j]); 2085d28107eSBarry Smith } 209*01a79839SBarry Smith MPI_Finalize(); 2105d28107eSBarry Smith return 0; 2115d28107eSBarry Smith } 2125d28107eSBarry Smith 2135d28107eSBarry Smith # define M 20 2145d28107eSBarry Smith 2155d28107eSBarry Smith int 2165d28107eSBarry Smith checktick() 2175d28107eSBarry Smith { 2185d28107eSBarry Smith int i, minDelta, Delta; 2195d28107eSBarry Smith double t1, t2, timesfound[M]; 2205d28107eSBarry Smith 2215d28107eSBarry Smith /* Collect a sequence of M unique time values from the system. */ 2225d28107eSBarry Smith 2235d28107eSBarry Smith for (i = 0; i < M; i++) { 2245d28107eSBarry Smith t1 = second(); 2255d28107eSBarry Smith while( ((t2=second()) - t1) < 1.0E-6 ) 2265d28107eSBarry Smith ; 2275d28107eSBarry Smith timesfound[i] = t1 = t2; 2285d28107eSBarry Smith } 2295d28107eSBarry Smith 2305d28107eSBarry Smith /* 2315d28107eSBarry Smith * Determine the minimum difference between these M values. 2325d28107eSBarry Smith * This result will be our estimate (in microseconds) for the 2335d28107eSBarry Smith * clock granularity. 2345d28107eSBarry Smith */ 2355d28107eSBarry Smith 2365d28107eSBarry Smith minDelta = 1000000; 2375d28107eSBarry Smith for (i = 1; i < M; i++) { 2385d28107eSBarry Smith Delta = (int)( 1.0E6 * (timesfound[i]-timesfound[i-1])); 2395d28107eSBarry Smith minDelta = MIN(minDelta, MAX(Delta,0)); 2405d28107eSBarry Smith } 2415d28107eSBarry Smith 2425d28107eSBarry Smith return(minDelta); 2435d28107eSBarry Smith } 2445d28107eSBarry Smith 245