Skip to content

Commit 64badb5

Browse files
committed
Make communication non-blocking by using iSend and iRecv
Fix comment semantics and structure Add -v verbose check Signed-off-by: vdhyasani17 <[email protected]>
1 parent b01b5a3 commit 64badb5

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

examples/Connectivity.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,71 @@
11
/*
22
* Test the connectivity between all processes
3-
* WIP
43
*/
54

65
import mpi.*;
6+
import java.nio.IntBuffer;
77

88
class Connectivity {
99
public static void main(String args[]) throws MPIException {
1010
MPI.Init(args);
1111

1212
/*
13-
* MPI_COMM_WORLD is the communicator provided when MPI is
13+
* MPI.COMM_WORLD is the communicator provided when MPI is
1414
* initialized. It contains all the processes that are created
1515
* upon program execution.
1616
*/
1717
int myRank = MPI.COMM_WORLD.getRank();
1818
int numProcesses = MPI.COMM_WORLD.getSize();
1919
Status status;
20-
int verbose = 0;
20+
boolean verbose = false;
2121
int peerProcess;
2222
String processorName = MPI.getProcessorName();
2323

24+
for (String arg : args) {
25+
if (arg.equals("-v") || arg.equals("--verbose")) {
26+
verbose = true;
27+
break;
28+
}
29+
}
30+
2431
for (int i = 0; i < numProcesses; i++) {
25-
if (myRank == i) { /* Find current process */
32+
/* Find current process */
33+
if (myRank == i) {
2634
/* send to and receive from all higher ranked processes */
2735
for (int j = i + 1; j < numProcesses; j++) {
28-
if (verbose != 0)
36+
if (verbose)
2937
System.out.printf("Checking connection between rank %d on %s and rank %d\n", i, processorName,
3038
j);
3139

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 */
40+
/*
41+
* rank is the Buffer passed into iSend to send to rank j.
42+
* rank is populated with myRank, which is the data to send off
43+
* peer is the Buffer received from rank j from iRecv
44+
*/
45+
IntBuffer rank = MPI.newIntBuffer(1);
46+
IntBuffer peer = MPI.newIntBuffer(1);
47+
rank.put(0, myRank);
3748

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);
49+
/*
50+
* To avoid deadlocks, use non-blocking communication iSend and iRecv
51+
* This will allow the program to progress, in the event that
52+
* two ranks both send to each other at the same time and could
53+
* potentially cause deadlock. The ranks can send their requests
54+
* without halting the program and immediately
55+
*/
56+
Request sendReq = MPI.COMM_WORLD.iSend(rank, 1, MPI.INT, j, myRank);
57+
Request recvReq = MPI.COMM_WORLD.iRecv(peer, 1, MPI.INT, j, j);
58+
sendReq.waitFor();
59+
recvReq.waitFor();
4160
}
4261
} else if (myRank > i) {
43-
int[] dataBuffer = { myRank };
44-
int[] recBuffer = new int[1];
62+
IntBuffer rank = MPI.newIntBuffer(1);
63+
IntBuffer peer = MPI.newIntBuffer(1);
64+
rank.put(0, myRank);
4565

4666
/* 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);
67+
MPI.COMM_WORLD.iRecv(peer, 1, MPI.INT, i, i).waitFor();
68+
MPI.COMM_WORLD.iSend(rank, 1, MPI.INT, i, myRank).waitFor();
4969
}
5070
}
5171

0 commit comments

Comments
 (0)