Refactored CUDA error macros to call inline functions.

This commit is contained in:
Kjetil Olsen Lye 2023-03-27 10:22:24 +02:00
parent bf9dd4e1dd
commit a204708f37
3 changed files with 156 additions and 63 deletions

View File

@ -16,18 +16,25 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CUBLAS_SAFE_CALL_HPP
#define CUBLAS_SAFE_CALL_HPP
#ifndef OPM_CUBLAS_SAFE_CALL_HPP
#define OPM_CUBLAS_SAFE_CALL_HPP
#include <cublas_v2.h>
#include <exception>
#include <fmt/core.h>
#include <opm/common/ErrorMacros.hpp>
#include <string_view>
namespace Opm::cuistl::detail
{
#define CHECK_CUBLAS_ERROR_TYPE(code, x) \
if (code == x) { \
return #x; \
}
namespace
{
/**
* @brief getCublasErrorMessage Converts an error code returned from a cublas function a human readable string.
* @param code an error code from a cublas routine
@ -49,9 +56,48 @@ getCublasErrorMessage(int code)
return fmt::format("UNKNOWN CUBLAS ERROR {}.", code);
}
} // namespace
#undef CHECK_CUSPRASE_ERROR_TYPE
#undef CHECK_CUSPRASE_ERROR_TYPE
/**
* @brief cublasSafeCall checks the return type of the CUBLAS expression (function call) and throws an exception if it
* does not equal CUBLAS_STATUS_SUCCESS.
*
* Example usage:
* @code{.cpp}
* #include <opm/simulators/linalg/cuistl/detail/cuda_safe_call.hpp>
* #include <cublas_v2.h>
*
* void some_function() {
* cublasHandle_t cublasHandle;
* cudaSafeCall(cublasCreate(&cublasHandle), "cublasCreate(&cublasHandle)", __FILE__, __func__, __LINE__);
* }
* @endcode
*
* @note It is probably easier to use the macro OPM_CUBLAS_SAFE_CALL
*
* @todo Refactor to use std::source_location once we shift to C++20
*/
inline void
cublasSafeCall(cublasStatus_t error,
const std::string_view& expression,
const std::string_view& filename,
const std::string_view& functionName,
size_t lineNumber)
{
if (error != CUBLAS_STATUS_SUCCESS) {
OPM_THROW(std::runtime_error,
fmt::format("cuBLAS expression did not execute correctly. Expression was: \n\n"
" {}\n\n"
"in function {}, in {}, at line {}.\n"
"CuBLAS error code was: {}\n",
expression,
functionName,
filename,
lineNumber,
getCublasErrorMessage(error)));
}
}
} // namespace Opm::cuistl::detail
/**
* @brief OPM_CUBLAS_SAFE_CALL checks the return type of the cublas expression (function call) and throws an exception
* if it does not equal CUBLAS_STATUS_SUCCESS.
@ -70,19 +116,6 @@ getCublasErrorMessage(int code)
* @note This should be used for any call to cuBlas unless you have a good reason not to.
*/
#define OPM_CUBLAS_SAFE_CALL(expression) \
do { \
cublasStatus_t error = expression; \
if (error != CUBLAS_STATUS_SUCCESS) { \
OPM_THROW(std::runtime_error, \
fmt::format("cuBLAS expression did not execute correctly. Expression was: \n\n" \
" {}\n\n" \
"in function {}, in {}, at line {}.\n" \
"CuBLAS error code was: {}\n", \
#expression, \
__func__, \
__FILE__, \
__LINE__, \
getCublasErrorMessage(error))); \
} \
} while (false)
#endif // CUBLAS_SAFE_CALL_HPP
::Opm::cuistl::detail::cublasSafeCall(expression, #expression, __FILE__, __func__, __LINE__)
#endif // OPM_CUBLAS_SAFE_CALL_HPP

View File

@ -16,11 +16,56 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CUDA_SAFE_CALL_HPP
#define CUDA_SAFE_CALL_HPP
#ifndef OPM_CUDA_SAFE_CALL_HPP
#define OPM_CUDA_SAFE_CALL_HPP
#include <cuda_runtime.h>
#include <fmt/core.h>
#include <opm/common/ErrorMacros.hpp>
#include <string_view>
namespace Opm::cuistl::detail
{
/**
* @brief cudaSafeCall checks the return type of the CUDA expression (function call) and throws an exception if it
* does not equal cudaSuccess.
*
* Example usage:
* @code{.cpp}
* #include <opm/simulators/linalg/cuistl/detail/cuda_safe_call.hpp>
* #include <cuda_runtime.h>
*
* void some_function() {
* void* somePointer;
* cudaSafeCall(cudaMalloc(&somePointer, 1), "cudaMalloc(&somePointer, 1)", __FILE__, __func__, __LINE__);
* }
* @endcode
*
* @note It is probably easier to use the macro OPM_CUDA_SAFE_CALL
*
* @todo Refactor to use std::source_location once we shift to C++20
*/
inline void
cudaSafeCall(cudaError_t error,
const std::string_view& expression,
const std::string_view& filename,
const std::string_view& functionName,
size_t lineNumber)
{
if (error != cudaSuccess) {
OPM_THROW(std::runtime_error,
fmt::format("CUDA expression did not execute correctly. Expression was: \n"
" {}\n"
"CUDA error was {}\n"
"in function {}, in {}, at line {}\n",
expression,
cudaGetErrorString(error),
functionName,
filename,
lineNumber));
}
}
} // namespace Opm::cuistl::detail
/**
* @brief OPM_CUDA_SAFE_CALL checks the return type of the CUDA expression (function call) and throws an exception if it
@ -37,22 +82,9 @@
* }
* @endcode
*
* @note This should be used for any call to cuSparse unless you have a good reason not to.
* @note This should be used for any call to the CUDA runtime API unless you have a good reason not to.
*/
#define OPM_CUDA_SAFE_CALL(expression) \
do { \
cudaError_t error = expression; \
if (error != cudaSuccess) { \
OPM_THROW(std::runtime_error, \
fmt::format("CUDA expression did not execute correctly. Expression was: \n" \
" {}\n" \
"CUDA error was {}\n" \
"in function {}, in {}, at line {}\n", \
#expression, \
cudaGetErrorString(error), \
__func__, \
__FILE__, \
__LINE__)); \
} \
} while (false)
::Opm::cuistl::detail::cudaSafeCall(expression, #expression, __FILE__, __func__, __LINE__)
#endif

View File

@ -16,19 +16,20 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CUSPARSE_SAFE_CALL_HPP
#define CUSPARSE_SAFE_CALL_HPP
#ifndef OPM_CUSPARSE_SAFE_CALL_HPP
#define OPM_CUSPARSE_SAFE_CALL_HPP
#include <cusparse.h>
#include <exception>
#include <fmt/core.h>
#include <opm/common/ErrorMacros.hpp>
namespace Opm::cuistl::detail
{
#define CHECK_CUSPARSE_ERROR_TYPE(code, x) \
if (code == x) { \
return #x; \
}
namespace
{
/**
* @brief getCusparseErrorMessage Converts an error code returned from a cusparse function a human readable string.
* @param code an error code from a cusparse routine
@ -51,11 +52,51 @@ getCusparseErrorMessage(int code)
CHECK_CUSPARSE_ERROR_TYPE(code, CUSPARSE_STATUS_INSUFFICIENT_RESOURCES);
return fmt::format("UNKNOWN CUSPARSE ERROR {}.", code);
}
} // namespace
#undef CHECK_CUSPARSE_ERROR_TYPE
/**
* @brief cusparseSafeCall checks the return type of the CUSPARSE expression (function call) and throws an exception if
* it does not equal CUSPARSE_STATUS_SUCCESS.
*
* Example usage:
* @code{.cpp}
* #include <opm/simulators/linalg/cuistl/detail/cuda_safe_call.hpp>
* #include <cublas_v2.h>
*
* void some_function() {
* cublasHandle_t cublasHandle;
* cudaSafeCall(cusparseCreate(&cusparseHandle), "cusparseCreate(&cusparseHandle)", __FILE__, __func__, __LINE__);
* }
* @endcode
*
* @note It is probably easier to use the macro OPM_CUBLAS_SAFE_CALL
*
* @todo Refactor to use std::source_location once we shift to C++20
*/
inline void
cusparseSafeCall(cusparseStatus_t error,
const std::string_view& expression,
const std::string_view& filename,
const std::string_view& functionName,
size_t lineNumber)
{
if (error != CUSPARSE_STATUS_SUCCESS) {
OPM_THROW(std::runtime_error,
fmt::format("cuSparse expression did not execute correctly. Expression was: \n\n"
" {}\n\nin function {}, in {}, at line {}\n"
"CuSparse error code was: {}\n",
expression,
functionName,
filename,
lineNumber,
getCusparseErrorMessage(error)));
}
}
} // namespace Opm::cuistl::detail
/**
* @brief OPM_CUSPARSE_SAFE_CALL checks the return type of the cusparse expression (function call) and throws an
* exception if it does not equal CUSPARSE_STATUS_SUCCESS.
@ -67,25 +108,12 @@ getCusparseErrorMessage(int code)
*
* void some_function() {
* cusparseHandle_t cusparseHandle;
* OPM_CUSPARSE_SAFE_CALL(cusparseCreate(&cublasHandle));
* OPM_CUSPARSE_SAFE_CALL(cusparseCreate(&cusparseHandle));
* }
* @endcode
*
* @note This should be used for any call to cuSparse unless you have a good reason not to.
*/
#define OPM_CUSPARSE_SAFE_CALL(expression) \
do { \
cusparseStatus_t error = expression; \
if (error != CUSPARSE_STATUS_SUCCESS) { \
OPM_THROW(std::runtime_error, \
fmt::format("cuSparse expression did not execute correctly. Expression was: \n\n" \
" {}\n\nin function {}, in {}, at line {}\n" \
"CuSparse error code was: {}\n", \
#expression, \
__func__, \
__FILE__, \
__LINE__, \
getCusparseErrorMessage(error))); \
} \
} while (false)
#endif // CUSPARSE_SAFE_CALL_HPP
::Opm::cuistl::detail::cusparseSafeCall(expression, #expression, __FILE__, __func__, __LINE__)
#endif // OPM_CUSPARSE_SAFE_CALL_HPP