32#ifndef GRID_SERIALISATION_HDF5_H
33#define GRID_SERIALISATION_HDF5_H
44#ifndef HDF5_DEF_DATASET_THRES
45#define HDF5_DEF_DATASET_THRES 6u
49#define HDF5_GRID_GUARD "_Grid_"
58 void push(
const std::string &s);
64 void writeRagged(
const std::string &s,
const std::vector<U> &x);
66 typename std::enable_if<is_flattenable<std::vector<U>>::value>
::type
67 writeDefault(
const std::string &s,
const std::vector<U> &x);
69 typename std::enable_if<!is_flattenable<std::vector<U>>::value>
::type
72 void writeMultiDim(
const std::string &s,
const std::vector<size_t> & Dimensions,
const U * pDataRowMajor,
size_t NumElements);
77 const H5NS::DataType &
type);
89 Hdf5Reader(
const std::string &fileName,
const bool readOnly =
true);
91 bool push(
const std::string &s);
96 void readRagged(
const std::string &s, std::vector<U> &x);
98 typename std::enable_if<is_flattenable<std::vector<U>>::value>
::type
99 readDefault(
const std::string &s, std::vector<U> &x);
100 template <
typename U>
101 typename std::enable_if<!is_flattenable<std::vector<U>>::value>
::type
103 template <
typename U>
104 void readMultiDim(
const std::string &s, std::vector<U> &buf, std::vector<size_t> &dim);
107 template <
typename U>
109 const H5NS::DataType &
type);
119 template <
typename U>
121 const H5NS::DataType &
type)
123 H5NS::Attribute attribute;
125 H5NS::DataSpace attrSpace(1, &attrDim);
127 attribute =
group_.createAttribute(name,
type, attrSpace);
128 attribute.write(
type, &x);
131 template <
typename U>
140 template <
typename U>
144 const int rank =
static_cast<int>(Dimensions.size());
145 std::vector<hsize_t> dim(rank);
146 for(
int i = 0; i < rank; i++)
147 dim[i] = Dimensions[i];
149 H5NS::DataSpace dataSpace(rank, dim.data());
154 const hsize_t MaxElements = (
sizeof(
U ) == 1 ) ? 0xffffffff : 0x100000000 /
sizeof(
U );
155 hsize_t ElementsPerChunk = 1;
156 bool bTooBig =
false;
157 for(
int i = rank - 1 ; i != -1 ; i-- ) {
163 while( d > MaxElements && ( d & 1 ) == 0 )
165 const char ErrorMsg[] =
" dimension > 4GB and not divisible by 2^n. "
166 "Hdf5IO chunk size will be inefficient. NB Serialisation is not intended for large datasets - please consider alternatives.";
167 if( d > MaxElements ) {
168 std::cout <<
GridLogWarning <<
"Individual" << ErrorMsg << std::endl;
169 hsize_t quotient = d / MaxElements;
170 if( d % MaxElements )
175 hsize_t OverflowCheck = ElementsPerChunk;
176 ElementsPerChunk *= d;
177 assert( OverflowCheck == ElementsPerChunk / d &&
"Product of dimensions overflowed hsize_t" );
179 while( ElementsPerChunk > MaxElements && ( ElementsPerChunk & 1 ) == 0 ) {
182 ElementsPerChunk >>= 1;
184 if( ElementsPerChunk > MaxElements ) {
185 std::cout <<
GridLogWarning <<
"Product of" << ErrorMsg << std::endl;
186 hsize_t quotient = ElementsPerChunk / MaxElements;
187 if( ElementsPerChunk % MaxElements )
190 ElementsPerChunk /= quotient;
194 H5NS::DataSet dataSet;
195 H5NS::DSetCreatPropList plist;
196 plist.setChunk(rank, dim.data());
197 plist.setFletcher32();
203 H5NS::Attribute attribute;
209 template <
typename U>
210 typename std::enable_if<is_flattenable<std::vector<U>>::value>
::type
220 std::vector<size_t> dim;
222 for (
auto &d: flat.
getDim())
232 template <
typename U>
238 for (hsize_t i = 0; i < x.size(); ++i)
240 write(s +
"_" + std::to_string(i), x[i]);
246 template <
typename U>
248 const H5NS::DataType &
type)
250 H5NS::Attribute attribute;
252 attribute =
group_.openAttribute(name);
253 attribute.read(
type, &x);
256 template <
typename U>
265 template <
typename U>
272 H5NS::DataSpace dataSpace;
273 std::vector<hsize_t> hdim;
278 dataSpace =
group_.openAttribute(s).getSpace();
282 dataSpace =
group_.openDataSet(s).getSpace();
284 hdim.resize(dataSpace.getSimpleExtentNdims());
285 dataSpace.getSimpleExtentDims(hdim.data());
297 H5NS::DataSet dataSet;
299 dataSet =
group_.openDataSet(s);
304 H5NS::Attribute attribute;
306 attribute =
group_.openAttribute(s);
311 template <
typename U>
312 typename std::enable_if<is_flattenable<std::vector<U>>::value>
::type
315 if (H5Lexists (
group_.getId(), s.c_str(), H5P_DEFAULT) > 0
325 std::vector<size_t> dim;
326 std::vector<Scalar> buf;
336 template <
typename U>
345 for (hsize_t i = 0; i < x.size(); ++i)
347 read(s +
"_" + std::to_string(i), x[i]);
#define HDF5_DEF_DATASET_THRES
GridLogger GridLogWarning(1, "Warning", GridLogColours, "YELLOW")
static INTERNAL_PRECISION U
const std::vector< size_t > & getDim(void) const
const std::vector< Scalar > & getFlatVector(void) const
unsigned int dataSetThres_
void readMultiDim(const std::string &s, std::vector< U > &buf, std::vector< size_t > &dim)
bool push(const std::string &s)
virtual ~Hdf5Reader(void)=default
void readSingleAttribute(U &x, const std::string &name, const H5NS::DataType &type)
std::enable_if<!is_flattenable< std::vector< U > >::value >::type readDefault(const std::string &s, std::vector< U > &x)
std::vector< std::string > path_
void readDefault(const std::string &s, U &output)
void readRagged(const std::string &s, std::vector< U > &x)
H5NS::Group & getGroup(void)
Hdf5Reader(const std::string &fileName, const bool readOnly=true)
const unsigned int dataSetThres_
H5NS::Group & getGroup(void)
void writeRagged(const std::string &s, const std::vector< U > &x)
void writeDefault(const std::string &s, const char *x)
std::enable_if<!is_flattenable< std::vector< U > >::value >::type writeDefault(const std::string &s, const std::vector< U > &x)
Hdf5Writer(const std::string &fileName)
std::vector< std::string > path_
void writeSingleAttribute(const U &x, const std::string &name, const H5NS::DataType &type)
void push(const std::string &s)
virtual ~Hdf5Writer(void)=default
void writeMultiDim(const std::string &s, const std::vector< size_t > &Dimensions, const U *pDataRowMajor, size_t NumElements)
std::enable_if< std::is_base_of< Serializable, U >::value, void >::type read(const std::string &s, U &output)
const V & getVector(void) const
std::enable_if< std::is_base_of< Serializable, U >::value >::type write(const std::string &s, const U &output)
bool isRegularShape(const T &t)