Dune Core Modules (2.4.2)

mpiguard.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
11#ifndef DUNE_COMMON_MPIGUARD_HH
12#define DUNE_COMMON_MPIGUARD_HH
13
14#include "mpihelper.hh"
18
19namespace Dune
20{
21
22#ifndef DOXYGEN
23
24 /*
25 Interface class for the communication needed by MPIGuard
26 */
27 struct GuardCommunicator
28 {
29 // cleanup
30 virtual ~GuardCommunicator() {};
31 // all the communication methods we need
32 virtual int rank() = 0;
33 virtual int size() = 0;
34 virtual int sum(int i) = 0;
35 // create a new GuardCommunicator pointer
36 template <class C>
37 static GuardCommunicator * create(const C & c);
38 };
39
40 namespace {
41 /*
42 templated implementation of different communication classes
43 */
44 // the default class will always fail, due to the missing implementation of "sum"
45 template <class Imp>
46 struct GenericGuardCommunicator
47 : public GuardCommunicator
48 {};
49 // specialization for CollectiveCommunication
50 template <class T>
51 struct GenericGuardCommunicator< CollectiveCommunication<T> >
52 : public GuardCommunicator
53 {
54 const CollectiveCommunication<T> comm;
55 GenericGuardCommunicator(const CollectiveCommunication<T> & c) :
56 comm(c) {}
57 virtual int rank() { return comm.rank(); };
58 virtual int size() { return comm.size(); };
59 virtual int sum(int i) { return comm.sum(i); }
60 };
61
62#if HAVE_MPI
63 // specialization for MPI_Comm
64 template <>
65 struct GenericGuardCommunicator<MPI_Comm>
66 : public GenericGuardCommunicator< CollectiveCommunication<MPI_Comm> >
67 {
68 GenericGuardCommunicator(const MPI_Comm & c) :
69 GenericGuardCommunicator< CollectiveCommunication<MPI_Comm> >(
70 CollectiveCommunication<MPI_Comm>(c)) {}
71 };
72#endif
73 } // anonymous namespace
74
75 template<class C>
76 GuardCommunicator * GuardCommunicator::create(const C & comm)
77 {
78 return new GenericGuardCommunicator<C>(comm);
79 }
80#endif
81
85 class MPIGuardError : public ParallelError {};
86
120 {
121 GuardCommunicator * comm_;
122 bool active_;
123
124 // we don't want to copy this class
125 MPIGuard (const MPIGuard &);
126
127 public:
132 MPIGuard (bool active=true) :
133 comm_(GuardCommunicator::create(
134 MPIHelper::getCollectiveCommunication())),
135 active_(active)
136 {}
137
143 MPIGuard (MPIHelper & m, bool active=true) :
144 comm_(GuardCommunicator::create(
145 m.getCollectiveCommunication())),
146 active_(active)
147 {}
148
159 template <class C>
160 MPIGuard (const C & comm, bool active=true) :
161 comm_(GuardCommunicator::create(comm)),
162 active_(active)
163 {}
164
168 {
169 if (active_)
170 {
171 active_ = false;
172 finalize(false);
173 }
174 delete comm_;
175 }
176
181 void reactivate() {
182 if (active_ == true)
183 finalize();
184 active_ = true;
185 }
186
197 void finalize(bool success = true)
198 {
199 int result = success ? 0 : 1;
200 bool was_active = active_;
201 active_ = false;
202 result = comm_->sum(result);
203 if (result>0 && was_active)
204 {
205 DUNE_THROW(MPIGuardError, "Terminating process "
206 << comm_->rank() << " due to "
207 << result << " remote error(s)");
208 }
209 }
210 };
211
212}
213
214#endif // DUNE_COMMON_MPIGUARD_HH
This exception is thrown if the MPIGuard detects an error on a remote process.
Definition: mpiguard.hh:85
detects a thrown exception and communicates to all other processes
Definition: mpiguard.hh:120
void reactivate()
reactivate the guard.
Definition: mpiguard.hh:181
void finalize(bool success=true)
stop the guard.
Definition: mpiguard.hh:197
~MPIGuard()
destroy the guard and check for undetected exceptions
Definition: mpiguard.hh:167
MPIGuard(const C &comm, bool active=true)
create an MPIGuard operating on an arbitrary communicator.
Definition: mpiguard.hh:160
MPIGuard(bool active=true)
create an MPIGuard operating on the Communicator of the global Dune::MPIHelper
Definition: mpiguard.hh:132
MPIGuard(MPIHelper &m, bool active=true)
create an MPIGuard operating on the Communicator of a special Dune::MPIHelper m
Definition: mpiguard.hh:143
A real mpi helper.
Definition: mpihelper.hh:160
Default exception if an error in the parallel communication of the programm occured.
Definition: exceptions.hh:312
Implements an utility class that provides collective communication methods for sequential programs.
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
Implements an utility class that provides MPI's collective communication methods.
Helpers for dealing with MPI.
Dune namespace.
Definition: alignment.hh:10
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)