BioCMAMC-ST
prng.hpp
1#ifndef __MC_PRNG_HPP__
2#define __MC_PRNG_HPP__
3
4#include "Kokkos_Macros.hpp"
5#include <Kokkos_Random.hpp>
6#include <common/common.hpp>
7#include <common/traits.hpp>
8#include <cstdint>
9#include <mc/alias.hpp>
10namespace MC
11{
12
13 pool_type get_pool(std::size_t seed = 0);
14
43
44 KOKKOS_INLINE_FUNCTION auto
45 sample_random_variables(const pool_type& pool, auto&& functor)
46 {
47 // Be sure that generator is passed to functor as reference
48 // As functor can be lambda usually [](auto gen){return gen.rand();};
49 // If auto is passed as value, value generated is UB. Need auto& to avoid
50 // any problem
51 using gen_t = typename pool_type::generator_type;
52 using Functor = decltype(functor);
53 static_assert(std::is_invocable_v<Functor, gen_t&>
54 && !std::is_invocable_v<Functor, gen_t>,
55 "Functor must accept generator by reference only");
56
57 auto gen = pool.get_state();
58 const auto result = functor(gen);
59 pool.free_state(gen);
60
61 return result;
62 }
63
64#define SAMPLE_RANDOM_VARIABLES(_random_pool_, ...) \
65 auto _generator_state_ = _random_pool_.get_state(); \
66 __VA_ARGS__; \
67 _random_pool_.free_state(_generator_state_);
68
72 class KPRNG // TODO remove deprecated
73 {
74 public:
76 using generator_type = pool_type::generator_type;
77
78 explicit KPRNG(size_t _seed = 0);
79
80 template <FloatingPointType T>
81 KOKKOS_INLINE_FUNCTION T
82 uniform() const
83 {
84 return this->uniform<T>(0, 1);
85 }
86
87 template <FloatingPointType T>
88 KOKKOS_INLINE_FUNCTION T
89 uniform(T a, T b) const
90 {
93 [a, b](auto gen)
94 {
95 T x;
96 if constexpr (std::is_same_v<T, float>)
97 {
98 x = gen.frand(a, b);
99 }
100 else if constexpr (std::is_same_v<T, double>)
101 {
102 x = gen.drand(a, b);
103 }
104 return x;
105 });
106 }
107
108 [[deprecated]] [[nodiscard]] Kokkos::View<double*, ComputeSpace>
109 double_uniform(size_t n_sample, double a = 0., double b = 1.) const;
110
111 template <FloatingPointType T, size_t n_r>
112 Kokkos::View<T[n_r], ComputeSpace>
114 {
115 Kokkos::View<T[n_r], ComputeSpace> A("random");
116 Kokkos::fill_random(A, random_pool, 0., 1.);
117 return A;
118 }
119
120 template <size_t n_r>
121 [[deprecated]] KOKKOS_INLINE_FUNCTION std::array<double, n_r>
123 {
125 random_pool, std::make_index_sequence<n_r>{});
126 }
127
128 [[nodiscard]] KOKKOS_INLINE_FUNCTION uint64_t
129 uniform_u(uint64_t a, uint64_t b) const
130 {
131
133 random_pool, [a, b](auto& gen) { return gen.urand64(a, b); });
134 }
135
137
138 [[nodiscard]] auto
139 get_seed() const
140 {
141 return seed;
142 } // TODO export this to result file
143
144 private:
145 std::size_t seed{};
146 template <typename random_pool_t, size_t n_r, size_t... I>
147 KOKKOS_INLINE_FUNCTION std::array<double, n_r>
148 generate_uniform_impl(random_pool_t pool,
149 std::index_sequence<I...> /*unused*/) const
150 {
151 // Constexpr loopunrolling to fill the array
152 auto generator = pool.get_state();
153 std::array<double, n_r> res
154 = { { (static_cast<void>(I), generator.drand(0., 1.))... } };
155 pool.free_state(generator);
156 return res;
157 }
158 };
159
160} // namespace MC
161
162#endif //__MC_PRNG_HPP__
KOKKOS_INLINE_FUNCTION uint64_t uniform_u(uint64_t a, uint64_t b) const
Definition prng.hpp:129
KOKKOS_INLINE_FUNCTION T uniform() const
Definition prng.hpp:82
KOKKOS_INLINE_FUNCTION T uniform(T a, T b) const
Definition prng.hpp:89
KOKKOS_INLINE_FUNCTION std::array< double, n_r > generate_uniform_impl(random_pool_t pool, std::index_sequence< I... >) const
Definition prng.hpp:148
auto get_seed() const
Definition prng.hpp:139
MC::pool_type pool_type
Definition prng.hpp:75
KPRNG(size_t _seed=0)
Definition prng.cpp:25
pool_type::generator_type generator_type
Definition prng.hpp:76
KOKKOS_INLINE_FUNCTION std::array< double, n_r > double_uniform() const
Definition prng.hpp:122
pool_type random_pool
Definition prng.hpp:136
Kokkos::View< T[n_r], ComputeSpace > random_view() const
Definition prng.hpp:113
std::size_t seed
Definition prng.hpp:145
Namespace that contains classes and structures related to Monte Carlo (MC) simulations.
Definition alias.hpp:15
KOKKOS_INLINE_FUNCTION auto sample_random_variables(const pool_type &pool, auto &&functor)
Definition prng.hpp:45
pool_type get_pool(std::size_t seed=0)
Definition prng.cpp:11
gen_pool_type< Kokkos::DefaultExecutionSpace > pool_type
Definition alias.hpp:39