-
Notifications
You must be signed in to change notification settings - Fork 10
/
mpi_util.h
404 lines (303 loc) · 10.3 KB
/
mpi_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#ifndef __MPI_UTIL
#define __MPI_UTIL
#ifdef USE_MPI
#include <mpi.h>
#endif // USE_MPI
#include <limits.h>
#include <deque>
#include <vector>
#include <string>
#include <string.h> // memcpy
#include <unordered_map>
// Forward function definitions for containers (needed to be able to transport nested C++ structures):
template<class T> size_t mpi_size(const std::deque<T> &m_obj);
template<class T> unsigned char* mpi_pack(unsigned char* m_ptr, const std::deque<T> &m_obj);
template<class T> unsigned char* mpi_unpack(unsigned char* m_ptr, std::deque<T> &m_obj);
template<class T> size_t mpi_size(const std::vector<T> &m_obj);
template<class T> unsigned char* mpi_pack(unsigned char* m_ptr, const std::vector<T> &m_obj);
template<class T> unsigned char* mpi_unpack(unsigned char* m_ptr, std::vector<T> &m_obj);
template<class A, class B> size_t mpi_size(const std::pair<A, B> &m_obj);
template<class A, class B> unsigned char* mpi_pack(unsigned char* m_ptr, const std::pair<A, B> &m_obj);
template<class A, class B> unsigned char* mpi_unpack(unsigned char* m_ptr, std::pair<A, B> &m_obj);
// Use a template for simple objects. Specialize as needed for more complex types
template <class T>
size_t mpi_size(const T &m_obj)
{
// Force a *compile* time test of whether this is a native or derived type
static_assert(std::is_fundamental<T>::value || std::is_enum<T>::value,
":mpi_size: Non-fundamental or non-enum type passed as template");
return sizeof(m_obj);
}
template <class T>
unsigned char* mpi_pack(unsigned char* m_ptr, const T &m_obj)
{
// Force a *compile* time test of whether this is a native or derived type
static_assert(std::is_fundamental<T>::value || std::is_enum<T>::value,
":mpi_pack: Non-fundamental or non-enum type passed as template");
memcpy( m_ptr, &m_obj, sizeof(m_obj) );
m_ptr += sizeof(m_obj);
return m_ptr;
}
template <class T>
unsigned char* mpi_unpack(unsigned char* m_ptr, T &m_obj)
{
// Force a *compile* time test of whether this is a native or derived type
static_assert(std::is_fundamental<T>::value || std::is_enum<T>::value,
":mpi_unpack: Non-fundamental or non-enum type passed as template");
memcpy( &m_obj, m_ptr, sizeof(m_obj) );
m_ptr += sizeof(m_obj);
return m_ptr;
}
// Specialization for string
template<>
size_t mpi_size(const std::string &m_str);
template<>
unsigned char* mpi_pack(unsigned char* m_ptr, const std::string &m_str);
template<>
unsigned char* mpi_unpack(unsigned char* m_ptr, std::string &m_str);
/////////////////////////////////////////////////////////////////////////////////////////
// Overload for std::deque
/////////////////////////////////////////////////////////////////////////////////////////
template<class T>
size_t mpi_size(const std::deque<T> &m_obj)
{
size_t len = sizeof(size_t);
for(typename std::deque<T>::const_iterator i = m_obj.begin();i != m_obj.end();++i){
len += mpi_size(*i);
}
return len;
}
template<class T>
unsigned char* mpi_pack(unsigned char* m_ptr, const std::deque<T> &m_obj)
{
size_t len = m_obj.size();
memcpy( m_ptr, &len, sizeof(size_t) );
m_ptr += sizeof(size_t);
for(typename std::deque<T>::const_iterator i = m_obj.begin();i != m_obj.end();++i){
m_ptr = mpi_pack(m_ptr, *i);
}
return m_ptr;
}
template<class T>
unsigned char* mpi_unpack(unsigned char* m_ptr, std::deque<T> &m_obj)
{
size_t len;
memcpy( &len, m_ptr, sizeof(size_t) );
m_ptr += sizeof(size_t);
m_obj.resize(len);
for(size_t i = 0;i < len;++i){
m_ptr = mpi_unpack(m_ptr, m_obj[i]);
}
return m_ptr;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Overload for std::vector
/////////////////////////////////////////////////////////////////////////////////////////
template<class T>
size_t mpi_size(const std::vector<T> &m_obj)
{
size_t len = sizeof(size_t);
for(typename std::vector<T>::const_iterator i = m_obj.begin();i != m_obj.end();++i){
len += mpi_size(*i);
}
return len;
}
template<class T>
unsigned char* mpi_pack(unsigned char* m_ptr, const std::vector<T> &m_obj)
{
size_t len = m_obj.size();
memcpy( m_ptr, &len, sizeof(size_t) );
m_ptr += sizeof(size_t);
for(typename std::vector<T>::const_iterator i = m_obj.begin();i != m_obj.end();++i){
m_ptr = mpi_pack(m_ptr, *i);
}
return m_ptr;
}
template<class T>
unsigned char* mpi_unpack(unsigned char* m_ptr, std::vector<T> &m_obj)
{
size_t len;
memcpy( &len, m_ptr, sizeof(size_t) );
m_ptr += sizeof(size_t);
m_obj.resize(len);
for(size_t i = 0;i < len;++i){
m_ptr = mpi_unpack(m_ptr, m_obj[i]);
}
return m_ptr;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Overload for std::pair
/////////////////////////////////////////////////////////////////////////////////////////
template<class A, class B>
size_t mpi_size(const std::pair<A, B> &m_obj)
{
return mpi_size(m_obj.first) + mpi_size(m_obj.second);
}
template<class A, class B>
unsigned char* mpi_pack(unsigned char* m_ptr, const std::pair<A, B> &m_obj)
{
m_ptr = mpi_pack(m_ptr, m_obj.first);
m_ptr = mpi_pack(m_ptr, m_obj.second);
return m_ptr;
}
template<class A, class B>
unsigned char* mpi_unpack(unsigned char* m_ptr, std::pair<A, B> &m_obj)
{
m_ptr = mpi_unpack(m_ptr, m_obj.first);
m_ptr = mpi_unpack(m_ptr, m_obj.second);
return m_ptr;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Generic broadcast (from rank m_src_rank to all other ranks)
//////////////////////////////////////////////////////////////////////////////////////////////
template <class T>
void broadcast(T &m_obj, const int &m_my_rank, const int &m_src_rank)
{
size_t len = (m_my_rank == m_src_rank) ? mpi_size(m_obj) : 0;
#ifdef USE_MPI
MPI_Bcast(&len, sizeof(len), MPI_BYTE, m_src_rank, MPI_COMM_WORLD);
#endif // USE_MPI
unsigned char *buffer = new unsigned char[len];
if(buffer == NULL){
throw __FILE__ ":broadcast: Unable to allocate buffer";
}
if(m_my_rank == m_src_rank){
mpi_pack(buffer, m_obj);
}
// MPI_Bcast can send memory buffers with at most INT_MAX elements.
// If we need to send more than INT_MAX elements, we will need to
// break the buffer into chunks. To reduce the total amount of memory
// used, we can make the max chunk size even smaller than INT_MAX.
unsigned char *ptr = buffer;
const size_t max_buffer_size = 1073741824UL; //Conserve memory with a max buffer size of 1 GB
while(len > 0){
const size_t curr_len = (len > max_buffer_size) ? max_buffer_size : len;
#ifdef USE_MPI
MPI_Bcast(ptr, curr_len, MPI_BYTE, m_src_rank, MPI_COMM_WORLD);
#endif // USE_MPI
len -= curr_len;
ptr += curr_len;
}
if(m_my_rank != m_src_rank){
mpi_unpack(buffer, m_obj);
}
delete [] buffer;
}
// Send from rank 0 to all other ranks
template<typename _tmp>
inline void send(const std::vector<_tmp> &m_array)
{
unsigned int num_elem = m_array.size();
unsigned int buffer_len = num_elem*sizeof(_tmp);
unsigned char *buffer = new unsigned char [buffer_len];
if(buffer == NULL){
throw __FILE__ ":send: Unable to allocate memory for send buffer";
}
unsigned char *ptr = buffer;
typename std::vector<_tmp>::const_iterator iter;
for(iter = m_array.begin();iter != m_array.end();iter++){
memcpy(ptr, &(*iter), sizeof(_tmp) );
ptr += sizeof(_tmp);
}
#ifdef USE_MPI
MPI_Bcast(&num_elem, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
MPI_Bcast(buffer, buffer_len, MPI_BYTE, 0, MPI_COMM_WORLD);
#endif // USE_MPI
delete [] buffer;
}
// Receive from rank 0
template<typename _tmp>
inline void receive(std::vector<_tmp> &m_array)
{
unsigned int num_elem;
#ifdef USE_MPI
MPI_Bcast(&num_elem, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
#endif // USE_MPI
unsigned char *buffer = new unsigned char [num_elem*sizeof(_tmp)];
if(buffer == NULL){
throw __FILE__ ":receive: Unable to allocate memory for buffer";
}
#ifdef USE_MPI
MPI_Bcast(buffer, num_elem*sizeof(_tmp), MPI_BYTE, 0, MPI_COMM_WORLD);
#endif // USE_MPI
m_array.resize(num_elem);
unsigned char *ptr = buffer;
typename std::vector<_tmp>::iterator iter;
for(iter = m_array.begin();iter != m_array.end();iter++){
*iter = *( (_tmp *)ptr );
ptr += sizeof(_tmp);
}
delete [] buffer;
}
inline void send(const std::string &m_str)
{
#ifdef USE_MPI
// Include the '\0' string terminator
unsigned int len = m_str.size() + 1;
MPI_Bcast(&len, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
MPI_Bcast((char*)m_str.c_str(), len, MPI_CHAR, 0, MPI_COMM_WORLD);
#endif // USE_MPI
}
// Receive from rank 0
inline void receive(std::string &m_str)
{
unsigned int len = 0;
#ifdef USE_MPI
MPI_Bcast(&len, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
#endif // USE_MPI
char *buffer = new char [len];
if(buffer == NULL){
throw __FILE__ ":receive: Unable to allocate memory for buffer";
}
#ifdef USE_MPI
MPI_Bcast(buffer,len, MPI_CHAR, 0, MPI_COMM_WORLD);
#endif // USE_MPI
m_str = buffer;
delete [] buffer;
}
template<typename _tmp>
inline void send(const std::deque<_tmp> &m_array)
{
unsigned int num_elem = m_array.size();
unsigned int buffer_len = num_elem*sizeof(_tmp);
unsigned char *buffer = new unsigned char [buffer_len];
if(buffer == NULL){
throw __FILE__ ":send: Unable to allocate memory for send buffer";
}
unsigned char *ptr = buffer;
typename std::deque<_tmp>::const_iterator iter;
for(iter = m_array.begin();iter != m_array.end();iter++){
memcpy(ptr, &(*iter), sizeof(_tmp) );
ptr += sizeof(_tmp);
}
#ifdef USE_MPI
MPI_Bcast(&num_elem, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
MPI_Bcast(buffer, buffer_len, MPI_BYTE, 0, MPI_COMM_WORLD);
#endif // USE_MPI
delete [] buffer;
}
// Receive from rank 0
template<typename _tmp>
inline void receive(std::deque<_tmp> &m_array)
{
unsigned int num_elem = 0;
#ifdef USE_MPI
MPI_Bcast(&num_elem, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
#endif // USE_MPI
unsigned char *buffer = new unsigned char [num_elem*sizeof(_tmp)];
if(buffer == NULL){
throw __FILE__ ":receive: Unable to allocate memory for buffer";
}
#ifdef USE_MPI
MPI_Bcast(buffer, num_elem*sizeof(_tmp), MPI_BYTE, 0, MPI_COMM_WORLD);
#endif // USE_MPI
m_array.resize(num_elem);
unsigned char *ptr = buffer;
typename std::deque<_tmp>::iterator iter;
for(iter = m_array.begin();iter != m_array.end();iter++){
*iter = *( (_tmp *)ptr );
ptr += sizeof(_tmp);
}
delete [] buffer;
}
#endif // __MPI_UTIL