dune-common  2.11
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 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_COMMON_PARALLEL_MPIHELPER_HH
6 #define DUNE_COMMON_PARALLEL_MPIHELPER_HH
7 
8 #include <cassert>
9 #include <mutex>
10 
11 #if HAVE_MPI
12 #include <mpi.h>
13 #endif
14 
19 
20 #if HAVE_MPI
22 #endif
23 
24 namespace Dune
25 {
76  {
77  public:
82  constexpr static bool isFake = true;
83 
88 
96  {
97  static MPICommunicator comm;
98  return comm;
99  }
100 
108  {
109  return getCommunicator();
110  }
111 
112 
115  {
117  }
118 
134  DUNE_EXPORT static FakeMPIHelper& instance([[maybe_unused]] int argc,
135  [[maybe_unused]] char** argv)
136  {
137  return instance();
138  }
139 
141  {
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:
175  constexpr static bool isFake = false;
176 
180  typedef MPI_Comm MPICommunicator;
181 
189  {
190  return MPI_COMM_WORLD;
191  }
192 
200  {
201  return MPI_COMM_SELF;
202  }
203 
206  {
208  }
228  DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
229  {
230  return instance(&argc, &argv);
231  }
232 
260  DUNE_EXPORT static MPIHelper& instance(int* argc = nullptr, char*** argv = nullptr)
261  {
262  assert((argc == nullptr) == (argv == nullptr));
263  static MPIHelper instance{argc, argv};
264  return instance;
265  }
266 
270  int rank () const { return rank_; }
274  int size () const { return size_; }
275 
278  {
279  int wasFinalized = -1;
280  MPI_Finalized( &wasFinalized );
281  if(!wasFinalized && initializedHere_)
282  {
283  MPI_Finalize();
284  dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
285  }
286 
287  }
288 
289  private:
290  int rank_;
291  int size_;
292  bool initializedHere_;
293  void prevent_warning(int){}
294 
296  MPIHelper(int* argc, char*** argv)
297  : initializedHere_(false)
298  {
299  int wasInitialized = -1;
300  MPI_Initialized( &wasInitialized );
301  if(!wasInitialized)
302  {
303  rank_ = -1;
304  size_ = -1;
305  static int is_initialized = MPI_Init(argc, argv);
306  prevent_warning(is_initialized);
307  initializedHere_ = true;
308  }
309 
310  MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
311  MPI_Comm_size(MPI_COMM_WORLD,&size_);
312 
313  assert( rank_ >= 0 );
314  assert( size_ >= 1 );
315 
316  dverb << "Called MPI_Init on p=" << rank_ << "!" << std::endl;
317  }
318 
319  MPIHelper(const MPIHelper&);
320  MPIHelper& operator=(const MPIHelper);
321  };
322 #else // !HAVE_MPI
323  // We do not have MPI therefore FakeMPIHelper
324  // is the MPIHelper
329  typedef FakeMPIHelper MPIHelper;
330 
331 #endif // !HAVE_MPI
332 
333 } // end namespace Dune
334 
335 #endif // DUNE_COMMON_PARALLEL_MPIHELPER_HH
int rank() const
return rank of process
Definition: mpihelper.hh:270
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:228
static constexpr bool isFake
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition: mpihelper.hh:175
int rank() const
return rank of process, i.e. zero
Definition: mpihelper.hh:149
Implements an utility class that provides MPI&#39;s collective communication methods. ...
static Communication< MPICommunicator > getCommunication()
Definition: mpihelper.hh:114
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:95
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:107
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:188
Definition of macros controlling symbol visibility at the ABI level.
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition: stdstreams.hh:117
int size() const
return number of processes
Definition: mpihelper.hh:274
static constexpr bool isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: mpihelper.hh:82
#define DUNE_EXPORT
Export a symbol as part of the public ABI.
Definition: visibility.hh:20
static DUNE_EXPORT FakeMPIHelper & instance([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:134
No_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:87
int size() const
return rank of process, i.e. one
Definition: mpihelper.hh:153
Collective communication interface and sequential default implementation.
Definition: communication.hh:99
Dune namespace
Definition: alignedallocator.hh:12
A real mpi helper.This helper should be used for parallel programs.
Definition: mpihelper.hh:168
~MPIHelper()
calls MPI_Finalize
Definition: mpihelper.hh:277
static Communication< MPICommunicator > getCommunication()
Definition: mpihelper.hh:205
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:199
static DUNE_EXPORT FakeMPIHelper & instance()
Definition: mpihelper.hh:140
A few common exception classes.
Standard Dune debug streams.
Definition: communication.hh:46
static DUNE_EXPORT MPIHelper & instance(int *argc=nullptr, char ***argv=nullptr)
Get the singleton instance of the helper.
Definition: mpihelper.hh:260
Implements an utility class that provides collective communication methods for sequential programs...
A fake mpi helper.
Definition: mpihelper.hh:75
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:180