Skip to content

Commit

Permalink
Minor simplifications in array_interface documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Apr 24, 2024
1 parent 88d12d4 commit 96f63a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
22 changes: 7 additions & 15 deletions c++/h5/array_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,23 @@ namespace h5::array_interface {
// ...
// N. np_strides[N - N] = h5_strides[N - N] * parent_shape[N - 1] * parent_shape[N - 2] * ... * parent_shape[1]
//
// note that np_strides[N - i] = m * s[N - 1] * s[N - 2] * ... * s[N - i + 1], where 0 < m < s[N - i] and
// s is the true shape of the parent array
// Note: The shape of the parent array as well as the HDF5 strides that fulfill the above equations are not unique.
// This is not a problem as long as HDF5 manages to select the correct elements in memory.

// initialize h5_strides with the np_strides
// create the result vectors
v_t parent_shape(rank), h5_strides(rank);
for (int u = 0; u < rank; ++u) h5_strides[u] = np_strides[u];

// from the above equations it follows that parent_shape[0] (size of the slowest varying dimension) is arbitrary
parent_shape[0] = view_size;

// to solve the equations, we use the following algorithm:
// - Eq. 1 is already satisfied
// - parent_shape[N - i] = gcd(np_strides[N - i], np_strides[N - i - 1], ..., np_strides[0])
// - h5_strides[N - 1] = np_strides[N - 1] / parent_shape[N - 1]
// - divide equations i, i+1, ..., N by parent_shape[N - i]
// - repeat until all equations are satisfied
// that way we guarantee that the parent_shape[i] >= s[i] (see note above), which is important to make sure that
// the view elements are not out of bounds when HDF5 selects the hyperslab
// We initialize the h5_strides array to the values of np_strides
// and successively divide off the parent_shape values as they appear
// in the equations above.
for (int u = 0; u < rank; ++u) h5_strides[u] = np_strides[u];
for (int u = rank - 2; u >= 0; --u) {
// calculate the gcd
hsize_t gcd = h5_strides[u];
for (int v = u - 1; v >= 0; --v) { gcd = std::gcd(gcd, h5_strides[v]); }
// set partent_shape to the gcd
parent_shape[u + 1] = gcd;
// divide the remaining equations by the gcd
for (int v = u; v >= 0; --v) { h5_strides[v] /= gcd; }
}

Expand Down
3 changes: 0 additions & 3 deletions c++/h5/array_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,6 @@ namespace h5::array_interface {
* - `...`
* - `np_strides[N - N] = h5_strides[N - N] * parent_shape[N - 1] * parent_shape[N - 2] * ... * parent_shape[1]`.
*
* Note that `np_strides[N - i] = m * s[N - 1] * s[N - 2] * ... * s[N - i + 1]`, where `0 < m < s[N - i]` is an integer
* an `s` is the true shape of the parent array.
*
* @note The shape of the parent array as well as the HDF5 strides that fulfill the above equations are not unique.
* This is not a problem as long as HDF5 manages to select the correct elements in memory.
*
Expand Down

0 comments on commit 96f63a8

Please sign in to comment.