3 #ifndef DUNE_COMMON_PARALLEL_MPICOMMUNICATION_HH
4 #define DUNE_COMMON_PARALLEL_MPICOMMUNICATION_HH
37 template<
typename Type,
typename BinaryFunction,
typename Enable=
void>
46 op = std::make_unique<MPI_Op>();
51 MPI_Op_create((
void (*)(
void*,
void*,
int*, MPI_Datatype*))&operation,
true,op.get());
56 static void operation (Type *in, Type *inout,
int *len, MPI_Datatype*)
60 for (
int i=0; i< *len; ++i, ++in, ++inout) {
62 temp = func(*in, *inout);
67 Generic_MPI_Op (
const Generic_MPI_Op& ) {}
68 static std::unique_ptr<MPI_Op> op;
72 template<
typename Type,
typename BinaryFunction,
typename Enable>
73 std::unique_ptr<MPI_Op> Generic_MPI_Op<Type,BinaryFunction, Enable>::op;
75 #define ComposeMPIOp(func,op) \
76 template<class T, class S> \
77 class Generic_MPI_Op<T, func<S>, std::enable_if_t<MPITraits<S>::is_intrinsic> >{ \
79 static MPI_Op get(){ \
83 Generic_MPI_Op () {} \
84 Generic_MPI_Op (const Generic_MPI_Op & ) {} \
112 if(communicator!=MPI_COMM_NULL) {
114 MPI_Initialized(&initialized);
116 DUNE_THROW(
ParallelError,
"You must call MPIHelper::instance(argc,argv) in your main() function before using the MPI Communication!");
117 MPI_Comm_rank(communicator,&me);
118 MPI_Comm_size(communicator,&procs);
139 int send(
const T& data,
int dest_rank,
int tag)
const
142 return MPI_Send(mpi_data.ptr(), mpi_data.size(), mpi_data.type(),
143 dest_rank, tag, communicator);
152 MPI_Isend(mpidata.ptr(), mpidata.size(), mpidata.type(),
153 dest_rank, tag, communicator, &future.req_);
159 T
recv(T&& data,
int source_rank,
int tag, MPI_Status* status = MPI_STATUS_IGNORE)
const
161 T lvalue_data(std::forward<T>(data));
163 MPI_Recv(mpi_data.ptr(), mpi_data.size(), mpi_data.type(),
164 source_rank, tag, communicator, status);
174 MPI_Irecv(mpidata.ptr(), mpidata.size(), mpidata.type(),
175 source_rank, tag, communicator, &future.req_);
180 T
rrecv(T&& data,
int source_rank,
int tag, MPI_Status* status = MPI_STATUS_IGNORE)
const
183 MPI_Message _message;
184 T lvalue_data(std::forward<T>(data));
186 static_assert(!mpi_data.static_size,
"rrecv work only for non-static-sized types.");
187 if(status == MPI_STATUS_IGNORE)
189 MPI_Mprobe(source_rank, tag, communicator, &_message, status);
191 MPI_Get_count(status, mpi_data.type(), &
size);
192 mpi_data.resize(
size);
193 MPI_Mrecv(mpi_data.ptr(), mpi_data.size(), mpi_data.type(), &_message, status);
202 allreduce<std::plus<T> >(&in,&out,1);
208 int sum (T* inout,
int len)
const
210 return allreduce<std::plus<T> >(inout,len);
218 allreduce<std::multiplies<T> >(&in,&out,1);
224 int prod (T* inout,
int len)
const
226 return allreduce<std::multiplies<T> >(inout,len);
234 allreduce<Min<T> >(&in,&out,1);
240 int min (T* inout,
int len)
const
242 return allreduce<Min<T> >(inout,len);
251 allreduce<Max<T> >(&in,&out,1);
257 int max (T* inout,
int len)
const
259 return allreduce<Max<T> >(inout,len);
265 return MPI_Barrier(communicator);
272 MPI_Ibarrier(communicator, &future.req_);
289 MPI_Ibcast(mpidata.ptr(),
301 int gather (
const T* in, T* out,
int len,
int root)
const
309 template<
class TIN,
class TOUT = std::vector<TIN>>
314 assert(root != me || mpidata_in.size()*procs <= mpidata_out.size());
315 int outlen = (me==root) * mpidata_in.size();
316 MPI_Igather(mpidata_in.ptr(), mpidata_in.size(), mpidata_in.type(),
317 mpidata_out.ptr(), outlen, mpidata_out.type(),
318 root, communicator, &future.req_);
324 int gatherv (
const T* in,
int sendDataLen, T* out,
int* recvDataLen,
int* displ,
int root)
const
334 int scatter (
const T* sendData, T* recvData,
int len,
int root)
const
342 template<
class TIN,
class TOUT = TIN>
348 int inlen = (me==root) * mpidata_in.size()/procs;
349 MPI_Iscatter(mpidata_in.ptr(), inlen, mpidata_in.type(),
350 mpidata_out.ptr(), mpidata_out.size(), mpidata_out.type(),
351 root, communicator, &future.req_);
357 int scatterv (
const T* sendData,
int* sendDataLen,
int* displ, T* recvData,
int recvDataLen,
int root)
const
365 operator MPI_Comm ()
const
371 template<
typename T,
typename T1>
380 template<
class TIN,
class TOUT = TIN>
386 assert(mpidata_in.size()*procs <= mpidata_out.size());
387 int outlen = mpidata_in.size();
388 MPI_Iallgather(mpidata_in.ptr(), mpidata_in.size(), mpidata_in.type(),
389 mpidata_out.ptr(), outlen, mpidata_out.type(),
390 communicator, &future.req_);
396 int allgatherv (
const T* in,
int sendDataLen, T* out,
int* recvDataLen,
int* displ)
const
404 template<
typename BinaryFunction,
typename Type>
407 Type* out =
new Type[len];
408 int ret = allreduce<BinaryFunction>(inout,out,len);
409 std::copy(out, out+len, inout);
414 template<
typename BinaryFunction,
typename Type>
416 Type lvalue_data = std::forward<Type>(in);
418 MPI_Allreduce(MPI_IN_PLACE, data.ptr(), data.size(), data.type(),
425 template<
class BinaryFunction,
class TIN,
class TOUT = TIN>
430 assert(mpidata_out.size() == mpidata_in.size());
431 assert(mpidata_out.type() == mpidata_in.type());
432 MPI_Iallreduce(mpidata_in.ptr(), mpidata_out.ptr(),
433 mpidata_out.size(), mpidata_out.type(),
435 communicator, &future.req_);
440 template<
class BinaryFunction,
class T>
444 MPI_Iallreduce(MPI_IN_PLACE, mpidata.ptr(),
445 mpidata.size(), mpidata.type(),
447 communicator, &future.req_);
452 template<
typename BinaryFunction,
typename Type>
460 MPI_Comm communicator;
helper classes to provide unique types for standard functions
A few common exception classes.
Implements an utility class that provides collective communication methods for sequential programs.
Interface class to translate objects to a MPI_Datatype, void* and size used for MPI calls.
Traits classes for mapping types onto MPI_Datatype.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:11
ComposeMPIOp(std::plus, MPI_SUM)
auto getMPIData(T &t)
Definition: mpidata.hh:41
A traits class describing the mapping of types onto MPI_Datatypes.
Definition: mpitraits.hh:39
Definition: binaryfunctions.hh:16
Definition: binaryfunctions.hh:32
Default exception if an error in the parallel communication of the program occurred.
Definition: exceptions.hh:285
Collective communication interface and sequential default implementation.
Definition: communication.hh:98
int size() const
Number of processes in set, is greater than 0.
Definition: communication.hh:124
Definition: mpicommunication.hh:39
static MPI_Op get()
Definition: mpicommunication.hh:42
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:257
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:396
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:248
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:159
MPIFuture< T > iallreduce(T &&data) const
Compute something over all processes nonblocking.
Definition: mpicommunication.hh:441
int barrier() const
Wait until all processes have arrived at this point in the program.
Definition: mpicommunication.hh:263
int rank() const
Return rank, is between 0 and size()-1.
Definition: mpicommunication.hh:126
MPIFuture< TOUT, TIN > iallreduce(TIN &&data_in, TOUT &&data_out) const
Compute something over all processes nonblocking.
Definition: mpicommunication.hh:426
MPIFuture< T > irecv(T &&data, int source_rank, int tag) const
Receives the data from the source_rank nonblocking.
Definition: mpicommunication.hh:170
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:357
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:381
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:286
Type allreduce(Type &&in) const
Definition: mpicommunication.hh:415
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:208
MPIFuture< const T > isend(const T &&data, int dest_rank, int tag) const
Sends the data to the dest_rank nonblocking.
Definition: mpicommunication.hh:148
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:279
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:199
int allreduce(const Type *in, Type *out, int len) const
Definition: mpicommunication.hh:453
int size() const
Number of processes in set, is greater than 0.
Definition: mpicommunication.hh:132
int gather(const T *in, T *out, int len, int root) const
Gather arrays on root task.
Definition: mpicommunication.hh:301
int allreduce(Type *inout, int len) const
Definition: mpicommunication.hh:405
T rrecv(T &&data, int source_rank, int tag, MPI_Status *status=MPI_STATUS_IGNORE) const
Definition: mpicommunication.hh:180
MPIFuture< TOUT, TIN > igather(TIN &&data_in, TOUT &&data_out, int root) const
Gather arrays on root task nonblocking.
Definition: mpicommunication.hh:310
int scatter(const T *sendData, T *recvData, int len, int root) const
Scatter array from a root to all other task.
Definition: mpicommunication.hh:334
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:343
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:224
MPIFuture< void > ibarrier() const
Nonblocking barrier.
Definition: mpicommunication.hh:269
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:231
Communication(const MPI_Comm &c=MPI_COMM_WORLD)
Instantiation using a MPI communicator.
Definition: mpicommunication.hh:109
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:324
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:240
int allgather(const T *sbuf, int count, T1 *rbuf) const
Gathers data from all tasks and distribute it to all.
Definition: mpicommunication.hh:372
int send(const T &data, int dest_rank, int tag) const
Sends the data to the dest_rank.
Definition: mpicommunication.hh:139
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:215
Provides a future-like object for MPI communication. It contains the object that will be received and...
Definition: mpifuture.hh:83
auto get_send_mpidata()
Definition: mpifuture.hh:167
auto get_mpidata()
Definition: mpifuture.hh:163