ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
RuntimeErrorCollector.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014-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
23
24#include <boost/mpi/collectives.hpp>
25
26#include <algorithm>
27#include <functional>
28#include <iostream>
29#include <sstream>
30#include <string>
31#include <utility>
32#include <vector>
33
34namespace ErrorHandling {
35
37 : m_comm(std::move(comm)) {}
38
40 if (!m_errors.empty()) {
41 /* Print remaining error messages on destruction */
42 std::cerr << "There were unhandled errors.\n";
43 flush();
44 }
45}
46
48 const std::string &msg,
49 const char *function, const char *file,
50 const int line) {
51 std::lock_guard<std::mutex> lock(mutex);
52 m_errors.emplace_back(level, m_comm.rank(), msg, std::string(function),
53 std::string(file), line);
54}
55
56void RuntimeErrorCollector::warning(const std::string &msg,
57 const char *function, const char *file,
58 const int line) {
59 std::lock_guard<std::mutex> lock(mutex);
60 m_errors.emplace_back(RuntimeError::ErrorLevel::WARNING, m_comm.rank(), msg,
61 std::string(function), std::string(file), line);
62}
63
64void RuntimeErrorCollector::error(const std::string &msg, const char *function,
65 const char *file, const int line) {
66 std::lock_guard<std::mutex> lock(mutex);
67 m_errors.emplace_back(RuntimeError::ErrorLevel::ERROR, m_comm.rank(), msg,
68 std::string(function), std::string(file), line);
69}
70
72 std::lock_guard<std::mutex> lock(mutex);
73 return boost::mpi::all_reduce(m_comm, static_cast<int>(m_errors.size()),
74 std::plus<>());
75}
76
78 return static_cast<int>(std::ranges::count_if(
79 m_errors, [level](auto const &e) { return e.level() >= level; }));
80}
81
83 std::lock_guard<std::mutex> lock(mutex);
84 m_errors.clear();
85}
86
88 {
89 std::lock_guard<std::mutex> lock(mutex);
90 for (auto const &e : m_errors) {
91 std::cerr << e.format() << std::endl;
92 }
93 }
94 this->clear();
95}
96
97std::vector<RuntimeError> RuntimeErrorCollector::gather() {
98 std::lock_guard<std::mutex> lock(mutex);
99 std::vector<RuntimeError> all_errors{};
100 std::swap(all_errors, m_errors);
101
102 Utils::Mpi::gather_buffer(all_errors, m_comm);
103
104 return all_errors;
105}
106
108 {
109 std::lock_guard<std::mutex> lock(mutex);
110 Utils::Mpi::gather_buffer(m_errors, m_comm);
111 }
112 this->clear();
113}
114
115} // namespace ErrorHandling
void flush()
Flush error messages to standard error.
int count() const
Get the number of all flying messages on all nodes.
void error(const std::string &msg, const char *function, const char *file, int line)
void warning(const std::string &msg, const char *function, const char *file, int line)
RuntimeErrorCollector(boost::mpi::communicator comm)
void message(RuntimeError::ErrorLevel level, const std::string &msg, const char *function, const char *file, int line)
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
STL namespace.