-
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 demos on testing and probing messages
- Loading branch information
Showing
3 changed files
with
92 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,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(); | ||
} |
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,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(); | ||
} |
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