openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1// Copyright 2023 Jij Inc.
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <climits>
18#include <random>
19
20#ifdef USE_CUDA
21
22#include <utility>
23#include <cassert>
24
25#include <cuda_runtime.h>
26#include <curand.h>
27
30
31#endif
32
33namespace openjij {
34namespace utility {
35
39class Xorshift {
40public:
41 using result_type = uint_fast32_t;
42
48 inline static constexpr unsigned min() { return 0u; }
49
55 inline static constexpr unsigned max() { return UINT_MAX; }
56
62 inline unsigned operator()() {
63 unsigned t = x ^ (x << 11);
64 x = y;
65 y = z;
66 z = w;
67 return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
68 }
69
74 std::random_device rd;
75 w = rd();
76 }
77
83 Xorshift(unsigned s) { w = s; }
84
85private:
86 unsigned x = 123456789u, y = 362436069u, z = 521288629u, w;
87};
88
89#ifdef USE_CUDA
90namespace cuda {
91template <typename FloatType>
92inline curandStatus_t curand_generate_uniform_impl(curandGenerator_t generator,
93 FloatType *outputPtr,
94 size_t num) {
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;
100}
101
102template <>
103inline curandStatus_t
104curand_generate_uniform_impl<float>(curandGenerator_t generator,
105 float *outputPtr, size_t num) {
106 return curandGenerateUniform(generator, outputPtr, num);
107}
108
109template <>
110inline curandStatus_t
111curand_generate_uniform_impl<double>(curandGenerator_t generator,
112 double *outputPtr, size_t num) {
113 return curandGenerateUniformDouble(generator, outputPtr, num);
114}
115
116template <typename FloatType, curandRngType_t rng_type> class CurandWrapper {
117public:
118 CurandWrapper(std::uint64_t seed) {
119 // generate psudo_random_number generator
120 HANDLE_ERROR_CURAND(curandCreateGenerator(&_rng, rng_type));
121 // set seed
122 HANDLE_ERROR_CURAND(curandSetPseudoRandomGeneratorSeed(_rng, seed));
123 }
124
125 CurandWrapper() : CurandWrapper(std::random_device{}()) {}
126
127 CurandWrapper(CurandWrapper &&obj) noexcept {
128 // move curand handler
129 this->_rng = obj._rng;
130 obj._rng = NULL;
131 }
132
133 ~CurandWrapper() {
134 // destroy generator
135 if (_rng != NULL)
136 HANDLE_ERROR_CURAND(curandDestroyGenerator(_rng));
137 }
138
139 inline void generate_uniform(std::size_t n,
140 cuda::unique_dev_ptr<FloatType[]> &dev_random) {
141 HANDLE_ERROR_CURAND(
142 curand_generate_uniform_impl(_rng, dev_random.get(), n));
143 }
144
145private:
146 curandGenerator_t _rng;
147};
148} // namespace cuda
149#endif
150} // namespace utility
151} // namespace openjij
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