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