Loading...
Searching...
No Matches
communication.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_COMMUNICATION_HH
#define DUNE_COMMON_PARALLEL_COMMUNICATION_HH
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <dune/common/binaryfunctions.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/parallel/future.hh>
namespace Dune
{
/* define some type that definitely differs from MPI_Comm */
struct No_Comm {};
inline bool operator==(const No_Comm&, const No_Comm&)
{
return true;
}
inline bool operator!=(const No_Comm&, const No_Comm&)
{
return false;
}
template<typename Communicator>
class Communication
{
public:
Communication()
{}
Communication (const Communicator&)
{}
int rank () const
{
return 0;
}
operator No_Comm() const
{
return {};
}
int size () const
{
return 1;
}
template<class T>
int send([[maybe_unused]] const T& data,
[[maybe_unused]] int dest_rank,
[[maybe_unused]] int tag) const
{
DUNE_THROW(ParallelError, "This method is not supported in sequential programs");
}
template<class T>
PseudoFuture<T> isend([[maybe_unused]] T&& data,
[[maybe_unused]] int dest_rank,
[[maybe_unused]] int tag) const
{
DUNE_THROW(ParallelError, "This method is not supported in sequential programs");
}
template<class T>
T recv([[maybe_unused]] T&& data,
[[maybe_unused]] int source_rank,
[[maybe_unused]] int tag,
[[maybe_unused]] void* status = 0) const
{
DUNE_THROW(ParallelError, "This method is not supported in sequential programs");
}
template<class T>
PseudoFuture<T> irecv([[maybe_unused]] T&& data,
[[maybe_unused]] int source_rank,
[[maybe_unused]] int tag) const
{
DUNE_THROW(ParallelError, "This method is not supported in sequential programs");
}
template<class T>
T rrecv([[maybe_unused]] T&& data,
[[maybe_unused]] int source_rank,
[[maybe_unused]] int tag,
[[maybe_unused]] void* status = 0) const
{
DUNE_THROW(ParallelError, "This method is not supported in sequential programs");
}
template<typename T>
T sum (const T& in) const
{
return in;
}
template<typename T>
int sum ([[maybe_unused]] T* inout, [[maybe_unused]] int len) const
{
return 0;
}
template<typename T>
T prod (const T& in) const
{
return in;
}
template<typename T>
int prod ([[maybe_unused]] T* inout, [[maybe_unused]] int len) const
{
return 0;
}
template<typename T>
T min (const T& in) const
{
return in;
}
template<typename T>
int min ([[maybe_unused]] T* inout, [[maybe_unused]] int len) const
{
return 0;
}
template<typename T>
T max (const T& in) const
{
return in;
}
template<typename T>
int max ([[maybe_unused]] T* inout, [[maybe_unused]] int len) const
{
return 0;
}
int barrier () const
{
return 0;
}
PseudoFuture<void> ibarrier () const
{
return {true}; // return a valid future
}
template<typename T>
int broadcast ([[maybe_unused]] T* inout,
[[maybe_unused]] int len,
[[maybe_unused]] int root) const
{
return 0;
}
template<class T>
PseudoFuture<T> ibroadcast(T&& data, int /*root*/) const{
return {std::forward<T>(data)};
}
template<typename T>
int gather (const T* in, T* out, int len, int /*root*/) const // note out must have same size as in
{
for (int i=0; i<len; i++)
out[i] = in[i];
return 0;
}
template<class TIN, class TOUT = std::vector<TIN>>
PseudoFuture<TOUT> igather(TIN&& data_in, TOUT&& data_out, int /*root*/){
*(data_out.begin()) = std::forward<TIN>(data_in);
return {std::forward<TOUT>(data_out)};
}
template<typename T>
int gatherv (const T* in,
int sendDataLen,
T* out,
[[maybe_unused]] int* recvDataLen,
int* displ,
[[maybe_unused]] int root) const
{
for (int i=*displ; i<sendDataLen; i++)
out[i] = in[i];
return 0;
}
template<typename T>
int scatter (const T* sendData, T* recvData, int len, int /*root*/) const // note out must have same size as in
{
for (int i=0; i<len; i++)
recvData[i] = sendData[i];
return 0;
}
template<class TIN, class TOUT = TIN>
PseudoFuture<TOUT> iscatter(TIN&& data_in, TOUT&& data_out, int /*root*/){
data_out = *(std::forward<TIN>(data_in).begin());
return {std::forward<TOUT>(data_out)};
}
template<typename T>
int scatterv (const T* sendData,int* sendDataLen, int* displ, T* recvData,
[[maybe_unused]] int recvDataLen, [[maybe_unused]] int root) const
{
for (int i=*displ; i<*sendDataLen; i++)
recvData[i] = sendData[i];
return 0;
}
template<typename T>
int allgather(const T* sbuf, int count, T* rbuf) const
{
for(const T* end=sbuf+count; sbuf < end; ++sbuf, ++rbuf)
*rbuf=*sbuf;
return 0;
}
template<class TIN, class TOUT = TIN>
PseudoFuture<TOUT> iallgather(TIN&& /*data_in*/, TOUT&& data_out){
return {std::forward<TOUT>(data_out)};
}
template<typename T>
int allgatherv (const T* in, int sendDataLen, T* out, [[maybe_unused]] int* recvDataLen, int* displ) const
{
for (int i=*displ; i<sendDataLen; i++)
out[i] = in[i];
return 0;
}
template<typename BinaryFunction, typename Type>
int allreduce([[maybe_unused]] Type* inout, [[maybe_unused]] int len) const
{
return 0;
}
template<class BinaryFunction, class TIN, class TOUT = TIN>
PseudoFuture<TOUT> iallreduce(TIN&& data_in, TOUT&& data_out){
data_out = std::forward<TIN>(data_in);
return {std::forward<TOUT>(data_out)};
}
template<class BinaryFunction, class T>
PseudoFuture<T> iallreduce(T&& data){
return {std::forward<T>(data)};
}
template<typename BinaryFunction, typename Type>
int allreduce(const Type* in, Type* out, int len) const
{
std::copy(in, in+len, out);
return 0;
}
};
}
#endif // DUNE_COMMON_PARALLEL_COMMUNICATION_HH
future.hh
exceptions.hh
A few common exception classes.
binaryfunctions.hh
helper classes to provide unique types for standard functions
DUNE_THROW
#define DUNE_THROW(E,...)
Definition exceptions.hh:314
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::operator!=
constexpr auto operator!=(const HybridMultiIndex< S... > &lhs, const HybridMultiIndex< T... > &rhs)
Compare two HybridMultiIndexs for inequality.
Definition hybridmultiindex.hh:441
Dune::operator==
constexpr bool operator==(const HybridMultiIndex< S... > &lhs, const HybridMultiIndex< T... > &rhs)
Compare two HybridMultiIndexs for value equality.
Definition hybridmultiindex.hh:404
Dune::ParallelError
Default exception if an error in the parallel communication of the program occurred.
Definition exceptions.hh:381
Dune::No_Comm
Definition communication.hh:46
Dune::Communication::ibroadcast
PseudoFuture< T > ibroadcast(T &&data, int) const
Distribute an array from the process with rank root to all other processes nonblocking.
Definition communication.hh:295
Dune::Communication::iallreduce
PseudoFuture< TOUT > iallreduce(TIN &&data_in, TOUT &&data_out)
Compute something over all processes nonblocking.
Definition communication.hh:496
Dune::Communication::allreduce
int allreduce(const Type *in, Type *out, int len) const
Compute something over all processes for each component of an array and return the result in every pr...
Definition communication.hh:525
Dune::Communication::max
T max(const T &in) const
Compute the maximum of the argument over all processes and return the result in every process....
Definition communication.hh:248
Dune::Communication::rank
int rank() const
Return rank, is between 0 and size()-1.
Definition communication.hh:114
Dune::Communication::sum
T sum(const T &in) const
Compute the sum of the argument over all processes and return the result in every process....
Definition communication.hh:188
Dune::Communication::scatterv
int scatterv(const T *sendData, int *sendDataLen, int *displ, T *recvData, int recvDataLen, int root) const
Scatter arrays of variable length from a root to all other tasks.
Definition communication.hh:411
Dune::Communication::prod
int prod(T *inout, int len) const
Compute the product over all processes for each component of an array and return the result in every ...
Definition communication.hh:219
Dune::Communication::ibarrier
PseudoFuture< void > ibarrier() const
Nonblocking barrier.
Definition communication.hh:275
Dune::Communication::allreduce
int allreduce(Type *inout, int len) const
Compute something over all processes for each component of an array and return the result in every pr...
Definition communication.hh:486
Dune::Communication::scatter
int scatter(const T *sendData, T *recvData, int len, int) const
Scatter array from a root to all other task.
Definition communication.hh:376
Dune::Communication::iallgather
PseudoFuture< TOUT > iallgather(TIN &&, TOUT &&data_out)
Gathers data from all tasks and distribute it to all nonblocking.
Definition communication.hh:445
Dune::Communication::send
int send(const T &data, int dest_rank, int tag) const
Sends the data to the dest_rank.
Definition communication.hh:135
Dune::Communication::size
int size() const
Number of processes in set, is greater than 0.
Definition communication.hh:126
Dune::Communication::iscatter
PseudoFuture< TOUT > iscatter(TIN &&data_in, TOUT &&data_out, int)
Scatter array from a root to all other task nonblocking.
Definition communication.hh:387
Dune::Communication::irecv
PseudoFuture< T > irecv(T &&data, int source_rank, int tag) const
Receives the data from the source_rank nonblocking.
Definition communication.hh:169
Dune::Communication::sum
int sum(T *inout, int len) const
Compute the sum over all processes for each component of an array and return the result in every proc...
Definition communication.hh:199
Dune::Communication::allgatherv
int allgatherv(const T *in, int sendDataLen, T *out, int *recvDataLen, int *displ) const
Gathers data of variable length from all tasks and distribute it to all.
Definition communication.hh:466
Dune::Communication::min
T min(const T &in) const
Compute the minimum of the argument over all processes and return the result in every process....
Definition communication.hh:228
Dune::Communication::isend
PseudoFuture< T > isend(T &&data, int dest_rank, int tag) const
Sends the data to the dest_rank nonblocking.
Definition communication.hh:146
Dune::Communication::gatherv
int gatherv(const T *in, int sendDataLen, T *out, int *recvDataLen, int *displ, int root) const
Gather arrays of variable size on root task.
Definition communication.hh:350
Dune::Communication::allgather
int allgather(const T *sbuf, int count, T *rbuf) const
Gathers data from all tasks and distribute it to all.
Definition communication.hh:433
Dune::Communication::gather
int gather(const T *in, T *out, int len, int) const
Gather arrays on root task.
Definition communication.hh:313
Dune::Communication::recv
T recv(T &&data, int source_rank, int tag, void *status=0) const
Receives the data from the source_rank.
Definition communication.hh:157
Dune::Communication::iallreduce
PseudoFuture< T > iallreduce(T &&data)
Compute something over all processes nonblocking and in-place.
Definition communication.hh:506
Dune::Communication::igather
PseudoFuture< TOUT > igather(TIN &&data_in, TOUT &&data_out, int)
Gather arrays on root task nonblocking.
Definition communication.hh:324
Dune::Communication::Communication
Communication(const Communicator &)
Constructor with a given communicator.
Definition communication.hh:110
Dune::Communication::max
int max(T *inout, int len) const
Compute the maximum over all processes for each component of an array and return the result in every ...
Definition communication.hh:259
Dune::Communication::prod
T prod(const T &in) const
Compute the product of the argument over all processes and return the result in every process....
Definition communication.hh:208
Dune::Communication::broadcast
int broadcast(T *inout, int len, int root) const
Distribute an array from the process with rank root to all other processes.
Definition communication.hh:284
Dune::Communication::rrecv
T rrecv(T &&data, int source_rank, int tag, void *status=0) const
Definition communication.hh:177
Dune::Communication::min
int min(T *inout, int len) const
Compute the minimum over all processes for each component of an array and return the result in every ...
Definition communication.hh:239
Dune::Communication::barrier
int barrier() const
Wait until all processes have arrived at this point in the program.
Definition communication.hh:267
Dune::Communication::Communication
Communication()
Construct default object.
Definition communication.hh:103
Dune::PseudoFuture
A wrapper-class for a object which is ready immediately.
Definition future.hh:124