5#ifndef DUNE_COMMON_PARALLEL_MPICOMMUNICATION_HH
6#define DUNE_COMMON_PARALLEL_MPICOMMUNICATION_HH
39 template<
typename Type,
typename BinaryFunction,
typename Enable=
void>
48 op = std::make_unique<MPI_Op>();
53 MPI_Op_create((
void (*)(
void*,
void*,
int*, MPI_Datatype*))&operation,
true,op.get());
58 static void operation (Type *in, Type *inout,
int *len, MPI_Datatype*)
62 for (
int i=0; i< *len; ++i, ++in, ++inout) {
64 temp = func(*in, *inout);
69 Generic_MPI_Op (
const Generic_MPI_Op& ) {}
70 static std::unique_ptr<MPI_Op> op;
74 template<
typename Type,
typename BinaryFunction,
typename Enable>
75 std::unique_ptr<MPI_Op> Generic_MPI_Op<Type,BinaryFunction, Enable>::op;
77#define ComposeMPIOp(func,op) \
78 template<class T, class S> \
79 class Generic_MPI_Op<T, func<S>, std::enable_if_t<MPITraits<S>::is_intrinsic> >{ \
81 static MPI_Op get(){ \
85 Generic_MPI_Op () {} \
86 Generic_MPI_Op (const Generic_MPI_Op & ) {} \
114 if(communicator!=MPI_COMM_NULL) {
116 MPI_Initialized(&initialized);
118 DUNE_THROW(
ParallelError,
"You must call MPIHelper::instance(argc,argv) in your main() function before using the MPI Communication!");
119 MPI_Comm_rank(communicator,&me);
120 MPI_Comm_size(communicator,&procs);
141 int send(
const T& data,
int dest_rank,
int tag)
const
144 return MPI_Send(mpi_data.ptr(), mpi_data.size(), mpi_data.type(),
145 dest_rank, tag, communicator);
154 MPI_Isend(mpidata.ptr(), mpidata.size(), mpidata.type(),
155 dest_rank, tag, communicator, &future.req_);
161 T
recv(T&& data,
int source_rank,
int tag, MPI_Status* status = MPI_STATUS_IGNORE)
const
163 T lvalue_data(std::forward<T>(data));
165 MPI_Recv(mpi_data.ptr(), mpi_data.size(), mpi_data.type(),
166 source_rank, tag, communicator, status);
176 MPI_Irecv(mpidata.ptr(), mpidata.size(), mpidata.type(),
177 source_rank, tag, communicator, &future.req_);
182 T
rrecv(T&& data,
int source_rank,
int tag, MPI_Status* status = MPI_STATUS_IGNORE)
const
185 MPI_Message _message;
186 T lvalue_data(std::forward<T>(data));
188 static_assert(!mpi_data.static_size,
"rrecv work only for non-static-sized types.");
189 if(status == MPI_STATUS_IGNORE)
191 MPI_Mprobe(source_rank, tag, communicator, &_message, status);
193 MPI_Get_count(status, mpi_data.type(), &
size);
194 mpi_data.resize(
size);
195 MPI_Mrecv(mpi_data.ptr(), mpi_data.size(), mpi_data.type(), &_message, status);
204 allreduce<std::plus<T> >(&in,&out,1);
210 int sum (T* inout,
int len)
const
212 return allreduce<std::plus<T> >(inout,len);
220 allreduce<std::multiplies<T> >(&in,&out,1);
226 int prod (T* inout,
int len)
const
228 return allreduce<std::multiplies<T> >(inout,len);
236 allreduce<Min<T> >(&in,&out,1);
242 int min (T* inout,
int len)
const
244 return allreduce<Min<T> >(inout,len);
253 allreduce<Max<T> >(&in,&out,1);
259 int max (T* inout,
int len)
const
261 return allreduce<Max<T> >(inout,len);
267 return MPI_Barrier(communicator);
274 MPI_Ibarrier(communicator, &future.req_);
291 MPI_Ibcast(mpidata.ptr(),
303 int gather (
const T* in, T* out,
int len,
int root)
const
311 template<
class TIN,
class TOUT = std::vector<TIN>>
316 assert(root != me || mpidata_in.size()*procs <= mpidata_out.size());
317 int outlen = (me==root) * mpidata_in.size();
318 MPI_Igather(mpidata_in.ptr(), mpidata_in.size(), mpidata_in.type(),
319 mpidata_out.ptr(), outlen, mpidata_out.type(),
320 root, communicator, &future.req_);
326 int gatherv (
const T* in,
int sendDataLen, T* out,
int* recvDataLen,
int* displ,
int root)
const
336 int scatter (
const T* sendData, T* recvData,
int len,
int root)
const
344 template<
class TIN,
class TOUT = TIN>
350 int inlen = (me==root) * mpidata_in.size()/procs;
351 MPI_Iscatter(mpidata_in.ptr(), inlen, mpidata_in.type(),
352 mpidata_out.ptr(), mpidata_out.size(), mpidata_out.type(),
353 root, communicator, &future.req_);
359 int scatterv (
const T* sendData,
int* sendDataLen,
int* displ, T* recvData,
int recvDataLen,
int root)
const
367 operator MPI_Comm ()
const
373 template<
typename T,
typename T1>
382 template<
class TIN,
class TOUT = TIN>
388 assert(mpidata_in.size()*procs <= mpidata_out.size());
389 int outlen = mpidata_in.size();
390 MPI_Iallgather(mpidata_in.ptr(), mpidata_in.size(), mpidata_in.type(),
391 mpidata_out.ptr(), outlen, mpidata_out.type(),
392 communicator, &future.req_);
398 int allgatherv (
const T* in,
int sendDataLen, T* out,
int* recvDataLen,
int* displ)
const
406 template<
typename BinaryFunction,
typename Type>
409 Type* out =
new Type[len];
410 int ret = allreduce<BinaryFunction>(inout,out,len);
411 std::copy(out, out+len, inout);
416 template<
typename BinaryFunction,
typename Type>
418 Type lvalue_data = std::forward<Type>(in);
420 MPI_Allreduce(MPI_IN_PLACE, data.ptr(), data.size(), data.type(),
427 template<
class BinaryFunction,
class TIN,
class TOUT = TIN>
432 assert(mpidata_out.size() == mpidata_in.size());
433 assert(mpidata_out.type() == mpidata_in.type());
434 MPI_Iallreduce(mpidata_in.ptr(), mpidata_out.ptr(),
435 mpidata_out.size(), mpidata_out.type(),
437 communicator, &future.req_);
442 template<
class BinaryFunction,
class T>
446 MPI_Iallreduce(MPI_IN_PLACE, mpidata.ptr(),
447 mpidata.size(), mpidata.type(),
449 communicator, &future.req_);
454 template<
typename BinaryFunction,
typename Type>
462 MPI_Comm communicator;
A few common exception classes.
helper classes to provide unique types for standard functions
Interface class to translate objects to a MPI_Datatype, void* and size used for MPI calls.
Implements an utility class that provides collective communication methods for sequential programs.
Traits classes for mapping types onto MPI_Datatype.
#define ComposeMPIOp(func, op)
Definition mpicommunication.hh:77
#define DUNE_THROW(E, m)
Definition exceptions.hh:218
Dune namespace.
Definition alignedallocator.hh:13
auto getMPIData(T &t)
Definition mpidata.hh:43
A traits class describing the mapping of types onto MPI_Datatypes.
Definition mpitraits.hh:41
Definition binaryfunctions.hh:18
Definition binaryfunctions.hh:34
Default exception if an error in the parallel communication of the program occurred.
Definition exceptions.hh:287
Collective communication interface and sequential default implementation.
Definition communication.hh:100
int size() const
Number of processes in set, is greater than 0.
Definition communication.hh:126
Definition mpicommunication.hh:41
static MPI_Op get()
Definition mpicommunication.hh:44
int max(T *inout, int len) const
Compute the maximum of the argument over all processes and return the result in every process....
Definition mpicommunication.hh:259
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 mpicommunication.hh:398
T max(const T &in) const
Compute the maximum of the argument over all processes and return the result in every process....
Definition mpicommunication.hh:250
MPIFuture< T > ibroadcast(T &&data, int root) const
Distribute an array from the process with rank root to all other processes nonblocking.
Definition mpicommunication.hh:288
MPIFuture< void > ibarrier() const
Nonblocking barrier.
Definition mpicommunication.hh:271
MPIFuture< const T > isend(const T &&data, int dest_rank, int tag) const
Sends the data to the dest_rank nonblocking.
Definition mpicommunication.hh:150
T recv(T &&data, int source_rank, int tag, MPI_Status *status=MPI_STATUS_IGNORE) const
Receives the data from the source_rank.
Definition mpicommunication.hh:161
int barrier() const
Wait until all processes have arrived at this point in the program.
Definition mpicommunication.hh:265
int rank() const
Return rank, is between 0 and size()-1.
Definition mpicommunication.hh:128
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 mpicommunication.hh:359
MPIFuture< TOUT, TIN > iallgather(TIN &&data_in, TOUT &&data_out) const
Gathers data from all tasks and distribute it to all nonblocking.
Definition mpicommunication.hh:383
Type allreduce(Type &&in) const
Definition mpicommunication.hh:417
int sum(T *inout, int len) const
Compute the sum of the argument over all processes and return the result in every process....
Definition mpicommunication.hh:210
int broadcast(T *inout, int len, int root) const
Distribute an array from the process with rank root to all other processes.
Definition mpicommunication.hh:281
MPIFuture< T > iallreduce(T &&data) const
Compute something over all processes nonblocking.
Definition mpicommunication.hh:443
T sum(const T &in) const
Compute the sum of the argument over all processes and return the result in every process....
Definition mpicommunication.hh:201
int allreduce(const Type *in, Type *out, int len) const
Definition mpicommunication.hh:455
MPIFuture< TOUT, TIN > iallreduce(TIN &&data_in, TOUT &&data_out) const
Compute something over all processes nonblocking.
Definition mpicommunication.hh:428
int size() const
Number of processes in set, is greater than 0.
Definition mpicommunication.hh:134
int gather(const T *in, T *out, int len, int root) const
Gather arrays on root task.
Definition mpicommunication.hh:303
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 mpicommunication.hh:407
T rrecv(T &&data, int source_rank, int tag, MPI_Status *status=MPI_STATUS_IGNORE) const
Definition mpicommunication.hh:182
int scatter(const T *sendData, T *recvData, int len, int root) const
Scatter array from a root to all other task.
Definition mpicommunication.hh:336
MPIFuture< T > irecv(T &&data, int source_rank, int tag) const
Receives the data from the source_rank nonblocking.
Definition mpicommunication.hh:172
int prod(T *inout, int len) const
Compute the product of the argument over all processes and return the result in every process....
Definition mpicommunication.hh:226
MPIFuture< TOUT, TIN > igather(TIN &&data_in, TOUT &&data_out, int root) const
Gather arrays on root task nonblocking.
Definition mpicommunication.hh:312
T min(const T &in) const
Compute the minimum of the argument over all processes and return the result in every process....
Definition mpicommunication.hh:233
Communication(const MPI_Comm &c=MPI_COMM_WORLD)
Instantiation using a MPI communicator.
Definition mpicommunication.hh:111
MPIFuture< TOUT, TIN > iscatter(TIN &&data_in, TOUT &&data_out, int root) const
Scatter array from a root to all other task nonblocking.
Definition mpicommunication.hh:345
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 mpicommunication.hh:326
int min(T *inout, int len) const
Compute the minimum of the argument over all processes and return the result in every process....
Definition mpicommunication.hh:242
int allgather(const T *sbuf, int count, T1 *rbuf) const
Gathers data from all tasks and distribute it to all.
Definition mpicommunication.hh:374
int send(const T &data, int dest_rank, int tag) const
Sends the data to the dest_rank.
Definition mpicommunication.hh:141
T prod(const T &in) const
Compute the product of the argument over all processes and return the result in every process....
Definition mpicommunication.hh:217
Provides a future-like object for MPI communication. It contains the object that will be received and...
Definition mpifuture.hh:85
auto get_send_mpidata()
Definition mpifuture.hh:169
auto get_mpidata()
Definition mpifuture.hh:165