getri.hh
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief Batched matrix inverse from LU factors (getri) 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 getri = Event(Queue&,
25 const MatrixView<T, MatrixFormat::Dense>&,
26 const MatrixView<T, MatrixFormat::Dense>&,
27 Span<int64_t>, Span<std::byte>, Span<int32_t>);
28
29template <typename T>
30using getri_buffer_size = size_t(Queue&,
31 const MatrixView<T, MatrixFormat::Dense>&);
32
33// Vendor signatures are spelled out, not aliased: a vendor parameter list can differ.
34template <typename T>
35using getri_vendor = Event(Queue&,
36 const MatrixView<T, MatrixFormat::Dense>&,
37 const MatrixView<T, MatrixFormat::Dense>&,
40 Span<int32_t>);
41
42template <typename T>
43using getri_vendor_buffer_size = size_t(Queue&,
44 const MatrixView<T, MatrixFormat::Dense>&);
45} // namespace sig
46
47
48/// @brief Validates A for getri_buffer_size(): non-negative extents only.
49/// @throws batchlas::invalid_argument on negative extents
50/// @ingroup api_factorizations_lowlevel
51// Runs before choose() in src/ops/getri/getri.cc, because the key reads A.rows()/A.cols().
52// Two arities because the query takes A alone and getri's key and can_run are
53// functions of A alone; neither checks squareness, agreement of A and C, or the
54// pivots length (a non-square A fails can_run(Blocked) and goes to the vendor).
55// evidence: docs/design/vendor-independence.md#positional-validators-reject-only-what-no-route-can-serve
56template <typename T>
58 if (A.rows() < 0 || A.cols() < 0) {
60 "GETRI: Matrix dimensions cannot be negative (A: rows=" +
61 std::to_string(A.rows()) + ", cols=" + std::to_string(A.cols()) + ")");
62 }
63}
64
65/// @brief Validates A and C for the positional getri(): non-negative extents, C not aliasing A.
66/// @throws batchlas::invalid_argument on negative extents of A or C, or if a non-empty C
67/// starts at A's storage (getri is out of place on every backend)
68/// @ingroup api_factorizations_lowlevel
69template <typename T>
73 if (C.rows() < 0 || C.cols() < 0) {
75 "GETRI: Matrix dimensions cannot be negative (C: rows=" +
76 std::to_string(C.rows()) + ", cols=" + std::to_string(C.cols()) + ")");
77 }
78 if (A.rows() > 0 && A.batch_size() > 0 && C.data_ptr() == A.data_ptr()) {
79 throw batchlas::invalid_argument("GETRI: C aliases A; getri is out of place");
80 }
81}
82
83
84/// @brief Batched inverse \f$ C = A^{-1} \f$ from the LU factors produced by getrf().
85///
86/// A and @p pivots must be exactly what getrf() produced on the same backend
87/// (see getrf() for the packed 1-based int32 pivot format). The inverse is
88/// written to C, out of place; A is not part of the output.
89///
90/// Asynchronous: C and @p info are readable after the returned event is waited on.
91/// @tparam B backend; the backend-deducing overload takes it from `ctx.backend()`
92/// @tparam T scalar type (float, double, std::complex<float>, std::complex<double>)
93/// @param ctx queue the kernels are enqueued on
94/// @param A batch of n x n LU factors from getrf()
95/// @param C batch of n x n outputs, receives \f$ A^{-1} \f$
96/// @param pivots pivots from getrf(), `n * batch` entries
97/// @param work_space device-accessible scratch of at least getri_buffer_size() bytes
98/// @param info per-item LAPACK status, one int32 per batch item: 0 on
99/// success, i > 0 if U(i,i) is exactly zero, so the item has
100/// no inverse (its C is not meaningful). Empty span = not requested.
101/// @return event of the last enqueued kernel
102/// @pre A and C square of the same order and batch, and
103/// `pivots.size() >= n * batch` (checked by the option overloads only)
104/// @throws batchlas::invalid_argument on negative extents, or if C aliases A
105/// @throws batchlas::NoRouteError if no native kernel can run the shape and the
106/// vendor library was not built in
107/// @note The workspace size does not depend on whether @p info is requested.
108/// @ingroup api_factorizations
109// evidence: docs/design/vendor-independence.md#per-item-info-spans-for-potrf-getrf-and-getri
110template <Backend B, typename T>
111BATCHLAS_API Event getri(Queue& ctx,
114 Span<int64_t> pivots,
115 Span<std::byte> work_space,
116 Span<int32_t> info);
117
118/// @brief getri() without per-item status (`info` not requested).
119/// @ingroup api_factorizations
120// Not a defaulted `info`: the sig:: aliases are function types (see potrf.hh).
121template <Backend B, typename T>
122inline Event getri(Queue& ctx,
125 Span<int64_t> pivots,
126 Span<std::byte> work_space) {
127 return getri<B,T>(ctx, A, C, pivots, work_space, Span<int32_t>{});
128}
129
130/// @brief Workspace, in bytes, that getri() needs for A on this queue.
131///
132/// Takes A alone: the kernel choice, and therefore the size, depends only on A.
133/// @ingroup api_factorizations_lowlevel
134template <Backend B, typename T>
135BATCHLAS_API size_t getri_buffer_size(Queue& ctx,
137
138} // namespace batchlas
139
140
141namespace batchlas::backend {
142
143/// @brief Vendor arm of getri(); called by getri() when it selects the `vendor`
144/// kernel family, not by users.
145/// @ingroup api_dispatch
146// DECLARATION ONLY: the public getri is defined in src/ops/getri/getri.cc.
147// evidence: docs/design/vendor-independence.md#the-entry-point-facade
148template <Backend B, typename T>
149BATCHLAS_API Event getri_vendor(Queue& ctx,
152 Span<int64_t> pivots,
153 Span<std::byte> work_space,
154 Span<int32_t> info_out);
155
156
157/// @brief Workspace query of the vendor arm of getri().
158/// @ingroup api_dispatch
159template <Backend B, typename T>
160BATCHLAS_API size_t getri_vendor_buffer_size(Queue& ctx,
162
163} // namespace batchlas::backend
164
165namespace batchlas {
166
167// Owning-container and backend-deducing overloads; see blas/queue-dispatch.hh.
168
171
172} // namespace batchlas
Non-owning view of a batch of matrices: the argument type of every entry point.
Definition matrix.hh:485
T * data_ptr() const
Base pointer of item 0.
Definition matrix.hh:600
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.
size_t getri_vendor_buffer_size(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A)
Workspace query of the vendor arm of getri().
Event getri_vendor(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, const MatrixView< T, MatrixFormat::Dense > &C, Span< int64_t > pivots, Span< std::byte > work_space, Span< int32_t > info_out)
Vendor arm of getri(); called by getri() when it selects the vendor kernel family,...
size_t getri_buffer_size(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A)
Workspace, in bytes, that getri() needs for A on this queue.
void getri_validate_params(const MatrixView< T, MatrixFormat::Dense > &A)
Validates A for getri_buffer_size(): non-negative extents only.
Definition getri.hh:57
Event getri(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, const MatrixView< T, MatrixFormat::Dense > &C, Span< int64_t > pivots, Span< std::byte > work_space, Span< int32_t > info)
Batched inverse from the LU factors produced by getrf().
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.