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