1 /*-----------------------------------------------------------------------*/ 2 /* Program: Stream */ 3 /* Revision: $Id: stream.c,v 5.9 2009/04/11 16:35:00 mccalpin Exp mccalpin $ */ 4 /* Original code developed by John D. McCalpin */ 5 /* Programmers: John D. McCalpin */ 6 /* Joe R. Zagar */ 7 /* */ 8 /* This program measures memory transfer rates in MB/s for simple */ 9 /* computational kernels coded in C. */ 10 /*-----------------------------------------------------------------------*/ 11 /* Copyright 1991-2005: John D. McCalpin */ 12 /*-----------------------------------------------------------------------*/ 13 /* License: */ 14 /* 1. You are free to use this program and/or to redistribute */ 15 /* this program. */ 16 /* 2. You are free to modify this program for your own use, */ 17 /* including commercial use, subject to the publication */ 18 /* restrictions in item 3. */ 19 /* 3. You are free to publish results obtained from running this */ 20 /* program, or from works that you derive from this program, */ 21 /* with the following limitations: */ 22 /* 3a. In order to be referred to as "STREAM benchmark results", */ 23 /* published results must be in conformance to the STREAM */ 24 /* Run Rules, (briefly reviewed below) published at */ 25 /* http://www.cs.virginia.edu/stream/ref.html */ 26 /* and incorporated herein by reference. */ 27 /* As the copyright holder, John McCalpin retains the */ 28 /* right to determine conformity with the Run Rules. */ 29 /* 3b. Results based on modified source code or on runs not in */ 30 /* accordance with the STREAM Run Rules must be clearly */ 31 /* labelled whenever they are published. Examples of */ 32 /* proper labelling include: */ 33 /* "tuned STREAM benchmark results" */ 34 /* "based on a variant of the STREAM benchmark code" */ 35 /* Other comparable, clear and reasonable labelling is */ 36 /* acceptable. */ 37 /* 3c. Submission of results to the STREAM benchmark web site */ 38 /* is encouraged, but not required. */ 39 /* 4. Use of this program or creation of derived works based on this */ 40 /* program constitutes acceptance of these licensing restrictions. */ 41 /* 5. Absolutely no warranty is expressed or implied. */ 42 /*-----------------------------------------------------------------------*/ 43 # include <stdio.h> 44 # include <math.h> 45 # include <limits.h> 46 # include <float.h> 47 # include <sys/time.h> 48 49 /* INSTRUCTIONS: 50 * 51 * 1) Stream requires a good bit of memory to run. Adjust the 52 * value of 'N' (below) to give a 'timing calibration' of 53 * at least 20 clock-ticks. This will provide rate estimates 54 * that should be good to about 5% precision. 55 */ 56 57 #if !defined(N) 58 # define N 2000000 59 #endif 60 #if !defined(NTIMES) 61 # define NTIMES 50 62 #endif 63 #if !defined(OFFSET) 64 # define OFFSET 0 65 #endif 66 67 /* 68 * 3) Compile the code with full optimization. Many compilers 69 * generate unreasonably bad code before the optimizer tightens 70 * things up. If the results are unreasonably good, on the 71 * other hand, the optimizer might be too smart for me! 72 * 73 * Try compiling with: 74 * cc -O stream_omp.c -o stream_omp 75 * 76 * This is known to work on Cray, SGI, IBM, and Sun machines. 77 * 78 * 79 * 4) Mail the results to mccalpin@cs.virginia.edu 80 * Be sure to include: 81 * a) computer hardware model number and software revision 82 * b) the compiler flags 83 * c) all of the output from the test case. 84 * Thanks! 85 * 86 */ 87 88 # define HLINE "-------------------------------------------------------------\n" 89 90 # if !defined(MIN) 91 # define MIN(x,y) ((x)<(y) ? (x) : (y)) 92 # endif 93 # if !defined(MAX) 94 # define MAX(x,y) ((x)>(y) ? (x) : (y)) 95 # endif 96 97 static double a[N+OFFSET], 98 b[N+OFFSET], 99 c[N+OFFSET]; 100 101 static double avgtime[4] = {0}, maxtime[4] = {0}, 102 mintime[4] = {FLT_MAX,FLT_MAX,FLT_MAX,FLT_MAX}; 103 104 static const char *label[4] = {"Copy: ", "Scale: ","Add: ", "Triad: "}; 105 106 static double bytes[4] = { 107 2 * sizeof(double) * N, 108 2 * sizeof(double) * N, 109 3 * sizeof(double) * N, 110 3 * sizeof(double) * N 111 }; 112 113 extern double mysecond(); 114 extern void checkSTREAMresults(); 115 #if defined(TUNED) 116 extern void tuned_STREAM_Copy(); 117 extern void tuned_STREAM_Scale(double scalar); 118 extern void tuned_STREAM_Add(); 119 extern void tuned_STREAM_Triad(double scalar); 120 #endif 121 extern int omp_get_num_threads(); 122 int main() 123 { 124 int quantum, checktick(); 125 int BytesPerWord; 126 register int j, k; 127 double scalar, t, times[4][NTIMES]; 128 129 /* --- SETUP --- determine precision and check timing --- */ 130 131 /*printf(HLINE); 132 printf("STREAM version $Revision: 5.9 $\n"); 133 printf(HLINE); */ 134 BytesPerWord = sizeof(double); 135 /* printf("This system uses %d bytes per DOUBLE PRECISION word.\n", 136 BytesPerWord); 137 138 printf(HLINE); 139 #if defined(NO_LONG_LONG) 140 printf("Array size = %d, Offset = %d\n" , N, OFFSET); 141 #else 142 printf("Array size = %llu, Offset = %d\n", (unsigned long long) N, OFFSET); 143 #endif 144 145 printf("Total memory required = %.1f MB.\n", 146 (3.0 * BytesPerWord) * ((double) N / 1048576.0)); 147 printf("Each test is run %d times, but only\n", NTIMES); 148 printf("the *best* time for each is used.\n"); 149 150 printf(HLINE); */ 151 #pragma omp parallel 152 { 153 #pragma omp master 154 { 155 k = omp_get_num_threads(); 156 printf(HLINE); 157 printf ("Number of OpenMP Threads requested = %i\n",k); 158 } 159 } 160 161 162 163 /* Get initial value for system clock. */ 164 #pragma omp parallel for 165 for (j=0; j<N; j++) { 166 a[j] = 1.0; 167 b[j] = 2.0; 168 c[j] = 0.0; 169 } 170 171 /*printf(HLINE);*/ 172 173 if ((quantum = checktick()) >= 1) ; /* printf("Your clock granularity/precision appears to be " 174 "%d microseconds.\n", quantum);*/ 175 else { 176 ; /* printf("Your clock granularity appears to be " 177 "less than one microsecond.\n");*/ 178 quantum = 1; 179 } 180 181 t = mysecond(); 182 #pragma omp parallel for 183 for (j = 0; j < N; j++) a[j] = 2.0E0 * a[j]; 184 t = 1.0E6 * (mysecond() - t); 185 186 /*printf("Each test below will take on the order" 187 " of %d microseconds.\n", (int) t); 188 printf(" (= %d clock ticks)\n", (int) (t/quantum)); 189 printf("Increase the size of the arrays if this shows that\n"); 190 printf("you are not getting at least 20 clock ticks per test.\n"); 191 192 printf(HLINE);*/ 193 194 /* --- MAIN LOOP --- repeat test cases NTIMES times --- */ 195 196 scalar = 3.0; 197 for (k=0; k<NTIMES; k++) 198 { 199 times[0][k] = mysecond(); 200 #if defined(TUNED) 201 tuned_STREAM_Copy(); 202 #else 203 #pragma omp parallel for 204 for (j=0; j<N; j++) c[j] = a[j]; 205 #endif 206 times[0][k] = mysecond() - times[0][k]; 207 208 times[1][k] = mysecond(); 209 #if defined(TUNED) 210 tuned_STREAM_Scale(scalar); 211 #else 212 #pragma omp parallel for 213 for (j=0; j<N; j++) b[j] = scalar*c[j]; 214 #endif 215 times[1][k] = mysecond() - times[1][k]; 216 217 times[2][k] = mysecond(); 218 #if defined(TUNED) 219 tuned_STREAM_Add(); 220 #else 221 #pragma omp parallel for 222 for (j=0; j<N; j++) c[j] = a[j]+b[j]; 223 #endif 224 times[2][k] = mysecond() - times[2][k]; 225 226 times[3][k] = mysecond(); 227 #if defined(TUNED) 228 tuned_STREAM_Triad(scalar); 229 #else 230 #pragma omp parallel for 231 for (j=0; j<N; j++) a[j] = b[j]+scalar*c[j]; 232 #endif 233 times[3][k] = mysecond() - times[3][k]; 234 } 235 236 /* --- SUMMARY --- */ 237 238 for (k=1; k<NTIMES; k++) /* note -- skip first iteration */ 239 for (j=0; j<4; j++) 240 { 241 avgtime[j] = avgtime[j] + times[j][k]; 242 mintime[j] = MIN(mintime[j], times[j][k]); 243 maxtime[j] = MAX(maxtime[j], times[j][k]); 244 } 245 246 printf("Function Rate (MB/s) \n"); 247 for (j=0; j<4; j++) { 248 avgtime[j] = avgtime[j]/(double)(NTIMES-1); 249 250 printf("%s%11.4f \n", label[j], 1.0E-06 * bytes[j]/mintime[j]); 251 } 252 /* printf(HLINE);*/ 253 254 /* --- Check Results --- */ 255 checkSTREAMresults(); 256 /* printf(HLINE);*/ 257 258 return 0; 259 } 260 261 # define M 20 262 263 int checktick() 264 { 265 int i, minDelta, Delta; 266 double t1, t2, timesfound[M]; 267 268 /* Collect a sequence of M unique time values from the system. */ 269 270 for (i = 0; i < M; i++) { 271 t1 = mysecond(); 272 while (((t2=mysecond()) - t1) < 1.0E-6) ; 273 timesfound[i] = t1 = t2; 274 } 275 276 /* 277 * Determine the minimum difference between these M values. 278 * This result will be our estimate (in microseconds) for the 279 * clock granularity. 280 */ 281 282 minDelta = 1000000; 283 for (i = 1; i < M; i++) { 284 Delta = (int)(1.0E6 * (timesfound[i]-timesfound[i-1])); 285 minDelta = MIN(minDelta, MAX(Delta,0)); 286 } 287 288 return(minDelta); 289 } 290 291 292 293 /* A gettimeofday routine to give access to the wall 294 clock timer on most UNIX-like systems. */ 295 296 #include <sys/time.h> 297 298 double mysecond() 299 { 300 struct timeval tp; 301 struct timezone tzp; 302 int i; 303 304 i = gettimeofday(&tp,&tzp); 305 return ((double) tp.tv_sec + (double) tp.tv_usec * 1.e-6); 306 } 307 308 void checkSTREAMresults() 309 { 310 double aj,bj,cj,scalar; 311 double asum,bsum,csum; 312 double epsilon; 313 int j,k; 314 315 /* reproduce initialization */ 316 aj = 1.0; 317 bj = 2.0; 318 cj = 0.0; 319 /* a[] is modified during timing check */ 320 aj = 2.0E0 * aj; 321 /* now execute timing loop */ 322 scalar = 3.0; 323 for (k=0; k<NTIMES; k++) 324 { 325 cj = aj; 326 bj = scalar*cj; 327 cj = aj+bj; 328 aj = bj+scalar*cj; 329 } 330 aj = aj * (double) (N); 331 bj = bj * (double) (N); 332 cj = cj * (double) (N); 333 334 asum = 0.0; 335 bsum = 0.0; 336 csum = 0.0; 337 for (j=0; j<N; j++) { 338 asum += a[j]; 339 bsum += b[j]; 340 csum += c[j]; 341 } 342 #if defined(VERBOSE) 343 printf ("Results Comparison: \n"); 344 printf (" Expected : %f %f %f \n",aj,bj,cj); 345 printf (" Observed : %f %f %f \n",asum,bsum,csum); 346 #endif 347 348 #if !defined(abs) 349 #define abs(a) ((a) >= 0 ? (a) : -(a)) 350 #endif 351 epsilon = 1.e-8; 352 353 if (abs(aj-asum)/asum > epsilon) { 354 printf ("Failed Validation on array a[]\n"); 355 printf (" Expected : %f \n",aj); 356 printf (" Observed : %f \n",asum); 357 } else if (abs(bj-bsum)/bsum > epsilon) { 358 printf ("Failed Validation on array b[]\n"); 359 printf (" Expected : %f \n",bj); 360 printf (" Observed : %f \n",bsum); 361 } else if (abs(cj-csum)/csum > epsilon) { 362 printf ("Failed Validation on array c[]\n"); 363 printf (" Expected : %f \n",cj); 364 printf (" Observed : %f \n",csum); 365 } else ; /* printf ("Solution Validates\n"); */ 366 } 367 368 void tuned_STREAM_Copy() 369 { 370 int j; 371 #pragma omp parallel for 372 for (j=0; j<N; j++) c[j] = a[j]; 373 } 374 375 void tuned_STREAM_Scale(double scalar) 376 { 377 int j; 378 #pragma omp parallel for 379 for (j=0; j<N; j++) b[j] = scalar*c[j]; 380 } 381 382 void tuned_STREAM_Add() 383 { 384 int j; 385 #pragma omp parallel for 386 for (j=0; j<N; j++) c[j] = a[j]+b[j]; 387 } 388 389 void tuned_STREAM_Triad(double scalar) 390 { 391 int j; 392 #pragma omp parallel for 393 for (j=0; j<N; j++) a[j] = b[j]+scalar*c[j]; 394 } 395