-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpi-2send-soln.c
46 lines (34 loc) · 1.04 KB
/
mpi-2send-soln.c
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
38
39
40
41
42
43
44
45
#include <mpi.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv)
{
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0)
{
char sbuf[16384] = "I have a present for you.";
char rbuf[16384];
MPI_Status stat;
MPI_Send(sbuf, sizeof(sbuf), MPI_CHAR, 1-rank, 15, MPI_COMM_WORLD);
MPI_Recv(rbuf, sizeof(rbuf), MPI_CHAR,
MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
printf("I'm rank %d and rank %d (tag %d) said: '%s'.\n",
rank, stat.MPI_SOURCE, stat.MPI_TAG, rbuf);
}
else
{
char sbuf[16384] = "I have a present for you.";
char rbuf[16384];
MPI_Status stat;
MPI_Recv(rbuf, sizeof(rbuf), MPI_CHAR,
MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
MPI_Send(sbuf, sizeof(sbuf), MPI_CHAR, 1-rank, 15, MPI_COMM_WORLD);
printf("I'm rank %d and rank %d (tag %d) said: '%s'.\n",
rank, stat.MPI_SOURCE, stat.MPI_TAG, rbuf);
}
MPI_Finalize();
return 0;
}