Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
blas_matrix.hpp
Go to the documentation of this file.
1#pragma once
2
12
13#include <utility>
14
15namespace uni20::linalg::blas
16{
17
22enum class MatrixTransform : unsigned
23{
24 normal = 0U,
25 transpose = 1U,
26 conjugate = 2U,
28};
29
30namespace detail
31{
32constexpr unsigned transform_mask = std::to_underlying(MatrixTransform::conjugate_transpose);
33constexpr unsigned transpose_bit = std::to_underlying(MatrixTransform::transpose);
34constexpr unsigned conjugate_bit = std::to_underlying(MatrixTransform::conjugate);
35
36constexpr void require_valid_transform(MatrixTransform transform)
37{
38 if (std::to_underlying(transform) > transform_mask)
39 {
40 PANIC("invalid MatrixTransform", std::to_underlying(transform));
41 }
42}
43} // namespace detail
44
46constexpr bool swaps_axes(MatrixTransform transform)
47{
48 return (std::to_underlying(transform) & detail::transpose_bit) != 0U;
49}
50
52constexpr bool conjugates_values(MatrixTransform transform)
53{
54 return (std::to_underlying(transform) & detail::conjugate_bit) != 0U;
55}
56
59{
60 return static_cast<MatrixTransform>((std::to_underlying(outer) ^ std::to_underlying(inner)) & detail::transform_mask);
61}
62
68
70template <uni20::BlasScalar Scalar> constexpr MatrixTransform canonical_transform_for_scalar(MatrixTransform transform)
71{
72 detail::require_valid_transform(transform);
73 if constexpr (uni20::is_complex_v<Scalar>)
74 {
75 return transform;
76 }
77 else
78 {
79 return static_cast<MatrixTransform>(std::to_underlying(transform) & detail::transpose_bit);
80 }
81}
82
84template <uni20::BlasScalar Scalar> constexpr bool blas_trans_char_is_supported(MatrixTransform transform)
85{
86 switch (canonical_transform_for_scalar<Scalar>(transform))
87 {
91 return true;
93 return false;
94 }
95 PANIC("invalid canonical MatrixTransform", std::to_underlying(transform));
96}
97
99template <uni20::BlasScalar Scalar> constexpr char blas_trans_char(MatrixTransform transform)
100{
101 switch (canonical_transform_for_scalar<Scalar>(transform))
102 {
104 return 'N';
106 return 'T';
108 return 'R';
110 return 'C';
111 }
112 PANIC("invalid canonical MatrixTransform", std::to_underlying(transform));
113}
114
118template <class Scalar, class Handle = Scalar*> struct BlasWritableMatrix
119{
120 Handle data{};
121 blas_int rows = 0;
122 blas_int cols = 0;
123 blas_int leading_dimension = 0;
124};
125
129template <class Scalar, class Handle = Scalar const*> struct BlasReadableMatrix
130{
131 Handle data{};
132 blas_int rows = 0;
133 blas_int cols = 0;
134 blas_int leading_dimension = 0;
136};
137
139constexpr blas_int transformed_rows(blas_int rows, blas_int cols, MatrixTransform transform)
140{
141 return swaps_axes(transform) ? cols : rows;
142}
143
145constexpr blas_int transformed_cols(blas_int rows, blas_int cols, MatrixTransform transform)
146{
147 return swaps_axes(transform) ? rows : cols;
148}
149
150} // namespace uni20::linalg::blas
Definition blas.hpp:18
constexpr blas_int transformed_cols(blas_int rows, blas_int cols, MatrixTransform transform)
Columns produced by applying transform to an untransformed matrix.
Definition blas_matrix.hpp:145
MatrixTransform
Logical matrix transform independent of a provider-specific opcode, although it happens to coincide w...
Definition blas_matrix.hpp:23
constexpr char blas_trans_char(MatrixTransform transform)
Lower a transform to the BLAS transpose character spelling used by selected providers.
Definition blas_matrix.hpp:99
constexpr blas_int transformed_rows(blas_int rows, blas_int cols, MatrixTransform transform)
Rows produced by applying transform to an untransformed matrix.
Definition blas_matrix.hpp:139
constexpr bool conjugates_values(MatrixTransform transform)
Return whether a matrix transform applies element-wise conjugation.
Definition blas_matrix.hpp:52
constexpr bool blas_trans_char_is_supported(MatrixTransform transform)
Return whether the current baseline provider path accepts a transform directly.
Definition blas_matrix.hpp:84
constexpr MatrixTransform canonical_transform_for_scalar(MatrixTransform transform)
Collapse conjugation-only distinctions for real scalar types.
Definition blas_matrix.hpp:70
constexpr MatrixTransform transpose_result_transform(MatrixTransform transform)
Transform induced on each GEMM operand by transposing the result.
Definition blas_matrix.hpp:64
constexpr bool swaps_axes(MatrixTransform transform)
Return whether a matrix transform swaps the logical axes.
Definition blas_matrix.hpp:46
constexpr MatrixTransform compose(MatrixTransform outer, MatrixTransform inner)
Compose two matrix transforms as outer(inner(A)).
Definition blas_matrix.hpp:58
Readable matrix shape plus transform as seen by a BLAS provider.
Definition blas_matrix.hpp:130
blas_int cols
Definition blas_matrix.hpp:133
MatrixTransform transform
Definition blas_matrix.hpp:135
blas_int leading_dimension
Definition blas_matrix.hpp:134
blas_int rows
Definition blas_matrix.hpp:132
Handle data
Definition blas_matrix.hpp:131
Writable matrix shape as seen by a BLAS/LAPACK provider.
Definition blas_matrix.hpp:119
blas_int rows
Definition blas_matrix.hpp:121
blas_int leading_dimension
Definition blas_matrix.hpp:123
Handle data
Definition blas_matrix.hpp:120
blas_int cols
Definition blas_matrix.hpp:122
#define PANIC(...)
Definition trace.hpp:382