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 <common/execinfo.hpp>
7#include <cstddef>
8#include <span>
9
10namespace MC
11{
26
27 constexpr size_t number_event_type = static_cast<std::size_t>(
28 EventType::__COUNT__); //< Number of different events
29
34 template <EventType event>
35 KOKKOS_INLINE_FUNCTION consteval size_t
37 {
38 return static_cast<size_t>(event);
39 }
40
45 {
46
47 // Use SharedHostPinnedSpace as this struct is meant to be small enough to
48 // be shared between Host and Device According to SharedHostPinnedSpace
49 // documentation, the size of this data can fit into one cache line so
50 // transfer is not a botteneck
51 Kokkos::View<std::size_t[number_event_type], Kokkos::SharedSpace> // NOLINT
52 _events; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
53
57 EventContainer() : _events("events")
58 {
59
60 Kokkos::deep_copy(_events, 0); // Ensure all event to 0 occurence
61 }
62
65 [[nodiscard]] std::span<std::size_t>
66 get_span() const
67 {
68 // As we use SharedHostPinnedSpace, we can deal with classic std
69 // containers to use host manipulation
70 return { _events.data(), number_event_type };
71 }
72
76 KOKKOS_INLINE_FUNCTION void
77 clear() const
78 {
79 Kokkos::deep_copy(_events, 0);
80 }
81
88 template <EventType event>
89 [[nodiscard]] constexpr std::size_t
90 get() const
91 {
92 static_assert(event != EventType::__COUNT__,
93 "Count is not a valid event");
95 }
96
104 template <EventType event>
105 KOKKOS_FORCEINLINE_FUNCTION constexpr void
106 incr() const
107 {
108 static_assert(event != EventType::__COUNT__,
109 "Count is not a valid event");
110 Kokkos::atomic_add(&_events[event_index<event>()], 1);
111 }
112
113 template <EventType event>
114 KOKKOS_FORCEINLINE_FUNCTION constexpr void
115 add(size_t val) const
116 {
117 static_assert(event != EventType::__COUNT__,
118 "Count is not a valid event");
119 Kokkos::atomic_add(&_events[event_index<event>()], val);
120 }
121
122 template <EventType event>
123 KOKKOS_FORCEINLINE_FUNCTION constexpr void
124 wrap_incr() const
125 {
126 if constexpr (AutoGenerated::FlagCompileTime::enable_event_counter)
127 {
128 incr<event>();
129 }
130 }
131
138 static EventContainer reduce(std::span<std::size_t> _data);
139
140 template <class Archive>
141 void
142 save(Archive& ar) const
143 {
144 std::array<std::size_t, number_event_type> array{};
145
146 auto rd = std::span<std::size_t>(_events.data(), number_event_type);
147
148 std::copy(rd.begin(), rd.end(), array.begin());
149 assert(rd[0] == _events[0] && rd[0] == array[0]);
150
151 ar(array);
152 }
153
154 template <class Archive>
155 void
156 load(Archive& ar)
157 {
158
159 std::array<std::size_t, number_event_type> array{};
160 ar(array);
161
162 auto rd = std::span<std::size_t>(_events.data(), number_event_type);
163
164 std::copy(array.begin(), array.end(), rd.begin());
165 assert(rd[0] == _events[0]);
166 }
167 };
168
169}; // namespace MC
170
171#endif //__MC_EVENTS_HPP__
Namespace that contains classes and structures related to Monte Carlo (MC) simulations.
Definition alias.hpp:15
constexpr size_t number_event_type
Definition events.hpp:27
EventType
Enumeration that represents events that can occurs during a Monte-Carlo cycle.
Definition events.hpp:17
@ Overflow
Definition events.hpp:22
@ Death
Remove particle from list.
Definition events.hpp:21
@ Move
Move in domain.
Definition events.hpp:20
@ __COUNT__
Definition events.hpp:24
@ NewParticle
Spawn new particle.
Definition events.hpp:18
@ ChangeWeight
Update in weight.
Definition events.hpp:23
@ Exit
Definition alias.hpp:61
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
Use to count events that occurs during Monte-Carlo processing cycles.
Definition events.hpp:45
KOKKOS_FORCEINLINE_FUNCTION constexpr void wrap_incr() const
Definition events.hpp:124
std::span< std::size_t > get_span() const
Get std const view of _events counter.
Definition events.hpp:66
void save(Archive &ar) const
Definition events.hpp:142
constexpr std::size_t get() const
Getter to specific event counter.
Definition events.hpp:90
KOKKOS_FORCEINLINE_FUNCTION constexpr void incr() const
Increment specific event counter.
Definition events.hpp:106
EventContainer()
Default container, initalise counter.
Definition events.hpp:57
KOKKOS_INLINE_FUNCTION void clear() const
Reset counters.
Definition events.hpp:77
static EventContainer reduce(std::span< std::size_t > _data)
Transform a linear contiguous counter data obtained via MPI gather into EventContainer object.
Definition events.cpp:8
KOKKOS_FORCEINLINE_FUNCTION constexpr void add(size_t val) const
Definition events.hpp:115
Kokkos::View< std::size_t[number_event_type], Kokkos::SharedSpace > _events
Definition events.hpp:52
void load(Archive &ar)
Definition events.hpp:156