there are so many cores

Just another WordPress.com site

Random number generation works

RNG works now.

#define NSET 100000               // number of monte carlo trials

Arrayf32
compute_pi(void)
{
    RNGf32 G(RNG_PHILOX, 271828); // create an RNG
    Arrayf32 X = rng_uniform_make(G, NSET, 1, 0.0, 1.0);
    Arrayf32 Y = rng_uniform_make(G, NSET, 1, 0.0, 1.0);
    Arrayf32 distance_from_zero = sqrt(X * X + Y * Y);
    Arrayf32 inside_circle = (distance_from_zero <= 1.0f);
    return 4.0f * sum(inside_circle) / NSET;
}

This returns 3.138920 as an estimate for PI.

The generated OpenCL includes a subset of the Random123 library with the philox PRNG. Below are just the two kernels that call into the (dynamically generated) library code.

__kernel void f_2331120269860868340_1(
  __global float4 * a1)
{
  float4 r1;
  r1= (float4)(0);
  for (int i0 = 0; i0 < 25000; i0++)
  {
    r1+= ((0 == (sqrt((float4)((((philox_uniform_4_4((i0), 271828)) * (philox_uniform_4_4((i0), 271828))) + ((philox_uniform_4_4((i0), 271829)) * (philox_uniform_4_4((i0), 271829)))))) <= (float4)(1))) ? (float4)(0) : (float4)(1)) ;
  }
  a1[0] = r1 ;
}

__kernel void f_2331120269860868340_2(
  __global float4 * a1,
  __global float4 * a2)
{
  for (int i0 = 0; i0 < 1; i0++)
  {
    a1[(i0) + get_global_id(0) * 0] = (((float4)(4) * a2[(i0) + get_global_id(0) * 0]) / (float4)(100000)) ;
  }
}

Code quality is poor. It’s a bigger issue with PRNG support.

Conceptually, RNG violates referential transparency by definition. Even though PRNG is in reality deterministic, the code transformations still change – random numbers should appear random even when the process generating them is not.

The good thing is that the Random123 library is integrated into Chai. Both rng_uniform_make() and rng_normal_make() work properly with two PRNG variants: Philox; Threefry.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: