From d552eb9609c02eac5e8640cead24202971eb4902 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 5 Jan 2015 09:17:20 -0600 Subject: [PATCH] Reintegrate do_init into constructor of MPI_InitFinalize. This is possible now that we have only one constructor left. --- include/deal.II/base/mpi.h | 7 --- source/base/mpi.cc | 109 +++++++++++++++++-------------------- 2 files changed, 51 insertions(+), 65 deletions(-) diff --git a/include/deal.II/base/mpi.h b/include/deal.II/base/mpi.h index e48796095a..9b1d4900d4 100644 --- a/include/deal.II/base/mpi.h +++ b/include/deal.II/base/mpi.h @@ -323,13 +323,6 @@ namespace Utilities * be called at destruction. */ const bool owns_mpi; - - - /** - * A common function called by all of the constructors. - */ - void do_init(int &argc, - char ** &argv); }; namespace internal diff --git a/source/base/mpi.cc b/source/base/mpi.cc index 0abcc758e8..7f25575378 100644 --- a/source/base/mpi.cc +++ b/source/base/mpi.cc @@ -320,8 +320,58 @@ namespace Utilities : owns_mpi (true) { - do_init(argc, argv); + static bool constructor_has_already_run = false; + Assert (constructor_has_already_run == false, + 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. + int MPI_has_been_started = 0; + MPI_Initialized(&MPI_has_been_started); + AssertThrow (MPI_has_been_started == 0, + ExcMessage ("MPI error. You can only start MPI once!")); + + int mpi_err, provided; + // this works likempi_err = MPI_Init (&argc, &argv); but tells MPI that + // we might use several threads but never call two MPI functions at the + // same time. For an explanation see on why we do this see + // http://www.open-mpi.org/community/lists/users/2010/03/12244.php + int wanted = MPI_THREAD_SERIALIZED; + mpi_err = MPI_Init_thread(&argc, &argv, wanted, &provided); + AssertThrow (mpi_err == 0, + ExcMessage ("MPI could not be initialized.")); + + // disable for now because at least some implementations always return MPI_THREAD_SINGLE. + //Assert(max_num_threads==1 || provided != MPI_THREAD_SINGLE, + // ExcMessage("MPI reports that we are not allowed to use multiple threads.")); +#else + // make sure the compiler doesn't warn + // about these variables + (void)argc; + (void)argv; + (void)owns_mpi; +#endif + + // we are allowed to call MPI_Init ourselves and PETScInitialize will + // detect this. This allows us to use MPI_Init_thread instead. +#ifdef DEAL_II_WITH_PETSC +# ifdef DEAL_II_WITH_SLEPC + // Initialize SLEPc (with PETSc): + SlepcInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL); +# else + // or just initialize PETSc alone: + PetscInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL); +# endif +#endif + + constructor_has_already_run = true; + + // Now also see how many threads we'd like to run if (max_num_threads != numbers::invalid_unsigned_int) { // set maximum number of threads (also respecting the environment @@ -393,63 +443,6 @@ namespace Utilities } - - void - MPI_InitFinalize::do_init(int &argc, - char ** &argv) - { - static bool constructor_has_already_run = false; - Assert (constructor_has_already_run == false, - 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. - int MPI_has_been_started = 0; - MPI_Initialized(&MPI_has_been_started); - AssertThrow (MPI_has_been_started == 0, - ExcMessage ("MPI error. You can only start MPI once!")); - - int mpi_err, provided; - // this works likempi_err = MPI_Init (&argc, &argv); but tells MPI that - // we might use several threads but never call two MPI functions at the - // same time. For an explanation see on why we do this see - // http://www.open-mpi.org/community/lists/users/2010/03/12244.php - int wanted = MPI_THREAD_SERIALIZED; - mpi_err = MPI_Init_thread(&argc, &argv, wanted, &provided); - AssertThrow (mpi_err == 0, - ExcMessage ("MPI could not be initialized.")); - - // disable for now because at least some implementations always return MPI_THREAD_SINGLE. - //Assert(max_num_threads==1 || provided != MPI_THREAD_SINGLE, - // ExcMessage("MPI reports that we are not allowed to use multiple threads.")); -#else - // make sure the compiler doesn't warn - // about these variables - (void)argc; - (void)argv; - (void)owns_mpi; -#endif - - // we are allowed to call MPI_Init ourselves and PETScInitialize will - // detect this. This allows us to use MPI_Init_thread instead. -#ifdef DEAL_II_WITH_PETSC -# ifdef DEAL_II_WITH_SLEPC - // Initialize SLEPc (with PETSc): - SlepcInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL); -# else - // or just initialize PETSc alone: - PetscInitialize(&argc, &argv, PETSC_NULL, PETSC_NULL); -# endif -#endif - - constructor_has_already_run = true; - } - - MPI_InitFinalize::~MPI_InitFinalize() { // make memory pool release all PETSc/Trilinos/MPI-based vectors that are no -- 2.39.5