KaMPIng 0.1.0
(Near) zero-overhead C++ MPI bindings.
Loading...
Searching...
No Matches
error_handling.hpp
Go to the documentation of this file.
1// This file is part of KaMPIng.
2//
3// Copyright 2022 The KaMPIng Authors
4//
5// KaMPIng is free software : you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
7// version. KaMPIng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
9// for more details.
10//
11// You should have received a copy of the GNU Lesser General Public License along with KaMPIng. If not, see
12// <https://www.gnu.org/licenses/>.:
13
14#pragma once
15
16/// @file
17/// @brief Code for error handling.
18
19#include <array>
20#include <exception>
21#include <string>
22
23#include <kassert/kassert.hpp>
24#include <mpi.h>
25
26/// @brief Wrapper around THROWING_KASSERT for MPI errors.
27///
28/// Throws an MpiErrorException if the supplied error code is not \c MPI_SUCCESS.
29///
30/// The macro accepts 2 parameters:
31/// 1. The error code returned by the MPI call.
32/// 2. The MPI function that returned the error code.
33#define THROW_IF_MPI_ERROR(error_code, function) \
34 THROWING_KASSERT_SPECIFIED( \
35 error_code == MPI_SUCCESS, \
36 #function << " failed!", \
37 kamping::MpiErrorException, \
38 error_code \
39 );
40
41namespace kamping {
42
43/// @brief The exception type used when an MPI call did not return \c MPI_SUCCESS.
44///
45/// When using this with THROWING_KASSERT you should call it like this: `THROWING_KASSERT_SPECIFIED(err == MPI_SUCCESS,
46/// "<MPI function that failed> failed", MpiErrorException, err);`
47class MpiErrorException : public std::exception {
48public:
49 /// @brief Constructs the exception
50 /// @param message A custom error message.
51 /// @param mpi_error_code The error code returned by the MPI call.
52 MpiErrorException(std::string message, int mpi_error_code) : _mpi_error_code(mpi_error_code) {
54 std::array<char, MPI_MAX_ERROR_STRING> errorString;
55 int err = MPI_Error_string(_mpi_error_code, errorString.data(), &errorStringLen);
56 if (err == MPI_SUCCESS) {
57 _what = message + "Failed with the following error message:\n" + std::string(errorString.data()) + "\n";
58 } else {
59 _what = message + "Error message could not be retrieved\n";
60 }
61 }
62
63 /// @brief Gets a description of this exception.
64 /// @return A description of this exception.
65 [[nodiscard]] char const* what() const noexcept final {
66 return _what.c_str();
67 }
68
69 /// @brief Gets the error code returned by the mpi call.
70 /// @return The error code returned by the mpi call.
71 [[nodiscard]] int mpi_error_code() const {
72 return _mpi_error_code;
73 }
74
75 /// @brief Gets the error class corresponding to the error code.
76 /// @return The error class corresponding to the error code.
77 [[nodiscard]] int mpi_error_class() const {
78 int error_class;
79 MPI_Error_class(_mpi_error_code, &error_class);
80 return error_class;
81 }
82
83private:
84 /// @brief The description of this exception.
85 std::string _what;
86 /// @brief The error code returned by the MPI call.
87 int _mpi_error_code;
88};
89
90} // namespace kamping
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
The exception type used when an MPI call did not return MPI_SUCCESS.
Definition error_handling.hpp:47
char const * what() const noexcept final
Gets a description of this exception.
Definition error_handling.hpp:65
int mpi_error_class() const
Gets the error class corresponding to the error code.
Definition error_handling.hpp:77
MpiErrorException(std::string message, int mpi_error_code)
Constructs the exception.
Definition error_handling.hpp:52
int mpi_error_code() const
Gets the error code returned by the mpi call.
Definition error_handling.hpp:71