| [5a071cc] | 1 | #include "mpi.h"
|
|---|
| 2 | #include "assert.h"
|
|---|
| 3 |
|
|---|
| 4 | int many_to_one () {
|
|---|
| 5 | int comm_size;
|
|---|
| 6 | int comm_rank;
|
|---|
| 7 | MPI_Comm comm = MPI_COMM_WORLD;
|
|---|
| 8 | MPI_Datatype type = MPI_INT;
|
|---|
| 9 |
|
|---|
| 10 | MPI_Comm_size (comm, &comm_size);
|
|---|
| 11 | MPI_Comm_rank (comm, &comm_rank);
|
|---|
| 12 |
|
|---|
| 13 | int rbuf[comm_size];
|
|---|
| 14 | MPI_Request reqs[comm_size];
|
|---|
| 15 | MPI_Status statuses[comm_size];
|
|---|
| 16 |
|
|---|
| 17 | MPI_Send(&comm_rank, 1, type, 0, comm_rank, comm);
|
|---|
| 18 | if (comm_rank == 0) {
|
|---|
| 19 | for (int i = 0; i < comm_size; i++) {
|
|---|
| 20 | $choose {
|
|---|
| 21 | $when ($true) {
|
|---|
| 22 | MPI_Irecv(rbuf + i, 1, type, MPI_ANY_SOURCE, MPI_ANY_TAG, comm, reqs + i);
|
|---|
| 23 | assert (reqs[i] != MPI_REQUEST_NULL);
|
|---|
| 24 | }
|
|---|
| 25 | $when ($true) {
|
|---|
| 26 | MPI_Recv(rbuf + i, 1, type, MPI_ANY_SOURCE, MPI_ANY_TAG, comm, statuses + i);
|
|---|
| 27 | reqs[i] = MPI_REQUEST_NULL;
|
|---|
| 28 | }
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | MPI_Waitall(comm_size, reqs, statuses);
|
|---|
| 32 | for (int i = 0; i < comm_size; i++) {
|
|---|
| 33 | int src = statuses[i].MPI_SOURCE;
|
|---|
| 34 |
|
|---|
| 35 | assert (statuses[i].MPI_TAG == src);
|
|---|
| 36 | assert (rbuf[i] == src);
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 | return 0;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | int main() {
|
|---|
| 43 | MPI_Init(NULL, NULL);
|
|---|
| 44 | many_to_one();
|
|---|
| 45 | MPI_Finalize();
|
|---|
| 46 | return 0;
|
|---|
| 47 | }
|
|---|