【问题标题】:Difficulty with MPI_Bcast: how to ensure that "correct" root is broadcastingMPI_Bcast 的困难:如何确保“正确”的根正在广播
【发布时间】: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 可以解决您的问题,您是否查看过它?

标签: c++ c mpi


【解决方案1】:

没有办法在接收者不知道根是什么的情况下执行MPI_Bcast。如果你知道只有一个 root,你可以先做一个MPI_Allreduce 同意它:

int root, maybe_root;
int i_am_root = ...;
maybe_root = (i_am_root ? rank : 0);
MPI_Allreduce(&maybe_root, &root, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);

然后每个等级都会知道相同的根,您可以进行广播。

【讨论】:

  • 谢谢,耶利米,非常感谢。效果很好!实际上,在我阅读您的回复之前,我才刚刚开始提出相同的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 2012-09-23
  • 2017-04-23
  • 1970-01-01
相关资源
最近更新 更多