Nektar++
Loading...
Searching...
No Matches
xsmm.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: xsmm.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 AVX execution space (LibXSMM).
32// Vendor headers are included here, never in NekBlas.hpp.
33//
34///////////////////////////////////////////////////////////////////////////////
35
37
38#include <stdexcept>
39
40#include <libxsmm.h>
41
42namespace Nektar::NekBlas
43{
44// NekBlas does not link LibUtilities, so it cannot use ErrorUtil.
45static inline void XsmmCheck(const bool condition, const char *msg)
46{
47 if (!condition)
48 {
49 throw std::runtime_error(msg);
50 }
51}
52
53template <typename TData>
54void Gemm([[maybe_unused]] Handle<NektarSpaces::AVX> handle,
55 std::string transposeA, std::string transposeB, const int M,
56 const int N, const int K, const TData alpha, const TData *a,
57 const int lda, const TData *b, const int ldb, const TData beta,
58 TData *c, const int ldc)
59{
60 XsmmCheck(transposeA == "N" && transposeB == "N",
61 "libxsmm: matrix transpose is not supported");
62 XsmmCheck(alpha == 1.0, "libxsmm: alpha must be equal to 1.0");
63 XsmmCheck(beta == 0.0 || beta == 1.0,
64 "libxsmm: beta must be equal to 0.0 or 1.0");
65 if (alpha != 1.0)
66 {
67 XsmmCheck(beta == 0.0,
68 "libxsmm: beta must be equal to 0.0 when alpha != 1.0");
69 }
70
71 // Dispatch kernel.
72 int lda0 = static_cast<int>(lda);
73 int ldb0 = static_cast<int>(ldb);
74 int ldc0 = static_cast<int>(ldc);
75 libxsmm_gemm(nullptr, nullptr, M, N, K, &alpha, a, &lda0, b, &ldb0, &beta,
76 c, &ldc0);
77 if (alpha != 1.0 && beta == 0.0)
78 {
79 for (int i = 0; i < M * N; i++)
80 {
81 c[i] *= alpha;
82 }
83 }
84}
85
86template <typename TData>
87void GemmStridedBatched([[maybe_unused]] Handle<NektarSpaces::AVX> handle,
88 std::string transposeA, std::string transposeB,
89 const int M, const int N, const int K,
90 const TData alpha, const TData *a, const int lda,
91 const int strideA, const TData *b, const int ldb,
92 const int strideB, const TData beta, TData *c,
93 const int ldc, const int strideC, const int batchSize)
94{
95 XsmmCheck(transposeA == "N" && transposeB == "N",
96 "libxsmm: matrix transpose is not supported");
97 XsmmCheck(alpha == 1.0, "libxsmm: alpha must be equal to 1.0");
98 XsmmCheck(beta == 0.0 || beta == 1.0,
99 "libxsmm: beta must be equal to 0.0 or 1.0");
100 if (alpha != 1.0)
101 {
102 XsmmCheck(beta == 0.0,
103 "libxsmm: beta must be equal to 0.0 when alpha != 1.0");
104 }
105
106 // Dispatch kernel.
107 int lda0 = static_cast<int>(lda);
108 int ldb0 = static_cast<int>(ldb);
109 int ldc0 = static_cast<int>(ldc);
110 for (int i = 0; i < batchSize; i++)
111 {
112 libxsmm_gemm(nullptr, nullptr, M, N, K, &alpha, a + strideA * i, &lda0,
113 b + strideB * i, &ldb0, &beta, c + strideC * i, &ldc0);
114 if (alpha != 1.0 && beta == 0.0)
115 {
116 for (int j = 0; j < M * N; j++)
117 {
118 c[strideC * i + j] *= alpha;
119 }
120 }
121 }
122}
123
124template <typename TData>
125void Gemv(Handle<NektarSpaces::AVX>, std::string transpose, const int M,
126 const int N, const TData alpha, const TData *A, const int lda,
127 const TData *x, const int incx, const TData beta, TData *y,
128 const int incy)
129{
130 XsmmCheck(transpose == "N", "libxsmm: transpose not supported in Gemv");
131 XsmmCheck(incx == 1 && incy == 1, "libxsmm: Gemv needs unit increments");
132 XsmmCheck(alpha == 1.0, "libxsmm: alpha must be equal to 1.0");
133 XsmmCheck(beta == 0.0 || beta == 1.0,
134 "libxsmm: beta must be equal to 0.0 or 1.0");
135
136 // Gemv is just Gemm with a single column vector
137 int lda0 = lda;
138 int ldx0 = N;
139 int ldy0 = M;
140 int n = 1;
141 libxsmm_gemm(nullptr, nullptr, M, n, N, &alpha, A, &lda0, x, &ldx0, &beta,
142 y, &ldy0);
143}
144
145template <typename TData>
147 std::string transpose, const int M, const int N,
148 const TData alpha, const TData *a, const int lda,
149 const int strideA, const TData *x, const int incx,
150 const int strideX, const TData beta, TData *y,
151 const int incy, const int strideY, const int batchSize)
152{
153 XsmmCheck(transpose == "N", "libxsmm: transpose not supported in Gemv");
154 XsmmCheck(incx == 1 && incy == 1, "libxsmm: Gemv needs unit increments");
155 XsmmCheck(alpha == 1.0, "libxsmm: alpha must be equal to 1.0");
156 XsmmCheck(beta == 0.0 || beta == 1.0,
157 "libxsmm: beta must be equal to 0.0 or 1.0");
158
159 for (int i = 0; i < batchSize; i++)
160 {
161 // Gemv is just Gemm with a single column vector
162 int lda0 = lda;
163 int ldx0 = N;
164 int ldy0 = M;
165 int n = 1;
166 libxsmm_gemm(nullptr, nullptr, M, n, N, &alpha, a + strideA * i, &lda0,
167 x + strideX * i, &ldx0, &beta, y + strideY * i, &ldy0);
168 }
169}
170
171template <typename TData>
172void GeamStridedBatched(Handle<NektarSpaces::AVX>, std::string, std::string,
173 const int, const int, const TData, const TData *,
174 const int, const int, const TData, const TData *,
175 const int, const int, TData *, const int, const int,
176 const int)
177{
178 throw std::runtime_error("GeamStridedBatched is not available for libxsmm");
179}
180
182 std::string transposeA, std::string transposeB,
183 const int M, const int N, const int K,
184 const float alpha, const float *a, const int lda,
185 const float *b, const int ldb, const float beta,
186 float *c, const int ldc);
187
189 std::string transposeA, std::string transposeB,
190 const int M, const int N, const int K,
191 const double alpha, const double *a, const int lda,
192 const double *b, const int ldb, const double beta,
193 double *c, const int ldc);
194
196 Handle<NektarSpaces::AVX> handle, std::string transposeA,
197 std::string transposeB, const int M, const int N, const int K,
198 const float alpha, const float *a, const int lda, const int strideA,
199 const float *b, const int ldb, const int strideB, const float beta,
200 float *c, const int ldc, const int strideC, const int batchSize);
201
203 Handle<NektarSpaces::AVX> handle, std::string transposeA,
204 std::string transposeB, const int M, const int N, const int K,
205 const double alpha, const double *a, const int lda, const int strideA,
206 const double *b, const int ldb, const int strideB, const double beta,
207 double *c, const int ldc, const int strideC, const int batchSize);
208
209template void Gemv<float>(Handle<NektarSpaces::AVX>, std::string, int, int,
210 float, const float *, int, const float *, int, float,
211 float *, int);
212
213template void Gemv<double>(Handle<NektarSpaces::AVX>, std::string, int, int,
214 double, const double *, int, const double *, int,
215 double, double *, int);
216
218 Handle<NektarSpaces::AVX> handle, std::string transpose, const int M,
219 const int N, const float alpha, const float *a, const int lda,
220 const int strideA, const float *x, const int incx, const int strideX,
221 const float beta, float *y, const int incy, const int strideY,
222 const int batchSize);
223
225 Handle<NektarSpaces::AVX> handle, std::string transpose, const int M,
226 const int N, const double alpha, const double *a, const int lda,
227 const int strideA, const double *x, const int incx, const int strideX,
228 const double beta, double *y, const int incy, const int strideY,
229 const int batchSize);
230
232 Handle<NektarSpaces::AVX> handle, std::string transposeA,
233 std::string transposeB, const int M, const int N, const float alpha,
234 const float *a, const int lda, const int strideA, const float beta,
235 const float *b, const int ldb, const int strideB, float *c, const int ldc,
236 const int strideC, const int batchSize);
237
239 Handle<NektarSpaces::AVX> handle, std::string transposeA,
240 std::string transposeB, const int M, const int N, const double alpha,
241 const double *a, const int lda, const int strideA, const double beta,
242 const double *b, const int ldb, const int strideB, double *c, const int ldc,
243 const int strideC, const int batchSize);
244} // namespace Nektar::NekBlas
#define N
@ 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)
static void XsmmCheck(const bool condition, const char *msg)
Definition xsmm.cpp:45
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)