WARPXM v1.10.0
Loading...
Searching...
No Matches
wxmsgbase.h
Go to the documentation of this file.
1#ifndef wxmsgbase_h
2#define wxmsgbase_h
3
4// WarpX includes
5#include "wxdatatypes.h"
6#include "wxexcept.h"
7#include "wxmsgtmpl.h"
8#include "wxtypelist.h"
9
10// std includes
11#include <vector>
12
22{
23public:
27 virtual ~WxMsgBase()
28 {
29 }
30
36 virtual int rank() const = 0;
37
43 virtual unsigned numProcs() const = 0;
44
51 {
52 return _parent;
53 }
54
62 virtual WxMsgBase* createSubComm(const std::vector<int>& ranks) = 0;
63
67 virtual WxMsgBase* splitComm(int color, int order) = 0;
68
72 virtual void barrier() const = 0;
73
81 template<typename T>
82 void send(const std::vector<T>& array, unsigned recvRank, int tag = -1) const
83 {
84 this->_getMsg<T>()->send(
85 array.size(), &(array[0]), recvRank, (tag == -1) ? _sendTag : tag);
86 }
87
96 template<typename T>
97 void send(unsigned num, const T* array, unsigned recvRank, int tag = -1) const
98 {
99 this->_getMsg<T>()->send(num, array, recvRank, (tag == -1) ? _sendTag : tag);
100 }
101
109 template<typename T>
111 startSend(const std::vector<T>& vec, unsigned recvRank, int tag = -1) const
112 {
113 return this->_getMsg<T>()->startSend(
114 vec.size(), &(vec[0]), recvRank, (tag == -1) ? _sendTag : tag);
115 }
116
126 template<typename T>
128 startSend(unsigned num, const T* arr, unsigned recvRank, int tag = -1) const
129 {
130 return this->_getMsg<T>()->startSend(
131 num, arr, recvRank, (tag == -1) ? _recvTag : tag);
132 }
133
139 virtual void finishSend(WxMsgStatus ms) const = 0;
140
149 template<typename T>
150 void recv(int num, std::vector<T>& array, unsigned sendRank, int tag = -1) const
151 {
152 array.resize(num);
153 this->_getMsg<T>()->recv(
154 num, &(array[0]), sendRank, (tag == -1) ? _recvTag : tag);
155 }
156
165 template<typename T>
166 void recv(unsigned num, T* array, unsigned sendRank, int tag = -1) const
167 {
168 this->_getMsg<T>()->recv(num, array, sendRank, (tag == -1) ? _recvTag : tag);
169 }
170
181 template<typename T>
182 WxMsgStatus startRecv(unsigned num, unsigned sendRank, int tag = -1) const
183 {
184 return this->_getMsg<T>()->startRecv(num, sendRank, (tag == -1) ? _recvTag : tag);
185 }
186
198 template<typename T>
199 WxMsgStatus startRecv(unsigned num, T* arr, unsigned sendRank, int tag = -1) const
200 {
201 return this->_getMsg<T>()->startRecv(
202 num, arr, sendRank, (tag == -1) ? _recvTag : tag);
203 }
204
215 template<typename T>
216 WxMsgStatus startRecv(std::vector<T>& vec, unsigned sendRank, int tag = -1) const
217 {
218 return startRecv(vec.size(), vec.data(), sendRank, tag);
219 }
220
229 virtual void* finishRecv(WxMsgStatus ms) const = 0;
230
239 virtual bool checkRecv(WxMsgStatus ms) const = 0;
240
249 template<typename T>
250 void allReduce(unsigned num, T* sendBuff, T* recvBuff, MPI_Op op) const
251 {
252 this->_getMsg<T>()->allReduce(num, sendBuff, recvBuff, op);
253 }
254
262 template<typename T>
263 std::vector<T> allReduce(std::vector<T> sendBuff, MPI_Op op) const
264 {
265 std::vector<T> recvBuff(sendBuff.size());
266 this->_getMsg<T>()->allReduce(
267 sendBuff.size(), &(sendBuff[0]), &(recvBuff[0]), op);
268 return recvBuff;
269 }
270
271 template<typename T>
272 void broadcast(unsigned num, T* buffer, int root) const
273 {
274 this->_getMsg<T>()->broadcast(num, buffer, root);
275 }
276
277protected:
281 WxMsgBase(int sendTag = 0, int recvTag = 0, WxMsgBase* parent = 0)
282 : _sendTag(sendTag), _recvTag(recvTag), _parent(parent)
283 {
284 }
285
290 template<typename T> void addMsg(WxMsgTmpl<T>* b)
291 {
292 wxTypeMapExtract<T>(_msgTypeMap)._msg = b;
293 }
294
295private:
296 // To prevent use
297 WxMsgBase(const WxMsgBase&);
298 WxMsgBase& operator=(const WxMsgBase&);
299
300 int _sendTag, _recvTag;
301 WxMsgBase* _parent;
302
306 template<typename T> WxMsgTmpl<T>* _getMsg()
307 {
308 WxMsgTmpl<T>* r = wxTypeMapExtract<T>(_msgTypeMap)._msg;
309 if (r)
310 return r;
311 WxExcept wxe;
312 wxe << "Message type not set properly";
313 throw wxe;
314 }
315
319 template<typename T> const WxMsgTmpl<T>* _getMsg() const
320 {
321 WxMsgTmpl<T>* r = wxTypeMapExtract<T>(_msgTypeMap)._msg;
322 if (r)
323 return r;
324 WxExcept wxe;
325 wxe << "Message type not set properly";
326 throw wxe;
327 }
328
329public:
330 // xlC doesn't like internal private structs
331
332 // Container class for all message-ers
333 template<typename T> struct WxMsgContainer
334 {
336 {
337 }
339 {
340 delete _msg;
341 }
342 // this points to a derived class of WxMsgTmpl<T>
344 };
345
346 // Objects of type WxMsgTypeMap_t inherit from all WxMsgTmpl<T>
347 // where T belongs to the WxMsgTypelist_t. Thus it acts like a
348 // container for all message-er objects in the system.
350
351 WxMsgTypeMap_t _msgTypeMap; // container of communicators
352};
353
355#endif // wxmsgbase_h
Provides an abstract interface for message based communication between different processes.
Definition: wxmsgbase.h:22
virtual void barrier() const =0
Block till all processes hit this barrier.
void send(unsigned num, const T *array, unsigned recvRank, int tag=-1) const
Send an array to another rank.
Definition: wxmsgbase.h:97
void recv(int num, std::vector< T > &array, unsigned sendRank, int tag=-1) const
Receive a std::vector from another rank.
Definition: wxmsgbase.h:150
virtual int rank() const =0
Rank of process.
WxMsgStatus startSend(const std::vector< T > &vec, unsigned recvRank, int tag=-1) const
Send an vector to another rank.
Definition: wxmsgbase.h:111
std::vector< T > allReduce(std::vector< T > sendBuff, MPI_Op op) const
Reduce data to all ranks (vector form)
Definition: wxmsgbase.h:263
WxMsgTypeMap_t _msgTypeMap
Definition: wxmsgbase.h:351
void addMsg(WxMsgTmpl< T > *b)
Add a new messager : the derived class should call this to setup WxMsgBase properly.
Definition: wxmsgbase.h:290
WxMsgBase * parent() const
Get parent communicating processor group.
Definition: wxmsgbase.h:50
virtual ~WxMsgBase()
Destructor.
Definition: wxmsgbase.h:27
WxMsgStatus startRecv(unsigned num, T *arr, unsigned sendRank, int tag=-1) const
Receive an array from another rank.
Definition: wxmsgbase.h:199
WxMsgStatus startSend(unsigned num, const T *arr, unsigned recvRank, int tag=-1) const
Send an array to another rank.
Definition: wxmsgbase.h:128
void broadcast(unsigned num, T *buffer, int root) const
Definition: wxmsgbase.h:272
WxTypeMap< WxDataTypes_t, WxMsgContainer > WxMsgTypeMap_t
Definition: wxmsgbase.h:349
virtual WxMsgBase * splitComm(int color, int order)=0
See MPI_Comm_split.
virtual void * finishRecv(WxMsgStatus ms) const =0
Finish the receive started by startRecv and return a pointer to the data recieved.
void recv(unsigned num, T *array, unsigned sendRank, int tag=-1) const
Receive an array from another rank.
Definition: wxmsgbase.h:166
virtual void finishSend(WxMsgStatus ms) const =0
Finish the send started by startSend.
void send(const std::vector< T > &array, unsigned recvRank, int tag=-1) const
Send a std::vector to another rank.
Definition: wxmsgbase.h:82
void allReduce(unsigned num, T *sendBuff, T *recvBuff, MPI_Op op) const
Reduce data to all ranks.
Definition: wxmsgbase.h:250
WxMsgBase(int sendTag=0, int recvTag=0, WxMsgBase *parent=0)
Constructor.
Definition: wxmsgbase.h:281
WxMsgStatus startRecv(std::vector< T > &vec, unsigned sendRank, int tag=-1) const
Receive an array from another rank.
Definition: wxmsgbase.h:216
virtual bool checkRecv(WxMsgStatus ms) const =0
Check status of recieve started by a startRecv.
virtual WxMsgBase * createSubComm(const std::vector< int > &ranks)=0
Split communicator into a child communicator.
virtual unsigned numProcs() const =0
Number of processes taking part in messaging.
WxMsgStatus startRecv(unsigned num, unsigned sendRank, int tag=-1) const
Receive an array from another rank.
Definition: wxmsgbase.h:182
Provides interface for messaging between processes.
Definition: wxmsgtmpl.h:27
WxTypeMap can be used to generate a whole class hierachy at compile time.
Definition: wxtypelist.h:200
wxm::lib::Except is the class to use for creating and throwing exceptions.
Definition: wxexcept.h:31
Definition: wxmsgbase.h:334
WxMsgTmpl< T > * _msg
Definition: wxmsgbase.h:343
virtual ~WxMsgContainer()
Definition: wxmsgbase.h:338
WxMsgContainer()
Definition: wxmsgbase.h:335
Provides a means for derived messengers to return implimentation specific message status flags and da...
Definition: wxmsgtmpl.h:19