GPU Workshop Sample Code
 All Data Structures Namespaces Files Functions Variables Macros Pages
mycuda_random.h
Go to the documentation of this file.
1 #include <curand_kernel.h>
2 #define SQRT2 1.414213562373095f
3 #define ONE_OVER_SQRT2 0.707106781186547f
4 
5 
6 
7 
8 
10 
11 namespace mycuda_random {
12 
13 
14 __global__ void random_init( curandState_t *state, const int seed, const int n )
15 {
16  int tid = blockIdx.x*blockDim.x + threadIdx.x;
17  int nthreads = blockDim.x*gridDim.x;
18 
19  for ( int i=tid; i<n; i+=nthreads) {
20  curand_init( seed, i, 0, &state[tid] );
21  }
22 }
23 
24 
25 __global__ void random_normal( float *z, curandState_t *state, const int n )
26 {
27  int tid = blockIdx.x*blockDim.x + threadIdx.x;
28  int nthreads = blockDim.x*gridDim.x;
29 
30  curandState_t s = state[tid];
31  for ( int i=tid; i<n; i+=nthreads) {
32  z[i] = curand_normal( &s );
33  }
34  state[tid] = s;
35 }
36 
37 
38 
46 __global__ void random_uniform( float *z, curandState_t *state, const int n )
47 {
48  int tid = blockIdx.x*blockDim.x + threadIdx.x;
49  int nthreads = blockDim.x*gridDim.x;
50 
51  curandState_t s = state[tid];
52  for ( int i=tid; i<n; i+=nthreads) {
53  z[i] = curand_uniform( &s );
54  }
55  state[tid] = s;
56 }
57 
58 
59 
60 
61 
62 
63  template <typename T>
64 __host__ __device__ T norm_cdf_inv( T q )
65 {
66  return -SQRT2*erfcinv( 2.0f*q );
67 }
68 
69 
70 
71  template <typename T>
72 __host__ __device__ T norm_cdf( T z )
73 {
74  return .5f * ( 1.0f + erf( ONE_OVER_SQRT2 * z ));
75 }
76 
77 
78 } // end namespace mycuda_random
79 
80