BioCMAMC-ST
probe.hpp
1#ifndef __CORE__PROBE_HPP__
2#define __CORE__PROBE_HPP__
3
4#include <Kokkos_Core.hpp>
5#include <biocma_cst_config.hpp>
6#include <cstddef>
7#include <cstdint>
8#include <span>
9#include <unordered_map>
10
11namespace Simulation
12{
13
14 enum class ProbeType : unsigned
15 {
18 };
19
20 // The name are namely used during export
21 static const std::array<std::string, 2> map_probe_name
22 = { "LeavingTime", "DivisionTime" };
23
24 // TODO: keep thi structure as a bulkstorage for fast compute but consider
25 // sorting into bins when exporting it to avoid unnecessary amount of data
26 // Unordered map could be adapted
27
28 /*
29 TODO, it seems that after some calculation (example cstr 0d) size(division
30 probes) = number of tally+1 where the last probe is 0 where size(leaving time)
31 == tally Is it a bug ?
32 */
33
39 template <std::size_t buffer_size> class Probes
40 {
41 public:
42 template <typename Space>
44 = Kokkos::View<double[buffer_size], Kokkos::LayoutRight, Space>;
45
46 // WARNING : marked const but mutates state
47 void
48 clear() const
49 {
50 if constexpr (AutoGenerated::FlagCompileTime::use_probe)
51 {
52 if (active)
53 {
54 Kokkos::deep_copy(buffer, 0.);
55 Kokkos::deep_copy(internal_counter, 0);
56 }
57 }
58 }
59
60 template <typename Space>
61 [[nodiscard]] KOKKOS_INLINE_FUNCTION bool
62 set(double val) const
63 {
64 static_assert(Kokkos::SpaceAccessibility<
65 Space,
66 Kokkos::DefaultExecutionSpace::memory_space>::accessible);
67 //Strategy here: for the moment kernel implementation do not allow to have use_probe==true and active ==false.
68 // Then check active for safetiness but useless
69 if constexpr (AutoGenerated::FlagCompileTime::use_probe)
70 {
71 if (const auto i = Kokkos::atomic_fetch_inc(&internal_counter());
72 (i < buffer_size) && active)
73 {
74 this->buffer(i) = val;
75 return true;
76 }
77 return false;
78 }
79 //Return true if probe is not activate indicates no error to caller
80 return true;
81 }
82
83 [[nodiscard]] bool
84 need_export() const noexcept
85 {
86 if constexpr (AutoGenerated::FlagCompileTime::use_probe)
87 {
88 return active && (internal_counter() >= buffer_size);
89 }
90 return false; // As the probe never need to be exported, there is no error
91 }
92
93 [[nodiscard]] std::span<const double>
94 get() const
95 {
96 if constexpr (AutoGenerated::FlagCompileTime::use_probe)
97 {
98 if (!active)
99 {
100 // return empty span is a valid return ,
101 // use {} constructor to comply clang and gcc
102 return {};
103 }
104 // TODO this should be alsway internal_counter(), but in case
105 // internal counter > buffersize (error in race condition) size
106 // never overrun buffer_size
107 const auto return_size = std::min(buffer_size, internal_counter());
108 assert(return_size <= buffer_size);
109 /*
110 // Safe: returning a span of size return_size is ok; the span may be
111 smaller than the full host_buffer
112 */
113 Kokkos::deep_copy(host_buffer, buffer);
114 return { host_buffer.data(), return_size };
115 }
116
117 return {}; // Also valid, probe not activated returns empty span
118 }
120 Probes()
121 {
122 if constexpr (AutoGenerated::FlagCompileTime::use_probe)
123 {
124 active = true;
126 internal_counter = Kokkos::View<uint64_t, Kokkos::SharedSpace>("i_c");
129
130 clear();
131 }
132 active = false;
133 }
134
135 private:
136 bool active{};
137 // NOLINTBEGIN
139 Kokkos::View<uint64_t, Kokkos::SharedSpace> internal_counter;
141 // NOLINTEND
142 };
143
145
146 using MapProbes = std::unordered_map<ProbeType, ProbeAutogeneratedBuffer>;
147
148 using ProbeIterator = MapProbes::const_iterator;
149
150} // namespace Simulation
151#endif
Class to store time event as bulk storage !! The behaviour of this class is directly link to value of...
Definition probe.hpp:40
Kokkos::View< uint64_t, Kokkos::SharedSpace > internal_counter
Definition probe.hpp:138
KOKKOS_INLINE_FUNCTION bool set(double val) const
Definition probe.hpp:61
Probes()
Definition probe.hpp:119
buffer_type< Kokkos::DefaultHostExecutionSpace > host_buffer
Definition probe.hpp:139
bool need_export() const noexcept
Definition probe.hpp:83
std::span< const double > get() const
Definition probe.hpp:93
buffer_type< Kokkos::DefaultExecutionSpace > buffer
Definition probe.hpp:137
Kokkos::View< double[buffer_size], Kokkos::LayoutRight, Space > buffer_type
Definition probe.hpp:43
void clear() const
Definition probe.hpp:47
Namespace that contains classes and structures related to simulation handling.
Definition host_specific.hpp:12
MapProbes::const_iterator ProbeIterator
Definition probe.hpp:148
ProbeType
Definition probe.hpp:15
@ LeavingTime
Definition probe.hpp:16
@ DivisionTime
Definition probe.hpp:17
Probes< AutoGenerated::probe_buffer_size > ProbeAutogeneratedBuffer
Definition probe.hpp:144
static const std::array< std::string, 2 > map_probe_name
Definition probe.hpp:22
std::unordered_map< ProbeType, ProbeAutogeneratedBuffer > MapProbes
Definition probe.hpp:146