From 4ea46657cc73ad74c8b9faaf357748929b9f8403 Mon Sep 17 00:00:00 2001 From: Tuomas Rossi Date: Mon, 24 Jun 2024 15:14:50 +0300 Subject: [PATCH] Add demos on testing and probing messages --- mpi/demos/send_and_recv_nonblocking_probing.c | 41 +++++++++++++++++ mpi/demos/send_and_recv_nonblocking_testing.c | 45 +++++++++++++++++++ mpi/docs/06-non-blocking-communication.md | 6 +++ 3 files changed, 92 insertions(+) create mode 100644 mpi/demos/send_and_recv_nonblocking_probing.c create mode 100644 mpi/demos/send_and_recv_nonblocking_testing.c diff --git a/mpi/demos/send_and_recv_nonblocking_probing.c b/mpi/demos/send_and_recv_nonblocking_probing.c new file mode 100644 index 000000000..5869ca498 --- /dev/null +++ b/mpi/demos/send_and_recv_nonblocking_probing.c @@ -0,0 +1,41 @@ +#include +#include // for sleep() +#include + +int main(int argc, char *argv[]) +{ + int size, rank; + MPI_Init(&argc, &argv); + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + printf("Hello from rank %d of %d\n", rank, size); + + if (rank == 0) { + usleep(100); // "computing" + double data = 42.0; + + // Send with rank 0 + MPI_Send(&data, 1, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD); + printf("Rank %d sent %f\n", rank, data); + + } else if (rank == 1) { + int message_waiting; + MPI_Status status; + MPI_Iprobe(0, 0, MPI_COMM_WORLD, &message_waiting, &status); + + while (!message_waiting) { + printf("Rank %d has no incoming messages. Let's do some computing\n", rank); + usleep(10); // "computing" + MPI_Iprobe(0, 0, MPI_COMM_WORLD, &message_waiting, &status); + } + + printf("Rank %d has incoming message. Let's receive\n", rank); + double data; + MPI_Recv(&data, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status); + printf("Rank %d received %f\n", rank, data); + + } + + MPI_Finalize(); +} diff --git a/mpi/demos/send_and_recv_nonblocking_testing.c b/mpi/demos/send_and_recv_nonblocking_testing.c new file mode 100644 index 000000000..6375829d3 --- /dev/null +++ b/mpi/demos/send_and_recv_nonblocking_testing.c @@ -0,0 +1,45 @@ +#include +#include // for sleep() +#include + +int main(int argc, char *argv[]) +{ + int size, rank; + MPI_Init(&argc, &argv); + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + printf("Hello from rank %d of %d\n", rank, size); + + if (rank == 0) { + usleep(100); // "computing" + double data = 42.0; + + // Send with rank 0 + MPI_Send(&data, 1, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD); + printf("Rank %d sent %f\n", rank, data); + + } else if (rank == 1) { + // Receive with rank 1 + double data = 0.0; + MPI_Request request; + MPI_Irecv(&data, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &request); + + printf("Rank %d data before wait %f\n", rank, data); + + int completed; + MPI_Status status; + MPI_Test(&request, &completed, &status); + while (!completed) { + printf("Rank %d request has not completed. Let's do some computing\n", rank); + usleep(10); // "computing" + MPI_Test(&request, &completed, &status); + } + + printf("Rank %d request has completed\n", rank); + printf("Rank %d received %f\n", rank, data); + + } + + MPI_Finalize(); +} diff --git a/mpi/docs/06-non-blocking-communication.md b/mpi/docs/06-non-blocking-communication.md index 982cad022..15350725a 100644 --- a/mpi/docs/06-non-blocking-communication.md +++ b/mpi/docs/06-non-blocking-communication.md @@ -106,6 +106,9 @@ MPI_Test(`request`{.input}, `flag`{.output}, `status`{.output}) - Flag is true if the operation has completed - Status of the completed communication is similar to that of `MPI_Recv` +

+- Demo: `send_and_recv_nonblocking_testing.c` + # Test for many non-blocking operations MPI_Testall(`count`{.input}, `array_of_requests`{.input}, `flag`{.output}, `array_of_statuses`{.output}) @@ -128,6 +131,9 @@ MPI_Iprobe(`source`{.input}, `tag`{.input}, `comm`{.input}, `flag`{.output}, `st - `MPI_ANY_SOURCE` can be used for source - `MPI_ANY_TAG` can be used for tag +

+- Demo: `send_and_recv_nonblocking_probing.c` + # Message probing