ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
data_descriptor.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2#ifndef TTG_SERIALIZATION_DATA_DESCRIPTOR_H
3#define TTG_SERIALIZATION_DATA_DESCRIPTOR_H
4
5#include <cstdint>
6
7#ifdef TTG_SERIALIZATION_SUPPORTS_MADNESS
8#include <madness/world/buffer_archive.h>
9#endif
10
12
14
15#include <cstring> // for std::memcpy
16
18
23extern "C" struct ttg_data_descriptor {
24 const char *name;
25
29 uint64_t (*payload_size)(const void *object);
30
37 uint64_t (*pack_payload)(const void *object, uint64_t max_nbytes_to_write, uint64_t offset, void *buf);
38
45 uint64_t (*unpack_payload)(void *object, uint64_t max_nbytes_to_read, uint64_t offset, const void *buf);
46
47 void (*print)(const void *object);
48};
49
50namespace ttg {
51
60 template <typename T, typename Enabler = void>
62
65 template <typename T>
67 T, std::enable_if_t<detail::is_memcpyable_v<T> && !detail::is_user_buffer_serializable_v<T> &&
68 !ttg::has_split_metadata<T>::value>> {
69 static constexpr const bool serialize_size_is_const = true;
70
74 static uint64_t payload_size(const void *object) { return static_cast<uint64_t>(sizeof(T)); }
75
82 static uint64_t pack_payload(const void *object, uint64_t max_nbytes_to_write, uint64_t begin, void *buf) {
83 unsigned char *char_buf = reinterpret_cast<unsigned char *>(buf);
84 assert(sizeof(T)<=max_nbytes_to_write);
85 std::memcpy(&char_buf[begin], object, sizeof(T));
86 return begin + sizeof(T);
87 }
88
95 static uint64_t unpack_payload(void *object, uint64_t max_nbytes_to_read, uint64_t begin, const void *buf) {
96 const unsigned char *char_buf = reinterpret_cast<const unsigned char *>(buf);
97 assert(sizeof(T)<=max_nbytes_to_read);
98 std::memcpy(object, &char_buf[begin], sizeof(T));
99 return begin + sizeof(T);
100 }
101 };
102
105 template <typename T>
106 struct default_data_descriptor<T, std::enable_if_t<ttg::has_split_metadata<T>::value>> {
107 static constexpr const bool serialize_size_is_const = false;
108
112 static uint64_t payload_size(const void *object) {
114 const T *t = reinterpret_cast<T *>(object);
115 auto metadata = smd.get_metadata(t);
116 size_t size = sizeof(metadata);
117 for (auto &&iovec : smd.get_data(t)) {
119 }
120
121 return static_cast<uint64_t>(size);
122 }
123
130 static uint64_t pack_payload(const void *object, uint64_t max_nbytes_to_write, uint64_t begin, void *buf) {
132 const T *t = reinterpret_cast<T *>(object);
133
134 unsigned char *char_buf = reinterpret_cast<unsigned char *>(buf);
135 auto metadata = smd.get_metadata(t);
136 assert(sizeof(metadata) <= max_nbytes_to_write);
137 std::memcpy(&char_buf[begin], metadata, sizeof(metadata));
138 size_t pos = sizeof(metadata);
139 for (auto &&iovec : smd.get_data(t)) {
140 std::memcpy(&char_buf[begin + pos], iovec.data, iovec.num_bytes);
141 pos += iovec.num_bytes;
142 assert(pos <= max_nbytes_to_write);
143 }
144 return begin + pos;
145 }
146
153 static uint64_t unpack_payload(void *object, uint64_t max_nbytes_to_read, uint64_t begin, const void *buf) {
155 T *t = reinterpret_cast<T *>(object);
156
157 using metadata_t = decltype(smd.get_metadata(t));
158 assert(sizeof(metadata_t) <= max_nbytes_to_read);
159 const unsigned char *char_buf = reinterpret_cast<const unsigned char *>(buf);
160 const metadata_t *metadata = reinterpret_cast<const metadata_t *>(char_buf + begin);
161 T t_created = smd.create_from_metadata();
162 size_t pos = sizeof(metadata);
163 *t = t_created;
164 for (auto &&iovec : smd.get_data(t)) {
165 std::memcpy(iovec.data, &char_buf[begin + pos], iovec.num_bytes);
166 pos += iovec.num_bytes;
167 assert(pos <= max_nbytes_to_read);
168 }
169 return begin + pos;
170 }
171 };
172
173} // namespace ttg
174
175#if defined(TTG_SERIALIZATION_SUPPORTS_MADNESS)
176
177namespace ttg {
178
180 template <typename T>
181 struct default_data_descriptor<
182 T, std::enable_if_t<((!detail::is_memcpyable_v<T> && detail::is_madness_buffer_serializable_v<T>) ||
183 detail::is_madness_user_buffer_serializable_v<T>)&&!ttg::has_split_metadata<T>::value>> {
184 static constexpr const bool serialize_size_is_const = false;
185
189 static uint64_t payload_size(const void *object) {
190 madness::archive::BufferOutputArchive ar;
191 ar & (*static_cast<std::add_pointer_t<std::add_const_t<T>>>(object));
192 return static_cast<uint64_t>(ar.size());
193 }
194
201 static uint64_t pack_payload(const void *object, uint64_t max_nbytes_to_write, uint64_t pos, void *_buf) {
202 unsigned char *buf = reinterpret_cast<unsigned char *>(_buf);
203 madness::archive::BufferOutputArchive ar(&buf[pos], max_nbytes_to_write);
204 ar & (*static_cast<std::add_pointer_t<std::add_const_t<T>>>(object));
205 return pos + ar.size();
206 }
207
214 static uint64_t unpack_payload(void *object, uint64_t max_nbytes_to_read, uint64_t pos, const void *_buf) {
215 const unsigned char *buf = reinterpret_cast<const unsigned char *>(_buf);
216 madness::archive::BufferInputArchive ar(&buf[pos], max_nbytes_to_read);
217 ar & (*static_cast<std::add_pointer_t<T>>(object));
218 return pos + (max_nbytes_to_read - ar.nbyte_avail());
219 }
220 };
221
222} // namespace ttg
223
224#endif // has MADNESS serialization
225
226#if defined(TTG_SERIALIZATION_SUPPORTS_BOOST)
227
229
230namespace ttg {
231
233 template <typename T>
234 struct default_data_descriptor<
235 T, std::enable_if_t<(!detail::is_memcpyable_v<T> && !detail::is_madness_buffer_serializable_v<T> &&
236 detail::is_boost_buffer_serializable_v<T>) ||
237 (!detail::is_madness_user_buffer_serializable_v<T> &&
238 detail::is_boost_user_buffer_serializable_v<T>)>> {
239 static constexpr const bool serialize_size_is_const = false;
240
244 static uint64_t payload_size(const void *object) {
246 oa << (*static_cast<std::add_pointer_t<std::add_const_t<T>>>(object));
247 return oa.streambuf().size();
248 }
249
256 static uint64_t pack_payload(const void *object, uint64_t max_nbytes_to_write, uint64_t pos, void *buf) {
257 auto oa = ttg::detail::make_boost_buffer_oarchive(buf, pos + max_nbytes_to_write, pos);
258 oa << (*static_cast<std::add_pointer_t<std::add_const_t<T>>>(object));
259 assert(oa.streambuf().size() <= max_nbytes_to_write);
260 return pos + oa.streambuf().size();
261 }
262
269 static uint64_t unpack_payload(void *object, uint64_t max_nbytes_to_read, uint64_t pos, const void *buf) {
270 auto ia = ttg::detail::make_boost_buffer_iarchive(buf, pos + max_nbytes_to_read, pos);
271 ia >> (*static_cast<std::add_pointer_t<T>>(object));
272 assert(ia.streambuf().size() <= max_nbytes_to_read);
273 return pos + ia.streambuf().size();
274 }
275 };
276
277} // namespace ttg
278
279#endif // has Boost serialization
280
281namespace ttg {
282
283 // Returns a pointer to a constant static instance initialized
284 // once at run time.
285 template <typename T>
292
293} // namespace ttg
294
295#endif // TTG_SERIALIZATION_DATA_DESCRIPTOR_H
optimized data-only serializer
Definition archive.h:95
STL namespace.
auto make_boost_buffer_iarchive(const void *const buf, std::size_t size, std::size_t buf_offset=0)
constructs a boost_buffer_iarchive object
Definition archive.h:391
auto make_boost_buffer_oarchive(void *const buf, std::size_t size, std::size_t buf_offset=0)
constructs a boost_buffer_oarchive object
Definition archive.h:231
top-level TTG namespace contains runtime-neutral functionality
Definition keymap.h:9
const ttg_data_descriptor * get_data_descriptor()
int size(World world=default_execution_context())
Definition run.h:131
static uint64_t unpack_payload(void *object, uint64_t max_nbytes_to_read, uint64_t begin, const void *buf)
deserializes object from a buffer
static uint64_t pack_payload(const void *object, uint64_t max_nbytes_to_write, uint64_t begin, void *buf)
serializes object to a buffer
static uint64_t unpack_payload(void *object, uint64_t max_nbytes_to_read, uint64_t begin, const void *buf)
deserializes object from a buffer
static uint64_t pack_payload(const void *object, uint64_t max_nbytes_to_write, uint64_t begin, void *buf)
serializes object to a buffer
static uint64_t payload_size(const void *object)
measures the size of the binary representation of object
Provides (de)serialization of C++ data that can be invoked from C via ttg_data_descriptor.
std::size_t num_bytes
The number of bytes to read from / write to the memory location given by data.
Definition iovec.h:14
void * data
Pointer to the data to be read from / written to.
Definition iovec.h:16
uint64_t(* pack_payload)(const void *object, uint64_t max_nbytes_to_write, uint64_t offset, void *buf)
serializes object to a buffer
uint64_t(* payload_size)(const void *object)
measures the size of the binary representation of object
uint64_t(* unpack_payload)(void *object, uint64_t max_nbytes_to_read, uint64_t offset, const void *buf)
deserializes object from a buffer
void(* print)(const void *object)