javampicluster-computingmpj-express

How Scatter and Gather works in MP J Express


I am to create a new cluster api for my project. So I started learning MP J Express these days.I just wrote program using Scatter and Gather in this way. But I am getting Null point exception.No idea where when wrong ?

Here is my code

    import mpi.MPI;

    public class ScatterGather {
        public static void main(String args[]){
        MPI.Init(args);
        int rank = MPI.COMM_WORLD.Rank();
        int size = MPI.COMM_WORLD.Size();
        int unitSize=4,root=0;
        int sendbuf[]=null;
        if(rank==root){
          sendbuf= new int[unitSize*size];
        }
        int recvbuf[] = new int[unitSize];
      MPI.COMM_WORLD.Scatter(sendbuf,0,unitSize,MPI.INT,recvbuf,0,unitSize,MPI.INT,root);
        if(rank!=root){
            for(int i=0;i<unitSize;i++){
               recvbuf[i]=rank;
            }
        }
      MPI.COMM_WORLD.Gather(recvbuf,0,unitSize,MPI.INT,sendbuf,0,unitSize,MPI.INT,root);
        if(rank==root){
           for(int i=0;i<unitSize;i++){
                System.out.println(sendbuf[i]+ " ");
           }
        }
        MPI.Finalize();
     }
}

Here is the error log

MPJ Express (0.43) is started in the multicore configuration
mpi.MPIException: java.lang.NullPointerException
at mpi.SimplePackerInt.unpack(SimplePackerInt.java:112)
at mpi.Comm.recv(Comm.java:1499)
at mpi.PureIntracomm.MST_Scatter(PureIntracomm.java:1102)
at mpi.PureIntracomm.Scatter(PureIntracomm.java:1066)
at mpi.Intracomm.Scatter(Intracomm.java:420)
at ScatterGather.main(ScatterGather.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)... 

Solution

  • Try initializing sendbuf= new int[unitSize*size]; for each process. Remove if(rank==root) condition and see if it works.

    Scatter finds sendbuf array to be null that's why it is throwing NullPointerException