Dune Core Modules (unstable)

communication.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_COMMON_PARALLEL_COMMUNICATION_HH
6 #define DUNE_COMMON_PARALLEL_COMMUNICATION_HH
14 #include <iostream>
15 #include <complex>
16 #include <algorithm>
17 #include <vector>
18 
21 #include <dune/common/parallel/future.hh>
22 
42 namespace Dune
43 {
44 
45  /* define some type that definitely differs from MPI_Comm */
46  struct No_Comm {};
47 
52  inline bool operator==(const No_Comm&, const No_Comm&)
53  {
54  return true;
55  }
56 
61  inline bool operator!=(const No_Comm&, const No_Comm&)
62  {
63  return false;
64  }
65 
98  template<typename Communicator>
100  {
101  public:
104  {}
105 
110  Communication (const Communicator&)
111  {}
112 
114  int rank () const
115  {
116  return 0;
117  }
118 
120  operator No_Comm() const
121  {
122  return {};
123  }
124 
126  int size () const
127  {
128  return 1;
129  }
130 
134  template<class T>
135  int send([[maybe_unused]] const T& data,
136  [[maybe_unused]] int dest_rank,
137  [[maybe_unused]] int tag)
138  {
139  DUNE_THROW(ParallelError, "This method is not supported in sequential programs");
140  }
141 
145  template<class T>
146  PseudoFuture<T> isend([[maybe_unused]] const T&& data,
147  [[maybe_unused]] int dest_rank,
148  [[maybe_unused]] int tag)
149  {
150  DUNE_THROW(ParallelError, "This method is not supported in sequential programs");
151  }
152 
156  template<class T>
157  T recv([[maybe_unused]] T&& data,
158  [[maybe_unused]] int source_rank,
159  [[maybe_unused]] int tag,
160  [[maybe_unused]] void* status = 0)
161  {
162  DUNE_THROW(ParallelError, "This method is not supported in sequential programs");
163  }
164 
168  template<class T>
169  PseudoFuture<T> irecv([[maybe_unused]] T&& data,
170  [[maybe_unused]] int source_rank,
171  [[maybe_unused]] int tag)
172  {
173  DUNE_THROW(ParallelError, "This method is not supported in sequential programs");
174  }
175 
176  template<class T>
177  T rrecv([[maybe_unused]] T&& data,
178  [[maybe_unused]] int source_rank,
179  [[maybe_unused]] int tag,
180  [[maybe_unused]] void* status = 0) const
181  {
182  DUNE_THROW(ParallelError, "This method is not supported in sequential programs");
183  }
187  template<typename T>
188  T sum (const T& in) const
189  {
190  return in;
191  }
192 
198  template<typename T>
199  int sum ([[maybe_unused]] T* inout, [[maybe_unused]] int len) const
200  {
201  return 0;
202  }
203 
207  template<typename T>
208  T prod (const T& in) const
209  {
210  return in;
211  }
212 
218  template<typename T>
219  int prod ([[maybe_unused]] T* inout, [[maybe_unused]] int len) const
220  {
221  return 0;
222  }
223 
227  template<typename T>
228  T min (const T& in) const
229  {
230  return in;
231  }
232 
238  template<typename T>
239  int min ([[maybe_unused]] T* inout, [[maybe_unused]] int len) const
240  {
241  return 0;
242  }
243 
247  template<typename T>
248  T max (const T& in) const
249  {
250  return in;
251  }
252 
258  template<typename T>
259  int max ([[maybe_unused]] T* inout, [[maybe_unused]] int len) const
260  {
261  return 0;
262  }
263 
267  int barrier () const
268  {
269  return 0;
270  }
271 
276  {
277  return {true}; // return a valid future
278  }
279 
283  template<typename T>
284  int broadcast ([[maybe_unused]] T* inout,
285  [[maybe_unused]] int len,
286  [[maybe_unused]] int root) const
287  {
288  return 0;
289  }
290 
294  template<class T>
295  PseudoFuture<T> ibroadcast(T&& data, int root) const{
296  return {std::forward<T>(data)};
297  }
298 
299 
312  template<typename T>
313  int gather (const T* in, T* out, int len, [[maybe_unused]] int root) const // note out must have same size as in
314  {
315  for (int i=0; i<len; i++)
316  out[i] = in[i];
317  return 0;
318  }
319 
323  template<class TIN, class TOUT = std::vector<TIN>>
324  PseudoFuture<TOUT> igather(TIN&& data_in, TOUT&& data_out, int root){
325  *(data_out.begin()) = std::forward<TIN>(data_in);
326  return {std::forward<TOUT>(data_out)};
327  }
328 
329 
349  template<typename T>
350  int gatherv (const T* in,
351  int sendDataLen,
352  T* out,
353  [[maybe_unused]] int* recvDataLen,
354  int* displ,
355  [[maybe_unused]] int root) const
356  {
357  for (int i=*displ; i<sendDataLen; i++)
358  out[i] = in[i];
359  return 0;
360  }
361 
375  template<typename T>
376  int scatter (const T* sendData, T* recvData, int len, [[maybe_unused]] int root) const // note out must have same size as in
377  {
378  for (int i=0; i<len; i++)
379  recvData[i] = sendData[i];
380  return 0;
381  }
382 
386  template<class TIN, class TOUT = TIN>
387  PseudoFuture<TOUT> iscatter(TIN&& data_in, TOUT&& data_out, int root){
388  data_out = *(std::forward<TIN>(data_in).begin());
389  return {std::forward<TOUT>(data_out)};
390  }
391 
410  template<typename T>
411  int scatterv (const T* sendData,int* sendDataLen, int* displ, T* recvData,
412  [[maybe_unused]] int recvDataLen, [[maybe_unused]] int root) const
413  {
414  for (int i=*displ; i<*sendDataLen; i++)
415  recvData[i] = sendData[i];
416  return 0;
417  }
418 
432  template<typename T>
433  int allgather(const T* sbuf, int count, T* rbuf) const
434  {
435  for(const T* end=sbuf+count; sbuf < end; ++sbuf, ++rbuf)
436  *rbuf=*sbuf;
437  return 0;
438  }
439 
444  template<class TIN, class TOUT = TIN>
445  PseudoFuture<TOUT> iallgather(TIN&& data_in, TOUT&& data_out){
446  return {std::forward<TOUT>(data_out)};
447  }
448 
465  template<typename T>
466  int allgatherv (const T* in, int sendDataLen, T* out, [[maybe_unused]] int* recvDataLen, int* displ) const
467  {
468  for (int i=*displ; i<sendDataLen; i++)
469  out[i] = in[i];
470  return 0;
471  }
472 
485  template<typename BinaryFunction, typename Type>
486  int allreduce([[maybe_unused]] Type* inout, [[maybe_unused]] int len) const
487  {
488  return 0;
489  }
490 
495  template<class BinaryFunction, class TIN, class TOUT = TIN>
496  PseudoFuture<TOUT> iallreduce(TIN&& data_in, TOUT&& data_out){
497  data_out = std::forward<TIN>(data_in);
498  return {std::forward<TOUT>(data_out)};
499  }
500 
505  template<class BinaryFunction, class T>
507  return {std::forward<T>(data)};
508  }
509 
510 
524  template<typename BinaryFunction, typename Type>
525  int allreduce(const Type* in, Type* out, int len) const
526  {
527  std::copy(in, in+len, out);
528  return 0;
529  }
530 
531  };
532 
538  template<class T>
540  [[deprecated("CollectiveCommunication is deprecated. Use Communication instead.")]]
542 }
543 
544 #endif // DUNE_COMMON_PARALLEL_COMMUNICATION_HH
helper classes to provide unique types for standard functions
Collective communication interface and sequential default implementation.
Definition: communication.hh:100
PseudoFuture< void > ibarrier() const
Nonblocking barrier.
Definition: communication.hh:275
PseudoFuture< T > irecv([[maybe_unused]] T &&data, [[maybe_unused]] int source_rank, [[maybe_unused]] int tag)
Receives the data from the source_rank nonblocking.
Definition: communication.hh:169
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
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
int gatherv(const T *in, int sendDataLen, T *out, [[maybe_unused]] int *recvDataLen, int *displ, [[maybe_unused]] int root) const
Gather arrays of variable size on root task.
Definition: communication.hh:350
int broadcast([[maybe_unused]] T *inout, [[maybe_unused]] int len, [[maybe_unused]] int root) const
Distribute an array from the process with rank root to all other processes.
Definition: communication.hh:284
int rank() const
Return rank, is between 0 and size()-1.
Definition: communication.hh:114
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
int prod([[maybe_unused]] T *inout, [[maybe_unused]] 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
int size() const
Number of processes in set, is greater than 0.
Definition: communication.hh:126
int scatterv(const T *sendData, int *sendDataLen, int *displ, T *recvData, [[maybe_unused]] int recvDataLen, [[maybe_unused]] int root) const
Scatter arrays of variable length from a root to all other tasks.
Definition: communication.hh:411
PseudoFuture< TOUT > iallreduce(TIN &&data_in, TOUT &&data_out)
Compute something over all processes nonblocking.
Definition: communication.hh:496
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
int allgather(const T *sbuf, int count, T *rbuf) const
Gathers data from all tasks and distribute it to all.
Definition: communication.hh:433
PseudoFuture< TOUT > iallgather(TIN &&data_in, TOUT &&data_out)
Gathers data from all tasks and distribute it to all nonblocking.
Definition: communication.hh:445
int scatter(const T *sendData, T *recvData, int len, [[maybe_unused]] int root) const
Scatter array from a root to all other task.
Definition: communication.hh:376
PseudoFuture< T > isend([[maybe_unused]] const T &&data, [[maybe_unused]] int dest_rank, [[maybe_unused]] int tag)
Sends the data to the dest_rank nonblocking.
Definition: communication.hh:146
Communication(const Communicator &)
Constructor with a given communicator.
Definition: communication.hh:110
PseudoFuture< T > iallreduce(T &&data)
Compute something over all processes nonblocking and in-place.
Definition: communication.hh:506
int gather(const T *in, T *out, int len, [[maybe_unused]] int root) const
Gather arrays on root task.
Definition: communication.hh:313
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
PseudoFuture< T > ibroadcast(T &&data, int root) const
Distribute an array from the process with rank root to all other processes nonblocking.
Definition: communication.hh:295
int min([[maybe_unused]] T *inout, [[maybe_unused]] 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
PseudoFuture< TOUT > igather(TIN &&data_in, TOUT &&data_out, int root)
Gather arrays on root task nonblocking.
Definition: communication.hh:324
int send([[maybe_unused]] const T &data, [[maybe_unused]] int dest_rank, [[maybe_unused]] int tag)
Sends the data to the dest_rank.
Definition: communication.hh:135
T recv([[maybe_unused]] T &&data, [[maybe_unused]] int source_rank, [[maybe_unused]] int tag, [[maybe_unused]] void *status=0)
Receives the data from the source_rank.
Definition: communication.hh:157
int barrier() const
Wait until all processes have arrived at this point in the program.
Definition: communication.hh:267
int max([[maybe_unused]] T *inout, [[maybe_unused]] 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
PseudoFuture< TOUT > iscatter(TIN &&data_in, TOUT &&data_out, int root)
Scatter array from a root to all other task nonblocking.
Definition: communication.hh:387
int allgatherv(const T *in, int sendDataLen, T *out, [[maybe_unused]] int *recvDataLen, int *displ) const
Gathers data of variable length from all tasks and distribute it to all.
Definition: communication.hh:466
int sum([[maybe_unused]] T *inout, [[maybe_unused]] 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
Communication()
Construct default object.
Definition: communication.hh:103
int allreduce([[maybe_unused]] Type *inout, [[maybe_unused]] 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
Default exception if an error in the parallel communication of the program occurred.
Definition: exceptions.hh:287
A wrapper-class for a object which is ready immediately.
Definition: future.hh:124
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:259
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:237
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (Apr 23, 22:30, 2024)