openPMD-api
Pickle.hpp
1 /* Copyright 2018-2021 Axel Huebl
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser 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  * openPMD-api 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 and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 #pragma once
22 
23 #include "openPMD/IO/Access.hpp"
24 #include "openPMD/Series.hpp"
25 #include "openPMD/backend/Attributable.hpp"
26 
27 #include "Common.hpp"
28 
29 #include <exception>
30 #include <string>
31 #include <tuple>
32 #include <vector>
33 
34 namespace openPMD
35 {
44 template <typename... T_Args, typename T_SeriesAccessor>
45 inline void
46 add_pickle(pybind11::class_<T_Args...> &cl, T_SeriesAccessor &&seriesAccessor)
47 {
48  // helper: get first class in py::class_ - that's the type we pickle
49  using PickledClass =
50  typename std::tuple_element<0, std::tuple<T_Args...> >::type;
51 
52  cl.def(py::pickle(
53  // __getstate__
54  [](const PickledClass &a) {
55  // Return a tuple that fully encodes the state of the object
56  Attributable::MyPath const myPath = a.myPath();
57  return py::make_tuple(myPath.filePath(), myPath.group);
58  },
59 
60  // __setstate__
61  [&seriesAccessor](py::tuple t) {
62  // our tuple has exactly two elements: filePath & group
63  if (t.size() != 2)
64  throw std::runtime_error("Invalid state!");
65 
66  std::string const filename = t[0].cast<std::string>();
67  std::vector<std::string> const group =
68  t[1].cast<std::vector<std::string> >();
69 
70  // Create a new openPMD Series and keep it alive.
71  // This is a big hack for now, but it works for our use
72  // case, which is spinning up remote serial read series
73  // for DASK.
74  static auto series = openPMD::Series(filename, Access::READ_ONLY);
75  return seriesAccessor(series, group);
76  }));
77 }
78 } // namespace openPMD
Implementation for the root level of the openPMD hierarchy.
Definition: Series.hpp:219
Public definitions of openPMD-api.
Definition: Date.cpp:29
@ READ_ONLY
Open Series as read-only, fails if Series is not found.
void add_pickle(pybind11::class_< T_Args... > &cl, T_SeriesAccessor &&seriesAccessor)
Helper to Pickle Attributable Classes.
Definition: Pickle.hpp:46
String serialization to describe an Attributable.
Definition: Attributable.hpp:225
std::string filePath() const
Reconstructs a path that can be passed to a Series constructor.
Definition: Attributable.cpp:196
std::vector< std::string > group
e.g., .bp, .h5, .json, ...
Definition: Attributable.hpp:237