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+ boolean verbose = false ;
20+ String processorName = MPI .getProcessorName ();
21+
22+ for (String arg : args ) {
23+ if (arg .equals ("-v" ) || arg .equals ("--verbose" )) {
24+ verbose = true ;
25+ break ;
26+ }
27+ }
28+
29+ for (int i = 0 ; i < numProcesses ; i ++) {
30+ /* Find current process */
31+ if (myRank == i ) {
32+ /* send to and receive from all higher ranked processes */
33+ for (int j = i + 1 ; j < numProcesses ; j ++) {
34+ if (verbose )
35+ System .out .printf ("Checking connection between rank %d on %s and rank %d\n " , i , processorName ,
36+ j );
37+
38+ /*
39+ * rank is the Buffer passed into sendRecv to send to rank j.
40+ * rank is populated with myRank, which is the data to send off
41+ * peer is the Buffer received from rank j to current rank
42+ */
43+ IntBuffer rank = MPI .newIntBuffer (1 );
44+ IntBuffer peer = MPI .newIntBuffer (1 );
45+ rank .put (0 , myRank );
46+
47+ /*
48+ * To avoid deadlocks, use combined sendRecv operation.
49+ * This performs a send and recv as a combined atomic operation
50+ * and allow MPI to efficiently handle the requests internally.
51+ */
52+ MPI .COMM_WORLD .sendRecv (rank , 1 , MPI .INT , j , myRank , peer , 1 , MPI .INT , j , j );
53+ }
54+ } else if (myRank > i ) {
55+ IntBuffer rank = MPI .newIntBuffer (1 );
56+ IntBuffer peer = MPI .newIntBuffer (1 );
57+ rank .put (0 , myRank );
58+
59+ /* receive from and reply to rank i */
60+ MPI .COMM_WORLD .sendRecv (rank , 1 , MPI .INT , i , myRank , peer , 1 , MPI .INT , i , i );
61+ }
62+ }
63+
64+ /* Wait for all processes to reach barrier before proceeding */
65+ MPI .COMM_WORLD .barrier ();
66+
67+ /*
68+ * Once all ranks have reached the barrier,
69+ * have only one process print out the confirmation message.
70+ * In this case, we are having the "master" process print the message.
71+ */
72+ if (myRank == 0 ) {
73+ System .out .printf ("Connectivity test on %d processes PASSED.\n " , numProcesses );
74+ }
75+
76+ MPI .Finalize ();
77+ }
78+ }
0 commit comments