gpt4 book ai didi

Java MPI 广播

转载 作者:行者123 更新时间:2023-11-30 11:04:50 25 4
gpt4 key购买 nike

刚刚掌握使用 MPI 的 Java 接口(interface)进行并行编程。只是想知道是否有人可以非常简单地解释广播的工作原理?

我有以下内容:

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() 方法调用未返回任何内容(返回 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 MPI 广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29884414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com