gemm.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 declaration below.
14namespace sig {
15template <typename T>
16using gemm = Event(Queue&,
17 const MatrixView<T, MatrixFormat::Dense>&,
18 const MatrixView<T, MatrixFormat::Dense>&,
19 const MatrixView<T, MatrixFormat::Dense>&,
21
22// backend::gemm_vendor shares gemm's signature.
23template <typename T>
24using gemm_vendor = gemm<T>;
25} // namespace sig
26
27/// @brief Batched general matrix-matrix multiply.
28///
29/// For every batch item \f$b\f$ computes
30/// \f[ C_b := \alpha \, \mathrm{op}(A_b) \, \mathrm{op}(B_b) + \beta \, C_b \f]
31/// where \f$\mathrm{op}(X)\f$ is \f$X\f$, \f$X^T\f$ or \f$X^H\f$ according to
32/// `transA` / `transB`. With \f$\mathrm{op}(A)\f$ of size m x k and
33/// \f$\mathrm{op}(B)\f$ of size k x n, `C` is m x n.
34///
35/// **Heterogeneous batch.** A batch whose items carry differing
36/// `active_rows` / `active_cols` is handled by this same entry point on every
37/// backend: an item with m == 0 or n == 0 is skipped, an item with k == 0
38/// computes \f$C_b := \beta C_b\f$, and an all-skipped batch still returns a
39/// valid Event. There is no separate `gemm_heterogeneous` in C++ (the Python
40/// binding's one exists to coerce a list of differently-shaped arrays).
41///
42/// Also callable as `gemm(ctx, A, B, C, GemmOptions<T>{...})`, with owning
43/// `Matrix` arguments, and without `Back` (taken from `ctx.backend()`).
44///
45/// @tparam Back backend the call is compiled for; must match `ctx`'s device
46/// @tparam T scalar type: `float`, `double`, `std::complex<float>` or `std::complex<double>`
47/// @param ctx queue the work is enqueued on
48/// @param A batch of matrices, m x k (NoTrans) or k x m (Trans/ConjTrans)
49/// @param B batch of matrices, k x n (NoTrans) or n x k (Trans/ConjTrans)
50/// @param C batch of m x n matrices; input scaled by `beta`, overwritten with the result
51/// @param alpha scale of the product
52/// @param beta scale of the input `C`
53/// @param transA op() applied to `A`
54/// @param transB op() applied to `B`
55/// @param precision compute precision; `ComputePrecision::Default` computes in `T`.
56/// Other values are honoured only by the vendor library path.
57/// @return event of the last enqueued kernel; `C` is valid once it completes
58/// @pre `A`, `B` and `C` have the same batch size and conforming shapes per item.
59/// @throws batchlas::NoRouteError in a build without the vendor BLAS for `Back`
60/// when no native kernel can run the call (a non-Default `precision`).
61/// An empty batch, m or n is a no-op, and k = 0 computes `C = beta * C`;
62/// neither reaches selection, so `precision` is not checked there.
63/// @see GemmOptions, @ref md_docs_2cpp-api
64/// @ingroup api_blas3
65template <Backend Back, typename T>
66BATCHLAS_API Event gemm(Queue& ctx,
70 T alpha,
71 T beta,
72 Transpose transA,
73 Transpose transB,
75
76} // namespace batchlas
77
79
80// DECLARATION ONLY: each vendor TU defines and instantiates it for its Backend;
81// the public gemm is defined in src/ops/gemm/gemm.cc.
82// evidence: docs/design/vendor-independence.md#the-entry-point-facade
83/// @brief Vendor-library implementation of gemm (cuBLAS, rocBLAS, host BLAS).
84///
85/// Not an entry point: batchlas::gemm calls it when it selects the `vendor`
86/// kernel family. Same arguments and semantics as batchlas::gemm.
87/// @ingroup api_dispatch
88template <Backend Back, typename T>
89BATCHLAS_API Event gemm_vendor(Queue& ctx,
93 T alpha,
94 T beta,
95 Transpose transA,
96 Transpose transB,
97 ComputePrecision precision);
98
99} // namespace batchlas::backend
100
101namespace batchlas {
102
103// Owning-argument (`f(ctx, Matrix, ...)`) and backend-deducing (`f(ctx, ...)`)
104// overloads; see blas/queue-dispatch.hh.
105
106
108
109} // 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.
Event gemm(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, const MatrixView< T, MatrixFormat::Dense > &B, const MatrixView< T, MatrixFormat::Dense > &C, T alpha, T beta, Transpose transA, Transpose transB, ComputePrecision precision=ComputePrecision::Default)
Batched general matrix-matrix multiply.
Event gemm_vendor(Queue &ctx, const MatrixView< T, MatrixFormat::Dense > &A, const MatrixView< T, MatrixFormat::Dense > &B, const MatrixView< T, MatrixFormat::Dense > &C, T alpha, T beta, Transpose transA, Transpose transB, ComputePrecision precision)
Vendor-library implementation of gemm (cuBLAS, rocBLAS, host BLAS).
Transpose
The operator op() applied to an operand: op(A) = A, A^T or A^H.
Definition enums.hh:171
ComputePrecision
Internal compute precision of a GEMM (the cuBLAS cublasComputeType_t).
Definition enums.hh:505
@ Default
The precision of the scalar type.
Definition enums.hh:506
The batched containers: Matrix (owning), MatrixView (non-owning), KernelMatrixView (device-side),...
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.
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.