genericio.cpp 11.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "GenericIO.h"
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <cstdint>
#include <optional>

#ifndef GENERICIO_NO_MPI
#include <mpi.h>
#endif

namespace py = pybind11;

class PyGenericIO : public gio::GenericIO {
public:
20
21
22
23
24
  PyGenericIO(
      const std::string& filename, 
      gio::GenericIO::FileIO method=gio::GenericIO::FileIOPOSIX, 
      gio::GenericIO::MismatchBehavior redistribute=gio::GenericIO::MismatchRedistribute,
      int eff_rank = -1)
25
26
27
28
29
30
#ifdef GENERICIO_NO_MPI
      : gio::GenericIO(filename, method), num_ranks(0) {
#else
      : gio::GenericIO(MPI_COMM_WORLD, filename, method), num_ranks(0) {
#endif
    // open headers and rank info
31
    openAndReadHeader(redistribute, eff_rank);
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    num_ranks = readNRanks();
    // read variable info
    getVariableInfo(variables);
  }

  void inspect() {
    int rank;
  #ifdef GENERICIO_NO_MPI
    rank = 0;
  #else
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  #endif
    if(rank == 0) {
      std::stringstream s;
      s << "Number of Elements: " << readNumElems() << "\n";
      s << "Total number of Elements: " << readTotalNumElems() << "\n";
      s << "[data type] Variable name\n";
      s << "---------------------------------------------\n";
      for (int i = 0; i < variables.size(); ++i) {
        gio::GenericIO::VariableInfo vinfo = variables[i];
        if (vinfo.IsFloat)
          s << "[f";
        else
          s << "[i";
        int NumElements = vinfo.Size / vinfo.ElementSize;
        s << " " << vinfo.ElementSize * 8;
        if (NumElements > 1)
          s << "x" << NumElements;
        s << "] ";
        s << vinfo.Name << "\n";
      }
      s << "\n(i=integer,f=floating point, number bits size)\n";
      py::print(s.str());
    }
  }

68
69
70
  std::map<std::string, py::array> read(
      std::optional<std::vector<std::string>> var_names, 
      bool print_stats=true, 
71
72
      bool collective_stats=true,
      int eff_rank=-1
73
    ) {
74
75
76
77
78
79
80
    int rank;
  #ifdef GENERICIO_NO_MPI
    rank = 0;
  #else
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  #endif

81
    // read number of elements
82
    int64_t num_elem = readNumElems(eff_rank);
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    
    // if no argument, read all
    if(!var_names.has_value()) {
      var_names.emplace(std::vector<std::string>());
      for(const auto& v: variables) {
        var_names->push_back(v.Name);
      }
    }

    clearVariables();
    std::map<std::string, py::array> result;

    for(const std::string& var_name: *var_names) {
      auto varp = std::find_if(
        variables.begin(), 
        variables.end(), 
        [&var_name](const auto& v){ return v.Name == var_name; }
        );
      if (varp != variables.end()) {
        // extra space
Michael Buehlmann's avatar
Michael Buehlmann committed
103
        py::ssize_t readsize = num_elem + requestedExtraSpace()/(*varp).ElementSize;
104
        if((*varp).IsFloat && (*varp).ElementSize == 4) {
Michael Buehlmann's avatar
Michael Buehlmann committed
105
          result[var_name] = py::array_t<float>(readsize);
106
107
          addVariable(*varp, result[var_name].mutable_data(), gio::GenericIO::VarHasExtraSpace);
        } else if((*varp).IsFloat && (*varp).ElementSize == 8) {
Michael Buehlmann's avatar
Michael Buehlmann committed
108
          result[var_name] = py::array_t<double>(readsize);
109
110
          addVariable(*varp, result[var_name].mutable_data(), gio::GenericIO::VarHasExtraSpace);
        } else if(!(*varp).IsFloat && (*varp).ElementSize == 4) {
Michael Buehlmann's avatar
Michael Buehlmann committed
111
          result[var_name] = py::array_t<int32_t>(readsize);
112
113
          addVariable(*varp, result[var_name].mutable_data(), gio::GenericIO::VarHasExtraSpace);
        } else if(!(*varp).IsFloat && (*varp).ElementSize == 8) {
Michael Buehlmann's avatar
Michael Buehlmann committed
114
          result[var_name] = py::array_t<int64_t>(readsize);
115
116
          addVariable(*varp, result[var_name].mutable_data(), gio::GenericIO::VarHasExtraSpace);
        } else if(!(*varp).IsFloat && (*varp).ElementSize == 2) {
Michael Buehlmann's avatar
Michael Buehlmann committed
117
          result[var_name] = py::array_t<uint16_t>(readsize);
118
119
120
121
          addVariable(*varp, result[var_name].mutable_data(), gio::GenericIO::VarHasExtraSpace);
        }
      }
    }
122

123
    readData(eff_rank, print_stats, collective_stats);
124
125
    clearVariables();
    
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  #ifndef GENERICIO_NO_MPI
    MPI_Barrier(MPI_COMM_WORLD);
  #endif

    // get rid of extraspace
    std::for_each(result.begin(), result.end(), [&](auto& item){ item.second.resize({num_elem}); });

    return result;
  }

  const std::vector<gio::GenericIO::VariableInfo> &get_variables() {
    return variables;
  }

  std::array<double, 3> read_phys_origin() {
    std::array<double, 3> origin;
    readPhysOrigin(origin.data());
    return origin;
  }

  std::array<double, 3> read_phys_scale() {
    std::array<double, 3> scale;
    readPhysScale(scale.data());
    return scale;
  }

152
153
154
155
156
157
  std::vector<int> get_source_ranks() {
    std::vector<int> sr;
    getSourceRanks(sr);
    return sr;
  }

158
159
160
161
162
private:
  int num_ranks;
  std::vector<gio::GenericIO::VariableInfo> variables;
};

163
164
165
166
167
std::map<std::string, py::array> read_genericio(
    std::string filename, 
    std::optional<std::vector<std::string>> var_names, 
    PyGenericIO::FileIO method=PyGenericIO::FileIO::FileIOPOSIX, 
    PyGenericIO::MismatchBehavior redistribute=PyGenericIO::MismatchBehavior::MismatchRedistribute, 
168
169
    bool print_stats=true,
    bool collective_stats=true,
170
171
    bool rebalance_source_ranks=false,
    int eff_rank=-1
172
  ) {
173
  PyGenericIO reader(filename, method, redistribute, eff_rank);
174
#ifndef GENERICIO_NO_MPI
175
176
  if(rebalance_source_ranks)
    reader.rebalanceSourceRanks();
177
#endif
178
  return reader.read(var_names, print_stats, collective_stats, eff_rank);
179
180
}

181
182
183
184
185
void inspect_genericio(
    std::string filename, 
    PyGenericIO::FileIO method=PyGenericIO::FileIO::FileIOPOSIX, 
    PyGenericIO::MismatchBehavior redistribute=PyGenericIO::MismatchBehavior::MismatchRedistribute
  ) {
186
187
188
189
  PyGenericIO reader(filename, method, redistribute);
  reader.inspect();
}

190
#ifndef GENERICIO_NO_MPI
191
192
193
194
195
196
void write_genericio(
    std::string filename, 
    std::map<std::string, py::array> variables, 
    std::array<double, 3> phys_scale, std::array<double, 3> phys_origin, 
    PyGenericIO::FileIO method=PyGenericIO::FileIO::FileIOPOSIX
  ) {
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  // check data integrity, find particle count
  int64_t particle_count = -1;
  for(auto const& [name, data]: variables) {
    if(data.ndim() != 1) {
      throw std::runtime_error("dimension of array must be 1 (" + name + ")");
    }
    if(particle_count == -1) {
      particle_count = data.size();
    } else if(particle_count != data.size()) {
      throw std::runtime_error("arrays do not have same length (" + name + ")");
    }
  }

  gio::GenericIO writer(MPI_COMM_WORLD, filename, method);

  writer.setNumElems(particle_count);

  // set size
  for (int d = 0; d < 3; ++d) {
    writer.setPhysOrigin(phys_origin[d], d);
    writer.setPhysScale(phys_scale[d], d);
  }

  for(auto& [name, data]: variables) {
    if(py::isinstance<py::array_t<float>>(data)) 
      writer.addVariable(name.c_str(), reinterpret_cast<float*>(data.mutable_data()));
    else if(py::isinstance<py::array_t<double>>(data)) 
      writer.addVariable(name.c_str(), reinterpret_cast<double*>(data.mutable_data()));
    else if(py::isinstance<py::array_t<int32_t>>(data)) 
      writer.addVariable(name.c_str(), reinterpret_cast<int32_t*>(data.mutable_data()));
    else if(py::isinstance<py::array_t<int64_t>>(data)) 
      writer.addVariable(name.c_str(), reinterpret_cast<int64_t*>(data.mutable_data()));
    else if(py::isinstance<py::array_t<uint16_t>>(data)) 
      writer.addVariable(name.c_str(), reinterpret_cast<uint16_t*>(data.mutable_data()));
    else
      throw std::runtime_error("array dtype not supported for " + name);
  }
  writer.write();
  MPI_Barrier(MPI_COMM_WORLD);

}
#endif

240
241
242
243
244

#ifdef GENERICIO_NO_MPI
PYBIND11_MODULE(pygio_nompi, m) {
  m.doc() = "genericio python module (no MPI support)";
#else  // GENERICIO_NO_MPI
245
PYBIND11_MODULE(pygio, m) {
246
  m.doc() = "genericio python module (with MPI support)";
247
248
249
  m.def("_init_mpi", [](){
    int initialized;
    MPI_Initialized(&initialized);
Michael Buehlmann's avatar
Michael Buehlmann committed
250
251
252
253
    if(!initialized) {
      int level_provided;
      MPI_Init_thread(nullptr, nullptr, MPI_THREAD_SINGLE, &level_provided); 
    }
254
  });
255
256
257
#endif

  py::class_<PyGenericIO> pyGenericIO(m, "PyGenericIO");
258
259
260
261
262
263
264
265
266
267
268

  py::enum_<PyGenericIO::FileIO>(pyGenericIO, "FileIO")
    .value("FileIOMPI", PyGenericIO::FileIO::FileIOMPI)
    .value("FileIOPOSIX", PyGenericIO::FileIO::FileIOPOSIX)
    .value("FileIOMPICollective", PyGenericIO::FileIO::FileIOMPICollective);

  py::enum_<PyGenericIO::MismatchBehavior>(pyGenericIO, "MismatchBehavior")
    .value("MismatchAllowed", PyGenericIO::MismatchBehavior::MismatchAllowed)
    .value("MismatchDisallowed", PyGenericIO::MismatchBehavior::MismatchDisallowed)
    .value("MismatchRedistribute", PyGenericIO::MismatchBehavior::MismatchRedistribute);

269
270
271
272
273
  pyGenericIO.def(
      py::init<std::string, PyGenericIO::FileIO, PyGenericIO::MismatchBehavior>(), 
        py::arg("filename"), 
        py::arg("method")=PyGenericIO::FileIO::FileIOPOSIX, 
        py::arg("redistribute")=PyGenericIO::MismatchBehavior::MismatchRedistribute)
274
275
276
277
278
279
      .def("inspect", &PyGenericIO::inspect, "Print variable infos and size of GenericIO file")
      .def("get_variables", &PyGenericIO::get_variables, "Get a list of VariableInformations defined in the GenericIO file")
      .def("read_num_elems", (size_t (PyGenericIO::*)(int))(&PyGenericIO::readNumElems), py::arg("eff_rank")=-1)
      .def("read_total_num_elems", (uint64_t (PyGenericIO::*)(void))(&PyGenericIO::readTotalNumElems))
      .def("read_phys_origin", &PyGenericIO::read_phys_origin)
      .def("read_phys_scale", &PyGenericIO::read_phys_scale)
280
      .def("read", &PyGenericIO::read,
281
        py::arg("variables")=nullptr,
282
283
284
285
        py::kw_only(),
        py::arg("print_stats")=true, 
        py::arg("collective_stats")=true,
        py::arg("eff_rank")=-1)
286
      .def("get_source_ranks", &PyGenericIO::get_source_ranks)
287
288
289
290
#ifndef GENERICIO_NO_MPI
      .def("rebalance_source_ranks", &PyGenericIO::rebalanceSourceRanks)
#endif
      ;
291
292
293
294
295
296
297
298
299
300

  py::class_<gio::GenericIO::VariableInfo>(pyGenericIO, "VariableInfo")
      .def_readonly("name", &gio::GenericIO::VariableInfo::Name)
      .def_readonly("size", &gio::GenericIO::VariableInfo::Size)
      .def_readonly("element_size", &gio::GenericIO::VariableInfo::ElementSize)
      .def_readonly("is_float", &gio::GenericIO::VariableInfo::IsFloat)
      .def("__repr__", [](const gio::GenericIO::VariableInfo &vi) {
        return std::string("<PyGenericIO.VariableInfo type=") +
               (vi.IsFloat ? "float" : "int") + " name='" + vi.Name + "'>";
      });
301
302
303
304
305
306
307

  m.def("read_genericio", &read_genericio, 
        py::arg("filename"), 
        py::arg("variables")=nullptr, 
        py::kw_only(),
        py::arg("method")=PyGenericIO::FileIO::FileIOPOSIX, 
        py::arg("redistribute")=PyGenericIO::MismatchBehavior::MismatchRedistribute, 
308
309
        py::arg("print_stats")=true,
        py::arg("collective_stats")=true,
310
311
        py::arg("rebalance_sourceranks")=false,
        py::arg("eff_rank")=-1,
312
313
314
315
316
317
318
319
        py::return_value_policy::move);

  m.def("inspect_genericio", &inspect_genericio, 
        py::arg("filename"), 
        py::kw_only(),
        py::arg("method")=PyGenericIO::FileIO::FileIOPOSIX, 
        py::arg("redistribute")=PyGenericIO::MismatchBehavior::MismatchRedistribute);

320
#ifndef GENERICIO_NO_MPI
321
322
323
324
325
326
327
  m.def("write_genericio", &write_genericio, 
        py::arg("filename"), 
        py::arg("variables"), 
        py::arg("phys_scale"), 
        py::arg("phys_origin") = std::array<double, 3>({0., 0., 0.}), 
        py::kw_only(),
        py::arg("method")=PyGenericIO::FileIO::FileIOPOSIX);
328
#endif
329
}