Loading...
Searching...
No Matches
mpihelper.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_PARALLEL_MPIHELPER_HH
#define DUNE_COMMON_PARALLEL_MPIHELPER_HH
#include <cassert>
#include <mutex>
#if HAVE_MPI
#include <mpi.h>
#endif
#include <dune/common/exceptions.hh>
#include <dune/common/visibility.hh>
#include <dune/common/parallel/communication.hh>
#include <dune/common/parallel/mpicommunication.hh>
#if HAVE_MPI
#include <dune/common/stdstreams.hh>
#endif
namespace Dune
{
class FakeMPIHelper
{
public:
constexpr static bool isFake = true;
typedef No_Comm MPICommunicator;
DUNE_EXPORT static MPICommunicator getCommunicator ()
{
static MPICommunicator comm;
return comm;
}
static MPICommunicator getLocalCommunicator ()
{
return getCommunicator();
}
static Communication<MPICommunicator>
getCommunication()
{
return Communication<MPICommunicator>(getCommunicator());
}
DUNE_EXPORT static FakeMPIHelper& instance([[maybe_unused]] int argc,
[[maybe_unused]] char** argv)
{
return instance();
}
DUNE_EXPORT static FakeMPIHelper& instance()
{
static FakeMPIHelper singleton;
return singleton;
}
int rank () const { return 0; }
int size () const { return 1; }
private:
FakeMPIHelper() {}
FakeMPIHelper(const FakeMPIHelper&);
FakeMPIHelper& operator=(const FakeMPIHelper);
};
#if HAVE_MPI
class MPIHelper
{
public:
constexpr static bool isFake = false;
typedef MPI_Comm MPICommunicator;
static MPICommunicator getCommunicator ()
{
return MPI_COMM_WORLD;
}
static MPICommunicator getLocalCommunicator ()
{
return MPI_COMM_SELF;
}
static Communication<MPICommunicator>
getCommunication()
{
return Communication<MPICommunicator>(getCommunicator());
}
DUNE_EXPORT static MPIHelper& instance(int& argc, char**& argv)
{
return instance(&argc, &argv);
}
DUNE_EXPORT static MPIHelper& instance(int* argc = nullptr, char*** argv = nullptr)
{
assert((argc == nullptr) == (argv == nullptr));
static MPIHelper instance{argc, argv};
return instance;
}
int rank () const { return rank_; }
int size () const { return size_; }
~MPIHelper()
{
int wasFinalized = -1;
MPI_Finalized( &wasFinalized );
if(!wasFinalized && initializedHere_)
{
MPI_Finalize();
dverb << "Called MPI_Finalize on p=" << rank_ << "!" <<std::endl;
}
}
private:
int rank_;
int size_;
bool initializedHere_;
void prevent_warning(int){}
MPIHelper(int* argc, char*** argv)
: initializedHere_(false)
{
int wasInitialized = -1;
MPI_Initialized( &wasInitialized );
if(!wasInitialized)
{
rank_ = -1;
size_ = -1;
static int is_initialized = MPI_Init(argc, argv);
prevent_warning(is_initialized);
initializedHere_ = true;
}
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;
}
MPIHelper(const MPIHelper&);
MPIHelper& operator=(const MPIHelper);
};
#else // !HAVE_MPI
// We do not have MPI therefore FakeMPIHelper
// is the MPIHelper
typedef FakeMPIHelper MPIHelper;
#endif // !HAVE_MPI
} // end namespace Dune
#endif // DUNE_COMMON_PARALLEL_MPIHELPER_HH
mpicommunication.hh
Implements an utility class that provides MPI's collective communication methods.
communication.hh
Implements an utility class that provides collective communication methods for sequential programs.
exceptions.hh
A few common exception classes.
visibility.hh
Definition of macros controlling symbol visibility at the ABI level.
DUNE_EXPORT
#define DUNE_EXPORT
Export a symbol as part of the public ABI.
Definition visibility.hh:20
stdstreams.hh
Standard Dune debug streams.
Dune::dverb
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition stdstreams.hh:117
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::No_Comm
Definition communication.hh:46
Dune::Communication
Collective communication interface and sequential default implementation.
Definition communication.hh:100
Dune::FakeMPIHelper
A fake mpi helper.
Definition mpihelper.hh:76
Dune::FakeMPIHelper::getCommunicator
static DUNE_EXPORT MPICommunicator getCommunicator()
get the default communicator
Definition mpihelper.hh:95
Dune::FakeMPIHelper::size
int size() const
return rank of process, i.e. one
Definition mpihelper.hh:153
Dune::FakeMPIHelper::getLocalCommunicator
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition mpihelper.hh:107
Dune::FakeMPIHelper::MPICommunicator
No_Comm MPICommunicator
The type of the mpi communicator.
Definition mpihelper.hh:87
Dune::FakeMPIHelper::instance
static DUNE_EXPORT FakeMPIHelper & instance(int argc, char **argv)
Get the singleton instance of the helper.
Definition mpihelper.hh:134
Dune::FakeMPIHelper::instance
static DUNE_EXPORT FakeMPIHelper & instance()
Definition mpihelper.hh:140
Dune::FakeMPIHelper::getCommunication
static Communication< MPICommunicator > getCommunication()
Definition mpihelper.hh:114
Dune::FakeMPIHelper::isFake
static constexpr bool isFake
Are we fake (i.e. pretend to have MPI support but are compiled without.).
Definition mpihelper.hh:82
Dune::FakeMPIHelper::rank
int rank() const
return rank of process, i.e. zero
Definition mpihelper.hh:149
Dune::MPIHelper
A real mpi helper.
Definition mpihelper.hh:169
Dune::MPIHelper::size
int size() const
return number of processes
Definition mpihelper.hh:274
Dune::MPIHelper::isFake
static constexpr bool isFake
Are we fake (i. e. pretend to have MPI support but are compiled without.
Definition mpihelper.hh:175
Dune::MPIHelper::instance
static DUNE_EXPORT MPIHelper & instance(int *argc=nullptr, char ***argv=nullptr)
Get the singleton instance of the helper.
Definition mpihelper.hh:260
Dune::MPIHelper::instance
static DUNE_EXPORT MPIHelper & instance(int &argc, char **&argv)
Get the singleton instance of the helper.
Definition mpihelper.hh:228
Dune::MPIHelper::getCommunication
static Communication< MPICommunicator > getCommunication()
Definition mpihelper.hh:205
Dune::MPIHelper::~MPIHelper
~MPIHelper()
calls MPI_Finalize
Definition mpihelper.hh:277
Dune::MPIHelper::rank
int rank() const
return rank of process
Definition mpihelper.hh:270
Dune::MPIHelper::MPICommunicator
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition mpihelper.hh:180
Dune::MPIHelper::getCommunicator
static MPICommunicator getCommunicator()
get the default communicator
Definition mpihelper.hh:188
Dune::MPIHelper::getLocalCommunicator
static MPICommunicator getLocalCommunicator()
get a local communicator
Definition mpihelper.hh:199