Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap all used H5D functions. #885

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/highfive/bits/H5Attribute_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "H5ReadWrite_misc.hpp"
#include "H5Utils.hpp"
#include "h5a_wrapper.hpp"
#include "h5d_wrapper.hpp"

namespace HighFive {

Expand Down Expand Up @@ -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
}
}
Expand Down
21 changes: 6 additions & 15 deletions include/highfive/bits/H5DataSet_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@
#include <sstream>
#include <string>

#include <H5Dpublic.h>
#include <H5Ppublic.h>

#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<DataSetException>("Unable to get DataSpace out of DataSet");
}
space._hid = detail::h5d_get_space(_hid);
return space;
}

Expand All @@ -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<DataSetException>("Cannot get offset of DataSet.");
}
return addr;
return static_cast<uint64_t>(detail::h5d_get_offset(_hid));
}

inline void DataSet::resize(const std::vector<size_t>& dims) {
Expand All @@ -58,10 +52,7 @@ inline void DataSet::resize(const std::vector<size_t>& dims) {
}

std::vector<hsize_t> real_dims(dims.begin(), dims.end());

if (H5Dset_extent(getId(), real_dims.data()) < 0) {
HDF5ErrMapper::ToException<DataSetException>("Could not resize dataset.");
}
detail::h5d_set_extent(getId(), real_dims.data());
}

} // namespace HighFive
31 changes: 10 additions & 21 deletions include/highfive/bits/H5Node_traits_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <vector>

#include <H5Apublic.h>
#include <H5Dpublic.h>
#include <H5Fpublic.h>
#include <H5Gpublic.h>
#include <H5Ppublic.h>
Expand All @@ -39,18 +38,13 @@ inline DataSet NodeTraits<Derivate>::createDataSet(const std::string& dataset_na
bool parents) {
LinkCreateProps lcpl;
lcpl.add(CreateIntermediateGroup(parents));
const auto hid = H5Dcreate2(static_cast<Derivate*>(this)->getId(),
dataset_name.c_str(),
dtype.getId(),
space.getId(),
lcpl.getId(),
createProps.getId(),
accessProps.getId());
if (hid < 0) {
HDF5ErrMapper::ToException<DataSetException>(
std::string("Unable to create the dataset \"") + dataset_name + "\":");
}
return DataSet(hid);
return DataSet(detail::h5d_create2(static_cast<Derivate*>(this)->getId(),
dataset_name.c_str(),
dtype.getId(),
space.getId(),
lcpl.getId(),
createProps.getId(),
accessProps.getId()));
}

template <typename Derivate>
Expand Down Expand Up @@ -119,14 +113,9 @@ inline DataSet NodeTraits<Derivate>::createDataSet(const std::string& dataset_na
template <typename Derivate>
inline DataSet NodeTraits<Derivate>::getDataSet(const std::string& dataset_name,
const DataSetAccessProps& accessProps) const {
const auto hid = H5Dopen2(static_cast<const Derivate*>(this)->getId(),
dataset_name.c_str(),
accessProps.getId());
if (hid < 0) {
HDF5ErrMapper::ToException<DataSetException>(std::string("Unable to open the dataset \"") +
dataset_name + "\":");
}
return DataSet(hid);
return DataSet(detail::h5d_open2(static_cast<const Derivate*>(this)->getId(),
dataset_name.c_str(),
accessProps.getId()));
}

template <typename Derivate>
Expand Down
35 changes: 17 additions & 18 deletions include/highfive/bits/H5Slice_traits_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <sstream>
#include <string>

#include <H5Dpublic.h>
#include "h5d_wrapper.hpp"
#include <H5Ppublic.h>

#include "H5ReadWrite_misc.hpp"
Expand Down Expand Up @@ -209,7 +209,10 @@ inline void SliceTraits<Derivate>::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
}
}
Expand All @@ -225,14 +228,12 @@ inline void SliceTraits<Derivate>::read(T* array,

const auto& slice = static_cast<const Derivate&>(*this);

if (H5Dread(details::get_dataset(slice).getId(),
mem_datatype.getId(),
details::get_memspace_id(slice),
slice.getSpace().getId(),
xfer_props.getId(),
static_cast<void*>(array)) < 0) {
HDF5ErrMapper::ToException<DataSetException>("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<void*>(array));
}

template <typename Derivate>
Expand Down Expand Up @@ -281,14 +282,12 @@ inline void SliceTraits<Derivate>::write_raw(const T* buffer,
const DataTransferProps& xfer_props) {
const auto& slice = static_cast<const Derivate&>(*this);

if (H5Dwrite(details::get_dataset(slice).getId(),
mem_datatype.getId(),
details::get_memspace_id(slice),
slice.getSpace().getId(),
xfer_props.getId(),
static_cast<const void*>(buffer)) < 0) {
HDF5ErrMapper::ToException<DataSetException>("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<const void*>(buffer));
}

template <typename Derivate>
Expand Down
125 changes: 125 additions & 0 deletions include/highfive/bits/h5d_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#pragma once

#include <H5Dpublic.h>
#include <H5Ipublic.h>

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");

Check warning on line 14 in include/highfive/bits/h5d_wrapper.hpp

View check run for this annotation

Codecov / codecov/patch

include/highfive/bits/h5d_wrapper.hpp#L14

Added line #L14 was not covered by tests
}

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<DataSetException>(
std::string("Unable to get dataspace of the dataset"));

Check warning on line 33 in include/highfive/bits/h5d_wrapper.hpp

View check run for this annotation

Codecov / codecov/patch

include/highfive/bits/h5d_wrapper.hpp#L32-L33

Added lines #L32 - L33 were not covered by tests
}

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<DataSetException>(
std::string("Unable to get datatype of the dataset"));

Check warning on line 43 in include/highfive/bits/h5d_wrapper.hpp

View check run for this annotation

Codecov / codecov/patch

include/highfive/bits/h5d_wrapper.hpp#L42-L43

Added lines #L42 - L43 were not covered by tests
}

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<DataSetException>(std::string("Unable to read the dataset"));

Check warning on line 57 in include/highfive/bits/h5d_wrapper.hpp

View check run for this annotation

Codecov / codecov/patch

include/highfive/bits/h5d_wrapper.hpp#L57

Added line #L57 was not covered by tests
}

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<DataSetException>(std::string("Unable to write the dataset"));

Check warning on line 71 in include/highfive/bits/h5d_wrapper.hpp

View check run for this annotation

Codecov / codecov/patch

include/highfive/bits/h5d_wrapper.hpp#L71

Added line #L71 was not covered by tests
}

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<DataSetException>("Cannot get offset of DataSet.");

Check warning on line 80 in include/highfive/bits/h5d_wrapper.hpp

View check run for this annotation

Codecov / codecov/patch

include/highfive/bits/h5d_wrapper.hpp#L80

Added line #L80 was not covered by tests
}
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<DataSetException>("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<DataSetException>(
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<DataSetException>(std::string("Unable to open the dataset \"") +
name + "\":");

Check warning on line 117 in include/highfive/bits/h5d_wrapper.hpp

View check run for this annotation

Codecov / codecov/patch

include/highfive/bits/h5d_wrapper.hpp#L116-L117

Added lines #L116 - L117 were not covered by tests
}

return dataset_id;
}


} // namespace detail
} // namespace HighFive