21 #ifndef ROCRAND_DISCRETE_H_
22 #define ROCRAND_DISCRETE_H_
26 #include "rocrand/rocrand_lfsr113.h"
27 #include "rocrand/rocrand_mrg31k3p.h"
28 #include "rocrand/rocrand_mrg32k3a.h"
29 #include "rocrand/rocrand_mtgp32.h"
30 #include "rocrand/rocrand_philox4x32_10.h"
31 #include "rocrand/rocrand_scrambled_sobol32.h"
32 #include "rocrand/rocrand_scrambled_sobol64.h"
33 #include "rocrand/rocrand_sobol32.h"
34 #include "rocrand/rocrand_sobol64.h"
35 #include "rocrand/rocrand_threefry2x32_20.h"
36 #include "rocrand/rocrand_threefry2x64_20.h"
37 #include "rocrand/rocrand_threefry4x32_20.h"
38 #include "rocrand/rocrand_threefry4x64_20.h"
39 #include "rocrand/rocrand_xorwow.h"
41 #include "rocrand/rocrand_discrete_types.h"
51 namespace rocrand_device {
54 __forceinline__ __device__ __host__
unsigned int
55 discrete_alias(
const double x,
56 const unsigned int size,
57 const unsigned int offset,
58 const unsigned int* __restrict__ alias,
59 const double* __restrict__ probability)
64 const double nx = size * x;
65 const double fnx = floor(nx);
66 const double y = nx - fnx;
67 const unsigned int i =
static_cast<unsigned int>(fnx);
68 return offset + (y < probability[i] ? i : alias[i]);
71 __forceinline__ __device__ __host__
unsigned int
77 __forceinline__ __device__ __host__
unsigned int
80 constexpr
double inv_double_32 = ROCRAND_2POW32_INV_DOUBLE;
81 const double x = r * inv_double_32;
82 return discrete_alias(x, dis);
86 __forceinline__ __device__ __host__
unsigned int
89 constexpr
double inv_double_32 = ROCRAND_2POW32_INV_DOUBLE;
90 const double x = r * inv_double_32;
91 return discrete_alias(x, dis);
94 __forceinline__ __device__ __host__
unsigned int
97 constexpr
double inv_double_64 = ROCRAND_2POW64_INV_DOUBLE;
98 const double x = r * inv_double_64;
99 return discrete_alias(x, dis);
102 __forceinline__ __device__ __host__
unsigned int discrete_cdf(
const double x,
103 const unsigned int size,
104 const unsigned int offset,
105 const double* __restrict__ cdf)
109 unsigned int min = 0;
110 unsigned int max = size - 1;
113 const unsigned int center = (min + max) / 2;
114 const double p = cdf[center];
129 __forceinline__ __device__ __host__
unsigned int
135 __forceinline__ __device__ __host__
unsigned int
138 constexpr
double inv_double_32 = ROCRAND_2POW32_INV_DOUBLE;
139 const double x = r * inv_double_32;
140 return discrete_cdf(x, dis);
144 __forceinline__ __device__ __host__
unsigned int
147 constexpr
double inv_double_32 = ROCRAND_2POW32_INV_DOUBLE;
148 const double x = r * inv_double_32;
149 return discrete_cdf(x, dis);
152 __forceinline__ __device__ __host__
unsigned int
155 constexpr
double inv_double_64 = ROCRAND_2POW64_INV_DOUBLE;
156 const double x = r * inv_double_64;
157 return discrete_cdf(x, dis);
180 __forceinline__ __device__ __host__
unsigned int
184 return rocrand_device::detail::discrete_alias(
rocrand(state), *discrete_distribution);
204 rocrand_device::detail::discrete_alias(u4.x, *discrete_distribution),
205 rocrand_device::detail::discrete_alias(u4.y, *discrete_distribution),
206 rocrand_device::detail::discrete_alias(u4.z, *discrete_distribution),
207 rocrand_device::detail::discrete_alias(u4.w, *discrete_distribution)
223 __forceinline__ __device__ __host__
unsigned int
227 return rocrand_device::detail::discrete_alias(
rocrand(state), *discrete_distribution);
242 __forceinline__ __device__ __host__
unsigned int
246 return rocrand_device::detail::discrete_alias(
rocrand(state), *discrete_distribution);
261 __forceinline__ __device__ __host__
unsigned int
265 return rocrand_device::detail::discrete_alias(
rocrand(state), *discrete_distribution);
280 __forceinline__ __device__
unsigned int
284 return rocrand_device::detail::discrete_cdf(
rocrand(state), *discrete_distribution);
299 __forceinline__ __device__ __host__
unsigned int
303 return rocrand_device::detail::discrete_cdf(
rocrand(state), *discrete_distribution);
318 __forceinline__ __device__ __host__
unsigned int
322 return rocrand_device::detail::discrete_cdf(
rocrand(state), *discrete_distribution);
337 __forceinline__ __device__ __host__
unsigned int
341 return rocrand_device::detail::discrete_cdf(
rocrand(state), *discrete_distribution);
356 __forceinline__ __device__ __host__
unsigned int
360 return rocrand_device::detail::discrete_cdf(
rocrand(state), *discrete_distribution);
375 __forceinline__ __device__ __host__
unsigned int
379 return rocrand_device::detail::discrete_cdf(
rocrand(state), *discrete_distribution);
394 __forceinline__ __device__ __host__
unsigned int
398 return rocrand_device::detail::discrete_cdf(
rocrand(state), *discrete_distribution);
413 __forceinline__ __device__ __host__
unsigned int
417 return rocrand_device::detail::discrete_cdf(
rocrand(state), *discrete_distribution);
432 __forceinline__ __device__ __host__
unsigned int
436 return rocrand_device::detail::discrete_cdf(
rocrand(state), *discrete_distribution);
451 __forceinline__ __device__ __host__
unsigned int
455 return rocrand_device::detail::discrete_cdf(
rocrand(state), *discrete_distribution);
__forceinline__ __device__ __host__ uint4 rocrand4(rocrand_state_philox4x32_10 *state)
Returns four uniformly distributed random unsigned int values from [0; 2^32 - 1] range.
Definition: rocrand_philox4x32_10.h:386
__forceinline__ __device__ __host__ uint4 rocrand_discrete4(rocrand_state_philox4x32_10 *state, const rocrand_discrete_distribution discrete_distribution)
Returns four discrete distributed unsigned int values.
Definition: rocrand_discrete.h:199
__forceinline__ __device__ __host__ unsigned int rocrand_discrete(rocrand_state_philox4x32_10 *state, const rocrand_discrete_distribution discrete_distribution)
Returns a discrete distributed unsigned int value.
Definition: rocrand_discrete.h:181
__forceinline__ __device__ __host__ unsigned int rocrand(rocrand_state_lfsr113 *state)
Returns uniformly distributed random unsigned int value from [0; 2^32 - 1] range.
Definition: rocrand_lfsr113.h:274
Represents a discrete probability distribution.
Definition: rocrand_discrete_types.h:26
unsigned int size
Number of entries in the probability table.
Definition: rocrand_discrete_types.h:28
unsigned int * alias
Alias table.
Definition: rocrand_discrete_types.h:33
double * cdf
Cumulative distribution function.
Definition: rocrand_discrete_types.h:38
double * probability
Probability data for the alias table.
Definition: rocrand_discrete_types.h:35
unsigned int offset
The distribution can be offset.
Definition: rocrand_discrete_types.h:30