-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add demo for nonblocking send and recv
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |