KaMPIng 0.2.0
Flexible and (near) zero-overhead C++ bindings for MPI
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 <mpi.h>
24
25#include "kamping/kassert/kassert.hpp"
26
27/// @brief Wrapper around THROWING_KAMPING_ASSERT for MPI errors.
28///
29/// Throws an MpiErrorException if the supplied error code is not \c MPI_SUCCESS.
30///
31/// The macro accepts 2 parameters:
32/// 1. The error code returned by the MPI call.
33/// 2. The MPI function that returned the error code.
34#define THROW_IF_MPI_ERROR(error_code, function) \
35 THROWING_KAMPING_ASSERT_SPECIFIED( \
36 error_code == MPI_SUCCESS, \
37 #function << " failed!", \
38 kamping::MpiErrorException, \
39 error_code \
40 );
41
42namespace kamping {
43
44/// @brief The exception type used when an MPI call did not return \c MPI_SUCCESS.
45///
46/// When using this with THROWING_KAMPING_ASSERT you should call it like this: `THROWING_KAMPING_ASSERT_SPECIFIED(err ==
47/// MPI_SUCCESS,
48/// "<MPI function that failed> failed", MpiErrorException, err);`
49class MpiErrorException : public std::exception {
50public:
51 /// @brief Constructs the exception
52 /// @param message A custom error message.
53 /// @param mpi_error_code The error code returned by the MPI call.
54 MpiErrorException(std::string message, int mpi_error_code) : _mpi_error_code(mpi_error_code) {
56 std::array<char, MPI_MAX_ERROR_STRING> errorString;
57 int err = MPI_Error_string(_mpi_error_code, errorString.data(), &errorStringLen);
58 if (err == MPI_SUCCESS) {
59 _what = message + "Failed with the following error message:\n" + std::string(errorString.data()) + "\n";
60 } else {
61 _what = message + "Error message could not be retrieved\n";
62 }
63 }
64
65 /// @brief Gets a description of this exception.
66 /// @return A description of this exception.
67 [[nodiscard]] char const* what() const noexcept final {
68 return _what.c_str();
69 }
70
71 /// @brief Gets the error code returned by the mpi call.
72 /// @return The error code returned by the mpi call.
73 [[nodiscard]] int mpi_error_code() const {
74 return _mpi_error_code;
75 }
76
77 /// @brief Gets the error class corresponding to the error code.
78 /// @return The error class corresponding to the error code.
79 [[nodiscard]] int mpi_error_class() const {
80 int error_class;
81 MPI_Error_class(_mpi_error_code, &error_class);
82 return error_class;
83 }
84
85private:
86 /// @brief The description of this exception.
87 std::string _what;
88 /// @brief The error code returned by the MPI call.
89 int _mpi_error_code;
90};
91
92} // 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:49
char const * what() const noexcept final
Gets a description of this exception.
Definition error_handling.hpp:67
int mpi_error_class() const
Gets the error class corresponding to the error code.
Definition error_handling.hpp:79
MpiErrorException(std::string message, int mpi_error_code)
Constructs the exception.
Definition error_handling.hpp:54
int mpi_error_code() const
Gets the error code returned by the mpi call.
Definition error_handling.hpp:73