pi_opencl.c (3963B)
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 OpenCL device kernel and host main program to 33 // compute pi via random darts at a square 34 35 // util_opencl.h has a large amount of OpenCL boilerplate. 36 // It contains nothing RNG-specific. 37 #include "../tests/util_opencl.h" 38 #include "pi_check.h" 39 #include "example_seeds.h" 40 41 static const char *opencl_src = 42 // pi_opencl_kernel.i contains a literal string 43 // with the kernel source code. It's generated 44 // by ./gencl.sh opencl_kernel.ocl 45 #include "pi_opencl_kernel.i" 46 ; 47 48 const char *progname; 49 int verbose = 0; 50 int debug = 0; 51 52 int 53 main(int argc, char **argv) 54 { 55 unsigned seed = example_seed_u32(EXAMPLE_SEED9_U32); // example user-settable seed 56 unsigned count = argc > 1 ? atoi(argv[1]) : 0; 57 UCLInfo *infop; 58 size_t i, nthreads, hits_sz; 59 cl_mem hits_dev; 60 cl_uint2 *hits_host; 61 const char *kernelname = "counthits"; 62 cl_int err; 63 cl_kernel kern; 64 double d = 0.; 65 66 d = timer(&d); 67 progname = argv[0]; 68 verbose = debug = argc > 2 ? atoi(argv[2]): 0; 69 infop = opencl_init(argc > 3 ? argv[3] : NULL, opencl_src, argc > 4 ? argv[4] : ""); 70 CHECKERR(kern = clCreateKernel(infop->prog, kernelname, &err)); 71 if (infop->wgsize > 64) infop->wgsize /= 2; 72 nthreads = infop->cores * infop->wgsize; 73 if (count == 0) 74 count = NTRIES/nthreads; 75 hits_sz = nthreads * sizeof(hits_host[0]); 76 CHECKNOTZERO(hits_host = (cl_uint2 *)malloc(hits_sz)); 77 CHECKERR(hits_dev = clCreateBuffer(infop->ctx, CL_MEM_WRITE_ONLY, hits_sz, 0, &err)); 78 CHECK(clSetKernelArg(kern, 0, sizeof(unsigned), (void*)&count)); 79 CHECK(clSetKernelArg(kern, 1, sizeof(unsigned), (void*)&seed)); 80 CHECK(clSetKernelArg(kern, 2, sizeof(cl_mem), (void*)&hits_dev)); 81 printf("queuing kernel for %lu threads with %lu work group size, %u points with seed 0x%x\n", 82 (unsigned long)nthreads, (unsigned long)infop->wgsize, count, seed); 83 CHECK(clEnqueueNDRangeKernel(infop->cmdq, kern, 1, 0, &nthreads, &infop->wgsize, 0, 0, 0)); 84 CHECK(clFinish(infop->cmdq)); 85 CHECK(clEnqueueReadBuffer(infop->cmdq, hits_dev, CL_TRUE, 0, hits_sz, hits_host, 0, 0, 0)); 86 87 unsigned long hits = 0, tries = 0; 88 for (i = 0; i < nthreads; i++) { 89 if (debug) 90 printf("%lu %u %u\n", (unsigned long)i, hits_host[i].x, hits_host[i].y); 91 hits += hits_host[i].x; 92 tries += hits_host[i].y; 93 } 94 CHECK(clReleaseMemObject(hits_dev)); 95 CHECK(clReleaseKernel(kern)); 96 opencl_done(infop); 97 return pi_check(hits, tries); 98 }