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 <pybind11/pybind11.h>
28 #include <pybind11/stl.h>
29 
30 #include <exception>
31 #include <string>
32 #include <tuple>
33 #include <vector>
34 
35 namespace openPMD
36 {
45 template <typename... T_Args, typename T_SeriesAccessor>
46 inline void
47 add_pickle(pybind11::class_<T_Args...> &cl, T_SeriesAccessor &&seriesAccessor)
48 {
49  namespace py = pybind11;
50 
51  // helper: get first class in py::class_ - that's the type we pickle
52  using PickledClass =
53  typename std::tuple_element<0, std::tuple<T_Args...> >::type;
54 
55  cl.def(py::pickle(
56  // __getstate__
57  [](const PickledClass &a) {
58  // Return a tuple that fully encodes the state of the object
59  Attributable::MyPath const myPath = a.myPath();
60  return py::make_tuple(myPath.filePath(), myPath.group);
61  },
62 
63  // __setstate__
64  [&seriesAccessor](py::tuple t) {
65  // our tuple has exactly two elements: filePath & group
66  if (t.size() != 2)
67  throw std::runtime_error("Invalid state!");
68 
69  std::string const filename = t[0].cast<std::string>();
70  std::vector<std::string> const group =
71  t[1].cast<std::vector<std::string> >();
72 
73  // Create a new openPMD Series and keep it alive.
74  // This is a big hack for now, but it works for our use
75  // case, which is spinning up remote serial read series
76  // for DASK.
77  static auto series = openPMD::Series(filename, Access::READ_ONLY);
78  return seriesAccessor(series, group);
79  }));
80 }
81 } // namespace openPMD
String serialization to describe an Attributable.
Definition: Attributable.hpp:212
void add_pickle(pybind11::class_< T_Args... > &cl, T_SeriesAccessor &&seriesAccessor)
Helper to Pickle Attributable Classes.
Definition: Pickle.hpp:47
Implementation for the root level of the openPMD hierarchy.
Definition: Series.hpp:186
Public definitions of openPMD-api.
std::string filePath() const
Reconstructs a path that can be passed to a Series constructor.
Definition: Attributable.cpp:175
std::vector< std::string > group
e.g., .bp, .h5, .json, ...
Definition: Attributable.hpp:224
Open Series as read-only, fails if Series is not found.