BioCMAMC-ST
events.hpp
1#ifndef __MC_EVENTS_HPP__
2#define __MC_EVENTS_HPP__
3
4#include "biocma_cst_config.hpp"
5#include "impl/Kokkos_Profiling.hpp"
6#include <Kokkos_Core.hpp>
7#include <Kokkos_Core_fwd.hpp>
8#include <common/execinfo.hpp>
9#include <cstddef>
10#include <span>
11
12namespace MC
13{
28
29 constexpr size_t number_event_type = static_cast<std::size_t>(
30 EventType::__COUNT__); //< Number of different events
35 template <EventType event>
36 KOKKOS_INLINE_FUNCTION consteval size_t
38 {
39 return static_cast<size_t>(event);
40 }
41
42 // class Events
43 // {
44 // public:
45 // using value_type = Kokkos::Array<std::size_t, number_event_type>;
46 // using view_type = Kokkos::View<value_type,
47 // Kokkos::DefaultExecutionSpace>;
48
49 // value_type counts;
50 // // std::size_t counts[number_event_type];
51
52 // KOKKOS_INLINE_FUNCTION void
53 // add_into(Events& dst) const
54 // {
55 // for (std::size_t i = 0; i < number_event_type; ++i)
56 // {
57 // dst.counts[i] += counts[i];
58 // }
59 // }
60
61 // KOKKOS_INLINE_FUNCTION
62 // Events() : counts()
63 // {
64 // init();
65 // }
66
67 // KOKKOS_INLINE_FUNCTION
68 // Events(const Events& rhs) : counts()
69 // {
70 // for (std::size_t i = 0; i < number_event_type; i++)
71 // {
72 // counts[i] = rhs.counts[i];
73 // }
74 // }
75
76 // template <EventType Event>
77 // KOKKOS_INLINE_FUNCTION void
78 // inc()
79 // {
80 // counts[event_index<Event>()]++;
81 // }
82 // template <EventType Event>
83 // KOKKOS_INLINE_FUNCTION void
84 // add(std::size_t val)
85 // {
86 // counts[event_index<Event>()] += val;
87 // }
88
89 // KOKKOS_INLINE_FUNCTION
90 // void
91 // init()
92 // {
93 // for (std::size_t i = 0; i < number_event_type; i++)
94 // {
95 // counts[i] = 0;
96 // }
97 // }
98 // };
99
100 // struct TallyReducer
101 // {
102 // using reducer = TallyReducer;
103 // using value_type = Events;
104 // using result_view_type = Kokkos::View<value_type, Kokkos::HostSpace>;
105
106 // private:
107 // result_view_type result_;
108
109 // public:
110 // explicit TallyReducer() : result_("reducer")
111 // {
112 // }
113
114 // KOKKOS_INLINE_FUNCTION
115 // void
116 // join(value_type& dst, const value_type& src) const
117 // {
118 // src.add_into(dst);
119 // }
120
121 // KOKKOS_INLINE_FUNCTION
122 // void
123 // init(value_type& val) const
124 // {
125 // val.init();
126 // }
127
128 // [[nodiscard]] KOKKOS_INLINE_FUNCTION value_type&
129 // reference() const
130 // {
131 // return *result_.data();
132 // }
133
134 // [[nodiscard]] KOKKOS_INLINE_FUNCTION result_view_type
135 // view() const
136 // {
137 // return result_;
138 // }
139
140 // [[nodiscard]] KOKKOS_INLINE_FUNCTION bool
141 // references_scalar() const
142 // {
143 // return true;
144 // }
145 // };
146
151 {
152
153 // Use SharedHostPinnedSpace as this struct is meant to be small enough to
154 // be shared between Host and Device According to SharedHostPinnedSpace
155 // documentation, the size of this data can fit into one cache line so
156 // transfer is not a botteneck
157 Kokkos::View<std::size_t[number_event_type], Kokkos::SharedSpace> // NOLINT
158 _events; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
159
160 // Kokkos::View<std::size_t[number_event_type], Kokkos::SharedSpace> //
161 // NOLINT
162 // m_cumulative; //
163 // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
164
169 // , m_cumulative("cumulativr_events")
170 {
171
172 Kokkos::deep_copy(_events, 0); // Ensure all event to 0 occurence
173
174 // Kokkos::deep_copy(m_cumulative, 0); // Ensure all event to 0 occurence
175 }
176
179 [[nodiscard]] std::span<std::size_t>
180 get_span() const
181 {
182 // As we use SharedHostPinnedSpace, we can deal with classic std
183 // containers to use host manipulation
184 return { _events.data(), number_event_type };
185 }
186
190 KOKKOS_INLINE_FUNCTION void
191 clear() const
192 {
193
194 // const auto& cumview = m_cumulative;
195 // const auto& evview = _events;
196 // Kokkos::parallel_for(
197 // "_sync_cumulative_event",
198 // number_event_type,
199 // KOKKOS_LAMBDA(const int i) { cumview[i] += evview[i]; });
200 Kokkos::deep_copy(_events, 0);
201 }
202
209 template <EventType event>
210 [[nodiscard]] constexpr std::size_t
211 get() const
212 {
213 static_assert(event != EventType::__COUNT__,
214 "Count is not a valid event");
215 return _events(event_index<event>());
216 }
217
224 template <EventType event>
225 [[nodiscard]] constexpr std::size_t
227 {
228 static_assert(event != EventType::__COUNT__,
229 "Count is not a valid event");
230 return m_cumulative(event_index<event>());
231 }
232
240 template <EventType event>
241 KOKKOS_FORCEINLINE_FUNCTION constexpr void
242 incr() const
243 {
244 static_assert(event != EventType::__COUNT__,
245 "Count is not a valid event");
246 Kokkos::atomic_add(&_events[event_index<event>()], 1);
247 }
248
249 template <EventType event>
250 KOKKOS_FORCEINLINE_FUNCTION constexpr void
251 add(size_t val) const
252 {
253 static_assert(event != EventType::__COUNT__,
254 "Count is not a valid event");
255 Kokkos::atomic_add(&_events[event_index<event>()], val);
256 }
257
258 template <EventType event>
259 KOKKOS_FORCEINLINE_FUNCTION constexpr void
260 wrap_incr() const
261 {
262 if constexpr (AutoGenerated::FlagCompileTime::enable_event_counter)
263 {
264 incr<event>();
265 }
266 }
267
268 template <class Archive>
269 void
270 save(Archive& ar) const
271 {
272 std::array<std::size_t, number_event_type> array{};
273
274 auto rd = std::span<std::size_t>(_events.data(), number_event_type);
275 std::copy(rd.begin(), rd.end(), array.begin());
276 assert(rd[0] == _events[0] && rd[0] == array[0]);
277
278 // std::array<std::size_t, number_event_type> array_cumulative{};
279 // rd = std::span<std::size_t>(m_cumulative.data(), number_event_type);
280 // std::copy(rd.begin(), rd.end(), array_cumulative.begin());
281 // assert(rd[0] == m_cumulative[0] && rd[0] == array_cumulative[0]);
282 // ar(array, array_cumulative);
283 //
284 ar(array);
285 }
286
287 template <class Archive>
288 void
289 load(Archive& ar)
290 {
291
292 std::array<std::size_t, number_event_type> array{};
293 // std::array<std::size_t, number_event_type> array_cumulative{};
294 // ar(array, array_cumulative);
295 ar(array);
296
297 auto rd = std::span<std::size_t>(_events.data(), number_event_type);
298 std::copy(array.begin(), array.end(), rd.begin());
299 assert(rd[0] == _events[0]);
300
301 // rd = std::span<std::size_t>(m_cumulative.data(), number_event_type);
302 // std::copy(array_cumulative.begin(), array_cumulative.end(),
303 // rd.begin()); assert(rd[0] == m_cumulative[0]);
304 }
305 };
306
307}; // namespace MC
308
309#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:29
EventType
Enumeration that represents events that can occurs during a Monte-Carlo cycle.
Definition events.hpp:19
@ Overflow
Definition events.hpp:24
@ Death
Remove particle from list.
Definition events.hpp:23
@ Move
Move in domain.
Definition events.hpp:22
@ __COUNT__
Definition events.hpp:26
@ NewParticle
Spawn new particle.
Definition events.hpp:20
@ ChangeWeight
Update in weight.
Definition events.hpp:25
@ 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:37
KOKKOS_FORCEINLINE_FUNCTION constexpr void wrap_incr() const
Definition events.hpp:260
std::span< std::size_t > get_span() const
Get std const view of _events counter.
Definition events.hpp:180
constexpr std::size_t get_cumulative() const
Getter to specific event counter.
Definition events.hpp:226
void save(Archive &ar) const
Definition events.hpp:270
constexpr std::size_t get() const
Getter to specific event counter.
Definition events.hpp:211
KOKKOS_FORCEINLINE_FUNCTION constexpr void incr() const
Increment specific event counter.
Definition events.hpp:242
EventContainer()
Default container, initalise counter.
Definition events.hpp:168
KOKKOS_INLINE_FUNCTION void clear() const
Reset counters.
Definition events.hpp:191
KOKKOS_FORCEINLINE_FUNCTION constexpr void add(size_t val) const
Definition events.hpp:251
Kokkos::View< std::size_t[number_event_type], Kokkos::SharedSpace > _events
Definition events.hpp:158
void load(Archive &ar)
Definition events.hpp:289