Nektar++
Loading...
Searching...
No Matches
hipBlas.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: hipBlas.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 (hipBLAS).
32// Vendor headers are included here, never in NekBlas.hpp.
33//
34///////////////////////////////////////////////////////////////////////////////
35
37
39
40#include <cstdio>
41#include <cstdlib>
42#include <iostream>
43#include <type_traits>
44#include <unordered_map>
45
46#include <hipblas/hipblas.h>
47
48#define HIPBLAS_CHECK(condition) \
49 { \
50 const hipblasStatus_t status = condition; \
51 if (status != HIPBLAS_STATUS_SUCCESS) \
52 { \
53 std::cerr << "hipBLAS error encountered: \"" \
54 << hipblasStatusToString(status) << "\" at " << __FILE__ \
55 << ':' << __LINE__ << std::endl; \
56 exit(0); \
57 } \
58 }
59
60namespace Nektar::NekBlas
61{
62namespace
63{
64/**
65 * @brief The hipBLAS handle of stream @p streamID, created on first use.
66 *
67 * One handle per stream: the kernels a handle launches share its device
68 * workspace, so one handle moved from stream to stream lets calls in flight
69 * on two streams, such as the per-block products of the MultiField helpers,
70 * use the same workspace concurrently.
71 */
72hipblasHandle_t GetHipBlasHandle(const unsigned int streamID)
73{
74 static std::unordered_map<unsigned int, hipblasHandle_t> handles;
75
76 auto it = handles.find(streamID);
77 if (it == handles.end())
78 {
79 hipblasHandle_t handle = nullptr;
80 if (hipblasCreate(&handle) != HIPBLAS_STATUS_SUCCESS)
81 {
82 printf("hipBLAS initialization failed\n");
83 }
85 hipblasSetStream(handle, HIPStream::GetInstance(streamID)));
86 it = handles.emplace(streamID, handle).first;
87 }
88
89 return it->second;
90}
91} // namespace
92
93template <typename TData>
94void Gemm(Handle<NektarSpaces::Device> handle, std::string transposeA,
95 std::string transposeB, const int M, const int N, const int K,
96 const TData alpha, const TData *a, const int lda, const TData *b,
97 const int ldb, const TData beta, TData *c, const int ldc)
98{
99 auto hipblasHandle = GetHipBlasHandle(handle.GetStreamID());
100
101 auto transA = (transposeA == "N") ? HIPBLAS_OP_N : HIPBLAS_OP_T;
102 auto transB = (transposeB == "N") ? HIPBLAS_OP_N : HIPBLAS_OP_T;
103
104 if constexpr (std::is_same_v<TData, float>)
105 {
106 HIPBLAS_CHECK(hipblasSgemm(hipblasHandle, transA, transB, M, N, K,
107 &alpha, a, lda, b, ldb, &beta, c, ldc));
108 }
109 else if constexpr (std::is_same_v<TData, double>)
110 {
111 HIPBLAS_CHECK(hipblasDgemm(hipblasHandle, transA, transB, M, N, K,
112 &alpha, a, lda, b, ldb, &beta, c, ldc));
113 }
114}
115
116template <typename TData>
117void GemmStridedBatched(Handle<NektarSpaces::Device> handle,
118 std::string transposeA, std::string transposeB,
119 const int M, const int N, const int K,
120 const TData alpha, const TData *a, const int lda,
121 const int strideA, const TData *b, const int ldb,
122 const int strideB, const TData beta, TData *c,
123 const int ldc, const int strideC, const int batchSize)
124{
125 auto hipblasHandle = GetHipBlasHandle(handle.GetStreamID());
126
127 auto transA = (transposeA == "N") ? HIPBLAS_OP_N : HIPBLAS_OP_T;
128 auto transB = (transposeB == "N") ? HIPBLAS_OP_N : HIPBLAS_OP_T;
129
130 if constexpr (std::is_same_v<TData, float>)
131 {
132 HIPBLAS_CHECK(hipblasSgemmStridedBatched(
133 hipblasHandle, transA, transB, M, N, K, &alpha, a, lda, strideA, b,
134 ldb, strideB, &beta, c, ldc, strideC, batchSize));
135 }
136 else if constexpr (std::is_same_v<TData, double>)
137 {
138 HIPBLAS_CHECK(hipblasDgemmStridedBatched(
139 hipblasHandle, transA, transB, M, N, K, &alpha, a, lda, strideA, b,
140 ldb, strideB, &beta, c, ldc, strideC, batchSize));
141 }
142}
143
144template <typename TData>
145void Gemv(Handle<NektarSpaces::Device> handle, std::string transpose,
146 const int M, const int N, const TData alpha, const TData *a,
147 const int lda, const TData *x, const int incx, const TData beta,
148 TData *y, const int incy)
149{
150 auto hipblasHandle = GetHipBlasHandle(handle.GetStreamID());
151
152 auto trans = (transpose == "N") ? HIPBLAS_OP_N : HIPBLAS_OP_T;
153
154 if constexpr (std::is_same_v<TData, float>)
155 {
156 HIPBLAS_CHECK(hipblasSgemv(hipblasHandle, trans, M, N, &alpha, a, lda,
157 x, incx, &beta, y, incy));
158 }
159 else if constexpr (std::is_same_v<TData, double>)
160 {
161 HIPBLAS_CHECK(hipblasDgemv(hipblasHandle, trans, M, N, &alpha, a, lda,
162 x, incx, &beta, y, incy));
163 }
164}
165
166template <typename TData>
167void GemvStridedBatched(Handle<NektarSpaces::Device> handle,
168 std::string transpose, const int M, const int N,
169 const TData alpha, const TData *a, const int lda,
170 const int strideA, const TData *x, const int incx,
171 const int strideX, const TData beta, TData *y,
172 const int incy, const int strideY, const int batchSize)
173{
174 auto hipblasHandle = GetHipBlasHandle(handle.GetStreamID());
175
176 auto trans = (transpose == "N") ? HIPBLAS_OP_N : HIPBLAS_OP_T;
177
178 if constexpr (std::is_same_v<TData, float>)
179 {
180 HIPBLAS_CHECK(hipblasSgemvStridedBatched(
181 hipblasHandle, trans, M, N, &alpha, a, lda, strideA, x, incx,
182 strideX, &beta, y, incy, strideY, batchSize));
183 }
184 else if constexpr (std::is_same_v<TData, double>)
185 {
186 HIPBLAS_CHECK(hipblasDgemvStridedBatched(
187 hipblasHandle, trans, M, N, &alpha, a, lda, strideA, x, incx,
188 strideX, &beta, y, incy, strideY, batchSize));
189 }
190}
191
192template <typename TData>
193void GeamStridedBatched(Handle<NektarSpaces::Device> handle,
194 std::string transposeA, std::string transposeB,
195 const int M, const int N, const TData alpha,
196 const TData *a, const int lda, const int strideA,
197 const TData beta, const TData *b, const int ldb,
198 const int strideB, TData *c, const int ldc,
199 const int strideC, const int batchSize)
200{
201 auto hipblasHandle = GetHipBlasHandle(handle.GetStreamID());
202
203 auto transA = (transposeA == "N") ? HIPBLAS_OP_N : HIPBLAS_OP_T;
204 auto transB = (transposeB == "N") ? HIPBLAS_OP_N : HIPBLAS_OP_T;
205
206 if constexpr (std::is_same_v<TData, float>)
207 {
208 HIPBLAS_CHECK(hipblasSgeamStridedBatched(
209 hipblasHandle, transA, transB, M, N, &alpha, a, lda, strideA, &beta,
210 b, ldb, strideB, c, ldc, strideC, batchSize));
211 }
212 else if constexpr (std::is_same_v<TData, double>)
213 {
214 HIPBLAS_CHECK(hipblasDgeamStridedBatched(
215 hipblasHandle, transA, transB, M, N, &alpha, a, lda, strideA, &beta,
216 b, ldb, strideB, c, ldc, strideC, batchSize));
217 }
218}
219
220template void Gemm<float>(Handle<NektarSpaces::Device> handle,
221 std::string transposeA, std::string transposeB,
222 const int M, const int N, const int K,
223 const float alpha, const float *a, const int lda,
224 const float *b, const int ldb, const float beta,
225 float *c, const int ldc);
226
227template void Gemm<double>(Handle<NektarSpaces::Device> handle,
228 std::string transposeA, std::string transposeB,
229 const int M, const int N, const int K,
230 const double alpha, const double *a, const int lda,
231 const double *b, const int ldb, const double beta,
232 double *c, const int ldc);
233
234template void GemmStridedBatched<float>(
235 Handle<NektarSpaces::Device> handle, std::string transposeA,
236 std::string transposeB, const int M, const int N, const int K,
237 const float alpha, const float *a, const int lda, const int strideA,
238 const float *b, const int ldb, const int strideB, const float beta,
239 float *c, const int ldc, const int strideC, const int batchSize);
240
241template void GemmStridedBatched<double>(
242 Handle<NektarSpaces::Device> handle, std::string transposeA,
243 std::string transposeB, const int M, const int N, const int K,
244 const double alpha, const double *a, const int lda, const int strideA,
245 const double *b, const int ldb, const int strideB, const double beta,
246 double *c, const int ldc, const int strideC, const int batchSize);
247
248template void Gemv<float>(Handle<NektarSpaces::Device> handle,
249 std::string transpose, const int M, const int N,
250 const float alpha, const float *a, const int lda,
251 const float *x, const int incx, const float beta,
252 float *y, const int incy);
253
254template void Gemv<double>(Handle<NektarSpaces::Device> handle,
255 std::string transpose, const int M, const int N,
256 const double alpha, const double *a, const int lda,
257 const double *x, const int incx, const double beta,
258 double *y, const int incy);
259
260template void GemvStridedBatched<float>(
261 Handle<NektarSpaces::Device> handle, std::string transpose, const int M,
262 const int N, const float alpha, const float *a, const int lda,
263 const int strideA, const float *x, const int incx, const int strideX,
264 const float beta, float *y, const int incy, const int strideY,
265 const int batchSize);
266
267template void GemvStridedBatched<double>(
268 Handle<NektarSpaces::Device> handle, std::string transpose, const int M,
269 const int N, const double alpha, const double *a, const int lda,
270 const int strideA, const double *x, const int incx, const int strideX,
271 const double beta, double *y, const int incy, const int strideY,
272 const int batchSize);
273
274template void GeamStridedBatched<float>(
275 Handle<NektarSpaces::Device> handle, std::string transposeA,
276 std::string transposeB, const int M, const int N, const float alpha,
277 const float *a, const int lda, const int strideA, const float beta,
278 const float *b, const int ldb, const int strideB, float *c, const int ldc,
279 const int strideC, const int batchSize);
280
281template void GeamStridedBatched<double>(
282 Handle<NektarSpaces::Device> handle, std::string transposeA,
283 std::string transposeB, const int M, const int N, const double alpha,
284 const double *a, const int lda, const int strideA, const double beta,
285 const double *b, const int ldb, const int strideB, double *c, const int ldc,
286 const int strideC, const int batchSize);
287} // namespace Nektar::NekBlas
#define N
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
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)