extra.hh
Go to the documentation of this file.
1#pragma once
2#include <batchlas/export.hh>
8
9/// @file
10/// @brief Matrix norms, condition numbers, conditioned test-matrix generators and transpose.
11/// @ingroup api_extra
12
13namespace batchlas
14{
15
16 /// @addtogroup api_extra
17 /// @{
18
19 /// @brief Per-item matrix norm, written into a caller-owned span.
20 ///
21 /// For each batch item \f$A_i\f$, `norms[i]` receives \f$\|A_i\|\f$ in the
22 /// requested norm: Frobenius, One (max column sum), Inf (max row sum), Max
23 /// (max absolute entry) or Spectral (largest \f$|\lambda|\f$).
24 /// @tparam T float, double, std::complex<float> or std::complex<double>
25 /// @tparam MF matrix format; only MatrixFormat::Dense is instantiated
26 /// @param ctx queue the kernels are enqueued on
27 /// @param A batch of matrices; not modified
28 /// @param norm_type which norm
29 /// @param norms device-accessible output, at least `A.batch_size()` elements
30 /// @return event of the last enqueued kernel; `norms` is valid after it (Spectral
31 /// waits before returning, so `norms` is valid on return)
32 /// @pre NormType::Spectral requires square symmetric/Hermitian `A` (the lower
33 /// triangle is read).
34 /// @throws batchlas::invalid_argument for Spectral on a non-square `A`
35 /// @throws batchlas::unsupported for Spectral on a non-dense format or when no
36 /// backend with a vendor `syev` is compiled in
37 /// @throws batchlas::NoRouteError for Spectral when the chosen backend's solver
38 /// library is not compiled in (the vendor `syev` is called directly;
39 /// Backend::SYCL calls the native `syev` instead)
40 /// @throws batchlas::unsupported for double or complex<double> on a GPU without fp64
41 template <typename T, MatrixFormat MF>
42 BATCHLAS_API Event norm(Queue &ctx,
43 const MatrixView<T, MF> &A,
44 const NormType norm_type,
45 const Span<float_t<T>> norms);
46
47 /// @brief Per-item matrix norm, returned in a new vector. Blocks until done.
48 ///
49 /// Same contract as the span overload, but allocates the result and waits on it
50 /// before returning.
51 /// @return one real norm per batch item
52 template <typename T, MatrixFormat MF>
54 const MatrixView<T, MF> &A,
55 const NormType norm_type = NormType::Frobenius);
56
57 /// @brief Per-item condition number, written into a caller-owned span.
58 ///
59 /// For norm types other than Spectral, `conds[i]` = \f$\|A_i\|\,\|A_i^{-1}\|\f$,
60 /// computed by explicit inversion. For NormType::Spectral, the ratio of the
61 /// largest to the smallest eigenvalue magnitude of a symmetric/Hermitian \f$A_i\f$
62 /// (lower triangle read; infinity when an eigenvalue is exactly zero).
63 /// @tparam B backend; deduced from the Queue on the `cond(ctx, ...)` spelling
64 /// @tparam T float or double (real only)
65 /// @tparam MF MatrixFormat::Dense
66 /// @param ctx queue the kernels are enqueued on
67 /// @param A batch of square matrices; not modified
68 /// @param norm_type which norm
69 /// @param conds device-accessible output, at least `A.batch_size()` elements
70 /// @param workspace scratch of at least cond_buffer_size() bytes; must outlive the kernels
71 /// @return event of the last enqueued kernel
72 /// @pre `A` is square and nonsingular
73 /// @throws batchlas::invalid_argument for Spectral on a non-square `A`
74 /// @throws batchlas::unsupported for Spectral on a non-dense format
75 /// @note The Spectral path calls the vendor `syev` directly, bypassing syev's
76 /// kernel selection, and throws batchlas::NoRouteError in a vendor-free
77 /// build; see @ref md_docs_2design_2known-defects (defect 2).
78 /// Backend::SYCL calls the native `syev` instead.
79 template <Backend B, typename T, MatrixFormat MF>
80 requires RealScalar<T>
81 BATCHLAS_API Event cond(Queue &ctx,
82 const MatrixView<T, MF> &A,
83 const NormType norm_type,
84 const Span<T> conds,
85 const Span<std::byte> workspace);
86
87 /// @brief Workspace bytes needed by the span overload of cond().
88 ///
89 /// Instantiated only for T in {float, double} with MatrixFormat::Dense: a complex T is a
90 /// compile error (`requires RealScalar<T>`), another format a link error.
91 /// @return size in bytes for the `workspace` argument of cond()
92 /// @ingroup api_extra_lowlevel
93 template <Backend B, typename T, MatrixFormat MF>
94 requires RealScalar<T>
95 BATCHLAS_API size_t cond_buffer_size(Queue &ctx,
96 const MatrixView<T, MF> &A,
97 const NormType norm_type);
98
99 /// @brief Per-item condition number, returned in a new vector. Blocks until done.
100 ///
101 /// Leases its workspace from the queue's arena and waits before returning.
102 /// @return one condition number per batch item
103 template <Backend B, typename T, MatrixFormat MF>
104 requires RealScalar<T>
105 BATCHLAS_API UnifiedVector<T> cond(Queue &ctx,
106 const MatrixView<T, MF> &A,
107 const NormType norm_type);
108
109 /// @brief Random dense matrices with a prescribed log10 condition number.
110 ///
111 /// Builds \f$A_i = U \Sigma V^H\f$ with random orthonormal factors and a singular
112 /// spectrum chosen so that \f$\log_{10}\kappa(A_i)\f$ equals @p log10_kappa in the
113 /// requested metric. Blocks until the result is written.
114 /// @note On Backend::NETLIB the result is the diagonal \f$\Sigma\f$ itself, with
115 /// no random rotation.
116 /// @tparam B backend (not deducible; spell `f<Backend, T>(...)`)
117 /// @tparam T scalar type (not deducible)
118 /// @param ctx queue the kernels are enqueued on
119 /// @param n order of each matrix, > 0
120 /// @param log10_kappa target \f$\log_{10}\kappa\f$, >= 0 (>= \f$\log_{10} n\f$ for Frobenius)
121 /// @param metric NormType::Spectral (\f$\kappa_2\f$) or NormType::Frobenius (\f$\kappa_F\f$)
122 /// @param batch_size number of matrices, > 0
123 /// @param seed RNG seed; equal seeds give equal batches
124 /// @param algo orthogonalisation used to build the random factors. Keep
125 /// the CGS2 default: Chol-QR and Householder can silently
126 /// miss the requested kappa.
127 /// @return a new n x n x batch_size matrix
128 /// @throws batchlas::invalid_argument on a non-positive size, a negative
129 /// `log10_kappa`, or a metric other than Spectral/Frobenius
130 // evidence: docs/design/api-conventions.md#api-conventions-cond-generators-default-to-cgs2
131 template <Backend B, typename T>
133 int n,
134 float_t<T> log10_kappa,
135 NormType metric,
136 int batch_size = 1,
137 unsigned int seed = 42,
139
140 /// @brief Random symmetric/Hermitian matrices with a prescribed log10 condition number.
141 ///
142 /// As random_with_log10_cond_metric(), with \f$A_i = Q \Lambda Q^H\f$.
143 template <Backend B, typename T>
145 int n,
146 float_t<T> log10_kappa,
147 NormType metric,
148 int batch_size = 1,
149 unsigned int seed = 42,
151
152 /// @brief Random banded matrices with a prescribed log10 condition number.
153 ///
154 /// The resulting bandwidth is at most @p kd; for small `kd` the result may be
155 /// diagonal.
156 /// @param kd maximum bandwidth, >= 0
157 /// @throws batchlas::invalid_argument on a negative `kd`, and as random_with_log10_cond_metric()
158 template <Backend B, typename T>
160 int n,
161 int kd,
162 float_t<T> log10_kappa,
163 NormType metric,
164 int batch_size = 1,
165 unsigned int seed = 42);
166
167 /// @brief Random symmetric/Hermitian banded matrices with a prescribed log10 condition number.
168 /// @param kd maximum bandwidth, >= 0
169 template <Backend B, typename T>
171 int n,
172 int kd,
173 float_t<T> log10_kappa,
174 NormType metric,
175 int batch_size = 1,
176 unsigned int seed = 42);
177
178 /// @brief Random tridiagonal matrices with a prescribed log10 condition number.
179 ///
180 /// The condition number is set through the diagonal spectrum; the off-diagonals
181 /// are zero.
182 template <Backend B, typename T>
184 int n,
185 float_t<T> log10_kappa,
186 NormType metric,
187 int batch_size = 1,
188 unsigned int seed = 42);
189
190 /// @brief Random Hermitian tridiagonal matrices with a prescribed log10 condition number.
191 template <Backend B, typename T>
193 int n,
194 float_t<T> log10_kappa,
195 NormType metric,
196 int batch_size = 1,
197 unsigned int seed = 42);
198
199 /// @brief Batched plain transpose \f$B_i := A_i^T\f$ (no conjugation).
200 /// @tparam T float or double
201 /// @param ctx queue the kernel is enqueued on
202 /// @param A batch of m x n matrices; not modified
203 /// @param B batch of n x m matrices, same batch size; must not alias `A`
204 /// @return event of the enqueued kernel
205 template <typename T, MatrixFormat MF>
206 BATCHLAS_API Event transpose(Queue &ctx,
207 const MatrixView<T, MF> &A,
208 const MatrixView<T, MF> &B);
209
210 /// @brief Batched plain transpose into a newly allocated matrix.
211 /// @return a new n x m x batch matrix holding \f$A_i^T\f$; valid after the queue is waited on
212 template <typename T, MatrixFormat MF>
213 BATCHLAS_API Matrix<T, MF> transpose(Queue &ctx,
214 const MatrixView<T, MF> &A);
215
216 /// @}
217
218 // The random_*_with_log10_cond_metric generators are deliberately absent: their T
219 // is non-deduced, so the macro would be an overload that never applies.
220 // evidence: docs/design/api-conventions.md#api-conventions-generators-without-a-dispatch-overload
221
224
225}
Non-owning view of a batch of matrices: the argument type of every entry point.
Definition matrix.hh:485
Owning batch of batch_size() matrices of equal (capacity) shape.
Definition matrix.hh:1399
A real floating-point scalar (float, double).
Definition enums.hh:57
Enumerations, scalar traits and scalar concepts shared by every public header.
NormType
Matrix norm computed by norm and used by the condition estimators.
Definition enums.hh:545
typename base_type< T >::type float_t
Shorthand for base_type<T>::type: the precision of T (tolerances, norms, eigenvalues).
Definition enums.hh:41
OrthoAlgorithm
Algorithm used by ortho to orthonormalise the columns of a batch of blocks.
Definition enums.hh:471
@ Frobenius
sqrt(sum |a_ij|^2).
Definition enums.hh:546
@ CGS2
Classical Gram-Schmidt with one reorthogonalisation pass.
Definition enums.hh:476
size_t cond_buffer_size(Queue &ctx, const MatrixView< T, MF > &A, const NormType norm_type)
Workspace bytes needed by the span overload of cond().
Event cond(Queue &ctx, const MatrixView< T, MF > &A, const NormType norm_type, const Span< T > conds, const Span< std::byte > workspace)
Per-item condition number, written into a caller-owned span.
Matrix< T, MatrixFormat::Dense > random_with_log10_cond_metric(Queue &ctx, int n, float_t< T > log10_kappa, NormType metric, int batch_size=1, unsigned int seed=42, OrthoAlgorithm algo=OrthoAlgorithm::CGS2)
Random dense matrices with a prescribed log10 condition number.
Matrix< T, MatrixFormat::Dense > random_hermitian_tridiagonal_with_log10_cond_metric(Queue &ctx, int n, float_t< T > log10_kappa, NormType metric, int batch_size=1, unsigned int seed=42)
Random Hermitian tridiagonal matrices with a prescribed log10 condition number.
Event transpose(Queue &ctx, const MatrixView< T, MF > &A, const MatrixView< T, MF > &B)
Batched plain transpose (no conjugation).
Matrix< T, MatrixFormat::Dense > random_hermitian_banded_with_log10_cond_metric(Queue &ctx, int n, int kd, float_t< T > log10_kappa, NormType metric, int batch_size=1, unsigned int seed=42)
Random symmetric/Hermitian banded matrices with a prescribed log10 condition number.
Matrix< T, MatrixFormat::Dense > random_banded_with_log10_cond_metric(Queue &ctx, int n, int kd, float_t< T > log10_kappa, NormType metric, int batch_size=1, unsigned int seed=42)
Random banded matrices with a prescribed log10 condition number.
Matrix< T, MatrixFormat::Dense > random_tridiagonal_with_log10_cond_metric(Queue &ctx, int n, float_t< T > log10_kappa, NormType metric, int batch_size=1, unsigned int seed=42)
Random tridiagonal matrices with a prescribed log10 condition number.
Event norm(Queue &ctx, const MatrixView< T, MF > &A, const NormType norm_type, const Span< float_t< T > > norms)
Per-item matrix norm, written into a caller-owned span.
Matrix< T, MatrixFormat::Dense > random_hermitian_with_log10_cond_metric(Queue &ctx, int n, float_t< T > log10_kappa, NormType metric, int batch_size=1, unsigned int seed=42, OrthoAlgorithm algo=OrthoAlgorithm::CGS2)
Random symmetric/Hermitian matrices with a prescribed log10 condition number.
The batched containers: Matrix (owning), MatrixView (non-owning), KernelMatrixView (device-side),...
Backend deduction from the Queue and the USM pointer check.
#define BATCHLAS_DISPATCH_ON_QUEUE(NAME)
Completion handle of enqueued work, returned by every entry point.
Where BatchLAS work runs: a device, a backend, an ordered SYCL queue and a workspace arena.
Non-owning view of size() contiguous elements of type T.
Definition sycl-span.hh:39
Owning, growable array of T in USM shared memory (sycl::malloc_shared).
batchlas::Device, batchlas::Event and batchlas::Queue: where work runs and how it is ordered.
batchlas::UnifiedVector, an owning growable array in USM shared memory.