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 <array>
9#include <common/execinfo.hpp>
10#include <cstddef>
11#include <span>
12
13namespace MC
14{
29
30 constexpr size_t number_event_type = static_cast<std::size_t>(
31 EventType::__COUNT__); //< Number of different events
36 template <EventType event>
37 KOKKOS_INLINE_FUNCTION consteval size_t
39 {
40 return static_cast<size_t>(event);
41 }
42
57 constexpr std::size_t event_stride = 64 / sizeof(std::size_t);
58
59 // class Events
60 // {
61 // public:
62 // using value_type = Kokkos::Array<std::size_t, number_event_type>;
63 // using view_type = Kokkos::View<value_type,
64 // Kokkos::DefaultExecutionSpace>;
65
66 // value_type counts;
67 // // std::size_t counts[number_event_type];
68
69 // KOKKOS_INLINE_FUNCTION void
70 // add_into(Events& dst) const
71 // {
72 // for (std::size_t i = 0; i < number_event_type; ++i)
73 // {
74 // dst.counts[i] += counts[i];
75 // }
76 // }
77
78 // KOKKOS_INLINE_FUNCTION
79 // Events() : counts()
80 // {
81 // init();
82 // }
83
84 // KOKKOS_INLINE_FUNCTION
85 // Events(const Events& rhs) : counts()
86 // {
87 // for (std::size_t i = 0; i < number_event_type; i++)
88 // {
89 // counts[i] = rhs.counts[i];
90 // }
91 // }
92
93 // template <EventType Event>
94 // KOKKOS_INLINE_FUNCTION void
95 // inc()
96 // {
97 // counts[event_index<Event>()]++;
98 // }
99 // template <EventType Event>
100 // KOKKOS_INLINE_FUNCTION void
101 // add(std::size_t val)
102 // {
103 // counts[event_index<Event>()] += val;
104 // }
105
106 // KOKKOS_INLINE_FUNCTION
107 // void
108 // init()
109 // {
110 // for (std::size_t i = 0; i < number_event_type; i++)
111 // {
112 // counts[i] = 0;
113 // }
114 // }
115 // };
116
117 // struct TallyReducer
118 // {
119 // using reducer = TallyReducer;
120 // using value_type = Events;
121 // using result_view_type = Kokkos::View<value_type, Kokkos::HostSpace>;
122
123 // private:
124 // result_view_type result_;
125
126 // public:
127 // explicit TallyReducer() : result_("reducer")
128 // {
129 // }
130
131 // KOKKOS_INLINE_FUNCTION
132 // void
133 // join(value_type& dst, const value_type& src) const
134 // {
135 // src.add_into(dst);
136 // }
137
138 // KOKKOS_INLINE_FUNCTION
139 // void
140 // init(value_type& val) const
141 // {
142 // val.init();
143 // }
144
145 // [[nodiscard]] KOKKOS_INLINE_FUNCTION value_type&
146 // reference() const
147 // {
148 // return *result_.data();
149 // }
150
151 // [[nodiscard]] KOKKOS_INLINE_FUNCTION result_view_type
152 // view() const
153 // {
154 // return result_;
155 // }
156
157 // [[nodiscard]] KOKKOS_INLINE_FUNCTION bool
158 // references_scalar() const
159 // {
160 // return true;
161 // }
162 // };
163
168 {
169
170 // Use SharedHostPinnedSpace as this struct is meant to be small enough to
171 // be shared between Host and Device According to SharedHostPinnedSpace
172 // documentation, the size of this data can fit into one cache line so
173 // transfer is not a botteneck
174 // Rank-1, but with event_stride elements between two consecutive counters
175 // so each one owns a cache line. LayoutStride::span() is
176 // max(extent * stride) == number_event_type * event_stride, so the
177 // allocation covers every counter.
179 = Kokkos::View<std::size_t*, Kokkos::LayoutStride, Kokkos::SharedSpace>;
182
183 // Kokkos::View<std::size_t[number_event_type], Kokkos::SharedSpace> //
184 // NOLINT
185 // m_cumulative; //
186 // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
187
190 */
192 : _events("events",
193 Kokkos::LayoutStride(number_event_type, event_stride))
194 // , m_cumulative("cumulativr_events")
195 {
196
197 Kokkos::deep_copy(_events, 0); // Ensure all event to 0 occurence
198
199 // Kokkos::deep_copy(m_cumulative, 0); // Ensure all event to 0 occurence
200 }
207 [[nodiscard]] std::array<std::size_t, number_event_type>
208 get_span() const
209 {
210 // As we use SharedHostPinnedSpace, we can deal with classic std
211 // containers to use host manipulation
212 std::array<std::size_t, number_event_type> packed{};
213 for (std::size_t i = 0; i < number_event_type; ++i)
214 {
215 packed[i] = _events(i);
216 }
217 return packed;
218 }
219
223 KOKKOS_INLINE_FUNCTION void
224 clear() const
225 {
226
227 // const auto& cumview = m_cumulative;
228 // const auto& evview = _events;
229 // Kokkos::parallel_for(
230 // "_sync_cumulative_event",
231 // number_event_type,
232 // KOKKOS_LAMBDA(const int i) { cumview[i] += evview[i]; });
233 Kokkos::deep_copy(_events, 0);
234 }
235
242 template <EventType event>
243 [[nodiscard]] constexpr std::size_t
244 get() const
245 {
246 static_assert(event != EventType::__COUNT__,
247 "Count is not a valid event");
248 return _events(event_index<event>());
249 }
250
257 template <EventType event>
258 [[nodiscard]] constexpr std::size_t
259 get_cumulative() const
260 {
261 static_assert(event != EventType::__COUNT__,
262 "Count is not a valid event");
263 return m_cumulative(event_index<event>());
264 }
265
273 template <EventType event>
274 KOKKOS_FORCEINLINE_FUNCTION constexpr void
275 incr() const
276 {
277 static_assert(event != EventType::__COUNT__,
278 "Count is not a valid event");
279 Kokkos::atomic_add(&_events(event_index<event>()), 1);
280 }
281
282 template <EventType event>
283 KOKKOS_FORCEINLINE_FUNCTION constexpr void
284 add(size_t val) const
285 {
286 static_assert(event != EventType::__COUNT__,
287 "Count is not a valid event");
288 Kokkos::atomic_add(&_events(event_index<event>()), val);
289 }
290
291 template <EventType event>
292 KOKKOS_FORCEINLINE_FUNCTION constexpr void
293 wrap_incr() const
294 {
295 if constexpr (AutoGenerated::FlagCompileTime::enable_event_counter)
296 {
297 incr<event>();
298 }
299 }
300
301 template <class Archive>
302 void
303 save(Archive& ar) const
304 {
305 const std::array<std::size_t, number_event_type> array = get_span();
306 assert(array[0] == _events(0));
307
308 // std::array<std::size_t, number_event_type> array_cumulative{};
309 // rd = std::span<std::size_t>(m_cumulative.data(), number_event_type);
310 // std::copy(rd.begin(), rd.end(), array_cumulative.begin());
311 // assert(rd[0] == m_cumulative[0] && rd[0] == array_cumulative[0]);
312 // ar(array, array_cumulative);
313 //
314 ar(array);
315 }
316
317 template <class Archive>
318 void
319 load(Archive& ar)
320 {
321
322 std::array<std::size_t, number_event_type> array{};
323 // std::array<std::size_t, number_event_type> array_cumulative{};
324 // ar(array, array_cumulative);
325 ar(array);
326
327 for (std::size_t i = 0; i < number_event_type; ++i)
328 {
329 _events(i) = array[i];
330 }
331 assert(array[0] == _events(0));
332
333 // rd = std::span<std::size_t>(m_cumulative.data(), number_event_type);
334 // std::copy(array_cumulative.begin(), array_cumulative.end(),
335 // rd.begin()); assert(rd[0] == m_cumulative[0]);
336 }
337 };
338
339}; // namespace MC
340
341#endif //__MC_EVENTS_HPP__
Definition move_kernel.hpp:631
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:30
EventType
Enumeration that represents events that can occurs during a Monte-Carlo cycle.
Definition events.hpp:20
@ Overflow
Definition events.hpp:25
@ Death
Remove particle from list.
Definition events.hpp:24
@ Move
Move in domain.
Definition events.hpp:23
@ __COUNT__
Definition events.hpp:27
@ NewParticle
Spawn new particle.
Definition events.hpp:21
@ ChangeWeight
Update in weight.
Definition events.hpp:26
@ Exit
Definition alias.hpp:128
constexpr std::size_t event_stride
Number of std::size_t slots reserved per counter.
Definition events.hpp:57
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:38
KOKKOS_FORCEINLINE_FUNCTION constexpr void wrap_incr() const
Definition events.hpp:292
constexpr std::size_t get_cumulative() const
Getter to specific event counter.
Definition events.hpp:258
void save(Archive &ar) const
Definition events.hpp:302
constexpr std::size_t get() const
Getter to specific event counter.
Definition events.hpp:243
KOKKOS_FORCEINLINE_FUNCTION constexpr void incr() const
Increment specific event counter.
Definition events.hpp:274
EventContainer()
Default container, initalise counter.
Definition events.hpp:190
event_view_type _events
Definition events.hpp:180
KOKKOS_INLINE_FUNCTION void clear() const
Reset counters.
Definition events.hpp:223
KOKKOS_FORCEINLINE_FUNCTION constexpr void add(size_t val) const
Definition events.hpp:283
void load(Archive &ar)
Definition events.hpp:318
Kokkos::View< std::size_t *, Kokkos::LayoutStride, Kokkos::SharedSpace > event_view_type
Definition events.hpp:178
std::array< std::size_t, number_event_type > get_span() const
Get a packed copy of the event counters.
Definition events.hpp:207