/usr/include/dune/common/parallel/mpihelper.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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// $Id: $
#ifndef DUNE_MPIHELPER
#define DUNE_MPIHELPER
#include <cassert>
#include "collectivecommunication.hh"
#if HAVE_MPI
#include "mpi.h"
#include "mpicollectivecommunication.hh"
#endif
#include <dune/common/stdstreams.hh>
#include <dune/common/visibility.hh>
namespace Dune
{
/**
* @file
* @brief Helpers for dealing with MPI.
*
* @ingroup ParallelCommunication
*
* Basically there are two helpers available:
* <dl>
* <dt>FakeMPIHelper</dt>
* <dd>A class adhering to the interface of MPIHelper
* that does not need MPI at all. This can be used
* to create a sequential program even if MPI is
* used to compile it.
* </dd>
* <dt>MPIHelper</dt>
* <dd>A real MPI helper. When the singleton
* gets instantiated MPI_Init will be
* called and before the program exits
* MPI_Finalize will be called.
* </dd>
* </dl>
*
* Example of who to use these classes:
*
* A program that is parallel if compiled with MPI
* and sequential otherwise:
* \code
* int main(int argc, char** argv){
* typedef Dune::MPIHelper MPIHelper;
* MPIHelper::instance(argc, argv);
* typename MPIHelper::MPICommunicator world =
* MPIHelper::getCommunicator();
* ...
* \endcode
*
* If one wants to have sequential program even if the code is
* compiled with mpi then one simply has to exchange the typedef
* with \code typedef Dune::MPIHelper FakeMPIHelper; \endcode.
*
* For checking whether we really use MPI or just fake please use
* MPIHelper::isFake (this is also possible at compile time!)
*/
/**
* @brief A fake mpi helper.
*
* This helper can be used if no MPI is available
* or one wants to run sequentially even if MPI is
* available and used.
*/
class FakeMPIHelper
{
public:
enum {
/**
* @brief Are we fake (i.e. pretend to have MPI support but are compiled
* without.)
*/
isFake = true
};
/**
* @brief The type of the mpi communicator.
*/
typedef No_Comm MPICommunicator;
/** \brief get the default communicator
*
* Return a communicator to exchange data with all processes
*
* \returns a fake communicator
*/
DUNE_EXPORT static MPICommunicator getCommunicator ()
{
static MPICommunicator comm;
return comm;
}
/** \brief get a local communicator
*
* Returns a communicator to communicate with the local process only
*
* \returns a fake communicator
*/
static MPICommunicator getLocalCommunicator ()
{
return getCommunicator();
}
static CollectiveCommunication<MPICommunicator>
getCollectiveCommunication()
{
return CollectiveCommunication<MPICommunicator>(getCommunicator());
}
/**
* @brief Get the singleton instance of the helper.
*
* This method has to be called with the same arguments
* that the main method of the program was called:
* \code
* int main(int argc, char** argv){
* MPIHelper::instance(argc, argv);
* // program code comes here
* ...
* }
* \endcode
* @param argc The number of arguments provided to main.
* @param argv The arguments provided to main.
*/
DUNE_EXPORT static FakeMPIHelper& instance(int argc, char** argv)
{
(void)argc; (void)argv;
// create singleton instance
static FakeMPIHelper singleton;
return singleton;
}
/**
* @brief return rank of process, i.e. zero
*/
int rank () const { return 0; }
/**
* @brief return rank of process, i.e. one
*/
int size () const { return 1; }
private:
FakeMPIHelper() {}
FakeMPIHelper(const FakeMPIHelper&);
FakeMPIHelper& operator=(const FakeMPIHelper);
};
#if HAVE_MPI
/**
* @brief A real mpi helper.
* @ingroup ParallelCommunication
*
* This helper should be used for parallel programs.
*/
class MPIHelper
{
public:
enum {
/**
* @brief Are we fake (i. e. pretend to have MPI support but are compiled
* without.
*/
isFake = false
};
/**
* @brief The type of the mpi communicator.
*/
typedef MPI_Comm MPICommunicator;
/** \brief get the default communicator
*
* Return a communicator to exchange data with all processes
*
* \returns MPI_COMM_WORLD
*/
static MPICommunicator getCommunicator ()
{
return MPI_COMM_WORLD;
}
/** \brief get a local communicator
*
* Returns a communicator to exchange data with the local process only
*
* \returns MPI_COMM_SELF
*/
static MPICommunicator getLocalCommunicator ()
{
return MPI_COMM_SELF;
}
static CollectiveCommunication<MPICommunicator>
getCollectiveCommunication()
{
return CollectiveCommunication<MPICommunicator>(getCommunicator());
}
/**
* @brief Get the singleton instance of the helper.
*
* This method has to be called with the same arguments
* that the main method of the program was called:
* \code
* int main(int argc, char** argv){
* MPIHelper::instance(argc, argv);
* // program code comes here
* ...
* }
* \endcode
* @param argc The number of arguments provided to main.
* @param argv The arguments provided to main.
*/
DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
{
// create singleton instance
static MPIHelper singleton (argc, argv);
return singleton;
}
/**
* @brief return rank of process
*/
int rank () const { return rank_; }
/**
* @brief return number of processes
*/
int size () const { return size_; }
private:
int rank_;
int size_;
void prevent_warning(int){}
//! \brief calls MPI_Init with argc and argv as parameters
MPIHelper(int& argc, char**& argv)
{
#if MPI_2
int wasInitialized = -1;
MPI_Initialized( &wasInitialized );
if(!wasInitialized)
#endif
{
rank_ = -1;
size_ = -1;
static int is_initialized = MPI_Init(&argc, &argv);
prevent_warning(is_initialized);
}
MPI_Comm_rank(MPI_COMM_WORLD,&rank_);
MPI_Comm_size(MPI_COMM_WORLD,&size_);
assert( rank_ >= 0 );
assert( size_ >= 1 );
dverb << "Called MPI_Init on p=" << rank_ << "!" << std::endl;
}
//! \brief calls MPI_Finalize
~MPIHelper()
{
#ifdef MPI_2
int wasFinalized = -1;
MPI_Finalized( &wasFinalized );
if(!wasFinalized) {
#endif
MPI_Finalize();
dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
#ifdef MPI_2
}
#endif
}
MPIHelper(const MPIHelper&);
MPIHelper& operator=(const MPIHelper);
};
#else
// We do not have MPI therefore FakeMPIHelper
// is the MPIHelper
/**
* @brief If no MPI is available FakeMPIHelper becomes the MPIHelper
* @ingroup ParallelCommunication
*/
typedef FakeMPIHelper MPIHelper;
#endif
} // end namespace Dune
#endif
|