ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
memcpy_archive.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <boost/mpl/bool.hpp>
23#include <boost/serialization/is_bitwise_serializable.hpp>
24#include <boost/serialization/nvp.hpp>
25#include <boost/serialization/serialization.hpp>
26
27#include <cassert>
28#include <cstddef>
29#include <cstring>
30#include <memory>
31#include <span>
32#include <type_traits>
33
34namespace Utils {
35/** @brief Type trait to indicate that a type is
36 * serializable with a static size, e.g. is
37 * suitable for memcpy serialization. Only
38 * specialize this to @c std::true_type if it is
39 * guaranteed that serializing this type always
40 * returns the same number of bytes, independent
41 * of object state.
42 *
43 * @tparam T type under consideration.
44 */
45template <class T>
47 : std::bool_constant<
48 std::is_trivially_copyable_v<T> or
49 boost::serialization::is_bitwise_serializable<T>::value> {};
50
51namespace detail {
52/* Use memcpy for packing */
53template <class T>
54using use_memcpy =
55 std::bool_constant<std::is_trivially_copyable_v<T> or
56 boost::serialization::is_bitwise_serializable<T>::value>;
57/* Use serialize function only if the type is opt-in but not
58 * trivially copyable, in which case memcpy is more efficient. */
59template <class T>
60using use_serialize = std::bool_constant<not use_memcpy<T>::value and
62
63template <class Derived> class BasicMemcpyArchive {
64 /** Buffer to write to */
65 std::span<char> buf;
66 /** Current position in the buffer */
67 char *insert;
68
69protected:
70 // NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
71 explicit BasicMemcpyArchive(std::span<char> buf)
72 : buf(buf), insert(buf.data()) {}
73
74public:
75 auto get_library_version() const { return std::size_t{4}; }
76
77 auto bytes_processed() const {
78 return static_cast<std::size_t>(insert - buf.data());
79 }
80
81 void skip(std::size_t bytes) {
82 assert((insert + bytes) <= &*buf.end());
83 insert += bytes;
84 }
85
86private:
87 void read(void *data, std::size_t bytes) {
88 /* check that there is enough space left in the buffer */
89 assert((insert + bytes) <= &*buf.end());
90 std::memcpy(data, insert, bytes);
91 insert += bytes;
92 }
93
94 void write(const void *data, std::size_t bytes) {
95 /* check that there is enough space left in the buffer */
96 assert((insert + bytes) <= &*buf.end());
97 std::memcpy(insert, data, bytes);
98 insert += bytes;
99 }
100
101public:
102 template <typename T>
103 requires use_memcpy<T>::value
104 void operator>>(T &value) {
105 read(&value, sizeof(T));
106 }
107
108 template <typename T>
109 requires use_memcpy<T>::value
110 void operator<<(T const &value) {
111 write(&value, sizeof(T));
112 }
113
114private:
115 template <class T> void process(T &value) {
116 auto const old_pos = insert;
117 boost::serialization::serialize_adl(*static_cast<Derived *>(this), value,
118 4);
119 auto const new_pos = insert;
120 assert(static_cast<std::size_t>(new_pos - old_pos) <= sizeof(T));
121
122 auto const padding_size = sizeof(T) - (new_pos - old_pos);
123 skip(padding_size);
124 }
125
126public:
127 template <class T>
128 requires detail::use_serialize<T>::value
129 void operator>>(T &value) {
130 process(value);
131 }
132
133 template <class T>
134 requires detail::use_serialize<T>::value
135 void operator<<(T &value) {
136 process(value);
137 }
138
139 template <class T> void operator<<(const boost::serialization::nvp<T> &nvp) {
140 operator<<(nvp.const_value());
141 }
142
143 template <class T> void operator>>(const boost::serialization::nvp<T> &nvp) {
144 operator>>(nvp.value());
145 }
146
147 /**
148 * @brief Determine the static packing size of a type.
149 * @tparam T Type to consider.
150 * @return Packed size in bytes.
151 */
152 template <class T> static constexpr std::size_t packing_size() {
153 return sizeof(T);
154 }
155};
156} // namespace detail
157
158/**
159 * @brief Archive that deserializes from a buffer via memcpy.
160 *
161 * Can only process types that have a static serialization size,
162 * e.g. that serialize to the same number of bytes independent of
163 * the state of the object. This can either be automatically detected
164 * for types that are trivially copyable, or by explicitly assured
165 * by specializing @ref is_statically_serializable to @c std::true_type.
166 */
167class MemcpyIArchive : public detail::BasicMemcpyArchive<MemcpyIArchive> {
168private:
169 using base_type = detail::BasicMemcpyArchive<MemcpyIArchive>;
170
171public:
172 using is_loading = boost::mpl::true_;
173 using is_saving = boost::mpl::false_;
174
175 /**
176 * @param buf Buffer to read from.
177 */
178 explicit MemcpyIArchive(std::span<char> buf) : base_type(buf) {}
179
180 /**
181 * @brief Number of bytes read from the buffer.
182 * @return Number of bytes read.
183 */
184 std::size_t bytes_read() const { return bytes_processed(); }
185
186 /** @copydoc base_type::packing_size */
187 using base_type::packing_size;
188 using base_type::operator>>;
189
190 template <class T> MemcpyIArchive &operator&(T &value) {
191 operator>>(value);
192
193 return *this;
194 }
195};
196
197/**
198 * @brief Archive that serializes to a buffer via memcpy.
199 *
200 * @copydetails MemcpyIArchive
201 */
202class MemcpyOArchive : public detail::BasicMemcpyArchive<MemcpyOArchive> {
203 using base_type = detail::BasicMemcpyArchive<MemcpyOArchive>;
204
205public:
206 using is_loading = boost::mpl::false_;
207 using is_saving = boost::mpl::true_;
208
209 /**
210 * @param buf Buffer to write to.
211 */
212 explicit MemcpyOArchive(std::span<char> buf) : base_type(buf) {}
213
214 /**
215 * @brief Number of bytes written to the buffer.
216 * @return Number of bytes written.
217 */
218 std::size_t bytes_written() const { return bytes_processed(); }
219
220 /** @copydoc base_type::packing_size */
221 using base_type::packing_size;
222 using base_type::operator<<;
223
224 template <class T> MemcpyOArchive &operator&(T &value) {
225 operator<<(value);
226
227 return *this;
228 }
229};
230} // namespace Utils
Archive that deserializes from a buffer via memcpy.
boost::mpl::true_ is_loading
MemcpyIArchive & operator&(T &value)
std::size_t bytes_read() const
Number of bytes read from the buffer.
MemcpyIArchive(std::span< char > buf)
boost::mpl::false_ is_saving
Archive that serializes to a buffer via memcpy.
boost::mpl::false_ is_loading
MemcpyOArchive(std::span< char > buf)
std::size_t bytes_written() const
Number of bytes written to the buffer.
MemcpyOArchive & operator&(T &value)
boost::mpl::true_ is_saving
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
static std::basic_ostream< char > & operator<<(std::basic_ostream< char > &os, const dim3 &dim)
Type trait to indicate that a type is serializable with a static size, e.g.