chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
matrix_product.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_MATRIX_PRODUCT_HPP
2#define CHIHAYA_MATRIX_PRODUCT_HPP
3
4#include "H5Cpp.h"
5#include "ritsuko/ritsuko.hpp"
6
7#include <string>
8
9#include "utils_public.hpp"
10#include "utils_misc.hpp"
11
17namespace chihaya {
18
23namespace matrix_product {
24
28namespace internal {
29
30inline std::pair<ArrayDetails, bool> fetch_seed(const H5::Group& handle, const std::string& target, const std::string& orientation, const ritsuko::Version& version, Options& options) {
31 // Checking the seed.
32 auto seed_details = internal_misc::load_seed_details(handle, target, version, options);
33 if (seed_details.dimensions.size() != 2) {
34 throw std::runtime_error("expected '" + target + "' to be a 2-dimensional array for a matrix product");
35 }
36 if (seed_details.type == STRING) {
37 throw std::runtime_error(std::string("type of '") + target + "' should be integer, float or boolean for a matrix product");
38 }
39
40 // Checking the orientation.
41 auto oristr = internal_misc::load_scalar_string_dataset(handle, orientation);
42 if (oristr != "N" && oristr != "T") {
43 throw std::runtime_error("'" + orientation + "' should be either 'N' or 'T' for a matrix product");
44 }
45
46 return std::pair<ArrayDetails, bool>(seed_details, oristr == "T");
47}
48
49}
62inline ArrayDetails validate(const H5::Group& handle, const ritsuko::Version& version, Options& options) {
63 auto left_details = internal::fetch_seed(handle, "left_seed", "left_orientation", version, options);
64 auto right_details = internal::fetch_seed(handle, "right_seed", "right_orientation", version, options);
65
66 ArrayDetails output;
67 output.dimensions.resize(2);
68 auto& nrow = output.dimensions[0];
69 auto& ncol = output.dimensions[1];
70 size_t common, common2;
71
72 if (left_details.second) {
73 nrow = left_details.first.dimensions[1];
74 common = left_details.first.dimensions[0];
75 } else {
76 nrow = left_details.first.dimensions[0];
77 common = left_details.first.dimensions[1];
78 }
79
80 if (right_details.second) {
81 ncol = right_details.first.dimensions[0];
82 common2 = right_details.first.dimensions[1];
83 } else {
84 ncol = right_details.first.dimensions[1];
85 common2 = right_details.first.dimensions[0];
86 }
87
88 if (!options.details_only) {
89 if (common != common2) {
90 throw std::runtime_error("inconsistent common dimensions (" + std::to_string(common) + " vs " + std::to_string(common2) + ")");
91 }
92 }
93
94 if (left_details.first.type == FLOAT || right_details.first.type == FLOAT) {
95 output.type = FLOAT;
96 } else {
97 output.type = INTEGER;
98 }
99
100 return output;
101}
102
103}
104
105}
106
107#endif
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: matrix_product.hpp:62
Namespace for all chihaya functions.
Definition: binary_arithmetic.hpp:22
Details about an array.
Definition: utils_public.hpp:36
std::vector< size_t > dimensions
Definition: utils_public.hpp:56
ArrayType type
Definition: utils_public.hpp:50
Validation options.
Definition: utils_public.hpp:66
bool details_only
Definition: utils_public.hpp:71
Various public utilities.