2222#pragma once
2323
2424#include < alpaka/rand/Traits.hpp>
25+ #include < alpaka/rand/TinyMT/Engine.hpp>
2526
2627#include < alpaka/core/Common.hpp>
28+ #include < alpaka/core/Unused.hpp>
2729
28- #include < boost/core/ignore_unused.hpp>
29-
30+ #include < cstdint>
3031#include < random>
3132#include < type_traits>
3233
@@ -35,19 +36,38 @@ namespace alpaka
3536 namespace rand
3637 {
3738 // #############################################################################
38- // ! The standard library rand implementation.
39- class RandStl
39+ // ! "Tiny" state mersenne twister implementation
40+ class TinyMersenneTwister
4041 {
4142 public:
42- using RandBase = RandStl;
43+ using RandBase = TinyMersenneTwister;
44+ };
45+ using RandStl = TinyMersenneTwister;
46+
47+ // #############################################################################
48+ // ! The standard library mersenne twister implementation.
49+ class MersenneTwister
50+ {
51+ public:
52+ using RandBase = MersenneTwister;
53+ };
54+
55+ // #############################################################################
56+ // ! The standard library rand device implementation.
57+ class RandomDevice
58+ {
59+ public:
60+ using RandBase = RandomDevice;
4361 };
4462
4563 namespace generator
4664 {
4765 namespace cpu
4866 {
4967 // #############################################################################
50- // ! The STL mersenne twister random number generator.
68+ // ! The standard library mersenne twister random number generator.
69+ // !
70+ // ! size of state: 19937 bytes
5171 class MersenneTwister
5272 {
5373 public:
@@ -68,6 +88,67 @@ namespace alpaka
6888 public:
6989 std::mt19937 m_State;
7090 };
91+
92+ // #############################################################################
93+ // ! "Tiny" state mersenne twister implementation
94+ // !
95+ // ! repository: github.com/MersenneTwister-Lab/TinyMT
96+ // !
97+ // ! license: 3-clause BSD
98+ // !
99+ // ! @author Mutsuo Saito (Hiroshima University)Tokio University.
100+ // ! @author Makoto Matsumoto (The University of Tokyo)
101+ // !
102+ // ! size of state: 28 bytes (127 bits?!)
103+ class TinyMersenneTwister
104+ {
105+ public:
106+ // -----------------------------------------------------------------------------
107+ TinyMersenneTwister () = default ;
108+
109+ // -----------------------------------------------------------------------------
110+ ALPAKA_FN_ACC_NO_CUDA TinyMersenneTwister (
111+ std::uint32_t const & seed,
112+ std::uint32_t const & subsequence = 0 ,
113+ std::uint32_t const & offset = 0 ) :
114+ // NOTE: XOR the seed and the subsequence to generate a unique seed.
115+ m_State((seed ^ subsequence) + offset)
116+ {
117+ }
118+
119+ public:
120+ TinyMTengine m_State;
121+ };
122+
123+ // #############################################################################
124+ // ! The standard library's random device based on the local entropy pool.
125+ // !
126+ // ! Warning: the entropy pool on many devices degrates quickly and performance
127+ // ! will drop significantly when this point occures.
128+ // !
129+ // ! size of state: 1 byte
130+ class RandomDevice
131+ {
132+ public:
133+ // -----------------------------------------------------------------------------
134+ RandomDevice () = default ;
135+ RandomDevice (RandomDevice&&) :
136+ m_State{}
137+ {
138+ }
139+
140+ // -----------------------------------------------------------------------------
141+ ALPAKA_FN_ACC_NO_CUDA RandomDevice (
142+ std::uint32_t const &,
143+ std::uint32_t const & = 0 ,
144+ std::uint32_t const & = 0 ) :
145+ m_State{}
146+ {
147+ }
148+
149+ public:
150+ std::random_device m_State;
151+ };
71152 }
72153 }
73154
@@ -166,7 +247,7 @@ namespace alpaka
166247 RandStl const & rand)
167248 -> rand::distribution::cpu::NormalReal<T>
168249 {
169- boost ::ignore_unused (rand);
250+ alpaka ::ignore_unused (rand);
170251 return rand::distribution::cpu::NormalReal<T>();
171252 }
172253 };
@@ -185,7 +266,7 @@ namespace alpaka
185266 RandStl const & rand)
186267 -> rand::distribution::cpu::UniformReal<T>
187268 {
188- boost ::ignore_unused (rand);
269+ alpaka ::ignore_unused (rand);
189270 return rand::distribution::cpu::UniformReal<T>();
190271 }
191272 };
@@ -204,7 +285,7 @@ namespace alpaka
204285 RandStl const & rand)
205286 -> rand::distribution::cpu::UniformUint<T>
206287 {
207- boost ::ignore_unused (rand);
288+ alpaka ::ignore_unused (rand);
208289 return rand::distribution::cpu::UniformUint<T>();
209290 }
210291 };
@@ -218,21 +299,57 @@ namespace alpaka
218299 // ! The CPU device random number default generator get trait specialization.
219300 template <>
220301 struct CreateDefault <
221- RandStl>
302+ TinyMersenneTwister>
303+ {
304+ // -----------------------------------------------------------------------------
305+ ALPAKA_FN_ACC_NO_CUDA static auto createDefault (
306+ TinyMersenneTwister const & rand,
307+ std::uint32_t const & seed,
308+ std::uint32_t const & subsequence)
309+ -> rand::generator::cpu::TinyMersenneTwister
310+ {
311+ alpaka::ignore_unused (rand);
312+ return rand::generator::cpu::TinyMersenneTwister (
313+ seed,
314+ subsequence);
315+ }
316+ };
317+
318+ template <>
319+ struct CreateDefault <
320+ MersenneTwister>
222321 {
223322 // -----------------------------------------------------------------------------
224323 ALPAKA_FN_ACC_NO_CUDA static auto createDefault (
225- RandStl const & rand,
324+ MersenneTwister const & rand,
226325 std::uint32_t const & seed,
227326 std::uint32_t const & subsequence)
228327 -> rand::generator::cpu::MersenneTwister
229328 {
230- boost ::ignore_unused (rand);
329+ alpaka ::ignore_unused (rand);
231330 return rand::generator::cpu::MersenneTwister (
232331 seed,
233332 subsequence);
234333 }
235334 };
335+
336+ template <>
337+ struct CreateDefault <
338+ RandomDevice>
339+ {
340+ // -----------------------------------------------------------------------------
341+ ALPAKA_FN_ACC_NO_CUDA static auto createDefault (
342+ RandomDevice const & rand,
343+ std::uint32_t const & seed,
344+ std::uint32_t const & subsequence)
345+ -> rand::generator::cpu::RandomDevice
346+ {
347+ alpaka::ignore_unused (rand);
348+ return rand::generator::cpu::RandomDevice (
349+ seed,
350+ subsequence);
351+ }
352+ };
236353 }
237354 }
238355 }
0 commit comments