diff options
Diffstat (limited to 'src/random.h')
-rw-r--r-- | src/random.h | 85 |
1 files changed, 45 insertions, 40 deletions
diff --git a/src/random.h b/src/random.h index 6abc415..e9d2e75 100644 --- a/src/random.h +++ b/src/random.h @@ -5,76 +5,81 @@ #include "physical_constants.h" -__device__ float uniform(curandState *s, const float &low, const float &high) +__device__ float +uniform(curandState *s, const float &low, const float &high) { - return low + curand_uniform(s)*(high-low); + return low + curand_uniform(s)*(high-low); } -__device__ float3 uniform_sphere(curandState *s) +__device__ float3 +uniform_sphere(curandState *s) { - float theta = uniform(s, 0.0f, 2*PI); - float u = uniform(s, -1.0f, 1.0f); - float c = sqrtf(1.0f-u*u); + float theta = uniform(s, 0.0f, 2*PI); + float u = uniform(s, -1.0f, 1.0f); + float c = sqrtf(1.0f-u*u); - return make_float3(c*cosf(theta), c*sinf(theta), u); + return make_float3(c*cosf(theta), c*sinf(theta), u); } // Draw a random sample given a cumulative distribution function // Assumptions: ncdf >= 2, cdf_y[0] is 0.0, and cdf_y[ncdf-1] is 1.0 -__device__ float sample_cdf(curandState *rng, int ncdf, - float *cdf_x, float *cdf_y) +__device__ float +sample_cdf(curandState *rng, int ncdf, float *cdf_x, float *cdf_y) { - float u = curand_uniform(rng); - - // Find u in cdf_y with binary search - // list must contain at least 2 elements: 0.0 and 1.0 - int lower=0; - int upper=ncdf-1; - while(lower < upper-1) { - int half = (lower+upper) / 2; - if (u < cdf_y[half]) - upper = half; - else - lower = half; - } + float u = curand_uniform(rng); + + // Find u in cdf_y with binary search + // list must contain at least 2 elements: 0.0 and 1.0 + int lower=0; + int upper=ncdf-1; + while(lower < upper-1) { + int half = (lower+upper) / 2; + if (u < cdf_y[half]) + upper = half; + else + lower = half; + } - float frac = (u - cdf_y[lower]) / (cdf_y[upper] - cdf_y[lower]); - return cdf_x[lower] * frac + cdf_x[upper] * (1.0f - frac); + float frac = (u - cdf_y[lower]) / (cdf_y[upper] - cdf_y[lower]); + return cdf_x[lower] * frac + cdf_x[upper] * (1.0f - frac); } - extern "C" { -__global__ void init_rng(int nthreads, curandState *s, unsigned long long seed, unsigned long long offset) +__global__ void +init_rng(int nthreads, curandState *s, unsigned long long seed, + unsigned long long offset) { - int id = blockIdx.x*blockDim.x + threadIdx.x; + int id = blockIdx.x*blockDim.x + threadIdx.x; - if (id >= nthreads) - return; + if (id >= nthreads) + return; - curand_init(seed, id, offset, &s[id]); + curand_init(seed, id, offset, &s[id]); } -__global__ void fill_uniform(int nthreads, curandState *s, float *a, float low, float high) +__global__ void +fill_uniform(int nthreads, curandState *s, float *a, float low, float high) { - int id = blockIdx.x*blockDim.x + threadIdx.x; + int id = blockIdx.x*blockDim.x + threadIdx.x; - if (id >= nthreads) - return; + if (id >= nthreads) + return; - a[id] = uniform(&s[id], low, high); + a[id] = uniform(&s[id], low, high); } -__global__ void fill_uniform_sphere(int nthreads, curandState *s, float3 *a) +__global__ void +fill_uniform_sphere(int nthreads, curandState *s, float3 *a) { - int id = blockIdx.x*blockDim.x + threadIdx.x; + int id = blockIdx.x*blockDim.x + threadIdx.x; - if (id >= nthreads) - return; + if (id >= nthreads) + return; - a[id] = uniform_sphere(&s[id]); + a[id] = uniform_sphere(&s[id]); } } // extern "c" |