dune-common  2.8.0
mpihelper.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_MPIHELPER
4 #define DUNE_MPIHELPER
5 
6 #if HAVE_MPI
7 #include <cassert>
8 #include <mpi.h>
9 #endif
10 
11 #include <mutex>
12 
14 #if HAVE_MPI
17 #endif
19 
20 namespace Dune
21 {
72  {
73  public:
74  enum {
79  isFake = true
80  };
81 
86 
94  {
95  static MPICommunicator comm;
96  return comm;
97  }
98 
106  {
107  return getCommunicator();
108  }
109 
110 
111 
112  // Will be deprecated after the 2.7 release
113  //[[deprecated("getCollectionCommunication is deprecated. Use getCommunication instead.")]]
115  {
117  }
118 
121  {
123  }
124 
140  DUNE_EXPORT static FakeMPIHelper& instance([[maybe_unused]] int argc,
141  [[maybe_unused]] char** argv)
142  {
143  return instance();
144  }
145 
147  {
148  static FakeMPIHelper singleton;
149  return singleton;
150  }
151 
155  int rank () const { return 0; }
159  int size () const { return 1; }
160 
161  private:
162  FakeMPIHelper() {}
163  FakeMPIHelper(const FakeMPIHelper&);
164  FakeMPIHelper& operator=(const FakeMPIHelper);
165  };
166 
167 #if HAVE_MPI
174  class MPIHelper
175  {
176  public:
177  enum {
182  isFake = false
183  };
184 
188  typedef MPI_Comm MPICommunicator;
189 
197  {
198  return MPI_COMM_WORLD;
199  }
200 
208  {
209  return MPI_COMM_SELF;
210  }
211 
212  // Will be deprecated after the 2.7 release
213  //[[deprecated("getCollectionCommunication is deprecated. Use getCommunication instead.")]]
216  {
218  }
219 
222  {
224  }
240  DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
241  {
242  // create singleton instance
243  if (!instance_){
244  static std::mutex mutex;
245  std::lock_guard<std::mutex> guard(mutex);
246  if(!instance_)
247  instance_.reset(new MPIHelper(argc,argv));
248  }
249  return *instance_;
250  }
251 
253  {
254  if(!instance_)
255  DUNE_THROW(InvalidStateException, "MPIHelper not initialized! Call MPIHelper::instance(argc, argv) with arguments first.");
256  return *instance_;
257  }
258 
262  int rank () const { return rank_; }
266  int size () const { return size_; }
267 
270  {
271  int wasFinalized = -1;
272  MPI_Finalized( &wasFinalized );
273  if(!wasFinalized && initializedHere_)
274  {
275  MPI_Finalize();
276  dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
277  }
278 
279  }
280 
281  private:
282  int rank_;
283  int size_;
284  bool initializedHere_;
285  void prevent_warning(int){}
286  static inline std::unique_ptr<MPIHelper> instance_ = {};
287 
289  MPIHelper(int& argc, char**& argv)
290  : initializedHere_(false)
291  {
292  int wasInitialized = -1;
293  MPI_Initialized( &wasInitialized );
294  if(!wasInitialized)
295  {
296  rank_ = -1;
297  size_ = -1;
298  static int is_initialized = MPI_Init(&argc, &argv);
299  prevent_warning(is_initialized);
300  initializedHere_ = true;
301  }
302 
303  MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
304  MPI_Comm_size(MPI_COMM_WORLD,&size_);
305 
306  assert( rank_ >= 0 );
307  assert( size_ >= 1 );
308 
309  dverb << "Called MPI_Init on p=" << rank_ << "!" << std::endl;
310  }
311 
312  MPIHelper(const MPIHelper&);
313  MPIHelper& operator=(const MPIHelper);
314  };
315 #else // !HAVE_MPI
316  // We do not have MPI therefore FakeMPIHelper
317  // is the MPIHelper
322  typedef FakeMPIHelper MPIHelper;
323 
324 #endif // !HAVE_MPI
325 
326 } // end namespace Dune
327 #endif
Implements an utility class that provides collective communication methods for sequential programs.
Implements an utility class that provides MPI's collective communication methods.
Standard Dune debug streams.
Definition of macros controlling symbol visibility at the ABI level.
#define DUNE_EXPORT
Export a symbol as part of the public ABI.
Definition: visibility.hh:18
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition: stdstreams.hh:114
Dune namespace.
Definition: alignedallocator.hh:11
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:279
Definition: communication.hh:44
Collective communication interface and sequential default implementation.
Definition: communication.hh:98
A fake mpi helper.
Definition: mpihelper.hh:72
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:93
static DUNE_EXPORT FakeMPIHelper & instance([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:140
static Communication< MPICommunicator > getCollectiveCommunication()
Definition: mpihelper.hh:114
int size() const
return rank of process, i.e. one
Definition: mpihelper.hh:159
static Communication< MPICommunicator > getCommunication()
Definition: mpihelper.hh:120
@ isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: mpihelper.hh:79
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:105
No_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:85
static DUNE_EXPORT FakeMPIHelper & instance()
Definition: mpihelper.hh:146
int rank() const
return rank of process, i.e. zero
Definition: mpihelper.hh:155
A real mpi helper.
Definition: mpihelper.hh:175
int size() const
return number of processes
Definition: mpihelper.hh:266
static Communication< MPICommunicator > getCollectiveCommunication()
Definition: mpihelper.hh:215
~MPIHelper()
calls MPI_Finalize
Definition: mpihelper.hh:269
int rank() const
return rank of process
Definition: mpihelper.hh:262
@ isFake
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition: mpihelper.hh:182
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:188
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:196
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:207
static Communication< MPICommunicator > getCommunication()
Definition: mpihelper.hh:221
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:240
static DUNE_EXPORT MPIHelper & instance()
Definition: mpihelper.hh:252