ug4
pcl_process_communicator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__PCL__PCL_PROCESS_COMMUNICATOR__
34 #define __H__PCL__PCL_PROCESS_COMMUNICATOR__
35 
36 #include <iostream>
37 #include <map>
38 #include <vector>
39 #include "pcl_methods.h"
43 #include "common/error.h"
44 
45 namespace pcl
46 {
47 
50 
53 {
54  PCD_EMPTY = 0, //< an empty communicator
55  PCD_WORLD = 1, //< a communicator to all available processes
56  PCD_LOCAL = 2 //< a local communicator, simulating current proc is the only proc
57 };
58 
59 
70 {
71  public:
73 
75 
77  inline bool empty() const {return !is_local() && m_comm->m_mpiComm == MPI_COMM_NULL;}
78 
80  inline bool is_world() const { return !is_local() && m_comm->m_mpiComm == PCL_COMM_WORLD; }
81 
83  inline bool is_local() const {return m_comm.valid() == false;}
84 
86  size_t size() const;
87 
89  int get_proc_id(size_t index) const;
90 
92  bool is_proc_id(size_t index) const
93  {
94  return get_proc_id(index) == ProcRank();
95  }
96 
98 
99  int get_local_proc_id(int globalProcID = pcl::ProcRank()) const;
100 
102  MPI_Comm get_mpi_communicator() const {return m_comm->m_mpiComm;}
103 
105 
107  ProcessCommunicator create_sub_communicator(bool participate) const;
108 
112  ProcessCommunicator create_sub_communicator(std::vector<int> &newProcs) const;
113 
116  static ProcessCommunicator create_communicator(std::vector<int> &newGlobalProcs);
117  static ProcessCommunicator create_communicator(size_t first, size_t num);
122 
130  void gather(const void* sendBuf, int sendCount, DataType sendType,
131  void* recBuf, int recCount, DataType recType, int root) const;
132 
136  void gather(ug::BinaryBuffer &buf, int root=0) const;
137 
139 
146  void scatter(const void* sendBuf, int sendCount, DataType sendType,
147  void* recBuf, int recCount, DataType recType, int root) const;
148 
150 
166  void gatherv(const void* sendBuf, int sendCount, DataType sendType,
167  void* recBuf, int* recCounts, int* displs,
168  DataType recType, int root) const;
169 
171 
185  template<class TValue>
186  void gatherv(std::vector<TValue>& recBufOut,
187  std::vector<TValue>& sendBuf, int root,
188  std::vector<int>* pSizesOut = NULL,
189  std::vector<int>* pOffsetsOut = NULL) const;
190 
192 
199  void allgather(const void* sendBuf, int sendCount, DataType sendType,
200  void* recBuf, int recCount, DataType recType) const;
201 
203 
211  void allgatherv(const void* sendBuf, int sendCount, DataType sendType,
212  void* recBuf, int* recCounts, int* displs,
213  DataType recType) const;
214 
218  void allgather(ug::BinaryBuffer &buf) const;
219 
221 
229  void alltoall(const void* sendBuf, int sendCount, DataType sendType,
230  void* recBuf, int recCount, DataType recType);
231 
233 
246  template<class TValue>
247  void allgatherv(std::vector<TValue>& recBufOut,
248  std::vector<TValue>& sendBuf,
249  std::vector<int>* pSizesOut = NULL,
250  std::vector<int>* pOffsetsOut = NULL) const;
251 
252 
254 
255  void reduce(const void* sendBuf, void* recBuf, int count,
256  DataType type, ReduceOperation op, int rootProc) const;
257 
264  template<typename T>
265  T reduce(const T &t, pcl::ReduceOperation op, int rootProc) const;
266 
273  template<typename T>
274  void reduce(const T *pSendBuff, T *pReceiveBuff, size_t count,
275  pcl::ReduceOperation op, int rootProc) const;
276 
279  template<typename T>
280  void reduce(const std::vector<T> &send, std::vector<T> &receive,
281  pcl::ReduceOperation op, int rootProc) const;
282 
284  size_t reduce(const size_t &t, pcl::ReduceOperation op, int rootProc) const;
285 
287 
288  void allreduce(const void* sendBuf, void* recBuf, int count,
289  DataType type, ReduceOperation op) const;
290 
296  template<typename T>
297  T allreduce(const T &t, pcl::ReduceOperation op) const;
298 
300  size_t allreduce(const size_t &t, pcl::ReduceOperation op) const;
301 
307  template<typename T>
308  void allreduce(const T *pSendBuff, T *pReceiveBuff, size_t count,
309  pcl::ReduceOperation op) const;
310 
313  template<typename T>
314  void allreduce(const std::vector<T> &send, std::vector<T> &receive,
315  pcl::ReduceOperation op) const;
316 
317 
323  void broadcast(void *v, size_t size, DataType type, int root=0) const;
324 
331  template<typename T>
332  inline void broadcast(T *p, size_t size=1, int root=0) const;
333 
338  template<typename T>
339  inline void broadcast(T &t, int root=0) const;
340 
342  template<typename T>
343  inline void broadcast(T &t, int root, DataTypeDirectlySupported d) const;
345  template<typename T>
346  void broadcast(T &t, int root, DataTypeIndirectlySupported d) const;
347 
349  void broadcast(size_t &s, int root=0) const;
350 
354  void broadcast(ug::BinaryBuffer &buf, int root=0) const;
355 
356 
358  void barrier() const;
359 
360 
362 
363  void send_data(void* pBuffer, int bufferSize, int destProc, int tag) const;
364 
366 
378  void send_data(void* pBuffer, int* pBufferSegSizes,
379  int* pRecProcMap, int numRecProcs, int tag) const;
380 
382 
383  void receive_data(void* pBuffOut, int bufferSize, int srcProc, int tag) const;
384 
386 
404  void distribute_data(void* pBufferOut, int* pBufferOutSegSizes,
405  int* pSenderProcMap, int numSenderProcs,
406  void* pBuffer, int* pBufferSegSizes,
407  int* pRecvProcMap, int numRecvProcs,
408  int tag = 1) const;
409 
411 
433  void distribute_data(ug::BinaryBuffer& recvBufOut, int* segSizesOut,
434  int* recvFromRanks, int numRecvFroms,
435  void* sendBuf, int* sendSegSizes,
436  int* sendToRanks, int numSendTos,
437  int tag = 1) const;
438 
440 
454  void distribute_data(ug::BinaryBuffer* recvBufs, int* recvFromRanks, int numRecvs,
455  ug::BinaryBuffer* sendBufs, int* sendToRanks, int numSendTos,
456  int tag = 1) const;
457  private:
459 
461  struct CommWrapper{
463  CommWrapper();
464  CommWrapper(const MPI_Comm& comm,
465  bool bReleaseComm);
466  ~CommWrapper();
467 
468  MPI_Comm m_mpiComm;
470 
472  std::vector<int> m_procs;
473  };
474 
477 
478  private:
481 
482 };
483 
484 std::ostream &operator << (std::ostream &out, const ProcessCommunicator &processCommunicator);
485 
486 // end group pcl
488 
489 }// end of namespace pcl
490 
492 // include implementation
494 
495 #endif
bool valid() const
returns true if the pointer is valid, false if not.
Definition: smart_pointer.h:206
Definition: pcl_datatype.h:63
Definition: pcl_datatype.h:64
Definition: pcl_process_communicator.h:70
SPCommWrapper m_comm
smart-pointer to an instance of a CommWrapper.
Definition: pcl_process_communicator.h:480
void receive_data(void *pBuffOut, int bufferSize, int srcProc, int tag) const
receives data from srcPrc with the specified tag.
Definition: pcl_process_communicator.cpp:511
bool is_proc_id(size_t index) const
returns true if we are the i-th process in the communicator
Definition: pcl_process_communicator.h:92
MPI_Comm get_mpi_communicator() const
returns the mpi-communicator, in case someone needs it
Definition: pcl_process_communicator.h:102
static ProcessCommunicator create_communicator(std::vector< int > &newGlobalProcs)
Definition: pcl_process_communicator.cpp:234
int get_proc_id(size_t index) const
returns the i-th process in the communicator
Definition: pcl_process_communicator.cpp:86
bool empty() const
returns true if the communicator is empty, false if not.
Definition: pcl_process_communicator.h:77
void barrier() const
this method will not return until all processes in the communicator have called it.
Definition: pcl_process_communicator.cpp:678
void send_data(void *pBuffer, int bufferSize, int destProc, int tag) const
sends data with the given tag to the specified process.
Definition: pcl_process_communicator.cpp:471
bool is_local() const
return true if the communicator is local, simulating current proc is the only proc
Definition: pcl_process_communicator.h:83
void alltoall(const void *sendBuf, int sendCount, DataType sendType, void *recBuf, int recCount, DataType recType)
performs MPI_Alltoall on the processes of the communicator.
Definition: pcl_process_communicator.cpp:458
void reduce(const void *sendBuf, void *recBuf, int count, DataType type, ReduceOperation op, int rootProc) const
performs MPI_Reduce on the processes of the communicator.
Definition: pcl_process_communicator.cpp:296
void gather(const void *sendBuf, int sendCount, DataType sendType, void *recBuf, int recCount, DataType recType, int root) const
performs MPI_Gather on the processes of the communicator.
Definition: pcl_process_communicator.cpp:339
bool is_world() const
return true if the communicator is PCD_WORLD
Definition: pcl_process_communicator.h:80
void gatherv(const void *sendBuf, int sendCount, DataType sendType, void *recBuf, int *recCounts, int *displs, DataType recType, int root) const
performs MPI_Gatherv on the processes of the communicator.
Definition: pcl_process_communicator.cpp:406
void allreduce(const void *sendBuf, void *recBuf, int count, DataType type, ReduceOperation op) const
performs MPI_Allreduce on the processes of the communicator.
Definition: pcl_process_communicator.cpp:318
size_t size() const
returns the size of the communicator
Definition: pcl_process_communicator.cpp:71
void scatter(const void *sendBuf, int sendCount, DataType sendType, void *recBuf, int recCount, DataType recType, int root) const
performs MPI_Scatter on the processes of the communicator
Definition: pcl_process_communicator.cpp:392
void broadcast(void *v, size_t size, DataType type, int root=0) const
Definition: pcl_process_communicator.cpp:685
void allgatherv(const void *sendBuf, int sendCount, DataType sendType, void *recBuf, int *recCounts, int *displs, DataType recType) const
performs MPI_Allgatherv on the processes of the communicator.
Definition: pcl_process_communicator.cpp:443
ProcessCommunicator create_sub_communicator(bool participate) const
creates a new communicator containing a subset of the current communicator
Definition: pcl_process_communicator.cpp:122
void distribute_data(void *pBufferOut, int *pBufferOutSegSizes, int *pSenderProcMap, int numSenderProcs, void *pBuffer, int *pBufferSegSizes, int *pRecvProcMap, int numRecvProcs, int tag=1) const
sends and receives data to / from multiple processes.
Definition: pcl_process_communicator.cpp:526
int get_local_proc_id(int globalProcID=pcl::ProcRank()) const
returns the proc-id relative to this communicator
Definition: pcl_process_communicator.cpp:95
SmartPtr< CommWrapper > SPCommWrapper
Smart-pointer that encapsulates a CommWrapper.
Definition: pcl_process_communicator.h:476
void allgather(const void *sendBuf, int sendCount, DataType sendType, void *recBuf, int recCount, DataType recType) const
performs MPI_Allgather on the processes of the communicator.
Definition: pcl_process_communicator.cpp:421
ProcessCommunicator(ProcessCommunicatorDefaults pcd=PCD_WORLD)
creates a communicator.
Definition: pcl_process_communicator.cpp:52
A Buffer for binary data.
Definition: binary_buffer.h:56
MPI_Datatype DataType
Definition: pcl_datatype.h:61
int ProcRank()
returns the rank of the process
Definition: pcl_base.cpp:83
MPI_Op ReduceOperation
Definition: pcl_methods.h:74
std::ostream & operator<<(std::ostream &out, const ProcessCommunicator &processCommunicator)
Definition: pcl_process_communicator.cpp:744
ProcessCommunicatorDefaults
values that can be passed to a ProcessCommunicators constructor.
Definition: pcl_process_communicator.h:53
@ PCD_EMPTY
Definition: pcl_process_communicator.h:54
@ PCD_LOCAL
Definition: pcl_process_communicator.h:56
@ PCD_WORLD
Definition: pcl_process_communicator.h:55
Definition: parallel_grid_layout.h:46
MPI_Comm PCL_COMM_WORLD
Definition: pcl_comm_world.cpp:34
holds an mpi-communicator.
Definition: pcl_process_communicator.h:461
bool m_bReleaseCommunicator
Definition: pcl_process_communicator.h:469
MPI_Comm m_mpiComm
Definition: pcl_process_communicator.h:468
std::vector< int > m_procs
only contains data if m_mpiComm != PCL_COMM_WORLD
Definition: pcl_process_communicator.h:472
CommWrapper()
initializes the commWrapper with PCL_COMM_WORLD
Definition: pcl_process_communicator.cpp:725
~CommWrapper()
Definition: pcl_process_communicator.cpp:737