alpaka
Abstraction Library for Parallel Kernel Acceleration
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 
7 #include "alpaka/core/Common.hpp"
9 #include "alpaka/rand/Traits.hpp"
10 
11 #include <cstdint>
12 #include <limits>
13 #include <random>
14 #include <type_traits>
15 
16 namespace alpaka::rand
17 {
18  //! "Tiny" state mersenne twister implementation
19  class TinyMersenneTwister : public concepts::Implements<ConceptRand, TinyMersenneTwister>
20  {
21  };
22 
24 
25  //! The standard library mersenne twister implementation.
26  class MersenneTwister : public concepts::Implements<ConceptRand, MersenneTwister>
27  {
28  };
29 
30  //! The standard library rand device implementation.
31  class RandomDevice : public concepts::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
41  {
42  std::mt19937 state;
43 
44  public:
45  MersenneTwister() = default;
46 
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 
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?!)
86  {
87  TinyMTengine state;
88 
89  public:
90  TinyMersenneTwister() = default;
91 
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
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 
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
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 
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>
161  struct NormalReal
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>
175  struct UniformReal
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>
189  struct UniformUint
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()
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  {
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  {
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  {
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.
Definition: RandStdLib.hpp:27
The standard library rand device implementation.
Definition: RandStdLib.hpp:32
"Tiny" state mersenne twister implementation
Definition: RandStdLib.hpp:20
The standard library mersenne twister random number generator.
Definition: RandStdLib.hpp:41
std::mt19937::result_type result_type
Definition: RandStdLib.hpp:57
ALPAKA_FN_HOST MersenneTwister(std::uint32_t const &seed, std::uint32_t const &subsequence=0, std::uint32_t const &offset=0)
Definition: RandStdLib.hpp:47
static constexpr ALPAKA_FN_HOST auto max() -> result_type
Definition: RandStdLib.hpp:64
ALPAKA_FN_HOST auto operator()() -> result_type
Definition: RandStdLib.hpp:69
static constexpr ALPAKA_FN_HOST auto min() -> result_type
Definition: RandStdLib.hpp:59
The standard library's random device based on the local entropy pool.
Definition: RandStdLib.hpp:127
std::random_device::result_type result_type
Definition: RandStdLib.hpp:138
ALPAKA_FN_HOST auto operator()() -> result_type
Definition: RandStdLib.hpp:150
static constexpr ALPAKA_FN_HOST auto max() -> result_type
Definition: RandStdLib.hpp:145
ALPAKA_FN_HOST RandomDevice(std::uint32_t const &, std::uint32_t const &=0, std::uint32_t const &=0)
Definition: RandStdLib.hpp:133
static constexpr ALPAKA_FN_HOST auto min() -> result_type
Definition: RandStdLib.hpp:140
"Tiny" state mersenne twister implementation
Definition: RandStdLib.hpp:86
static constexpr ALPAKA_FN_HOST auto min() -> result_type
Definition: RandStdLib.hpp:104
static constexpr ALPAKA_FN_HOST auto max() -> result_type
Definition: RandStdLib.hpp:109
ALPAKA_FN_HOST auto operator()() -> result_type
Definition: RandStdLib.hpp:114
ALPAKA_FN_HOST TinyMersenneTwister(std::uint32_t const &seed, std::uint32_t const &subsequence=0, std::uint32_t const &offset=0)
Definition: RandStdLib.hpp:92
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto max(T const &max_ctx, Tx const &x, Ty const &y)
Returns the larger of two arguments. NaNs are treated as missing data (between a NaN and a numeric va...
Definition: Traits.hpp:1263
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto min(T const &min_ctx, Tx const &x, Ty const &y)
Returns the smaller of two arguments. NaNs are treated as missing data (between a NaN and a numeric v...
Definition: Traits.hpp:1280
constexpr auto offset
Definition: Extent.hpp:34
Tag used in class inheritance hierarchies that describes that a specific concept (TConcept) is implem...
Definition: Concepts.hpp:15
The CPU random number normal distribution.
Definition: RandStdLib.hpp:162
ALPAKA_FN_HOST auto operator()(TEngine &engine) -> T
Definition: RandStdLib.hpp:164
The CPU random number uniform distribution.
Definition: RandStdLib.hpp:176
ALPAKA_FN_HOST auto operator()(TEngine &engine) -> T
Definition: RandStdLib.hpp:178
The CPU random number normal distribution.
Definition: RandStdLib.hpp:190
ALPAKA_FN_HOST auto operator()(TEngine &engine) -> T
Definition: RandStdLib.hpp:192
The random number float normal distribution get trait.
Definition: Traits.hpp:27
The random number float uniform distribution get trait.
Definition: Traits.hpp:31
static ALPAKA_FN_HOST auto createUniformUint(RandStdLib const &) -> cpu::UniformUint< T >
Definition: RandStdLib.hpp:230
The random number integer uniform distribution get trait.
Definition: Traits.hpp:35
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
Definition: RandStdLib.hpp:256
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
Definition: RandStdLib.hpp:269
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
Definition: RandStdLib.hpp:243
The random number default generator engine get trait.
Definition: Traits.hpp:82