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 <type_traits>
9
10// template <typename ViewType, typename ExecutionSpace>
11// KOKKOS_INLINE_FUNCTION bool is_view_accessible(const ViewType& view)
12// {
13// return Kokkos::SpaceAccessibility<typename ExecutionSpace::memory_space,
14// typename ViewType::memory_space>::accessible;
15// }
16
17namespace Simulation
18{
19 template <std::size_t buffer_size> class Probes
20 {
21 public:
22 void clear();
23
24 [[nodiscard]] KOKKOS_INLINE_FUNCTION bool set(double val) const;
25
26 [[nodiscard]] bool need_export() const noexcept;
27
28 // [[nodiscard]] const double* raw_get() const;
29
30 [[nodiscard]] std::span<const double> get() const;
31
33
34 private:
35 // NOLINTBEGIN
36 Kokkos::View<double[buffer_size], Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace> buffer;
37 Kokkos::View<uint64_t, Kokkos::SharedSpace> internal_counter;
38 Kokkos::View<double[buffer_size], Kokkos::LayoutRight, Kokkos::DefaultHostExecutionSpace>
40 // NOLINTEND
41 };
42
43 using ProbeAutogeneratedBuffer = Probes<AutoGenerated::probe_buffer_size>;
44
45} // namespace Simulation
46
47namespace Simulation
48{
49 template <std::size_t buffer_size>
51 : buffer("buffer"), internal_counter("i_c"), host_buffer("host_buffer")
52 {
53 clear();
54 }
55
56 template <std::size_t buffer_size>
57 KOKKOS_INLINE_FUNCTION bool Probes<buffer_size>::set(double val) const
58 {
59 const auto i = Kokkos::atomic_fetch_inc(&internal_counter());
60
61 //Innaccessible from host space if use CUDA because of direct assignment
62 if (i < buffer_size)
63 {
64 // this->buffer(i) = val;
65 // if constexpr (is_view_accessible<decltype(buffer), Kokkos::DefaultExecutionSpace>())
66 {
67 this->buffer(i) = val;
68 }
69 // else
70 // {
71 // // Kokkos::deep_copy(buffer(i), val);
72 // Kokkos::View<double, Kokkos::DefaultHostExecutionSpace> single_val("SingleValue");
73 // single_val() = val;
74
75 // Kokkos::deep_copy(buffer(i), single_val);
76 // }
77 return true;
78 }
79 return false;
80 }
81
82 template <std::size_t buffer_size> bool Probes<buffer_size>::need_export() const noexcept
83 {
84 return internal_counter() >= buffer_size;
85 }
86 template <std::size_t buffer_size> void Probes<buffer_size>::clear()
87 {
88 Kokkos::deep_copy(buffer, 0.);
89 // Kokkos::deep_copy(host_buffer, 0);
90 Kokkos::deep_copy(internal_counter, 0);
91 }
92
93 template <std::size_t buffer_size>
94 [[nodiscard]] std::span<const double> Probes<buffer_size>::get() const
95 {
96 Kokkos::deep_copy(host_buffer, buffer);
97 return {host_buffer.data(), std::min(buffer_size, internal_counter())};
98 }
99} // namespace Simulation
100
101/* OLD IMPLEMENTATION
102
103#include "biocma_cst_config.hpp"
104#include "cmt_common/macro_constructor_assignment.hpp"
105#include <Kokkos_Core.hpp>
106#include <Kokkos_Core_fwd.hpp>
107#include <Kokkos_Macros.hpp>
108
109#include <cstdint>
110#include <decl/Kokkos_Declare_OPENMP.hpp>
111#include <initializer_list>
112#include <optional>
113#include <span>
114#include <string>
115#include <string_view>
116#include <traits/Kokkos_IterationPatternTrait.hpp>
117namespace Simulation
118{
119
120 class Probes
121 {
122 public:
123 static constexpr uint64_t buffer_size = AutoGenerated::probe_buffer_size;
124
125 void clear();
126
127 void set(double val) const;
128
129 [[nodiscard]] bool need_export() const;
130
131 [[nodiscard]] double* raw_get() const;
132
133 private:
134 using SubViewProbeType =
135 Kokkos::Subview<Kokkos::View<double***, Kokkos::LayoutRight, ComputeSpace>,
136 decltype(Kokkos::ALL),
137 decltype(Kokkos::ALL),
138 size_t>;
139
140 size_t n_p = 0;
141 size_t n_t = 0;
142 size_t n_s = 0;
143 uint64_t get_counter = 0;
144 [[deprecated]] Probes(std::string_view _label, uint64_t n_step, uint64_t n_compartments);
145
146
147
148
149
150 Kokkos::View<double***, Kokkos::LayoutRight, ComputeSpace> probes;
151 Kokkos::View<double***, Kokkos::LayoutRight, ComputeSpace> host;
152 Kokkos::View<std::string*, HostSpace> labels;
153
154 Kokkos::View<uint64_t, Kokkos::SharedSpace> internal_counter;
155
156 Kokkos::View<double[Probes::buffer_size], Kokkos::LayoutRight, ComputeSpace> buffer;
157
158 public:
159 using SubViewTimeType =
160 Kokkos::Subview<Kokkos::View<double***, Kokkos::LayoutRight, ComputeSpace>,
161 decltype(Kokkos::ALL),
162 size_t,
163 decltype(Kokkos::ALL)>;
164 DEFAULT_COPY_MOVE_AC(Probes)
165 ~Probes() = default;
166 Probes(size_t initial_n_particle, size_t n_t_flush);
167
168 Probes(size_t initial_n_particle, size_t n_t_flush, std::span<std::string> _labels);
169
170 Probes(size_t initial_n_particle, size_t n_t_flush, std::initializer_list<std::string> _labels);
171
172 [[deprecated]] void add_probe(std::string_view label);
173 [[deprecated]] [[nodiscard]] std::span<std::string> get_labels() const;
174
175 [[deprecated]] KOKKOS_FUNCTION std::optional<SubViewProbeType> get(std::string_view label);
176
177 [[deprecated]] SubViewTimeType get();
178
179 [[deprecated]] std::optional<std::array<size_t, 3>> get_raw(double** ptr);
180 };
181} // namespace Simulation
182
183
184*/
185
186#endif
Definition probe.hpp:20
Kokkos::View< uint64_t, Kokkos::SharedSpace > internal_counter
Definition probe.hpp:37
Probes()
Definition probe.hpp:50
bool need_export() const noexcept
Definition probe.hpp:82
std::span< const double > get() const
Definition probe.hpp:94
void clear()
Definition probe.hpp:86
Kokkos::View< double[buffer_size], Kokkos::LayoutRight, Kokkos::DefaultHostExecutionSpace > host_buffer
Definition probe.hpp:39
KOKKOS_INLINE_FUNCTION bool set(double val) const
Definition probe.hpp:57
Kokkos::View< double[buffer_size], Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace > buffer
Definition probe.hpp:36
Namespace that contains classes and structures related to simulation handling.
Definition host_specific.hpp:12