dune-common  2.7.1
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 
12 #if HAVE_MPI
15 #endif
17 
18 namespace Dune
19 {
70  {
71  public:
72  enum {
77  isFake = true
78  };
79 
84 
92  {
93  static MPICommunicator comm;
94  return comm;
95  }
96 
104  {
105  return getCommunicator();
106  }
107 
108 
109 
110  // Will be deprecated after the 2.7 release
111  //[[deprecated("getCollectionCommunication is deprecated. Use getCommunication instead.")]]
113  {
115  }
116 
119  {
121  }
122 
138  DUNE_EXPORT static FakeMPIHelper& instance(int argc, char** argv)
139  {
140  (void)argc; (void)argv;
141  // create singleton instance
142  static FakeMPIHelper singleton;
143  return singleton;
144  }
145 
149  int rank () const { return 0; }
153  int size () const { return 1; }
154 
155  private:
156  FakeMPIHelper() {}
157  FakeMPIHelper(const FakeMPIHelper&);
158  FakeMPIHelper& operator=(const FakeMPIHelper);
159  };
160 
161 #if HAVE_MPI
168  class MPIHelper
169  {
170  public:
171  enum {
176  isFake = false
177  };
178 
182  typedef MPI_Comm MPICommunicator;
183 
191  {
192  return MPI_COMM_WORLD;
193  }
194 
202  {
203  return MPI_COMM_SELF;
204  }
205 
206  // Will be deprecated after the 2.7 release
207  //[[deprecated("getCollectionCommunication is deprecated. Use getCommunication instead.")]]
210  {
212  }
213 
216  {
218  }
234  DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
235  {
236  // create singleton instance
237  static MPIHelper singleton (argc, argv);
238  return singleton;
239  }
240 
244  int rank () const { return rank_; }
248  int size () const { return size_; }
249 
250  private:
251  int rank_;
252  int size_;
253  bool initializedHere_;
254  void prevent_warning(int){}
255 
257  MPIHelper(int& argc, char**& argv)
258  : initializedHere_(false)
259  {
260  int wasInitialized = -1;
261  MPI_Initialized( &wasInitialized );
262  if(!wasInitialized)
263  {
264  rank_ = -1;
265  size_ = -1;
266  static int is_initialized = MPI_Init(&argc, &argv);
267  prevent_warning(is_initialized);
268  initializedHere_ = true;
269  }
270 
271  MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
272  MPI_Comm_size(MPI_COMM_WORLD,&size_);
273 
274  assert( rank_ >= 0 );
275  assert( size_ >= 1 );
276 
277  dverb << "Called MPI_Init on p=" << rank_ << "!" << std::endl;
278  }
280  ~MPIHelper()
281  {
282  int wasFinalized = -1;
283  MPI_Finalized( &wasFinalized );
284  if(!wasFinalized && initializedHere_)
285  {
286  MPI_Finalize();
287  dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
288  }
289 
290  }
291  MPIHelper(const MPIHelper&);
292  MPIHelper& operator=(const MPIHelper);
293  };
294 #else // !HAVE_MPI
295  // We do not have MPI therefore FakeMPIHelper
296  // is the MPIHelper
301  typedef FakeMPIHelper MPIHelper;
302 
303 #endif // !HAVE_MPI
304 
305 } // end namespace Dune
306 #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
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition: stdstreams.hh:114
Dune namespace.
Definition: alignedallocator.hh:14
Definition: communication.hh:45
Collective communication interface and sequential default implementation.
Definition: communication.hh:81
A fake mpi helper.
Definition: mpihelper.hh:70
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:91
static Communication< MPICommunicator > getCollectiveCommunication()
Definition: mpihelper.hh:112
int size() const
return rank of process, i.e. one
Definition: mpihelper.hh:153
static Communication< MPICommunicator > getCommunication()
Definition: mpihelper.hh:118
static DUNE_EXPORT FakeMPIHelper & instance(int argc, char **argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:138
@ isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: mpihelper.hh:77
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:103
No_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:83
int rank() const
return rank of process, i.e. zero
Definition: mpihelper.hh:149
A real mpi helper.
Definition: mpihelper.hh:169
int size() const
return number of processes
Definition: mpihelper.hh:248
static Communication< MPICommunicator > getCollectiveCommunication()
Definition: mpihelper.hh:209
int rank() const
return rank of process
Definition: mpihelper.hh:244
@ isFake
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition: mpihelper.hh:176
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:182
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:190
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:201
static Communication< MPICommunicator > getCommunication()
Definition: mpihelper.hh:215
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:234