From fba5fcf70fd4b4719d1a3a63f43f7f4d0f5b8c82 Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Mon, 19 Apr 2021 13:13:50 -0500 Subject: [PATCH] MPI_InitFinalize: add an at_mpi_init and at_mpi_finalize signal handler --- include/deal.II/base/mpi.h | 28 ++++++++++++++++++++++++++++ source/base/mpi.cc | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/include/deal.II/base/mpi.h b/include/deal.II/base/mpi.h index 5bda17c693..2a0e52342b 100644 --- a/include/deal.II/base/mpi.h +++ b/include/deal.II/base/mpi.h @@ -23,6 +23,8 @@ #include #include +#include + #include #include #include @@ -991,6 +993,32 @@ namespace Utilities static void unregister_request(MPI_Request &request); + /** + * A structure that has boost::signal objects to register a call back + * to run after MPI init or finalize. + * + * For documentation on signals, see + * http://www.boost.org/doc/libs/release/libs/signals2 . + */ + struct Signals + { + /** + * A signal that is triggered immediately after we have + * initialized the MPI context with MPI_Init(). + */ + boost::signals2::signal at_mpi_init; + + /** + * A signal that is triggered just before we close the MPI context + * with MPI_Finalize(). It can be used to deallocate + * statically allocated MPI resources that need to be deallocated + * before MPI_Finalize() is called. + */ + boost::signals2::signal at_mpi_finalize; + }; + + static Signals signals; + private: /** * Requests to MPI_Wait before finalizing diff --git a/source/base/mpi.cc b/source/base/mpi.cc index c47c1eb244..4829c78891 100644 --- a/source/base/mpi.cc +++ b/source/base/mpi.cc @@ -922,6 +922,9 @@ namespace Utilities // finally set this number of threads MultithreadInfo::set_thread_limit(n_threads); } + + // As a final step call the at_mpi_init() signal handler. + signals.at_mpi_init(); } @@ -954,6 +957,9 @@ namespace Utilities MPI_InitFinalize::~MPI_InitFinalize() { + // First, call the at_mpi_finalize() signal handler. + signals.at_mpi_finalize(); + // make memory pool release all PETSc/Trilinos/MPI-based vectors that // are no longer used at this point. this is relevant because the static // object destructors run for these vectors at the end of the program -- 2.39.5