1 #ifndef DUNE_FEM_MPIMANAGER_HH
2 #define DUNE_FEM_MPIMANAGER_HH
6 #include <dune/common/parallel/mpicommunication.hh>
7 #include <dune/common/parallel/mpihelper.hh>
25 typedef Dune::CollectiveCommunication< MPIHelper::MPICommunicator >
33 static bool mpiFinalized ()
35 bool finalized = false ;
39 int wasFinalized = -1;
40 MPI_Finalized( &wasFinalized );
41 finalized = bool( wasFinalized );
52 if( ! mpiFinalized() )
54 ::Dune::Petsc::finalize();
58 static void initialize(
const bool verbose,
int &argc,
char **&argv )
62 ::Dune::Petsc::initialize( verbose, argc, argv );
73 if( wasInitializedHere_ && !mpiFinalized() )
82 MPIHelper *&helper = instance().helper_;
83 std::unique_ptr< CollectiveCommunication > &
comm = instance().comm_;
88 int wasInitialized = -1;
89 MPI_Initialized( &wasInitialized );
92 #ifndef USE_SMP_PARALLEL
97 int is_initialized = MPI_Init(&argc, &argv);
98 if( is_initialized != MPI_SUCCESS )
99 DUNE_THROW(InvalidStateException,
"MPI_Init failed!");
105 int is_initialized = MPI_Init_thread(&argc, &argv, MPI_THREAD_FUNNELED, &provided );
107 if( is_initialized != MPI_SUCCESS )
108 DUNE_THROW(InvalidStateException,
"MPI_Init_thread failed!");
110 #if not defined NDEBUG && defined DUNE_DEVEL_MODE
114 if( provided != MPI_THREAD_FUNNELED )
116 if( provided == MPI_THREAD_SINGLE )
117 dwarn <<
"MPI thread support = single (instead of funneled)!" << std::endl;
119 dwarn <<
"WARNING: MPI thread support = " << provided <<
" != MPI_THREAD_FUNNELED " << MPI_THREAD_FUNNELED << std::endl;
124 instance().wasInitializedHere_ =
true;
135 helper = &MPIHelper::instance( argc, argv );
140 PETSc::initialize(
rank() == 0, argc, argv );
149 const std::unique_ptr< CollectiveCommunication > &
comm = instance().comm_;
151 DUNE_THROW( InvalidStateException,
"MPIManager has not been initialized." );
157 return comm().rank();
162 return comm().size();
166 MPIHelper *helper_ =
nullptr;
167 std::unique_ptr< CollectiveCommunication > comm_;
168 bool wasInitializedHere_ = false ;
Definition: bindguard.hh:11
Definition: mpimanager.hh:24
static const CollectiveCommunication & comm()
Definition: mpimanager.hh:147
Dune::CollectiveCommunication< MPIHelper::MPICommunicator > CollectiveCommunication
Definition: mpimanager.hh:26
~MPIManager()
Definition: mpimanager.hh:68
static int size()
Definition: mpimanager.hh:160
static int rank()
Definition: mpimanager.hh:155
static void initialize(int &argc, char **&argv)
Definition: mpimanager.hh:80
static void initialize()
initialize static variables
Definition: registry.hh:62
static Object & instance(Args &&... args)
return singleton instance of given Object type.
Definition: singleton.hh:101