pi_cuda.cu (3990B)
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 int debug = 0; 41 const char *progname; 42 43 // CUDA Kernel: 44 // generates n x,y points and returns hits[tid] with the count of number 45 // of those points within the unit circle on each thread. 46 __global__ void counthits(unsigned n, unsigned useed, uint2 *hitsp) 47 { 48 unsigned tid = blockDim.x * blockIdx.x + threadIdx.x; 49 unsigned hits = 0, tries = 0; 50 philox4x32_key_t k = {{tid, useed}}; 51 philox4x32_ctr_t c = {{}}; // start counter from 0 52 53 while (tries < n) { 54 union { 55 philox4x32_ctr_t c; 56 int4 i; 57 }u; 58 c.v[0] = tries; 59 u.c = philox4x32(c, k); 60 int64_t x1 = u.i.x, y1 = u.i.y; 61 int64_t x2 = u.i.z, y2 = u.i.w; 62 if ((x1*x1 + y1*y1) < (1LL<<62)) { 63 hits++; 64 } 65 tries++; 66 if ((x2*x2 + y2*y2) < (1LL<<62)) { 67 hits++; 68 } 69 tries++; 70 } 71 hitsp[tid] = make_uint2(hits, tries); 72 } 73 74 #include "pi_check.h" 75 #include "example_seeds.h" 76 77 int 78 main(int argc, char **argv) 79 { 80 unsigned seed = example_seed_u32(EXAMPLE_SEED9_U32); // example user-settable seed 81 CUDAInfo *infop; 82 uint2 *hits_host, *hits_dev; 83 size_t hits_sz; 84 unsigned nthreads; 85 unsigned count = argc > 1 ? atoi(argv[1]) : 0; 86 double d = 0.; 87 88 d = timer(&d); 89 progname = argv[0]; 90 debug = argc > 2 ? atoi(argv[2]): 0; 91 92 infop = cuda_init(argc > 3 ? argv[3] : NULL); 93 nthreads = infop->blocks_per_grid * infop->threads_per_block; 94 if (count == 0) 95 count = NTRIES/nthreads; 96 97 hits_sz = nthreads * sizeof(hits_host[0]); 98 CHECKCALL(cudaMalloc(&hits_dev, hits_sz)); 99 CHECKNOTZERO((hits_host = (uint2 *)malloc(hits_sz))); 100 101 printf("starting %u blocks with %u threads/block for %u points each with seed 0x%x\n", 102 infop->blocks_per_grid, infop->threads_per_block, count, seed); 103 fflush(stdout); 104 105 counthits<<<infop->blocks_per_grid, infop->threads_per_block>>>(count, seed, hits_dev); 106 107 CHECKCALL(cudaDeviceSynchronize()); 108 CHECKCALL(cudaMemcpy(hits_host, hits_dev, hits_sz, cudaMemcpyDeviceToHost)); 109 110 unsigned long long hits = 0, tries = 0; 111 for (unsigned i = 0; i < nthreads; i++) { 112 if (debug) 113 printf("%u %u %u\n", i, hits_host[i].x, hits_host[i].y); 114 hits += hits_host[i].x; 115 tries += hits_host[i].y; 116 } 117 CHECKCALL(cudaFree(hits_dev)); 118 free(hits_host); 119 cuda_done(infop); 120 return pi_check(hits, tries); 121 }