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
7#include <stdexcept>
8#include <exception>
9#include <vector>
10#include <cstdint>
11#include <cstddef>
12
13#include "utils_public.hpp"
14#include "utils_misc.hpp"
15#include "utils_type.hpp"
16
22namespace chihaya {
23
27template<typename Perm_>
28std::vector<std::size_t> check_permutation(const H5::DataSet& phandle, hsize_t plen, const std::vector<std::size_t>& input_dimensions, bool details_only) {
29 auto permutation = sanisizer::create<std::vector<Perm_> >(plen);
30 phandle.read(permutation.data(), ritsuko::hdf5::as_numeric_datatype<Perm_>());
31
32 auto new_dimensions = sanisizer::create<std::vector<std::size_t> >(plen);
33 if (!details_only) {
34 if (!sanisizer::is_equal(plen, input_dimensions.size())) {
35 throw std::runtime_error("length should match dimensionality of 'seed'");
36 }
37
38 auto present = sanisizer::create<std::vector<char> >(plen);
39 for (I<decltype(plen)> p = 0; p < plen; ++p) {
40 const auto current = permutation[p];
41 if (current < 0) {
42 throw std::runtime_error("expected non-negative indices");
43 }
44 if (sanisizer::is_greater_than_or_equal(current, plen)) {
45 throw std::runtime_error("detected out-of-bounds indices");
46 }
47 new_dimensions[p] = input_dimensions[current];
48 if (present[current]) {
49 throw std::runtime_error("indices should be unique");
50 }
51 present[current] = 1;
52 }
53
54 } else {
55 for (I<decltype(plen)> p = 0; p < plen; ++p) {
56 new_dimensions[p] = input_dimensions[permutation[p]];
57 }
58 }
59
60 return new_dimensions;
61}
78inline ArrayDetails validate_transpose(const H5::Group& group, const ritsuko::Version& version, const Options& options) {
79 ArrayDetails seed_details;
80 try {
81 auto shandle = group.openGroup("seed");
82 seed_details = validate(shandle, version, options);
83 } catch (...) {
84 std::throw_with_nested(std::runtime_error("failed to validate 'seed'"));
85 }
86
87 try {
88 auto phandle = group.openDataSet("permutation");
89 auto pspace = phandle.getSpace();
90 if (pspace.getSimpleExtentNdims() != 1) {
91 throw std::runtime_error("expected a 1-dimensional dataset");
92 }
93 hsize_t plen;
94 pspace.getSimpleExtentDims(&plen);
95
96 if (version.lt(1, 1, 0)) {
97 // Older versions didn't actually specify the integer type, so we just check we can load it into an 'int' of any type.
98 if (!ritsuko::hdf5::exceeds_integer_limit(phandle, 64, true)) {
99 seed_details.dimensions = check_permutation<std::int64_t>(phandle, plen, seed_details.dimensions, options.details_only);
100 } else if (!ritsuko::hdf5::exceeds_integer_limit(phandle, 64, false)) {
101 seed_details.dimensions = check_permutation<std::uint64_t>(phandle, plen, seed_details.dimensions, options.details_only);
102 } else {
103 throw create_integer_error_0_99(phandle.getTypeClass());
104 }
105 } else {
106 if (ritsuko::hdf5::exceeds_integer_limit(phandle, 64, false)) {
107 throw std::runtime_error("datatype should fit in a 64-bit unsigned integer");
108 }
109 seed_details.dimensions = check_permutation<std::uint64_t>(phandle, plen, seed_details.dimensions, options.details_only);
110 }
111 } catch (...) {
112 std::throw_with_nested(std::runtime_error("failed to validate 'permutation'"));
113 }
114
115 return seed_details;
116}
117
118}
119
120#endif
Namespace for all chihaya functions.
Definition binary_arithmetic.hpp:21
ArrayDetails validate_transpose(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition transpose.hpp:78
ArrayDetails validate(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition validate.hpp:96
Container_ create(Value_ x, Args_ &&... args)
constexpr bool is_equal(Left_ left, Right_ right)
constexpr bool is_greater_than_or_equal(Left_ left, Right_ right)
Details about an array.
Definition utils_public.hpp:38
std::vector< std::size_t > dimensions
Definition utils_public.hpp:58
Options for validate().
Definition utils_public.hpp:79
bool details_only
Definition utils_public.hpp:84
Various public utilities.