]> https://gitweb.dealii.org/ - dealii.git/commitdiff
MPI_InitFinalize: do not finalize PETSc/SLEPc if we are not responsible for it 14678/head
authorStefano Zampini <stefano.zampini@gmail.com>
Mon, 28 Nov 2022 21:10:07 +0000 (00:10 +0300)
committerStefano Zampini <stefano.zampini@gmail.com>
Fri, 13 Jan 2023 09:26:18 +0000 (12:26 +0300)
include/deal.II/base/mpi.h
source/base/mpi.cc

index 45432ac0d262b159259878d793c11915ae85aa39..05e1f0a7b7c07ef3e863a3441323366d6b2903aa 100644 (file)
@@ -1205,6 +1205,10 @@ namespace Utilities
        * Requests to MPI_Wait before finalizing
        */
       static std::set<MPI_Request *> requests;
+
+#ifdef DEAL_II_WITH_PETSC
+      bool finalize_petscslepc;
+#endif
     };
 
     /**
index cb58254d592e1fbc28dd40714e3b3e6c316a7a8e..8cb2a9c34d12ead1c5dcdc4946e6a71cb0abf7f3 100644 (file)
@@ -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

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.