2017-01-18 00:21:18 +00:00
|
|
|
#ifndef GRID_SERIALISATION_HDF5_H
|
|
|
|
#define GRID_SERIALISATION_HDF5_H
|
|
|
|
|
|
|
|
#include <stack>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <H5Cpp.h>
|
2018-03-08 09:51:22 +00:00
|
|
|
#include <Grid/tensors/Tensors.h>
|
2017-01-18 00:21:18 +00:00
|
|
|
#include "Hdf5Type.h"
|
|
|
|
|
|
|
|
#ifndef H5_NO_NAMESPACE
|
|
|
|
#define H5NS H5
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// default thresold above which datasets are used instead of attributes
|
2017-01-19 00:50:21 +00:00
|
|
|
#ifndef HDF5_DEF_DATASET_THRES
|
|
|
|
#define HDF5_DEF_DATASET_THRES 6u
|
2017-01-18 00:21:18 +00:00
|
|
|
#endif
|
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
// name guard for Grid metadata
|
|
|
|
#define HDF5_GRID_GUARD "_Grid_"
|
|
|
|
|
2017-01-18 00:21:18 +00:00
|
|
|
namespace Grid
|
|
|
|
{
|
|
|
|
class Hdf5Writer: public Writer<Hdf5Writer>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Hdf5Writer(const std::string &fileName);
|
2017-01-19 00:50:21 +00:00
|
|
|
virtual ~Hdf5Writer(void) = default;
|
2017-01-18 00:21:18 +00:00
|
|
|
void push(const std::string &s);
|
|
|
|
void pop(void);
|
|
|
|
void writeDefault(const std::string &s, const char *x);
|
|
|
|
template <typename U>
|
|
|
|
void writeDefault(const std::string &s, const U &x);
|
|
|
|
template <typename U>
|
2017-01-20 20:10:41 +00:00
|
|
|
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
|
2017-01-18 00:21:18 +00:00
|
|
|
writeDefault(const std::string &s, const std::vector<U> &x);
|
|
|
|
template <typename U>
|
2017-01-20 20:10:41 +00:00
|
|
|
typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
|
2017-01-18 00:21:18 +00:00
|
|
|
writeDefault(const std::string &s, const std::vector<U> &x);
|
2018-04-20 17:13:21 +01:00
|
|
|
H5NS::Group & getGroup(void);
|
2017-01-19 00:50:21 +00:00
|
|
|
private:
|
2017-01-18 00:21:18 +00:00
|
|
|
template <typename U>
|
2017-01-19 00:50:21 +00:00
|
|
|
void writeSingleAttribute(const U &x, const std::string &name,
|
|
|
|
const H5NS::DataType &type);
|
2017-01-18 00:21:18 +00:00
|
|
|
private:
|
|
|
|
std::string fileName_;
|
|
|
|
std::vector<std::string> path_;
|
|
|
|
H5NS::H5File file_;
|
|
|
|
H5NS::Group group_;
|
2017-01-19 00:50:21 +00:00
|
|
|
unsigned int dataSetThres_{HDF5_DEF_DATASET_THRES};
|
2017-01-18 00:21:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Hdf5Reader: public Reader<Hdf5Reader>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Hdf5Reader(const std::string &fileName);
|
2017-01-19 00:50:21 +00:00
|
|
|
virtual ~Hdf5Reader(void) = default;
|
2017-06-27 14:39:27 +01:00
|
|
|
bool push(const std::string &s);
|
2017-01-18 00:21:18 +00:00
|
|
|
void pop(void);
|
|
|
|
template <typename U>
|
|
|
|
void readDefault(const std::string &s, U &output);
|
|
|
|
template <typename U>
|
2017-01-20 20:10:41 +00:00
|
|
|
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
|
2017-01-19 00:50:21 +00:00
|
|
|
readDefault(const std::string &s, std::vector<U> &x);
|
|
|
|
template <typename U>
|
2017-01-20 20:10:41 +00:00
|
|
|
typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
|
2017-01-19 00:50:21 +00:00
|
|
|
readDefault(const std::string &s, std::vector<U> &x);
|
2018-04-20 17:13:21 +01:00
|
|
|
H5NS::Group & getGroup(void);
|
2017-01-19 00:50:21 +00:00
|
|
|
private:
|
|
|
|
template <typename U>
|
|
|
|
void readSingleAttribute(U &x, const std::string &name,
|
|
|
|
const H5NS::DataType &type);
|
2017-01-18 00:21:18 +00:00
|
|
|
private:
|
2017-01-19 00:50:21 +00:00
|
|
|
std::string fileName_;
|
|
|
|
std::vector<std::string> path_;
|
|
|
|
H5NS::H5File file_;
|
|
|
|
H5NS::Group group_;
|
|
|
|
unsigned int dataSetThres_;
|
2017-01-18 00:21:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Writer template implementation ////////////////////////////////////////////
|
|
|
|
template <typename U>
|
2017-01-19 00:50:21 +00:00
|
|
|
void Hdf5Writer::writeSingleAttribute(const U &x, const std::string &name,
|
|
|
|
const H5NS::DataType &type)
|
2017-01-18 00:21:18 +00:00
|
|
|
{
|
|
|
|
H5NS::Attribute attribute;
|
|
|
|
hsize_t attrDim = 1;
|
|
|
|
H5NS::DataSpace attrSpace(1, &attrDim);
|
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
attribute = group_.createAttribute(name, type, attrSpace);
|
|
|
|
attribute.write(type, &x);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
void Hdf5Writer::writeDefault(const std::string &s, const U &x)
|
|
|
|
{
|
2017-01-20 02:23:55 +00:00
|
|
|
writeSingleAttribute(x, s, Hdf5Type<U>::type());
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void Hdf5Writer::writeDefault(const std::string &s, const std::string &x);
|
|
|
|
|
|
|
|
template <typename U>
|
2017-01-20 20:10:41 +00:00
|
|
|
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
|
2017-01-18 00:21:18 +00:00
|
|
|
Hdf5Writer::writeDefault(const std::string &s, const std::vector<U> &x)
|
|
|
|
{
|
2017-01-19 00:50:21 +00:00
|
|
|
// alias to element type
|
|
|
|
typedef typename element<std::vector<U>>::type Element;
|
|
|
|
|
|
|
|
// flatten the vector and getting dimensions
|
|
|
|
Flatten<std::vector<U>> flat(x);
|
|
|
|
std::vector<hsize_t> dim;
|
|
|
|
const auto &flatx = flat.getFlatVector();
|
2017-01-18 00:21:18 +00:00
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
for (auto &d: flat.getDim())
|
2017-01-18 00:21:18 +00:00
|
|
|
{
|
2017-01-19 00:50:21 +00:00
|
|
|
dim.push_back(d);
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
// write to file
|
|
|
|
H5NS::DataSpace dataSpace(dim.size(), dim.data());
|
2017-01-18 00:21:18 +00:00
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
if (flatx.size() > dataSetThres_)
|
2017-01-18 00:21:18 +00:00
|
|
|
{
|
2017-01-19 00:50:21 +00:00
|
|
|
H5NS::DataSet dataSet;
|
2017-01-18 00:21:18 +00:00
|
|
|
|
2017-01-20 02:23:55 +00:00
|
|
|
dataSet = group_.createDataSet(s, Hdf5Type<Element>::type(), dataSpace);
|
2017-01-20 02:33:04 +00:00
|
|
|
dataSet.write(flatx.data(), Hdf5Type<Element>::type());
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
H5NS::Attribute attribute;
|
|
|
|
|
2017-01-20 02:33:04 +00:00
|
|
|
attribute = group_.createAttribute(s, Hdf5Type<Element>::type(), dataSpace);
|
|
|
|
attribute.write(Hdf5Type<Element>::type(), flatx.data());
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
2017-01-20 20:10:41 +00:00
|
|
|
typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
|
2017-01-18 00:21:18 +00:00
|
|
|
Hdf5Writer::writeDefault(const std::string &s, const std::vector<U> &x)
|
|
|
|
{
|
|
|
|
push(s);
|
2017-01-19 00:50:21 +00:00
|
|
|
writeSingleAttribute(x.size(), HDF5_GRID_GUARD "vector_size",
|
2017-01-20 02:23:55 +00:00
|
|
|
Hdf5Type<uint64_t>::type());
|
2017-01-18 00:21:18 +00:00
|
|
|
for (hsize_t i = 0; i < x.size(); ++i)
|
|
|
|
{
|
|
|
|
write(s + "_" + std::to_string(i), x[i]);
|
|
|
|
}
|
|
|
|
pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader template implementation ////////////////////////////////////////////
|
2017-01-19 00:50:21 +00:00
|
|
|
template <typename U>
|
|
|
|
void Hdf5Reader::readSingleAttribute(U &x, const std::string &name,
|
|
|
|
const H5NS::DataType &type)
|
|
|
|
{
|
|
|
|
H5NS::Attribute attribute;
|
|
|
|
|
|
|
|
attribute = group_.openAttribute(name);
|
|
|
|
attribute.read(type, &x);
|
|
|
|
}
|
|
|
|
|
2017-01-18 00:21:18 +00:00
|
|
|
template <typename U>
|
|
|
|
void Hdf5Reader::readDefault(const std::string &s, U &output)
|
|
|
|
{
|
2017-01-20 02:23:55 +00:00
|
|
|
readSingleAttribute(output, s, Hdf5Type<U>::type());
|
2017-01-19 00:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void Hdf5Reader::readDefault(const std::string &s, std::string &x);
|
|
|
|
|
|
|
|
template <typename U>
|
2017-01-20 20:10:41 +00:00
|
|
|
typename std::enable_if<element<std::vector<U>>::is_number, void>::type
|
2017-01-19 00:50:21 +00:00
|
|
|
Hdf5Reader::readDefault(const std::string &s, std::vector<U> &x)
|
|
|
|
{
|
|
|
|
// alias to element type
|
|
|
|
typedef typename element<std::vector<U>>::type Element;
|
|
|
|
|
|
|
|
// read the dimensions
|
|
|
|
H5NS::DataSpace dataSpace;
|
|
|
|
std::vector<hsize_t> hdim;
|
|
|
|
std::vector<size_t> dim;
|
|
|
|
hsize_t size = 1;
|
|
|
|
|
2017-01-20 19:03:19 +00:00
|
|
|
if (group_.attrExists(s))
|
2017-01-19 00:50:21 +00:00
|
|
|
{
|
2017-01-20 19:03:19 +00:00
|
|
|
dataSpace = group_.openAttribute(s).getSpace();
|
2017-01-19 00:50:21 +00:00
|
|
|
}
|
2017-01-20 19:03:19 +00:00
|
|
|
else
|
2017-01-19 00:50:21 +00:00
|
|
|
{
|
2017-01-20 19:03:19 +00:00
|
|
|
dataSpace = group_.openDataSet(s).getSpace();
|
2017-01-19 00:50:21 +00:00
|
|
|
}
|
|
|
|
hdim.resize(dataSpace.getSimpleExtentNdims());
|
|
|
|
dataSpace.getSimpleExtentDims(hdim.data());
|
|
|
|
for (auto &d: hdim)
|
|
|
|
{
|
|
|
|
dim.push_back(d);
|
|
|
|
size *= d;
|
|
|
|
}
|
2017-01-18 00:21:18 +00:00
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
// read the flat vector
|
|
|
|
std::vector<Element> buf(size);
|
|
|
|
|
|
|
|
if (size > dataSetThres_)
|
|
|
|
{
|
|
|
|
H5NS::DataSet dataSet;
|
|
|
|
|
|
|
|
dataSet = group_.openDataSet(s);
|
2017-01-20 02:23:55 +00:00
|
|
|
dataSet.read(buf.data(), Hdf5Type<Element>::type());
|
2017-01-19 00:50:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
H5NS::Attribute attribute;
|
|
|
|
|
|
|
|
attribute = group_.openAttribute(s);
|
2017-01-20 02:23:55 +00:00
|
|
|
attribute.read(Hdf5Type<Element>::type(), buf.data());
|
2017-01-19 00:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reconstruct the multidimensional vector
|
|
|
|
Reconstruct<std::vector<U>> r(buf, dim);
|
|
|
|
|
|
|
|
x = r.getVector();
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
2017-01-20 20:10:41 +00:00
|
|
|
typename std::enable_if<!element<std::vector<U>>::is_number, void>::type
|
2017-01-19 00:50:21 +00:00
|
|
|
Hdf5Reader::readDefault(const std::string &s, std::vector<U> &x)
|
2017-01-18 00:21:18 +00:00
|
|
|
{
|
2017-01-19 00:50:21 +00:00
|
|
|
uint64_t size;
|
2017-01-18 00:21:18 +00:00
|
|
|
|
2017-01-19 00:50:21 +00:00
|
|
|
push(s);
|
|
|
|
readSingleAttribute(size, HDF5_GRID_GUARD "vector_size",
|
2017-01-20 02:23:55 +00:00
|
|
|
Hdf5Type<uint64_t>::type());
|
2017-01-19 00:50:21 +00:00
|
|
|
x.resize(size);
|
|
|
|
for (hsize_t i = 0; i < x.size(); ++i)
|
|
|
|
{
|
|
|
|
read(s + "_" + std::to_string(i), x[i]);
|
|
|
|
}
|
|
|
|
pop();
|
2017-01-18 00:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|