mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
templateCublas.hpp
Go to the documentation of this file.
1/** \file templateCublas.hpp
2 * \author Jared R. Males
3 * \brief A template interface to cuBlas
4 * \ingroup cuda_files
5 *
6 */
7
8//***********************************************************************//
9// Copyright 2019,2020 Jared R. Males (jaredmales@gmail.com)
10//
11// This file is part of mxlib.
12//
13// mxlib is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// mxlib is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25//***********************************************************************//
26
27#ifndef math_templateCublas_hpp
28#define math_templateCublas_hpp
29
30#ifdef MXLIB_CUDA
31
32#include <cuda_runtime.h>
33#include <cublas_v2.h>
34
35namespace mx
36{
37namespace cuda
38{
39
40/// Multiplies a vector by a scalar, overwriting the vector with the result.
41/** Implements
42 * \f[
43 * \vec{x} = \alpha \vec{x}
44 * \f]
45 *
46 * Specializations are provided for float, double, complex-float, and complex-double
47 *
48 * \tparam floatT a floating-point type, either float, double, complex-float, or complex-double
49 *
50 * \ingroup cublas
51 */
52template <typename floatT>
53cublasStatus_t cublasTscal( cublasHandle_t handle, ///< [in] The cublas context handle
54 int n, ///< [in] Number of elements in the vector
55 const floatT *alpha, ///< [in] The scalar
56 floatT *x, ///< [in.out] The vector of length n
57 int incx ///< [in] The stride of the vector
58);
59
60/// Multiplies a vector by a scalar, adding it to a second vector which is overwritten by the result.
61/** Implements
62 * \f[
63 * \vec{y} = \alpha \vec{x} + \vec{y}
64 * \f]
65 *
66 * Specializations are provided for float, double, complex-float, and complex-double
67 *
68 * \tparam floatT a floating-point type, either float, double, complex-float, or complex-double
69 *
70 *
71 * \ingroup cublas
72 */
73template <typename floatT>
74cublasStatus_t cublasTaxpy( cublasHandle_t handle, ///< [in] handle to the cuBLAS library context.
75 int n, ///< [in] number of elements in the vector x and y
76 const floatT *alpha, ///< [in] scalar used for multiplication.
77 const floatT *x, ///< [in] vector with n elements.
78 int incx, ///< [in] stride between consecutive elements of x
79 floatT *y, ///< [in.out] vector with n elements.
80 int incy ///< [in] stride between consecutive elements of y
81);
82
83//----------------------------------------------------
84// Element-wise (Hadamard) products of vectors
85
86/// Calculates the element-wise product of two vectors, storing the result in the first.
87/** Calculates
88 * \f$
89 * x = x * y
90 * \f$
91 * element by element, a.k.a. the Hadamard product.
92 *
93 * Specializations are provided for:
94 * - float,float
95 * - complex-float, float
96 * - complex-float, complex-float
97 * - double, double
98 * - complex-double, double
99 * - complex-double, complex-double
100 *
101 * \ingroup cublas
102 */
103template <typename dataT1, typename dataT2>
104cudaError_t elementwiseXxY(
105 dataT1 *x, ///< [in.out] device pointer for the 1st vector. Is replaced with the product of the two vectors
106 dataT2 *y, ///< [in] device pointer for the 2nd vector.
107 int size ///< [in] the number of elements in the vectors.
108);
109
110/// Calculates the element-wise product of two vectors, storing the result in a third vector.
111/** Calculates
112 * \f$
113 * z = x * y
114 * \f$
115 * element by element, a.k.a. the Hadamard product.
116 *
117 * Specializations are provided for:
118 * - float, float,float
119 * - complex-float, complex-float, float
120 * - complex-float, complex-float, complex-float
121 * - double, double, double
122 * - complex-double, complex-double, double
123 * - complex-double, complex-double, complex-double
124 *
125 * \ingroup cublas
126 */
127template <typename dataT0, typename dataT1, typename dataT2>
128cudaError_t elementwiseXxY( dataT0 *z, /**< [out] device pointer for the result vector. Is filled in with the
129 product of the second two vectors*/
130 dataT1 *x, ///< [in] device pointer for the 1st vector.
131 dataT2 *y, ///< [in] device pointer for the 2nd vector.
132 int size ///< [in] the number of elements in the vectors.
133);
134
135/// Calculates the element-wise product of two vectors, accumulating the result in a third vector.
136/** Calculates
137 * \f$
138 * z += x * y
139 * \f$
140 * element by element, a.k.a. the Hadamard product of x and y.
141 *
142 * Specializations are provided for:
143 * - float, float,float
144 * - double, double, double
145 *
146 * \todo Complex overloads won't compile for some reason.
147 *
148 * \ingroup cublas
149 */
150template <typename dataT0, typename dataT1, typename dataT2>
151cudaError_t elementwiseXxYAccum( dataT0 *z, /**< [out] device pointer for the result vector. Is filled in with the
152 product of the second two vectors*/
153 dataT1 *x, ///< [in] device pointer for the 1st vector.
154 dataT2 *y, ///< [in] device pointer for the 2nd vector.
155 int size ///< [in] the number of elements in the vectors.
156);
157//----------------------------------------------------
158// Tgemv
159
160/// Perform a matrix-vector multiplication.
161/** Implements
162 * \f[
163 * \vec{y} = \alpha \mathbf{A} \vec{x} + \beta \vec{y}
164 * \f]
165 *
166 * Specializations are provided for float, double, complex-float, and complex-double
167 *
168 * \tparam floatT a floating-point type, either float, double, complex-float, or complex-double
169 *
170 * \ingroup cublas
171 */
172template <typename floatT>
173cublasStatus_t
174cublasTgemv( cublasHandle_t handle, /**< [in] handle to the cuBLAS library context. */
175 cublasOperation_t trans, /**< [in] operation on a, CUBLAS_OP_N for none, and CUBLAS_OP_T for transpose */
176 int m, /**< [in] [host] rows in matrix A. */
177 int n, /**< [in] [host] columns in matrix A. */
178 const floatT *alpha, /**< [in] [host/device] scalar used for multiplication of A */
179 const floatT *A, /**< [in] [device] vector of at least (1+(n-1)*abs(incx)) elements if
180 transa==CUBLAS_OP_N and at least (1+(m-1)*abs(incx))
181 elements otherwise. */
182 int lda, /**< [in] [host] leading dimension of A. lda must be at least max(1,m). */
183 const floatT *x, /**< [in] [device] vector of at least (1+(n-1)*abs(incx)) elements if
184 transa==CUBLAS_OP_N and at least (1+(m-1)*abs(incx))
185 elements otherwise. */
186 int incx, /**< [in] [host] stride of x. */
187 const floatT *beta, /**< [in] [host/device] scalar used for multiplication of y, if beta==0
188 then y does not need to be initialized.*/
189 floatT *y, /**< [in.out] [device] vector of at least (1+(m-1)*abs(incy)) elements
190 if transa==CUBLAS_OP_N and at
191 least (1+(n-1)*abs(incy)) elements otherwise.*/
192 int incy /**< [in] [host] stride of y */
193);
194
195/// Perform a matrix-vector multiplication for stride-less arrays
196/** Implements
197 * \f[
198 * \vec{y} = \alpha \mathbf{A} \vec{x} + \beta \vec{y}
199 * \f]
200 *
201 * Specializations are provided for float, double, complex-float, and complex-double
202 *
203 * \overload
204 * This version assumes stride is 1 in all arrays.
205 *
206 * \tparam floatT a floating-point type, either float, double, complex-float, or complex-double
207 *
208 * \ingroup cublas
209 */
210template <typename floatT>
211cublasStatus_t
212cublasTgemv( cublasHandle_t handle, /**< [in] handle to the cuBLAS library context. */
213 cublasOperation_t trans, /**< [in] operation on a, CUBLAS_OP_N for none, and CUBLAS_OP_T for transpose */
214 int m, /**< [in] rows in matrix A. */
215 int n, /**< [in] columns in matrix A. */
216 const floatT *alpha, /**< [in] scalar used for multiplication of A */
217 const floatT *A, /**< [in] [device] vector of at least (1+(n-1)*abs(incx)) elements if
218 transa==CUBLAS_OP_N and at least (1+(m-1)*abs(incx))
219 elements otherwise. */
220 const floatT *x, /**< [in] [device] vector of at least (1+(n-1)*abs(incx)) elements if
221 transa==CUBLAS_OP_N and at least (1+(m-1)*abs(incx))
222 elements otherwise. */
223 const floatT *beta, /**< [in] [host/device] scalar used for multiplication of y, if beta==0
224 then y does not need to be initialized.*/
225 floatT *y /**< [in/out] [device] vector of at least (1+(m-1)*abs(incy)) elements
226 if transa==CUBLAS_OP_N and at
227 least (1+(n-1)*abs(incy)) elements otherwise.*/
228);
229
230template <>
231cublasStatus_t cublasTgemv<float>( cublasHandle_t handle,
232 cublasOperation_t trans,
233 int m,
234 int n,
235 const float *alpha,
236 const float *A,
237 int lda,
238 const float *x,
239 int incx,
240 const float *beta,
241 float *y,
242 int incy );
243
244template <>
245cublasStatus_t cublasTgemv<double>( cublasHandle_t handle,
246 cublasOperation_t trans,
247 int m,
248 int n,
249 const double *alpha,
250 const double *A,
251 int lda,
252 const double *x,
253 int incx,
254 const double *beta,
255 double *y,
256 int incy );
257
258template <>
259cublasStatus_t cublasTgemv<float>( cublasHandle_t handle,
260 cublasOperation_t trans,
261 int m,
262 int n,
263 const float *alpha,
264 const float *A,
265 const float *x,
266 const float *beta,
267 float *y );
268
269template <>
270cublasStatus_t cublasTgemv<double>( cublasHandle_t handle,
271 cublasOperation_t trans,
272 int m,
273 int n,
274 const double *alpha,
275 const double *A,
276 const double *x,
277 const double *beta,
278 double *y );
279
280} // namespace cuda
281} // namespace mx
282
283#endif // MXLIB_CUDA
284
285#endif // math_templateCublas_hpp
The mxlib c++ namespace.
Definition mxlib.hpp:37