ExcMessage ("You can only create a single object of this class "
"in a program since it initializes the MPI system."));
-#ifdef DEAL_II_WITH_MPI
- // if we have PETSc, we will initialize it and let it handle MPI.
- // Otherwise, we will do it.
#ifdef DEAL_II_WITH_PETSC
# ifdef DEAL_II_WITH_SLEPC
// Initialise SLEPc (with PETSc):
PetscInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL);
# endif
#else
+# ifdef DEAL_II_WITH_MPI
+ // if we have PETSc, we will initialize it and let it handle MPI.
+ // Otherwise, we will do it.
int MPI_has_been_started = 0;
MPI_Initialized(&MPI_has_been_started);
AssertThrow (MPI_has_been_started == 0,
mpi_err = MPI_Init (&argc, &argv);
AssertThrow (mpi_err == 0,
ExcMessage ("MPI could not be initialized."));
-#endif
-#else
+# else
// make sure the compiler doesn't warn
// about these variables
(void)argc;
(void)argv;
(void)owns_mpi;
+# endif
#endif
constructor_has_already_run = true;
MPI_InitFinalize::~MPI_InitFinalize()
{
-#ifdef DEAL_II_WITH_MPI
-
- // make memory pool release all MPI-based vectors that are no
+ // make memory pool release all PETSc/Trilinos/MPI-based vectors that are no
// longer used at this point. this is relevant because the
// static object destructors run for these vectors at the end of
// the program would run after MPI_Finalize is called, leading
// to errors
+
+#ifdef DEAL_II_WITH_MPI
+ // Start with Trilinos (need to do this before finalizing PETSc because it finalizes MPI.
+ // Delete vectors from the pools:
# if defined(DEAL_II_WITH_TRILINOS)
GrowingVectorMemory<TrilinosWrappers::MPI::Vector>
::release_unused_memory ();
GrowingVectorMemory<TrilinosWrappers::MPI::BlockVector>
::release_unused_memory ();
# endif
+#endif
- // Same for PETSc. only do this if PETSc hasn't been
- // terminated yet since PETSc deletes all vectors that
- // have been allocated but not freed at the time of
- // calling PETScFinalize. running the calls below after
- // PetscFinalize has already been called will therefore
- // yield errors of double deallocations
+
+ // 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)
&&
// or just end PETSc.
PetscFinalize();
# endif
- }
-#else
+ }
+#endif
+ // only MPI_Finalize if we are running with MPI and we are not using PETSc
+ // (otherwise we called it above already):
+#ifdef DEAL_II_WITH_MPI
+#ifndef DEAL_II_WITH_PETSC
int mpi_err = 0;
int MPI_has_been_started = 0;