【发布时间】:2012-03-27 20:33:17
【问题描述】:
我对 MPI(使用 C)比较陌生,并且在使用 MPI_Bcast 向所有进程发送 int 时遇到了一些麻烦。
在我的代码中,我决定哪个等级是 for 循环中的根,其中不同的进程负责循环的不同元素。然后,我想将一个结果从 root 广播到所有进程,除了所有非 root 进程不知道该 bcast 来自谁,所以不要接收它。
代码块看起来像这样:
for (iX=start; iX<end; iX++)
//start and end are the starting row and ending row for each processes, defined earlier
for (int iY=1; iY<nn; iY++)
// do some calculations
if (some condition)
int bcastroot = rank; // initialized above
int X = 111; // initialized above
else
//do other calculations
end
end
MPI_Bcast(&X, 1, MPI_INT, bcastroot, comm);
// remainder of code and MPI_FINALIZE
当我执行此代码时,无论 bcastroot 默认值(所有非 root 进程的值)都与 root 竞争,因此 X 无法正确广播。我不知道X的值,也无法预先预测根,所以无法提前定义。
我尝试初始化 bcastroot = -1,然后将其设置为等级,但这不起作用。有没有一种方法可以在不为所有进程设置 root 的情况下 Bcast 这个值?
谢谢, 乔佐尔
【问题讨论】:
-
如果你不知道root,你需要沟通它。也许 bcast 在这里不是正确的选择?如果 MPI_Allreduce 可以解决您的问题,您是否查看过它?