From: Stefano Zampini Date: Mon, 28 Nov 2022 21:10:07 +0000 (+0300) Subject: MPI_InitFinalize: do not finalize PETSc/SLEPc if we are not responsible for it X-Git-Tag: v9.5.0-rc1~648^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F14678%2Fhead;p=dealii.git MPI_InitFinalize: do not finalize PETSc/SLEPc if we are not responsible for it --- 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