Skip to content

Commit

Permalink
Wrap all used H5S functions. (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
1uc authored Dec 7, 2023
1 parent be8ed40 commit dfa3801
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 49 deletions.
42 changes: 10 additions & 32 deletions include/highfive/bits/H5Dataspace_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "H5Utils.hpp"
#include "H5Converter_misc.hpp"
#include "h5s_wrapper.hpp"

namespace HighFive {

Expand All @@ -38,9 +39,7 @@ template <class IT, typename>
inline DataSpace::DataSpace(const IT begin, const IT end) {
std::vector<hsize_t> real_dims(begin, end);

if ((_hid = H5Screate_simple(int(real_dims.size()), real_dims.data(), NULL)) < 0) {
throw DataSpaceException("Impossible to create dataspace");
}
_hid = detail::h5s_create_simple(int(real_dims.size()), real_dims.data(), nullptr);
}

inline DataSpace::DataSpace(const std::vector<size_t>& dims, const std::vector<size_t>& maxdims) {
Expand All @@ -57,10 +56,8 @@ inline DataSpace::DataSpace(const std::vector<size_t>& dims, const std::vector<s
static_cast<hsize_t>(DataSpace::UNLIMITED),
H5S_UNLIMITED);

if ((_hid = H5Screate_simple(int(dims.size()), real_dims.data(), real_maxdims.data())) < 0) {
throw DataSpaceException("Impossible to create dataspace");
}
} // namespace HighFive
_hid = detail::h5s_create_simple(int(dims.size()), real_dims.data(), real_maxdims.data());
}

inline DataSpace::DataSpace(DataSpace::DataspaceType space_type) {
H5S_class_t h5_dataspace_type;
Expand All @@ -77,53 +74,34 @@ inline DataSpace::DataSpace(DataSpace::DataspaceType space_type) {
"dataspace_scalar or dataspace_null");
}

if ((_hid = H5Screate(h5_dataspace_type)) < 0) {
throw DataSpaceException("Unable to create dataspace");
}
_hid = detail::h5s_create(h5_dataspace_type);
}

inline DataSpace DataSpace::clone() const {
DataSpace res;
if ((res._hid = H5Scopy(_hid)) < 0) {
throw DataSpaceException("Unable to copy dataspace");
}
res._hid = detail::h5s_copy(_hid);
return res;
}

inline size_t DataSpace::getNumberDimensions() const {
const int ndim = H5Sget_simple_extent_ndims(_hid);
if (ndim < 0) {
HDF5ErrMapper::ToException<DataSetException>(
"Unable to get dataspace number of dimensions");
}
return size_t(ndim);
return static_cast<size_t>(detail::h5s_get_simple_extent_ndims(_hid));
}

inline std::vector<size_t> DataSpace::getDimensions() const {
std::vector<hsize_t> dims(getNumberDimensions());
if (!dims.empty()) {
if (H5Sget_simple_extent_dims(_hid, dims.data(), NULL) < 0) {
HDF5ErrMapper::ToException<DataSetException>("Unable to get dataspace dimensions");
}
detail::h5s_get_simple_extent_dims(_hid, dims.data(), nullptr);
}
return details::to_vector_size_t(std::move(dims));
}

inline size_t DataSpace::getElementCount() const {
hssize_t nelements = H5Sget_simple_extent_npoints(_hid);
if (nelements < 0) {
HDF5ErrMapper::ToException<DataSetException>(
"Unable to get number of elements in dataspace");
}

return static_cast<size_t>(nelements);
return static_cast<size_t>(detail::h5s_get_simple_extent_npoints(_hid));
}

inline std::vector<size_t> DataSpace::getMaxDimensions() const {
std::vector<hsize_t> maxdims(getNumberDimensions());
if (H5Sget_simple_extent_dims(_hid, NULL, maxdims.data()) < 0) {
HDF5ErrMapper::ToException<DataSetException>("Unable to get dataspace dimensions");
}
detail::h5s_get_simple_extent_dims(_hid, nullptr, maxdims.data());

std::replace(maxdims.begin(),
maxdims.end(),
Expand Down
20 changes: 8 additions & 12 deletions include/highfive/bits/H5Slice_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "H5Utils.hpp"

#include "../H5PropertyList.hpp"
#include "h5s_wrapper.hpp"

namespace HighFive {

Expand Down Expand Up @@ -174,19 +175,14 @@ class HyperSlab {
auto space = space_.clone();
for (const auto& sel: selects) {
if (sel.op == Op::None) {
H5Sselect_none(space.getId());
detail::h5s_select_none(space.getId());
} else {
auto error_code =
H5Sselect_hyperslab(space.getId(),
convert(sel.op),
sel.offset.empty() ? nullptr : sel.offset.data(),
sel.stride.empty() ? nullptr : sel.stride.data(),
sel.count.empty() ? nullptr : sel.count.data(),
sel.block.empty() ? nullptr : sel.block.data());

if (error_code < 0) {
HDF5ErrMapper::ToException<DataSpaceException>("Unable to select hyperslab");
}
detail::h5s_select_hyperslab(space.getId(),
convert(sel.op),
sel.offset.empty() ? nullptr : sel.offset.data(),
sel.stride.empty() ? nullptr : sel.stride.data(),
sel.count.empty() ? nullptr : sel.count.data(),
sel.block.empty() ? nullptr : sel.block.data());
}
}
return space;
Expand Down
8 changes: 3 additions & 5 deletions include/highfive/bits/H5Slice_traits_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <string>

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

#include "H5ReadWrite_misc.hpp"
#include "H5Converter_misc.hpp"
Expand Down Expand Up @@ -84,7 +84,7 @@ inline Selection SliceTraits<Derivate>::select(const HyperSlab& hyper_slab) cons
auto filespace = slice.getSpace();
filespace = hyper_slab.apply(filespace);

auto n_elements = H5Sget_select_npoints(filespace.getId());
auto n_elements = detail::h5s_get_select_npoints(filespace.getId());
auto memspace = DataSpace(std::array<size_t, 1>{size_t(n_elements)});

return detail::make_selection(memspace, filespace, details::get_dataset(slice));
Expand Down Expand Up @@ -149,9 +149,7 @@ inline Selection SliceTraits<Derivate>::select(const ElementSet& elements) const
data = raw_elements.data();
}

if (H5Sselect_elements(space.getId(), H5S_SELECT_SET, num_elements, data) < 0) {
HDF5ErrMapper::ToException<DataSpaceException>("Unable to select elements");
}
detail::h5s_select_elements(space.getId(), H5S_SELECT_SET, num_elements, data);

return detail::make_selection(DataSpace(num_elements), space, details::get_dataset(slice));
}
Expand Down
106 changes: 106 additions & 0 deletions include/highfive/bits/h5s_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#pragma once

#include <H5Spublic.h>
namespace HighFive {
namespace detail {

inline hid_t h5s_create_simple(int rank, const hsize_t dims[], const hsize_t maxdims[]) {
hid_t space_id = H5Screate_simple(rank, dims, maxdims);
if (space_id == H5I_INVALID_HID) {
throw DataSpaceException("Unable to create simple dataspace");
}

return space_id;
}

inline hid_t h5s_create(H5S_class_t type) {
hid_t space_id = H5Screate(type);

if (space_id == H5I_INVALID_HID) {
throw DataSpaceException("Unable to create dataspace");
}

return space_id;
}

inline hid_t h5s_copy(hid_t space_id) {
hid_t copy_id = H5Scopy(space_id);

if (copy_id < 0) {
throw DataSpaceException("Unable to copy dataspace");
}

return copy_id;
}

inline herr_t h5s_select_none(hid_t spaceid) {
herr_t err = H5Sselect_none(spaceid);
if (err < 0) {
HDF5ErrMapper::ToException<DataSpaceException>("Unable to select None space");
}
return err;
}

inline herr_t h5s_select_hyperslab(hid_t space_id,
H5S_seloper_t op,
const hsize_t start[],
const hsize_t stride[],
const hsize_t count[],
const hsize_t block[]) {
herr_t err = H5Sselect_hyperslab(space_id, op, start, stride, count, block);
if (err < 0) {
HDF5ErrMapper::ToException<DataSpaceException>("Unable to select hyperslab");
}
return err;
}

inline hssize_t h5s_get_select_npoints(hid_t spaceid) {
hssize_t n_points = H5Sget_select_npoints(spaceid);
if (n_points < 0) {
HDF5ErrMapper::ToException<DataSpaceException>(
"Unable to get number of points in selection");
}
return n_points;
}

inline herr_t h5s_select_elements(hid_t space_id,
H5S_seloper_t op,
size_t num_elem,
const hsize_t* coord) {
herr_t err = H5Sselect_elements(space_id, op, num_elem, coord);
if (err < 0) {
HDF5ErrMapper::ToException<DataSpaceException>("Unable to select elements");
}
return err;
}

inline int h5s_get_simple_extent_ndims(hid_t space_id) {
int ndim = H5Sget_simple_extent_ndims(space_id);
if (ndim < 0) {
HDF5ErrMapper::ToException<DataSetException>(
"Unable to get number of dimensions of dataspace");
}
return ndim;
}

inline herr_t h5s_get_simple_extent_dims(hid_t space_id, hsize_t dims[], hsize_t maxdims[]) {
herr_t err = H5Sget_simple_extent_dims(space_id, dims, maxdims);
if (err < 0) {
HDF5ErrMapper::ToException<DataSetException>("Unable to get dimensions of dataspace");
}
return err;
}

inline hssize_t h5s_get_simple_extent_npoints(hid_t space_id) {
hssize_t nelements = H5Sget_simple_extent_npoints(space_id);
if (nelements < 0) {
HDF5ErrMapper::ToException<DataSetException>(
"Unable to get number of elements in dataspace");
}

return nelements;
}


} // namespace detail
} // namespace HighFive

0 comments on commit dfa3801

Please sign in to comment.