From afc46234e8c695563623e936c6b58a0aa38ac9a2 Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Mon, 4 Dec 2023 11:19:12 +0100 Subject: [PATCH] Wrap all used H5D functions. --- include/highfive/bits/H5Attribute_misc.hpp | 3 +- include/highfive/bits/H5DataSet_misc.hpp | 21 +-- include/highfive/bits/H5Node_traits_misc.hpp | 31 ++--- include/highfive/bits/H5Slice_traits_misc.hpp | 35 +++-- include/highfive/bits/h5d_wrapper.hpp | 125 ++++++++++++++++++ 5 files changed, 160 insertions(+), 55 deletions(-) create mode 100644 include/highfive/bits/h5d_wrapper.hpp diff --git a/include/highfive/bits/H5Attribute_misc.hpp b/include/highfive/bits/H5Attribute_misc.hpp index 7be83cb01..6ec780433 100644 --- a/include/highfive/bits/H5Attribute_misc.hpp +++ b/include/highfive/bits/H5Attribute_misc.hpp @@ -21,6 +21,7 @@ #include "H5ReadWrite_misc.hpp" #include "H5Utils.hpp" #include "h5a_wrapper.hpp" +#include "h5d_wrapper.hpp" namespace HighFive { @@ -95,7 +96,7 @@ inline void Attribute::read(T& array) const { (void) detail::h5t_reclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.getPointer()); #else // This one is deprecated since 1.12.0 - (void) H5Dvlen_reclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.getPointer()); + (void) detail::h5d_vlen_reclaim(t.getId(), mem_space.getId(), H5P_DEFAULT, r.getPointer()); #endif } } diff --git a/include/highfive/bits/H5DataSet_misc.hpp b/include/highfive/bits/H5DataSet_misc.hpp index 4411b4c0d..4817fe001 100644 --- a/include/highfive/bits/H5DataSet_misc.hpp +++ b/include/highfive/bits/H5DataSet_misc.hpp @@ -14,26 +14,24 @@ #include #include -#include #include +#include "h5d_wrapper.hpp" #include "H5Utils.hpp" namespace HighFive { inline uint64_t DataSet::getStorageSize() const { - return H5Dget_storage_size(_hid); + return detail::h5d_get_storage_size(_hid); } inline DataType DataSet::getDataType() const { - return DataType(H5Dget_type(_hid)); + return DataType(detail::h5d_get_type(_hid)); } inline DataSpace DataSet::getSpace() const { DataSpace space; - if ((space._hid = H5Dget_space(_hid)) < 0) { - HDF5ErrMapper::ToException("Unable to get DataSpace out of DataSet"); - } + space._hid = detail::h5d_get_space(_hid); return space; } @@ -42,11 +40,7 @@ inline DataSpace DataSet::getMemSpace() const { } inline uint64_t DataSet::getOffset() const { - uint64_t addr = H5Dget_offset(_hid); - if (addr == HADDR_UNDEF) { - HDF5ErrMapper::ToException("Cannot get offset of DataSet."); - } - return addr; + return static_cast(detail::h5d_get_offset(_hid)); } inline void DataSet::resize(const std::vector& dims) { @@ -58,10 +52,7 @@ inline void DataSet::resize(const std::vector& dims) { } std::vector real_dims(dims.begin(), dims.end()); - - if (H5Dset_extent(getId(), real_dims.data()) < 0) { - HDF5ErrMapper::ToException("Could not resize dataset."); - } + detail::h5d_set_extent(getId(), real_dims.data()); } } // namespace HighFive diff --git a/include/highfive/bits/H5Node_traits_misc.hpp b/include/highfive/bits/H5Node_traits_misc.hpp index d7ab82b61..2cbda126a 100644 --- a/include/highfive/bits/H5Node_traits_misc.hpp +++ b/include/highfive/bits/H5Node_traits_misc.hpp @@ -12,7 +12,6 @@ #include #include -#include #include #include #include @@ -39,18 +38,13 @@ inline DataSet NodeTraits::createDataSet(const std::string& dataset_na bool parents) { LinkCreateProps lcpl; lcpl.add(CreateIntermediateGroup(parents)); - const auto hid = H5Dcreate2(static_cast(this)->getId(), - dataset_name.c_str(), - dtype.getId(), - space.getId(), - lcpl.getId(), - createProps.getId(), - accessProps.getId()); - if (hid < 0) { - HDF5ErrMapper::ToException( - std::string("Unable to create the dataset \"") + dataset_name + "\":"); - } - return DataSet(hid); + return DataSet(detail::h5d_create2(static_cast(this)->getId(), + dataset_name.c_str(), + dtype.getId(), + space.getId(), + lcpl.getId(), + createProps.getId(), + accessProps.getId())); } template @@ -119,14 +113,9 @@ inline DataSet NodeTraits::createDataSet(const std::string& dataset_na template inline DataSet NodeTraits::getDataSet(const std::string& dataset_name, const DataSetAccessProps& accessProps) const { - const auto hid = H5Dopen2(static_cast(this)->getId(), - dataset_name.c_str(), - accessProps.getId()); - if (hid < 0) { - HDF5ErrMapper::ToException(std::string("Unable to open the dataset \"") + - dataset_name + "\":"); - } - return DataSet(hid); + return DataSet(detail::h5d_open2(static_cast(this)->getId(), + dataset_name.c_str(), + accessProps.getId())); } template diff --git a/include/highfive/bits/H5Slice_traits_misc.hpp b/include/highfive/bits/H5Slice_traits_misc.hpp index 313343d4e..3569b5094 100644 --- a/include/highfive/bits/H5Slice_traits_misc.hpp +++ b/include/highfive/bits/H5Slice_traits_misc.hpp @@ -15,7 +15,7 @@ #include #include -#include +#include "h5d_wrapper.hpp" #include #include "H5ReadWrite_misc.hpp" @@ -209,7 +209,10 @@ inline void SliceTraits::read(T& array, const DataTransferProps& xfer_ detail::h5t_reclaim(t.getId(), mem_space.getId(), xfer_props.getId(), r.getPointer()); #else // This one is deprecated since 1.12.0 - (void) H5Dvlen_reclaim(t.getId(), mem_space.getId(), xfer_props.getId(), r.getPointer()); + (void) detail::h5d_vlen_reclaim(t.getId(), + mem_space.getId(), + xfer_props.getId(), + r.getPointer()); #endif } } @@ -225,14 +228,12 @@ inline void SliceTraits::read(T* array, const auto& slice = static_cast(*this); - if (H5Dread(details::get_dataset(slice).getId(), - mem_datatype.getId(), - details::get_memspace_id(slice), - slice.getSpace().getId(), - xfer_props.getId(), - static_cast(array)) < 0) { - HDF5ErrMapper::ToException("Error during HDF5 Read."); - } + detail::h5d_read(details::get_dataset(slice).getId(), + mem_datatype.getId(), + details::get_memspace_id(slice), + slice.getSpace().getId(), + xfer_props.getId(), + static_cast(array)); } template @@ -281,14 +282,12 @@ inline void SliceTraits::write_raw(const T* buffer, const DataTransferProps& xfer_props) { const auto& slice = static_cast(*this); - if (H5Dwrite(details::get_dataset(slice).getId(), - mem_datatype.getId(), - details::get_memspace_id(slice), - slice.getSpace().getId(), - xfer_props.getId(), - static_cast(buffer)) < 0) { - HDF5ErrMapper::ToException("Error during HDF5 Write: "); - } + detail::h5d_write(details::get_dataset(slice).getId(), + mem_datatype.getId(), + details::get_memspace_id(slice), + slice.getSpace().getId(), + xfer_props.getId(), + static_cast(buffer)); } template diff --git a/include/highfive/bits/h5d_wrapper.hpp b/include/highfive/bits/h5d_wrapper.hpp new file mode 100644 index 000000000..6de7fa5a3 --- /dev/null +++ b/include/highfive/bits/h5d_wrapper.hpp @@ -0,0 +1,125 @@ +#pragma once + +#include +#include + +namespace HighFive { +namespace detail { + + +#if !H5_VERSION_GE(1, 12, 0) +inline herr_t h5d_vlen_reclaim(hid_t type_id, hid_t space_id, hid_t dxpl_id, void* buf) { + herr_t err = H5Dvlen_reclaim(type_id, space_id, dxpl_id, buf); + if (err < 0) { + throw DataSetException("Failed to reclaim HDF5 internal memory"); + } + + return err; +} +#endif + +inline hsize_t h5d_get_storage_size(hid_t dset_id) { + // Docs: + // H5Dget_storage_size() does not differentiate between 0 (zero), the + // value returned for the storage size of a dataset with no stored values, + // and 0 (zero), the value returned to indicate an error. + return H5Dget_storage_size(dset_id); +} + +inline hid_t h5d_get_space(hid_t dset_id) { + hid_t dset = H5Dget_space(dset_id); + if (dset == H5I_INVALID_HID) { + HDF5ErrMapper::ToException( + std::string("Unable to get dataspace of the dataset")); + } + + return dset; +} + +inline hid_t h5d_get_type(hid_t dset_id) { + hid_t type_id = H5Dget_type(dset_id); + if (type_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException( + std::string("Unable to get datatype of the dataset")); + } + + return type_id; +} + +inline herr_t h5d_read(hid_t dset_id, + hid_t mem_type_id, + hid_t mem_space_id, + hid_t file_space_id, + hid_t dxpl_id, + void* buf) { + herr_t err = H5Dread(dset_id, mem_type_id, mem_space_id, file_space_id, dxpl_id, buf); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to read the dataset")); + } + + return err; +} + +inline herr_t h5d_write(hid_t dset_id, + hid_t mem_type_id, + hid_t mem_space_id, + hid_t file_space_id, + hid_t dxpl_id, + const void* buf) { + herr_t err = H5Dwrite(dset_id, mem_type_id, mem_space_id, file_space_id, dxpl_id, buf); + if (err < 0) { + HDF5ErrMapper::ToException(std::string("Unable to write the dataset")); + } + + return err; +} + +inline haddr_t h5d_get_offset(hid_t dset_id) { + uint64_t addr = H5Dget_offset(dset_id); + if (addr == HADDR_UNDEF) { + HDF5ErrMapper::ToException("Cannot get offset of DataSet."); + } + return addr; +} + + +inline herr_t h5d_set_extent(hid_t dset_id, const hsize_t size[]) { + herr_t err = H5Dset_extent(dset_id, size); + if (H5Dset_extent(dset_id, size) < 0) { + HDF5ErrMapper::ToException("Could not resize dataset."); + } + + return err; +} + +inline hid_t h5d_create2(hid_t loc_id, + const char* name, + hid_t type_id, + hid_t space_id, + hid_t lcpl_id, + hid_t dcpl_id, + hid_t dapl_id) { + hid_t dataset_id = H5Dcreate2(loc_id, name, type_id, space_id, lcpl_id, dcpl_id, dapl_id); + + if (dataset_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException( + std::string("Failed to create the dataset \"") + name + "\":"); + } + + return dataset_id; +} + +inline hid_t h5d_open2(hid_t loc_id, const char* name, hid_t dapl_id) { + hid_t dataset_id = H5Dopen2(loc_id, name, dapl_id); + + if (dataset_id == H5I_INVALID_HID) { + HDF5ErrMapper::ToException(std::string("Unable to open the dataset \"") + + name + "\":"); + } + + return dataset_id; +} + + +} // namespace detail +} // namespace HighFive