25 #ifndef MXNET_COMMON_RANDOM_GENERATOR_H_ 26 #define MXNET_COMMON_RANDOM_GENERATOR_H_ 33 #include <curand_kernel.h> 34 #include "../common/cuda_utils.h" 35 #endif // MXNET_USE_CUDA 43 template<
typename Device,
typename DType MSHADOW_DEFAULT_DTYPE>
46 template<
typename DType>
57 typedef typename std::conditional<std::is_floating_point<DType>::value,
61 : engine_(gen->states_ + state_idx) {}
63 Impl(
const Impl &) =
delete;
64 Impl &operator=(
const Impl &) =
delete;
66 MSHADOW_XINLINE
int rand() {
return engine_->operator()(); }
69 typedef typename std::conditional<std::is_integral<DType>::value,
70 std::uniform_int_distribution<DType>,
71 std::uniform_real_distribution<FType>>::type GType;
73 return dist_uniform(*engine_);
77 std::normal_distribution<FType> dist_normal;
78 return dist_normal(*engine_);
82 std::mt19937 *engine_;
86 inst->states_ =
new std::mt19937[kNumRandomStates];
90 delete[] inst->states_;
93 MSHADOW_XINLINE
void Seed(Stream<cpu> *, uint32_t seed) {
94 for (
int i = 0; i < kNumRandomStates; ++i) (states_ + i)->seed(seed + i);
98 std::mt19937 *states_;
101 template<
typename DType>
104 template<
typename DType>
109 template<
typename DType>
123 Impl &operator=(
const Impl &) =
delete;
124 Impl(
const Impl &) =
delete;
129 global_state_idx_(state_idx),
130 state_(*(gen->states_ + state_idx)) {}
134 global_gen_->states_[global_state_idx_] = state_;
137 MSHADOW_FORCE_INLINE __device__
int rand() {
138 return curand(&state_);
141 MSHADOW_FORCE_INLINE __device__
float uniform() {
142 return static_cast<float>(1.0) - curand_uniform(&state_);
145 MSHADOW_FORCE_INLINE __device__
float normal() {
146 return curand_normal(&state_);
151 int global_state_idx_;
152 curandStatePhilox4_32_10_t state_;
157 kNumRandomStates *
sizeof(curandStatePhilox4_32_10_t)));
164 void Seed(Stream<gpu> *s, uint32_t seed);
167 curandStatePhilox4_32_10_t *states_;
179 Impl &operator=(
const Impl &) =
delete;
180 Impl(
const Impl &) =
delete;
185 global_state_idx_(state_idx),
186 state_(*(gen->states_ + state_idx)) {}
190 global_gen_->states_[global_state_idx_] = state_;
193 MSHADOW_FORCE_INLINE __device__
int rand() {
194 return curand(&state_);
197 MSHADOW_FORCE_INLINE __device__
double uniform() {
198 return static_cast<float>(1.0) - curand_uniform_double(&state_);
201 MSHADOW_FORCE_INLINE __device__
double normal() {
202 return curand_normal_double(&state_);
207 int global_state_idx_;
208 curandStatePhilox4_32_10_t state_;
212 curandStatePhilox4_32_10_t *states_;
215 #endif // MXNET_USE_CUDA 220 #endif // MXNET_COMMON_RANDOM_GENERATOR_H_ static void FreeState(RandGenerator< gpu, DType > *inst)
Definition: random_generator.h:160
MSHADOW_FORCE_INLINE __device__ float normal()
Definition: random_generator.h:145
static void AllocState(RandGenerator< cpu, DType > *inst)
Definition: random_generator.h:85
Definition: random_generator.h:171
namespace of mxnet
Definition: base.h:127
static const int kMinNumRandomPerThread
Definition: random_generator.h:113
__device__ Impl(RandGenerator< gpu, DType > *gen, int state_idx)
Definition: random_generator.h:127
MSHADOW_XINLINE int rand()
Definition: random_generator.h:66
MSHADOW_XINLINE FType normal()
Definition: random_generator.h:76
__device__ ~Impl()
Definition: random_generator.h:132
static const int kNumRandomStates
Definition: random_generator.h:115
MSHADOW_FORCE_INLINE __device__ int rand()
Definition: random_generator.h:193
static void FreeState(RandGenerator< cpu, DType > *inst)
Definition: random_generator.h:89
MSHADOW_XINLINE void Seed(Stream< cpu > *, uint32_t seed)
Definition: random_generator.h:93
Impl(RandGenerator< cpu, DType > *gen, int state_idx)
Definition: random_generator.h:60
MSHADOW_FORCE_INLINE __device__ float uniform()
Definition: random_generator.h:141
std::conditional< std::is_floating_point< DType >::value, DType, double >::type FType
Definition: random_generator.h:58
static const int kNumRandomStates
Definition: random_generator.h:52
Definition: random_generator.h:110
mshadow::gpu gpu
mxnet gpu
Definition: base.h:131
static const int kMinNumRandomPerThread
Definition: random_generator.h:50
MSHADOW_XINLINE FType uniform()
Definition: random_generator.h:68
Definition: random_generator.h:47
mshadow::cpu cpu
mxnet cpu
Definition: base.h:129
MSHADOW_FORCE_INLINE __device__ int rand()
Definition: random_generator.h:137
__device__ ~Impl()
Definition: random_generator.h:188
static void AllocState(RandGenerator< gpu, DType > *inst)
Definition: random_generator.h:155
Definition: random_generator.h:44
MSHADOW_FORCE_INLINE __device__ double normal()
Definition: random_generator.h:201
#define CUDA_CALL(func)
Protected CUDA call.
Definition: cuda_utils.h:187
MSHADOW_FORCE_INLINE __device__ double uniform()
Definition: random_generator.h:197
__device__ Impl(RandGenerator< gpu, double > *gen, int state_idx)
Definition: random_generator.h:183