random123

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

time_opencl.c (7558B)


      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  * OpenCL test and timing harness for Random123 RNGs.  Uses macros
     34  * and util_expandtpl.h to "templatize" over all the different
     35  * permutations of RNGs and NxW and R.
     36  */
     37 
     38 #define R123_USE_AES_NI	0 /* never use this for OpenCL */
     39 
     40 #include "util_opencl.h"
     41 
     42 #include "Random123/philox.h"
     43 #include "Random123/threefry.h"
     44 
     45 #include "time_misc.h"
     46 #include "util_print.h"
     47 
     48 #define TEST_TPL(NAME, N, W, R) \
     49 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, UCLInfo *tp) \
     50 { \
     51     const char *kernelname = PREFIX #NAME #N "x" #W "_" #R; \
     52     NAME##N##x##W##_ctr_t *hC; \
     53     int n, niterations = numtrials; /* we make niterations + 2 (warmup, overhead) calls to the kernel */ \
     54     int narg; \
     55     double cur_time; \
     56     size_t i; \
     57     cl_int err; \
     58     cl_mem dC; \
     59     cl_kernel kern; \
     60     \
     61     /* create handle to kernel in program */ \
     62     dprintf(("%s kernel\n", kernelname)); \
     63     CHECKERR(kern = clCreateKernel(tp->prog, kernelname, &err)); \
     64     const size_t nworkitems = tp->wgsize * tp->cores; \
     65     const size_t szC = nworkitems*sizeof(hC[0]); \
     66     /* allocate and initialize vector of counters in host memory */ \
     67     CHECKNOTZERO(hC = (NAME##N##x##W##_ctr_t *) malloc(szC)); \
     68     for (i = 0; i < nworkitems; i++) { \
     69 	int xi; \
     70 	for (xi = 0; xi < N; xi++) \
     71 	    hC[i].v[xi] = 0; \
     72     } \
     73     /* allocate vector of counters in device memory, initialize from current host memory */ \
     74     CHECKERR(dC = clCreateBuffer(tp->ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, szC, hC, &err)); \
     75     cl_uint kcount = 0;                                                    \
     76     double basetime = 0., dt = 0., mindt = 0.; \
     77     /* first two iterations are for warmup & baseline */ \
     78     for (n = -2; n < niterations; n++) { \
     79 	if (n == -2) { \
     80 	    if (count == 0) { \
     81 		/* try to set a good guess for count */ \
     82 		count = (unsigned)(tp->cycles ? tp->cycles * 1e-8 : 10000); \
     83 		dprintf(("starting with count = %u\n", count)); \
     84 	    } \
     85 	    kcount = count; \
     86 	} else if (n == -1) { \
     87 	    /* use first iteration time to calibrate count to get approximately sec_per_trial */ \
     88 	    if (count > 1) { \
     89 		count = (unsigned)(count * sec_per_trial / dt); \
     90 		dprintf(("scaled count = %u\n", count)); \
     91 	    } \
     92 	    /* second iteration is to calculate overhead after warmup */ \
     93 	    kcount = 1; \
     94 	} else if (n == 0) { \
     95 	    int xj; \
     96 	    /* Check that we got the expected value */ \
     97 	    for (xj = 0; xj < N; xj++) { \
     98 		if (kactr.v[xj] != hC[0].v[xj]) { \
     99 		    printf("%s mismatch: xj = %d, expected\n", kernelname, xj); \
    100 		    printline_##NAME##N##x##W##_##R(ukey, ctr, &kactr, 1); \
    101 		    printf("    but got\n"); \
    102 		    printline_##NAME##N##x##W##_##R(ukey, ctr, hC, 1); \
    103 		    if(!debug) exit(1);                                 \
    104                     else break;                                         \
    105 		} else { \
    106 		    dprintf(("%s matched word %d\n", kernelname, xj)); \
    107 		} \
    108 	    } \
    109 	    basetime = dt; \
    110 	    if (debug||verbose) { \
    111 		dprintf(("%s %.3f secs for %lu workitems test on device %s\n", \
    112 		       kernelname, basetime, (unsigned long) nworkitems, \
    113 		       tp->devname)); \
    114 		printline_##NAME##N##x##W##_##R(ukey, ctr, hC, (verbose < 2) ? 1 : nworkitems); \
    115 	    } \
    116 	    kcount = count + 1; \
    117 	} \
    118 	(void)timer(&cur_time); \
    119 	dprintf(("setup arguments to kernel function %s\n", kernelname)); \
    120 	narg = 0; \
    121 	CHECK(clSetKernelArg(kern, narg, sizeof(kcount), (void *)&kcount)); \
    122 	narg++; \
    123 	CHECK(clSetKernelArg(kern, narg, sizeof(ctr), (void *)&ctr)); \
    124 	narg++; \
    125 	CHECK(clSetKernelArg(kern, narg, sizeof(ukey), (void *)&ukey)); \
    126 	narg++; \
    127 	CHECK(clSetKernelArg(kern, narg, sizeof(cl_mem), (void *)&dC)); \
    128 	dprintf(("queue kernel for execution on device %s\n", tp->devname)); \
    129 	CHECK(clEnqueueNDRangeKernel(tp->cmdq, kern, 1, 0, &nworkitems, &tp->wgsize, 0, 0, 0)); \
    130 	CHECK(clFlush(tp->cmdq)); \
    131 	dprintf(("copy results from device memory to host memory\n")); \
    132 	CHECK(clEnqueueReadBuffer(tp->cmdq, dC, CL_FALSE, 0, szC, hC, 0, 0, 0)); \
    133 	CHECK(clFlush(tp->cmdq)); \
    134 	dprintf(("synchronize\n")); \
    135 	CHECK(clFinish(tp->cmdq)); \
    136 	dt = timer(&cur_time); \
    137 	dprintf(("iteration %d took %.3f secs\n", n, dt)); \
    138 	ALLZEROS(hC, nworkitems, N); \
    139 	if (n == 0 || dt < mindt) mindt = dt; \
    140     } \
    141     if (count > 1) { \
    142 	double tpB = (mindt - basetime) / ( (count - 1.) * nworkitems * (N * W / 8.) ); \
    143 	printf("%-17s %#5.3g cpB, %#5.3g GB/s %u B granularity (best %u in %.3f s - %.6f s)\n", \
    144 	       kernelname + sizeof(PREFIX) - 1, \
    145 	       tpB * tp->cycles , 1e-9/tpB, \
    146 	       (unsigned)(N*W/8), count, mindt, basetime ); \
    147 	fflush(stdout); \
    148     } \
    149     /* free host and device memory */ \
    150     free(hC); \
    151     CHECK(clReleaseMemObject(dC)); \
    152     /* free the kernel */ \
    153     CHECK(clReleaseKernel(kern)); \
    154 }
    155 
    156 #include "util_expandtpl.h"
    157 
    158 /* Include the preprocessed source code, stashed in a literal string */
    159 const char *opencl_src = 
    160 #include "time_opencl_kernel.i"
    161      ;
    162 
    163 int main(int argc, char **argv)
    164 {
    165     const char *cp;
    166     unsigned count = 0;
    167     UCLInfo *infop;
    168     int keyctroffset = 0;
    169     
    170     progname = argv[0];
    171     if (argc > 3|| (argv[1] && argv[1][0] == '-')) {
    172 	fprintf(stderr, "Usage: %s [COUNT [DEVICESTRING]]\n", progname);
    173 	exit(1);
    174     }
    175     if (argc  > 1)
    176 	count = atoi(argv[1]);
    177     if ((cp = getenv("TIME_OPENCL_VERBOSE")) != NULL) {
    178 	verbose = atoi(cp);
    179     }
    180     if ((cp = getenv("TIME_OPENCL_DEBUG")) != NULL) {
    181 	debug = atoi(cp);
    182     }
    183     if ((cp = getenv("TIME_OPENCL_OFFSET")) != NULL) {
    184 	keyctroffset = atoi(cp);
    185     }
    186     if ((cp = getenv("TIME_OPENCL_NUMTRIALS")) != NULL) {
    187         numtrials = atoi(cp);
    188     }
    189     if ((cp = getenv("TIME_OPENCL_SEC_PER_TRIAL")) != NULL) {
    190         sec_per_trial = atof(cp);
    191     }
    192     if ((cp = getenv("TIME_OPENCL_BUILD_OPTIONS")) == NULL) {
    193 	cp = "";
    194     }
    195     infop = opencl_init(argc > 2 ? argv[2] : NULL, opencl_src, cp);
    196     /* define macro to initialize and call the kernels */
    197 #   include "time_initkeyctr.h"
    198     opencl_done(infop);
    199     return 0;
    200 }