Skip to content

Commit d7c506a

Browse files
committed
Implement the code for Connectivity.java
Add comments to explain the functionality Signed-off-by: vdhyasani17 <[email protected]>
1 parent 448388b commit d7c506a

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

examples/Connectivity.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,66 @@
11
/*
2-
* File for testing connectivity for the Java interface
2+
* Test the connectivity between all processes
33
* WIP
44
*/
55

66
import mpi.*;
77

88
class Connectivity {
9-
static public void main(String args[]) {
10-
System.out.println("Java Connectivity Test");
11-
}
9+
public static void main(String args[]) throws MPIException {
10+
MPI.Init(args);
11+
12+
/*
13+
* MPI_COMM_WORLD is the communicator provided when MPI is
14+
* initialized. It contains all the processes that are created
15+
* upon program execution.
16+
*/
17+
int myRank = MPI.COMM_WORLD.getRank();
18+
int numProcesses = MPI.COMM_WORLD.getSize();
19+
Status status;
20+
int verbose = 0;
21+
int peerProcess;
22+
String processorName = MPI.getProcessorName();
23+
24+
for (int i = 0; i < numProcesses; i++) {
25+
if (myRank == i) { /* Find current process */
26+
/* send to and receive from all higher ranked processes */
27+
for (int j = i + 1; j < numProcesses; j++) {
28+
if (verbose != 0)
29+
System.out.printf("Checking connection between rank %d on %s and rank %d\n", i, processorName,
30+
j);
31+
32+
int[] dataBuffer = { myRank }; /*
33+
* Data buffer to be sent to next process (in this case, only send
34+
* rank num)
35+
*/
36+
int[] recBuffer = new int[1]; /* Data buffer that current process receives from previous process */
37+
38+
/* Process must send message first then wait to receive to avoid deadlock */
39+
MPI.COMM_WORLD.send(dataBuffer, 1, MPI.INT, j, myRank);
40+
MPI.COMM_WORLD.recv(recBuffer, 1, MPI.INT, j, j);
41+
}
42+
} else if (myRank > i) {
43+
int[] dataBuffer = { myRank };
44+
int[] recBuffer = new int[1];
1245

46+
/* receive from and reply to rank i */
47+
MPI.COMM_WORLD.recv(recBuffer, 1, MPI.INT, i, i);
48+
MPI.COMM_WORLD.send(dataBuffer, 1, MPI.INT, i, myRank);
49+
}
50+
}
51+
52+
/* Wait for all processes to reach barrier before proceeding */
53+
MPI.COMM_WORLD.barrier();
54+
55+
/*
56+
* Once all ranks have reached the barrier,
57+
* have only one process print out the confirmation message.
58+
* In this case, we are having the "master" process print the message.
59+
*/
60+
if (myRank == 0) {
61+
System.out.printf("Connectivity test on %d processes PASSED.\n", numProcesses);
62+
}
63+
64+
MPI.Finalize();
65+
}
1366
}

0 commit comments

Comments
 (0)