Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
linalg/cublas/gemm.hpp
Go to the documentation of this file.
1#pragma once
2
12
13#include <concepts>
14#include <limits>
15
17{
18namespace detail
19{
20
21inline int cublas_int(blas_int value)
22{
23 CHECK(value >= 0 && value <= std::numeric_limits<int>::max(), value);
24 return static_cast<int>(value);
25}
26
27template <uni20::cublas::CublasScalar Scalar, class LhsHandle, class RhsHandle>
28constexpr bool provider_transforms_are_supported(blas::BlasReadableMatrix<Scalar, LhsHandle> lhs,
30{
31 return blas::blas_trans_char_is_supported<Scalar>(lhs.transform) &&
32 blas::blas_trans_char_is_supported<Scalar>(rhs.transform);
33}
34
35template <uni20::cublas::CublasScalar Scalar, class OutputHandle, class LhsHandle, class RhsHandle>
36blas_int require_gemm_shape(blas::BlasWritableMatrix<Scalar, OutputHandle> output,
39{
40 blas_int const lhs_rows = blas::transformed_rows(lhs.rows, lhs.cols, lhs.transform);
41 blas_int const lhs_cols = blas::transformed_cols(lhs.rows, lhs.cols, lhs.transform);
42 blas_int const rhs_rows = blas::transformed_rows(rhs.rows, rhs.cols, rhs.transform);
43 blas_int const rhs_cols = blas::transformed_cols(rhs.rows, rhs.cols, rhs.transform);
44 CHECK_EQUAL(lhs_cols, rhs_rows);
45 CHECK_EQUAL(output.rows, lhs_rows);
46 CHECK_EQUAL(output.cols, rhs_cols);
47 return lhs_cols;
48}
49
50} // namespace detail
51
55template <uni20::cublas::CublasScalar Scalar, class OutputHandle, class LhsHandle, class RhsHandle>
56 requires std::convertible_to<OutputHandle, Scalar*> && std::convertible_to<LhsHandle, Scalar const*> &&
57 std::convertible_to<RhsHandle, Scalar const*>
61{
62 CHECK(detail::provider_transforms_are_supported(lhs, rhs));
63
64 blas_int const lhs_cols = detail::require_gemm_shape(output, lhs, rhs);
65 if (output.rows == 0 || output.cols == 0) return;
66
67 uni20::cublas::gemm(execution, blas::blas_trans_char<Scalar>(lhs.transform),
68 blas::blas_trans_char<Scalar>(rhs.transform), detail::cublas_int(output.rows),
69 detail::cublas_int(output.cols), detail::cublas_int(lhs_cols), alpha,
70 static_cast<Scalar const*>(lhs.data), detail::cublas_int(lhs.leading_dimension),
71 static_cast<Scalar const*>(rhs.data), detail::cublas_int(rhs.leading_dimension), beta,
72 static_cast<Scalar*>(output.data), detail::cublas_int(output.leading_dimension));
73}
74
75} // namespace uni20::linalg::cublas
Checked column-major GEMM wrappers over a leased cuBLAS execution context.
Provider-ready BLAS matrix operands and transform helpers.
Move-only dynamic pairing of one cuBLAS handle slot and one idle stream.
Definition cublas/execution.hpp:56
Concept for numeric scalar types (integer, real, or complex).
Definition scalar_concepts.hpp:81
locale_t value
Definition data_table.cpp:49
void gemm(ExecutionLease &execution, char lhs_transform, char rhs_transform, int rows, int cols, int inner, Scalar alpha, Scalar const *lhs, int lhs_leading_dimension, Scalar const *rhs, int rhs_leading_dimension, Scalar beta, Scalar *output, int output_leading_dimension)
Enqueue column-major GEMM through an exclusive handle/stream lease.
Definition backend/cublas/gemm.hpp:98
constexpr blas_int transformed_cols(blas_int rows, blas_int cols, MatrixTransform transform)
Columns produced by applying transform to an untransformed matrix.
Definition blas_matrix.hpp:145
constexpr blas_int transformed_rows(blas_int rows, blas_int cols, MatrixTransform transform)
Rows produced by applying transform to an untransformed matrix.
Definition blas_matrix.hpp:139
Definition linalg/cublas/gemm.hpp:17
void gemm(uni20::cublas::ExecutionLease &execution, blas::BlasWritableMatrix< Scalar, OutputHandle > output, Scalar alpha, blas::BlasReadableMatrix< Scalar, LhsHandle > lhs, blas::BlasReadableMatrix< Scalar, RhsHandle > rhs, Scalar beta)
Run provider-ready column-major GEMM through cuBLAS.
Definition linalg/cublas/gemm.hpp:58
mpreal beta(mpreal const &a, mpreal const &x)
Real Beta function; positive rational/integer pairs retain exact values.
Definition mpreal_special.hpp:164
Readable matrix shape plus transform as seen by a BLAS provider.
Definition blas_matrix.hpp:130
blas_int cols
Definition blas_matrix.hpp:133
MatrixTransform transform
Definition blas_matrix.hpp:135
blas_int leading_dimension
Definition blas_matrix.hpp:134
blas_int rows
Definition blas_matrix.hpp:132
Handle data
Definition blas_matrix.hpp:131
Writable matrix shape as seen by a BLAS/LAPACK provider.
Definition blas_matrix.hpp:119
blas_int rows
Definition blas_matrix.hpp:121
blas_int leading_dimension
Definition blas_matrix.hpp:123
Handle data
Definition blas_matrix.hpp:120
blas_int cols
Definition blas_matrix.hpp:122
#define CHECK(cond,...)
Definition trace.hpp:299
#define CHECK_EQUAL(a, b,...)
Definition trace.hpp:309