BioCMAMC-ST
prng.hpp
1#ifndef __MC_PRNG_HPP__
2#define __MC_PRNG_HPP__
3
4#include <Kokkos_Random.hpp>
5#include <common/common.hpp>
6#include <common/traits.hpp>
7#include <cstdint>
8
9namespace MC
10{
14 class KPRNG //TODO remove deprecated
15 {
16 public:
17 using pool_type = Kokkos::Random_XorShift1024_Pool<Kokkos::DefaultExecutionSpace>;
18 explicit KPRNG(size_t _seed = 0);
19
20 [[deprecated]] [[nodiscard]] KOKKOS_INLINE_FUNCTION double double_uniform() const
21 {
22 auto generator = random_pool.get_state();
23 double x = generator.drand(0., 1.);
24 random_pool.free_state(generator);
25 return x;
26 }
27
28 template <FloatingPointType T> KOKKOS_INLINE_FUNCTION T uniform() const
29 {
30 auto generator = random_pool.get_state();
31 T x;
32 if constexpr (std::is_same_v<T, float>)
33 {
34 x = generator.frand();
35 }
36 else if constexpr (std::is_same_v<T, double>)
37 {
38 x = generator.drand();
39 }
40 random_pool.free_state(generator);
41 return x;
42 }
43
44 template <FloatingPointType T> KOKKOS_INLINE_FUNCTION T uniform(T a, T b) const
45 {
46 auto generator = random_pool.get_state();
47 T x;
48 if constexpr (std::is_same_v<T, float>)
49 {
50 x = generator.frand(a, b);
51 }
52 else if constexpr (std::is_same_v<T, double>)
53 {
54 x = generator.drand(a, b);
55 }
56 random_pool.free_state(generator);
57 return x;
58 }
59
60 [[deprecated]] [[nodiscard]] Kokkos::View<double*, ComputeSpace>
61 double_uniform(size_t n_sample, double a = 0., double b = 1.) const;
62
63 template <size_t n_r> Kokkos::View<double[n_r], ComputeSpace> double_uniform_view() const
64 {
65 Kokkos::View<double[n_r], ComputeSpace> A("random");
66 Kokkos::fill_random(A, random_pool, 0., 1.);
67 return A;
68 }
69
70 template <size_t n_r>
71 [[deprecated]] KOKKOS_INLINE_FUNCTION std::array<double, n_r> double_uniform() const
72 {
73 return generate_uniform_impl<pool_type, n_r>(random_pool, std::make_index_sequence<n_r>{});
74 }
75
76 [[nodiscard]] KOKKOS_INLINE_FUNCTION uint64_t uniform_u(uint64_t a, uint64_t b) const
77 {
78
79 auto generator = random_pool.get_state();
80 uint64_t x = generator.urand64(a, b);
81 random_pool.free_state(generator);
82 return x;
83 }
84
86
87 [[nodiscard]] auto get_seed() const
88 {
89 return seed;
90 } // TODO export this to result file
91
92 private:
93 std::size_t seed{};
94 template <typename random_pool_t, size_t n_r, size_t... I>
95 KOKKOS_INLINE_FUNCTION std::array<double, n_r>
96 generate_uniform_impl(random_pool_t pool, std::index_sequence<I...> /*unused*/) const
97 {
98 // Constexpr loopunrolling to fill the array
99 auto generator = pool.get_state();
100 std::array<double, n_r> res = {{(static_cast<void>(I), generator.drand(0., 1.))...}};
101 pool.free_state(generator);
102 return res;
103 }
104 };
105
106} // namespace MC
107
108#endif //__MC_PRNG_HPP__
Utilities and wrap around kokkos random generator.
Definition prng.hpp:15
KOKKOS_INLINE_FUNCTION uint64_t uniform_u(uint64_t a, uint64_t b) const
Definition prng.hpp:76
Kokkos::View< double[n_r], ComputeSpace > double_uniform_view() const
Definition prng.hpp:63
KOKKOS_INLINE_FUNCTION T uniform() const
Definition prng.hpp:28
KOKKOS_INLINE_FUNCTION T uniform(T a, T b) const
Definition prng.hpp:44
KOKKOS_INLINE_FUNCTION std::array< double, n_r > generate_uniform_impl(random_pool_t pool, std::index_sequence< I... >) const
Definition prng.hpp:96
auto get_seed() const
Definition prng.hpp:87
KPRNG(size_t _seed=0)
Definition prng.cpp:9
Kokkos::Random_XorShift1024_Pool< Kokkos::DefaultExecutionSpace > pool_type
Definition prng.hpp:17
KOKKOS_INLINE_FUNCTION std::array< double, n_r > double_uniform() const
Definition prng.hpp:71
KOKKOS_INLINE_FUNCTION double double_uniform() const
Definition prng.hpp:20
pool_type random_pool
Definition prng.hpp:85
std::size_t seed
Definition prng.hpp:93
Namespace that contains classes and structures related to Monte Carlo (MC) simulations.
Definition alias.hpp:9