| 1 | #include "mpi.h"
|
|---|
| 2 | #include "assert.h"
|
|---|
| 3 |
|
|---|
| 4 | #ifndef TAG
|
|---|
| 5 | #define TAG 0
|
|---|
| 6 | #endif
|
|---|
| 7 |
|
|---|
| 8 | #ifndef WILDCARD
|
|---|
| 9 | #define SRC 0
|
|---|
| 10 | #else
|
|---|
| 11 | #define SRC MPI_ANY_SOURCE
|
|---|
| 12 | #endif
|
|---|
| 13 |
|
|---|
| 14 | int main(int argc, char *argv[]) {
|
|---|
| 15 | int numtasks, rank;
|
|---|
| 16 |
|
|---|
| 17 | MPI_Init(&argc,&argv);
|
|---|
| 18 | MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
|
|---|
| 19 | MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|---|
| 20 |
|
|---|
| 21 | if (numtasks < 2) {
|
|---|
| 22 | MPI_Finalize();
|
|---|
| 23 | return 0;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | MPI_Comm comm = MPI_COMM_WORLD;
|
|---|
| 27 | MPI_Request reqs[4];
|
|---|
| 28 |
|
|---|
| 29 | if (rank == 0) {
|
|---|
| 30 | int dat[4] = {1,2,3,4};
|
|---|
| 31 |
|
|---|
| 32 | MPI_Isend(dat, 1, MPI_INT, 1, TAG, comm, reqs);
|
|---|
| 33 | MPI_Isend(dat + 1, 1, MPI_INT, 1, TAG, comm, reqs + 1);
|
|---|
| 34 | MPI_Isend(dat + 2, 1, MPI_INT, 1, TAG, comm, reqs + 2);
|
|---|
| 35 | MPI_Isend(dat + 3, 1, MPI_INT, 1, TAG, comm, reqs + 3);
|
|---|
| 36 | MPI_Waitall(4, reqs, MPI_STATUS_IGNORE);
|
|---|
| 37 | } else if (rank == 1) {
|
|---|
| 38 | int rbuf[4];
|
|---|
| 39 |
|
|---|
| 40 | MPI_Irecv(rbuf, 1, MPI_INT, SRC, TAG, comm, reqs);
|
|---|
| 41 | MPI_Irecv(rbuf + 1, 1, MPI_INT, SRC, TAG, comm, reqs + 1);
|
|---|
| 42 | MPI_Irecv(rbuf + 2, 1, MPI_INT, SRC, TAG, comm, reqs + 2);
|
|---|
| 43 | MPI_Irecv(rbuf + 3, 1, MPI_INT, SRC, TAG, comm, reqs + 3);
|
|---|
| 44 | MPI_Waitall(4, reqs, MPI_STATUS_IGNORE);
|
|---|
| 45 | assert (rbuf[0] == 1 && rbuf[1] == 2 && rbuf[2] == 3 && rbuf[3] == 4);
|
|---|
| 46 | }
|
|---|
| 47 | MPI_Finalize();
|
|---|
| 48 | }
|
|---|