-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsr2.cpp
More file actions
37 lines (28 loc) · 852 Bytes
/
sr2.cpp
File metadata and controls
37 lines (28 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* Send and receive example
* Exchanging send and receive may
* lead to deadlock
*/
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
int rank;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
int message_out = 42;
int message_in;
MPI_Status status;
MPI_Recv(&message_in, 1, MPI_INT, 2, 999, MPI_COMM_WORLD, &status);
MPI_Send(&message_out, 1, MPI_INT, 2, 999, MPI_COMM_WORLD);
printf("Rank %d received %d\n", rank, message_in);
} else if (rank == 2) {
int message_out = 80;
int message_in;
MPI_Status status_2;
MPI_Send(&message_out, 1, MPI_INT, 0, 999, MPI_COMM_WORLD);
MPI_Recv(&message_in, 1, MPI_INT, 0, 999, MPI_COMM_WORLD, &status_2);
printf("Rank %d received %d\n", rank, message_in);
}
MPI_Finalize();
return 0;
}