21 #ifndef ROCRAND_SOBOL32_H_
22 #define ROCRAND_SOBOL32_H_
24 #include "rocrand/rocrand_common.h"
26 namespace rocrand_device {
28 template<
bool UseSharedVectors>
33 unsigned int vectors[32];
35 __forceinline__ __device__ __host__ sobol32_state() : d(), i(), vectors() {}
37 __forceinline__ __device__ __host__ sobol32_state(
const unsigned int d,
39 const unsigned int* vectors)
42 for(
int k = 0; k < 32; k++)
44 this->vectors[k] = vectors[k];
50 struct sobol32_state<true>
54 const unsigned int * vectors;
56 __forceinline__ __device__ __host__ sobol32_state() : d(), i(), vectors() {}
58 __forceinline__ __device__ __host__ sobol32_state(
const unsigned int d,
60 const unsigned int* vectors)
61 : d(d), i(i), vectors(vectors)
65 template<
bool UseSharedVectors>
70 typedef struct sobol32_state<UseSharedVectors> sobol32_state;
72 __forceinline__ __device__ __host__ sobol32_engine() {}
74 __forceinline__ __device__ __host__ sobol32_engine(
const unsigned int* vectors,
75 const unsigned int offset)
76 : m_state(0, 0, vectors)
78 discard_state(offset);
82 __forceinline__ __device__ __host__
void discard(
unsigned int offset)
84 discard_state(offset);
87 __forceinline__ __device__ __host__
void discard()
93 __forceinline__ __device__ __host__
void discard_stride(
unsigned int stride)
95 discard_state_power2(stride);
98 __forceinline__ __device__ __host__
unsigned int operator()()
103 __forceinline__ __device__ __host__
unsigned int next()
105 unsigned int p = m_state.d;
110 __forceinline__ __device__ __host__
unsigned int current()
const
115 __forceinline__ __device__ __host__
static constexpr
bool uses_shared_vectors()
117 return UseSharedVectors;
122 __forceinline__ __device__ __host__
void discard_state(
unsigned int offset)
125 const unsigned int g = m_state.i ^ (m_state.i >> 1);
127 for(
int i = 0; i < 32; i++)
129 m_state.d ^= (g & (1U << i) ? m_state.vectors[i] : 0);
134 __forceinline__ __device__ __host__
void discard_state()
136 m_state.d ^= m_state.vectors[rightmost_zero_bit(m_state.i)];
140 __forceinline__ __device__ __host__
void discard_state_power2(
unsigned int stride)
153 m_state.d ^= m_state.vectors[rightmost_zero_bit(~stride) - 1];
155 m_state.d ^= m_state.vectors[rightmost_zero_bit(m_state.i | (stride - 1))];
161 __forceinline__ __device__ __host__
unsigned int rightmost_zero_bit(
unsigned int x)
163 #if defined(__HIP_DEVICE_COMPILE__)
164 unsigned int z = __ffs(~x);
165 return z ? z - 1 : 0;
182 sobol32_state m_state;
194 typedef rocrand_device::sobol32_engine<false> rocrand_state_sobol32;
207 __forceinline__ __device__ __host__
void rocrand_init(
const unsigned int* vectors,
208 const unsigned int offset,
209 rocrand_state_sobol32* state)
211 *state = rocrand_state_sobol32(vectors, offset);
226 __forceinline__ __device__ __host__
unsigned int rocrand(rocrand_state_sobol32* state)
228 return state->next();
239 __forceinline__ __device__ __host__
void skipahead(
unsigned long long offset,
240 rocrand_state_sobol32* state)
242 return state->discard(offset);
__forceinline__ __device__ __host__ unsigned int rocrand(rocrand_state_sobol32 *state)
Returns uniformly distributed random unsigned int value from [0; 2^32 - 1] range.
Definition: rocrand_sobol32.h:226
__forceinline__ __device__ __host__ void rocrand_init(const unsigned int *vectors, const unsigned int offset, rocrand_state_sobol32 *state)
Initialize SOBOL32 state.
Definition: rocrand_sobol32.h:207
__forceinline__ __device__ __host__ void skipahead(unsigned long long offset, rocrand_state_sobol32 *state)
Updates SOBOL32 state to skip ahead by offset elements.
Definition: rocrand_sobol32.h:239