-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpi_gather_in_place.cpp
51 lines (37 loc) · 992 Bytes
/
mpi_gather_in_place.cpp
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
/*
Expected output for 4 ranks
Root process gathered data:
0 1 2 3
*/
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int rank, size;
const int root = 0;
int sendbuf;
int *recvbuf = NULL;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
sendbuf = rank;
if (rank == root) {
recvbuf = (int *)malloc(size * sizeof(int));
recvbuf[rank] = rank;
}
if (rank == root) {
MPI_Gather(MPI_IN_PLACE, 1, MPI_INT, recvbuf, 1, MPI_INT, root, MPI_COMM_WORLD);
} else {
MPI_Gather(&sendbuf, 1, MPI_INT, recvbuf, 1, MPI_INT, root, MPI_COMM_WORLD);
}
if (rank == root) {
printf("Root process gathered data:\n");
for (int i = 0; i < size; i++) {
printf("%d ", recvbuf[i]);
}
printf("\n");
free(recvbuf);
}
MPI_Finalize();
return 0;
}