spmm.hh
Go to the documentation of this file.
1#pragma once
2
3#include <batchlas/export.hh>
9
10namespace batchlas {
11
12// Signature aliases for explicit instantiation; see BATCHLAS_INSTANTIATE in
13// src/util/template-instantiations.hh. Keep in sync with the declarations below.
14namespace sig {
15template <typename T, MatrixFormat F>
16using spmm = Event(Queue&,
17 const MatrixView<T, F>&,
18 const MatrixView<T, MatrixFormat::Dense>&,
19 const MatrixView<T, MatrixFormat::Dense>&,
21
22template <typename T, MatrixFormat F>
23using spmm_buffer_size = size_t(Queue&,
24 const MatrixView<T, F>&,
25 const MatrixView<T, MatrixFormat::Dense>&,
26 const MatrixView<T, MatrixFormat::Dense>&,
27 T, T, Transpose, Transpose);
28
29// backend::spmm_vendor / _vendor_buffer_size share the public signatures.
30template <typename T, MatrixFormat F>
31using spmm_vendor = spmm<T, F>;
32template <typename T, MatrixFormat F>
33using spmm_vendor_buffer_size = spmm_buffer_size<T, F>;
34} // namespace sig
35
36
37/// @brief Batched sparse-times-dense matrix multiply.
38///
39/// For every batch item computes
40/// \f[ C := \alpha \, \mathrm{op}(A) \, \mathrm{op}(B) + \beta \, C \f]
41/// with `A` sparse (CSR) and `B`, `C` dense; \f$\mathrm{op}(X)\f$ is
42/// \f$X\f$, \f$X^T\f$ or \f$X^H\f$ according to `transA` / `transB`. With
43/// \f$\mathrm{op}(A)\f$ m x k and \f$\mathrm{op}(B)\f$ k x n, `C` is m x n.
44///
45/// Takes a caller-supplied workspace, sized by spmm_buffer_size with the same
46/// arguments. Lease it from the queue's arena with `ctx.workspace(bytes)`. Also
47/// callable with owning `Matrix` arguments and without `B` (taken from
48/// `ctx.backend()`); spell a partial explicit call `spmm<Backend::CUDA>(...)`
49/// and let `T` and `MFormat` deduce.
50///
51/// @tparam B backend the call is compiled for; must match `ctx`'s device
52/// @tparam T scalar type: `float`, `double`, `std::complex<float>` or `std::complex<double>`
53/// @tparam MFormat storage format of `A`; only `MatrixFormat::CSR` is instantiated
54/// @param ctx queue the work is enqueued on
55/// @param A batch of sparse matrices; not modified
56/// @param descrB batch of dense k x n (NoTrans) or n x k matrices; not modified
57/// @param descrC batch of dense m x n matrices; input scaled by `beta`, overwritten with the result
58/// @param alpha scale of the product
59/// @param beta scale of the input `C`
60/// @param transA op() applied to `A`
61/// @param transB op() applied to `B`
62/// @param workspace device-accessible scratch of at least spmm_buffer_size bytes
63/// @return event of the last enqueued kernel; `C` is valid once it completes
64/// @pre All operands have the same batch size and conforming shapes per item.
65/// @throws batchlas::NoRouteError in a build without the vendor
66/// sparse library for `B` when the native kernel does not support the call
67/// (a heterogeneous batch, or negative extents).
68/// @note No argument validation is done up front: a shape the native kernel
69/// refuses goes to the vendor library, which reports it.
70/// @note On `Backend::ROCM` a real `T` with `ConjTrans` is suspected to give wrong
71/// results (known defect 4, @ref md_docs_2design_2known-defects); use `Trans`.
72/// @see spmm_buffer_size, @ref md_docs_2perf_2spmm
73/// @ingroup api_sparse
74template <Backend B, typename T, MatrixFormat MFormat>
75BATCHLAS_API Event spmm(Queue& ctx,
79 T alpha,
80 T beta,
81 Transpose transA,
82 Transpose transB,
83 Span<std::byte> workspace);
84
85/// @brief Bytes of workspace spmm needs for these arguments.
86///
87/// Resolves the same route as spmm and returns that route's need; a natively
88/// routed call needs zero bytes and is sized without touching device memory.
89/// Pass exactly the arguments the spmm call will get.
90/// @tparam B backend the call is compiled for
91/// @tparam T scalar type
92/// @tparam MFormat storage format of `A`; only `MatrixFormat::CSR` is instantiated
93/// @param ctx queue the call will run on
94/// @param A the sparse operand of the spmm call
95/// @param B_mat the dense operand of the spmm call
96/// @param C the output of the spmm call
97/// @param alpha the spmm call's `alpha`
98/// @param beta the spmm call's `beta`
99/// @param transA the spmm call's `transA`
100/// @param transB the spmm call's `transB`
101/// @return required workspace size in bytes (may be 0)
102/// @throws batchlas::NoRouteError under the same conditions as spmm
103/// @ingroup api_sparse_lowlevel
104template <Backend B, typename T, MatrixFormat MFormat>
105BATCHLAS_API size_t spmm_buffer_size(Queue& ctx,
106 const MatrixView<T, MFormat>& A,
109 T alpha,
110 T beta,
111 Transpose transA,
112 Transpose transB);
113
114} // namespace batchlas
115
116
117namespace batchlas::backend {
118
119// Declaration only (see gemm_vendor). spmm carries a MatrixFormat parameter, so
120// its instantiations are hand-written in each vendor TU.
121// evidence: docs/design/vendor-independence.md#the-entry-point-facade
122/// @brief Vendor-library implementation of spmm (cuSPARSE, rocSPARSE, host).
123///
124/// Not an entry point: batchlas::spmm calls it. Same arguments and semantics.
125/// @ingroup api_dispatch
126template <Backend B, typename T, MatrixFormat MFormat>
127BATCHLAS_API Event spmm_vendor(Queue& ctx,
128 const MatrixView<T, MFormat>& A,
131 T alpha,
132 T beta,
133 Transpose transA,
134 Transpose transB,
135 Span<std::byte> workspace);
136
137/// @brief Workspace bytes spmm_vendor needs; called by batchlas::spmm_buffer_size.
138/// @ingroup api_dispatch
139template <Backend B, typename T, MatrixFormat MFormat>
140BATCHLAS_API size_t spmm_vendor_buffer_size(Queue& ctx,
141 const MatrixView<T, MFormat>& A,
144 T alpha,
145 T beta,
146 Transpose transA,
147 Transpose transB);
148
149} // namespace batchlas::backend
150
151namespace batchlas {
152
153// Owning-argument (`f(ctx, Matrix, ...)`) and backend-deducing (`f(ctx, ...)`)
154// overloads; see blas/queue-dispatch.hh.
155
156
159
160} // namespace batchlas
Non-owning view of a batch of matrices: the argument type of every entry point.
Definition matrix.hh:485
Enumerations, scalar traits and scalar concepts shared by every public header.
size_t spmm_vendor_buffer_size(Queue &ctx, const MatrixView< T, MFormat > &A, const MatrixView< T, MatrixFormat::Dense > &B_mat, const MatrixView< T, MatrixFormat::Dense > &C, T alpha, T beta, Transpose transA, Transpose transB)
Workspace bytes spmm_vendor needs; called by batchlas::spmm_buffer_size.
Event spmm_vendor(Queue &ctx, const MatrixView< T, MFormat > &A, const MatrixView< T, MatrixFormat::Dense > &B_mat, const MatrixView< T, MatrixFormat::Dense > &C, T alpha, T beta, Transpose transA, Transpose transB, Span< std::byte > workspace)
Vendor-library implementation of spmm (cuSPARSE, rocSPARSE, host).
Transpose
The operator op() applied to an operand: op(A) = A, A^T or A^H.
Definition enums.hh:171
size_t spmm_buffer_size(Queue &ctx, const MatrixView< T, MFormat > &A, const MatrixView< T, MatrixFormat::Dense > &B_mat, const MatrixView< T, MatrixFormat::Dense > &C, T alpha, T beta, Transpose transA, Transpose transB)
Bytes of workspace spmm needs for these arguments.
Event spmm(Queue &ctx, const MatrixView< T, MFormat > &A, const MatrixView< T, MatrixFormat::Dense > &descrB, const MatrixView< T, MatrixFormat::Dense > &descrC, T alpha, T beta, Transpose transA, Transpose transB, Span< std::byte > workspace)
Batched sparse-times-dense matrix multiply.
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.