25#include <cuda_runtime.h>
48 inline static constexpr unsigned min() {
return 0u; }
55 inline static constexpr unsigned max() {
return UINT_MAX; }
63 unsigned t =
x ^ (
x << 11);
67 return w = (
w ^ (
w >> 19)) ^ (t ^ (t >> 8));
74 std::random_device rd;
86 unsigned x = 123456789u,
y = 362436069u,
z = 521288629u,
w;
91template <
typename FloatType>
92inline curandStatus_t curand_generate_uniform_impl(curandGenerator_t generator,
95 static_assert(std::is_floating_point<FloatType>::value,
96 "FloatType must be float or double.");
97 static_assert(!std::is_same<FloatType, long double>::value,
98 "long double is not supported");
99 return CURAND_STATUS_SUCCESS;
104curand_generate_uniform_impl<float>(curandGenerator_t generator,
105 float *outputPtr,
size_t num) {
106 return curandGenerateUniform(generator, outputPtr, num);
111curand_generate_uniform_impl<double>(curandGenerator_t generator,
112 double *outputPtr,
size_t num) {
113 return curandGenerateUniformDouble(generator, outputPtr, num);
116template <
typename FloatType, curandRngType_t rng_type>
class CurandWrapper {
118 CurandWrapper(std::uint64_t seed) {
120 HANDLE_ERROR_CURAND(curandCreateGenerator(&_rng, rng_type));
122 HANDLE_ERROR_CURAND(curandSetPseudoRandomGeneratorSeed(_rng, seed));
125 CurandWrapper() : CurandWrapper(std::random_device{}()) {}
127 CurandWrapper(CurandWrapper &&obj)
noexcept {
129 this->_rng = obj._rng;
136 HANDLE_ERROR_CURAND(curandDestroyGenerator(_rng));
139 inline void generate_uniform(std::size_t n,
140 cuda::unique_dev_ptr<FloatType[]> &dev_random) {
142 curand_generate_uniform_impl(_rng, dev_random.get(), n));
146 curandGenerator_t _rng;
xorshift random generator for c++11 random
Definition random.hpp:39
unsigned operator()()
generate random number
Definition random.hpp:62
static constexpr unsigned min()
returns minimum value
Definition random.hpp:48
unsigned x
Definition random.hpp:86
unsigned w
Definition random.hpp:86
Xorshift(unsigned s)
Xorshift constructor with seed.
Definition random.hpp:83
uint_fast32_t result_type
Definition random.hpp:41
static constexpr unsigned max()
returns maximum value
Definition random.hpp:55
Xorshift()
Xorshift constructor.
Definition random.hpp:73
unsigned y
Definition random.hpp:86
unsigned z
Definition random.hpp:86
Definition algorithm.hpp:24
double FloatType
Note:
Definition compile_config.hpp:37