| 1 | #include "mpi.h"
|
|---|
| 2 | #include "assert.h"
|
|---|
| 3 |
|
|---|
| 4 | int many_to_one () {
|
|---|
| 5 | int size;
|
|---|
| 6 | int rank;
|
|---|
| 7 | MPI_Comm comm = MPI_COMM_WORLD;
|
|---|
| 8 | MPI_Datatype type = MPI_INT;
|
|---|
| 9 |
|
|---|
| 10 | MPI_Comm_size (comm, &size);
|
|---|
| 11 | MPI_Comm_rank (comm, &rank);
|
|---|
| 12 | if (size < 3)
|
|---|
| 13 | return 0;
|
|---|
| 14 |
|
|---|
| 15 | int dat[3] = {rank * size + 1, rank * size + 2, rank * size + 3};
|
|---|
| 16 |
|
|---|
| 17 | MPI_Send(dat, 1, MPI_INT, 0, 1, comm);
|
|---|
| 18 | MPI_Send(dat + 1, 1, MPI_INT, 0, 2, comm);
|
|---|
| 19 | MPI_Send(dat + 2, 1, MPI_INT, 0, 3, comm);
|
|---|
| 20 | if (rank == 0) {
|
|---|
| 21 | int ub = 3 * size;
|
|---|
| 22 | int rbuf[ub];
|
|---|
| 23 | MPI_Request reqs[ub];
|
|---|
| 24 | MPI_Status statuses[ub];
|
|---|
| 25 | int oft = 0;
|
|---|
| 26 |
|
|---|
| 27 | MPI_Irecv(rbuf + oft, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, comm, reqs + oft);
|
|---|
| 28 | oft++;
|
|---|
| 29 | MPI_Irecv(rbuf + oft, 1, MPI_INT, 0, MPI_ANY_TAG, comm, reqs + oft);
|
|---|
| 30 | oft++;
|
|---|
| 31 | MPI_Irecv(rbuf + oft, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, comm, reqs + oft);
|
|---|
| 32 | oft++;
|
|---|
| 33 | MPI_Irecv(rbuf + oft, 1, MPI_INT, 1, MPI_ANY_TAG, comm, reqs + oft);
|
|---|
| 34 | oft++;
|
|---|
| 35 | MPI_Irecv(rbuf + oft, 1, MPI_INT, 2, MPI_ANY_TAG, comm, reqs + oft);
|
|---|
| 36 | oft++;
|
|---|
| 37 | for (int i = 0; i < ub - 5; i++) {
|
|---|
| 38 | MPI_Irecv(rbuf + oft, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, comm, reqs + oft);
|
|---|
| 39 | oft++;
|
|---|
| 40 | }
|
|---|
| 41 | MPI_Waitall(ub, reqs, statuses);
|
|---|
| 42 | for (int i = 0; i < ub; i++) {
|
|---|
| 43 | int src = statuses[i].MPI_SOURCE;
|
|---|
| 44 | int tag = statuses[i].MPI_TAG;
|
|---|
| 45 |
|
|---|
| 46 | assert (rbuf[i] == src * size + tag);
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 | return 0;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | int main() {
|
|---|
| 53 | MPI_Init(NULL, NULL);
|
|---|
| 54 | many_to_one();
|
|---|
| 55 | MPI_Finalize();
|
|---|
| 56 | return 0;
|
|---|
| 57 | }
|
|---|