This source file includes following definitions.
- main
1
2
3
4
5
6
7 import mpi.* ;
8
9 class Ring {
10 static public void main(String[] args) throws MPIException {
11
12
13 MPI.Init(args) ;
14
15 int source;
16 int dest;
17 int tag=50;
18 int next;
19 int prev;
20 int message[] = new int [1];
21
22 int myrank = MPI.COMM_WORLD.getRank() ;
23 int size = MPI.COMM_WORLD.getSize() ;
24
25
26
27
28
29 next = (myrank + 1) % size;
30 prev = (myrank + size - 1) % size;
31
32
33
34
35
36 if (0 == myrank) {
37 message[0] = 10;
38
39 System.out.println("Process 0 sending " + message[0] + " to rank " + next + " (" + size + " processes in ring)");
40 MPI.COMM_WORLD.send(message, 1, MPI.INT, next, tag);
41 }
42
43
44
45
46
47
48
49
50
51 while (true) {
52 MPI.COMM_WORLD.recv(message, 1, MPI.INT, prev, tag);
53
54 if (0 == myrank) {
55 --message[0];
56 System.out.println("Process 0 decremented value: " + message[0]);
57 }
58
59 MPI.COMM_WORLD.send(message, 1, MPI.INT, next, tag);
60 if (0 == message[0]) {
61 System.out.println("Process " + myrank + " exiting");
62 break;
63 }
64 }
65
66
67
68
69 if (0 == myrank) {
70 MPI.COMM_WORLD.recv(message, 1, MPI.INT, prev, tag);
71 }
72
73 MPI.Finalize();
74 }
75 }