-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMPMD.hpp
343 lines (301 loc) · 10.2 KB
/
MPMD.hpp
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
#ifndef MPMD_H
#define MPMD_H
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <map>
#include <mpi.h>
#include <assert.h>
#include <cstring>
struct MPMDIntercomm {
MPI_Comm local;
MPI_Comm work;
int local_size;
int work_size;
bool in_world;
bool in_local;
bool in_work;
std::string name;
bool connected;
inline MPMDIntercomm() {
local = work = MPI_COMM_NULL;
local_size = work_size = 0;
connected = in_world = in_local = in_work = false;
name = "";
}
inline operator bool() const { return connected; }
};
inline void MPI_Exchange(void * out, int out_count, void * in, int in_count, MPI_Datatype datatype, MPI_Comm intercomm, MPI_Comm comm) {
MPI_Request request;
MPI_Status status;
int rank;
MPI_Comm_rank(intercomm, &rank);
fflush(stdout);
if (rank == 0) {
MPI_Isend(out, out_count, datatype, 0, 123, intercomm, &request);
MPI_Recv(in, in_count, datatype, 0, 123, intercomm, &status);
MPI_Wait(&request, &status);
}
MPI_Bcast(in, in_count, datatype, 0, comm);
}
template <typename T> inline MPI_Datatype MPI_Auto_datatype();
template <> inline MPI_Datatype MPI_Auto_datatype< int >() { return MPI_INT; }
template <> inline MPI_Datatype MPI_Auto_datatype< unsigned int >() { return MPI_UNSIGNED; }
template <> inline MPI_Datatype MPI_Auto_datatype< long int >() { return MPI_LONG; }
template <> inline MPI_Datatype MPI_Auto_datatype< unsigned long int >() { return MPI_UNSIGNED_LONG; }
template <> inline MPI_Datatype MPI_Auto_datatype< char >() { return MPI_CHAR; }
template <class T> inline T MPI_Exchange(T& out, MPI_Comm intercomm, MPI_Comm comm) {
T ret;
MPI_Exchange(&out, 1, &ret, 1, MPI_Auto_datatype<T>(), intercomm, comm);
return ret;
};
template <>
inline int MPI_Exchange(int& out, MPI_Comm intercomm, MPI_Comm comm) {
int ret;
MPI_Exchange(&out, 1, &ret, 1, MPI_INT, intercomm, comm);
return ret;
}
template <class T>
inline T MPI_Exchange_container(T& out, MPI_Comm intercomm, MPI_Comm comm) {
size_t my_size = out.size();
size_t other_size = MPI_Exchange(my_size, intercomm, comm);
size_t mx = my_size;
if (other_size > mx) mx = other_size;
T ret;
for (size_t i=0; i<mx; i++) {
typename T::value_type a,b;
if (i < my_size) a = out[i];
b = MPI_Exchange(a, intercomm, comm);
if (i < other_size) ret.push_back(b);
}
return ret;
}
template <class T>
inline std::vector<T> MPI_Exchange(std::vector<T>& out, MPI_Comm intercomm, MPI_Comm comm) {
return MPI_Exchange_container(out, intercomm, comm);
}
template <>
inline std::string MPI_Exchange(std::string& out, MPI_Comm intercomm, MPI_Comm comm) {
return MPI_Exchange_container(out, intercomm, comm);
}
class MPMDHelper {
static char * char_vec_from_string( std::string str, size_t size) {
char * ret = new char[size];
size_t i=0;
for (; i<str.size(); i++) ret[i] = str[i];
for (; i<size; i++) ret[i] = '\0';
return ret;
}
public:
MPI_Comm world;
MPI_Comm local;
MPI_Group local_group;
MPI_Comm work;
MPI_Group work_group;
int world_rank, world_size;
int local_rank, local_size;
int work_rank, work_size;
int universe_size;
bool in_work;
typedef MPMDIntercomm intercomm_t;
typedef std::map< std::string, intercomm_t > intercomm_map;
intercomm_map intercomm;
intercomm_t parent;
int leader;
int local_leader;
std::string name;
std::string host_name;
inline MPMDHelper() {
world = MPI_COMM_NULL;
local = MPI_COMM_NULL;
local_rank = -1;
};
inline ~MPMDHelper() {
};
MPMDIntercomm operator[](const std::string& key) {
intercomm_map::iterator it = intercomm.find(key);
if (it != intercomm.end()) return it->second;
return MPMDIntercomm();
};
inline void Init(const MPI_Comm& world_, const std::string& name_, std::vector<int> excl = std::vector<int>()) {
int mylen,maxlen;
world = world_;
std::vector<std::string> names;
std::vector<int> leaders;
int color = -1;
int colors = 0;
name = name_;
MPI_Comm_rank(world, &world_rank);
MPI_Comm_size(world, &world_size);
{
char host_name_c[MPI_MAX_PROCESSOR_NAME];
int host_len;
MPI_Get_processor_name( host_name_c, &host_len );
host_name = host_name_c;
}
{
int *universe_sizep, flag;
MPI_Comm_get_attr(MPI_COMM_WORLD, MPI_UNIVERSE_SIZE, &universe_sizep, &flag);
if (!flag) {
universe_size = 0; // LCOV_EXCL_LINE
} else universe_size = *universe_sizep;
}
mylen = name.size() + 1;
MPI_Allreduce( &mylen, &maxlen, 1, MPI_INT, MPI_MAX, world);
MPI_Barrier(world);
char* my_name = char_vec_from_string(name, maxlen);
char* other_name = char_vec_from_string("", maxlen);
color = -1;
colors = 0;
int rank, who;
rank = world_rank;
int end_value = world_size;
while (colors <= world_size) {
MPI_Allreduce( &rank, &who, 1, MPI_INT, MPI_MIN, world);
if (who == end_value) break;
if (world_rank == who) {
for (int i=0; i<maxlen; i++) other_name[i] = my_name[i];
}
MPI_Bcast(other_name, maxlen, MPI_CHAR, who, world);
names.push_back(other_name);
leaders.push_back(who);
if (strcmp(my_name, other_name) == 0) {
rank = end_value;
color = colors;
leader = who;
}
colors++;
}
assert(colors <= world_size);
delete[] my_name;
delete[] other_name;
MPI_Comm_split(world, color, rank, &local);
MPI_Comm_rank(local, &local_rank);
MPI_Comm_size(local, &local_size);
MPI_Comm_group(local,&local_group);
if (excl.size() > 0) {
MPI_Group_excl(local_group,excl.size(),&excl[0],&work_group);
MPI_Group_rank(work_group, &work_rank);
MPI_Group_size(work_group, &work_size);
MPI_Comm_create(local,work_group,&work);
in_work = (work_rank != MPI_UNDEFINED);
} else {
work_group=local_group;
work = local;
work_rank = local_rank;
work_size = local_size;
in_work = true;
}
for (int c=0; c<colors; c++) {
rank = local_rank;
MPI_Bcast(&rank, 1, MPI_INT, leaders[c], world);
if (color == c) local_leader = rank;
}
for (int c=0; c<colors; c++) {
MPI_Comm inter = MPI_COMM_NULL;
if (c != color) {
int tag;
if (c < color) {
tag = c*colors + color;
} else {
tag = color*colors + c;
}
MPI_Intercomm_create(local, local_leader, world, leaders[c], tag, &inter);
ConnectIntercomm(inter, true);
}
}
MPI_Comm parent_;
MPI_Comm_get_parent(&parent_);
if (parent_ != MPI_COMM_NULL) {
parent=ConnectIntercomm(parent_, false);
}
}
inline intercomm_t ConnectIntercomm(MPI_Comm inter, bool world_=true, bool work_=false) {
intercomm_t ret;
ret.in_world = world_;
MPI_Comm my_comm = local;
if (work_) my_comm = work;
int my_work = work_ && (local != work);
int other_work;
// MPI_Exchange(&my_work, 1, &other_work, 1, MPI_INT, inter, my_comm);
other_work = MPI_Exchange(my_work, inter, my_comm);
ret.in_local = ! (my_work || other_work);
ret.name = MPI_Exchange(name, inter, my_comm);
if (ret.in_local) {
ret.local = inter;
MPI_Comm_remote_size(ret.local, &ret.local_size);
} else {
ret.local = MPI_COMM_NULL;
ret.local_size = 0;
}
int my_trivial_group = (my_comm == work);
int other_trivial_group;
MPI_Exchange(&my_trivial_group, 1, &other_trivial_group, 1, MPI_INT, inter, my_comm);
if (my_trivial_group && other_trivial_group) {
ret.work = inter;
} else {
MPI_Comm_create(inter, work_group, &ret.work);
}
if (ret.work != MPI_COMM_NULL) {
ret.in_work = true;
MPI_Comm_remote_size(ret.work, &ret.work_size);
} else {
ret.in_work = false;
ret.work_size = 0;
}
ret.connected = true;
intercomm[ret.name] = ret;
return ret;
}
inline intercomm_t Spawn( char *command, char *argv[], int maxprocs, MPI_Info info, bool work_ = false) {
MPI_Comm comm, inter;
std::vector<int> err(maxprocs);
int root;
if (work_) {
comm = work;
root = 0;
} else {
comm = local;
root = 0;
}
MPI_Comm_spawn( command, argv, maxprocs, info, root, comm, &inter, &err[0]);
if (inter != MPI_COMM_NULL) {
return ConnectIntercomm(inter, false, work_);
} else {
intercomm_t ret;
return ret;
}
}
inline void Identify(bool world_ = false) {
// printf("MPMD: %03d.%03d world:%d/%d local:%d/%d color:%d/%d leader:%d/%d --- %s @ %s\n", color, local_rank, world_rank, world_size, local_rank, local_size, color, colors, local_leader, leader, my_name, host_name);
char iden_c[400];
std::string iden = "MPMD: ";
iden += name;
iden += ":";
if (world_) {
sprintf(iden_c, " world:%d/%d", world_rank, world_size); iden += iden_c; // LCOV_EXCL_LINE
}
sprintf(iden_c, " local:%d/%d", local_rank, local_size); iden += iden_c;
if (in_work) {
sprintf(iden_c, " work:%d/%d", work_rank, work_size);
} else {
sprintf(iden_c, " work:NA/%d", work_size);
}
iden += iden_c;
iden += " --- ";
iden += " connected to:";
for (intercomm_map::iterator c = intercomm.begin(); c != intercomm.end(); c++) {
intercomm_t inter = c->second;
iden += " ";
iden += inter.name;
iden += "(";
if (inter.in_work) { sprintf(iden_c, "%d", inter.work_size); iden += iden_c; } else iden += "NA";
iden += "/";
if (inter.in_local) { sprintf(iden_c, "%d", inter.local_size); iden += iden_c; } else iden += "NA";
iden += ")";
}
printf("%s\n",iden.c_str());
}
};
#endif