next up previous contents
Next: Execution result Up: MPI Java Wrapper Implementation. Previous: Conclusion   Contents

Test example

test.java :
import MPI.*;

class test {
  static public void main(String[] args) {
    MPI JMPI = new MPI(args);
    byte[] buf = new byte[1024];
    int i, done = 0;
    int[] n = new int[1];
    int[] myid = new int[1];
    int[] numprocs = new int[1]; 
    Datatype N = new Datatype(n);
    double PI25DT = 3.141592653589793238462643;
    double[] mypi = new double[1];
    double[] pi = new double[1];
    Datatype Mypi = new Datatype(mypi);
    Datatype Pi = new Datatype(pi);
    double h, sum, x;
    double startwtime = 0.0;
    double endwtime;
    String processor_name;
    Status stat = new Status();

    JMPI.COMM_WORLD.Size(numprocs);
    JMPI.COMM_WORLD.Rank(myid);
    System.out.println("Process "+myid[0]+"/"+numprocs[0]+
      " on "+JMPI.Get_processor_name());
    n[0] = 0;
    while (done == 0) {
      if (myid[0] == 0) {
        n[0] = (n[0]==0) ? 100 : 0;
        startwtime = JMPI.Wtime();
      }
      JMPI.COMM_WORLD.Bcast(N, 0);
      if (n[0] != 0) {
        h = 1.0 / (double)n[0];
        h = 1.0 / (double)n[0];
        sum = 0.0;
        for (i = myid[0] + 1; i <= n[0]; i += numprocs[0]) {
          x = h * ((double)i - 0.5);
          sum += (4.0 / (1.0 + x*x));
        }
        mypi[0] = h * sum;
        JMPI.COMM_WORLD.Reduce(Mypi, Pi, JMPI.SUM, 0);
        if (myid[0] == 0) {
          System.out.println("pi is approximately "+pi[0]+
            ", Error is "+Math.abs(pi[0] - PI25DT));
          endwtime = JMPI.Wtime();
          System.out.println("wall clock time = " +
            (endwtime-startwtime));
        }
      } else done = 1;
    }
    n[0] = 99;
    System.out.println("Send self n: "+n[0]);
    JMPI.COMM_WORLD.Send(N, myid[0], 23);
    JMPI.COMM_WORLD.Recv(N, myid[0], 23, stat);
    System.out.println("Recv self n: "+n[0]);
    System.out.println("Status: cound: "+stat.count+", SOURCE: "+
      stat.SOURCE+", TAG: "+stat.TAG+", ERROR:"+stat.ERROR);
    JMPI.Buffer_attach(buf);
    n[0] = 98;
    System.out.println("Buffer Send self n: "+n[0]);
    JMPI.COMM_WORLD.Bsend(N, myid[0], 22);
    JMPI.COMM_WORLD.Recv(N, myid[0], 22, stat);
    System.out.println("Recv self n: "+n[0]);
    JMPI.Buffer_detach(buf);
    System.out.println("Status: cound: "+stat.count+", SOURCE: "+
      stat.SOURCE+", TAG: "+stat.TAG+", ERROR:"+stat.ERROR);
    JMPI.finalize();
  }
}



Bryan Carpenter 2002-07-12