GPrathap
GPrathap

Reputation: 7820

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)... 

Upvotes: 1

Views: 1789

Answers (3)

Swapnil
Swapnil

Reputation: 1

instead of code

if (rank == root) 
    {
        send_buffer = new int [unitsize * size];
    }

keep only following line.

send_buffer = new int [unitsize * size];

your output will be : MPJ Express (0.44) is started in the multicore configuration

0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3

Upvotes: 0

Aleem
Aleem

Reputation: 520

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

Upvotes: 5

Ansar
Ansar

Reputation: 131

I think scattered object (int array) in uninitialized. Can you try to initialize sendbuf to some dummy values and try it again.

Upvotes: 0

Related Questions