Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
replaced discrete_uniform function by rand_int64 for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaiBapchya committed Nov 14, 2018
1 parent 274366b commit 0b08216
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
36 changes: 13 additions & 23 deletions include/mxnet/random_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class RandGenerator<cpu, DType> {

MSHADOW_XINLINE int rand() { return engine_->operator()(); }

MSHADOW_XINLINE int64_t rand_int64() {
int64_t result = (engine_->operator()() << 32) + engine_->operator()();
return result;
}

MSHADOW_XINLINE FType uniform() {
typedef typename std::conditional<std::is_integral<DType>::value,
std::uniform_int_distribution<DType>,
Expand All @@ -73,15 +78,6 @@ class RandGenerator<cpu, DType> {
return dist_uniform(*engine_);
}

MSHADOW_XINLINE IType discrete_uniform(const int64_t lower, const int64_t upper) {
typedef typename std::conditional<sizeof(IType) != sizeof(int32_t) ||
sizeof(IType) != sizeof(int64_t),
std::uniform_int_distribution<int>,
std::uniform_int_distribution<IType>>::type GType;
GType dist_discrete_uniform(lower, upper);
return dist_discrete_uniform(*engine_);
}

MSHADOW_XINLINE FType normal() {
std::normal_distribution<FType> dist_normal;
return dist_normal(*engine_);
Expand Down Expand Up @@ -148,6 +144,10 @@ class RandGenerator<gpu, DType> {
return curand(&state_);
}

MSHADOW_FORCE_INLINE __device__ int64_t rand_int64() {
return static_cast<int64_t>(curand(&state_) << 32) + curand(&state_);
}

MSHADOW_FORCE_INLINE __device__ float uniform() {
return static_cast<float>(1.0) - curand_uniform(&state_);
}
Expand All @@ -156,13 +156,6 @@ class RandGenerator<gpu, DType> {
return curand_normal(&state_);
}

MSHADOW_FORCE_INLINE __device__ int discrete_uniform(const int64_t lower, const int64_t upper) {
float randu_f = curand_uniform(&state_);
randu_f *= (upper-lower+0.999999);
randu_f += lower;
return static_cast<int>(trunc(randu_f));
}

private:
RandGenerator<gpu, DType> *global_gen_;
int global_state_idx_;
Expand Down Expand Up @@ -207,6 +200,10 @@ class RandGenerator<gpu, double> {
return curand(&state_);
}

MSHADOW_FORCE_INLINE __device__ int64_t rand_int64() {
return static_cast<int64_t>(curand(&state_) << 32) + curand(&state_);
}

MSHADOW_FORCE_INLINE __device__ double uniform() {
return static_cast<float>(1.0) - curand_uniform_double(&state_);
}
Expand All @@ -215,13 +212,6 @@ class RandGenerator<gpu, double> {
return curand_normal_double(&state_);
}

MSHADOW_FORCE_INLINE __device__ int discrete_uniform(const int64_t lower, const int64_t upper) {
float randu_f = curand_uniform(&state_);
randu_f *= (upper-lower+0.999999);
randu_f += lower;
return static_cast<int>(trunc(randu_f));
}

private:
RandGenerator<gpu, double> *global_gen_;
int global_state_idx_;
Expand Down
7 changes: 6 additions & 1 deletion src/operator/random/sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ struct SampleRandIntKernel {
const IType *lower, const IType *upper, OType *out) {
RNG_KERNEL_LOOP(xpu, OType, id, gen, N, step, {
index_t nBatch(1 + (nSample - 1) / nParm);
out[i] = OType(genImpl.discrete_uniform(lower[i / nBatch], upper[i / nBatch]));
if (sizeof(IType) == sizeof(int64_t))
out[i] = OType(lower[i / nBatch] +
(upper[i / nBatch] - lower[i / nBatch]) * genImpl.rand_int64());
else
out[i] = OType(lower[i / nBatch] +
(upper[i / nBatch] - lower[i / nBatch]) * genImpl.rand());
});
}
};
Expand Down

0 comments on commit 0b08216

Please sign in to comment.