Dune Core Modules (2.9.1)

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 (C) 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_MPIHELPER
6#define DUNE_MPIHELPER
7
8#if HAVE_MPI
9#include <cassert>
10#include <mpi.h>
11#endif
12
13#include <mutex>
14
16#if HAVE_MPI
19#endif
21
22namespace Dune
23{
74 {
75 public:
80 constexpr static bool isFake = true;
81
85 typedef No_Comm MPICommunicator;
86
94 {
95 static MPICommunicator comm;
96 return comm;
97 }
98
106 {
107 return getCommunicator();
108 }
109
110
111
117 [[deprecated("getCollectionCommunication is deprecated. Use getCommunication instead.")]]
119 {
121 }
122
124 getCommunication()
125 {
127 }
128
144 DUNE_EXPORT static FakeMPIHelper& instance([[maybe_unused]] int argc,
145 [[maybe_unused]] char** argv)
146 {
147 return instance();
148 }
149
150 DUNE_EXPORT static FakeMPIHelper& instance()
151 {
152 static FakeMPIHelper singleton;
153 return singleton;
154 }
155
159 int rank () const { return 0; }
163 int size () const { return 1; }
164
165 private:
166 FakeMPIHelper() {}
167 FakeMPIHelper(const FakeMPIHelper&);
168 FakeMPIHelper& operator=(const FakeMPIHelper);
169 };
170
171#if HAVE_MPI
179 {
180 public:
185 constexpr static bool isFake = false;
186
190 typedef MPI_Comm MPICommunicator;
191
199 {
200 return MPI_COMM_WORLD;
201 }
202
210 {
211 return MPI_COMM_SELF;
212 }
213
219 [[deprecated("getCollectionCommunication is deprecated. Use getCommunication instead.")]]
222 {
224 }
225
227 getCommunication()
228 {
230 }
246 DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
247 {
248 // create singleton instance
249 if (!instance_){
250 static std::mutex mutex;
251 std::lock_guard<std::mutex> guard(mutex);
252 if(!instance_)
253 instance_.reset(new MPIHelper(argc,argv));
254 }
255 return *instance_;
256 }
257
258 DUNE_EXPORT static MPIHelper& instance()
259 {
260 if(!instance_)
261 DUNE_THROW(InvalidStateException, "MPIHelper not initialized! Call MPIHelper::instance(argc, argv) with arguments first.");
262 return *instance_;
263 }
264
268 int rank () const { return rank_; }
272 int size () const { return size_; }
273
276 {
277 int wasFinalized = -1;
278 MPI_Finalized( &wasFinalized );
279 if(!wasFinalized && initializedHere_)
280 {
281 MPI_Finalize();
282 dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
283 }
284
285 }
286
287 private:
288 int rank_;
289 int size_;
290 bool initializedHere_;
291 void prevent_warning(int){}
292 static inline std::unique_ptr<MPIHelper> instance_ = {};
293
295 MPIHelper(int& argc, char**& argv)
296 : initializedHere_(false)
297 {
298 int wasInitialized = -1;
299 MPI_Initialized( &wasInitialized );
300 if(!wasInitialized)
301 {
302 rank_ = -1;
303 size_ = -1;
304 static int is_initialized = MPI_Init(&argc, &argv);
305 prevent_warning(is_initialized);
306 initializedHere_ = true;
307 }
308
309 MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
310 MPI_Comm_size(MPI_COMM_WORLD,&size_);
311
312 assert( rank_ >= 0 );
313 assert( size_ >= 1 );
314
315 dverb << "Called MPI_Init on p=" << rank_ << "!" << std::endl;
316 }
317
318 MPIHelper(const MPIHelper&);
319 MPIHelper& operator=(const MPIHelper);
320 };
321#else // !HAVE_MPI
322 // We do not have MPI therefore FakeMPIHelper
323 // is the MPIHelper
328 typedef FakeMPIHelper MPIHelper;
329
330#endif // !HAVE_MPI
331
332} // end namespace Dune
333#endif
Collective communication interface and sequential default implementation.
Definition: communication.hh:100
A fake mpi helper.
Definition: mpihelper.hh:74
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:93
int size() const
return rank of process, i.e. one
Definition: mpihelper.hh:163
static Communication< MPICommunicator > getCollectiveCommunication()
Definition: mpihelper.hh:118
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:105
No_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:85
static DUNE_EXPORT FakeMPIHelper & instance(int argc, char **argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:144
static constexpr bool isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: mpihelper.hh:80
int rank() const
return rank of process, i.e. zero
Definition: mpihelper.hh:159
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
A real mpi helper.
Definition: mpihelper.hh:179
int size() const
return number of processes
Definition: mpihelper.hh:272
static constexpr bool isFake
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition: mpihelper.hh:185
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:246
~MPIHelper()
calls MPI_Finalize
Definition: mpihelper.hh:275
int rank() const
return rank of process
Definition: mpihelper.hh:268
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:190
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:198
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:209
static Communication< MPICommunicator > getCollectiveCommunication()
Definition: mpihelper.hh:221
Implements an utility class that provides collective communication methods for sequential programs.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition: stdstreams.hh:116
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 (Nov 21, 23:30, 2024)