chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
transpose.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_TRANSPOSE_HPP
2#define CHIHAYA_TRANSPOSE_HPP
3
4#include "H5Cpp.h"
5#include "ritsuko/ritsuko.hpp"
6#include "ritsuko/hdf5/hdf5.hpp"
7
8#include <stdexcept>
9#include <algorithm>
10#include <vector>
11#include <cstdint>
12
13#include "utils_misc.hpp"
14
20namespace chihaya {
21
26namespace transpose {
27
31namespace internal {
32
33template<typename Perm_>
34std::vector<size_t> check_permutation(const H5::DataSet& phandle, size_t ndims, const H5::PredType& h5type, const std::vector<size_t>& input_dimensions, bool details_only) {
35 if (ndims != input_dimensions.size()) {
36 throw std::runtime_error("length of 'permutation' should match dimensionality of 'seed'");
37 }
38
39 std::vector<Perm_> permutation(ndims);
40 phandle.read(permutation.data(), h5type);
41
42 std::vector<size_t> new_dimensions(ndims);
43 for (size_t p = 0; p < ndims; ++p) {
44 auto current = permutation[p];
45 if (current < 0) {
46 throw std::runtime_error("'permutation' should contain non-negative indices");
47 }
48 if (static_cast<size_t>(current) >= ndims) {
49 throw std::runtime_error("'permutation' contains out-of-bounds indices");
50 }
51 new_dimensions[p] = input_dimensions[permutation[p]];
52 }
53
54 if (!details_only) {
55 std::sort(permutation.begin(), permutation.end());
56 for (size_t p = 0; p < permutation.size(); ++p) {
57 if (p != static_cast<size_t>(permutation[p])) {
58 throw std::runtime_error("indices in 'permutation' should be unique for a transpose operation");
59 }
60 }
61 }
62
63 return new_dimensions;
64}
65
66}
79inline ArrayDetails validate(const H5::Group& handle, const ritsuko::Version& version, Options& options) {
80 auto seed_details = internal_misc::load_seed_details(handle, "seed", version, options);
81
82 auto phandle = ritsuko::hdf5::open_dataset(handle, "permutation");
83 auto ndims = ritsuko::hdf5::get_1d_length(phandle, false);
84
85 if (version.lt(1, 1, 0)) {
86 if (phandle.getTypeClass() != H5T_INTEGER) {
87 throw std::runtime_error("'permutation' should be integer");
88 }
89 seed_details.dimensions = internal::check_permutation<int>(phandle, ndims, H5::PredType::NATIVE_INT, seed_details.dimensions, options.details_only);
90 } else {
91 if (ritsuko::hdf5::exceeds_integer_limit(phandle, 64, false)) {
92 throw std::runtime_error("'permutation' should have a datatype that can be represented by a 64-bit unsigned integer");
93 }
94 seed_details.dimensions = internal::check_permutation<uint64_t>(phandle, ndims, H5::PredType::NATIVE_UINT64, seed_details.dimensions, options.details_only);
95 }
96
97 return seed_details;
98}
99
100}
101
102}
103
104#endif
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: transpose.hpp:79
Namespace for all chihaya functions.
Definition: binary_arithmetic.hpp:22
Details about an array.
Definition: utils_public.hpp:36
Validation options.
Definition: utils_public.hpp:66
bool details_only
Definition: utils_public.hpp:71