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 sample_random_variables(const pool_type& pool,
45 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> KOKKOS_INLINE_FUNCTION T uniform() const
81 {
82 return this->uniform<T>(0, 1);
83 }
84
85 template <FloatingPointType T>
86 KOKKOS_INLINE_FUNCTION T uniform(T a, T b) const
87 {
90 [a, b](auto gen)
91 {
92 T x;
93 if constexpr (std::is_same_v<T, float>)
94 {
95 x = gen.frand(a, b);
96 }
97 else if constexpr (std::is_same_v<T, double>)
98 {
99 x = gen.drand(a, b);
100 }
101 return x;
102 });
103 }
104
105 [[deprecated]] [[nodiscard]] Kokkos::View<double*, ComputeSpace>
106 double_uniform(size_t n_sample, double a = 0., double b = 1.) const;
107
108 template <FloatingPointType T, size_t n_r>
109 Kokkos::View<T[n_r], ComputeSpace> random_view() const
110 {
111 Kokkos::View<T[n_r], ComputeSpace> A("random");
112 Kokkos::fill_random(A, random_pool, 0., 1.);
113 return A;
114 }
115
116 template <size_t n_r>
117 [[deprecated]] KOKKOS_INLINE_FUNCTION std::array<double, n_r>
119 {
121 random_pool, std::make_index_sequence<n_r>{});
122 }
123
124 [[nodiscard]] KOKKOS_INLINE_FUNCTION uint64_t uniform_u(uint64_t a,
125 uint64_t b) const
126 {
127
129 random_pool, [a, b](auto& gen) { return gen.urand64(a, b); });
130 }
131
133
134 [[nodiscard]] auto get_seed() const
135 {
136 return seed;
137 } // TODO export this to result file
138
139 private:
140 std::size_t seed{};
141 template <typename random_pool_t, size_t n_r, size_t... I>
142 KOKKOS_INLINE_FUNCTION std::array<double, n_r>
143 generate_uniform_impl(random_pool_t pool,
144 std::index_sequence<I...> /*unused*/) const
145 {
146 // Constexpr loopunrolling to fill the array
147 auto generator = pool.get_state();
148 std::array<double, n_r> res = {
149 {(static_cast<void>(I), generator.drand(0., 1.))...}};
150 pool.free_state(generator);
151 return res;
152 }
153 };
154
155} // namespace MC
156
157#endif //__MC_PRNG_HPP__
KOKKOS_INLINE_FUNCTION uint64_t uniform_u(uint64_t a, uint64_t b) const
Definition prng.hpp:124
KOKKOS_INLINE_FUNCTION T uniform() const
Definition prng.hpp:80
KOKKOS_INLINE_FUNCTION T uniform(T a, T b) const
Definition prng.hpp:86
KOKKOS_INLINE_FUNCTION std::array< double, n_r > generate_uniform_impl(random_pool_t pool, std::index_sequence< I... >) const
Definition prng.hpp:143
auto get_seed() const
Definition prng.hpp:134
MC::pool_type pool_type
Definition prng.hpp:75
KPRNG(size_t _seed=0)
Definition prng.cpp:24
pool_type::generator_type generator_type
Definition prng.hpp:76
KOKKOS_INLINE_FUNCTION std::array< double, n_r > double_uniform() const
Definition prng.hpp:118
pool_type random_pool
Definition prng.hpp:132
Kokkos::View< T[n_r], ComputeSpace > random_view() const
Definition prng.hpp:109
std::size_t seed
Definition prng.hpp:140
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:44
pool_type get_pool(std::size_t seed=0)
Definition prng.cpp:10
gen_pool_type< Kokkos::DefaultExecutionSpace > pool_type
Definition alias.hpp:40