/usr/include/dune/common/parallel/mpiguard.hh is in libdune-common-dev 2.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
/**
* @file
* @brief Implements a MPIGuard which detects an error on a remote process
* @author Christian Engwer
* @ingroup ParallelCommunication
*/
#ifndef DUNE_COMMON_MPIGUARD_HH
#define DUNE_COMMON_MPIGUARD_HH
#include "mpihelper.hh"
#include "collectivecommunication.hh"
#include "mpicollectivecommunication.hh"
#include <dune/common/exceptions.hh>
namespace Dune
{
#ifndef DOXYGEN
/*
Interface class for the communication needed by MPIGuard
*/
struct GuardCommunicator
{
// cleanup
virtual ~GuardCommunicator() {};
// all the communication methods we need
virtual int rank() = 0;
virtual int size() = 0;
virtual int sum(int i) = 0;
// create a new GuardCommunicator pointer
template <class C>
static GuardCommunicator * create(const C & c);
};
namespace {
/*
templated implementation of different communication classes
*/
// the default class will always fail, due to the missing implementation of "sum"
template <class Imp>
struct GenericGuardCommunicator
: public GuardCommunicator
{};
// specialization for CollectiveCommunication
template <class T>
struct GenericGuardCommunicator< CollectiveCommunication<T> >
: public GuardCommunicator
{
const CollectiveCommunication<T> comm;
GenericGuardCommunicator(const CollectiveCommunication<T> & c) :
comm(c) {}
virtual int rank() { return comm.rank(); };
virtual int size() { return comm.size(); };
virtual int sum(int i) { return comm.sum(i); }
};
#if HAVE_MPI
// specialization for MPI_Comm
template <>
struct GenericGuardCommunicator<MPI_Comm>
: public GenericGuardCommunicator< CollectiveCommunication<MPI_Comm> >
{
GenericGuardCommunicator(const MPI_Comm & c) :
GenericGuardCommunicator< CollectiveCommunication<MPI_Comm> >(
CollectiveCommunication<MPI_Comm>(c)) {}
};
#endif
} // anonymous namespace
template<class C>
GuardCommunicator * GuardCommunicator::create(const C & comm)
{
return new GenericGuardCommunicator<C>(comm);
}
#endif
/*! @brief This exception is thrown if the MPIGuard detects an error on a remote process
@ingroup ParallelCommunication
*/
class MPIGuardError : public ParallelError {};
/*! @brief detects a thrown exception and communicates to all other processes
@ingroup ParallelCommunication
@code
{
MPIGuard guard(...);
do_something();
// tell the guard that you successfully passed a critical operation
guard.finalize();
// reactivate the guard for the next critical operation
guard.reactivate();
int result = do_something_else();
// tell the guard the result of your operation
guard.finalize(result == success);
}
@endcode
You create a MPIGuard object. If an exception is risen on a
process the MPIGuard detects the exception, because the finalize
method was not called. when reaching the finalize call all
other processes are informed that an error occured and the
MPIGuard throws an exception of type MPIGuardError.
@note You can initialize the MPIGuard from different types of communication objects:
- MPIHelper
- CollectiveCommunication
- MPI_Comm
*/
class MPIGuard
{
GuardCommunicator * comm_;
bool active_;
// we don't want to copy this class
MPIGuard (const MPIGuard &);
public:
/*! @brief create an MPIGuard operating on the Communicator of the global Dune::MPIHelper
@param active should the MPIGuard be active upon creation?
*/
MPIGuard (bool active=true) :
comm_(GuardCommunicator::create(
MPIHelper::getCollectiveCommunication())),
active_(active)
{}
/*! @brief create an MPIGuard operating on the Communicator of a special Dune::MPIHelper m
@param m a reference to an MPIHelper
@param active should the MPIGuard be active upon creation?
*/
MPIGuard (MPIHelper & m, bool active=true) :
comm_(GuardCommunicator::create(
m.getCollectiveCommunication())),
active_(active)
{}
/*! @brief create an MPIGuard operating on an arbitrary communicator.
Supported types for the communication object are:
- MPIHelper
- CollectiveCommunication
- MPI_Comm
@param comm reference to a communication object
@param active should the MPIGuard be active upon creation?
*/
template <class C>
MPIGuard (const C & comm, bool active=true) :
comm_(GuardCommunicator::create(comm)),
active_(active)
{}
/*! @brief destroy the guard and check for undetected exceptions
*/
~MPIGuard()
{
if (active_)
{
active_ = false;
finalize(false);
}
delete comm_;
}
/*! @brief reactivate the guard.
If the guard is still active finalize(true) is called first.
*/
void reactivate() {
if (active_ == true)
finalize();
active_ = true;
}
/*! @brief stop the guard.
If no success parameter is passed, the guard assumes that
everything worked as planned. All errors are communicated
and an exception of type MPIGuardError is thrown if an error
(or exception) occured on any of the processors in the
communicator.
@param success inform the guard about possible errors
*/
void finalize(bool success = true)
{
int result = success ? 0 : 1;
bool was_active = active_;
active_ = false;
result = comm_->sum(result);
if (result>0 && was_active)
{
DUNE_THROW(MPIGuardError, "Terminating process "
<< comm_->rank() << " due to "
<< result << " remote error(s)");
}
}
};
}
#endif // DUNE_COMMON_MPIGUARD_HH
|