MP J Express中Scatter and Gather的工作原理

GPr*_*hap 1 java cluster-computing mpi mpj-express

我要为我的项目创建一个新的集群api.所以这些天我开始学习MP J Express.我只是用这种方式用Scatter和Gather编写程序.但是我得到Null点异常.没有想到哪里出错了?

这是我的代码

    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();
     }
}
Run Code Online (Sandbox Code Playgroud)

这是错误日志

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)... 
Run Code Online (Sandbox Code Playgroud)

Ale*_*eem 5

尝试初始化sendbuf= new int[unitSize*size];每个进程.删除if(rank==root)条件,看看它是否有效.

Scatter发现sendbuf数组null就是它投掷的原因NullPointerException