getrf.hh
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief Batched LU factorization with partial pivoting (getrf) 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 getrf = Event(Queue&,
25 const MatrixView<T, MatrixFormat::Dense>&,
26 Span<int64_t>, Span<std::byte>, Span<int32_t>);
27
28template <typename T>
29using getrf_buffer_size = size_t(Queue&,
30 const MatrixView<T, MatrixFormat::Dense>&);
31
32// Vendor signatures are spelled out, not aliased: a vendor parameter list can differ.
33template <typename T>
34using getrf_vendor = Event(Queue&,
35 const MatrixView<T, MatrixFormat::Dense>&,
38 Span<int32_t>);
39
40template <typename T>
41using getrf_vendor_buffer_size = size_t(Queue&,
42 const MatrixView<T, MatrixFormat::Dense>&);
43} // namespace sig
44
45
46/// @brief Validates the arguments of the positional getrf() entry point.
47///
48/// Checks only non-negative extents. Squareness and the pivot span's length are
49/// checked by the option overloads, not here: every native kernel's can_run
50/// refuses a non-square view, which sends it to the vendor rather than rejecting
51/// it (in a vendor-free build: batchlas::NoRouteError).
52/// @throws batchlas::invalid_argument on negative extents
53/// @ingroup api_factorizations_lowlevel
54// Runs in src/ops/getrf/getrf.cc before kernel selection reads A.rows()/A.cols().
55// Deliberately no squareness or pivots-length check; adding either is a
56// user-visible behaviour change.
57// evidence: docs/design/vendor-independence.md#positional-validators-reject-only-what-no-route-can-serve
58template <typename T>
60 if (A.rows() < 0 || A.cols() < 0) {
62 "GETRF: Matrix dimensions cannot be negative (rows=" +
63 std::to_string(A.rows()) + ", cols=" + std::to_string(A.cols()) + ")");
64 }
65}
66
67
68/// @brief Batched LU factorization with partial pivoting.
69///
70/// For every batch item computes \f$ A = P L U \f$ with L unit lower triangular
71/// and U upper triangular, and overwrites A with L (below the diagonal, unit
72/// diagonal implied) and U (on and above it), as LAPACK `?getrf`.
73///
74/// **Pivot format.** @p pivots is typed `int64_t`, but on the GPU backends and
75/// every native kernel it holds *packed 1-based int32* indices: item b's row
76/// interchange i (row i was swapped with row `ipiv[i]`) is the int32 at index
77/// `b * n + i` of the span's bytes reinterpreted as `int32_t`. The NETLIB
78/// backend writes genuine 1-based int64 instead. Pass the span unchanged to
79/// getrs() / getri() on the same backend; do not read or compare the entries
80/// across backends. Complex pivots are chosen on `|Re| + |Im|` natively and on
81/// the modulus by cuBLAS, so pivot sequences can differ between kernels while
82/// both factorizations are valid.
83///
84/// Asynchronous: A, @p pivots and @p info are readable after the returned event
85/// is waited on.
86/// @tparam B backend; the backend-deducing overload takes it from `ctx.backend()`
87/// @tparam T scalar type (float, double, std::complex<float>, std::complex<double>)
88/// @param ctx queue the kernels are enqueued on
89/// @param A batch of n x n matrices; overwritten with L and U. An m x n batch is
90/// factored whole by the NETLIB and ROCm vendors (min(m, n) pivots per item,
91/// packed); CUDA has no route for it
92/// @param pivots pivot output, at least `n * batch` `int64_t` elements
93/// @param work_space device-accessible scratch of at least getrf_buffer_size() bytes
94/// @param info per-item LAPACK status, one int32 per batch item: 0 on
95/// success, i > 0 if U(i,i) is exactly zero (1-based; the
96/// factorization completed but U is singular). Empty span =
97/// not requested.
98/// @return event of the last enqueued kernel
99/// @pre `A.rows() == A.cols()` (checked by the option overloads only)
100/// @pre `pivots.size() >= A.rows() * A.batch_size()` (checked by the option overloads only)
101/// @pre a non-empty @p info holds at least `A.batch_size()` elements; a shorter
102/// one is silently ignored by this overload
103/// @throws batchlas::invalid_argument on negative extents
104/// @throws batchlas::NoRouteError if no native kernel can run the shape and the
105/// vendor library was not built in
106/// @note The workspace size does not depend on whether @p info is requested.
107/// @ingroup api_factorizations
108// evidence: docs/design/vendor-independence.md#per-item-info-spans-for-potrf-getrf-and-getri
109template <Backend B, typename T>
110BATCHLAS_API Event getrf(Queue& ctx,
112 Span<int64_t> pivots,
113 Span<std::byte> work_space,
114 Span<int32_t> info);
115
116/// @brief getrf() without per-item status (`info` not requested).
117/// @ingroup api_factorizations
118// Not a defaulted `info`: the sig:: aliases are function types (see potrf.hh).
119template <Backend B, typename T>
120inline Event getrf(Queue& ctx,
122 Span<int64_t> pivots,
123 Span<std::byte> work_space) {
124 return getrf<B,T>(ctx, A, pivots, work_space, Span<int32_t>{});
125}
126
127/// @brief Workspace, in bytes, that getrf() needs for this shape on this queue.
128/// @param ctx queue the factorization will run on (kernel selection reads its device)
129/// @param A batch of n x n matrices to be factorized
130/// @return bytes to pass as the `work_space` span of getrf()
131/// @ingroup api_factorizations_lowlevel
132template <Backend B, typename T>
133BATCHLAS_API size_t getrf_buffer_size(Queue& ctx,
135
136} // namespace batchlas
137
138
139namespace batchlas::backend {
140
141/// @brief Vendor arm of getrf(); called by getrf() when it selects the `vendor`
142/// kernel family, not by users.
143/// @ingroup api_dispatch
144// DECLARATION ONLY: the public getrf is defined in src/ops/getrf/getrf.cc.
145// evidence: docs/design/vendor-independence.md#the-entry-point-facade
146template <Backend B, typename T>
147BATCHLAS_API Event getrf_vendor(Queue& ctx,
149 Span<int64_t> pivots,
150 Span<std::byte> work_space,
151 Span<int32_t> info_out);
152
153
154/// @brief Workspace query of the vendor arm of getrf().
155/// @ingroup api_dispatch
156template <Backend B, typename T>
157BATCHLAS_API size_t getrf_vendor_buffer_size(Queue& ctx,
159
160} // namespace batchlas::backend
161
162namespace batchlas {
163
164// Owning-container and backend-deducing overloads; see blas/queue-dispatch.hh.
165
168
169} // 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 getrf_vendor(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, Span< int64_t > pivots, Span< std::byte > work_space, Span< int32_t > info_out)
Vendor arm of getrf(); called by getrf() when it selects the vendor kernel family,...
size_t getrf_vendor_buffer_size(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A)
Workspace query of the vendor arm of getrf().
void getrf_validate_params(const MatrixView< T, MatrixFormat::Dense > &A)
Validates the arguments of the positional getrf() entry point.
Definition getrf.hh:59
size_t getrf_buffer_size(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A)
Workspace, in bytes, that getrf() needs for this shape on this queue.
Event getrf(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, Span< int64_t > pivots, Span< std::byte > work_space, Span< int32_t > info)
Batched LU factorization with partial pivoting.
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.