opm-simulators
cublas_wrapper.hpp
1 /*
2  Copyright 2022-2023 SINTEF AS
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
27 #ifndef OPM_CUBLASWRAPPER_HEADER_INCLUDED
28 #define OPM_CUBLASWRAPPER_HEADER_INCLUDED
29 #include <cublas_v2.h>
30 #include <opm/common/ErrorMacros.hpp>
31 
32 namespace Opm::gpuistl::detail
33 {
34 
35 inline cublasStatus_t
36 cublasScal(cublasHandle_t handle,
37  int n,
38  const double* alpha, /* host or device pointer */
39  double* x,
40  int incx)
41 {
42  return cublasDscal(handle,
43  n,
44  alpha, /* host or device pointer */
45  x,
46  incx);
47 }
48 
49 inline cublasStatus_t
50 cublasScal(cublasHandle_t handle,
51  int n,
52  const float* alpha, /* host or device pointer */
53  float* x,
54  int incx)
55 {
56  return cublasSscal(handle,
57  n,
58  alpha, /* host or device pointer */
59  x,
60  incx);
61 }
62 
63 inline cublasStatus_t
64 cublasScal([[maybe_unused]] cublasHandle_t handle,
65  [[maybe_unused]] int n,
66  [[maybe_unused]] const int* alpha, /* host or device pointer */
67  [[maybe_unused]] int* x,
68  [[maybe_unused]] int incx)
69 {
70  OPM_THROW(std::runtime_error, "cublasScal multiplication for integer vectors is not implemented yet.");
71 
72  // Avoid warning about missing return statement
73  return CUBLAS_STATUS_NOT_SUPPORTED;
74 }
75 inline cublasStatus_t
76 cublasAxpy(cublasHandle_t handle,
77  int n,
78  const double* alpha, /* host or device pointer */
79  const double* x,
80  int incx,
81  double* y,
82  int incy)
83 {
84  return cublasDaxpy(handle,
85  n,
86  alpha, /* host or device pointer */
87  x,
88  incx,
89  y,
90  incy);
91 }
92 
93 inline cublasStatus_t
94 cublasAxpy(cublasHandle_t handle,
95  int n,
96  const float* alpha, /* host or device pointer */
97  const float* x,
98  int incx,
99  float* y,
100  int incy)
101 {
102  return cublasSaxpy(handle,
103  n,
104  alpha, /* host or device pointer */
105  x,
106  incx,
107  y,
108  incy);
109 }
110 
111 inline cublasStatus_t
112 cublasAxpy([[maybe_unused]] cublasHandle_t handle,
113  [[maybe_unused]] int n,
114  [[maybe_unused]] const int* alpha, /* host or device pointer */
115  [[maybe_unused]] const int* x,
116  [[maybe_unused]] int incx,
117  [[maybe_unused]] int* y,
118  [[maybe_unused]] int incy)
119 {
120  OPM_THROW(std::runtime_error, "axpy multiplication for integer vectors is not implemented yet.");
121 
122  // Avoid warning about missing return statement
123  return CUBLAS_STATUS_NOT_SUPPORTED;
124 }
125 
126 inline cublasStatus_t
127 cublasDot(cublasHandle_t handle, int n, const double* x, int incx, const double* y, int incy, double* result)
128 {
129  return cublasDdot(handle, n, x, incx, y, incy, result);
130 }
131 
132 inline cublasStatus_t
133 cublasDot(cublasHandle_t handle, int n, const float* x, int incx, const float* y, int incy, float* result)
134 {
135  return cublasSdot(handle, n, x, incx, y, incy, result);
136 }
137 
138 inline cublasStatus_t
139 cublasDot([[maybe_unused]] cublasHandle_t handle,
140  [[maybe_unused]] int n,
141  [[maybe_unused]] const int* x,
142  [[maybe_unused]] int incx,
143  [[maybe_unused]] const int* y,
144  [[maybe_unused]] int incy,
145  [[maybe_unused]] int* result)
146 {
147  OPM_THROW(std::runtime_error, "inner product for integer vectors is not implemented yet.");
148 
149  // Avoid warning about missing return statement
150  return CUBLAS_STATUS_NOT_SUPPORTED;
151 }
152 
153 inline cublasStatus_t
154 cublasNrm2(cublasHandle_t handle, int n, const double* x, int incx, double* result)
155 {
156  return cublasDnrm2(handle, n, x, incx, result);
157 }
158 
159 
160 inline cublasStatus_t
161 cublasNrm2(cublasHandle_t handle, int n, const float* x, int incx, float* result)
162 {
163  return cublasSnrm2(handle, n, x, incx, result);
164 }
165 
166 inline cublasStatus_t
167 cublasNrm2([[maybe_unused]] cublasHandle_t handle,
168  [[maybe_unused]] int n,
169  [[maybe_unused]] const int* x,
170  [[maybe_unused]] int incx,
171  [[maybe_unused]] int* result)
172 {
173  OPM_THROW(std::runtime_error, "norm2 for integer vectors is not implemented yet.");
174 
175  // Avoid warning about missing return statement
176  return CUBLAS_STATUS_NOT_SUPPORTED;
177 }
178 
179 } // namespace Opm::gpuistl::detail
180 #endif
Contains wrappers to make the CuBLAS library behave as a modern C++ library with function overlading...
Definition: autotuner.hpp:29