ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
errorhandling.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21/** \file
22 * Implementation of \ref errorhandling.hpp.
23 */
24
25#include "errorhandling.hpp"
26
27#include "MpiCallbacks.hpp"
29
30#include <boost/mpi/collectives.hpp>
31#include <boost/mpi/communicator.hpp>
32
33#include <cstdlib>
34#include <functional>
35#include <memory>
36#include <string>
37#include <utility>
38#include <vector>
39
40namespace ErrorHandling {
41/** RuntimeErrorCollector instance.
42 * This is a unique pointer so we don't
43 * leak on repeated calls of @ref init_error_handling.
44 */
45static std::unique_ptr<RuntimeErrorCollector> runtimeErrorCollector;
46
47/** The callback loop we are on. */
48static std::weak_ptr<Communication::MpiCallbacks> m_callbacks;
49
50void init_error_handling(std::weak_ptr<Communication::MpiCallbacks> callbacks) {
51 m_callbacks = std::move(callbacks);
52
54 std::make_unique<RuntimeErrorCollector>(m_callbacks.lock()->comm());
55}
56
58 const std::string &file,
59 const int line,
60 const std::string &function) {
61 return {*runtimeErrorCollector, level, file, line, function};
62}
63
65 runtimeErrorCollector->gather_local();
66}
67
69
74
75std::vector<RuntimeError> mpi_gather_runtime_errors_all(bool is_head_node) {
76 if (is_head_node) {
77 return runtimeErrorCollector->gather();
78 }
79 runtimeErrorCollector->gather_local();
80 return {};
81}
82} // namespace ErrorHandling
83
84void errexit() {
85 ErrorHandling::m_callbacks.lock()->comm().abort(1);
86
87 std::abort();
88}
89
91 using namespace ErrorHandling;
92 return runtimeErrorCollector->count(RuntimeError::ErrorLevel::ERROR);
93}
94
95int check_runtime_errors(boost::mpi::communicator const &comm) {
96 return boost::mpi::all_reduce(comm, check_runtime_errors_local(),
97 std::plus<int>());
98}
99
Communication::MpiCallbacks manages MPI communication using a visitor pattern.
#define REGISTER_CALLBACK(cb)
Register a static callback without return value.
Create a runtime error message via the streaming operator.
int check_runtime_errors(boost::mpi::communicator const &comm)
Count runtime errors on all nodes.
void flush_runtime_errors_local()
Flush runtime errors to standard error on the local node.
int check_runtime_errors_local()
Count runtime errors on the local node.
void errexit()
exit ungracefully, core dump if switched on.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
RuntimeErrorStream _runtimeMessageStream(RuntimeError::ErrorLevel level, const std::string &file, const int line, const std::string &function)
std::vector< RuntimeError > mpi_gather_runtime_errors()
Gather messages on main rank.
std::vector< RuntimeError > mpi_gather_runtime_errors_all(bool is_head_node)
Gather messages on main rank.
static std::unique_ptr< RuntimeErrorCollector > runtimeErrorCollector
RuntimeErrorCollector instance.
static void mpi_gather_runtime_errors_local()
void init_error_handling(std::weak_ptr< Communication::MpiCallbacks > callbacks)
Initialize the error collection system.
static std::weak_ptr< Communication::MpiCallbacks > m_callbacks
The callback loop we are on.
ErrorLevel
The error level, warnings are only displayed to the user, errors are fatal.