Skip to content

Commit

Permalink
Add demos on testing and probing messages
Browse files Browse the repository at this point in the history
  • Loading branch information
trossi committed Jun 24, 2024
1 parent 160ec31 commit 4ea4665
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
41 changes: 41 additions & 0 deletions mpi/demos/send_and_recv_nonblocking_probing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <unistd.h> // for sleep()
#include <mpi.h>

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();
}
45 changes: 45 additions & 0 deletions mpi/demos/send_and_recv_nonblocking_testing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>
#include <unistd.h> // for sleep()
#include <mpi.h>

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();
}
6 changes: 6 additions & 0 deletions mpi/docs/06-non-blocking-communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

<p>
- 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})
Expand All @@ -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

<p>
- Demo: `send_and_recv_nonblocking_probing.c`


# Message probing

Expand Down

0 comments on commit 4ea4665

Please sign in to comment.