random123

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

pi_opencl_kernel.ocl (2520B)


      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  * This file is the OpenCL kernel.  It gets preprocessed, munged
     34  * into a C string declaration and included in pi_opencl, so that
     35  * running the compiled pi_opencl does not depend on any include
     36  * files, paths etc.
     37  */
     38 
     39 #include <Random123/threefry.h>
     40 
     41 /*
     42  * counthits generates n x,y points and returns hits[tid] with
     43  * the count of number of those points within the unit circle on
     44  * each thread.
     45  */
     46 __kernel void counthits(unsigned n, unsigned useed, __global uint2 *hitsp) {
     47     unsigned tid = get_global_id(0);
     48     unsigned hits = 0, tries = 0;
     49     threefry4x32_key_t k = {{tid, useed}};
     50     threefry4x32_ctr_t c = {{}}; // start counter from 0
     51     while (tries < n) {
     52 	union {
     53 	    threefry4x32_ctr_t c;
     54 	    int4 i;
     55 	} u;
     56 	c.v[0]++;
     57 	u.c = threefry4x32(c, k);
     58 	long x1 = u.i.x, y1 = u.i.y;
     59 	long x2 = u.i.z, y2 = u.i.w;
     60 	if ((x1*x1 + y1*y1) < (1L<<62)) {
     61 	    hits++;
     62 	}
     63 	tries++;
     64 	if ((x2*x2 + y2*y2) < (1L<<62)) {
     65 	    hits++;
     66 	}
     67 	tries++;
     68     }
     69     hitsp[tid].x = hits;
     70     hitsp[tid].y = tries;
     71 }