KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
hdf5_utils.h
Go to the documentation of this file.
1#ifndef HDF5_UTILS_H
2#define HDF5_UTILS_H
3
4#include <highfive/H5DataSet.hpp>
5#include <highfive/H5DataSpace.hpp>
6#include <highfive/H5File.hpp>
7#include <highfive/H5FileDriver.hpp>
8#include <highfive/H5Group.hpp>
9
10#include "mpi_utils.h"
11
12#define DUMP(pfile, data, size) hdump(pfile, #data, data, size)
13
14#define LOAD(pfile, data, size) hload(pfile, #data, data, size)
15
16using namespace HighFive;
17
18template <typename T>
19int hdump(HighFive::File* pfile, const std::string& name, T* data, const std::size_t& size) {
20 if (pfile->exist(name)) {
21 // no type & dimensions check! @unsafe
22 auto ds = pfile->getDataSet(name);
23 ds.select({std::size_t(mpi_rank), 0}, {1, size}).write(data);
24 } else {
25 auto ds = pfile->createDataSet<T>(name, DataSpace({std::size_t(mpi_nprocs), size}));
26 ds.select({std::size_t(mpi_rank), 0}, {1, size}).write(data);
27 }
28 return 0;
29}
30
31template <typename T>
32int hload(HighFive::File* pfile, const std::string& name, T* data, const std::size_t& size) {
33 try {
34 auto ds = pfile->getDataSet(name);
35 auto dims = ds.getDimensions();
36 CHECK_EQ(dims[0], mpi_nprocs);
37 CHECK_EQ(dims[1], size);
38
39 ds.select({std::size_t(mpi_rank), 0}, {1, size}).read(data);
40 } catch (HighFive::Exception& e) { LOG(FATAL); }
41 return 0;
42}
43
44// Using MPI rank to manage data load & dump is too limited and inconvenient for communication
45// between jobs with different MPI procs. Thus I overload this two API
46template <typename T>
47int hdump_extend(HighFive::File* pfile, const std::string& name, T* data, const std::size_t& size, const int& DIM0,
48 const int& rank) {
49 if (pfile->exist(name)) {
50 // no type & dimensions check! @unsafe
51 auto ds = pfile->getDataSet(name);
52 ds.select({std::size_t(rank), 0}, {1, size}).write(data);
53 } else {
54 auto ds = pfile->createDataSet<T>(name, DataSpace({std::size_t(DIM0), size}));
55 ds.select({std::size_t(rank), 0}, {1, size}).write(data);
56 }
57 return 0;
58}
59
60template <typename T>
61int hload_extend(HighFive::File* pfile, const std::string& name, T* data, const std::size_t& size, const int& DIM0,
62 const int& rank) {
63 try {
64 auto ds = pfile->getDataSet(name);
65 auto dims = ds.getDimensions();
66 CHECK_EQ(dims[0], DIM0);
67 CHECK_EQ(dims[1], size);
68
69 ds.select({std::size_t(rank), 0}, {1, size}).read(data);
70 } catch (HighFive::Exception& e) { LOG(FATAL); }
71 return 0;
72}
73
74#endif // HDF5_UTILS_H
int mpi_rank
int hload_extend(HighFive::File *pfile, const std::string &name, T *data, const std::size_t &size, const int &DIM0, const int &rank)
Definition hdf5_utils.h:61
int hload(HighFive::File *pfile, const std::string &name, T *data, const std::size_t &size)
Definition hdf5_utils.h:32
int hdump_extend(HighFive::File *pfile, const std::string &name, T *data, const std::size_t &size, const int &DIM0, const int &rank)
Definition hdf5_utils.h:47
int hdump(HighFive::File *pfile, const std::string &name, T *data, const std::size_t &size)
Definition hdf5_utils.h:19