#ifndef GRID_SERIALISATION_HDF5_H #define GRID_SERIALISATION_HDF5_H #include #include #include #include #include #include "Hdf5Type.h" #ifndef H5_NO_NAMESPACE #define H5NS H5 #endif // default thresold above which datasets are used instead of attributes #ifndef HDF5_DEF_DATASET_THRES #define HDF5_DEF_DATASET_THRES 6u #endif // name guard for Grid metadata #define HDF5_GRID_GUARD "_Grid_" namespace Grid { class Hdf5Writer: public Writer { public: Hdf5Writer(const std::string &fileName); virtual ~Hdf5Writer(void) = default; void push(const std::string &s); void pop(void); void writeDefault(const std::string &s, const char *x); template void writeDefault(const std::string &s, const U &x); template typename std::enable_if>::is_number, void>::type writeDefault(const std::string &s, const std::vector &x); template typename std::enable_if>::is_number, void>::type writeDefault(const std::string &s, const std::vector &x); template void writeDefault(const std::string &s, const iScalar &t); template void writeDefault(const std::string &s, const iVector &t); template void writeDefault(const std::string &s, const iMatrix &t); private: template void writeSingleAttribute(const U &x, const std::string &name, const H5NS::DataType &type); private: std::string fileName_; std::vector path_; H5NS::H5File file_; H5NS::Group group_; unsigned int dataSetThres_{HDF5_DEF_DATASET_THRES}; }; class Hdf5Reader: public Reader { public: Hdf5Reader(const std::string &fileName); virtual ~Hdf5Reader(void) = default; bool push(const std::string &s); void pop(void); template void readDefault(const std::string &s, U &output); template typename std::enable_if>::is_number, void>::type readDefault(const std::string &s, std::vector &x); template typename std::enable_if>::is_number, void>::type readDefault(const std::string &s, std::vector &x); private: template void readSingleAttribute(U &x, const std::string &name, const H5NS::DataType &type); private: std::string fileName_; std::vector path_; H5NS::H5File file_; H5NS::Group group_; unsigned int dataSetThres_; }; // Writer template implementation //////////////////////////////////////////// template void Hdf5Writer::writeSingleAttribute(const U &x, const std::string &name, const H5NS::DataType &type) { H5NS::Attribute attribute; hsize_t attrDim = 1; H5NS::DataSpace attrSpace(1, &attrDim); attribute = group_.createAttribute(name, type, attrSpace); attribute.write(type, &x); } template void Hdf5Writer::writeDefault(const std::string &s, const U &x) { writeSingleAttribute(x, s, Hdf5Type::type()); } template <> void Hdf5Writer::writeDefault(const std::string &s, const std::string &x); template typename std::enable_if>::is_number, void>::type Hdf5Writer::writeDefault(const std::string &s, const std::vector &x) { // alias to element type typedef typename element>::type Element; // flatten the vector and getting dimensions Flatten> flat(x); std::vector dim; const auto &flatx = flat.getFlatVector(); for (auto &d: flat.getDim()) { dim.push_back(d); } // write to file H5NS::DataSpace dataSpace(dim.size(), dim.data()); if (flatx.size() > dataSetThres_) { H5NS::DataSet dataSet; dataSet = group_.createDataSet(s, Hdf5Type::type(), dataSpace); dataSet.write(flatx.data(), Hdf5Type::type()); } else { H5NS::Attribute attribute; attribute = group_.createAttribute(s, Hdf5Type::type(), dataSpace); attribute.write(Hdf5Type::type(), flatx.data()); } } template typename std::enable_if>::is_number, void>::type Hdf5Writer::writeDefault(const std::string &s, const std::vector &x) { push(s); writeSingleAttribute(x.size(), HDF5_GRID_GUARD "vector_size", Hdf5Type::type()); for (hsize_t i = 0; i < x.size(); ++i) { write(s + "_" + std::to_string(i), x[i]); } pop(); } template void Hdf5Writer::writeDefault(const std::string &s, const iScalar &t) { writeDefault(s, tensorToVec(t)); } template void Hdf5Writer::writeDefault(const std::string &s, const iVector &t) { writeDefault(s, tensorToVec(t)); } template void Hdf5Writer::writeDefault(const std::string &s, const iMatrix &t) { writeDefault(s, tensorToVec(t)); } // Reader template implementation //////////////////////////////////////////// template void Hdf5Reader::readSingleAttribute(U &x, const std::string &name, const H5NS::DataType &type) { H5NS::Attribute attribute; attribute = group_.openAttribute(name); attribute.read(type, &x); } template void Hdf5Reader::readDefault(const std::string &s, U &output) { readSingleAttribute(output, s, Hdf5Type::type()); } template <> void Hdf5Reader::readDefault(const std::string &s, std::string &x); template typename std::enable_if>::is_number, void>::type Hdf5Reader::readDefault(const std::string &s, std::vector &x) { // alias to element type typedef typename element>::type Element; // read the dimensions H5NS::DataSpace dataSpace; std::vector hdim; std::vector dim; hsize_t size = 1; if (group_.attrExists(s)) { dataSpace = group_.openAttribute(s).getSpace(); } else { dataSpace = group_.openDataSet(s).getSpace(); } hdim.resize(dataSpace.getSimpleExtentNdims()); dataSpace.getSimpleExtentDims(hdim.data()); for (auto &d: hdim) { dim.push_back(d); size *= d; } // read the flat vector std::vector buf(size); if (size > dataSetThres_) { H5NS::DataSet dataSet; dataSet = group_.openDataSet(s); dataSet.read(buf.data(), Hdf5Type::type()); } else { H5NS::Attribute attribute; attribute = group_.openAttribute(s); attribute.read(Hdf5Type::type(), buf.data()); } // reconstruct the multidimensional vector Reconstruct> r(buf, dim); x = r.getVector(); } template typename std::enable_if>::is_number, void>::type Hdf5Reader::readDefault(const std::string &s, std::vector &x) { uint64_t size; push(s); readSingleAttribute(size, HDF5_GRID_GUARD "vector_size", Hdf5Type::type()); x.resize(size); for (hsize_t i = 0; i < x.size(); ++i) { read(s + "_" + std::to_string(i), x[i]); } pop(); } } #endif