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