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