Skip to content

Commit

Permalink
fix(sessions): only free communicator if it isn't already null
Browse files Browse the repository at this point in the history
  • Loading branch information
dssgabriel committed May 24, 2024
1 parent d064469 commit f0d5285
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/KokkosComm_communicator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ class Communicator {
Communicator(MPI_Comm comm) : _comm(comm) {}
Communicator(const Communicator& other) = delete;
Communicator(const Communicator&& other) { _comm = std::move(other._comm); }
~Communicator() { /*MPI_Comm_free(&_comm);*/ }
~Communicator() {
// Only free the communicator if it hasn't been set to `MPI_COMM_NULL` before. This is to prevent double freeing
// when we explicitly call the communicator's dtor in the `Context` dtor.
if (MPI_COMM_NULL != _comm) {
MPI_Comm_free(&_comm);
}
}

static auto dup_raw(MPI_Comm raw) -> Communicator {
MPI_Comm new_comm;
Expand Down
6 changes: 5 additions & 1 deletion src/impl/KokkosComm_MPI_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ class Context {
public:
Context(MPI_Session shandle, MPI_Comm comm) : _shandle(shandle), _comm(Communicator<ExecSpace>(comm)) {}

~Context() { MPI_Session_finalize(&_shandle); }
~Context() {
// Ensure the session-associated communicator is destroyed before the session is finalized.
_comm.~Communicator();
MPI_Session_finalize(&_shandle);
}

auto comm(void) -> const Communicator<ExecSpace>& { return _comm; }
};
Expand Down

0 comments on commit f0d5285

Please sign in to comment.