Skip to content

Commit

Permalink
Add demo for nonblocking send and recv
Browse files Browse the repository at this point in the history
  • Loading branch information
trossi committed Jun 24, 2024
1 parent f888e5e commit 884a577
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions mpi/demos/send_and_recv_nonblocking.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#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) {
// Send with rank 0
double data = 42.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);

MPI_Status status;
MPI_Wait(&request, &status);
printf("Rank %d received %f\n", rank, data);

}

MPI_Finalize();
}

0 comments on commit 884a577

Please sign in to comment.