BioCMAMC-ST
probe.hpp
1#ifndef __CORE__PROBE_HPP__
2#define __CORE__PROBE_HPP__
3
4#include "Kokkos_Core_fwd.hpp"
5#include <Kokkos_Core.hpp>
6#include <biocma_cst_config.hpp>
7#include <cstddef>
8#include <cstdint>
9#include <unordered_map>
10// #include <type_traits>
11
12// template <typename ViewType, typename ExecutionSpace>
13// KOKKOS_INLINE_FUNCTION bool is_view_accessible(const ViewType& view)
14// {
15// return Kokkos::SpaceAccessibility<typename ExecutionSpace::memory_space,
16// typename ViewType::memory_space>::accessible;
17// }
18
19namespace Simulation
20{
21
22 enum class ProbeType
23 {
26 };
27
28 // TODO: keep thi structure as a bulkstorage for fast compute but consider sorting into bins when
29 // exporting it to avoid unnecessary amount of data
30 // Unordered map could be adapted
31
33 template <std::size_t buffer_size> class Probes
34 {
35 public:
36 template <typename Space>
37 using buffer_type = Kokkos::View<double[buffer_size], Kokkos::LayoutRight, Space>;
38 void clear();
39
40 [[nodiscard]] KOKKOS_INLINE_FUNCTION bool set(double val) const;
41
42 [[nodiscard]] bool need_export() const noexcept;
43
44 // [[nodiscard]] const double* raw_get() const;
45
46 [[nodiscard]] std::span<const double> get() const;
47
49
50 private:
51 bool active{};
52 // NOLINTBEGIN
54 Kokkos::View<uint64_t, Kokkos::SharedSpace> internal_counter;
56 // NOLINTEND
57 };
58
60
61 using MapProbes = std::unordered_map<ProbeType, ProbeAutogeneratedBuffer>;
62
63} // namespace Simulation
64
65namespace Simulation
66{
67 template <std::size_t buffer_size> Probes<buffer_size>::Probes()
68 {
69 if (AutoGenerated::FlagCompileTime::use_probe)
70 {
71 active = true;
72 buffer = buffer_type<Kokkos::DefaultExecutionSpace>("probe_buffer");
73 internal_counter = Kokkos::View<uint64_t, Kokkos::SharedSpace>("i_c");
74 host_buffer = buffer_type<Kokkos::DefaultHostExecutionSpace>("host_buffer");
75 }
76 clear();
77 }
78
79 template <std::size_t buffer_size>
80 KOKKOS_INLINE_FUNCTION bool Probes<buffer_size>::set(double val) const
81 {
82 const auto i = (active)?Kokkos::atomic_fetch_inc(&internal_counter()):0;
83
84 // Innaccessible from host space if use CUDA because of direct assignment
85 if ((i < buffer_size) && active)
86 {
87 // this->buffer(i) = val;
88 // if constexpr (is_view_accessible<decltype(buffer), Kokkos::DefaultExecutionSpace>())
89 {
90 this->buffer(i) = val;
91 }
92 // else
93 // {
94 // // Kokkos::deep_copy(buffer(i), val);
95 // Kokkos::View<double, Kokkos::DefaultHostExecutionSpace> single_val("SingleValue");
96 // single_val() = val;
97
98 // Kokkos::deep_copy(buffer(i), single_val);
99 // }
100 return true;
101 }
102 return false;
103 }
104
105 template <std::size_t buffer_size> bool Probes<buffer_size>::need_export() const noexcept
106 {
107 return (internal_counter() >= buffer_size) && active;
108 }
109 template <std::size_t buffer_size> void Probes<buffer_size>::clear()
110 {
111 Kokkos::deep_copy(buffer, 0.);
112 Kokkos::deep_copy(internal_counter, 0);
113 }
114
115 template <std::size_t buffer_size>
116 [[nodiscard]] std::span<const double> Probes<buffer_size>::get() const
117 {
118 Kokkos::deep_copy(host_buffer, buffer);
119 return {host_buffer.data(), std::min(buffer_size, internal_counter())};
120 }
121} // namespace Simulation
122
123#endif
Class to store time event as bulk storage
Definition probe.hpp:34
Kokkos::View< uint64_t, Kokkos::SharedSpace > internal_counter
Definition probe.hpp:54
Probes()
Definition probe.hpp:67
buffer_type< Kokkos::DefaultHostExecutionSpace > host_buffer
Definition probe.hpp:55
bool need_export() const noexcept
Definition probe.hpp:105
std::span< const double > get() const
Definition probe.hpp:116
void clear()
Definition probe.hpp:109
buffer_type< Kokkos::DefaultExecutionSpace > buffer
Definition probe.hpp:53
Kokkos::View< double[buffer_size], Kokkos::LayoutRight, Space > buffer_type
Definition probe.hpp:37
KOKKOS_INLINE_FUNCTION bool set(double val) const
Definition probe.hpp:80
bool active
Definition probe.hpp:51
Namespace that contains classes and structures related to simulation handling.
Definition host_specific.hpp:12
ProbeType
Definition probe.hpp:23
std::unordered_map< ProbeType, ProbeAutogeneratedBuffer > MapProbes
Definition probe.hpp:61