17 #ifndef QMCPLUSPLUS_HDF_WRAPPER_FUNCTIONS_H 18 #define QMCPLUSPLUS_HDF_WRAPPER_FUNCTIONS_H 44 template<
typename T,
typename IT>
45 inline bool getDataShape(hid_t grp,
const std::string& aname, std::vector<IT>& sizes_out)
50 hid_t h1 = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
51 hid_t dataspace = H5Dget_space(h1);
52 int rank = H5Sget_simple_extent_ndims(dataspace);
55 if (h1 >= 0 && dataspace >= 0 &&
rank >= 0)
59 throw std::runtime_error(aname +
" dataset is too small for the requested data type");
62 std::vector<hsize_t> sizes_in(
rank);
63 int status_n = H5Sget_simple_extent_dims(dataspace, sizes_in.data(), NULL);
66 int user_rank =
rank - TSpaceType::added_rank();
67 bool size_match =
true;
68 for (
int dim = user_rank, dim_type = 0; dim <
rank; dim++, dim_type++)
69 if (sizes_in[dim] != TSpace.dims[dim_type])
72 throw std::runtime_error(
"The lower dimensions (container element type) of " + aname +
73 " dataset do not match the requested data type");
77 sizes_out.resize(user_rank);
78 for (
int dim = 0; dim < user_rank; dim++)
79 sizes_out[dim] = static_cast<IT>(sizes_in[dim]);
102 std::vector<hsize_t> dims_in;
103 if (getDataShape<T>(grp, aname, dims_in))
105 const int user_rank =
rank - TSpaceType::added_rank();
106 if (dims_in.size() != user_rank)
107 throw std::runtime_error(aname +
" dataspace rank does not match\n");
110 for (
int i = 0; i < user_rank; ++i)
113 dims[i] = dims_in[i];
123 inline bool h5d_read(hid_t grp,
const std::string& aname, T* first, hid_t xfer_plist)
127 hid_t h1 = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
131 herr_t ret = H5Dread(h1, h5d_type_id, H5S_ALL, H5S_ALL, xfer_plist, first);
140 hid_t h1 = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
155 hid_t h1 = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
158 hid_t datatype = H5Dget_type(h1);
159 if (datatype == H5I_INVALID_HID)
160 throw std::runtime_error(aname +
" dataset either does not exist or there was an error determining its type.");
161 htri_t equality_check = H5Tequal(datatype, h5d_type_id);
163 switch (equality_check)
170 throw std::runtime_error(
"Type comparison attempted with an invalid type or nonexistent dataset " + aname);
176 const std::string& aname,
185 hid_t h1 = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
189 hid_t dataspace = H5Screate_simple(ndims,
dims, NULL);
190 hid_t dataset = H5Dcreate(grp, aname.c_str(), h5d_type_id, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
191 ret = H5Dwrite(dataset, h5d_type_id, H5S_ALL, H5S_ALL, xfer_plist, first);
197 ret = H5Dwrite(h1, h5d_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, first);
211 const std::string& aname,
213 const hsize_t* gcounts,
214 const hsize_t* counts,
215 const hsize_t* offsets,
221 hid_t h1 = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
226 hid_t dataspace = H5Dget_space(h1);
227 hid_t memspace = H5Screate_simple(ndims, counts, NULL);
237 const std::vector<hsize_t> ones(ndims, 1);
238 herr_t ret = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offsets, NULL, ones.data(), counts);
241 ret = H5Dread(h1, h5d_type_id, memspace, dataspace, xfer_plist, first);
253 const std::string& aname,
255 const hsize_t* gcounts,
256 const hsize_t* counts,
257 const hsize_t* offsets,
264 hid_t h1 = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
265 hid_t filespace, memspace;
268 const std::vector<hsize_t> ones(ndims, 1);
271 hid_t dataspace = H5Screate_simple(ndims, gcounts, NULL);
272 hid_t dataset = H5Dcreate(grp, aname.c_str(), h5d_type_id, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
274 hid_t filespace = H5Dget_space(dataset);
275 ret = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsets, NULL, ones.data(), counts);
277 hid_t memspace = H5Screate_simple(ndims, counts, NULL);
278 ret = H5Dwrite(dataset, h5d_type_id, memspace, filespace, xfer_plist, first);
287 filespace = H5Dget_space(h1);
288 ret = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsets, NULL, ones.data(), counts);
290 memspace = H5Screate_simple(ndims, counts, NULL);
291 ret = H5Dwrite(h1, h5d_type_id, memspace, filespace, xfer_plist, first);
303 const std::string& aname,
305 const hsize_t* gcounts,
306 const hsize_t* counts,
307 const hsize_t* offsets,
309 const hsize_t* mem_gcounts,
310 const hsize_t* mem_counts,
311 const hsize_t* mem_offsets,
317 hid_t h1 = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
321 hid_t dataspace = H5Dget_space(h1);
322 if (ndims != H5Sget_simple_extent_ndims(dataspace))
323 throw std::runtime_error(aname +
" dataspace does not match ");
325 const std::vector<hsize_t> ones(std::max(ndims, mem_ndims), 1);
326 herr_t ret = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offsets, NULL, ones.data(), counts);
328 hid_t memspace = H5Screate_simple(mem_ndims, mem_gcounts, NULL);
329 herr_t mem_ret = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, mem_offsets, NULL, ones.data(), mem_counts);
332 ret = H5Dread(h1, h5d_type_id, memspace, dataspace, xfer_plist, first);
343 const std::string& aname,
345 const hsize_t* gcounts,
346 const hsize_t* counts,
347 const hsize_t* offsets,
349 const hsize_t* mem_gcounts,
350 const hsize_t* mem_counts,
351 const hsize_t* mem_offsets,
357 std::cout <<
" h5d_write: " << mem_ndims <<
" " << *mem_gcounts <<
" " << *(mem_gcounts + 1) <<
" " 358 << *(mem_gcounts + 2) <<
" " << *mem_counts <<
" " << *(mem_counts + 1) <<
" " << *(mem_counts + 2) <<
" " 359 << *mem_offsets <<
" " << *(mem_offsets + 1) <<
" " << *(mem_offsets + 2) <<
" " << std::endl;
361 hid_t h1 = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
364 const std::vector<hsize_t> ones(std::max(ndims, mem_ndims), 1);
367 hid_t dataspace = H5Screate_simple(ndims, gcounts, NULL);
368 hid_t dataset = H5Dcreate(grp, aname.c_str(), h5d_type_id, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
370 hid_t filespace = H5Dget_space(dataset);
371 ret = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsets, NULL, ones.data(), counts);
373 hid_t memspace = H5Screate_simple(mem_ndims, mem_gcounts, NULL);
374 ret = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, mem_offsets, NULL, ones.data(), mem_counts);
375 ret = H5Dwrite(dataset, h5d_type_id, memspace, filespace, xfer_plist, first);
384 hid_t filespace = H5Dget_space(h1);
385 ret = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offsets, NULL, ones.data(), counts);
387 hid_t memspace = H5Screate_simple(mem_ndims, mem_gcounts, NULL);
388 ret = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, mem_offsets, NULL, ones.data(), mem_counts);
389 ret = H5Dwrite(h1, h5d_type_id, memspace, filespace, xfer_plist, first);
400 const std::string& aname,
403 const hsize_t*
const dims,
404 const T*
const first,
405 hsize_t chunk_size = 1,
406 hid_t xfer_plist = H5P_DEFAULT)
414 hid_t dataset = H5Dopen(grp, aname.c_str(), H5P_DEFAULT);
415 std::vector<hsize_t> max_dims(ndims);
416 max_dims[0] = H5S_UNLIMITED;
417 for (
int d = 1; d < ndims; ++d)
418 max_dims[d] =
dims[d];
425 std::vector<hsize_t> chunk_dims(ndims);
426 chunk_dims[0] = chunk_size;
427 for (
int d = 1; d < ndims; ++d)
428 chunk_dims[d] =
dims[d];
430 dataspace = H5Screate_simple(ndims,
dims, max_dims.data());
432 hid_t p = H5Pcreate(H5P_DATASET_CREATE);
434 hid_t sl = H5Pset_layout(p, H5D_CHUNKED);
436 hid_t cs = H5Pset_chunk(p, ndims, chunk_dims.data());
438 dataset = H5Dcreate(grp, aname.c_str(), h5d_type_id, dataspace, H5P_DEFAULT, p, H5P_DEFAULT);
440 memspace = H5Screate_simple(ndims,
dims, NULL);
442 ret = H5Dwrite(dataset, h5d_type_id, memspace, dataspace, xfer_plist, first);
469 std::vector<hsize_t> start(ndims);
470 std::vector<hsize_t> end(ndims);
471 for (
int d = 1; d < ndims; ++d)
477 end[0] = start[0] +
dims[0];
479 herr_t he = H5Dset_extent(dataset, end.data());
481 dataspace = H5Dget_space(dataset);
483 herr_t hse = H5Sset_extent_simple(dataspace, ndims, end.data(), max_dims.data());
485 const std::vector<hsize_t> ones(ndims, 1);
486 herr_t hsh = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start.data(), NULL, ones.data(),
dims);
488 memspace = H5Screate_simple(ndims,
dims, NULL);
490 ret = H5Dwrite(dataset, h5d_type_id, memspace, dataspace, H5P_DEFAULT, first);
define h5_space_type to handle basic datatype for hdf5
bool h5d_append(hid_t grp, const std::string &aname, hsize_t ¤t, hsize_t ndims, const hsize_t *const dims, const T *const first, hsize_t chunk_size=1, hid_t xfer_plist=H5P_DEFAULT)
helper functions for EinsplineSetBuilder
bool getDataShape(hid_t grp, const std::string &aname, std::vector< IT > &sizes_out)
free template function to read the (user) dimension and shape of the dataset.
bool is_same(const xmlChar *a, const char *b)
bool h5d_check_existence(hid_t grp, const std::string &aname)
bool h5d_check_type(hid_t grp, const std::string &aname)
bool h5d_read(hid_t grp, const std::string &aname, T *first, hid_t xfer_plist)
return true, if successful
hid_t get_h5_datatype(const T &)
map C types to hdf5 native types bool is explicit removed due to the fact that it is implementation-d...
bool checkShapeConsistency(hid_t grp, const std::string &aname, int rank, hsize_t *dims)
free function to check dimension
bool h5d_write(hid_t grp, const std::string &aname, hsize_t ndims, const hsize_t *dims, const T *first, hid_t xfer_plist)