DDC 0.12.0
Loading...
Searching...
No Matches
pdi.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <any>
8#include <array>
9#include <cstddef>
10#include <list>
11#include <string>
12#include <type_traits>
13#include <utility>
14#include <vector>
15
16#include <ddc/ddc.hpp>
17
18#include <pdi.h>
19
20namespace ddc {
21
22template <class T>
23static constexpr PDI_inout_t default_access_v
24 = (std::is_lvalue_reference_v<T> && !std::is_const_v<std::remove_reference_t<T>>)
25 ? PDI_INOUT
26 : PDI_OUT;
27
28template <class T>
29static constexpr PDI_inout_t chunk_default_access_v = is_writable_chunk_v<T> ? PDI_INOUT : PDI_OUT;
30
31class PdiEvent
32{
33 std::string m_event_name;
34
35 std::vector<std::string> m_names;
36
37 std::list<std::any> m_metadata;
38
39 char const* store_name(std::string&& name);
40
41 char const* store_name(std::string const& name);
42
43 template <class T>
44 T* store_scalar(T t)
45 {
46 std::any& ref = m_metadata.emplace_back(std::in_place_type<T>, std::move(t));
47 return std::any_cast<T>(&ref);
48 }
49
50 template <class T>
51 T* store_array(std::vector<T> v)
52 {
53 std::any& ref = m_metadata.emplace_back(std::in_place_type<std::vector<T>>, std::move(v));
54 return std::any_cast<std::vector<T>>(&ref)->data();
55 }
56
57public:
58 explicit PdiEvent(std::string const& event_name);
59
60 PdiEvent(PdiEvent const& rhs) = delete;
61
62 PdiEvent(PdiEvent&& rhs) noexcept = delete;
63
64 ~PdiEvent() noexcept;
65
66 PdiEvent& operator=(PdiEvent const& rhs) = delete;
67
68 PdiEvent& operator=(PdiEvent&& rhs) noexcept = delete;
69
70 /// @{
71 /// API with access argument
72
73 template <PDI_inout_t Access, concepts::borrowed_chunk BorrowedChunk>
74 PdiEvent& with(std::string const& name, BorrowedChunk&& data)
75 {
76 static_assert(
77 !(Access & PDI_IN) || (chunk_default_access_v<BorrowedChunk> & PDI_IN),
78 "Invalid access for constant data");
79 std::array const extents = detail::array(data.domain().extents());
80 PDI_share(store_name(name + "_rank"), store_scalar(extents.size()), PDI_OUT);
81 PDI_share(
82 store_name(name + "_extents"),
83 store_array(std::vector<std::size_t>(extents.begin(), extents.end())),
84 PDI_OUT);
85 PDI_share(
86 store_name(name),
87 const_cast<chunk_value_t<BorrowedChunk>*>(data.data_handle()),
88 Access);
89 return *this;
90 }
91
92 template <PDI_inout_t Access, class Arithmetic>
93 PdiEvent& with(std::string const& name, Arithmetic&& data)
94 requires(std::is_arithmetic_v<std::remove_reference_t<Arithmetic>>)
95 {
96 static_assert(
97 !(Access & PDI_IN) || (default_access_v<Arithmetic> & PDI_IN),
98 "Invalid access for constant data");
99 using value_type = std::remove_cvref_t<Arithmetic>;
100 // NOLINTNEXTLINE(misc-const-correctness)
101 value_type* data_ptr = const_cast<value_type*>(&data);
102 // for read-only data, we share a copy instead of the data itself in case we received a ref on a temporary,
103 if constexpr (!(Access & PDI_IN)) {
104 data_ptr = store_scalar(data);
105 }
106 PDI_share(store_name(name), data_ptr, Access);
107 return *this;
108 }
109
110 /// @}
111 /// API with access deduction
112 /// @{
113
114 /// Borrowed chunk overload (Chunk (const)& or ChunkSpan&& or ChunkSpan (const)&)
115 template <concepts::borrowed_chunk BorrowedChunk>
116 PdiEvent& with(std::string const& name, BorrowedChunk&& data)
117 {
118 return with<chunk_default_access_v<BorrowedChunk>>(name, std::forward<BorrowedChunk>(data));
119 }
120
121 /// Arithmetic overload
122 template <class Arithmetic>
123 PdiEvent& with(std::string const& name, Arithmetic&& data)
124 requires(std::is_arithmetic_v<std::remove_reference_t<Arithmetic>>)
125 {
126 return with<default_access_v<Arithmetic>>(name, std::forward<Arithmetic>(data));
127 }
128
129 /// C-string overload
130 PdiEvent& with(std::string const& name, char const* c_string);
131
132 /// @}
133};
134
135template <PDI_inout_t Access, class DataType>
136void expose_to_pdi(std::string const& name, DataType&& data)
137{
138 PdiEvent(name).with<Access>(name, std::forward<DataType>(data));
139}
140
141template <class DataType>
142void expose_to_pdi(std::string const& name, DataType&& data)
143{
144 PdiEvent(name).with(name, std::forward<DataType>(data));
145}
146
147} // namespace ddc
PdiEvent(PdiEvent const &rhs)=delete
PdiEvent & operator=(PdiEvent &&rhs) noexcept=delete
~PdiEvent() noexcept
Definition pdi.cpp:28
PdiEvent & operator=(PdiEvent const &rhs)=delete
PdiEvent(std::string const &event_name)
Definition pdi.cpp:26
PdiEvent & with(std::string const &name, BorrowedChunk &&data)
Definition pdi.hpp:74
PdiEvent & with(std::string const &name, BorrowedChunk &&data)
API with access deduction.
Definition pdi.hpp:116
PdiEvent(PdiEvent &&rhs) noexcept=delete
PdiEvent & with(std::string const &name, char const *c_string)
C-string overload.
Definition pdi.cpp:36
The top-level namespace of DDC.
void expose_to_pdi(std::string const &name, DataType &&data)
Definition pdi.hpp:136
void expose_to_pdi(std::string const &name, DataType &&data)
Definition pdi.hpp:142