| 
1 | 1 | /*  | 
2 | 2 |  * Test the connectivity between all processes  | 
3 |  | - * WIP  | 
4 | 3 |  */  | 
5 | 4 | 
 
  | 
6 | 5 | import mpi.*;  | 
 | 6 | +import java.nio.IntBuffer;  | 
7 | 7 | 
 
  | 
8 | 8 | class Connectivity {  | 
9 | 9 |     public static void main(String args[]) throws MPIException {  | 
10 | 10 |         MPI.Init(args);  | 
11 | 11 | 
 
  | 
12 | 12 |         /*  | 
13 |  | -         * MPI_COMM_WORLD is the communicator provided when MPI is  | 
 | 13 | +         * MPI.COMM_WORLD is the communicator provided when MPI is  | 
14 | 14 |          * initialized. It contains all the processes that are created  | 
15 | 15 |          * upon program execution.  | 
16 | 16 |          */  | 
17 | 17 |         int myRank = MPI.COMM_WORLD.getRank();  | 
18 | 18 |         int numProcesses = MPI.COMM_WORLD.getSize();  | 
19 | 19 |         Status status;  | 
20 |  | -        int verbose = 0;  | 
 | 20 | +        boolean verbose = false;  | 
21 | 21 |         int peerProcess;  | 
22 | 22 |         String processorName = MPI.getProcessorName();  | 
23 | 23 | 
 
  | 
 | 24 | +        for (String arg : args) {  | 
 | 25 | +            if (arg.equals("-v") || arg.equals("--verbose")) {  | 
 | 26 | +                verbose = true;  | 
 | 27 | +                break;  | 
 | 28 | +            }  | 
 | 29 | +        }  | 
 | 30 | + | 
24 | 31 |         for (int i = 0; i < numProcesses; i++) {  | 
25 |  | -            if (myRank == i) { /* Find current process */  | 
 | 32 | +            /* Find current process */  | 
 | 33 | +            if (myRank == i) {  | 
26 | 34 |                 /* send to and receive from all higher ranked processes */  | 
27 | 35 |                 for (int j = i + 1; j < numProcesses; j++) {  | 
28 |  | -                    if (verbose != 0)  | 
 | 36 | +                    if (verbose)  | 
29 | 37 |                         System.out.printf("Checking connection between rank %d on %s and rank %d\n", i, processorName,  | 
30 | 38 |                                 j);  | 
31 | 39 | 
 
  | 
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);  | 
37 | 48 | 
 
  | 
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();  | 
41 | 60 |                 }  | 
42 | 61 |             } 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);  | 
45 | 65 | 
 
  | 
46 | 66 |                 /* 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();  | 
49 | 69 |             }  | 
50 | 70 |         }  | 
51 | 71 | 
 
  | 
 | 
0 commit comments