ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ParallelExceptionHandler.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo 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 * ESPResSo 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 this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
22#include "Exception.hpp"
23
26
27#include <boost/mpi/collectives.hpp>
28#include <boost/serialization/string.hpp>
29
30#include <cassert>
31#include <cstddef>
32#include <functional>
33#include <stdexcept>
34#include <string>
35#include <vector>
36
37namespace ScriptInterface {
38
39void ParallelExceptionHandler::handle_impl(std::exception const *error) const {
40 auto const head_node = 0;
41 auto const this_node = m_comm.rank();
42
43 enum : unsigned char {
44 NO_RANK_FAILED = 0u,
45 SOME_RANK_FAILED = 1u,
46 THIS_RANK_SUCCESS = 0u,
47 THIS_RANK_FAILED = 1u,
48 MAIN_RANK_FAILED = 2u,
49 };
50 auto const this_fail_flag =
51 ((error)
52 ? ((this_node == head_node) ? MAIN_RANK_FAILED : THIS_RANK_FAILED)
53 : THIS_RANK_SUCCESS);
54 auto const fail_flag = boost::mpi::all_reduce(
55 m_comm, static_cast<unsigned char>(this_fail_flag), std::bit_or<>());
56 auto const main_rank_failed = fail_flag & MAIN_RANK_FAILED;
57 auto const some_rank_failed = fail_flag & SOME_RANK_FAILED;
58
59 if (main_rank_failed) {
61 if (this_node == head_node) {
62 throw;
63 }
64 throw Exception("");
65 }
66
67 if (some_rank_failed) {
69 std::vector<std::string> messages;
70 std::string this_message{(error) ? error->what() : ""};
71 boost::mpi::gather(m_comm, this_message, messages, head_node);
72 if (this_node == head_node) {
73 std::string error_message{"an error occurred on one or more MPI ranks:"};
74 for (std::size_t i = 0; i < messages.size(); ++i) {
75 error_message += "\n rank " + std::to_string(i) + ": " + messages[i];
76 }
77 throw std::runtime_error(error_message.c_str());
78 }
79 throw Exception("");
80 }
81}
82
83} // namespace ScriptInterface
int this_node
The number of this node.
void flush_runtime_errors_local()
Flush runtime errors to standard error on the local node.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...