potrf.hh
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief Batched Cholesky factorization (potrf) and its workspace query.
5/// @ingroup api_factorizations
6
7#include <batchlas/export.hh>
8#include <cstdint>
9#include <stdexcept>
10#include <string>
11
17
18namespace batchlas {
19
20// Signature aliases for explicit instantiation; see BATCHLAS_INSTANTIATE in
21// src/util/template-instantiations.hh. Keep in sync with the declarations below.
22namespace sig {
23template <typename T>
24using potrf = Event(Queue&,
25 const MatrixView<T, MatrixFormat::Dense>&,
26 Uplo, Span<std::byte>, Span<int32_t>);
27
28template <typename T>
29using potrf_buffer_size = size_t(Queue&,
30 const MatrixView<T, MatrixFormat::Dense>&,
31 Uplo);
32
33// Vendor signatures are spelled out, not aliased: a vendor parameter list can differ.
34template <typename T>
35using potrf_vendor = Event(Queue&,
36 const MatrixView<T, MatrixFormat::Dense>&,
37 Uplo,
39 Span<int32_t>);
40
41template <typename T>
42using potrf_vendor_buffer_size = size_t(Queue&,
43 const MatrixView<T,MatrixFormat::Dense>&,
44 Uplo);
45} // namespace sig
46
47/// @brief Validates the arguments of the positional potrf() entry point.
48///
49/// Checks only what no kernel can serve: non-negative extents, a square A and a
50/// valid @p uplo. It does not check the length of a non-empty `info` span.
51/// Called by the public potrf() before the selection key is built.
52/// @throws batchlas::invalid_argument on negative extents, a non-square A or an
53/// invalid @p uplo.
54/// @ingroup api_factorizations_lowlevel
55// evidence: docs/design/vendor-independence.md#positional-validators-reject-only-what-no-route-can-serve
56template <typename T>
58 Uplo uplo) {
59 if (A.rows() < 0 || A.cols() < 0) {
61 "POTRF: Matrix dimensions cannot be negative (rows=" +
62 std::to_string(A.rows()) + ", cols=" + std::to_string(A.cols()) + ")");
63 }
64 if (A.rows() != A.cols()) {
66 "POTRF: A must be square, got " + std::to_string(A.rows()) + "x" +
67 std::to_string(A.cols()));
68 }
69 if (uplo != Uplo::Lower && uplo != Uplo::Upper) {
71 "POTRF: Invalid uplo parameter: " +
72 std::to_string(static_cast<int>(uplo)));
73 }
74}
75
76
77/// @brief Workspace, in bytes, that potrf() needs for this shape on this queue.
78///
79/// The size does not depend on whether a caller passes an `info` span, so the
80/// same value serves both potrf() overloads.
81/// @tparam B backend; the backend-deducing overload takes it from `ctx.backend()`
82/// @tparam T scalar type (float, double, std::complex<float>, std::complex<double>)
83/// @param ctx queue the factorization will run on (kernel selection reads its device)
84/// @param A batch of n x n matrices to be factorized
85/// @param uplo triangle that will be factorized
86/// @return bytes to pass as the `workspace` span of potrf()
87/// @ingroup api_factorizations_lowlevel
88template <Backend B, typename T>
89BATCHLAS_API size_t potrf_buffer_size(Queue& ctx,
91 Uplo uplo);
92
93/// @brief Batched Cholesky factorization of Hermitian positive-definite matrices.
94///
95/// For every batch item computes \f$ A = L L^H \f$ (`Uplo::Lower`) or
96/// \f$ A = U^H U \f$ (`Uplo::Upper`) and overwrites the @p uplo triangle of A
97/// with the factor. Only that triangle is read. The opposite triangle is not
98/// part of the result: some vendor paths (cuSOLVER's Upper potrf) overwrite it.
99///
100/// The call is asynchronous: it enqueues on @p ctx and returns; A and @p info
101/// are readable only after the returned event (or the queue) has been waited on.
102/// @tparam B backend; the backend-deducing overload takes it from `ctx.backend()`
103/// @tparam T scalar type (float, double, std::complex<float>, std::complex<double>)
104/// @param ctx queue the kernels are enqueued on
105/// @param descrA batch of n x n matrices (any `ld >= n`, any batch stride);
106/// overwritten with the Cholesky factor
107/// @param uplo triangle of A that holds the input and receives the factor
108/// @param workspace device-accessible scratch of at least potrf_buffer_size() bytes
109/// @param info per-item LAPACK status, one int32 per batch item: 0 on
110/// success, i > 0 if the leading minor of order i is not
111/// positive definite (that item's factor is incomplete).
112/// An empty span means "not requested".
113/// @return event of the last enqueued kernel
114/// @pre `descrA.rows() == descrA.cols()`
115/// @pre a non-empty @p info holds at least `descrA.batch_size()` elements; a
116/// shorter non-empty span is silently ignored by this overload (the
117/// checked option overloads throw).
118/// @throws batchlas::invalid_argument on negative extents, a non-square A or an
119/// invalid @p uplo
120/// @throws batchlas::NoRouteError if no native kernel can run the shape and the
121/// vendor library was not built in
122/// @see PotrfOptions for the option-struct spelling; its checked overloads
123/// validate the info span, and its arena overloads lease the workspace.
124/// @ingroup api_factorizations
125// evidence: docs/design/vendor-independence.md#per-item-info-spans-for-potrf-getrf-and-getri
126template <Backend B, typename T>
127BATCHLAS_API Event potrf(Queue& ctx,
129 Uplo uplo,
130 Span<std::byte> workspace,
131 Span<int32_t> info);
132
133/// @brief potrf() without per-item status (`info` not requested).
134/// @ingroup api_factorizations
135// Not a defaulted `info`: the sig:: aliases are function types, which cannot
136// carry default arguments, so a default would not be part of the instantiation.
137template <Backend B, typename T>
138inline Event potrf(Queue& ctx,
140 Uplo uplo,
141 Span<std::byte> workspace) {
142 return potrf<B,T>(ctx, descrA, uplo, workspace, Span<int32_t>{});
143}
144
145} // namespace batchlas
146
147
148namespace batchlas::backend {
149
150/// @brief Vendor arm of potrf(); called by the public potrf(), not by users.
151/// @ingroup api_dispatch
152// Declaration only: the public potrf lives in src/ops/potrf/potrf.cc.
153// evidence: docs/design/vendor-independence.md#the-entry-point-facade
154template <Backend B, typename T>
155BATCHLAS_API Event potrf_vendor(Queue& ctx,
157 Uplo uplo,
158 Span<std::byte> workspace,
159 Span<int32_t> info_out);
160
161
162/// @brief Workspace query of the vendor arm of potrf().
163/// @ingroup api_dispatch
164template <Backend B, typename T>
165BATCHLAS_API size_t potrf_vendor_buffer_size(Queue& ctx,
167 Uplo uplo);
168
169} // namespace batchlas::backend
170
171namespace batchlas {
172
173// Owning-container and backend-deducing overloads; see blas/queue-dispatch.hh.
174
177
178} // namespace batchlas
Non-owning view of a batch of matrices: the argument type of every entry point.
Definition matrix.hh:485
int rows() const
Rows per item (the capacity on a heterogeneous batch).
Definition matrix.hh:605
int cols() const
Columns per item (the capacity on a heterogeneous batch).
Definition matrix.hh:611
The call violates the API contract.
Definition error.hh:77
Enumerations, scalar traits and scalar concepts shared by every public header.
Event potrf_vendor(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &descrA, Uplo uplo, Span< std::byte > workspace, Span< int32_t > info_out)
Vendor arm of potrf(); called by the public potrf(), not by users.
size_t potrf_vendor_buffer_size(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, Uplo uplo)
Workspace query of the vendor arm of potrf().
Uplo
Which triangle of a symmetric, Hermitian or triangular operand is referenced.
Definition enums.hh:287
@ Upper
The upper triangle (i <= j).
Definition enums.hh:288
@ Lower
The lower triangle (i >= j).
Definition enums.hh:289
size_t potrf_buffer_size(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, Uplo uplo)
Workspace, in bytes, that potrf() needs for this shape on this queue.
void potrf_validate_params(const MatrixView< T, MatrixFormat::Dense > &A, Uplo uplo)
Validates the arguments of the positional potrf() entry point.
Definition potrf.hh:57
Event potrf(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &descrA, Uplo uplo, Span< std::byte > workspace, Span< int32_t > info)
Batched Cholesky factorization of Hermitian positive-definite matrices.
The batched containers: Matrix (owning), MatrixView (non-owning), KernelMatrixView (device-side),...
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)
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
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.