Dune Core Modules (unstable)

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
24namespace Dune
25{
76 {
77 public:
82 constexpr static bool isFake = true;
83
87 typedef No_Comm MPICommunicator;
88
96 {
97 static MPICommunicator comm;
98 return comm;
99 }
100
108 {
109 return getCommunicator();
110 }
111
112
113
119 [[deprecated("getCollectionCommunication is deprecated. Use getCommunication instead.")]]
121 {
123 }
124
126 getCommunication()
127 {
129 }
130
146 DUNE_EXPORT static FakeMPIHelper& instance([[maybe_unused]] int argc,
147 [[maybe_unused]] char** argv)
148 {
149 return instance();
150 }
151
152 DUNE_EXPORT static FakeMPIHelper& instance()
153 {
154 static FakeMPIHelper singleton;
155 return singleton;
156 }
157
161 int rank () const { return 0; }
165 int size () const { return 1; }
166
167 private:
168 FakeMPIHelper() {}
169 FakeMPIHelper(const FakeMPIHelper&);
170 FakeMPIHelper& operator=(const FakeMPIHelper);
171 };
172
173#if HAVE_MPI
181 {
182 public:
187 constexpr static bool isFake = false;
188
192 typedef MPI_Comm MPICommunicator;
193
201 {
202 return MPI_COMM_WORLD;
203 }
204
212 {
213 return MPI_COMM_SELF;
214 }
215
221 [[deprecated("getCollectionCommunication is deprecated. Use getCommunication instead.")]]
224 {
226 }
227
229 getCommunication()
230 {
232 }
252 DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
253 {
254 return instance(&argc, &argv);
255 }
256
284 DUNE_EXPORT static MPIHelper& instance(int* argc = nullptr, char*** argv = nullptr)
285 {
286 assert((argc == nullptr) == (argv == nullptr));
287 static MPIHelper instance{argc, argv};
288 return instance;
289 }
290
294 int rank () const { return rank_; }
298 int size () const { return size_; }
299
302 {
303 int wasFinalized = -1;
304 MPI_Finalized( &wasFinalized );
305 if(!wasFinalized && initializedHere_)
306 {
307 MPI_Finalize();
308 dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
309 }
310
311 }
312
313 private:
314 int rank_;
315 int size_;
316 bool initializedHere_;
317 void prevent_warning(int){}
318
320 MPIHelper(int* argc, char*** argv)
321 : initializedHere_(false)
322 {
323 int wasInitialized = -1;
324 MPI_Initialized( &wasInitialized );
325 if(!wasInitialized)
326 {
327 rank_ = -1;
328 size_ = -1;
329 static int is_initialized = MPI_Init(argc, argv);
330 prevent_warning(is_initialized);
331 initializedHere_ = true;
332 }
333
334 MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
335 MPI_Comm_size(MPI_COMM_WORLD,&size_);
336
337 assert( rank_ >= 0 );
338 assert( size_ >= 1 );
339
340 dverb << "Called MPI_Init on p=" << rank_ << "!" << std::endl;
341 }
342
343 MPIHelper(const MPIHelper&);
344 MPIHelper& operator=(const MPIHelper);
345 };
346#else // !HAVE_MPI
347 // We do not have MPI therefore FakeMPIHelper
348 // is the MPIHelper
353 typedef FakeMPIHelper MPIHelper;
354
355#endif // !HAVE_MPI
356
357} // end namespace Dune
358
359#endif // DUNE_COMMON_PARALLEL_MPIHELPER_HH
Collective communication interface and sequential default implementation.
Definition: communication.hh:100
A fake mpi helper.
Definition: mpihelper.hh:76
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:95
int size() const
return rank of process, i.e. one
Definition: mpihelper.hh:165
static Communication< MPICommunicator > getCollectiveCommunication()
Definition: mpihelper.hh:120
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:107
No_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:87
static DUNE_EXPORT FakeMPIHelper & instance(int argc, char **argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:146
static constexpr bool isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: mpihelper.hh:82
int rank() const
return rank of process, i.e. zero
Definition: mpihelper.hh:161
A real mpi helper.
Definition: mpihelper.hh:181
int size() const
return number of processes
Definition: mpihelper.hh:298
static constexpr bool isFake
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition: mpihelper.hh:187
static DUNE_EXPORT MPIHelper & instance(int *argc=nullptr, char ***argv=nullptr)
Get the singleton instance of the helper.
Definition: mpihelper.hh:284
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:252
~MPIHelper()
calls MPI_Finalize
Definition: mpihelper.hh:301
int rank() const
return rank of process
Definition: mpihelper.hh:294
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:192
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:200
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:211
static Communication< MPICommunicator > getCollectiveCommunication()
Definition: mpihelper.hh:223
Implements an utility class that provides collective communication methods for sequential programs.
A few common exception classes.
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition: stdstreams.hh:117
Implements an utility class that provides MPI's collective communication methods.
Dune namespace.
Definition: alignedallocator.hh:13
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:20
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)