syev.hh
Go to the documentation of this file.
1#pragma once
2
3#include <batchlas/export.hh>
4#include <cstdlib>
5#include <optional>
6#include <stdexcept>
7#include <type_traits>
8#include <string_view>
9
10#include <batchlas/settings.hh>
14
17
19
20namespace batchlas {
21
22// Signature aliases for explicit instantiation (BATCHLAS_INSTANTIATE, in
23// src/util/template-instantiations.hh); keep in sync with the declarations below.
24namespace sig {
25template <typename T>
26using syev = Event(Queue&,
27 const MatrixView<T, MatrixFormat::Dense>&,
28 Span<typename base_type<T>::type>,
29 JobType, Uplo, Span<std::byte>, Span<int32_t>);
30
31template <typename T>
32using syev_buffer_size = size_t(Queue&,
33 const MatrixView<T, MatrixFormat::Dense>&,
34 Span<typename base_type<T>::type>,
35 JobType, Uplo);
36
37// backend::syev_vendor / syev_vendor_buffer_size share these signatures.
38template <typename T> using syev_vendor = syev<T>;
39template <typename T> using syev_vendor_buffer_size = syev_buffer_size<T>;
40} // namespace sig
41
42
43/**
44 * @brief Eigenvalues, and optionally eigenvectors, of a batch of symmetric/Hermitian
45 * matrices (LAPACK `?syev` / `?heev`).
46 *
47 * Computes \f$ A = Q \Lambda Q^H \f$ for every batch item from its `uplo` triangle.
48 * The kernel family is the first entry of the nearest row of the device's tuned table
49 * (`tuned/syev.<device>.txt`) that can run the call: on a GPU queue a native
50 * tier (the n <= 32 sub-group solvers `cta`, `cta_fused` and `jacobi`, which need
51 * sub-group size 32; `blocked` or `two_stage` at any n), otherwise `vendor`, the
52 * solver library. Pin a family with `BATCHLAS_SYEV_ROUTE` (e.g. `two_stage`, `vendor`).
53 * Asynchronous: returns once the work is enqueued.
54 *
55 * @tparam B backend (NETLIB always runs the vendor LAPACKE path)
56 * @tparam T float, double, std::complex<float> or std::complex<double>
57 * @param ctx queue the work is enqueued on
58 * @param descrA batch of n x n matrices; with `JobType::EigenVectors` overwritten by
59 * the orthonormal eigenvectors (column j pairs with eigenvalue j),
60 * otherwise its contents are destroyed
61 * @param eigenvalues n real eigenvalues per batch item, packed, ascending
62 * @param jobtype `EigenVectors` or `NoEigenVectors`
63 * @param uplo which triangle of `descrA` holds the matrix
64 * @param workspace at least syev_buffer_size() bytes for the same arguments
65 * @param info per-item convergence status: 0 converged, > 0 LAPACK-like (the
66 * number of off-diagonals that failed to converge, or 1 where the tier
67 * only tracks failure). An EMPTY span means "not requested" and costs
68 * nothing; syev_buffer_size() is the same either way.
69 * @return event of the last enqueued kernel
70 * @throws batchlas::invalid_argument if `descrA` is not square
71 * @throws batchlas::workspace_error if the workspace span is smaller than the chosen kernel needs
72 * @throws batchlas::NoRouteError when no native kernel can run (e.g. a CPU queue) in a
73 * build without the solver library
74 * @throws std::invalid_argument if `BATCHLAS_SYEV_ROUTE` names a family that is not
75 * compiled or cannot run this call (the words `native` and `vendor` instead
76 * fall back to the tuned choice with a warning)
77 * @see @ref selection_tables (which family ranks first where), @ref perf_syev,
78 * @ref md_docs_2cpp-api (convergence status)
79 * @ingroup api_eigen
80 */
81template <Backend B, typename T>
82BATCHLAS_API Event syev(Queue& ctx,
83 const MatrixView<T, MatrixFormat::Dense>& descrA, // A is overwritten with eigenvectors
84 Span<typename base_type<T>::type> eigenvalues,
85 JobType jobtype,
86 Uplo uplo,
87 Span<std::byte> workspace,
88 Span<int32_t> info);
89
90// Old-arity forwarder, not a defaulted `info`: sig::syev is a function TYPE and cannot
91// carry a default, and BATCHLAS_INSTANTIATE needs alias and declaration identical.
92// evidence: docs/design/vendor-independence.md#info-spans-on-syev-gesvd-and-steqr-forwarder-or-default
93/**
94 * @brief syev() without the convergence status (`info` empty).
95 * @ingroup api_eigen
96 */
97template <Backend B, typename T>
98inline Event syev(Queue& ctx,
100 Span<typename base_type<T>::type> eigenvalues,
101 JobType jobtype,
102 Uplo uplo,
103 Span<std::byte> workspace) {
104 return syev<B, T>(ctx, descrA, eigenvalues, jobtype, uplo, workspace, Span<int32_t>{});
105}
106
107/**
108 * @brief Workspace, in bytes, that syev() needs for the same arguments.
109 *
110 * Makes the same kernel choice as the call, so the size is for the tier that will
111 * run. `info` does not affect it.
112 * @ingroup api_eigen_lowlevel
113 */
114template <Backend B, typename T>
115BATCHLAS_API size_t syev_buffer_size(Queue& ctx,
117 Span<typename base_type<T>::type> eigenvalues,
118 JobType jobtype,
119 Uplo uplo);
120
121} // namespace batchlas
122
123namespace batchlas::backend {
124
125// Defined per backend TU (cuSOLVER / rocSOLVER / LAPACKE). `info_out` is DEFAULTED, unlike
126// syev's forwarder: a default is not part of the function type, so sig::syev_vendor still
127// matches and six-argument callers (norm.cc, cond.cc, syevx_lobpcg.cc) need no forwarder.
128// evidence: docs/design/vendor-independence.md#info-spans-on-syev-gesvd-and-steqr-forwarder-or-default
129/**
130 * @brief The vendor solver's syev (cuSOLVER, rocSOLVER or a LAPACKE loop), as syev()'s
131 * `vendor` family calls it; same contract as syev(), with `info_out` as `info`.
132 * @ingroup api_dispatch
133 */
134template <Backend B, typename T>
135BATCHLAS_API Event syev_vendor(Queue& ctx,
137 Span<typename base_type<T>::type> eigenvalues,
138 JobType jobtype,
139 Uplo uplo,
140 Span<std::byte> workspace,
141 Span<int32_t> info_out = Span<int32_t>());
142
143/** @brief Workspace, in bytes, for backend::syev_vendor(). @ingroup api_dispatch */
144template <Backend B, typename T>
145BATCHLAS_API size_t syev_vendor_buffer_size(Queue& ctx,
147 Span<typename base_type<T>::type> eigenvalues,
148 JobType jobtype,
149 Uplo uplo);
150
151} // namespace batchlas::backend
152
154
155// Capability introspection for the Python binding: whether the cta / blocked / two_stage
156// kernel can run A on this queue's device. They ask syev's own can_run (src/ops/syev/syev.cc),
157// so they cannot drift from what selection does. `uplo` is accepted and ignored: both
158// large-n kernels mirror Upper into Lower.
159template <typename T>
160BATCHLAS_API bool syev_supports_cta(const Queue& ctx, const MatrixView<T, MatrixFormat::Dense>& A);
161template <typename T>
162BATCHLAS_API bool syev_supports_blocked(const Queue& ctx, const MatrixView<T, MatrixFormat::Dense>& A, Uplo uplo);
163template <typename T>
164BATCHLAS_API bool syev_supports_two_stage(const Queue& ctx, const MatrixView<T, MatrixFormat::Dense>& A, Uplo uplo);
165
166} // namespace batchlas::ops::syev::detail
167
168namespace batchlas {
169
170// Backend-deducing overloads: `f(ctx, ...)` uses ctx.backend(). See
171// BATCHLAS_DISPATCH_ON_QUEUE in blas/queue-dispatch.hh.
172
173
176
177} // namespace batchlas
Non-owning view of a batch of matrices: the argument type of every entry point.
Definition matrix.hh:485
size_t syev_vendor_buffer_size(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &descrA, Span< typename base_type< T >::type > eigenvalues, JobType jobtype, Uplo uplo)
Workspace, in bytes, for backend::syev_vendor().
Event syev_vendor(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &descrA, Span< typename base_type< T >::type > eigenvalues, JobType jobtype, Uplo uplo, Span< std::byte > workspace, Span< int32_t > info_out=Span< int32_t >())
The vendor solver's syev (cuSOLVER, rocSOLVER or a LAPACKE loop), as syev()'s vendor family calls it;...
size_t syev_buffer_size(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, Span< typename base_type< T >::type > eigenvalues, JobType jobtype, Uplo uplo)
Workspace, in bytes, that syev() needs for the same arguments.
Event syev(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &descrA, Span< typename base_type< T >::type > eigenvalues, JobType jobtype, Uplo uplo, Span< std::byte > workspace, Span< int32_t > info)
Eigenvalues, and optionally eigenvectors, of a batch of symmetric/Hermitian matrices (LAPACK ?...
JobType
Whether an eigensolver computes eigenvectors (LAPACK jobz).
Definition enums.hh:189
Uplo
Which triangle of a symmetric, Hermitian or triangular operand is referenced.
Definition enums.hh:287
Everything <batchlas.hh> provides: containers, entry points, extensions and the linalg layer.
The batched containers: Matrix (owning), MatrixView (non-owning), KernelMatrixView (device-side),...
bool syev_supports_blocked(const Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, Uplo uplo)
bool syev_supports_cta(const Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A)
bool syev_supports_two_stage(const Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, Uplo uplo)
Span(T *, typename Span< T >::size_t) -> Span< T >
Backend deduction from the Queue and the USM pointer check.
#define BATCHLAS_DISPATCH_ON_QUEUE(NAME)
Every BATCHLAS_* environment variable the library reads, captured once into batchlas::Settings,...
Completion handle of enqueued work, returned by every entry point.
Event()
An empty Event, already complete.
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
T type
The real type.
Definition enums.hh:30
batchlas::Device, batchlas::Event and batchlas::Queue: where work runs and how it is ordered.
batchlas::Span, the non-owning pointer + length every entry point takes for 1-D data.