Nektar++
Loading...
Searching...
No Matches
magma.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: magma.cpp
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: NekBlas backend for the Device execution space (MAGMA).
32// Vendor headers are included here, never in NekBlas.hpp.
33//
34///////////////////////////////////////////////////////////////////////////////
35
37
38#include <cstdlib>
39#include <iostream>
40#include <type_traits>
41#include <unordered_map>
42
43#include <magma_v2.h>
44
45#if defined(NEKTAR_ENABLE_CUDA)
47
49#elif defined(NEKTAR_ENABLE_HIP)
51
52#define HIPBLAS_CHECK(condition) \
53 { \
54 const hipblasStatus_t status = condition; \
55 if (status != HIPBLAS_STATUS_SUCCESS) \
56 { \
57 std::cerr << "hipBLAS error encountered: \"" \
58 << hipblasStatusToString(status) << "\" at " << __FILE__ \
59 << ':' << __LINE__ << std::endl; \
60 exit(0); \
61 } \
62 }
63#endif
64
65namespace Nektar::NekBlas
66{
67namespace
68{
69magma_queue_t GetMagmaQueue(const unsigned int streamID)
70{
71 static std::unordered_map<unsigned int, magma_queue_t> handle;
72
73 if (handle.find(streamID) == handle.end())
74 {
75 magma_queue_t magma_queue;
76 int device_rank = 0;
77#if defined(NEKTAR_ENABLE_CUDA)
78 (void)cudaGetDevice(&device_rank);
79 cudaStream_t stream = CUDAStream::GetInstance(streamID);
80 cublasHandle_t cublas_handle;
81 cusparseHandle_t cusparse_handle;
82 CUBLAS_CHECK(cublasCreate(&cublas_handle));
83 (void)cusparseCreate(&cusparse_handle);
84 CUBLAS_CHECK(cublasSetStream(cublas_handle, stream));
85 (void)cusparseSetStream(cusparse_handle, stream);
86 magma_queue_create_from_cuda(device_rank, stream, cublas_handle,
87 cusparse_handle, &magma_queue);
88#elif defined(NEKTAR_ENABLE_HIP)
89 (void)hipGetDevice(&device_rank);
90 hipStream_t stream = HIPStream::GetInstance(streamID);
91 hipblasHandle_t hipblas_handle;
92 hipsparseHandle_t hipsparse_handle;
93 HIPBLAS_CHECK(hipblasCreate(&hipblas_handle));
94 (void)hipsparseCreate(&hipsparse_handle);
95 HIPBLAS_CHECK(hipblasSetStream(hipblas_handle, stream));
96 (void)hipsparseSetStream(hipsparse_handle, stream);
97 magma_queue_create_from_hip(device_rank, stream, hipblas_handle,
98 hipsparse_handle, &magma_queue);
99#endif
100 handle[streamID] = magma_queue;
101 }
102
103 return handle[streamID];
104}
105} // namespace
106
107template <typename TData>
108void Gemm(Handle<NektarSpaces::Device> handle, std::string transposeA,
109 std::string transposeB, const int M, const int N, const int K,
110 const TData alpha, const TData *a, const int lda, const TData *b,
111 const int ldb, const TData beta, TData *c, const int ldc)
112{
113 auto queue = GetMagmaQueue(handle.GetStreamID());
114
115 auto transA = (transposeA == "N") ? MagmaNoTrans : MagmaTrans;
116 auto transB = (transposeB == "N") ? MagmaNoTrans : MagmaTrans;
117
118 if constexpr (std::is_same_v<TData, float>)
119 {
120 magma_sgemm(transA, transB, M, N, K, alpha, a, lda, b, ldb, beta, c,
121 ldc, queue);
122 }
123 else if constexpr (std::is_same_v<TData, double>)
124 {
125 magma_dgemm(transA, transB, M, N, K, alpha, a, lda, b, ldb, beta, c,
126 ldc, queue);
127 }
128}
129
130template <typename TData>
131void GemmStridedBatched(Handle<NektarSpaces::Device> handle,
132 std::string transposeA, std::string transposeB,
133 const int M, const int N, const int K,
134 const TData alpha, const TData *a, const int lda,
135 const int strideA, const TData *b, const int ldb,
136 const int strideB, const TData beta, TData *c,
137 const int ldc, const int strideC, const int batchSize)
138{
139 auto queue = GetMagmaQueue(handle.GetStreamID());
140
141 auto transA = (transposeA == "N") ? MagmaNoTrans : MagmaTrans;
142 auto transB = (transposeB == "N") ? MagmaNoTrans : MagmaTrans;
143
144 if constexpr (std::is_same_v<TData, float>)
145 {
146 magmablas_sgemm_batched_strided(transA, transB, M, N, K, alpha, a, lda,
147 strideA, b, ldb, strideB, beta, c, ldc,
148 strideC, batchSize, queue);
149 }
150 else if constexpr (std::is_same_v<TData, double>)
151 {
152 magmablas_dgemm_batched_strided(transA, transB, M, N, K, alpha, a, lda,
153 strideA, b, ldb, strideB, beta, c, ldc,
154 strideC, batchSize, queue);
155 }
156}
157
158template <typename TData>
159void Gemv(Handle<NektarSpaces::Device> handle, std::string transpose,
160 const int M, const int N, const TData alpha, const TData *a,
161 const int lda, const TData *x, const int incx, const TData beta,
162 TData *y, const int incy)
163{
164 auto queue = GetMagmaQueue(handle.GetStreamID());
165
166 auto trans = (transpose == "N") ? MagmaNoTrans : MagmaTrans;
167
168 if constexpr (std::is_same_v<TData, float>)
169 {
170 magma_sgemv(trans, M, N, alpha, a, lda, x, incx, beta, y, incy, queue);
171 }
172 else if constexpr (std::is_same_v<TData, double>)
173 {
174 magma_dgemv(trans, M, N, alpha, a, lda, x, incx, beta, y, incy, queue);
175 }
176}
177
178template <typename TData>
179void GemvStridedBatched(Handle<NektarSpaces::Device> handle,
180 std::string transpose, const int M, const int N,
181 const TData alpha, const TData *a, const int lda,
182 const int strideA, const TData *x, const int incx,
183 const int strideX, const TData beta, TData *y,
184 const int incy, const int strideY, const int batchSize)
185{
186 auto queue = GetMagmaQueue(handle.GetStreamID());
187
188 auto trans = (transpose == "N") ? MagmaNoTrans : MagmaTrans;
189
190 if constexpr (std::is_same_v<TData, float>)
191 {
192 magmablas_sgemv_batched_strided(trans, M, N, alpha, a, lda, strideA, x,
193 incx, strideX, beta, y, incy, strideY,
194 batchSize, queue);
195 }
196 else if constexpr (std::is_same_v<TData, double>)
197 {
198 magmablas_dgemv_batched_strided(trans, M, N, alpha, a, lda, strideA, x,
199 incx, strideX, beta, y, incy, strideY,
200 batchSize, queue);
201 }
202}
203
204template <typename TData>
205void GeamStridedBatched(Handle<NektarSpaces::Device> handle,
206 std::string transposeA, std::string transposeB,
207 const int M, const int N, const TData alpha,
208 const TData *a, const int lda, const int strideA,
209 const TData beta, const TData *b, const int ldb,
210 const int strideB, TData *c, const int ldc,
211 const int strideC, const int batchSize)
212{
213#if defined(NEKTAR_ENABLE_CUDA)
214 detail::CuBlasLtGeamStridedBatched(handle, transposeA, transposeB, M, N,
215 alpha, a, lda, strideA, beta, b, ldb,
216 strideB, c, ldc, strideC, batchSize);
217#elif defined(NEKTAR_ENABLE_HIP)
218 auto queue = GetMagmaQueue(handle.GetStreamID());
219 auto hipblasHandle = magma_queue_get_hipblas_handle(queue);
220 auto transA = (transposeA == "N") ? HIPBLAS_OP_N : HIPBLAS_OP_T;
221 auto transB = (transposeB == "N") ? HIPBLAS_OP_N : HIPBLAS_OP_T;
222
223 if constexpr (std::is_same_v<TData, float>)
224 {
225 HIPBLAS_CHECK(hipblasSgeamStridedBatched(
226 hipblasHandle, transA, transB, M, N, &alpha, a, lda, strideA, &beta,
227 b, ldb, strideB, c, ldc, strideC, batchSize));
228 }
229 else if constexpr (std::is_same_v<TData, double>)
230 {
231 HIPBLAS_CHECK(hipblasDgeamStridedBatched(
232 hipblasHandle, transA, transB, M, N, &alpha, a, lda, strideA, &beta,
233 b, ldb, strideB, c, ldc, strideC, batchSize));
234 }
235#endif
236}
237
238template void Gemm<float>(Handle<NektarSpaces::Device> handle,
239 std::string transposeA, std::string transposeB,
240 const int M, const int N, const int K,
241 const float alpha, const float *a, const int lda,
242 const float *b, const int ldb, const float beta,
243 float *c, const int ldc);
244
245template void Gemm<double>(Handle<NektarSpaces::Device> handle,
246 std::string transposeA, std::string transposeB,
247 const int M, const int N, const int K,
248 const double alpha, const double *a, const int lda,
249 const double *b, const int ldb, const double beta,
250 double *c, const int ldc);
251
252template void GemmStridedBatched<float>(
253 Handle<NektarSpaces::Device> handle, std::string transposeA,
254 std::string transposeB, const int M, const int N, const int K,
255 const float alpha, const float *a, const int lda, const int strideA,
256 const float *b, const int ldb, const int strideB, const float beta,
257 float *c, const int ldc, const int strideC, const int batchSize);
258
259template void GemmStridedBatched<double>(
260 Handle<NektarSpaces::Device> handle, std::string transposeA,
261 std::string transposeB, const int M, const int N, const int K,
262 const double alpha, const double *a, const int lda, const int strideA,
263 const double *b, const int ldb, const int strideB, const double beta,
264 double *c, const int ldc, const int strideC, const int batchSize);
265
266template void Gemv<float>(Handle<NektarSpaces::Device> handle,
267 std::string transpose, const int M, const int N,
268 const float alpha, const float *a, const int lda,
269 const float *x, const int incx, const float beta,
270 float *y, const int incy);
271
272template void Gemv<double>(Handle<NektarSpaces::Device> handle,
273 std::string transpose, const int M, const int N,
274 const double alpha, const double *a, const int lda,
275 const double *x, const int incx, const double beta,
276 double *y, const int incy);
277
278template void GemvStridedBatched<float>(
279 Handle<NektarSpaces::Device> handle, std::string transpose, const int M,
280 const int N, const float alpha, const float *a, const int lda,
281 const int strideA, const float *x, const int incx, const int strideX,
282 const float beta, float *y, const int incy, const int strideY,
283 const int batchSize);
284
285template void GemvStridedBatched<double>(
286 Handle<NektarSpaces::Device> handle, std::string transpose, const int M,
287 const int N, const double alpha, const double *a, const int lda,
288 const int strideA, const double *x, const int incx, const int strideX,
289 const double beta, double *y, const int incy, const int strideY,
290 const int batchSize);
291
292template void GeamStridedBatched<float>(
293 Handle<NektarSpaces::Device> handle, std::string transposeA,
294 std::string transposeB, const int M, const int N, const float alpha,
295 const float *a, const int lda, const int strideA, const float beta,
296 const float *b, const int ldb, const int strideB, float *c, const int ldc,
297 const int strideC, const int batchSize);
298
299template void GeamStridedBatched<double>(
300 Handle<NektarSpaces::Device> handle, std::string transposeA,
301 std::string transposeB, const int M, const int N, const double alpha,
302 const double *a, const int lda, const int strideA, const double beta,
303 const double *b, const int ldb, const int strideB, double *c, const int ldc,
304 const int strideC, const int batchSize);
305} // namespace Nektar::NekBlas
#define CUBLAS_CHECK(condition)
Definition CuBlasLt.hpp:50
#define N
static cudaStream_t & GetInstance(unsigned int id)
static hipStream_t & GetInstance(unsigned int id)
Definition HIPStream.hpp:85
#define HIPBLAS_CHECK(condition)
Definition hipBlas.cpp:48
@ beta
Gauss Radau pinned at x=-1,.
Definition PointsType.h:59
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
template void Gemv< float >(Handle< NektarSpaces::Serial > handle, std::string transpose, const int M, const int N, const float alpha, const float *a, const int lda, const float *x, const int incx, const float beta, float *y, const int incy)
void GemmStridedBatched(Handle< NektarSpaces::Serial > handle, std::string transposeA, std::string transposeB, const int M, const int N, const int K, const TData alpha, const TData *a, const int lda, const int strideA, const TData *b, const int ldb, const int strideB, const TData beta, TData *c, const int ldc, const int strideC, const int batchSize)
template void Gemm< double >(Handle< NektarSpaces::Serial > handle, std::string transposeA, std::string transposeB, const int M, const int N, const int K, const double alpha, const double *a, const int lda, const double *b, const int ldb, const double beta, double *c, const int ldc)
template void GeamStridedBatched< float >(Handle< NektarSpaces::Serial > handle, std::string transposeA, std::string transposeB, const int M, const int N, const float alpha, const float *a, const int lda, const int strideA, const float beta, const float *b, const int ldb, const int strideB, float *c, const int ldc, const int strideC, const int batchSize)
template void GemvStridedBatched< float >(Handle< NektarSpaces::Serial > handle, std::string transpose, const int M, const int N, const float alpha, const float *a, const int lda, const int strideA, const float *x, const int incx, const int strideX, const float beta, float *y, const int incy, const int strideY, const int batchSize)
template void Gemm< float >(Handle< NektarSpaces::Serial > handle, std::string transposeA, std::string transposeB, const int M, const int N, const int K, const float alpha, const float *a, const int lda, const float *b, const int ldb, const float beta, float *c, const int ldc)
void GeamStridedBatched(Handle< NektarSpaces::Serial >, std::string, std::string, const int, const int, const TData, const TData *, const int, const int, const TData, const TData *, const int, const int, TData *, const int, const int, const int)
template void Gemv< double >(Handle< NektarSpaces::Serial > handle, std::string transpose, const int M, const int N, const double alpha, const double *a, const int lda, const double *x, const int incx, const double beta, double *y, const int incy)
template void GeamStridedBatched< double >(Handle< NektarSpaces::Serial > handle, std::string transposeA, std::string transposeB, const int M, const int N, const double alpha, const double *a, const int lda, const int strideA, const double beta, const double *b, const int ldb, const int strideB, double *c, const int ldc, const int strideC, const int batchSize)
template void GemmStridedBatched< double >(Handle< NektarSpaces::Serial > handle, std::string transposeA, std::string transposeB, const int M, const int N, const int K, const double alpha, const double *a, const int lda, const int strideA, const double *b, const int ldb, const int strideB, const double beta, double *c, const int ldc, const int strideC, const int batchSize)
template void GemmStridedBatched< float >(Handle< NektarSpaces::Serial > handle, std::string transposeA, std::string transposeB, const int M, const int N, const int K, const float alpha, const float *a, const int lda, const int strideA, const float *b, const int ldb, const int strideB, const float beta, float *c, const int ldc, const int strideC, const int batchSize)
void GemvStridedBatched(Handle< NektarSpaces::Serial > handle, std::string transpose, const int M, const int N, const TData alpha, const TData *a, const int lda, const int strideA, const TData *x, const int incx, const int strideX, const TData beta, TData *y, const int incy, const int strideY, const int batchSize)
void Gemv(Handle< NektarSpaces::Serial > handle, std::string transpose, const int M, const int N, const TData alpha, const TData *a, const int lda, const TData *x, const int incx, const TData beta, TData *y, const int incy)
template void GemvStridedBatched< double >(Handle< NektarSpaces::Serial > handle, std::string transpose, const int M, const int N, const double alpha, const double *a, const int lda, const int strideA, const double *x, const int incx, const int strideX, const double beta, double *y, const int incy, const int strideY, const int batchSize)
void Gemm(Handle< NektarSpaces::Serial > handle, std::string transposeA, std::string transposeB, const int M, const int N, const int K, const TData alpha, const TData *a, const int lda, const TData *b, const int ldb, const TData beta, TData *c, const int ldc)