Nektar++
Loading...
Searching...
No Matches
CuBlasLt.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: CuBlasLt.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: cuBLASLt helpers shared by the CUDA NekBlas back-ends.
32// Private to the NekBlas sources, never installed.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#pragma once
37
39
41
42#include <cstdint>
43#include <cstdlib>
44#include <iostream>
45#include <string>
46#include <type_traits>
47
48#include <cublasLt.h>
49
50#define CUBLAS_CHECK(condition) \
51 { \
52 const cublasStatus_t status = condition; \
53 if (status != CUBLAS_STATUS_SUCCESS) \
54 { \
55 std::cerr << "cuBLAS error encountered: \"" \
56 << cublasGetStatusString(status) << "\" at " << __FILE__ \
57 << ':' << __LINE__ << std::endl; \
58 exit(0); \
59 } \
60 }
61
63{
64inline cublasLtHandle_t GetCuBlasLtHandle()
65{
66 static cublasLtHandle_t handle = nullptr;
67
68 if (!handle)
69 {
70 CUBLAS_CHECK(cublasLtCreate(&handle));
71 }
72
73 return handle;
74}
75
76// Layout of the stored (pre-op) matrix whose op() is M x N.
77inline cublasLtMatrixLayout_t CreateCuBlasLtLayout(
78 const cudaDataType_t dataType, const cublasOperation_t trans, const int M,
79 const int N, const int ld, const int stride, const int batchSize)
80{
81 const bool transposed = (trans == CUBLAS_OP_T);
82
83 cublasLtMatrixLayout_t layout;
84 CUBLAS_CHECK(cublasLtMatrixLayoutCreate(
85 &layout, dataType, transposed ? N : M, transposed ? M : N, ld));
86
87 const int32_t batchCount = batchSize;
88 const int64_t batchOffset = stride;
89 CUBLAS_CHECK(cublasLtMatrixLayoutSetAttribute(
90 layout, CUBLASLT_MATRIX_LAYOUT_BATCH_COUNT, &batchCount,
91 sizeof(batchCount)));
92 CUBLAS_CHECK(cublasLtMatrixLayoutSetAttribute(
93 layout, CUBLASLT_MATRIX_LAYOUT_STRIDED_BATCH_OFFSET, &batchOffset,
94 sizeof(batchOffset)));
95
96 return layout;
97}
98
99// cuBLAS has no strided-batched geam; cublasLtMatrixTransform computes
100// C = alpha*op(A) + beta*op(B) with the batching carried by the layouts.
101template <typename TData>
103 std::string transposeA, std::string transposeB,
104 const int M, const int N, const TData alpha,
105 const TData *a, const int lda,
106 const int strideA, const TData beta,
107 const TData *b, const int ldb,
108 const int strideB, TData *c, const int ldc,
109 const int strideC, const int batchSize)
110{
111 constexpr cudaDataType_t dataType =
112 std::is_same_v<TData, float> ? CUDA_R_32F : CUDA_R_64F;
113
114 auto transA = (transposeA == "N") ? CUBLAS_OP_N : CUBLAS_OP_T;
115 auto transB = (transposeB == "N") ? CUBLAS_OP_N : CUBLAS_OP_T;
116
117 cublasLtMatrixLayout_t layoutA =
118 CreateCuBlasLtLayout(dataType, transA, M, N, lda, strideA, batchSize);
119 cublasLtMatrixLayout_t layoutC = CreateCuBlasLtLayout(
120 dataType, CUBLAS_OP_N, M, N, ldc, strideC, batchSize);
121
122 // A null B (beta == 0) is passed with a null layout.
123 cublasLtMatrixLayout_t layoutB = nullptr;
124 if (b != nullptr)
125 {
126 layoutB = CreateCuBlasLtLayout(dataType, transB, M, N, ldb, strideB,
127 batchSize);
128 }
129
130 cublasLtMatrixTransformDesc_t transformDesc;
131 CUBLAS_CHECK(cublasLtMatrixTransformDescCreate(&transformDesc, dataType));
132 CUBLAS_CHECK(cublasLtMatrixTransformDescSetAttribute(
133 transformDesc, CUBLASLT_MATRIX_TRANSFORM_DESC_TRANSA, &transA,
134 sizeof(transA)));
135 if (b != nullptr)
136 {
137 CUBLAS_CHECK(cublasLtMatrixTransformDescSetAttribute(
138 transformDesc, CUBLASLT_MATRIX_TRANSFORM_DESC_TRANSB, &transB,
139 sizeof(transB)));
140 }
141
142 CUBLAS_CHECK(cublasLtMatrixTransform(
143 GetCuBlasLtHandle(), transformDesc, &alpha, a, layoutA, &beta, b,
144 layoutB, c, layoutC, CUDAStream::GetInstance(handle.GetStreamID())));
145
146 CUBLAS_CHECK(cublasLtMatrixTransformDescDestroy(transformDesc));
147 if (layoutB != nullptr)
148 {
149 CUBLAS_CHECK(cublasLtMatrixLayoutDestroy(layoutB));
150 }
151 CUBLAS_CHECK(cublasLtMatrixLayoutDestroy(layoutA));
152 CUBLAS_CHECK(cublasLtMatrixLayoutDestroy(layoutC));
153}
154} // namespace Nektar::NekBlas::detail
#define CUBLAS_CHECK(condition)
Definition CuBlasLt.hpp:50
#define N
static cudaStream_t & GetInstance(unsigned int id)
unsigned int GetStreamID() const
Definition NekBlas.hpp:59
@ beta
Gauss Radau pinned at x=-1,.
Definition PointsType.h:59
cublasLtMatrixLayout_t CreateCuBlasLtLayout(const cudaDataType_t dataType, const cublasOperation_t trans, const int M, const int N, const int ld, const int stride, const int batchSize)
Definition CuBlasLt.hpp:77
cublasLtHandle_t GetCuBlasLtHandle()
Definition CuBlasLt.hpp:64
void CuBlasLtGeamStridedBatched(Handle< NektarSpaces::Device > handle, std::string transposeA, std::string transposeB, const int M, const int N, const TData alpha, const TData *a, const int lda, const int strideA, const TData beta, const TData *b, const int ldb, const int strideB, TData *c, const int ldc, const int strideC, const int batchSize)
Definition CuBlasLt.hpp:102
std::int32_t int32_t
std::int64_t int64_t