Java MPI广播

时间:2015-04-26 22:36:48

标签: java parallel-processing mpi

使用MPI的Java接口来掌握并行编程。 只是想知道是否有人可以非常简单地解释广播是如何工作的?

我有以下内容:

if (me ==0) { // This is the master process
   int bvalue = 4;
   MPI.COMM_WORLD.Bcast(bvalue, 0, 4, MPI.INT, 0);
}
else { // The worker processes
   MPI.COMM_WORLD.Bcast(bvalue, 0, 4, MPI.INT, 0);
}

所以我知道工作进程必须调用bcast来接收bvalue ..我如何在workers部分中使用这个值?

如果我这样做:

int workerb = MPI.COMM_WORLD.Bcast(bvalue, 0, 4, MPI.INT, 0);

我得到一个不兼容的类型错误,void无法转换为int。

非常感谢任何帮助。 谢谢, 麦克

1 个答案:

答案 0 :(得分:1)

我相信你可能会在这里弄错参数。对Bcast()的方法调用具有以下方法签名(取自here):

public void Bcast(java.lang.Object buf, int offset, int count, Datatype type, int root)

第一个参数通常描述一些东西(在这种情况下可能是一个整数数组)。第二个参数描述了从此数组开始广播的偏移量。第三个参数count描述了要发送的偏移量中的元素数量。最后一个参数描述了发送者的等级(0是主节点)。

您收到该错误,因为Bcast()方法调用不返回任何内容(返回void)。此外,我认为对Bcast的调用是阻止调用,因此您基本上可以将上面的代码重写为:

int[] bvalue = new int[1];

if (me == 0){ //this is the master process 
     bvalue[0] = 4;
}

//the master node will broadcast the value '4' and
//workers will block here waiting for the broadcast 
//to complete
MPI.COMM_WORLD.Bcast(bvalue, 0, 1, MPI.INT, 0);

//output the contents of bvalue
System.out.println("bvalue is " + bvalue[0]);

这应该完成你所期望的行为。希望这会有所帮助..