Java MPI广播

Java MPI广播,java,parallel-processing,mpi,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

刚刚开始使用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

任何帮助都将不胜感激。 谢谢
迈克

我相信你可能把参数搞错了。对
Bcast()
的方法调用具有以下方法签名(取自):

第一个参数通常描述一个数组(在本例中可能是一个整数数组)。第二个参数描述该数组中广播将从中开始的偏移量。第三个参数,
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]);

我相信这会达到你所期望的行为。希望这有帮助。

我已经有一段时间没有清理Java了,但是我相信您需要在所有进程上分配数组,因为主节点不是唯一一个使用数组
bvalue
的节点。哎呀!忽略了…谢谢!我更新了我的答案以反映这一点,非常感谢您的评论!这解释了很多!我仍然对
Bcast
的去向有点迷茫。在if(me==0)块中,我有Bcast,在worker块中,我有
println(bvalue)
但是,当我运行程序时,它只是在主块到达Bcast时挂起。有什么想法吗?再次感谢。您必须确保主进程和工作进程都在调用Bcast()方法,否则会看到程序挂起。例如,您可以执行类似于我上面发布的示例的操作,也可以像以前一样在worker块中进行另一个Bcast调用。就我个人而言,只要调用一个MPI方法,我的调试工作就会更轻松:)
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]);