alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
RandStdLib.hpp
Go to the documentation of this file.
1/* Copyright 2022 Axel Huebl, Benjamin Worpitz, René Widera, Jan Stephan, Bernhard Manfred Gruber
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
10
11#include <cstdint>
12#include <limits>
13#include <random>
14#include <type_traits>
15
16namespace alpaka::rand
17{
18 //! "Tiny" state mersenne twister implementation
19 class TinyMersenneTwister : public interface::Implements<ConceptRand, TinyMersenneTwister>
20 {
21 };
22
24
25 //! The standard library mersenne twister implementation.
26 class MersenneTwister : public interface::Implements<ConceptRand, MersenneTwister>
27 {
28 };
29
30 //! The standard library rand device implementation.
31 class RandomDevice : public interface::Implements<ConceptRand, RandomDevice>
32 {
33 };
34
35 namespace engine::cpu
36 {
37 //! The standard library mersenne twister random number generator.
38 //!
39 //! size of state: 19937 bytes
40 class MersenneTwister
41 {
42 std::mt19937 state;
43
44 public:
45 MersenneTwister() = default;
46
47 ALPAKA_FN_HOST MersenneTwister(
48 std::uint32_t const& seed,
49 std::uint32_t const& subsequence = 0,
50 std::uint32_t const& offset = 0)
51 : // NOTE: XOR the seed and the subsequence to generate a unique seed.
52 state((seed ^ subsequence) + offset)
53 {
54 }
55
56 // STL UniformRandomBitGenerator concept interface
57 using result_type = std::mt19937::result_type;
58
59 ALPAKA_FN_HOST static constexpr auto min() -> result_type
60 {
61 return std::mt19937::min();
62 }
63
64 ALPAKA_FN_HOST static constexpr auto max() -> result_type
65 {
66 return std::mt19937::max();
67 }
68
69 ALPAKA_FN_HOST auto operator()() -> result_type
70 {
71 return state();
72 }
73 };
74
75 //! "Tiny" state mersenne twister implementation
76 //!
77 //! repository: github.com/MersenneTwister-Lab/TinyMT
78 //!
79 //! license: 3-clause BSD
80 //!
81 //! @author Mutsuo Saito (Hiroshima University)Tokio University.
82 //! @author Makoto Matsumoto (The University of Tokyo)
83 //!
84 //! size of state: 28 bytes (127 bits?!)
85 class TinyMersenneTwister
86 {
87 TinyMTengine state;
88
89 public:
90 TinyMersenneTwister() = default;
91
92 ALPAKA_FN_HOST TinyMersenneTwister(
93 std::uint32_t const& seed,
94 std::uint32_t const& subsequence = 0,
95 std::uint32_t const& offset = 0)
96 : // NOTE: XOR the seed and the subsequence to generate a unique seed.
97 state((seed ^ subsequence) + offset)
98 {
99 }
100
101 // STL UniformRandomBitGenerator concept interface
102 using result_type = TinyMTengine::result_type;
103
104 ALPAKA_FN_HOST static constexpr auto min() -> result_type
105 {
106 return TinyMTengine::min();
107 }
108
109 ALPAKA_FN_HOST static constexpr auto max() -> result_type
110 {
111 return TinyMTengine::max();
112 }
113
114 ALPAKA_FN_HOST auto operator()() -> result_type
115 {
116 return state();
117 }
118 };
119
120 //! The standard library's random device based on the local entropy pool.
121 //!
122 //! Warning: the entropy pool on many devices degrates quickly and performance
123 //! will drop significantly when this point occures.
124 //!
125 //! size of state: 1 byte
126 class RandomDevice
127 {
128 std::random_device state;
129
130 public:
131 RandomDevice() = default;
132
133 ALPAKA_FN_HOST RandomDevice(std::uint32_t const&, std::uint32_t const& = 0, std::uint32_t const& = 0)
134 {
135 }
136
137 // STL UniformRandomBitGenerator concept interface
138 using result_type = std::random_device::result_type;
139
140 ALPAKA_FN_HOST static constexpr auto min() -> result_type
141 {
142 return std::random_device::min();
143 }
144
145 ALPAKA_FN_HOST static constexpr auto max() -> result_type
146 {
147 return std::random_device::max();
148 }
149
150 ALPAKA_FN_HOST auto operator()() -> result_type
151 {
152 return state();
153 }
154 };
155 } // namespace engine::cpu
156
157 namespace distribution::cpu
158 {
159 //! The CPU random number normal distribution.
160 template<typename T>
162 {
163 template<typename TEngine>
164 ALPAKA_FN_HOST auto operator()(TEngine& engine) -> T
165 {
166 return m_dist(engine);
167 }
168
169 private:
170 std::normal_distribution<T> m_dist;
171 };
172
173 //! The CPU random number uniform distribution.
174 template<typename T>
176 {
177 template<typename TEngine>
178 ALPAKA_FN_HOST auto operator()(TEngine& engine) -> T
179 {
180 return m_dist(engine);
181 }
182
183 private:
184 std::uniform_real_distribution<T> m_dist;
185 };
186
187 //! The CPU random number normal distribution.
188 template<typename T>
190 {
191 template<typename TEngine>
192 ALPAKA_FN_HOST auto operator()(TEngine& engine) -> T
193 {
194 return m_dist(engine);
195 }
196
197 private:
198 std::uniform_int_distribution<T> m_dist{
199 0, // For signed integer: std::numeric_limits<T>::lowest()
200 std::numeric_limits<T>::max()};
201 };
202 } // namespace distribution::cpu
203
204 namespace distribution::trait
205 {
206 //! The CPU device random number float normal distribution get trait specialization.
207 template<typename T>
208 struct CreateNormalReal<RandStdLib, T, std::enable_if_t<std::is_floating_point_v<T>>>
209 {
210 ALPAKA_FN_HOST static auto createNormalReal(RandStdLib const& /* rand */) -> cpu::NormalReal<T>
211 {
212 return {};
213 }
214 };
215
216 //! The CPU device random number float uniform distribution get trait specialization.
217 template<typename T>
218 struct CreateUniformReal<RandStdLib, T, std::enable_if_t<std::is_floating_point_v<T>>>
219 {
220 ALPAKA_FN_HOST static auto createUniformReal(RandStdLib const& /* rand */) -> cpu::UniformReal<T>
221 {
222 return {};
223 }
224 };
225
226 //! The CPU device random number integer uniform distribution get trait specialization.
227 template<typename T>
228 struct CreateUniformUint<RandStdLib, T, std::enable_if_t<std::is_integral_v<T>>>
229 {
230 ALPAKA_FN_HOST static auto createUniformUint(RandStdLib const& /* rand */) -> cpu::UniformUint<T>
231 {
232 return {};
233 }
234 };
235 } // namespace distribution::trait
236
237 namespace engine::trait
238 {
239 //! The CPU device random number default generator get trait specialization.
240 template<>
242 {
244 TinyMersenneTwister const& /* rand */,
245 std::uint32_t const& seed = 0,
246 std::uint32_t const& subsequence = 0,
247 std::uint32_t const& offset = 0) -> cpu::TinyMersenneTwister
248 {
249 return {seed, subsequence, offset};
250 }
251 };
252
253 template<>
255 {
257 MersenneTwister const& /* rand */,
258 std::uint32_t const& seed = 0,
259 std::uint32_t const& subsequence = 0,
260 std::uint32_t const& offset = 0) -> cpu::MersenneTwister
261 {
262 return {seed, subsequence, offset};
263 }
264 };
265
266 template<>
268 {
270 RandomDevice const& /* rand */,
271 std::uint32_t const& seed = 0,
272 std::uint32_t const& subsequence = 0,
273 std::uint32_t const& offset = 0) -> cpu::RandomDevice
274 {
275 return {seed, subsequence, offset};
276 }
277 };
278 } // namespace engine::trait
279} // namespace alpaka::rand
The standard library mersenne twister implementation.
The standard library rand device implementation.
"Tiny" state mersenne twister implementation
#define ALPAKA_FN_HOST
Definition Common.hpp:40
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto createNormalReal(TRand const &rand)
Definition Traits.hpp:41
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto createUniformUint(TRand const &rand)
Definition Traits.hpp:63
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto createUniformReal(TRand const &rand)
Definition Traits.hpp:52
TinyMersenneTwister RandStdLib
STL namespace.
Tag used in class inheritance hierarchies that describes that a specific interface (TInterface) is im...
Definition Interface.hpp:15
The CPU random number normal distribution.
ALPAKA_FN_HOST auto operator()(TEngine &engine) -> T
The CPU random number uniform distribution.
ALPAKA_FN_HOST auto operator()(TEngine &engine) -> T
The CPU random number normal distribution.
ALPAKA_FN_HOST auto operator()(TEngine &engine) -> T
Implementation of std::UniformRandomBitGenerator for TinyMT32.
Definition Engine.hpp:15
static constexpr auto max() -> result_type
Definition Engine.hpp:53
static constexpr auto min() -> result_type
Definition Engine.hpp:48
static ALPAKA_FN_HOST auto createDefault(MersenneTwister const &, std::uint32_t const &seed=0, std::uint32_t const &subsequence=0, std::uint32_t const &offset=0) -> cpu::MersenneTwister
static ALPAKA_FN_HOST auto createDefault(RandomDevice const &, std::uint32_t const &seed=0, std::uint32_t const &subsequence=0, std::uint32_t const &offset=0) -> cpu::RandomDevice
static ALPAKA_FN_HOST auto createDefault(TinyMersenneTwister const &, std::uint32_t const &seed=0, std::uint32_t const &subsequence=0, std::uint32_t const &offset=0) -> cpu::TinyMersenneTwister
The random number default generator engine get trait.
Definition Traits.hpp:82