Skip to content

Commit

Permalink
fix: prevent overlap w/ MPI_{Init,Finalize} & `Kokkos::{initialize,…
Browse files Browse the repository at this point in the history
…finalize}`
  • Loading branch information
dssgabriel committed Jun 14, 2024
1 parent cfdd01e commit ee700c6
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/KokkosComm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ enum class ThreadSupportLevel {
inline void initialize(int &argc, char *argv[], ThreadSupportLevel required) {
int flag;
MPI_Initialized(&flag);
// Eagerly abort if MPI has already been initialized
// Forbid calling this function if MPI has already been initialized
if (0 != flag) {
MPI_Abort(MPI_COMM_WORLD, -1);
}

// Forbid calling this function if Kokkos has already been initialized
if (Kokkos::is_initialized()) {
std::abort();
}

int provided;
MPI_Init_thread(&argc, &argv, static_cast<int>(required), &provided);
// Abort if MPI failed to provide the required thread support level
Expand All @@ -74,7 +79,18 @@ inline void initialize(int &argc, char *argv[], ThreadSupportLevel required) {
inline void initialize(int &argc, char *argv[]) { initialize(argc, argv, ThreadSupportLevel::Multiple); }

inline void finalize() {
// Forbid calling this function if Kokkos has already been finalized or isn't yet initialized
if (Kokkos::is_finalized() || !Kokkos::is_initialized()) {
MPI_Abort(MPI_COMM_WORLD, -1);
}
Kokkos::finalize();

int flag;
MPI_Finalized(&flag);
// Forbid calling this function if MPI has already been finalized
if (0 != flag) {
std::abort();
}
MPI_Finalize();
}

Expand Down

0 comments on commit ee700c6

Please sign in to comment.