Dune Core Modules (2.3.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// $Id: $
4#ifndef DUNE_MPIHELPER
5#define DUNE_MPIHELPER
6
7#include <cassert>
9#if HAVE_MPI
10#include "mpi.h"
12#endif
13
16
17namespace Dune
18{
69 {
70 public:
71 enum {
76 isFake = true
77 };
78
82 typedef No_Comm MPICommunicator;
83
91 {
92 static MPICommunicator comm;
93 return comm;
94 }
95
103 {
104 return getCommunicator();
105 }
106
107
108
110 getCollectiveCommunication()
111 {
113 }
114
130 DUNE_EXPORT static FakeMPIHelper& instance(int argc, char** argv)
131 {
132 (void)argc; (void)argv;
133 // create singleton instance
134 static FakeMPIHelper singleton;
135 return singleton;
136 }
137
141 int rank () const { return 0; }
145 int size () const { return 1; }
146
147 private:
148 FakeMPIHelper() {}
149 FakeMPIHelper(const FakeMPIHelper&);
150 FakeMPIHelper& operator=(const FakeMPIHelper);
151 };
152
153#if HAVE_MPI
161 {
162 public:
163 enum {
168 isFake = false
169 };
170
174 typedef MPI_Comm MPICommunicator;
175
183 {
184 return MPI_COMM_WORLD;
185 }
186
194 {
195 return MPI_COMM_SELF;
196 }
197
199 getCollectiveCommunication()
200 {
202 }
218 DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
219 {
220 // create singleton instance
221 static MPIHelper singleton (argc, argv);
222 return singleton;
223 }
224
228 int rank () const { return rank_; }
232 int size () const { return size_; }
233
234 private:
235 int rank_;
236 int size_;
237 void prevent_warning(int){}
238
240 MPIHelper(int& argc, char**& argv)
241 {
242#if MPI_2
243 int wasInitialized = -1;
244 MPI_Initialized( &wasInitialized );
245 if(!wasInitialized)
246#endif
247 {
248 rank_ = -1;
249 size_ = -1;
250 static int is_initialized = MPI_Init(&argc, &argv);
251 prevent_warning(is_initialized);
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#ifdef MPI_2
266 int wasFinalized = -1;
267 MPI_Finalized( &wasFinalized );
268 if(!wasFinalized) {
269#endif
270 MPI_Finalize();
271 dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
272#ifdef MPI_2
273 }
274
275#endif
276 }
277 MPIHelper(const MPIHelper&);
278 MPIHelper& operator=(const MPIHelper);
279 };
280#else
281 // We do not have MPI therefore FakeMPIHelper
282 // is the MPIHelper
287 typedef FakeMPIHelper MPIHelper;
288
289#endif
290
291} // end namespace Dune
292#endif
Collective communication interface and sequential default implementation.
Definition: collectivecommunication.hh:72
A fake mpi helper.
Definition: mpihelper.hh:69
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:90
int size() const
return rank of process, i.e. one
Definition: mpihelper.hh:145
@ isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: mpihelper.hh:76
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:102
No_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:82
static DUNE_EXPORT FakeMPIHelper & instance(int argc, char **argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:130
int rank() const
return rank of process, i.e. zero
Definition: mpihelper.hh:141
A real mpi helper.
Definition: mpihelper.hh:161
@ isFake
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition: mpihelper.hh:168
int size() const
return number of processes
Definition: mpihelper.hh:232
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:218
int rank() const
return rank of process
Definition: mpihelper.hh:228
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:174
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:182
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:193
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition: stdstreams.hh:115
Dune namespace.
Definition: alignment.hh:14
Implements an utility class that provides collective communication methods for sequential programs.
Implements an utility class that provides MPI's collective communication methods.
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)