opm-simulators
cublas_safe_call.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 #ifndef OPM_CUBLAS_SAFE_CALL_HPP
20 #define OPM_CUBLAS_SAFE_CALL_HPP
21 #include <cublas_v2.h>
22 #include <exception>
23 #include <fmt/core.h>
24 #include <opm/common/ErrorMacros.hpp>
25 #include <opm/common/OpmLog/OpmLog.hpp>
26 #include <string_view>
27 
28 
29 
30 
31 namespace Opm::gpuistl::detail
32 {
33 
34 #define CHECK_CUBLAS_ERROR_TYPE(code, x) \
35  if (code == x) { \
36  return #x; \
37  }
38 
39 namespace
40 {
46  inline std::string getCublasErrorCodeToString(int code)
47  {
48  CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_SUCCESS);
49  CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_NOT_INITIALIZED);
50  CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_ALLOC_FAILED);
51  CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_INVALID_VALUE);
52  CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_ARCH_MISMATCH);
53  CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_MAPPING_ERROR);
54  CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_EXECUTION_FAILED);
55  CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_INTERNAL_ERROR);
56  CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_NOT_SUPPORTED);
57  CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_LICENSE_ERROR);
58 
59  return fmt::format(fmt::runtime("UNKNOWN CUBLAS ERROR {}."), code);
60  }
61 
62 #undef CHECK_CUBLAS_ERROR_TYPE
63 
64 } // namespace
65 
81 inline std::string
82 getCublasErrorMessage(cublasStatus_t error,
83  const std::string_view& expression,
84  const std::string_view& filename,
85  const std::string_view& functionName,
86  size_t lineNumber)
87 {
88  return fmt::format(fmt::runtime("cuBLAS expression did not execute correctly. Expression was: \n\n"
89  " {}\n\n"
90  "in function {}, in {}, at line {}.\n"
91  "CuBLAS error code was: {}\n"),
92  expression,
93  functionName,
94  filename,
95  lineNumber,
96  getCublasErrorCodeToString(error));
97 }
98 
124 inline void
125 cublasSafeCall(cublasStatus_t error,
126  const std::string_view& expression,
127  const std::string_view& filename,
128  const std::string_view& functionName,
129  size_t lineNumber)
130 {
131  if (error != CUBLAS_STATUS_SUCCESS) {
132  OPM_THROW(std::runtime_error, getCublasErrorMessage(error, expression, filename, functionName, lineNumber));
133  }
134 }
135 
164 inline cublasStatus_t
165 cublasWarnIfError(cublasStatus_t error,
166  const std::string_view& expression,
167  const std::string_view& filename,
168  const std::string_view& functionName,
169  size_t lineNumber)
170 {
171  if (error != CUBLAS_STATUS_SUCCESS) {
172  OpmLog::warning(getCublasErrorMessage(error, expression, filename, functionName, lineNumber));
173  }
174 
175  return error;
176 }
177 } // namespace Opm::gpuistl::detail
178 
196 #define OPM_CUBLAS_SAFE_CALL(expression) \
197  ::Opm::gpuistl::detail::cublasSafeCall(expression, #expression, __FILE__, __func__, __LINE__)
198 
216 #define OPM_CUBLAS_WARN_IF_ERROR(expression) \
217  ::Opm::gpuistl::detail::cublasWarnIfError(expression, #expression, __FILE__, __func__, __LINE__)
218 
219 #endif // OPM_CUBLAS_SAFE_CALL_HPP
std::string getCublasErrorMessage(cublasStatus_t error, const std::string_view &expression, const std::string_view &filename, const std::string_view &functionName, size_t lineNumber)
getCublasErrorMessage generates the error message to display for a given error.
Definition: cublas_safe_call.hpp:82
void cublasSafeCall(cublasStatus_t error, const std::string_view &expression, const std::string_view &filename, const std::string_view &functionName, size_t lineNumber)
cublasSafeCall checks the return type of the CUBLAS expression (function call) and throws an exceptio...
Definition: cublas_safe_call.hpp:125
Contains wrappers to make the CuBLAS library behave as a modern C++ library with function overlading...
Definition: autotuner.hpp:29
cublasStatus_t cublasWarnIfError(cublasStatus_t error, const std::string_view &expression, const std::string_view &filename, const std::string_view &functionName, size_t lineNumber)
cublasWarnIfError checks the return type of the CUBLAS expression (function call) and issues a warnin...
Definition: cublas_safe_call.hpp:165