Skip to content

Commit

Permalink
Merge pull request #1172 from parthenon-hpc-lab/jmm/dont-init-mpi-twice
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurlungur authored Sep 11, 2024
2 parents a0c665d + 06c6172 commit e8ed786
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


### Changed (changing behavior/API/variables/...)

- [[PR1172]](https://github.com/parthenon-hpc-lab/parthenon/pull/1172) Make parthenon manager robust against external MPI init and finalize calls

### Fixed (not changing behavior/API/variables/...)

Expand Down
24 changes: 9 additions & 15 deletions src/parthenon_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,18 @@ ParthenonStatus ParthenonManager::ParthenonInitEnv(int argc, char *argv[]) {

// initialize MPI
#ifdef MPI_PARALLEL
if (MPI_SUCCESS != MPI_Init(&argc, &argv)) {
std::cout << "### FATAL ERROR in ParthenonInit" << std::endl
int mpi_initialized;
PARTHENON_MPI_CHECK(MPI_Initialized(&mpi_initialized));
if (!mpi_initialized && (MPI_SUCCESS != MPI_Init(&argc, &argv))) {
std::cerr << "### FATAL ERROR in ParthenonInit" << std::endl
<< "MPI Initialization failed." << std::endl;
return ParthenonStatus::error;
}
// Get process id (rank) in MPI_COMM_WORLD
if (MPI_SUCCESS != MPI_Comm_rank(MPI_COMM_WORLD, &(Globals::my_rank))) {
std::cout << "### FATAL ERROR in ParthenonInit" << std::endl
<< "MPI_Comm_rank failed." << std::endl;
// MPI_Finalize();
return ParthenonStatus::error;
}
PARTHENON_MPI_CHECK(MPI_Comm_rank(MPI_COMM_WORLD, &(Globals::my_rank)));

// Get total number of MPI processes (ranks)
if (MPI_SUCCESS != MPI_Comm_size(MPI_COMM_WORLD, &Globals::nranks)) {
std::cout << "### FATAL ERROR in main" << std::endl
<< "MPI_Comm_size failed." << std::endl;
// MPI_Finalize();
return ParthenonStatus::error;
}
PARTHENON_MPI_CHECK(MPI_Comm_size(MPI_COMM_WORLD, &Globals::nranks));
#else // no MPI
Globals::my_rank = 0;
Globals::nranks = 1;
Expand Down Expand Up @@ -232,7 +224,9 @@ ParthenonStatus ParthenonManager::ParthenonFinalize() {
pmesh.reset();
Kokkos::finalize();
#ifdef MPI_PARALLEL
MPI_Finalize();
int mpi_finalized;
PARTHENON_MPI_CHECK(MPI_Finalized(&mpi_finalized));
if (!mpi_finalized) PARTHENON_MPI_CHECK(MPI_Finalize());
#endif
return ParthenonStatus::complete;
}
Expand Down

0 comments on commit e8ed786

Please sign in to comment.