Dune Core Modules (2.8.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#include <mpi.h>
9#endif
10
11#include <mutex>
12
14#if HAVE_MPI
17#endif
19
20namespace Dune
21{
72 {
73 public:
74 enum {
79 isFake = true
80 };
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
112 // Will be deprecated after the 2.7 release
113 //[[deprecated("getCollectionCommunication is deprecated. Use getCommunication instead.")]]
114 static Communication<MPICommunicator> getCollectiveCommunication()
115 {
117 }
118
119 static Communication<MPICommunicator>
120 getCommunication()
121 {
122 return Communication<MPICommunicator>(getCommunicator());
123 }
124
140 DUNE_EXPORT static FakeMPIHelper& instance([[maybe_unused]] int argc,
141 [[maybe_unused]] char** argv)
142 {
143 return instance();
144 }
145
146 DUNE_EXPORT static FakeMPIHelper& instance()
147 {
148 static FakeMPIHelper singleton;
149 return singleton;
150 }
151
155 int rank () const { return 0; }
159 int size () const { return 1; }
160
161 private:
162 FakeMPIHelper() {}
163 FakeMPIHelper(const FakeMPIHelper&);
164 FakeMPIHelper& operator=(const FakeMPIHelper);
165 };
166
167#if HAVE_MPI
175 {
176 public:
177 enum {
182 isFake = false
183 };
184
188 typedef MPI_Comm MPICommunicator;
189
197 {
198 return MPI_COMM_WORLD;
199 }
200
208 {
209 return MPI_COMM_SELF;
210 }
211
212 // Will be deprecated after the 2.7 release
213 //[[deprecated("getCollectionCommunication is deprecated. Use getCommunication instead.")]]
215 getCollectiveCommunication()
216 {
218 }
219
220 static Communication<MPICommunicator>
221 getCommunication()
222 {
223 return Communication<MPICommunicator>(getCommunicator());
224 }
240 DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
241 {
242 // create singleton instance
243 if (!instance_){
244 static std::mutex mutex;
245 std::lock_guard<std::mutex> guard(mutex);
246 if(!instance_)
247 instance_.reset(new MPIHelper(argc,argv));
248 }
249 return *instance_;
250 }
251
252 DUNE_EXPORT static MPIHelper& instance()
253 {
254 if(!instance_)
255 DUNE_THROW(InvalidStateException, "MPIHelper not initialized! Call MPIHelper::instance(argc, argv) with arguments first.");
256 return *instance_;
257 }
258
262 int rank () const { return rank_; }
266 int size () const { return size_; }
267
270 {
271 int wasFinalized = -1;
272 MPI_Finalized( &wasFinalized );
273 if(!wasFinalized && initializedHere_)
274 {
275 MPI_Finalize();
276 dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
277 }
278
279 }
280
281 private:
282 int rank_;
283 int size_;
284 bool initializedHere_;
285 void prevent_warning(int){}
286 static inline std::unique_ptr<MPIHelper> instance_ = {};
287
289 MPIHelper(int& argc, char**& argv)
290 : initializedHere_(false)
291 {
292 int wasInitialized = -1;
293 MPI_Initialized( &wasInitialized );
294 if(!wasInitialized)
295 {
296 rank_ = -1;
297 size_ = -1;
298 static int is_initialized = MPI_Init(&argc, &argv);
299 prevent_warning(is_initialized);
300 initializedHere_ = true;
301 }
302
303 MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
304 MPI_Comm_size(MPI_COMM_WORLD,&size_);
305
306 assert( rank_ >= 0 );
307 assert( size_ >= 1 );
308
309 dverb << "Called MPI_Init on p=" << rank_ << "!" << std::endl;
310 }
311
312 MPIHelper(const MPIHelper&);
313 MPIHelper& operator=(const MPIHelper);
314 };
315#else // !HAVE_MPI
316 // We do not have MPI therefore FakeMPIHelper
317 // is the MPIHelper
322 typedef FakeMPIHelper MPIHelper;
323
324#endif // !HAVE_MPI
325
326} // end namespace Dune
327#endif
Collective communication interface and sequential default implementation.
Definition: communication.hh:98
A fake mpi helper.
Definition: mpihelper.hh:72
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:159
@ isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.)
Definition: mpihelper.hh:79
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:140
int rank() const
return rank of process, i.e. zero
Definition: mpihelper.hh:155
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:279
A real mpi helper.
Definition: mpihelper.hh:175
int size() const
return number of processes
Definition: mpihelper.hh:266
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition: mpihelper.hh:240
~MPIHelper()
calls MPI_Finalize
Definition: mpihelper.hh:269
int rank() const
return rank of process
Definition: mpihelper.hh:262
@ isFake
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition: mpihelper.hh:182
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:188
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:196
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition: mpihelper.hh:207
Implements an utility class that provides collective communication methods for sequential programs.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
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:11
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 (Nov 12, 23:30, 2024)