cuda_safe_call.hpp
Go to the documentation of this file.
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_CUDA_SAFE_CALL_HPP
20#define OPM_CUDA_SAFE_CALL_HPP
21#include <cuda_runtime.h>
22#include <fmt/core.h>
23#include <opm/common/ErrorMacros.hpp>
24#include <opm/common/OpmLog/OpmLog.hpp>
25#include <string_view>
26
27namespace Opm::cuistl::detail
28{
44inline std::string
45getCudaErrorMessage(cudaError_t error,
46 const std::string_view& expression,
47 const std::string_view& filename,
48 const std::string_view& functionName,
49 size_t lineNumber)
50{
51 return fmt::format("CUDA expression did not execute correctly. Expression was: \n"
52 " {}\n"
53 "CUDA error was {}\n"
54 "in function {}, in {}, at line {}\n",
55 expression,
56 cudaGetErrorString(error),
57 functionName,
58 filename,
59 lineNumber);
60}
61
81inline void
82cudaSafeCall(cudaError_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 if (error != cudaSuccess) {
89 OPM_THROW(std::runtime_error, getCudaErrorMessage(error, expression, filename, functionName, lineNumber));
90 }
91}
92
122inline cudaError_t
123cudaWarnIfError(cudaError_t error,
124 const std::string_view& expression,
125 const std::string_view& filename,
126 const std::string_view& functionName,
127 size_t lineNumber)
128{
129 if (error != cudaSuccess) {
130 OpmLog::warning(getCudaErrorMessage(error, expression, filename, functionName, lineNumber));
131 }
132
133 return error;
134}
135} // namespace Opm::cuistl::detail
136
154#define OPM_CUDA_SAFE_CALL(expression) \
155 ::Opm::cuistl::detail::cudaSafeCall(expression, #expression, __FILE__, __func__, __LINE__)
156
157
175#define OPM_CUDA_WARN_IF_ERROR(expression) \
176 ::Opm::cuistl::detail::cudaWarnIfError(expression, #expression, __FILE__, __func__, __LINE__)
177
178#endif
Definition: cublas_safe_call.hpp:32
void cudaSafeCall(cudaError_t error, const std::string_view &expression, const std::string_view &filename, const std::string_view &functionName, size_t lineNumber)
cudaSafeCall checks the return type of the CUDA expression (function call) and throws an exception if...
Definition: cuda_safe_call.hpp:82
std::string getCudaErrorMessage(cudaError_t error, const std::string_view &expression, const std::string_view &filename, const std::string_view &functionName, size_t lineNumber)
getCudaErrorMessage generates the error message to display for a given error.
Definition: cuda_safe_call.hpp:45
cudaError_t cudaWarnIfError(cudaError_t error, const std::string_view &expression, const std::string_view &filename, const std::string_view &functionName, size_t lineNumber)
cudaWarnIfError checks the return type of the CUDA expression (function call) and issues a warning if...
Definition: cuda_safe_call.hpp:123