Skip to content

Commit fcdc735

Browse files
authored
Merge pull request #13201 from vdhyasani17/java-connectivity-example
Implement Connectivity.java example using Java bindings
2 parents 8023312 + d779f9b commit fcdc735

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

config/ompi_setup_java.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ AC_DEFUN([_OMPI_SETUP_JAVA],[
8484
[ompi_java_dir=`/usr/libexec/java_home`],
8585
[ompi_java_dir=/System/Library/Frameworks/JavaVM.framework/Versions/Current])
8686
AC_MSG_CHECKING([for Java in OS X/macOS locations])
87-
AS_IF([test -d $ompi_java_dir],
87+
AS_IF([test -d "$ompi_java_dir"],
8888
[AC_MSG_RESULT([found ($ompi_java_dir)])
8989
ompi_java_found=1
9090
if test -d "$ompi_java_dir/Headers" && test -d "$ompi_java_dir/Commands"; then

examples/Connectivity.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

examples/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ EXAMPLES = \
6060
ring_oshmemfh \
6161
Ring.class \
6262
connectivity_c \
63+
Connectivity.class \
6364
oshmem_shmalloc \
6465
oshmem_circular_shift \
6566
oshmem_max_reduction \

0 commit comments

Comments
 (0)