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{
16 enum class EventType : char
17 {
18 ChangeWeight = 0,
20 Death,
21 Move,
22 Exit,
25 };
26
27 constexpr size_t number_event_type =
28 static_cast<std::size_t>(EventType::__COUNT__); //< Number of different events
29
34 template <EventType event> KOKKOS_INLINE_FUNCTION consteval size_t event_index()
35 {
36 return static_cast<size_t>(event);
37 }
38
43 {
44
45 // Use SharedHostPinnedSpace as this struct is meant to be small enough to
46 // be shared between Host and Device According to SharedHostPinnedSpace
47 // documentation, the size of this data can fit into one cache line so
48 // transfer is not a botteneck
49 Kokkos::View<std::size_t[number_event_type], Kokkos::SharedSpace> // NOLINT
50 _events; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
51
55 EventContainer() : _events("events")
56 {
57
58 Kokkos::deep_copy(_events, 0); // Ensure all event to 0 occurence
59 }
63 [[nodiscard]] std::span<std::size_t> get_span() const
64 {
65 // As we use SharedHostPinnedSpace, we can deal with classic std
66 // containers to use host manipulation
67 return {_events.data(), number_event_type};
68 }
69
73 KOKKOS_INLINE_FUNCTION void clear() const
74 {
75 Kokkos::deep_copy(_events, 0);
76 }
77
84 template <EventType event> [[nodiscard]] constexpr std::size_t get() const
85 {
86 static_assert(event != EventType::__COUNT__, "Count is not a valid event");
88 }
89
97 template <EventType event> KOKKOS_FORCEINLINE_FUNCTION constexpr void incr() const
98 {
99 static_assert(event != EventType::__COUNT__, "Count is not a valid event");
100 Kokkos::atomic_add(&_events[event_index<event>()], 1);
101 }
102
103 template <EventType event> KOKKOS_FORCEINLINE_FUNCTION constexpr void add(size_t val) const
104 {
105 static_assert(event != EventType::__COUNT__, "Count is not a valid event");
106 Kokkos::atomic_add(&_events[event_index<event>()], val);
107 }
108
109 template <EventType event> KOKKOS_FORCEINLINE_FUNCTION constexpr void wrap_incr() const
110 {
111 if constexpr (AutoGenerated::FlagCompileTime::enable_event_counter)
112 {
113 incr<event>();
114 }
115 }
116
123 static EventContainer reduce(std::span<std::size_t> _data);
124
125 template <class Archive> void save(Archive& ar) const
126 {
127 std::array<std::size_t, number_event_type> array{};
128
129 auto rd = std::span<std::size_t>(_events.data(), number_event_type);
130
131 std::copy(rd.begin(), rd.end(), array.begin());
132 assert(rd[0] == _events[0] && rd[0] == array[0]);
133
134 ar(array);
135 }
136
137 template <class Archive> void load(Archive& ar)
138 {
139
140 std::array<std::size_t, number_event_type> array{};
141 ar(array);
142
143 auto rd = std::span<std::size_t>(_events.data(), number_event_type);
144
145 std::copy(array.begin(), array.end(), rd.begin());
146 assert(rd[0] == _events[0]);
147 }
148 };
149
150}; // namespace MC
151
152#endif //__MC_EVENTS_HPP__
Namespace that contains classes and structures related to Monte Carlo (MC) simulations.
Definition alias.hpp:9
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
@ Death
Remove particle from list.
@ Move
Move in domain.
@ NewParticle
Spawn new particle.
@ ChangeWeight
Update in weight.
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:34
Use to count events that occurs during Monte-Carlo processing cycles.
Definition events.hpp:43
KOKKOS_FORCEINLINE_FUNCTION constexpr void wrap_incr() const
Definition events.hpp:109
std::span< std::size_t > get_span() const
Get std const view of _events counter.
Definition events.hpp:63
void save(Archive &ar) const
Definition events.hpp:125
constexpr std::size_t get() const
Getter to specific event counter.
Definition events.hpp:84
KOKKOS_FORCEINLINE_FUNCTION constexpr void incr() const
Increment specific event counter.
Definition events.hpp:97
EventContainer()
Default container, initalise counter.
Definition events.hpp:55
KOKKOS_INLINE_FUNCTION void clear() const
Reset counters.
Definition events.hpp:73
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:103
Kokkos::View< std::size_t[number_event_type], Kokkos::SharedSpace > _events
Definition events.hpp:50
void load(Archive &ar)
Definition events.hpp:137