random123

Counter-based Random Number Generators
git clone git://git.meso-star.com/random123.git
Log | Files | Refs | README | LICENSE

time_serial.c (5939B)


      1 /*
      2 Copyright 2010-2011, D. E. Shaw Research.
      3 All rights reserved.
      4 
      5 Redistribution and use in source and binary forms, with or without
      6 modification, are permitted provided that the following conditions are
      7 met:
      8 
      9 * Redistributions of source code must retain the above copyright
     10   notice, this list of conditions, and the following disclaimer.
     11 
     12 * Redistributions in binary form must reproduce the above copyright
     13   notice, this list of conditions, and the following disclaimer in the
     14   documentation and/or other materials provided with the distribution.
     15 
     16 * Neither the name of D. E. Shaw Research nor the names of its
     17   contributors may be used to endorse or promote products derived from
     18   this software without specific prior written permission.
     19 
     20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 */
     32 /*
     33  * Single-core CPU test and timing harness for Random123 RNGs.
     34  * Uses macros and util_expandtpl.h to "templatize" over all the
     35  * different permutations of RNGs and NxW and R.
     36  */
     37 #include "util.h"
     38 
     39 #include "Random123/philox.h"
     40 #include "Random123/threefry.h"
     41 #include "Random123/ars.h"
     42 #include "Random123/aes.h"
     43 
     44 #include "time_misc.h"
     45 #include "util_print.h"
     46 
     47 #include "util.h"
     48 #define KERNEL R123_STATIC_INLINE
     49 #define get_global_id(i)    (i)
     50 #include "time_random123.h"
     51 
     52 static double cpu_hz = -1.;
     53 
     54 #define TEST_TPL(NAME, N, W, R) \
     55 void NAME##N##x##W##_##R(NAME##N##x##W##_ctr_t ctr, NAME##N##x##W##_ukey_t ukey, NAME##N##x##W##_ctr_t kactr, unsigned count, void* unused) \
     56 { \
     57     const char *kernelname = #NAME #N "x" #W "_" #R; \
     58     double cur_time; \
     59     int n, niterations = numtrials; /* we make niterations + 2 (warmup, overhead) calls to the kernel */ \
     60     unsigned kcount = 0; \
     61     double basetime = 0., dt = 0., mindt = 0.; \
     62     NAME##N##x##W##_ctr_t C, *hC = &C; \
     63     (void)unused; /* suppress warning */        \
     64     \
     65     for (n = -2; n < niterations; n++) { \
     66 	if (n == -2) { \
     67 	    if (count == 0) { \
     68 		/* try to set a good guess for count */ \
     69 		count = 1000000; \
     70 		dprintf(("starting with count = %u\n", count)); \
     71 	    } \
     72 	    kcount = count; \
     73 	} else if (n == -1) { \
     74 	    /* use first iteration time to calibrate count to get approximately sec_per_trial */ \
     75 	    if (count > 1) { \
     76 		count = (unsigned)(count * sec_per_trial / dt); \
     77 		dprintf(("scaled count = %u\n", count)); \
     78 	    } \
     79 	    /* second iteration is to calculate overhead after warmup */ \
     80 	    kcount = 1; \
     81 	} else if (n == 0) { \
     82 	    int xj; \
     83 	    /* Check that we got the expected value */ \
     84 	    for (xj = 0; xj < N; xj++) { \
     85 		if (kactr.v[xj] != hC[0].v[xj]) { \
     86 		    printf("%s mismatch: xj = %d, expected\n", kernelname, xj); \
     87 		    printline_##NAME##N##x##W##_##R(ukey, ctr, &kactr, 1); \
     88 		    printf("    but got\n"); \
     89 		    printline_##NAME##N##x##W##_##R(ukey, ctr, hC, 1); \
     90 		    if(!debug) exit(1);                                 \
     91                     else break;                                         \
     92 		} else { \
     93 		    dprintf(("%s matched word %d\n", kernelname, xj)); \
     94 		} \
     95 	    } \
     96 	    basetime = dt; \
     97 	    if (debug||verbose) { \
     98 		dprintf(("%s %.3f secs\n", kernelname, basetime)); \
     99 		printline_##NAME##N##x##W##_##R(ukey, ctr, hC, 1); \
    100 	    } \
    101 	    kcount = count + 1; \
    102 	} \
    103 	/* calling timer *before* dprintf avoids an ARMv7 gcc 4.8.3 -O3 compiler bug! */ \
    104 	(void)timer(&cur_time); \
    105 	dprintf(("call function %s\n", kernelname)); \
    106 	test_##NAME##N##x##W##_##R(kcount, ctr, ukey, hC); \
    107 	dt = timer(&cur_time); \
    108 	dprintf(("iteration %d took %.3f secs\n", n, dt)); \
    109 	ALLZEROS(hC, 1, N); \
    110 	if (n == 0 || dt < mindt) mindt = dt; \
    111     } \
    112     if (count > 1) { \
    113 	double tpB = (mindt - basetime) / ( (kcount - 1.) * (N * W / 8.) ); \
    114         if(cpu_hz > 0.) \
    115             printf("%-17s %#5.3g cpB %#5.3g GB/s %u B granularity (best %u in %.3f s - %.6f s)\n", \
    116                    kernelname, tpB*cpu_hz, 1e-9/tpB,                    \
    117                    (unsigned)(N*W/8), kcount, mindt, basetime );        \
    118         else                                                            \
    119             printf("%-17s %#5.3g GB/s %u B granularity (best %u in %.3f s - %.6f s)\n", \
    120                    kernelname, 1e-9/tpB,                                \
    121                    (unsigned)(N*W/8), kcount, mindt, basetime );        \
    122 	fflush(stdout); \
    123     } \
    124 }
    125 
    126 #include "util_expandtpl.h"
    127 
    128 int main(int argc, char **argv)
    129 {
    130     char *cp;
    131     unsigned count = 0;
    132     int keyctroffset = 0;
    133     void* infop = NULL;
    134     
    135     progname = argv[0];
    136     if (argc > 3|| (argv[1] && argv[1][0] == '-')) {
    137 	fprintf(stderr, "Usage: %s [COUNT]]\n", progname);
    138 	exit(1);
    139     }
    140     if (argc > 1)
    141 	count = atoi(argv[1]);
    142     if ((cp = getenv("TIME_SERIAL_CPU_GHZ")) != NULL) {
    143         cpu_hz = 1.e9 * atof(cp);
    144     }
    145     if ((cp = getenv("TIME_SERIAL_VERBOSE")) != NULL) {
    146 	verbose = atoi(cp);
    147     }
    148     if ((cp = getenv("TIME_SERIAL_DEBUG")) != NULL) {
    149 	debug = atoi(cp);
    150     }
    151     if ((cp = getenv("TIME_SERIAL_OFFSET")) != NULL) {
    152 	keyctroffset = atoi(cp);
    153     }
    154     if ((cp = getenv("TIME_SERIAL_NUMTRIALS")) != NULL) {
    155         numtrials = atoi(cp);
    156     }
    157     if ((cp = getenv("TIME_SERIAL_SEC_PER_TRIAL")) != NULL) {
    158         sec_per_trial = atof(cp);
    159     }
    160 #   include "time_initkeyctr.h"
    161     return 0;
    162 }