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 event_index()
36 {
37 return static_cast<size_t>(event);
38 }
39
44 {
45
46 // Use SharedHostPinnedSpace as this struct is meant to be small enough to
47 // be shared between Host and Device According to SharedHostPinnedSpace
48 // documentation, the size of this data can fit into one cache line so
49 // transfer is not a botteneck
50 Kokkos::View<std::size_t[number_event_type], Kokkos::SharedSpace> // NOLINT
51 _events; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
52
56 EventContainer() : _events("events")
57 {
58
59 Kokkos::deep_copy(_events, 0); // Ensure all event to 0 occurence
60 }
61
64 [[nodiscard]] std::span<std::size_t> get_span() const
65 {
66 // As we use SharedHostPinnedSpace, we can deal with classic std
67 // containers to use host manipulation
68 return {_events.data(), number_event_type};
69 }
70
74 KOKKOS_INLINE_FUNCTION void clear() const
75 {
76 Kokkos::deep_copy(_events, 0);
77 }
78
85 template <EventType event> [[nodiscard]] constexpr std::size_t get() const
86 {
87 static_assert(event != EventType::__COUNT__,
88 "Count is not a valid event");
90 }
91
99 template <EventType event>
100 KOKKOS_FORCEINLINE_FUNCTION constexpr void incr() const
101 {
102 static_assert(event != EventType::__COUNT__,
103 "Count is not a valid event");
104 Kokkos::atomic_add(&_events[event_index<event>()], 1);
105 }
106
107 template <EventType event>
108 KOKKOS_FORCEINLINE_FUNCTION constexpr void add(size_t val) const
109 {
110 static_assert(event != EventType::__COUNT__,
111 "Count is not a valid event");
112 Kokkos::atomic_add(&_events[event_index<event>()], val);
113 }
114
115 template <EventType event>
116 KOKKOS_FORCEINLINE_FUNCTION constexpr void wrap_incr() const
117 {
118 if constexpr (AutoGenerated::FlagCompileTime::enable_event_counter)
119 {
120 incr<event>();
121 }
122 }
123
130 static EventContainer reduce(std::span<std::size_t> _data);
131
132 template <class Archive> void save(Archive& ar) const
133 {
134 std::array<std::size_t, number_event_type> array{};
135
136 auto rd = std::span<std::size_t>(_events.data(), number_event_type);
137
138 std::copy(rd.begin(), rd.end(), array.begin());
139 assert(rd[0] == _events[0] && rd[0] == array[0]);
140
141 ar(array);
142 }
143
144 template <class Archive> void load(Archive& ar)
145 {
146
147 std::array<std::size_t, number_event_type> array{};
148 ar(array);
149
150 auto rd = std::span<std::size_t>(_events.data(), number_event_type);
151
152 std::copy(array.begin(), array.end(), rd.begin());
153 assert(rd[0] == _events[0]);
154 }
155 };
156
157}; // namespace MC
158
159#endif //__MC_EVENTS_HPP__
Namespace that contains classes and structures related to Monte Carlo (MC) simulations.
Definition alias.hpp:11
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:42
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:35
Use to count events that occurs during Monte-Carlo processing cycles.
Definition events.hpp:44
KOKKOS_FORCEINLINE_FUNCTION constexpr void wrap_incr() const
Definition events.hpp:116
std::span< std::size_t > get_span() const
Get std const view of _events counter.
Definition events.hpp:64
void save(Archive &ar) const
Definition events.hpp:132
constexpr std::size_t get() const
Getter to specific event counter.
Definition events.hpp:85
KOKKOS_FORCEINLINE_FUNCTION constexpr void incr() const
Increment specific event counter.
Definition events.hpp:100
EventContainer()
Default container, initalise counter.
Definition events.hpp:56
KOKKOS_INLINE_FUNCTION void clear() const
Reset counters.
Definition events.hpp:74
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:7
KOKKOS_FORCEINLINE_FUNCTION constexpr void add(size_t val) const
Definition events.hpp:108
Kokkos::View< std::size_t[number_event_type], Kokkos::SharedSpace > _events
Definition events.hpp:51
void load(Archive &ar)
Definition events.hpp:144