Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
Serializer.h
Go to the documentation of this file.
1 //===-- serialbox/core/frontend/gridtools/Serializer.h ------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
15 #ifndef SERIALBOX_CORE_FRONTEND_GRIDTOOLS_SERIALIZER_H
16 #define SERIALBOX_CORE_FRONTEND_GRIDTOOLS_SERIALIZER_H
17 
27 #include <memory>
28 
29 namespace serialbox {
30 
33 namespace gridtools {
34 
38 class serializer {
39 public:
46 
59  static int serializationStatus() noexcept { return SerializerImpl::serializationStatus(); }
60 
71 
82 
91  serializer(open_mode mode, const std::string& directory, const std::string& prefix,
92  std::string archive_name = "Binary")
93  : serializerImpl_(std::make_shared<SerializerImpl>(mode, directory, prefix, archive_name)) {}
94 
98  serializer(const serializer&) = default;
99 
101  serializer(serializer&&) = default;
102 
106  serializer& operator=(const serializer&) = delete;
107 
109  serializer& operator=(serializer&&) = default;
110 
112  open_mode mode() const noexcept { return serializerImpl_->mode(); }
113 
115  std::string directory() const noexcept { return serializerImpl_->directory().string(); }
116 
118  std::string prefix() const noexcept { return serializerImpl_->prefix(); }
119 
121  std::string meta_data_file() const noexcept { return serializerImpl_->metaDataFile().string(); }
122 
124  std::string archive_name() const noexcept { return serializerImpl_->archiveName(); }
125 
129  void clear() noexcept { serializerImpl_->clear(); }
130 
136  void update_meta_data() { serializerImpl_->updateMetaData(); }
137 
138  //===----------------------------------------------------------------------------------------===//
139  // Global Meta-Information
140  //===----------------------------------------------------------------------------------------===//
141 
148  template <class StringType, class ValueType>
149  void add_global_meta_info(StringType&& key, ValueType&& value) {
150  serializerImpl_->addGlobalMetainfo(std::forward<StringType>(key),
151  std::forward<ValueType>(value));
152  }
153 
160  template <class T, class StringType>
161  T get_global_meta_info_as(StringType&& key) const {
162  return serializerImpl_->getGlobalMetainfoAs<T>(std::forward<StringType>(key));
163  }
164 
167  return meta_info_map(serializerImpl_->globalMetainfoPtr());
168  }
169 
170  //===----------------------------------------------------------------------------------------===//
171  // Register and Query Fields
172  //===----------------------------------------------------------------------------------------===//
173 
180  template <class StringType, typename... Args>
181  void register_field(StringType&& name, Args&&... args) {
182  serializerImpl_->registerField(std::forward<StringType>(name),
183  *field_meta_info(std::forward<Args>(args)...).impl());
184  }
185 
190  template <class StringType>
191  bool has_field(StringType&& name) const noexcept {
192  return serializerImpl_->hasField(std::forward<StringType>(name));
193  }
194 
203  template <class StringType, class KeyType, class ValueType>
204  bool add_meta_info_to_field(StringType&& name, KeyType&& key, ValueType&& value) {
205  return serializerImpl_->addFieldMetainfoImpl(
206  std::forward<StringType>(name), std::forward<KeyType>(key), std::forward<ValueType>(value));
207  }
208 
214  template <class StringType>
215  field_meta_info get_field_meta_info(StringType&& name) const {
216  return field_meta_info(
217  serializerImpl_->fieldMap().getFieldMetainfoImplPtrOf(std::forward<StringType>(name)));
218  }
219 
222  std::vector<std::string> fieldnames() const { return serializerImpl_->fieldnames(); }
223 
224  //===----------------------------------------------------------------------------------------===//
225  // Register and Query Savepoints
226  //===----------------------------------------------------------------------------------------===//
227 
232  template <typename... Args>
233  bool register_savepoint(Args&&... args) noexcept {
234  return serializerImpl_->registerSavepoint(*savepoint(std::forward<Args>(args)...).impl());
235  }
236 
241  template <typename... Args>
242  bool has_savepoint(const savepoint sp) const noexcept {
243  return serializerImpl_->savepointVector().exists(*sp.impl());
244  }
245 
247  const std::vector<savepoint>& savepoints() {
248  const auto& savepoints = serializerImpl_->savepointVector().savepoints();
249  if(!savepoints_ || (savepoints.size() != savepoints_->size())) {
250  savepoints_ = std::make_shared<std::vector<savepoint>>();
251  for(std::size_t i = 0; i < savepoints.size(); ++i)
252  savepoints_->emplace_back(savepoints[i]);
253  }
254  return *savepoints_;
255  }
256 
257  //===----------------------------------------------------------------------------------------===//
258  // Writing
259  //===----------------------------------------------------------------------------------------===//
260 
272  template <class StorageType>
273  void write(const std::string& name, const savepoint& sp, const StorageType& storage,
274  bool register_field = true) {
275  if(register_field && !serializerImpl_->fieldMap().hasField(name))
276  this->register_field(name, storage);
277 
278  StorageView storageView(
279  internal::get_origin_ptr(storage, 0), ToTypeID<typename StorageType::data_t>::value,
280  std::move(internal::get_dims(storage)), std::move(internal::get_strides(storage)));
281  serializerImpl_->write(name, *sp.impl(), storageView);
282  }
283 
295  template <class T>
296  void write(const std::string& name, const savepoint& sp, T* origin_ptr,
297  const std::vector<int>& strides) {
298  const auto& dims = get_field_meta_info(name).dims();
299  StorageView storageView(origin_ptr, ToTypeID<T>::value, dims, strides);
300  serializerImpl_->write(name, *sp.impl(), storageView);
301  }
302 
312  template <class StorageType>
313  static void to_file(std::string file, const StorageType& storage, std::string archive_name = "") {
314  StorageView storageView(
315  internal::get_origin_ptr(storage, 0), ToTypeID<typename StorageType::data_t>::value,
316  std::move(internal::get_dims(storage)), std::move(internal::get_strides(storage)));
317 
318  if(archive_name.empty())
320 
321  ArchiveFactory::writeToFile(file, storageView, archive_name, storage.name());
322  }
323 
324  //===----------------------------------------------------------------------------------------===//
325  // Reading
326  //===----------------------------------------------------------------------------------------===//
327 
338  template <class StorageType>
339  void read(const std::string& name, const savepoint& sp, StorageType& storage) {
340  StorageView storageView(
341  internal::get_origin_ptr(storage, 0), ToTypeID<typename StorageType::data_t>::value,
342  std::move(internal::get_dims(storage)), std::move(internal::get_strides(storage)));
343 
344  serializerImpl_->read(name, *sp.impl(), storageView);
345  }
346 
360  template <class StorageType>
361  void read_slice(const std::string& name, const savepoint& sp, StorageType& storage, Slice slice) {
362  StorageView storageView(
363  internal::get_origin_ptr(storage, 0), ToTypeID<typename StorageType::data_t>::value,
364  std::move(internal::get_dims(storage)), std::move(internal::get_strides(storage)));
365  storageView.setSlice(slice);
366  serializerImpl_->read(name, *sp.impl(), storageView);
367  }
368 
380  template <class T>
381  void read(const std::string& name, const savepoint& sp, T* origin_ptr,
382  const std::vector<int>& strides) {
383  const auto& dims = get_field_meta_info(name).dims();
384  StorageView storageView(origin_ptr, ToTypeID<T>::value, dims, strides);
385  serializerImpl_->read(name, *sp.impl(), storageView);
386  }
387 
398  template <class StorageType>
399  static void from_file(std::string file, StorageType& storage, std::string archive_name = "") {
400  StorageView storageView(
401  internal::get_origin_ptr(storage, 0), ToTypeID<typename StorageType::data_t>::value,
402  std::move(internal::get_dims(storage)), std::move(internal::get_strides(storage)));
403 
404  if(archive_name.empty())
406 
407  ArchiveFactory::readFromFile(file, storageView, archive_name, storage.name());
408  }
409 
411  std::string to_string() const { return serializerImpl_->toString(); }
412 
414  friend std::ostream& operator<<(std::ostream& stream, const serializer& s) {
415  return (stream << *s.serializerImpl_);
416  }
417 
418 private:
419  std::shared_ptr<SerializerImpl> serializerImpl_;
420  std::shared_ptr<std::vector<savepoint>> savepoints_;
421 };
422 
423 } // namespace gridtools
424 
425 } // namespace serialbox
426 
427 #endif
static void enableSerialization() noexcept
Enable serialization.
Definition: Serializer.h:70
bool has_savepoint(const savepoint sp) const noexcept
Check if savepoint exists.
Definition: Serializer.h:242
static std::string archiveFromExtension(std::string filename)
Deduce the name of the archive according to the extension of the filename
const std::vector< savepoint > & savepoints()
Get a refrence to savepoint vector.
Definition: Serializer.h:247
void update_meta_data()
Convert meta-data to JSON and serialize to MetaData-prefix.json and ArchiveMetaData-prefix.json.
Definition: Serializer.h:136
open_mode mode() const noexcept
Access the mode of the serializer.
Definition: Serializer.h:112
static void from_file(std::string file, StorageType &storage, std::string archive_name="")
Directly read a gridools field, given by storage, from file
Definition: Serializer.h:399
void write(const std::string &name, const savepoint &sp, T *origin_ptr, const std::vector< int > &strides)
Serialize field name given as origin_ptr and strides at savepoint to disk.
Definition: Serializer.h:296
const std::shared_ptr< SavepointImpl > & impl() const
Get implementation pointer.
Definition: Savepoint.h:155
meta_info_map global_meta_info() noexcept
Get a refrence to the global meta information.
Definition: Serializer.h:166
static int serializationStatus() noexcept
Get the status of serialization.
static void to_file(std::string file, const StorageType &storage, std::string archive_name="")
Directly write gridools field, given by storage, to file
Definition: Serializer.h:313
Definition: json.hpp:10517
T get_global_meta_info_as(StringType &&key) const
Query global meta_info_map for key and retrieve value as type T
Definition: Serializer.h:161
Represent a mutable view to a multi-dimensional storage.
Definition: StorageView.h:33
void register_field(StringType &&name, Args &&... args)
Register a new field within the serializer.
Definition: Serializer.h:181
void setSlice(Slice slice)
Set the slice of the StorageView
Definition: StorageView.cpp:71
static void disableSerialization() noexcept
Disable serialization.
Definition: Serializer.h:81
Namespace of the serialbox library.
Definition: Archive.h:25
static void disableSerialization() noexcept
Disable serialization.
Shared implementation of the Serializer.
Meta-information of a data field.
Definition: FieldMetainfo.h:33
void read_slice(const std::string &name, const savepoint &sp, StorageType &storage, Slice slice)
Deserialize sliced field name (given as storageView and slice) at savepoint from disk.
Definition: Serializer.h:361
std::vector< int > & dims() noexcept
Access dimensions.
Hash-map of meta-information of the form key = value pair or key = {value1, ..., valueN} ...
Definition: MetainfoMap.h:34
static void readFromFile(std::string filename, StorageView &storageView, std::string archiveName, std::string fieldname)
Directly read field (given by storageView) from file.
friend std::ostream & operator<<(std::ostream &stream, const serializer &s)
Convert to stream.
Definition: Serializer.h:414
std::string to_string() const
Convert to string.
Definition: Serializer.h:411
void read(const std::string &name, const savepoint &sp, T *origin_ptr, const std::vector< int > &strides)
Deserialize field name given as origin_ptr and strides at savepoint to disk.
Definition: Serializer.h:381
serializer(open_mode mode, const std::string &directory, const std::string &prefix, std::string archive_name="Binary")
Construct the serializer.
Definition: Serializer.h:91
Namespace of the gridtools frontend.
void read(const std::string &name, const savepoint &sp, StorageType &storage)
Deserialize gridtools field name given as storage at savepoint to disk.
Definition: Serializer.h:339
field_meta_info get_field_meta_info(StringType &&name) const
Get field_meta_info of field name
Definition: Serializer.h:215
std::string meta_data_file() const noexcept
Access the path to the meta-data file.
Definition: Serializer.h:121
static void writeToFile(std::string filename, const StorageView &storageView, std::string archiveName, std::string fieldname)
Directly write field (given by storageView) to file.
void clear() noexcept
Drop all field and savepoint meta-data.
Definition: Serializer.h:129
void add_global_meta_info(StringType &&key, ValueType &&value)
Add a new key-value pair to the global meta-information of the serializer.
Definition: Serializer.h:149
std::string directory() const noexcept
Access the directory in which the Serializer and Archive are opened.
Definition: Serializer.h:115
OpenModeKind
Policy for opening files in the Serializer and Archive.
Definition: Type.h:40
void write(const std::string &name, const savepoint &sp, const StorageType &storage, bool register_field=true)
Serialize gridtools field name given as storage at savepoint to disk.
Definition: Serializer.h:273
bool has_field(StringType &&name) const noexcept
Check if field name has been registred within the serializer.
Definition: Serializer.h:191
static void enableSerialization() noexcept
Enable serialization.
std::vector< std::string > fieldnames() const
Get a vector of all registered fields.
Definition: Serializer.h:222
bool register_savepoint(Args &&... args) noexcept
Register a savepoint.
Definition: Serializer.h:233
Serializer implemenation of the gridtools frontend.
Definition: Serializer.h:38
serializer & operator=(const serializer &)=delete
Copy assignment.
Savepoint implemenation of the gridtools frontend.
Definition: Savepoint.h:34
Convert C++ type T to TypeID.
Definition: Type.h:130
std::string prefix() const noexcept
Access prefix of all filenames.
Definition: Serializer.h:118
Specification of the slice indices which is used for partial loading of serialized data...
Definition: Slice.h:53
static int serializationStatus() noexcept
Get the status of serialization.
Definition: Serializer.h:59
std::string archive_name() const noexcept
Name of the archive in use.
Definition: Serializer.h:124
bool add_meta_info_to_field(StringType &&name, KeyType &&key, ValueType &&value)
Add key = value or key = {value1, ..., valueN} meta-information to field name
Definition: Serializer.h:204