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 <float.h> 46 # include <limits.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 #ifndef N 58 # define N 2000000 59 #endif 60 #ifndef NTIMES 61 # define NTIMES 10 62 #endif 63 #ifndef 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 # ifndef MIN 91 # define MIN(x,y) ((x)<(y)?(x):(y)) 92 # endif 93 # ifndef 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 char *label[4] = {"Copy: ", "Scale: ", 105 "Add: ", "Triad: "}; 106 107 static double bytes[4] = { 108 2 * sizeof(double) * N, 109 2 * sizeof(double) * N, 110 3 * sizeof(double) * N, 111 3 * sizeof(double) * N 112 }; 113 114 extern double mysecond(); 115 extern void checkSTREAMresults(); 116 #ifdef TUNED 117 extern void tuned_STREAM_Copy(); 118 extern void tuned_STREAM_Scale(double scalar); 119 extern void tuned_STREAM_Add(); 120 extern void tuned_STREAM_Triad(double scalar); 121 #endif 122 extern int omp_get_num_threads(); 123 int main() 124 { 125 int quantum, checktick(); 126 int BytesPerWord; 127 register int j, k; 128 double scalar, t, times[4][NTIMES]; 129 130 /* --- SETUP --- determine precision and check timing --- */ 131 132 printf(HLINE); 133 printf("STREAM version $Revision: 5.9 $\n"); 134 printf(HLINE); 135 BytesPerWord = sizeof(double); 136 printf("This system uses %d bytes per DOUBLE PRECISION word.\n", 137 BytesPerWord); 138 139 printf(HLINE); 140 #ifdef NO_LONG_LONG 141 printf("Array size = %d, Offset = %d\n" , N, OFFSET); 142 #else 143 printf("Array size = %llu, Offset = %d\n", (unsigned long long) N, OFFSET); 144 #endif 145 146 printf("Total memory required = %.1f MB.\n", 147 (3.0 * BytesPerWord) * ( (double) N / 1048576.0)); 148 printf("Each test is run %d times, but only\n", NTIMES); 149 printf("the *best* time for each is used.\n"); 150 151 printf(HLINE); 152 #pragma omp parallel 153 { 154 #pragma omp master 155 { 156 k = omp_get_num_threads(); 157 printf ("Number of Threads requested = %i\n",k); 158 } 159 } 160 161 printf(HLINE); 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) 174 printf("Your clock granularity/precision appears to be " 175 "%d microseconds.\n", quantum); 176 else { 177 printf("Your clock granularity appears to be " 178 "less than one microsecond.\n"); 179 quantum = 1; 180 } 181 182 t = mysecond(); 183 #pragma omp parallel for 184 for (j = 0; j < N; j++) 185 a[j] = 2.0E0 * a[j]; 186 t = 1.0E6 * (mysecond() - t); 187 188 printf("Each test below will take on the order" 189 " of %d microseconds.\n", (int) t ); 190 printf(" (= %d clock ticks)\n", (int) (t/quantum) ); 191 printf("Increase the size of the arrays if this shows that\n"); 192 printf("you are not getting at least 20 clock ticks per test.\n"); 193 194 printf(HLINE); 195 196 /* --- MAIN LOOP --- repeat test cases NTIMES times --- */ 197 198 scalar = 3.0; 199 for (k=0; k<NTIMES; k++) 200 { 201 times[0][k] = mysecond(); 202 #ifdef TUNED 203 tuned_STREAM_Copy(); 204 #else 205 #pragma omp parallel for 206 for (j=0; j<N; j++) 207 c[j] = a[j]; 208 #endif 209 times[0][k] = mysecond() - times[0][k]; 210 211 times[1][k] = mysecond(); 212 #ifdef TUNED 213 tuned_STREAM_Scale(scalar); 214 #else 215 #pragma omp parallel for 216 for (j=0; j<N; j++) 217 b[j] = scalar*c[j]; 218 #endif 219 times[1][k] = mysecond() - times[1][k]; 220 221 times[2][k] = mysecond(); 222 #ifdef TUNED 223 tuned_STREAM_Add(); 224 #else 225 #pragma omp parallel for 226 for (j=0; j<N; j++) 227 c[j] = a[j]+b[j]; 228 #endif 229 times[2][k] = mysecond() - times[2][k]; 230 231 times[3][k] = mysecond(); 232 #ifdef TUNED 233 tuned_STREAM_Triad(scalar); 234 #else 235 #pragma omp parallel for 236 for (j=0; j<N; j++) 237 a[j] = b[j]+scalar*c[j]; 238 #endif 239 times[3][k] = mysecond() - times[3][k]; 240 } 241 242 /* --- SUMMARY --- */ 243 244 for (k=1; k<NTIMES; k++) /* note -- skip first iteration */ 245 { 246 for (j=0; j<4; j++) 247 { 248 avgtime[j] = avgtime[j] + times[j][k]; 249 mintime[j] = MIN(mintime[j], times[j][k]); 250 maxtime[j] = MAX(maxtime[j], times[j][k]); 251 } 252 } 253 254 printf("Function Rate (MB/s) Avg time Min time Max time\n"); 255 for (j=0; j<4; j++) { 256 avgtime[j] = avgtime[j]/(double)(NTIMES-1); 257 258 printf("%s%11.4f %11.4f %11.4f %11.4f\n", label[j], 259 1.0E-06 * bytes[j]/mintime[j], 260 avgtime[j], 261 mintime[j], 262 maxtime[j]); 263 } 264 printf(HLINE); 265 266 /* --- Check Results --- */ 267 checkSTREAMresults(); 268 printf(HLINE); 269 270 return 0; 271 } 272 273 # define M 20 274 275 int 276 checktick() 277 { 278 int i, minDelta, Delta; 279 double t1, t2, timesfound[M]; 280 281 /* Collect a sequence of M unique time values from the system. */ 282 283 for (i = 0; i < M; i++) { 284 t1 = mysecond(); 285 while( ((t2=mysecond()) - t1) < 1.0E-6 ) 286 ; 287 timesfound[i] = t1 = t2; 288 } 289 290 /* 291 * Determine the minimum difference between these M values. 292 * This result will be our estimate (in microseconds) for the 293 * clock granularity. 294 */ 295 296 minDelta = 1000000; 297 for (i = 1; i < M; i++) { 298 Delta = (int)( 1.0E6 * (timesfound[i]-timesfound[i-1])); 299 minDelta = MIN(minDelta, MAX(Delta,0)); 300 } 301 302 return(minDelta); 303 } 304 305 306 307 /* A gettimeofday routine to give access to the wall 308 clock timer on most UNIX-like systems. */ 309 310 #include <sys/time.h> 311 312 double mysecond() 313 { 314 struct timeval tp; 315 struct timezone tzp; 316 int i; 317 318 i = gettimeofday(&tp,&tzp); 319 return ( (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6 ); 320 } 321 322 void checkSTREAMresults () 323 { 324 double aj,bj,cj,scalar; 325 double asum,bsum,csum; 326 double epsilon; 327 int j,k; 328 329 /* reproduce initialization */ 330 aj = 1.0; 331 bj = 2.0; 332 cj = 0.0; 333 /* a[] is modified during timing check */ 334 aj = 2.0E0 * aj; 335 /* now execute timing loop */ 336 scalar = 3.0; 337 for (k=0; k<NTIMES; k++) 338 { 339 cj = aj; 340 bj = scalar*cj; 341 cj = aj+bj; 342 aj = bj+scalar*cj; 343 } 344 aj = aj * (double) (N); 345 bj = bj * (double) (N); 346 cj = cj * (double) (N); 347 348 asum = 0.0; 349 bsum = 0.0; 350 csum = 0.0; 351 for (j=0; j<N; j++) { 352 asum += a[j]; 353 bsum += b[j]; 354 csum += c[j]; 355 } 356 #ifdef VERBOSE 357 printf ("Results Comparison: \n"); 358 printf (" Expected : %f %f %f \n",aj,bj,cj); 359 printf (" Observed : %f %f %f \n",asum,bsum,csum); 360 #endif 361 362 #ifndef abs 363 #define abs(a) ((a) >= 0 ? (a) : -(a)) 364 #endif 365 epsilon = 1.e-8; 366 367 if (abs(aj-asum)/asum > epsilon) { 368 printf ("Failed Validation on array a[]\n"); 369 printf (" Expected : %f \n",aj); 370 printf (" Observed : %f \n",asum); 371 } 372 else if (abs(bj-bsum)/bsum > epsilon) { 373 printf ("Failed Validation on array b[]\n"); 374 printf (" Expected : %f \n",bj); 375 printf (" Observed : %f \n",bsum); 376 } 377 else if (abs(cj-csum)/csum > epsilon) { 378 printf ("Failed Validation on array c[]\n"); 379 printf (" Expected : %f \n",cj); 380 printf (" Observed : %f \n",csum); 381 } 382 else { 383 printf ("Solution Validates\n"); 384 } 385 } 386 387 void tuned_STREAM_Copy() 388 { 389 int j; 390 #pragma omp parallel for 391 for (j=0; j<N; j++) 392 c[j] = a[j]; 393 } 394 395 void tuned_STREAM_Scale(double scalar) 396 { 397 int j; 398 #pragma omp parallel for 399 for (j=0; j<N; j++) 400 b[j] = scalar*c[j]; 401 } 402 403 void tuned_STREAM_Add() 404 { 405 int j; 406 #pragma omp parallel for 407 for (j=0; j<N; j++) 408 c[j] = a[j]+b[j]; 409 } 410 411 void tuned_STREAM_Triad(double scalar) 412 { 413 int j; 414 #pragma omp parallel for 415 for (j=0; j<N; j++) 416 a[j] = b[j]+scalar*c[j]; 417 } 418