All methods (except for constructors and destructors) of MPI classes are public native. Which means in Java program the methods identifier and arguments are defined without further implementation.
Example 1. Example showing a simple Java MPI wrapper usage.
Example1.java :
import MPI.*;
public class Example1 {
static public void main(String[] args) {
MPI JMPI = new MPI(args);
int[] myid = new int[1];
int[] numprocs = new int[1];
JMPI.COMM_WORLD.Size(numprocs);
JMPI.COMM_WORLD.Rank(myid);
System.out.println("Process "+myid[0]+"/"+numprocs[0]+
" on "+JMPI.Get_processor_name());
JMPI.finalize();
}
}
MPI JMPI = new MPI(args);This statement will create a MPI class instance called
JMPI. The
MPI
classes constructor will transform the String[] arguments into C style
string array reference, call MPI_Init, create communicator
COMM_WORLD,
create default MPI reduce operators MIN, MAX, SUM, etc.
int[] myid = new int[1]; int[] numprocs = new int[1];Here, two variable
myid and numprocs are declared.
In Java, the simple
type like int, byte, double, ...will only
pass by value. So we can never
reflect the value change back to the caller. To work around the fact
that Java don*t have pass by reference and pointer, we can use simple
type array instead.
JMPI.COMM_WORLD.Size(numprocs); JMPI.COMM_WORLD.Rank(myid);Now, two native methods belong to
COMM_WORLD were called. The
COMM_WORLD
is a Comm class instance which initiated in MPI()
constructor. The COMM_WORLD communicator provide all the MPI
communication binding that use the MPI_COMM_WORLD communicator.
As we had mention before, the Size() and Rank() methods
will take a int[] argument, so the result can be received.
System.out.println("Process "+myid[0]+"/"+numprocs[0]+
" on "+JMPI.Get_processor_name());
Output the myid, numberprocs, and processor_name
into standard output.
JMPI.finalize();The last step that conclude the MPI usage is calling
finalize() method.