21 #ifndef ROCRAND_MRG31K3P_H_
22 #define ROCRAND_MRG31K3P_H_
24 #include "rocrand/rocrand_common.h"
25 #include "rocrand/rocrand_mrg31k3p_precomputed.h"
27 #include <hip/hip_runtime.h>
29 #define ROCRAND_MRG31K3P_M1 2147483647U
30 #define ROCRAND_MRG31K3P_M2 2147462579U
31 #define ROCRAND_MRG31K3P_MASK12 511U
32 #define ROCRAND_MRG31K3P_MASK13 16777215U
33 #define ROCRAND_MRG31K3P_MASK21 65535U
34 #define ROCRAND_MRG31K3P_NORM_DOUBLE (4.656612875245796923e-10)
35 #define ROCRAND_MRG31K3P_UINT32_NORM \
36 (2.000000001396983862)
46 #define ROCRAND_MRG31K3P_DEFAULT_SEED 12345ULL
49 namespace rocrand_device
57 #ifndef ROCRAND_DETAIL_BM_NOT_IN_STATE
63 double boxmuller_double;
64 float boxmuller_float;
70 __forceinline__ __device__ __host__ mrg31k3p_engine()
83 __forceinline__ __device__ __host__ mrg31k3p_engine(
const unsigned long long seed,
84 const unsigned long long subsequence,
85 const unsigned long long offset)
87 this->seed(seed, subsequence, offset);
98 __forceinline__ __device__ __host__
void seed(
unsigned long long seed_value,
99 const unsigned long long subsequence,
100 const unsigned long long offset)
106 unsigned int x =
static_cast<unsigned int>(seed_value ^ 0x55555555U);
107 unsigned int y =
static_cast<unsigned int>((seed_value >> 32) ^ 0xAAAAAAAAU);
108 m_state.x1[0] = mod_mul_m1(x, seed_value);
109 m_state.x1[1] = mod_mul_m1(y, seed_value);
110 m_state.x1[2] = mod_mul_m1(x, seed_value);
111 m_state.x2[0] = mod_mul_m2(y, seed_value);
112 m_state.x2[1] = mod_mul_m2(x, seed_value);
113 m_state.x2[2] = mod_mul_m2(y, seed_value);
114 this->restart(subsequence, offset);
118 __forceinline__ __device__ __host__
void discard(
unsigned long long offset)
120 this->discard_impl(offset);
125 __forceinline__ __device__ __host__
void discard_subsequence(
unsigned long long subsequence)
127 this->discard_subsequence_impl(subsequence);
132 __forceinline__ __device__ __host__
void discard_sequence(
unsigned long long sequence)
134 this->discard_sequence_impl(sequence);
137 __forceinline__ __device__ __host__
void restart(
const unsigned long long subsequence,
138 const unsigned long long offset)
140 #ifndef ROCRAND_DETAIL_BM_NOT_IN_STATE
141 m_state.boxmuller_float = ROCRAND_NAN_FLOAT;
142 m_state.boxmuller_double = ROCRAND_NAN_DOUBLE;
144 this->discard_subsequence_impl(subsequence);
145 this->discard_impl(offset);
148 __forceinline__ __device__ __host__
unsigned int operator()()
154 __forceinline__ __device__ __host__
unsigned int next()
158 = (((m_state.x1[1] & ROCRAND_MRG31K3P_MASK12) << 22) + (m_state.x1[1] >> 9))
159 + (((m_state.x1[2] & ROCRAND_MRG31K3P_MASK13) << 7) + (m_state.x1[2] >> 24));
160 tmp -= (tmp >= ROCRAND_MRG31K3P_M1) ? ROCRAND_MRG31K3P_M1 : 0;
161 tmp += m_state.x1[2];
162 tmp -= (tmp >= ROCRAND_MRG31K3P_M1) ? ROCRAND_MRG31K3P_M1 : 0;
163 m_state.x1[2] = m_state.x1[1];
164 m_state.x1[1] = m_state.x1[0];
168 tmp = (((m_state.x2[0] & ROCRAND_MRG31K3P_MASK21) << 15) + 21069 * (m_state.x2[0] >> 16));
169 tmp -= (tmp >= ROCRAND_MRG31K3P_M2) ? ROCRAND_MRG31K3P_M2 : 0;
170 tmp += ((m_state.x2[2] & ROCRAND_MRG31K3P_MASK21) << 15);
171 tmp -= (tmp >= ROCRAND_MRG31K3P_M2) ? ROCRAND_MRG31K3P_M2 : 0;
172 tmp += 21069 * (m_state.x2[2] >> 16);
173 tmp -= (tmp >= ROCRAND_MRG31K3P_M2) ? ROCRAND_MRG31K3P_M2 : 0;
174 tmp += m_state.x2[2];
175 tmp -= (tmp >= ROCRAND_MRG31K3P_M2) ? ROCRAND_MRG31K3P_M2 : 0;
176 m_state.x2[2] = m_state.x2[1];
177 m_state.x2[1] = m_state.x2[0];
181 return m_state.x1[0] - m_state.x2[0]
182 + (m_state.x1[0] <= m_state.x2[0] ? ROCRAND_MRG31K3P_M1 : 0);
187 __forceinline__ __device__ __host__
void discard_impl(
unsigned long long offset)
189 discard_state(offset);
193 __forceinline__ __device__ __host__
void
194 discard_subsequence_impl(
unsigned long long subsequence)
198 while(subsequence > 0)
202 #if defined(__HIP_DEVICE_COMPILE__)
203 mod_mat_vec_m1(d_mrg31k3p_A1P72 + i, m_state.x1);
204 mod_mat_vec_m2(d_mrg31k3p_A2P72 + i, m_state.x2);
206 mod_mat_vec_m1(h_mrg31k3p_A1P72 + i, m_state.x1);
207 mod_mat_vec_m2(h_mrg31k3p_A2P72 + i, m_state.x2);
216 __forceinline__ __device__ __host__
void discard_sequence_impl(
unsigned long long sequence)
224 #if defined(__HIP_DEVICE_COMPILE__)
225 mod_mat_vec_m1(d_mrg31k3p_A1P134 + i, m_state.x1);
226 mod_mat_vec_m2(d_mrg31k3p_A2P134 + i, m_state.x2);
228 mod_mat_vec_m1(h_mrg31k3p_A1P134 + i, m_state.x1);
229 mod_mat_vec_m2(h_mrg31k3p_A2P134 + i, m_state.x2);
238 __forceinline__ __device__ __host__
void discard_state(
unsigned long long offset)
246 #if defined(__HIP_DEVICE_COMPILE__)
247 mod_mat_vec_m1(d_mrg31k3p_A1 + i, m_state.x1);
248 mod_mat_vec_m2(d_mrg31k3p_A2 + i, m_state.x2);
250 mod_mat_vec_m1(h_mrg31k3p_A1 + i, m_state.x1);
251 mod_mat_vec_m2(h_mrg31k3p_A2 + i, m_state.x2);
260 __forceinline__ __device__ __host__
void discard_state()
266 __forceinline__ __device__ __host__
static void mod_mat_vec_m1(
const unsigned int* A,
269 unsigned long long x[3] = {s[0], s[1], s[2]};
271 s[0] = mod_m1(mod_m1(A[0] * x[0]) + mod_m1(A[1] * x[1]) + mod_m1(A[2] * x[2]));
273 s[1] = mod_m1(mod_m1(A[3] * x[0]) + mod_m1(A[4] * x[1]) + mod_m1(A[5] * x[2]));
275 s[2] = mod_m1(mod_m1(A[6] * x[0]) + mod_m1(A[7] * x[1]) + mod_m1(A[8] * x[2]));
278 __forceinline__ __device__ __host__
static void mod_mat_vec_m2(
const unsigned int* A,
281 unsigned long long x[3] = {s[0], s[1], s[2]};
283 s[0] = mod_m2(mod_m2(A[0] * x[0]) + mod_m2(A[1] * x[1]) + mod_m2(A[2] * x[2]));
285 s[1] = mod_m2(mod_m2(A[3] * x[0]) + mod_m2(A[4] * x[1]) + mod_m2(A[5] * x[2]));
287 s[2] = mod_m2(mod_m2(A[6] * x[0]) + mod_m2(A[7] * x[1]) + mod_m2(A[8] * x[2]));
290 __forceinline__ __device__ __host__
static unsigned long long mod_mul_m1(
unsigned int i,
291 unsigned long long j)
293 return mod_m1(i * j);
296 __forceinline__ __device__ __host__
static unsigned long long mod_m1(
unsigned long long p)
298 return p % ROCRAND_MRG31K3P_M1;
301 __forceinline__ __device__ __host__
static unsigned long long mod_mul_m2(
unsigned int i,
302 unsigned long long j)
304 return mod_m2(i * j);
307 __forceinline__ __device__ __host__
static unsigned long long mod_m2(
unsigned long long p)
309 return p % ROCRAND_MRG31K3P_M2;
314 mrg31k3p_state m_state;
316 #ifndef ROCRAND_DETAIL_BM_NOT_IN_STATE
317 friend struct detail::engine_boxmuller_helper<mrg31k3p_engine>;
329 typedef rocrand_device::mrg31k3p_engine rocrand_state_mrg31k3p;
343 __forceinline__ __device__ __host__
345 const unsigned long long subsequence,
346 const unsigned long long offset,
347 rocrand_state_mrg31k3p* state)
349 *state = rocrand_state_mrg31k3p(seed, subsequence, offset);
364 __forceinline__ __device__ __host__
365 unsigned int rocrand(rocrand_state_mrg31k3p* state)
368 return static_cast<unsigned int>((state->next() - 1) * ROCRAND_MRG31K3P_UINT32_NORM);
379 __forceinline__ __device__ __host__
380 void skipahead(
unsigned long long offset, rocrand_state_mrg31k3p* state)
382 return state->discard(offset);
394 __forceinline__ __device__ __host__
397 return state->discard_subsequence(subsequence);
409 __forceinline__ __device__ __host__
412 return state->discard_sequence(sequence);
#define ROCRAND_MRG31K3P_DEFAULT_SEED
Default seed for MRG31K3P PRNG.
Definition: rocrand_mrg31k3p.h:46
__forceinline__ __device__ __host__ unsigned int rocrand(rocrand_state_mrg31k3p *state)
Returns uniformly distributed random unsigned int value from [0; 2^32 - 1] range.
Definition: rocrand_mrg31k3p.h:365
__forceinline__ __device__ __host__ void skipahead_subsequence(unsigned long long subsequence, rocrand_state_mrg31k3p *state)
Updates MRG31K3P state to skip ahead by subsequence subsequences.
Definition: rocrand_mrg31k3p.h:395
__forceinline__ __device__ __host__ void skipahead(unsigned long long offset, rocrand_state_mrg31k3p *state)
Updates MRG31K3P state to skip ahead by offset elements.
Definition: rocrand_mrg31k3p.h:380
__forceinline__ __device__ __host__ void skipahead_sequence(unsigned long long sequence, rocrand_state_mrg31k3p *state)
Updates MRG31K3P state to skip ahead by sequence sequences.
Definition: rocrand_mrg31k3p.h:410
__forceinline__ __device__ __host__ void rocrand_init(const unsigned long long seed, const unsigned long long subsequence, const unsigned long long offset, rocrand_state_mrg31k3p *state)
Initializes MRG31K3P state.
Definition: rocrand_mrg31k3p.h:344