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