BioCMAMC-ST
events.hpp
1#ifndef __MC_EVENTS_HPP__
2#define __MC_EVENTS_HPP__
3
4#include "biocma_cst_config.hpp"
5#include <Kokkos_Core.hpp>
6#include <Kokkos_Core_fwd.hpp>
7#include <common/execinfo.hpp>
8#include <cstddef>
9#include <span>
10
11namespace MC
12{
27
28 constexpr size_t number_event_type = static_cast<std::size_t>(
29 EventType::__COUNT__); //< Number of different events
34 template <EventType event>
35 KOKKOS_INLINE_FUNCTION consteval size_t
37 {
38 return static_cast<size_t>(event);
39 }
40
41 // class Events
42 // {
43 // public:
44 // using value_type = Kokkos::Array<std::size_t, number_event_type>;
45 // using view_type = Kokkos::View<value_type,
46 // Kokkos::DefaultExecutionSpace>;
47
48 // value_type counts;
49 // // std::size_t counts[number_event_type];
50
51 // KOKKOS_INLINE_FUNCTION void
52 // add_into(Events& dst) const
53 // {
54 // for (std::size_t i = 0; i < number_event_type; ++i)
55 // {
56 // dst.counts[i] += counts[i];
57 // }
58 // }
59
60 // KOKKOS_INLINE_FUNCTION
61 // Events() : counts()
62 // {
63 // init();
64 // }
65
66 // KOKKOS_INLINE_FUNCTION
67 // Events(const Events& rhs) : counts()
68 // {
69 // for (std::size_t i = 0; i < number_event_type; i++)
70 // {
71 // counts[i] = rhs.counts[i];
72 // }
73 // }
74
75 // template <EventType Event>
76 // KOKKOS_INLINE_FUNCTION void
77 // inc()
78 // {
79 // counts[event_index<Event>()]++;
80 // }
81 // template <EventType Event>
82 // KOKKOS_INLINE_FUNCTION void
83 // add(std::size_t val)
84 // {
85 // counts[event_index<Event>()] += val;
86 // }
87
88 // KOKKOS_INLINE_FUNCTION
89 // void
90 // init()
91 // {
92 // for (std::size_t i = 0; i < number_event_type; i++)
93 // {
94 // counts[i] = 0;
95 // }
96 // }
97 // };
98
99 // struct TallyReducer
100 // {
101 // using reducer = TallyReducer;
102 // using value_type = Events;
103 // using result_view_type = Kokkos::View<value_type, Kokkos::HostSpace>;
104
105 // private:
106 // result_view_type result_;
107
108 // public:
109 // explicit TallyReducer() : result_("reducer")
110 // {
111 // }
112
113 // KOKKOS_INLINE_FUNCTION
114 // void
115 // join(value_type& dst, const value_type& src) const
116 // {
117 // src.add_into(dst);
118 // }
119
120 // KOKKOS_INLINE_FUNCTION
121 // void
122 // init(value_type& val) const
123 // {
124 // val.init();
125 // }
126
127 // [[nodiscard]] KOKKOS_INLINE_FUNCTION value_type&
128 // reference() const
129 // {
130 // return *result_.data();
131 // }
132
133 // [[nodiscard]] KOKKOS_INLINE_FUNCTION result_view_type
134 // view() const
135 // {
136 // return result_;
137 // }
138
139 // [[nodiscard]] KOKKOS_INLINE_FUNCTION bool
140 // references_scalar() const
141 // {
142 // return true;
143 // }
144 // };
145
150 {
151
152 // Use SharedHostPinnedSpace as this struct is meant to be small enough to
153 // be shared between Host and Device According to SharedHostPinnedSpace
154 // documentation, the size of this data can fit into one cache line so
155 // transfer is not a botteneck
156 Kokkos::View<std::size_t[number_event_type], Kokkos::SharedSpace> // NOLINT
157 _events; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
158
163 {
164
165 Kokkos::deep_copy(_events, 0); // Ensure all event to 0 occurence
166 }
167
170 [[nodiscard]] std::span<std::size_t>
171 get_span() const
172 {
173 // As we use SharedHostPinnedSpace, we can deal with classic std
174 // containers to use host manipulation
175 return { _events.data(), number_event_type };
176 }
177
181 KOKKOS_INLINE_FUNCTION void
182 clear() const
183 {
184 Kokkos::deep_copy(_events, 0);
185 }
186
193 template <EventType event>
194 [[nodiscard]] constexpr std::size_t
195 get() const
196 {
197 static_assert(event != EventType::__COUNT__,
198 "Count is not a valid event");
199 return _events(event_index<event>());
200 }
201
209 template <EventType event>
210 KOKKOS_FORCEINLINE_FUNCTION constexpr void
211 incr() const
212 {
213 static_assert(event != EventType::__COUNT__,
214 "Count is not a valid event");
215 Kokkos::atomic_add(&_events[event_index<event>()], 1);
216 }
217
218 template <EventType event>
219 KOKKOS_FORCEINLINE_FUNCTION constexpr void
220 add(size_t val) const
221 {
222 static_assert(event != EventType::__COUNT__,
223 "Count is not a valid event");
224 Kokkos::atomic_add(&_events[event_index<event>()], val);
225 }
226
227 template <EventType event>
228 KOKKOS_FORCEINLINE_FUNCTION constexpr void
229 wrap_incr() const
230 {
231 if constexpr (AutoGenerated::FlagCompileTime::enable_event_counter)
232 {
233 incr<event>();
234 }
235 }
236
237 template <class Archive>
238 void
239 save(Archive& ar) const
240 {
241 std::array<std::size_t, number_event_type> array{};
242
243 auto rd = std::span<std::size_t>(_events.data(), number_event_type);
244
245 std::copy(rd.begin(), rd.end(), array.begin());
246 assert(rd[0] == _events[0] && rd[0] == array[0]);
247
248 ar(array);
249 }
250
251 template <class Archive>
252 void
253 load(Archive& ar)
254 {
255
256 std::array<std::size_t, number_event_type> array{};
257 ar(array);
258
259 auto rd = std::span<std::size_t>(_events.data(), number_event_type);
260
261 std::copy(array.begin(), array.end(), rd.begin());
262 assert(rd[0] == _events[0]);
263 }
264 };
265
266}; // namespace MC
267
268#endif //__MC_EVENTS_HPP__
Namespace that contains classes and structures related to Monte Carlo (MC) simulations.
Definition alias.hpp:16
constexpr size_t number_event_type
Definition events.hpp:28
EventType
Enumeration that represents events that can occurs during a Monte-Carlo cycle.
Definition events.hpp:18
@ Overflow
Definition events.hpp:23
@ Death
Remove particle from list.
Definition events.hpp:22
@ Move
Move in domain.
Definition events.hpp:21
@ __COUNT__
Definition events.hpp:25
@ NewParticle
Spawn new particle.
Definition events.hpp:19
@ ChangeWeight
Update in weight.
Definition events.hpp:24
@ Exit
Definition alias.hpp:128
KOKKOS_INLINE_FUNCTION consteval size_t event_index()
inline getter, converts event to its value in order to be use as array index
Definition events.hpp:36
KOKKOS_FORCEINLINE_FUNCTION constexpr void wrap_incr() const
Definition events.hpp:229
std::span< std::size_t > get_span() const
Get std const view of _events counter.
Definition events.hpp:171
void save(Archive &ar) const
Definition events.hpp:239
constexpr std::size_t get() const
Getter to specific event counter.
Definition events.hpp:195
KOKKOS_FORCEINLINE_FUNCTION constexpr void incr() const
Increment specific event counter.
Definition events.hpp:211
EventContainer()
Default container, initalise counter.
Definition events.hpp:162
KOKKOS_INLINE_FUNCTION void clear() const
Reset counters.
Definition events.hpp:182
KOKKOS_FORCEINLINE_FUNCTION constexpr void add(size_t val) const
Definition events.hpp:220
Kokkos::View< std::size_t[number_event_type], Kokkos::SharedSpace > _events
Definition events.hpp:157
void load(Archive &ar)
Definition events.hpp:253