27 #ifndef MXNET_COMMON_SERIALIZATION_H_ 28 #define MXNET_COMMON_SERIALIZATION_H_ 30 #include <dmlc/logging.h> 32 #include <nnvm/graph_attr_types.h> 33 #include <nnvm/tuple.h> 40 #include <unordered_map> 41 #include <unordered_set> 55 template<
typename K,
typename V>
61 template<
typename... Args>
65 inline void Serialize(
const T &obj,
char **buffer);
68 inline void Serialize(
const nnvm::Tuple <T> &obj,
char **buffer);
70 template<
typename K,
typename V>
71 inline void Serialize(
const std::map <K, V> &obj,
char **buffer);
74 inline void Serialize(
const std::string &obj,
char **buffer);
76 template<
typename... Args>
77 inline void Serialize(
const std::tuple<Args...> &obj,
char **buffer);
80 inline void Deserialize(T *obj,
const std::string &buffer,
size_t *curr_pos);
83 inline void Deserialize(nnvm::Tuple <T> *obj,
const std::string &buffer,
size_t *curr_pos);
85 template<
typename K,
typename V>
86 inline void Deserialize(std::map <K, V> *obj,
const std::string &buffer,
size_t *curr_pos);
89 inline void Deserialize(std::string *obj,
const std::string &buffer,
size_t *curr_pos);
91 template<
typename... Args>
92 inline void Deserialize(std::tuple<Args...> *obj,
const std::string &buffer,
size_t *curr_pos);
97 static const bool value = !std::is_pod<T>::value;
109 for (
const auto& el : obj) {
114 return 4 + (obj.ndim() *
sizeof(T));
118 template<
typename K,
typename V>
122 for (
const auto& p : obj) {
126 for (
const auto& p : obj) {
129 sum_val +=
sizeof(V) * obj.size();
131 for (
const auto& p : obj) {
134 sum_val +=
sizeof(K) * obj.size();
136 sum_val += (
sizeof(K) +
sizeof(V)) * obj.size();
143 return obj.size() + 4;
148 template<
typename... Args>
149 static inline size_t Compute(
const std::tuple<Args...> &obj) {
156 template<
typename... Args>
157 static inline size_t Compute(
const std::tuple<Args...> &obj) {
162 template<
typename... Args>
171 uint32_t size = obj.size();
172 std::memcpy(*buffer, &size, 4);
174 return (
size_t) size;
179 std::memcpy(*buffer, &obj,
sizeof(T));
180 *buffer +=
sizeof(T);
184 inline void Serialize(
const nnvm::Tuple <T> &obj,
char **buffer) {
185 uint32_t size = obj.ndim();
186 std::memcpy(*buffer, &size, 4);
188 for (
auto& el : obj) {
193 template<
typename K,
typename V>
194 inline void Serialize(
const std::map <K, V> &obj,
char **buffer) {
196 for (
auto& p : obj) {
203 inline void Serialize(
const std::string &obj,
char **buffer) {
205 std::memcpy(*buffer, &obj[0], size);
211 template<
typename... Args>
212 static inline void Compute(
const std::tuple<Args...> &obj,
char **buffer) {
220 template<
typename... Args>
221 static inline void Compute(
const std::tuple<Args...> &obj,
char **buffer) {
226 template<
typename... Args>
227 inline void Serialize(
const std::tuple<Args...> &obj,
char **buffer) {
235 uint32_t size = obj->size();
236 std::memcpy(&size, &buffer[*curr_pos], 4);
238 return (
size_t) size;
242 inline void Deserialize(T *obj,
const std::string &buffer,
size_t *curr_pos) {
243 std::memcpy(obj, &buffer[*curr_pos],
sizeof(T));
244 *curr_pos +=
sizeof(T);
248 inline void Deserialize(nnvm::Tuple <T> *obj,
const std::string &buffer,
size_t *curr_pos) {
249 uint32_t size = obj->ndim();
250 std::memcpy(&size, &buffer[*curr_pos], 4);
253 for (
size_t i = 0; i < size; ++i) {
258 template<
typename K,
typename V>
259 inline void Deserialize(std::map <K, V> *obj,
const std::string &buffer,
size_t *curr_pos) {
262 for (
size_t i = 0; i < size; ++i) {
269 inline void Deserialize(std::string *obj,
const std::string &buffer,
size_t *curr_pos) {
272 std::memcpy(&(obj->front()), &buffer[*curr_pos], size);
278 template<
typename... Args>
279 static inline void Compute(std::tuple<Args...> *obj,
280 const std::string &buffer,
size_t *curr_pos) {
288 template<
typename... Args>
289 static inline void Compute(std::tuple<Args...> *obj,
290 const std::string &buffer,
size_t *curr_pos) {
295 template<
typename... Args>
296 inline void Deserialize(std::tuple<Args...> *obj,
const std::string &buffer,
size_t *curr_pos) {
302 inline void Serialize(
const T& obj, std::string* serialized_data) {
304 char* curr_pos = &(serialized_data->front());
306 CHECK_EQ((int64_t)curr_pos - (int64_t)&(serialized_data->front()),
307 serialized_data->size());
311 inline void Deserialize(T* obj,
const std::string& serialized_data) {
314 CHECK_EQ(curr_pos, serialized_data.size());
319 #endif // MXNET_COMMON_SERIALIZATION_H_ namespace of mxnet
Definition: base.h:118
Definition: serialization.h:96
static void Compute(const std::tuple< Args... > &obj, char **buffer)
Definition: serialization.h:212
static size_t Compute(const std::tuple< Args... > &obj)
Definition: serialization.h:149
size_t SerializedContainerSize(const T &obj, char **buffer)
Definition: serialization.h:170
Definition: serialization.h:277
static void Compute(std::tuple< Args... > *obj, const std::string &buffer, size_t *curr_pos)
Definition: serialization.h:289
size_t DeserializedContainerSize(T *obj, const std::string &buffer, size_t *curr_pos)
Definition: serialization.h:234
void Deserialize(T *obj, const std::string &buffer, size_t *curr_pos)
Definition: serialization.h:242
static const bool value
Definition: serialization.h:97
static void Compute(std::tuple< Args... > *obj, const std::string &buffer, size_t *curr_pos)
Definition: serialization.h:279
Definition: serialization.h:210
Data structures that can appear in graph attributes.
void Serialize(const T &obj, char **buffer)
Definition: serialization.h:178
Definition: serialization.h:147
static void Compute(const std::tuple< Args... > &obj, char **buffer)
Definition: serialization.h:221
size_t SerializedSize(const T &obj)
Definition: serialization.h:101
static size_t Compute(const std::tuple< Args... > &obj)
Definition: serialization.h:157