-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task_6.cpp
30 lines (30 loc) · 1.01 KB
/
Task_6.cpp
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
#include <iostream>
#include <mpi.h>
using namespace std;
int main(int argc, char **argv)
{
int rank, size, ibuf;
MPI_Status status;
float rbuf;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
ibuf = rank;
rbuf = 1.0 * rank;
if (rank == 1) MPI_Send(&ibuf, 1, MPI_INT, 0, 5, MPI_COMM_WORLD);
if (rank == 2) MPI_Send(&rbuf, 1, MPI_FLOAT, 0, 5, MPI_COMM_WORLD);
if (rank == 0) {
MPI_Probe(MPI_ANY_SOURCE, 5, MPI_COMM_WORLD, &status);
if (status.MPI_SOURCE == 1) {
MPI_Recv(&ibuf, 1, MPI_INT, 1, 5, MPI_COMM_WORLD, &status);
MPI_Recv(&rbuf, 1, MPI_FLOAT, 2, 5, MPI_COMM_WORLD, &status);
cout << "Process 0 recv " << ibuf << " from process 1, " << rbuf << "from process 2\n";
}
else if (status.MPI_SOURCE == 2) {
MPI_Recv(&rbuf, 1, MPI_FLOAT, 2, 5, MPI_COMM_WORLD, &status);
MPI_Recv(&ibuf, 1, MPI_INT, 1, 5, MPI_COMM_WORLD, &status);
cout << "Process 0 recv " << rbuf << " from process 2, " << ibuf << "from process 1\n";
}
}
MPI_Finalize();
}