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