Skip to content

Commit

Permalink
Simplify logic in sliced h5_write implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Mar 1, 2024
1 parent 6a9bbc9 commit 68cb8e8
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions c++/nda/h5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,31 +226,24 @@ namespace nda {

constexpr int size_of_slice = sizeof...(IRs);
static_assert(size_of_slice > 0, "Invalid empty slice provided in h5_write call");
static_assert(not std::is_same_v<typename A::value_type, std::string>, "Slicing not supported in h5_write for array of string");

if constexpr (is_scalar_v<typename A::value_type>) {

static constexpr bool is_complex = is_complex_v<typename A::value_type>;

// Dataset must already exist for the sliced h5_write
auto lt = h5::array_interface::get_h5_lengths_type(g, name);
if (is_complex != lt.has_complex_attribute)
NDA_RUNTIME_ERROR << "Error in sliced h5_write. Existing dataset and array must both be either complex or real";
auto const [sl, sh] = hyperslab_and_shape_from_slice<A::rank>(slice, lt.lengths, is_complex);
if (sh != a.shape())
NDA_RUNTIME_ERROR << "Error in sliced h5_write. Shape of slice and Array shape incompatible"
<< "\n shape of slice : " << sh << "\n array : " << a.shape();

auto rank_in_file = lt.rank() - is_complex;
h5::array_interface::h5_array_view v{h5::hdf5_type<get_value_t<A>>(), (void *)(a.data()), rank_in_file, is_complex};
v.slab.count = sl.count;
v.L_tot = sl.count;

h5::array_interface::write_slice(g, name, v, lt, sl);

} else { // generic unknown type to hdf5
static_assert(false, "Slicing not supported in generic h5_write");
}
static_assert(is_scalar_v<typename A::value_type>, "Slicing in h5_write is only supported for scalar types");
static constexpr bool is_complex = is_complex_v<typename A::value_type>;

// Dataset must already exist for the sliced h5_write
auto lt = h5::array_interface::get_h5_lengths_type(g, name);
if (is_complex != lt.has_complex_attribute)
NDA_RUNTIME_ERROR << "Error in sliced h5_write. Existing dataset and array must both be either complex or real";
auto const [sl, sh] = hyperslab_and_shape_from_slice<A::rank>(slice, lt.lengths, is_complex);
if (sh != a.shape())
NDA_RUNTIME_ERROR << "Error in sliced h5_write. Shape of slice and Array shape incompatible"
<< "\n shape of slice : " << sh << "\n array : " << a.shape();

auto rank_in_file = lt.rank() - is_complex;
h5::array_interface::h5_array_view v{h5::hdf5_type<get_value_t<A>>(), (void *)(a.data()), rank_in_file, is_complex};
v.slab.count = sl.count;
v.L_tot = sl.count;

h5::array_interface::write_slice(g, name, v, lt, sl);
}

template <MemoryArray A, typename... IRs>
Expand Down

0 comments on commit 68cb8e8

Please sign in to comment.