random123

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

pi_cudapp.cu (4020B)


      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 // Simple CUDA device kernel and host main program to
     33 // compute pi via random darts at a square
     34 
     35 // functions for boilerplate CUDA init and done
     36 #include "../tests/util_cuda.h"
     37 
     38 #include <Random123/philox.h>
     39 
     40 using namespace r123;
     41 
     42 int debug = 0;
     43 const char *progname;
     44 
     45 
     46 // CUDA Kernel:
     47 // generates n x,y points and returns hits[tid] with the count of number
     48 // of those points within the unit circle on each thread.
     49 __global__ void counthits(unsigned n, unsigned useed, uint2 *hitsp)
     50 {
     51     unsigned tid = blockDim.x * blockIdx.x + threadIdx.x;
     52     unsigned hits = 0, tries = 0;
     53     typedef Philox4x32 G;
     54     G rng;
     55     G::key_type k = {{tid, useed}};
     56     G::ctr_type c = {{}};
     57 
     58     while (tries < n) {
     59 	union {
     60 	    G::ctr_type c;
     61 	    int4 i;
     62 	}u;
     63 	c.incr();
     64 	u.c = rng(c, k);
     65 	int64_t x1 = u.i.x, y1 = u.i.y;
     66 	int64_t x2 = u.i.z, y2 = u.i.w;
     67 	if ((x1*x1 + y1*y1) < (1LL<<62)) {
     68 	    hits++;
     69 	}
     70 	tries++;
     71 	if ((x2*x2 + y2*y2) < (1LL<<62)) {
     72 	    hits++;
     73 	}
     74 	tries++;
     75     }
     76     hitsp[tid] = make_uint2(hits, tries);
     77 }
     78 
     79 #include "pi_check.h"
     80 #include "example_seeds.h"
     81 
     82 int
     83 main(int argc, char **argv)
     84 {
     85     unsigned seed = example_seed_u32(EXAMPLE_SEED9_U32); // example user-settable seed
     86     CUDAInfo *infop;
     87     uint2 *hits_host, *hits_dev;
     88     size_t hits_sz;
     89     unsigned nthreads;
     90     unsigned count = argc > 1 ? atoi(argv[1]) : 0;
     91     double d  = 0.;
     92 
     93     d = timer(&d);
     94     progname = argv[0];
     95     debug = argc > 2 ? atoi(argv[2]): 0;
     96 
     97     infop = cuda_init(argc > 3 ? argv[3] : NULL);
     98     nthreads =  infop->blocks_per_grid * infop->threads_per_block;
     99     if (count == 0)
    100 	count = NTRIES/nthreads;
    101 
    102     hits_sz = nthreads * sizeof(hits_host[0]);
    103     CHECKCALL(cudaMalloc(&hits_dev, hits_sz));
    104     CHECKNOTZERO((hits_host = (uint2 *)malloc(hits_sz)));
    105 
    106     printf("starting %u blocks with %u threads/block for %u points each with seed 0x%x\n",
    107 	   infop->blocks_per_grid, infop->threads_per_block, count, seed);
    108     fflush(stdout);
    109 
    110     counthits<<<infop->blocks_per_grid, infop->threads_per_block>>>(count, seed, hits_dev);
    111 
    112     CHECKCALL(cudaDeviceSynchronize());
    113     CHECKCALL(cudaMemcpy(hits_host, hits_dev, nthreads*sizeof(hits_dev[0]),
    114 		   cudaMemcpyDeviceToHost));
    115 
    116     unsigned long hits = 0, tries = 0;
    117     for (unsigned i = 0; i < nthreads; i++) {
    118 	if (debug)
    119 	    printf("%u %u %u\n", i, hits_host[i].x, hits_host[i].y);
    120 	hits += hits_host[i].x;
    121 	tries += hits_host[i].y;
    122     }
    123     CHECKCALL(cudaFree(hits_dev));
    124     free(hits_host);
    125     cuda_done(infop);
    126     return pi_check(hits, tries);
    127 }