Back to index

numactl  2.0.8~rc4
stream_lib.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <math.h>
00003 #include <float.h>
00004 #include <limits.h>
00005 #include <sys/time.h>
00006 #include <stdlib.h>
00007 #include "stream_lib.h"
00008 
00009 static inline double mysecond()
00010 {
00011        struct timeval tv;
00012        gettimeofday(&tv, NULL);
00013        return tv.tv_sec + tv.tv_usec * 1.e-6;
00014 }
00015 
00016 
00017 /*
00018  * Program: Stream
00019  * Programmer: Joe R. Zagar
00020  * Revision: 4.0-BETA, October 24, 1995
00021  * Original code developed by John D. McCalpin
00022  *
00023  * This program measures memory transfer rates in MB/s for simple
00024  * computational kernels coded in C.  These numbers reveal the quality
00025  * of code generation for simple uncacheable kernels as well as showing
00026  * the cost of floating-point operations relative to memory accesses.
00027  *
00028  * INSTRUCTIONS:
00029  *
00030  *     1) Stream requires a good bit of memory to run.  Adjust the
00031  *          value of 'N' (below) to give a 'timing calibration' of
00032  *          at least 20 clock-ticks.  This will provide rate estimates
00033  *          that should be good to about 5% precision.
00034  *
00035  * Hacked by AK to be a library
00036  */
00037 
00038 long N = 8000000;
00039 #define NTIMES       10
00040 #define OFFSET       0
00041 
00042 /*
00043  *     3) Compile the code with full optimization.  Many compilers
00044  *        generate unreasonably bad code before the optimizer tightens
00045  *        things up.  If the results are unreasonably good, on the
00046  *        other hand, the optimizer might be too smart for me!
00047  *
00048  *         Try compiling with:
00049  *               cc -O stream_d.c second_wall.c -o stream_d -lm
00050  *
00051  *         This is known to work on Cray, SGI, IBM, and Sun machines.
00052  *
00053  *
00054  *     4) Mail the results to mccalpin@cs.virginia.edu
00055  *        Be sure to include:
00056  *            a) computer hardware model number and software revision
00057  *            b) the compiler flags
00058  *            c) all of the output from the test case.
00059  * Thanks!
00060  *
00061  */
00062 
00063 int checktick();
00064 
00065 # define HLINE "-------------------------------------------------------------\n"
00066 
00067 # ifndef MIN
00068 # define MIN(x,y) ((x)<(y)?(x):(y))
00069 # endif
00070 # ifndef MAX
00071 # define MAX(x,y) ((x)>(y)?(x):(y))
00072 # endif
00073 
00074 static double *a, *b, *c;
00075 
00076 static double rmstime[4] = { 0 }, maxtime[4] = {
00077 0}, mintime[4] = {
00078 FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX};
00079 
00080 static char *label[4] = { "Copy:      ", "Scale:     ",
00081        "Add:       ", "Triad:     "
00082 };
00083 char *stream_names[] = { "Copy","Scale","Add","Triad" };
00084 
00085 static double bytes[4];
00086 
00087 int stream_verbose = 1;
00088 
00089 #define Vprintf(x...) do { if (stream_verbose) printf(x); } while(0)
00090 
00091 void stream_check(void)
00092 {
00093        int quantum;
00094        int BytesPerWord;
00095        register int j;
00096        double t;
00097 
00098        /* --- SETUP --- determine precision and check timing --- */
00099 
00100        Vprintf(HLINE);
00101        BytesPerWord = sizeof(double);
00102        Vprintf("This system uses %d bytes per DOUBLE PRECISION word.\n",
00103               BytesPerWord);
00104 
00105        Vprintf(HLINE);
00106        Vprintf("Array size = %lu, Offset = %d\n", N, OFFSET);
00107        Vprintf("Total memory required = %.1f MB.\n",
00108               (3 * N * BytesPerWord) / 1048576.0);
00109        Vprintf("Each test is run %d times, but only\n", NTIMES);
00110        Vprintf("the *best* time for each is used.\n");
00111 
00112        /* Get initial value for system clock. */
00113 
00114        for (j = 0; j < N; j++) {
00115               a[j] = 1.0;
00116               b[j] = 2.0;
00117               c[j] = 0.0;
00118        }
00119 
00120        Vprintf(HLINE);
00121 
00122        if ((quantum = checktick()) >= 1)
00123               Vprintf("Your clock granularity/precision appears to be "
00124                      "%d microseconds.\n", quantum);
00125        else
00126               Vprintf("Your clock granularity appears to be "
00127                      "less than one microsecond.\n");
00128 
00129        t = mysecond();
00130        for (j = 0; j < N; j++)
00131               a[j] = 2.0E0 * a[j];
00132        t = 1.0E6 * (mysecond() - t);
00133 
00134        Vprintf("Each test below will take on the order"
00135               " of %d microseconds.\n", (int) t);
00136        Vprintf("   (= %d clock ticks)\n", (int) (t / quantum));
00137        Vprintf("Increase the size of the arrays if this shows that\n");
00138        Vprintf("you are not getting at least 20 clock ticks per test.\n");
00139 
00140        Vprintf(HLINE);
00141 
00142        Vprintf("WARNING -- The above is only a rough guideline.\n");
00143        Vprintf("For best results, please be sure you know the\n");
00144        Vprintf("precision of your system timer.\n");
00145        Vprintf(HLINE);
00146 }
00147 
00148 void stream_test(double *res)
00149 {
00150        register int j, k;
00151        double scalar, times[4][NTIMES];
00152 
00153        /*  --- MAIN LOOP --- repeat test cases NTIMES times --- */
00154 
00155        scalar = 3.0;
00156        for (k = 0; k < NTIMES; k++) {
00157               times[0][k] = mysecond();
00158               for (j = 0; j < N; j++)
00159                      c[j] = a[j];
00160               times[0][k] = mysecond() - times[0][k];
00161 
00162               times[1][k] = mysecond();
00163               for (j = 0; j < N; j++)
00164                      b[j] = scalar * c[j];
00165               times[1][k] = mysecond() - times[1][k];
00166 
00167               times[2][k] = mysecond();
00168               for (j = 0; j < N; j++)
00169                      c[j] = a[j] + b[j];
00170               times[2][k] = mysecond() - times[2][k];
00171 
00172               times[3][k] = mysecond();
00173               for (j = 0; j < N; j++)
00174                      a[j] = b[j] + scalar * c[j];
00175               times[3][k] = mysecond() - times[3][k];
00176        }
00177 
00178        /*  --- SUMMARY --- */
00179 
00180        for (k = 0; k < NTIMES; k++) {
00181               for (j = 0; j < 4; j++) {
00182                      rmstime[j] =
00183                          rmstime[j] + (times[j][k] * times[j][k]);
00184                      mintime[j] = MIN(mintime[j], times[j][k]);
00185                      maxtime[j] = MAX(maxtime[j], times[j][k]);
00186               }
00187        }
00188 
00189        Vprintf
00190            ("Function      Rate (MB/s)   RMS time     Min time     Max time\n");
00191        for (j = 0; j < 4; j++) {
00192               double speed = 1.0E-06 * bytes[j] / mintime[j];
00193 
00194               rmstime[j] = sqrt(rmstime[j] / (double) NTIMES);
00195 
00196               Vprintf("%s%11.4f  %11.4f  %11.4f  %11.4f\n", label[j],
00197                      speed,
00198                      rmstime[j], mintime[j], maxtime[j]);
00199 
00200               if (res)
00201                      res[j] = speed;
00202 
00203        }
00204 }
00205 
00206 # define      M      20
00207 
00208 int checktick()
00209 {
00210        int i, minDelta, Delta;
00211        double t1, t2, timesfound[M];
00212 
00213 /*  Collect a sequence of M unique time values from the system. */
00214 
00215        for (i = 0; i < M; i++) {
00216               t1 = mysecond();
00217               while (((t2 = mysecond()) - t1) < 1.0E-6);
00218               timesfound[i] = t1 = t2;
00219        }
00220 
00221 /*
00222  * Determine the minimum difference between these M values.
00223  * This result will be our estimate (in microseconds) for the
00224  * clock granularity.
00225  */
00226 
00227        minDelta = 1000000;
00228        for (i = 1; i < M; i++) {
00229               Delta =
00230                   (int) (1.0E6 * (timesfound[i] - timesfound[i - 1]));
00231               minDelta = MIN(minDelta, MAX(Delta, 0));
00232        }
00233 
00234        return (minDelta);
00235 }
00236 
00237 void stream_setmem(unsigned long size)
00238 {
00239        N = (size - OFFSET) / (3*sizeof(double));
00240 }
00241 
00242 long stream_memsize(void)
00243 {
00244        return 3*(sizeof(double) * (N+OFFSET)) ;
00245 }
00246 
00247 long stream_init(void *mem)
00248 {
00249        int i;
00250 
00251        for (i = 0; i < 4; i++) {
00252               rmstime[i] = 0;
00253               maxtime[i] = 0;
00254               mintime[i] = FLT_MAX;
00255        }
00256 
00257        bytes[0] = 2 * sizeof(double) * N;
00258        bytes[1] = 2 * sizeof(double) * N;
00259        bytes[2] = 3 * sizeof(double) * N;
00260        bytes[3] = 3 * sizeof(double) * N;
00261 
00262        a = mem;
00263        b = (double *)mem +   (N+OFFSET);
00264        c = (double *)mem + 2*(N+OFFSET);
00265        stream_check();
00266        return 0;
00267 }