-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_util.h
368 lines (274 loc) · 10.2 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
#ifndef __MPI_UTIL
#define __MPI_UTIL
#include <mpi.h>
#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);
template<class A, class B> size_t mpi_size(const std::unordered_map<A,B> &m_obj);
template<class A, class B> unsigned char* mpi_pack(unsigned char* m_ptr, const std::unordered_map<A,B> &m_obj);
template<class A, class B> unsigned char* mpi_unpack(unsigned char* m_ptr, std::unordered_map<A,B> &m_obj);
template<class A, class B> size_t mpi_size(const std::unordered_multimap<A, B> &m_obj);
template<class A, class B> unsigned char* mpi_pack(unsigned char* m_ptr, const std::unordered_multimap<A, B> &m_obj);
template<class A, class B> unsigned char* mpi_unpack(unsigned char* m_ptr, std::unordered_multimap<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);
// Specialization for __uint128_t (which is not recognized by g++ as either a fundamental
// integral or scalar type!). __uint128_t is used to store both accessions and taxonomy ids ...
template<>
size_t mpi_size(const __uint128_t &m_obj);
template<>
unsigned char* mpi_pack(unsigned char* m_ptr, const __uint128_t &m_obj);
template<>
unsigned char* mpi_unpack(unsigned char* m_ptr, __uint128_t &m_obj);
/////////////////////////////////////////////////////////////////////////////////////////
// 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;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Overload for std::unordered_map
/////////////////////////////////////////////////////////////////////////////////////////
template<class A, class B>
size_t mpi_size(const std::unordered_map<A,B> &m_obj)
{
size_t len = sizeof(size_t);
for(typename std::unordered_map<A,B>::const_iterator i = m_obj.begin();i != m_obj.end();++i){
len += mpi_size(i->first);
len += mpi_size(i->second);
}
return len;
}
template<class A, class B>
unsigned char* mpi_pack(unsigned char* m_ptr, const std::unordered_map<A,B> &m_obj)
{
size_t len = m_obj.size();
memcpy( m_ptr, &len, sizeof(size_t) );
m_ptr += sizeof(size_t);
for(typename std::unordered_map<A,B>::const_iterator i = m_obj.begin();i != m_obj.end();++i){
m_ptr = mpi_pack(m_ptr, i->first);
m_ptr = mpi_pack(m_ptr, i->second);
}
return m_ptr;
}
template<class A, class B>
unsigned char* mpi_unpack(unsigned char* m_ptr, std::unordered_map<A,B> &m_obj)
{
size_t len;
memcpy( &len, m_ptr, sizeof(size_t) );
m_ptr += sizeof(size_t);
m_obj.clear();
for(size_t i = 0;i < len;++i){
std::pair<A,B> local;
m_ptr = mpi_unpack(m_ptr, local.first);
m_ptr = mpi_unpack(m_ptr, local.second);
m_obj.insert(local);
}
return m_ptr;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Overload for std::unordered_multimap
/////////////////////////////////////////////////////////////////////////////////////////
template<class A, class B>
size_t mpi_size(const std::unordered_multimap<A, B> &m_obj)
{
size_t len = sizeof(size_t);
for(typename std::unordered_multimap<A, B>::const_iterator i = m_obj.begin();i != m_obj.end();++i){
len += mpi_size(i->first);
len += mpi_size(i->second);
}
return len;
}
template<class A, class B>
unsigned char* mpi_pack(unsigned char* m_ptr, const std::unordered_multimap<A, B> &m_obj)
{
size_t len = m_obj.size();
memcpy( m_ptr, &len, sizeof(size_t) );
m_ptr += sizeof(size_t);
for(typename std::unordered_multimap<A, B>::const_iterator i = m_obj.begin();i != m_obj.end();++i){
m_ptr = mpi_pack(m_ptr, i->first);
m_ptr = mpi_pack(m_ptr, i->second);
}
return m_ptr;
}
template<class A, class B>
unsigned char* mpi_unpack(unsigned char* m_ptr, std::unordered_multimap<A, B> &m_obj)
{
size_t len;
memcpy( &len, m_ptr, sizeof(size_t) );
m_ptr += sizeof(size_t);
m_obj.clear();
for(size_t i = 0;i < len;++i){
std::pair<A, B> local;
m_ptr = mpi_unpack(m_ptr, local.first);
m_ptr = mpi_unpack(m_ptr, local.second);
m_obj.insert(local);
}
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;
MPI_Bcast(&len, sizeof(len), MPI_BYTE, m_src_rank, MPI_COMM_WORLD);
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;
MPI_Bcast(ptr, curr_len, MPI_BYTE, m_src_rank, MPI_COMM_WORLD);
len -= curr_len;
ptr += curr_len;
}
if(m_my_rank != m_src_rank){
mpi_unpack(buffer, m_obj);
}
delete [] buffer;
}
#endif // __MPI_UTIL