HypreErrorHandling.hpp
Go to the documentation of this file.
1/*
2 Copyright 2025 Equinor ASA
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
20#ifndef OPM_HYPRE_ERROR_HANDLING_HPP
21#define OPM_HYPRE_ERROR_HANDLING_HPP
22
23#include <HYPRE.h>
24#include <HYPRE_parcsr_ls.h>
25#include <_hypre_utilities.h>
26#include <fmt/core.h>
27#include <stdexcept>
28#include <string>
29
30namespace Opm::gpuistl
31{
32
36class HypreError : public std::runtime_error
37{
38public:
39 explicit HypreError(const std::string& msg)
40 : std::runtime_error(msg)
41 {
42 }
43};
44
55inline std::string
57 HYPRE_Int err, const std::string& expression, const std::string& file, const std::string& function, int line)
58{
59 return fmt::format("Hypre error in expression: {}\nError code: {}\nLocation: {}:{} in function {}",
60 expression,
61 err,
62 file,
63 line,
64 function);
65}
66
79inline void
81 HYPRE_Int rc, const std::string& expression, const std::string& file, const std::string& function, int line)
82{
83 if (rc != 0) {
84 throw HypreError(getHypreErrorMessage(rc, expression, file, function, line));
85 }
86}
87
96#define OPM_HYPRE_SAFE_CALL(expr) ::Opm::gpuistl::hypreSafeCall((expr), #expr, __FILE__, __func__, __LINE__)
97
101#ifndef HYPRE_SAFE_CALL
102#define HYPRE_SAFE_CALL(expr) OPM_HYPRE_SAFE_CALL(expr)
103#endif
104
105} // namespace Opm::gpuistl
106
107#endif // OPM_HYPRE_ERROR_HANDLING_HPP
Exception class for Hypre errors.
Definition: HypreErrorHandling.hpp:37
HypreError(const std::string &msg)
Definition: HypreErrorHandling.hpp:39
Definition: AmgxInterface.hpp:38
void hypreSafeCall(HYPRE_Int rc, const std::string &expression, const std::string &file, const std::string &function, int line)
Safe call wrapper for Hypre functions.
Definition: HypreErrorHandling.hpp:80
std::string getHypreErrorMessage(HYPRE_Int err, const std::string &expression, const std::string &file, const std::string &function, int line)
Get a descriptive error message for a Hypre error code.
Definition: HypreErrorHandling.hpp:56