Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
script_interface: MPI-safe exception mechanism
Make C++ exceptions from core classes safe in a MPI-parallel context via a parallel exception handler. When an exception occurs, re-throw the exception on the head node and throw a ScriptInterface::Exception on worker nodes.
- Loading branch information
Showing
21 changed files
with
407 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (C) 2022 The ESPResSo project | ||
* | ||
* This file is part of ESPResSo. | ||
* | ||
* ESPResSo is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* ESPResSo is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "ParallelExceptionHandler.hpp" | ||
|
||
#include "Exception.hpp" | ||
|
||
#include "core/MpiCallbacks.hpp" | ||
#include "core/RuntimeError.hpp" | ||
#include "core/communication.hpp" | ||
#include "core/errorhandling.hpp" | ||
|
||
#include <boost/mpi/collectives.hpp> | ||
#include <boost/serialization/string.hpp> | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include <functional> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace ScriptInterface { | ||
|
||
void ParallelExceptionHandler::handle_impl(std::exception const *error) const { | ||
auto const head_node = 0; | ||
auto const this_node = m_comm.rank(); | ||
|
||
enum : unsigned char { | ||
NO_RANK_FAILED = 0u, | ||
SOME_RANK_FAILED = 1u, | ||
THIS_RANK_SUCCESS = 0u, | ||
THIS_RANK_FAILED = 1u, | ||
MAIN_RANK_FAILED = 2u, | ||
}; | ||
auto const this_fail_flag = | ||
((error) | ||
? ((this_node == head_node) ? MAIN_RANK_FAILED : THIS_RANK_FAILED) | ||
: THIS_RANK_SUCCESS); | ||
auto const fail_flag = boost::mpi::all_reduce( | ||
m_comm, static_cast<unsigned char>(this_fail_flag), std::bit_or<>()); | ||
auto const main_rank_failed = fail_flag & MAIN_RANK_FAILED; | ||
auto const some_rank_failed = fail_flag & SOME_RANK_FAILED; | ||
|
||
if (main_rank_failed) { | ||
flush_runtime_errors_local(); | ||
if (this_node == head_node) { | ||
throw; | ||
} | ||
throw Exception(""); | ||
} | ||
|
||
if (some_rank_failed) { | ||
flush_runtime_errors_local(); | ||
std::vector<std::string> messages; | ||
std::string this_message{(error) ? error->what() : ""}; | ||
boost::mpi::gather(m_comm, this_message, messages, head_node); | ||
if (this_node == head_node) { | ||
std::string error_message{"an error occurred on one or more MPI ranks:"}; | ||
for (std::size_t i = 0; i < messages.size(); ++i) { | ||
error_message += "\n rank " + std::to_string(i) + ": " + messages[i]; | ||
} | ||
throw std::runtime_error(error_message.c_str()); | ||
} | ||
throw Exception(""); | ||
} | ||
} | ||
|
||
} // namespace ScriptInterface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (C) 2022 The ESPResSo project | ||
* | ||
* This file is part of ESPResSo. | ||
* | ||
* ESPResSo is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* ESPResSo is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef ESPRESSO_SCRIPT_INTERFACE_PARALLEL_EXCEPTION_HANDLER_HPP | ||
#define ESPRESSO_SCRIPT_INTERFACE_PARALLEL_EXCEPTION_HANDLER_HPP | ||
|
||
#include "core/errorhandling.hpp" | ||
|
||
#include <boost/mpi/communicator.hpp> | ||
|
||
#include <stdexcept> | ||
#include <string> | ||
#include <utility> | ||
|
||
namespace ScriptInterface { | ||
/** | ||
* Handle exceptions thrown in MPI parallel code. | ||
* | ||
* Instantiate this class inside the catch block and after the catch block, | ||
* like so: | ||
* @code{.cpp} | ||
* boost::mpi::communicator world; | ||
* auto handler = ScriptInterface::ParallelExceptionHandler{world}; | ||
* std::shared_ptr<MyClass> obj; | ||
* context()->parallel_try_catch([&obj]() { | ||
* obj = std::make_shared<MyClass>(2., true); | ||
* }); | ||
* @endcode | ||
* | ||
* Exceptions are handled as follows: | ||
* * the main rank throws: re-throw on main rank and throw @ref Exception | ||
* on all other ranks | ||
* * one or more of the worker nodes throw: collect error messages from | ||
* worker nodes and throw them on the main rank as a @c std::runtime_error, | ||
* throw @ref Exception on all other ranks | ||
* | ||
* Throwing a @ref Exception guarantees that the partially initialized script | ||
* interface object won't be registered in the @ref GlobalContext dictionary; | ||
* this is the only side-effect on worker nodes, since the exception itself | ||
* is otherwise silently ignored. On the main rank, the thrown exception is | ||
* converted to a Python exception. | ||
*/ | ||
class ParallelExceptionHandler { | ||
public: | ||
ParallelExceptionHandler(boost::mpi::communicator comm) | ||
: m_comm(std::move(comm)) {} | ||
|
||
/** | ||
* @brief Handle exceptions in synchronous code. | ||
* Error messages queued in the runtime error collector are flushed | ||
* to standard error if the code throws on any rank. | ||
* @pre Must be called on all ranks. | ||
* @pre The @p callback cannot invoke remote functions from the | ||
* @ref Communication::MpiCallbacks framework due to blocking | ||
* communication (risk of MPI deadlock on worker nodes). | ||
* @param[in] callback Callback to execute synchronously on all ranks. | ||
*/ | ||
template <typename T> | ||
void parallel_try_catch(std::function<void()> const &callback) const { | ||
try { | ||
callback(); | ||
} catch (T const &error) { | ||
handle_impl(&error); | ||
} | ||
handle_impl(nullptr); | ||
} | ||
|
||
private: | ||
void handle_impl(std::exception const *error) const; | ||
boost::mpi::communicator m_comm; | ||
}; | ||
} // namespace ScriptInterface | ||
|
||
#endif |
Oops, something went wrong.