-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_bug1.c
52 lines (46 loc) · 1.49 KB
/
mpi_bug1.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
46
47
48
49
50
51
52
/******************************************************************************
* FILE: mpi_bug1.c
* DESCRIPTION:
* This program has a bug that causes it to hang.
* AUTHOR: Blaise Barney
* LAST REVISED: 04/13/05
******************************************************************************/
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
int numtasks, rank, dest, tag, source, rc, count;
char inmsg, outmsg='x';
MPI_Status Stat;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
printf("Task %d starting...\n",rank);
if (rank == 0) {
if (numtasks > 2)
printf("Numtasks=%d. Only 2 needed. Ignoring extra...\n",numtasks);
dest = rank + 1;
source = dest;
tag = rank;
rc = MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
printf("Sent to task %d...\n",dest);
rc = MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat);
printf("Received from task %d...\n",source);
}
else if (rank == 1) {
dest = rank - 1;
source = dest;
tag = rank;
rc = MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat);
printf("Received from task %d...\n",source);
rc = MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
printf("Sent to task %d...\n",dest);
}
if (rank < 2) {
rc = MPI_Get_count(&Stat, MPI_CHAR, &count);
printf("Task %d: Received %d char(s) from task %d with tag %d \n",
rank, count, Stat.MPI_SOURCE, Stat.MPI_TAG);
}
MPI_Finalize();
}