Java MPI 广播

Java MPI broadcast

刚刚掌握使用 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。我将如何在工作部分中使用这个值?

如果我这样做:

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

出现类型不兼容错误,void 无法转换为 int。

如有任何帮助,我们将不胜感激。 谢谢, 麦克

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

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

第一个参数通常描述一个数组(在本例中可能是一个整数数组)。第二个参数描述广播开始的数组偏移量。第三个参数,count,描述了从偏移量开始发送多少个元素。最后一个参数描述了发送者的等级(0是主节点)。

您收到该错误是因为 Bcast() 方法调用没有 return 任何内容 (returns 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]);

我相信这应该会实现您所期望的行为。希望这有帮助..