Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Serializer.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2021 The Eigen Team
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_SERIALIZER_H
12#define EIGEN_SERIALIZER_H
13
14#include <type_traits>
15
16// The Serializer class encodes data into a memory buffer so it can be later
17// reconstructed. This is mainly used to send objects back-and-forth between
18// the CPU and GPU.
19
20namespace Eigen {
21
27template <typename T, typename EnableIf = void>
29
30// Specialization for POD types.
31template <typename T>
32class Serializer<T, std::enable_if_t<std::is_trivially_copyable<T>::value && std::is_standard_layout<T>::value>> {
33 public:
40 EIGEN_DEVICE_FUNC size_t size(const T& value) const { return sizeof(value); }
41
49 EIGEN_DEVICE_FUNC uint8_t* serialize(uint8_t* dest, uint8_t* end, const T& value) const {
50 if (EIGEN_PREDICT_FALSE(dest == nullptr)) return nullptr;
51 if (EIGEN_PREDICT_FALSE(dest + sizeof(value) > end)) return nullptr;
52 EIGEN_USING_STD(memcpy)
53 memcpy(dest, &value, sizeof(value));
54 return dest + sizeof(value);
55 }
56
64 EIGEN_DEVICE_FUNC const uint8_t* deserialize(const uint8_t* src, const uint8_t* end, T& value) const {
65 if (EIGEN_PREDICT_FALSE(src == nullptr)) return nullptr;
66 if (EIGEN_PREDICT_FALSE(src + sizeof(value) > end)) return nullptr;
67 EIGEN_USING_STD(memcpy)
68 memcpy(&value, src, sizeof(value));
69 return src + sizeof(value);
70 }
71};
72
73// Specialization for DenseBase.
74// Serializes [rows, cols, data...].
75template <typename Derived>
76class Serializer<DenseBase<Derived>, void> {
77 public:
78 using Scalar = typename Derived::Scalar;
79
80 struct Header {
81 typename Derived::Index rows;
82 typename Derived::Index cols;
83 };
84
85 EIGEN_DEVICE_FUNC size_t size(const Derived& value) const { return sizeof(Header) + sizeof(Scalar) * value.size(); }
86
87 EIGEN_DEVICE_FUNC uint8_t* serialize(uint8_t* dest, uint8_t* end, const Derived& value) const {
88 if (EIGEN_PREDICT_FALSE(dest == nullptr)) return nullptr;
89 if (EIGEN_PREDICT_FALSE(dest + size(value) > end)) return nullptr;
90 const size_t header_bytes = sizeof(Header);
91 const size_t data_bytes = sizeof(Scalar) * value.size();
92 Header header = {value.rows(), value.cols()};
93 EIGEN_USING_STD(memcpy)
94 memcpy(dest, &header, header_bytes);
95 dest += header_bytes;
96 // An empty object may have a null data(), which memcpy does not accept even for zero bytes.
97 if (data_bytes > 0) memcpy(dest, value.data(), data_bytes);
98 return dest + data_bytes;
99 }
100
101 EIGEN_DEVICE_FUNC const uint8_t* deserialize(const uint8_t* src, const uint8_t* end, Derived& value) const {
102 if (EIGEN_PREDICT_FALSE(src == nullptr)) return nullptr;
103 if (EIGEN_PREDICT_FALSE(src + sizeof(Header) > end)) return nullptr;
104 const size_t header_bytes = sizeof(Header);
105 Header header;
106 EIGEN_USING_STD(memcpy)
107 memcpy(&header, src, header_bytes);
108 src += header_bytes;
109 // The header is untrusted: reject sizes that are negative, that value cannot take, or that overrun the buffer.
110 if (EIGEN_PREDICT_FALSE(header.rows < 0 || header.cols < 0)) return nullptr;
111 if (EIGEN_PREDICT_FALSE((Derived::RowsAtCompileTime != Dynamic && header.rows != Derived::RowsAtCompileTime) ||
112 (Derived::ColsAtCompileTime != Dynamic && header.cols != Derived::ColsAtCompileTime) ||
113 (Derived::MaxRowsAtCompileTime != Dynamic && header.rows > Derived::MaxRowsAtCompileTime) ||
114 (Derived::MaxColsAtCompileTime != Dynamic && header.cols > Derived::MaxColsAtCompileTime)))
115 return nullptr;
116 // resize() forms rows * cols in Index, so bound the product by the largest Index as well as by the buffer.
117 using Count = numext::uint64_t;
118 const Count buffer_coeffs = static_cast<Count>(end - src) / sizeof(Scalar);
119 const Count index_max = static_cast<Count>(NumTraits<typename Derived::Index>::highest());
120 const Count max_coeffs = buffer_coeffs < index_max ? buffer_coeffs : index_max;
121 if (EIGEN_PREDICT_FALSE(header.cols != 0 &&
122 static_cast<Count>(header.rows) > max_coeffs / static_cast<Count>(header.cols)))
123 return nullptr;
124 const size_t data_bytes = sizeof(Scalar) * header.rows * header.cols;
125 value.resize(header.rows, header.cols);
126 if (data_bytes > 0) memcpy(value.data(), src, data_bytes);
127 return src + data_bytes;
128 }
129};
130
131template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
132class Serializer<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>
133 : public Serializer<DenseBase<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>> {};
134
135template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
136class Serializer<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>
137 : public Serializer<DenseBase<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>> {};
138
139namespace internal {
140
141template <typename Arg>
142EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t serialize_size_one(const Arg& arg) {
143 Serializer<std::decay_t<Arg>> serializer;
144 return serializer.size(arg);
145}
146
147template <typename Arg>
148EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE uint8_t* serialize_one(uint8_t* dest, uint8_t* end, const Arg& arg) {
149 Serializer<std::decay_t<Arg>> serializer;
150 return serializer.serialize(dest, end, arg);
151}
152
153template <typename Arg>
154EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const uint8_t* deserialize_one(const uint8_t* src, const uint8_t* end, Arg& arg) {
155 Serializer<std::decay_t<Arg>> serializer;
156 return serializer.deserialize(src, end, arg);
157}
158
159} // namespace internal
160
167template <typename... Args>
168EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t serialize_size(const Args&... args) {
169 size_t size = 0;
170 int unused[] = {0, (size += internal::serialize_size_one(args), 0)...};
171 EIGEN_UNUSED_VARIABLE(unused);
172 return size;
173}
174
183template <typename... Args>
184EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE uint8_t* serialize(uint8_t* dest, uint8_t* end, const Args&... args) {
185 EIGEN_UNUSED_VARIABLE(end);
186 int unused[] = {0, (dest = internal::serialize_one(dest, end, args), 0)...};
187 EIGEN_UNUSED_VARIABLE(unused);
188 return dest;
189}
190
199template <typename... Args>
200EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const uint8_t* deserialize(const uint8_t* src, const uint8_t* end,
201 Args&... args) {
202 EIGEN_UNUSED_VARIABLE(end);
203 int unused[] = {0, (src = internal::deserialize_one(src, end, args), 0)...};
204 EIGEN_UNUSED_VARIABLE(unused);
205 return src;
206}
207
208} // namespace Eigen
209
210#endif // EIGEN_SERIALIZER_H
General-purpose arrays with easy API for coefficient-wise operations.
Definition Array.h:55
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:45
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
Definition Serializer.h:28