dune-common  2.3.1
parallel/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 // $Id: $
4 #ifndef DUNE_MPIHELPER
5 #define DUNE_MPIHELPER
6 
7 #include <cassert>
9 #if HAVE_MPI
10 #include "mpi.h"
12 #endif
13 
16 
17 namespace Dune
18 {
69  {
70  public:
71  enum {
76  isFake = true
77  };
78 
83 
90  DUNE_EXPORT static MPICommunicator getCommunicator ()
91  {
92  static MPICommunicator comm;
93  return comm;
94  }
95 
102  static MPICommunicator getLocalCommunicator ()
103  {
104  return getCommunicator();
105  }
106 
107 
108 
111  {
113  }
114 
130  DUNE_EXPORT static FakeMPIHelper& instance(int argc, char** argv)
131  {
132  (void)argc; (void)argv;
133  // create singleton instance
134  static FakeMPIHelper singleton;
135  return singleton;
136  }
137 
141  int rank () const { return 0; }
145  int size () const { return 1; }
146 
147  private:
148  FakeMPIHelper() {}
149  FakeMPIHelper(const FakeMPIHelper&);
150  FakeMPIHelper& operator=(const FakeMPIHelper);
151  };
152 
153 #if HAVE_MPI
154 
160  class MPIHelper
161  {
162  public:
163  enum {
168  isFake = false
169  };
170 
174  typedef MPI_Comm MPICommunicator;
175 
182  static MPICommunicator getCommunicator ()
183  {
184  return MPI_COMM_WORLD;
185  }
186 
193  static MPICommunicator getLocalCommunicator ()
194  {
195  return MPI_COMM_SELF;
196  }
197 
200  {
202  }
218  DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
219  {
220  // create singleton instance
221  static MPIHelper singleton (argc, argv);
222  return singleton;
223  }
224 
228  int rank () const { return rank_; }
232  int size () const { return size_; }
233 
234  private:
235  int rank_;
236  int size_;
237  void prevent_warning(int){}
238 
240  MPIHelper(int& argc, char**& argv)
241  {
242 #if MPI_2
243  int wasInitialized = -1;
244  MPI_Initialized( &wasInitialized );
245  if(!wasInitialized)
246 #endif
247  {
248  rank_ = -1;
249  size_ = -1;
250  static int is_initialized = MPI_Init(&argc, &argv);
251  prevent_warning(is_initialized);
252  }
253 
254  MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
255  MPI_Comm_size(MPI_COMM_WORLD,&size_);
256 
257  assert( rank_ >= 0 );
258  assert( size_ >= 1 );
259 
260  dverb << "Called MPI_Init on p=" << rank_ << "!" << std::endl;
261  }
263  ~MPIHelper()
264  {
265 #ifdef MPI_2
266  int wasFinalized = -1;
267  MPI_Finalized( &wasFinalized );
268  if(!wasFinalized) {
269 #endif
270  MPI_Finalize();
271  dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
272 #ifdef MPI_2
273  }
274 
275 #endif
276  }
277  MPIHelper(const MPIHelper&);
278  MPIHelper& operator=(const MPIHelper);
279  };
280 #else
281  // We do not have MPI therefore FakeMPIHelper
282  // is the MPIHelper
287  typedef FakeMPIHelper MPIHelper;
288 
289 #endif
290 
291 } // end namespace Dune
292 #endif
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: parallel/mpihelper.hh:193
Dune namespace.
Definition: alignment.hh:13
int rank() const
return rank of process, i.e. zero
Definition: parallel/mpihelper.hh:141
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: parallel/mpihelper.hh:218
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition: parallel/mpihelper.hh:168
static MPICommunicator getCommunicator()
get the default communicator
Definition: parallel/mpihelper.hh:182
int size() const
return rank of process, i.e. one
Definition: parallel/mpihelper.hh:145
int rank() const
return rank of process
Definition: parallel/mpihelper.hh:228
A fake mpi helper.
Definition: parallel/mpihelper.hh:68
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: parallel/mpihelper.hh:174
#define DUNE_EXPORT
Export a symbol as part of the public ABI.
Definition: visibility.hh:18
Definition of macros controlling symbol visibility at the ABI level.
Definition: parallel/collectivecommunication.hh:41
int size() const
return number of processes
Definition: parallel/mpihelper.hh:232
Standard Dune debug streams.
Implements an utility class that provides collective communication methods for sequential programs...
static CollectiveCommunication< MPICommunicator > getCollectiveCommunication()
Definition: parallel/mpihelper.hh:199
A real mpi helper.This helper should be used for parallel programs.
Definition: parallel/mpihelper.hh:160
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition: stdstreams.hh:115
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition: parallel/mpihelper.hh:90
Implements an utility class that provides MPI's collective communication methods. ...
Collective communication interface and sequential default implementation.
Definition: parallel/collectivecommunication.hh:71
static DUNE_EXPORT FakeMPIHelper & instance(int argc, char **argv)
Get the singleton instance of the helper.
Definition: parallel/mpihelper.hh:130
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: parallel/mpihelper.hh:102
static CollectiveCommunication< MPICommunicator > getCollectiveCommunication()
Definition: parallel/mpihelper.hh:110
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: parallel/mpihelper.hh:76
No_Comm MPICommunicator
The type of the mpi communicator.
Definition: parallel/mpihelper.hh:82