dune-common  2.7.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 
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
162 
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.")]]
208  static Communication<MPICommunicator>
210  {
211  return Communication<MPICommunicator>(getCommunicator());
212  }
213 
214  static Communication<MPICommunicator>
216  {
217  return Communication<MPICommunicator>(getCommunicator());
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
302 
303 #endif // !HAVE_MPI
304 
305 } // end namespace Dune
306 #endif
mpicommunication.hh
Implements an utility class that provides MPI's collective communication methods.
Dune::FakeMPIHelper::size
int size() const
return rank of process, i.e. one
Definition: mpihelper.hh:153
communication.hh
Implements an utility class that provides collective communication methods for sequential programs.
visibility.hh
Definition of macros controlling symbol visibility at the ABI level.
Dune::MPIHelper
FakeMPIHelper MPIHelper
If no MPI is available FakeMPIHelper becomes the MPIHelper.
Definition: mpihelper.hh:301
DUNE_EXPORT
#define DUNE_EXPORT
Export a symbol as part of the public ABI.
Definition: visibility.hh:18
Dune::FakeMPIHelper
A fake mpi helper.
Definition: mpihelper.hh:69
Dune::FakeMPIHelper::getLocalCommunicator
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:103
Dune::FakeMPIHelper::getCommunication
static Communication< MPICommunicator > getCommunication()
Definition: mpihelper.hh:118
Dune::FakeMPIHelper::rank
int rank() const
return rank of process, i.e. zero
Definition: mpihelper.hh:149
Dune::No_Comm
Definition: communication.hh:45
Dune::FakeMPIHelper::getCommunicator
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:91
Dune::FakeMPIHelper::isFake
@ isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: mpihelper.hh:77
Dune::FakeMPIHelper::MPICommunicator
No_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:83
Dune::Communication
Collective communication interface and sequential default implementation.
Definition: communication.hh:80
Dune::FakeMPIHelper::instance
static DUNE_EXPORT FakeMPIHelper & instance(int argc, char **argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:138
stdstreams.hh
Standard Dune debug streams.
Dune::dverb
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition: stdstreams.hh:114
Dune::FakeMPIHelper::getCollectiveCommunication
static Communication< MPICommunicator > getCollectiveCommunication()
Definition: mpihelper.hh:112
Dune
Dune namespace.
Definition: alignedallocator.hh:13