Dune Core Modules (2.6.0)

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#if HAVE_MPI
7#include <cassert>
8#endif
9
10#if HAVE_MPI
11#include <mpi.h>
12#endif
13
15#if HAVE_MPI
18#endif
20
21namespace Dune
22{
73 {
74 public:
75 enum {
80 isFake = true
81 };
82
86 typedef No_Comm MPICommunicator;
87
95 {
96 static MPICommunicator comm;
97 return comm;
98 }
99
107 {
108 return getCommunicator();
109 }
110
111
112
114 getCollectiveCommunication()
115 {
117 }
118
134 DUNE_EXPORT static FakeMPIHelper& instance(int argc, char** argv)
135 {
136 (void)argc; (void)argv;
137 // create singleton instance
138 static FakeMPIHelper singleton;
139 return singleton;
140 }
141
145 int rank () const { return 0; }
149 int size () const { return 1; }
150
151 private:
152 FakeMPIHelper() {}
153 FakeMPIHelper(const FakeMPIHelper&);
154 FakeMPIHelper& operator=(const FakeMPIHelper);
155 };
156
157#if HAVE_MPI
165 {
166 public:
167 enum {
172 isFake = false
173 };
174
178 typedef MPI_Comm MPICommunicator;
179
187 {
188 return MPI_COMM_WORLD;
189 }
190
198 {
199 return MPI_COMM_SELF;
200 }
201
203 getCollectiveCommunication()
204 {
206 }
222 DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
223 {
224 // create singleton instance
225 static MPIHelper singleton (argc, argv);
226 return singleton;
227 }
228
232 int rank () const { return rank_; }
236 int size () const { return size_; }
237
238 private:
239 int rank_;
240 int size_;
241 bool initializedHere_;
242 void prevent_warning(int){}
243
245 MPIHelper(int& argc, char**& argv)
246 : initializedHere_(false)
247 {
248 int wasInitialized = -1;
249 MPI_Initialized( &wasInitialized );
250 if(!wasInitialized)
251 {
252 rank_ = -1;
253 size_ = -1;
254 static int is_initialized = MPI_Init(&argc, &argv);
255 prevent_warning(is_initialized);
256 initializedHere_ = true;
257 }
258
259 MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
260 MPI_Comm_size(MPI_COMM_WORLD,&size_);
261
262 assert( rank_ >= 0 );
263 assert( size_ >= 1 );
264
265 dverb << "Called MPI_Init on p=" << rank_ << "!" << std::endl;
266 }
268 ~MPIHelper()
269 {
270 int wasFinalized = -1;
271 MPI_Finalized( &wasFinalized );
272 if(!wasFinalized && initializedHere_)
273 {
274 MPI_Finalize();
275 dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
276 }
277
278 }
279 MPIHelper(const MPIHelper&);
280 MPIHelper& operator=(const MPIHelper);
281 };
282#else // !HAVE_MPI
283 // We do not have MPI therefore FakeMPIHelper
284 // is the MPIHelper
289 typedef FakeMPIHelper MPIHelper;
290
291#endif // !HAVE_MPI
292
293} // end namespace Dune
294#endif
Collective communication interface and sequential default implementation.
Definition: collectivecommunication.hh:80
A fake mpi helper.
Definition: mpihelper.hh:73
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:94
int size() const
return rank of process, i.e. one
Definition: mpihelper.hh:149
@ isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: mpihelper.hh:80
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:106
No_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:86
static DUNE_EXPORT FakeMPIHelper & instance(int argc, char **argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:134
int rank() const
return rank of process, i.e. zero
Definition: mpihelper.hh:145
A real mpi helper.
Definition: mpihelper.hh:165
int size() const
return number of processes
Definition: mpihelper.hh:236
@ isFake
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition: mpihelper.hh:172
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:222
int rank() const
return rank of process
Definition: mpihelper.hh:232
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:178
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:186
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:197
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: alignedallocator.hh:10
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.111.3 (Jul 15, 22:36, 2024)