This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10 #include <stdio.h>
11 #include "mpi.h"
12
13 int main(int argc, char *argv[])
14 {
15 int rank, size, next, prev, message, tag = 201;
16
17
18
19 MPI_Init(&argc, &argv);
20 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
21 MPI_Comm_size(MPI_COMM_WORLD, &size);
22
23
24
25
26
27 next = (rank + 1) % size;
28 prev = (rank + size - 1) % size;
29
30
31
32
33
34 if (0 == rank) {
35 message = 10;
36
37 printf("Process 0 sending %d to %d, tag %d (%d processes in ring)\n",
38 message, next, tag, size);
39 MPI_Send(&message, 1, MPI_INT, next, tag, MPI_COMM_WORLD);
40 printf("Process 0 sent to %d\n", next);
41 }
42
43
44
45
46
47
48
49
50
51 while (1) {
52 MPI_Recv(&message, 1, MPI_INT, prev, tag, MPI_COMM_WORLD,
53 MPI_STATUS_IGNORE);
54
55 if (0 == rank) {
56 --message;
57 printf("Process 0 decremented value: %d\n", message);
58 }
59
60 MPI_Send(&message, 1, MPI_INT, next, tag, MPI_COMM_WORLD);
61 if (0 == message) {
62 printf("Process %d exiting\n", rank);
63 break;
64 }
65 }
66
67
68
69
70 if (0 == rank) {
71 MPI_Recv(&message, 1, MPI_INT, prev, tag, MPI_COMM_WORLD,
72 MPI_STATUS_IGNORE);
73 }
74
75
76
77 MPI_Finalize();
78 return 0;
79 }