-
Notifications
You must be signed in to change notification settings - Fork 12
/
vcollectives_x.c
521 lines (479 loc) · 25.3 KB
/
vcollectives_x.c
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include "bigmpi_impl.h"
#include "pthread.h"
/* The displacements vector cannot be represented in the existing set of MPI-3
functions because it is an integer rather than an MPI_Aint. */
typedef enum { GATHERV,
SCATTERV,
ALLGATHERV,
ALLTOALLV,
ALLTOALLW } bigmpi_collective_t;
typedef enum { ALLTOALLW_OFFSET,
#if MPI_VERSION >= 3
NEIGHBORHOOD_ALLTOALLW,
#endif
P2P,
RMA } bigmpi_method_t;
static pthread_once_t BigMPI_vcollectives_method_is_initialized = PTHREAD_ONCE_INIT;
bigmpi_method_t BigMPI_vcollectives_method;
/* Tries to deduce collective operation implementation strategy from
environment */
void BigMPI_Detect_default_vcollectives_method()
{
char *env_var = getenv("BIGMPI_DEFAULT_METHOD");
if (env_var != NULL) {
#if MPI_VERSION >= 3
if (strcmp(env_var, "NEIGHBORHOOD_ALLTOALLW")) {
BigMPI_vcollectives_method = NEIGHBORHOOD_ALLTOALLW;
return;
}
#endif
if (strcmp(env_var, "P2P")) {
BigMPI_vcollectives_method = P2P;
return;
}
if (strcmp(env_var, "RMA")) {
BigMPI_vcollectives_method = RMA;
return;
}
fprintf(stderr, "Unknown value \"%s\" for environment variable BIGMPI_DEFAULT_METHOD\n", env_var);
}
// fallback to default:
BigMPI_vcollectives_method = P2P;
}
bigmpi_method_t BigMPI_Get_default_vcollectives_method()
{
pthread_once(&BigMPI_vcollectives_method_is_initialized, BigMPI_Detect_default_vcollectives_method);
return BigMPI_vcollectives_method;
}
int BigMPI_Collective(bigmpi_collective_t coll, bigmpi_method_t method,
BIGMPI_CONST void *sendbuf,
const MPI_Count sendcount, const MPI_Count sendcounts[],
const MPI_Aint senddispls[],
const MPI_Datatype sendtype, const MPI_Datatype sendtypes[],
void *recvbuf,
const MPI_Count recvcount, const MPI_Count recvcounts[],
const MPI_Aint recvdispls[],
const MPI_Datatype recvtype, const MPI_Datatype recvtypes[],
int root,
MPI_Comm comm)
{
int rc = MPI_SUCCESS;
int is_intercomm;
MPI_Comm_test_inter(comm, &is_intercomm);
if (is_intercomm)
BigMPI_Error("BigMPI does not support intercommunicators yet.\n");
if (sendbuf==MPI_IN_PLACE)
BigMPI_Error("BigMPI does not support in-place in the v-collectives. Sorry. \n");
int size, rank;
MPI_Comm_size(comm, &size);
MPI_Comm_rank(comm, &rank);
if (method==P2P) {
switch(coll) {
case ALLTOALLW: /* See page 173 of MPI-3 */
{
MPI_Request * reqs = malloc(2*size*sizeof(MPI_Request)); assert(reqs!=NULL);
/* No extent calculation because alltoallw does not use that. */
/* Use tag=0 because there is perfect pair-wise matching. */
for (int i=0; i<size; i++) {
/* Pre-post all receives... */
MPIX_Irecv_x(recvbuf+recvdispls[i], recvcounts[i], recvtypes[i],
i /* source */, 0 /* tag */, comm, &reqs[i]);
}
for (int j=rank; j<(size+rank); j++) {
/* Schedule communication in balanced way... */
int i = j%size;
MPIX_Isend_x(sendbuf+senddispls[i], sendcounts[i], sendtypes[i],
i /* target */, 0 /* tag */, comm, &reqs[size+i]);
}
MPI_Waitall(2*size, reqs, MPI_STATUSES_IGNORE);
free(reqs);
}
break;
case ALLTOALLV: /* See page 171 of MPI-3 */
{
MPI_Request * reqs = malloc(2*size*sizeof(MPI_Request)); assert(reqs!=NULL);
MPI_Aint lb /* unused */, sendextent, recvextent;
MPI_Type_get_extent(sendtype, &lb, &sendextent);
MPI_Type_get_extent(recvtype, &lb, &recvextent);
/* Use tag=0 because there is perfect pair-wise matching without it. */
for (int i=0; i<size; i++) {
/* Pre-post all receives... */
MPIX_Irecv_x(recvbuf+recvdispls[i]*recvextent, recvcounts[i], recvtype,
i /* source */, 0 /* tag */, comm, &reqs[i]);
}
for (int j=rank; j<(size+rank); j++) {
/* Schedule communication in balanced way... */
int i = j%size;
MPIX_Isend_x(sendbuf+senddispls[i]*sendextent, sendcounts[i], sendtype,
i /* target */, 0 /* tag */, comm, &reqs[size+i]);
}
MPI_Waitall(2*size, reqs, MPI_STATUSES_IGNORE);
free(reqs);
}
break;
case ALLGATHERV:
{
MPI_Request * reqs = malloc(2*size*sizeof(MPI_Request)); assert(reqs!=NULL);
MPI_Aint lb /* unused */, recvextent;
MPI_Type_get_extent(recvtype, &lb, &recvextent);
/* Use tag=0 because there is perfect pair-wise matching without it. */
for (int i=0; i<size; i++) {
/* Pre-post all receives... */
MPIX_Irecv_x(recvbuf+recvdispls[i]*recvextent, recvcounts[i], recvtype,
i /* source */, 0 /* tag */, comm, &reqs[i]);
}
for (int j=rank; j<(size+rank); j++) {
/* Schedule communication in balanced way... */
int i = j%size;
MPIX_Isend_x(sendbuf, sendcount, sendtype,
i /* target */, 0 /* tag */, comm, &reqs[size+i]);
}
MPI_Waitall(2*size, reqs, MPI_STATUSES_IGNORE);
free(reqs);
}
break;
case GATHERV:
{
int nreqs = (rank==root ? size+1 : 1);
MPI_Request * reqs = malloc(nreqs*sizeof(MPI_Request)); assert(reqs!=NULL);
if (rank==root) {
MPI_Aint lb /* unused */, recvextent;
MPI_Type_get_extent(recvtype, &lb, &recvextent);
for (int i=0; i<size; i++) {
/* Use tag=0 because there is perfect pair-wise matching without it. */
MPIX_Irecv_x(recvbuf+recvdispls[i]*recvextent, recvcounts[i], recvtype,
i /* source */, 0 /* tag */, comm, &reqs[i+1]);
}
}
MPIX_Isend_x(sendbuf, sendcount, sendtype,
root /* target */, 0 /* tag */, comm, &reqs[0]);
MPI_Waitall(nreqs, reqs, MPI_STATUSES_IGNORE);
free(reqs);
}
break;
case SCATTERV:
{
int nreqs = (rank==root ? size+1 : 1);
MPI_Request * reqs = malloc(nreqs*sizeof(MPI_Request)); assert(reqs!=NULL);
if (rank==root) {
MPI_Aint lb /* unused */, sendextent;
MPI_Type_get_extent(sendtype, &lb, &sendextent);
for (int i=0; i<size; i++) {
/* Use tag=0 because there is perfect pair-wise matching without it. */
MPIX_Isend_x(sendbuf+senddispls[i]*sendextent, sendcounts[i], sendtype,
i /* target */, 0 /* tag */, comm, &reqs[i+1]);
}
}
MPIX_Irecv_x(recvbuf, recvcount, recvtype,
root /* source */, 0 /* tag */, comm, &reqs[0]);
MPI_Waitall(nreqs, reqs, MPI_STATUSES_IGNORE);
free(reqs);
}
break;
default:
BigMPI_Error("Invalid collective chosen. \n");
break;
}
#if 0
} else if (method==ALLTOALLW_OFFSET) {
BigMPI_Error("ALLTOALL_OFFSET implementation of v-collectives is incomplete!\n");
int * newsendcounts = malloc(size*sizeof(int)); assert(newsendcounts!=NULL);
MPI_Datatype * newsendtypes = malloc(size*sizeof(MPI_Datatype)); assert(newsendtypes!=NULL);
int * newsdispls = malloc(size*sizeof(int)); assert(newsdispls!=NULL);
int * newrecvcounts = malloc(size*sizeof(int)); assert(newrecvcounts!=NULL);
MPI_Datatype * newrecvtypes = malloc(size*sizeof(MPI_Datatype)); assert(newrecvtypes!=NULL);
int * newrdispls = malloc(size*sizeof(int)); assert(newrdispls!=NULL);
switch(coll) {
case ALLTOALLW:
assert(root == -1);
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, sendcounts,
0 /* splat type */, 0, sendtypes,
0 /* zero displs */, senddispls,
newsendcounts, newsendtypes, newsdispls);
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, recvcounts,
0 /* splat type */, 0, recvtypes,
0 /* zero displs */, recvdispls,
newrecvcounts, newrecvtypes, newrdispls);
break;
case ALLTOALLV:
assert(root == -1);
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, sendcounts,
1 /* splat type */, sendtype, NULL,
0 /* zero displs */, senddispls,
newsendcounts, newsendtypes, newsdispls);
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, recvcounts,
1 /* splat type */, recvtype, NULL,
0 /* zero displs */, recvdispls,
newrecvcounts, newrecvtypes, newrdispls);
break;
case ALLGATHERV:
assert(root == -1);
BigMPI_Convert_vectors(size,
1 /* splat count */, sendcount, NULL,
1 /* splat type */, sendtype, NULL,
1 /* zero displs */, NULL,
newsendcounts, newsendtypes, newsdispls);
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, recvcounts,
1 /* splat type */, recvtype, NULL,
0 /* zero displs */, recvdispls,
newrecvcounts, newrecvtypes, newrdispls);
break;
case GATHERV:
assert(root != -1);
BigMPI_Convert_vectors(size,
1 /* splat count */, sendcount, NULL,
1 /* splat type */, sendtype, NULL,
1 /* zero displs */, NULL,
newsendcounts, newsendtypes, newsdispls);
/* Gatherv: Only the root receives data. */
if (rank==root) {
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, recvcounts,
1 /* splat type */, recvtype, NULL,
0 /* zero displs */, recvdispls,
newrecvcounts, newrecvtypes, newrdispls);
} else {
BigMPI_Convert_vectors(size,
1 /* splat count */, 0, NULL,
1 /* splat type */, MPI_DATATYPE_NULL, NULL,
1 /* zero displs */, NULL,
newrecvcounts, newrecvtypes, newrdispls);
}
break;
case SCATTERV:
assert(root != -1);
/* Scatterv: Only the root sends data. */
if (rank==root) {
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, sendcounts,
1 /* splat type */, sendtype, NULL,
0 /* zero displs */, senddispls,
newsendcounts, newsendtypes, newsdispls);
} else {
BigMPI_Convert_vectors(size,
1 /* splat count */, 0, NULL,
1 /* splat type */, MPI_DATATYPE_NULL, NULL,
1 /* zero displs */, NULL,
newsendcounts, newsendtypes, newsdispls);
}
BigMPI_Convert_vectors(size,
1 /* splat count */, recvcount, NULL,
1 /* splat type */, recvtype, NULL,
1 /* zero displs */, NULL,
newrecvcounts, newrecvtypes, newrdispls);
break;
default:
BigMPI_Error("Invalid collective chosen. \n");
break;
}
rc = MPI_Alltoallw(sendbuf, newsendcounts, newsdispls, newsendtypes,
recvbuf, newrecvcounts, newrdispls, newrecvtypes, comm);
for (int i=0; i<size; i++) {
MPI_Type_free(&newsendtypes[i]);
MPI_Type_free(&newrecvtypes[i]);
}
free(newsendcounts);
free(newsdispls);
free(newsendtypes);
free(newrecvcounts);
free(newrecvtypes);
free(newrdispls);
#endif
#if MPI_VERSION >= 3
} else if (method==NEIGHBORHOOD_ALLTOALLW) {
int * newsendcounts = malloc(size*sizeof(int)); assert(newsendcounts!=NULL);
MPI_Datatype * newsendtypes = malloc(size*sizeof(MPI_Datatype)); assert(newsendtypes!=NULL);
MPI_Aint * newsdispls = malloc(size*sizeof(MPI_Aint)); assert(newsdispls!=NULL);
int * newrecvcounts = malloc(size*sizeof(int)); assert(newrecvcounts!=NULL);
MPI_Datatype * newrecvtypes = malloc(size*sizeof(MPI_Datatype)); assert(newrecvtypes!=NULL);
MPI_Aint * newrdispls = malloc(size*sizeof(MPI_Aint)); assert(newrdispls!=NULL);
switch(coll) {
case ALLTOALLW:
assert(root == -1);
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, sendcounts,
0 /* splat type */, 0, sendtypes,
0 /* zero displs */, senddispls,
newsendcounts, newsendtypes, newsdispls);
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, recvcounts,
0 /* splat type */, 0, recvtypes,
0 /* zero displs */, recvdispls,
newrecvcounts, newrecvtypes, newrdispls);
break;
case ALLTOALLV:
assert(root == -1);
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, sendcounts,
1 /* splat type */, sendtype, NULL,
0 /* zero displs */, senddispls,
newsendcounts, newsendtypes, newsdispls);
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, recvcounts,
1 /* splat type */, recvtype, NULL,
0 /* zero displs */, recvdispls,
newrecvcounts, newrecvtypes, newrdispls);
break;
case ALLGATHERV:
assert(root == -1);
BigMPI_Convert_vectors(size,
1 /* splat count */, sendcount, NULL,
1 /* splat type */, sendtype, NULL,
1 /* zero displs */, NULL,
newsendcounts, newsendtypes, newsdispls);
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, recvcounts,
1 /* splat type */, recvtype, NULL,
0 /* zero displs */, recvdispls,
newrecvcounts, newrecvtypes, newrdispls);
break;
case GATHERV:
assert(root != -1);
BigMPI_Convert_vectors(size,
1 /* splat count */, sendcount, NULL,
1 /* splat type */, sendtype, NULL,
1 /* zero displs */, NULL,
newsendcounts, newsendtypes, newsdispls);
/* Gatherv: Only the root receives data. */
if (rank==root) {
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, recvcounts,
1 /* splat type */, recvtype, NULL,
0 /* zero displs */, recvdispls,
newrecvcounts, newrecvtypes, newrdispls);
} else {
BigMPI_Convert_vectors(size,
1 /* splat count */, 0, NULL,
1 /* splat type */, MPI_DATATYPE_NULL, NULL,
1 /* zero displs */, NULL,
newrecvcounts, newrecvtypes, newrdispls);
}
break;
case SCATTERV:
assert(root != -1);
/* Scatterv: Only the root sends data. */
if (rank==root) {
BigMPI_Convert_vectors(size,
0 /* splat count */, 0, sendcounts,
1 /* splat type */, sendtype, NULL,
0 /* zero displs */, senddispls,
newsendcounts, newsendtypes, newsdispls);
} else {
BigMPI_Convert_vectors(size,
1 /* splat count */, 0, NULL,
1 /* splat type */, MPI_DATATYPE_NULL, NULL,
1 /* zero displs */, NULL,
newsendcounts, newsendtypes, newsdispls);
}
BigMPI_Convert_vectors(size,
1 /* splat count */, recvcount, NULL,
1 /* splat type */, recvtype, NULL,
1 /* zero displs */, NULL,
newrecvcounts, newrecvtypes, newrdispls);
break;
default:
BigMPI_Error("Invalid collective chosen. \n");
break;
}
MPI_Comm comm_dist_graph;
BigMPI_Create_graph_comm(comm, root, &comm_dist_graph);
rc = MPI_Neighbor_alltoallw(sendbuf, newsendcounts, newsdispls, newsendtypes,
recvbuf, newrecvcounts, newrdispls, newrecvtypes, comm_dist_graph);
MPI_Comm_free(&comm_dist_graph);
for (int i=0; i<size; i++) {
MPI_Type_free(&newsendtypes[i]);
MPI_Type_free(&newrecvtypes[i]);
}
free(newsendcounts);
free(newsdispls);
free(newsendtypes);
free(newrecvcounts);
free(newrecvtypes);
free(newrdispls);
#endif
} else if (method==RMA) {
/* TODO Add MPI_Win_create_dynamic version of this?
* This may entail an MPI_Allgather over base addresses,
* but MPI_Win_create has to do that, plus some, so it
* may work out to be faster. */
printf("RMA implementation of v-collectives is incomplete!\n");
MPI_Abort(comm, 1);
/* In the RMA implementation, we will treat send as source (buf) and recv as target (win). */
MPI_Win win;
/* This is the most (?) conservative approach possible, and assumes that datatypes are
* noncontiguous and potentially out-of-order. */
MPI_Aint max_size = 0;
for (int i=0; i<size; i++) {
MPI_Aint lb /* unused */, extent;
MPI_Type_get_extent(recvtypes[i], &lb, &extent);
MPI_Aint offset = recvdispls[i]+recvcounts[i]*extent;
max_size = ((offset > max_size) ? offset : max_size);
}
MPI_Win_create(recvbuf, max_size, 1, MPI_INFO_NULL, comm, &win);
MPI_Win_fence(MPI_MODE_NOPRECEDE | MPI_MODE_NOSTORE, win);
for (int i=0; i<size; i++) {
MPI_Put(sendbuf+senddispls[i], sendcounts[i], sendtypes[i],
i, recvdispls[i], recvcounts[i], recvtypes[i], win);
}
MPI_Win_fence(MPI_MODE_NOSUCCEED | MPI_MODE_NOSTORE, win);
MPI_Win_free(&win);
} else {
/* This should be unreachable... */
BigMPI_Error("Invalid method for v-collectives chosen. \n");
}
return rc;
}
int MPIX_Gatherv_x(BIGMPI_CONST void *sendbuf, MPI_Count sendcount, MPI_Datatype sendtype,
void *recvbuf, const MPI_Count recvcounts[], const MPI_Aint rdispls[], MPI_Datatype recvtype,
int root, MPI_Comm comm)
{
bigmpi_method_t method = BigMPI_Get_default_vcollectives_method();
return BigMPI_Collective(GATHERV, method,
sendbuf, sendcount, NULL, NULL, sendtype, NULL,
recvbuf, -1 /* recvcount */, recvcounts, rdispls, recvtype, NULL,
root, comm);
}
int MPIX_Allgatherv_x(BIGMPI_CONST void *sendbuf, MPI_Count sendcount, MPI_Datatype sendtype,
void *recvbuf, const MPI_Count recvcounts[], const MPI_Aint rdispls[], MPI_Datatype recvtype,
MPI_Comm comm)
{
bigmpi_method_t method = BigMPI_Get_default_vcollectives_method();
return BigMPI_Collective(ALLGATHERV, method,
sendbuf, sendcount, NULL, NULL, sendtype, NULL,
recvbuf, -1 /* recvcount */, recvcounts, rdispls, recvtype, NULL,
-1 /* root */, comm);
}
int MPIX_Scatterv_x(BIGMPI_CONST void *sendbuf, const MPI_Count sendcounts[], const MPI_Aint sdispls[], MPI_Datatype sendtype,
void *recvbuf, MPI_Count recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
{
bigmpi_method_t method = BigMPI_Get_default_vcollectives_method();
return BigMPI_Collective(SCATTERV, method,
sendbuf, -1 /* sendcount */, sendcounts, sdispls, sendtype, NULL,
recvbuf, recvcount, NULL, NULL, recvtype, NULL,
root, comm);
}
int MPIX_Alltoallv_x(BIGMPI_CONST void *sendbuf, const MPI_Count sendcounts[], const MPI_Aint sdispls[], MPI_Datatype sendtype,
void *recvbuf, const MPI_Count recvcounts[], const MPI_Aint rdispls[], MPI_Datatype recvtype,
MPI_Comm comm)
{
bigmpi_method_t method = BigMPI_Get_default_vcollectives_method();
return BigMPI_Collective(ALLTOALLV, method,
sendbuf, -1 /* sendcount */, sendcounts, sdispls, sendtype, NULL,
recvbuf, -1 /* recvcount */, recvcounts, rdispls, recvtype, NULL,
-1 /* root */, comm);
}
int MPIX_Alltoallw_x(BIGMPI_CONST void *sendbuf, const MPI_Count sendcounts[], const MPI_Aint sdispls[], const MPI_Datatype sendtypes[],
void *recvbuf, const MPI_Count recvcounts[], const MPI_Aint rdispls[], const MPI_Datatype recvtypes[],
MPI_Comm comm)
{
bigmpi_method_t method = BigMPI_Get_default_vcollectives_method();
return BigMPI_Collective(ALLTOALLW, method,
sendbuf, -1 /* sendcount */, sendcounts, sdispls, MPI_DATATYPE_NULL, sendtypes,
recvbuf, -1 /* recvcount */, recvcounts, rdispls, MPI_DATATYPE_NULL, recvtypes,
-1 /* root */, comm);
}