1+ /*
2+ * Test the connectivity between all processes
3+ */
4+
5+ import mpi .*;
6+ import java .nio .IntBuffer ;
7+
8+ class Connectivity {
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+ boolean verbose = false ;
21+ int peerProcess ;
22+ String processorName = MPI .getProcessorName ();
23+
24+ for (String arg : args ) {
25+ if (arg .equals ("-v" ) || arg .equals ("--verbose" )) {
26+ verbose = true ;
27+ break ;
28+ }
29+ }
30+
31+ for (int i = 0 ; i < numProcesses ; i ++) {
32+ /* Find current process */
33+ if (myRank == i ) {
34+ /* send to and receive from all higher ranked processes */
35+ for (int j = i + 1 ; j < numProcesses ; j ++) {
36+ if (verbose )
37+ System .out .printf ("Checking connection between rank %d on %s and rank %d\n " , i , processorName ,
38+ j );
39+
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 );
48+
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 ();
60+ }
61+ } else if (myRank > i ) {
62+ IntBuffer rank = MPI .newIntBuffer (1 );
63+ IntBuffer peer = MPI .newIntBuffer (1 );
64+ rank .put (0 , myRank );
65+
66+ /* receive from and reply to rank i */
67+ MPI .COMM_WORLD .iRecv (peer , 1 , MPI .INT , i , i ).waitFor ();
68+ MPI .COMM_WORLD .iSend (rank , 1 , MPI .INT , i , myRank ).waitFor ();
69+ }
70+ }
71+
72+ /* Wait for all processes to reach barrier before proceeding */
73+ MPI .COMM_WORLD .barrier ();
74+
75+ /*
76+ * Once all ranks have reached the barrier,
77+ * have only one process print out the confirmation message.
78+ * In this case, we are having the "master" process print the message.
79+ */
80+ if (myRank == 0 ) {
81+ System .out .printf ("Connectivity test on %d processes PASSED.\n " , numProcesses );
82+ }
83+
84+ MPI .Finalize ();
85+ }
86+ }
0 commit comments