mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
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 #include <cuda_runtime.h>
31 #include <cublas_v2.h>
32 
33 namespace mx
34 {
35 namespace cuda
36 {
37 
38 /// Multiplies a vector by a scalar, overwriting the vector with the result.
39 /** Implements
40  * \f[
41  * \vec{x} = \alpha \vec{x}
42  * \f]
43  *
44  * Specializations are provided for float, double, complex-float, and complex-double
45  *
46  * \tparam floatT a floating-point type, either float, double, complex-float, or complex-double
47  *
48  * \test Scenario: scaling a vector with cublas \ref test_math_templateCublas_scal "[test doc]"
49  *
50  * \ingroup cublas
51  */
52 template<typename floatT>
53 cublasStatus_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  * \test Scenario: scaling and accumulating a vector with cublas \ref test_math_templateCublas_axpy "[test doc]"
71  *
72  * \ingroup cublas
73  */
74 template<typename floatT>
75 cublasStatus_t cublasTaxpy( cublasHandle_t handle, ///< [in] handle to the cuBLAS library context.
76  int n, ///< [in] scalar used for multiplication.
77  const floatT *alpha, ///< [in] number of elements in the vector x and y
78  const floatT *x, ///< [in] vector with n elements.
79  int incx, ///< [in] stride between consecutive elements of x
80  floatT *y, ///< [in.out] vector with n elements.
81  int incy ///< [in] stride between consecutive elements of y
82  );
83 
84 //----------------------------------------------------
85 // Element-wise (Hadamard) products of vectors
86 
87 
88 /// Calculates the element-wise product of two vectors, storing the result in the first.
89 /** Calculates
90  * \f$
91  * x = x * y
92  * \f$
93  * element by element, a.k.a. the Hadamard product.
94  *
95  * Specializations are provided for:
96  * - float,float
97  * - complex-float, float
98  * - complex-float, complex-float
99  * - double, double
100  * - complex-double, double
101  * - complex-double, complex-double
102  *
103  * \test Scenario: multiplying two vectors element by element \ref test_math_templateCublas_elementwiseXxY "[test doc]"
104  *
105  * \ingroup cublas
106  */
107 template<typename dataT1, typename dataT2>
108 cudaError_t elementwiseXxY( dataT1 * x, ///< [in.out] device pointer for the 1st vector. Is replaced with the product of the two vectors
109  dataT2 * y, ///< [in] device pointer for the 2nd vector.
110  int size ///< [in] the number of elements in the vectors.
111  );
112 
113 //----------------------------------------------------
114 // Tgemv
115 
116 /// Perform a matrix-vector multiplication.
117 /** Implements
118  * \f[
119  * \vec{y} = \alpha \mathbf{A} \vec{x} + \beta \vec{y}
120  * \f]
121  *
122  * Specializations are provided for float, double, complex-float, and complex-double
123  *
124  * \tparam floatT a floating-point type, either float, double, complex-float, or complex-double
125  *
126  * \tests Scenario: multiplying a vectory by a matrix \ref test_math_templateCublas_cublasTgemv_inc "[code doc]"
127  *
128  * \ingroup cublas
129  */
130 template<typename floatT>
131 cublasStatus_t cublasTgemv( cublasHandle_t handle, ///< [in] handle to the cuBLAS library context.
132  cublasOperation_t trans, ///< [in] operation on a, CUBLAS_OP_N for none, and CUBLAS_OP_T for transpose
133  int m, ///< [in] rows in matrix A.
134  int n, ///< [in] columns in matrix A.
135  const floatT *alpha, ///< [in] scalar used for multiplication of A
136  const floatT *A, ///< [in] array of dimension lda x n with lda >= max(1,m). The leading m by n part of the array A is multiplied by alpha and x. Unchanged.
137  int lda, ///< [in] leading dimension of A. lda must be at least max(1,m).
138  const floatT *x, ///< [in] vector of at least (1+(n-1)*abs(incx)) elements if transa==CUBLAS_OP_N and at least (1+(m-1)*abs(incx)) elements otherwise.
139  int incx, ///< [in] stride of x.
140  const floatT *beta, ///< [in] scalar used for multiplication of y, if beta==0 then y does not need to be initialized.
141  floatT *y, ///< [in.out] vector of at least (1+(m-1)*abs(incy)) elements if transa==CUBLAS_OP_N and at least (1+(n-1)*abs(incy)) elements otherwise.
142  int incy ///< [in] stride of y
143  );
144 
145 /// Perform a matrix-vector multiplication for stride-less arrays
146 /** Implements
147  * \f[
148  * \vec{y} = \alpha \mathbf{A} \vec{x} + \beta \vec{y}
149  * \f]
150  *
151  * Specializations are provided for float, double, complex-float, and complex-double
152  *
153  * \overload
154  * This version assumes stride is 1 in all arrays.
155  *
156  * \tparam floatT a floating-point type, either float, double, complex-float, or complex-double
157  *
158  * \ingroup cublas
159  */
160 template<typename floatT>
161 cublasStatus_t cublasTgemv( cublasHandle_t handle, ///< [in] handle to the cuBLAS library context.
162  cublasOperation_t trans, ///< [in] operation on a, CUBLAS_OP_N for none, and CUBLAS_OP_T for transpose
163  int m, ///< [in] rows in matrix A.
164  int n, ///< [in] columns in matrix A.
165  const floatT *alpha, ///< [in] scalar used for multiplication of A
166  const floatT *A, ///< [in] array of dimension m x n. Unchanged.
167  const floatT *x, ///< [in] vector of at least (1+(n-1)*abs(incx)) elements if transa==CUBLAS_OP_N and at least (1+(m-1)*abs(incx)) elements otherwise.
168  const floatT *beta, ///< [in] scalar used for multiplication of y, if beta==0 then y does not need to be initialized.
169  floatT *y ///< [in.out] vector of at least (1+(m-1)*abs(incy)) elements if transa==CUBLAS_OP_N and at least (1+(n-1)*abs(incy)) elements otherwise.
170  );
171 
172 template<>
173 cublasStatus_t cublasTgemv<float>( cublasHandle_t handle,
174  cublasOperation_t trans,
175  int m,
176  int n,
177  const float *alpha,
178  const float *A,
179  int lda,
180  const float *x,
181  int incx,
182  const float *beta,
183  float *y,
184  int incy
185  );
186 
187 template<>
188 cublasStatus_t cublasTgemv<double>( cublasHandle_t handle,
189  cublasOperation_t trans,
190  int m,
191  int n,
192  const double *alpha,
193  const double *A,
194  int lda,
195  const double *x,
196  int incx,
197  const double *beta,
198  double *y,
199  int incy
200  );
201 
202 template<>
203 cublasStatus_t cublasTgemv<float>( cublasHandle_t handle,
204  cublasOperation_t trans,
205  int m,
206  int n,
207  const float *alpha,
208  const float *A,
209  const float *x,
210  const float *beta,
211  float *y
212  );
213 
214 template<>
215 cublasStatus_t cublasTgemv<double>( cublasHandle_t handle,
216  cublasOperation_t trans,
217  int m,
218  int n,
219  const double *alpha,
220  const double *A,
221  const double *x,
222  const double *beta,
223  double *y
224  );
225 
226 }//namespace cuda
227 }//namespace mx
228 #endif // math_templateCublas_hpp
cublasStatus_t cublasTgemv(cublasHandle_t handle, cublasOperation_t trans, int m, int n, const floatT *alpha, const floatT *A, const floatT *x, const floatT *beta, floatT *y)
Perform a matrix-vector multiplication for stride-less arrays.
cudaError_t elementwiseXxY(dataT1 *x, dataT2 *y, int size)
Calculates the element-wise product of two vectors, storing the result in the first.
cublasStatus_t cublasTaxpy(cublasHandle_t handle, int n, const floatT *alpha, const floatT *x, int incx, floatT *y, int incy)
Multiplies a vector by a scalar, adding it to a second vector which is overwritten by the result.
cublasStatus_t cublasTscal(cublasHandle_t handle, int n, const floatT *alpha, floatT *x, int incx)
Multiplies a vector by a scalar, overwriting the vector with the result.
The mxlib c++ namespace.
Definition: mxError.hpp:107