Dune Core Modules (2.5.2)

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 #include <cassert>
8 #if HAVE_MPI
9 #include "mpi.h"
11 #endif
12 
15 
16 namespace Dune
17 {
68  {
69  public:
70  enum {
75  isFake = true
76  };
77 
81  typedef No_Comm MPICommunicator;
82 
90  {
91  static MPICommunicator comm;
92  return comm;
93  }
94 
102  {
103  return getCommunicator();
104  }
105 
106 
107 
109  getCollectiveCommunication()
110  {
112  }
113 
129  DUNE_EXPORT static FakeMPIHelper& instance(int argc, char** argv)
130  {
131  (void)argc; (void)argv;
132  // create singleton instance
133  static FakeMPIHelper singleton;
134  return singleton;
135  }
136 
140  int rank () const { return 0; }
144  int size () const { return 1; }
145 
146  private:
147  FakeMPIHelper() {}
148  FakeMPIHelper(const FakeMPIHelper&);
149  FakeMPIHelper& operator=(const FakeMPIHelper);
150  };
151 
152 #if HAVE_MPI
159  class MPIHelper
160  {
161  public:
162  enum {
167  isFake = false
168  };
169 
173  typedef MPI_Comm MPICommunicator;
174 
182  {
183  return MPI_COMM_WORLD;
184  }
185 
193  {
194  return MPI_COMM_SELF;
195  }
196 
198  getCollectiveCommunication()
199  {
201  }
217  DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
218  {
219  // create singleton instance
220  static MPIHelper singleton (argc, argv);
221  return singleton;
222  }
223 
227  int rank () const { return rank_; }
231  int size () const { return size_; }
232 
233  private:
234  int rank_;
235  int size_;
236  bool initializedHere_;
237  void prevent_warning(int){}
238 
240  MPIHelper(int& argc, char**& argv)
241  : initializedHere_(false)
242  {
243  int wasInitialized = -1;
244  MPI_Initialized( &wasInitialized );
245  if(!wasInitialized)
246  {
247  rank_ = -1;
248  size_ = -1;
249  static int is_initialized = MPI_Init(&argc, &argv);
250  prevent_warning(is_initialized);
251  initializedHere_ = true;
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  int wasFinalized = -1;
266  MPI_Finalized( &wasFinalized );
267  if(!wasFinalized && initializedHere_)
268  {
269  MPI_Finalize();
270  dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
271  }
272 
273  }
274  MPIHelper(const MPIHelper&);
275  MPIHelper& operator=(const MPIHelper);
276  };
277 #else
278  // We do not have MPI therefore FakeMPIHelper
279  // is the MPIHelper
284  typedef FakeMPIHelper MPIHelper;
285 
286 #endif
287 
288 } // end namespace Dune
289 #endif
Collective communication interface and sequential default implementation.
Definition: collectivecommunication.hh:79
A fake mpi helper.
Definition: mpihelper.hh:68
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:89
int size() const
return rank of process, i.e. one
Definition: mpihelper.hh:144
@ isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: mpihelper.hh:75
static DUNE_EXPORT FakeMPIHelper & instance(int argc, char **argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:129
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:101
No_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:81
int rank() const
return rank of process, i.e. zero
Definition: mpihelper.hh:140
A real mpi helper.
Definition: mpihelper.hh:160
int size() const
return number of processes
Definition: mpihelper.hh:231
@ isFake
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition: mpihelper.hh:167
int rank() const
return rank of process
Definition: mpihelper.hh:227
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:173
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:181
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:192
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:217
Implements an utility class that provides collective communication methods for sequential programs.
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition: stdstreams.hh:114
Implements an utility class that provides MPI's collective communication methods.
Dune namespace.
Definition: alignment.hh:11
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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 8, 22:30, 2024)