From 772dd5ed2d746ccbee05e6e48fa7bb2a8298ff6b Mon Sep 17 00:00:00 2001 From: Stefano Zampini Date: Tue, 29 Nov 2022 00:10:07 +0300 Subject: [PATCH] MPI_InitFinalize: do not finalize PETSc/SLEPc if we are not responsible for it --- include/deal.II/base/mpi.h | 4 ++++ source/base/mpi.cc | 22 ++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/include/deal.II/base/mpi.h b/include/deal.II/base/mpi.h index 45432ac0d2..05e1f0a7b7 100644 --- a/include/deal.II/base/mpi.h +++ b/include/deal.II/base/mpi.h @@ -1205,6 +1205,10 @@ namespace Utilities * Requests to MPI_Wait before finalizing */ static std::set requests; + +#ifdef DEAL_II_WITH_PETSC + bool finalize_petscslepc; +#endif }; /** diff --git a/source/base/mpi.cc b/source/base/mpi.cc index cb58254d59..8cb2a9c34d 100644 --- a/source/base/mpi.cc +++ b/source/base/mpi.cc @@ -778,11 +778,13 @@ namespace Utilities #ifdef DEAL_II_WITH_PETSC # ifdef DEAL_II_WITH_SLEPC // Initialize SLEPc (with PETSc): - ierr = SlepcInitialize(&argc, &argv, nullptr, nullptr); + finalize_petscslepc = SlepcInitializeCalled ? false : true; + ierr = SlepcInitialize(&argc, &argv, nullptr, nullptr); AssertThrow(ierr == 0, SLEPcWrappers::SolverBase::ExcSLEPcError(ierr)); # else // or just initialize PETSc alone: - ierr = PetscInitialize(&argc, &argv, nullptr, nullptr); + finalize_petscslepc = PetscInitializeCalled ? false : true; + ierr = PetscInitialize(&argc, &argv, nullptr, nullptr); AssertThrow(ierr == 0, ExcPETScError(ierr)); # endif @@ -960,22 +962,22 @@ namespace Utilities // Now deal with PETSc (with or without MPI). Only delete the vectors if // finalize hasn't been called yet, otherwise this will lead to errors. #ifdef DEAL_II_WITH_PETSC - if ((PetscInitializeCalled == PETSC_TRUE) && - (PetscFinalizeCalled == PETSC_FALSE)) + if (!PetscFinalizeCalled) { GrowingVectorMemory< PETScWrappers::MPI::Vector>::release_unused_memory(); GrowingVectorMemory< PETScWrappers::MPI::BlockVector>::release_unused_memory(); - + } # ifdef DEAL_II_WITH_SLEPC - // and now end SLEPc (with PETSc) - SlepcFinalize(); + // and now end SLEPc with PETSc if we did so + if (finalize_petscslepc) + SlepcFinalize(); # else - // or just end PETSc. - PetscFinalize(); + // or just end PETSc if we did so + if (finalize_petscslepc) + PetscFinalize(); # endif - } #endif // There is a similar issue with CUDA: The destructor of static objects might -- 2.39.5