Loading...
Searching...
No Matches
cublas_safe_call.hpp
/*
Copyright 2022-2023 SINTEF AS
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#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 <opm/common/OpmLog/OpmLog.hpp>
#include <string_view>
namespace Opm::gpuistl::detail
{
#define CHECK_CUBLAS_ERROR_TYPE(code, x) \
if (code == x) { \
return #x; \
}
namespace
{
inline std::string getCublasErrorCodeToString(int code)
{
CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_SUCCESS);
CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_NOT_INITIALIZED);
CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_ALLOC_FAILED);
CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_INVALID_VALUE);
CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_ARCH_MISMATCH);
CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_MAPPING_ERROR);
CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_EXECUTION_FAILED);
CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_INTERNAL_ERROR);
CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_NOT_SUPPORTED);
CHECK_CUBLAS_ERROR_TYPE(code, CUBLAS_STATUS_LICENSE_ERROR);
return fmt::format("UNKNOWN CUBLAS ERROR {}.", code);
}
#undef CHECK_CUBLAS_ERROR_TYPE
} // namespace
inline std::string
getCublasErrorMessage(cublasStatus_t error,
const std::string_view& expression,
const std::string_view& filename,
const std::string_view& functionName,
size_t lineNumber)
{
return 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,
getCublasErrorCodeToString(error));
}
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, getCublasErrorMessage(error, expression, filename, functionName, lineNumber));
}
}
inline cublasStatus_t
cublasWarnIfError(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) {
OpmLog::warning(getCublasErrorMessage(error, expression, filename, functionName, lineNumber));
}
return error;
}
} // namespace Opm::gpuistl::detail
#define OPM_CUBLAS_SAFE_CALL(expression) \
::Opm::gpuistl::detail::cublasSafeCall(expression, #expression, __FILE__, __func__, __LINE__)
#define OPM_CUBLAS_WARN_IF_ERROR(expression) \
::Opm::gpuistl::detail::cublasWarnIfError(expression, #expression, __FILE__, __func__, __LINE__)
#endif // OPM_CUBLAS_SAFE_CALL_HPP
Opm::gpuistl::detail
Contains wrappers to make the CuBLAS library behave as a modern C++ library with function overlading.
Definition autotuner.hpp:30
Opm::gpuistl::detail::cublasWarnIfError
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
Opm::gpuistl::detail::cublasSafeCall
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
Opm::gpuistl::detail::getCublasErrorMessage
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