posv.hh
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief Batched Hermitian positive-definite solve (posv) by Cholesky.
5/// @ingroup api_factorizations
6
7// No vendor arm and no `potrs` op: the composed arm is potrf then two routed
8// trsm calls, and the documented contract below is exactly that composition.
9// evidence: docs/perf/potrf.md#the-fused-posv-tier
10
11#include <batchlas/export.hh>
12#include <cstdint>
13#include <stdexcept>
14#include <string>
15
21
22namespace batchlas {
23
24// Signature aliases for explicit instantiation; see BATCHLAS_INSTANTIATE in
25// src/util/template-instantiations.hh. Keep in sync with the declarations below.
26namespace sig {
27template <typename T>
28using posv = Event(Queue&,
29 const MatrixView<T, MatrixFormat::Dense>&,
30 const MatrixView<T, MatrixFormat::Dense>&,
31 Uplo, Span<std::byte>, Span<int32_t>);
32
33template <typename T>
34using posv_buffer_size = size_t(Queue&,
35 const MatrixView<T, MatrixFormat::Dense>&,
36 const MatrixView<T, MatrixFormat::Dense>&,
37 Uplo);
38} // namespace sig
39
40/// @brief Validates the arguments of posv() and posv_buffer_size().
41///
42/// Checks non-negative extents, a square A, `B.rows() == A.rows()`, equal batch
43/// sizes and a valid @p uplo.
44/// @throws batchlas::invalid_argument if any check fails
45/// @ingroup api_factorizations_lowlevel
46// Stricter than potrf's validator on purpose: with no vendor arm, a
47// non-conforming pair would otherwise surface as a misleading "no route" error.
48template <typename T>
51 Uplo uplo) {
52 if (A.rows() < 0 || A.cols() < 0 || B.rows() < 0 || B.cols() < 0) {
54 "POSV: Matrix dimensions cannot be negative (A: rows=" +
55 std::to_string(A.rows()) + ", cols=" + std::to_string(A.cols()) +
56 "; B: rows=" + std::to_string(B.rows()) +
57 ", cols=" + std::to_string(B.cols()) + ")");
58 }
59 if (A.rows() != A.cols()) {
61 "POSV: A must be square, got " + std::to_string(A.rows()) + "x" +
62 std::to_string(A.cols()));
63 }
64 if (A.rows() != B.rows()) {
66 "POSV: B must have A.rows() rows, got " + std::to_string(B.rows()) +
67 " against " + std::to_string(A.rows()));
68 }
69 if (A.batch_size() != B.batch_size()) {
71 "POSV: A and B must share a batch size, got " +
72 std::to_string(A.batch_size()) + " and " + std::to_string(B.batch_size()));
73 }
74 if (uplo != Uplo::Lower && uplo != Uplo::Upper) {
76 "POSV: Invalid uplo parameter: " + std::to_string(static_cast<int>(uplo)));
77 }
78}
79
80/// @brief Batched solve of \f$ A X = B \f$ for Hermitian positive-definite A.
81///
82/// Semantically `potrf(A, uplo)` followed by the two triangular solves with the
83/// factor: on return A holds the Cholesky factor in the @p uplo triangle (the
84/// other triangle is not read, and, as for potrf(), a vendor potrf leg may
85/// overwrite it), B holds X, and @p info carries potrf's status. There is
86/// no vendor library call behind this op on any backend; it is served by native
87/// fused tiers or by the routed potrf + trsm composition.
88///
89/// Asynchronous: A, B and @p info are readable after the returned event is waited on.
90/// @tparam Back backend; the backend-deducing overload takes it from `ctx.backend()`
91/// @tparam T scalar type (float, double, std::complex<float>, std::complex<double>)
92/// @param ctx queue the kernels are enqueued on
93/// @param A batch of n x n matrices; overwritten with the Cholesky factor
94/// @param B batch of n x nrhs right-hand sides; overwritten with X
95/// @param uplo triangle of A that is read and receives the factor
96/// @param work_space device-accessible scratch of at least posv_buffer_size() bytes
97/// @param info per-item status as for potrf(): 0 on success, i > 0 if the
98/// leading minor of order i is not positive definite (that
99/// item's X is meaningless). Empty span = not requested.
100/// @return event of the last enqueued kernel
101/// @throws batchlas::invalid_argument if posv_validate_params() rejects the call
102/// @ingroup api_factorizations
103template <Backend Back, typename T>
104BATCHLAS_API Event posv(Queue& ctx,
107 Uplo uplo,
108 Span<std::byte> work_space,
109 Span<int32_t> info);
110
111/// @brief Workspace, in bytes, that posv() needs for these operands on this queue.
112///
113/// Takes the same operands as the call, so both resolve the same route.
114/// @throws batchlas::invalid_argument if posv_validate_params() rejects the operands
115/// @ingroup api_factorizations_lowlevel
116template <Backend Back, typename T>
117BATCHLAS_API size_t posv_buffer_size(Queue& ctx,
120 Uplo uplo);
121
122/// @brief posv() without per-item status (`info` not requested).
123/// @ingroup api_factorizations
124// Not a defaulted `info`: the sig:: aliases are function types (see potrf.hh).
125template <Backend Back, typename T>
126inline Event posv(Queue& ctx,
129 Uplo uplo,
130 Span<std::byte> work_space) {
131 return posv<Back, T>(ctx, A, B, uplo, work_space, Span<int32_t>{});
132}
133
134
137
138} // 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
int batch_size() const
Number of batch items.
Definition matrix.hh:603
The call violates the API contract.
Definition error.hh:77
Enumerations, scalar traits and scalar concepts shared by every public header.
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
void posv_validate_params(const MatrixView< T, MatrixFormat::Dense > &A, const MatrixView< T, MatrixFormat::Dense > &B, Uplo uplo)
Validates the arguments of posv() and posv_buffer_size().
Definition posv.hh:49
size_t posv_buffer_size(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, const MatrixView< T, MatrixFormat::Dense > &B, Uplo uplo)
Workspace, in bytes, that posv() needs for these operands on this queue.
Event posv(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, const MatrixView< T, MatrixFormat::Dense > &B, Uplo uplo, Span< std::byte > work_space, Span< int32_t > info)
Batched solve of for Hermitian positive-definite A.
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.