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