MPI_Bcast的困难:如何确保"correct"根正在广播
Difficulty with MPI_Bcast: how to ensure that "correct" root is broadcasting
我对MPI(使用C)相对较新,在使用MPI_Bast向所有进程发送int时遇到了一些问题。
在我的代码中,我决定哪个秩是for循环中的根,其中不同的进程负责循环的不同元素。然后,我想从根向所有进程Bcast一个结果,除了所有非根进程不知道该从谁那里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
当我执行这段代码时,无论bcastrot默认值(所有非根进程的值)是什么,都会与root竞争,所以X没有正确广播。我不知道X的值,也不能预先预测根,所以我不能预先定义它。
我尝试过初始化bcastrot=-1,然后将其设置为rank,但这不起作用。有没有一种方法可以在不为所有进程设置根目录的情况下Bcast此值?
谢谢,JonZor
如果接收者不知道根是什么,就没有办法进行MPI_Bcast
。如果你知道只有一个根,你可以先进行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);
然后每个等级都知道相同的根,你就可以进行广播了。
相关文章:
- 如何确保C++函数在定义之前声明(如override关键字)
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 使用Unique_ptr确保工厂中的对象唯一
- MESI协议和std::atomic-它是否确保所有写入立即对其他线程可见?
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 确保流程关闭
- 如何确保接受的C++模板类型使运算符重载?
- 确保编译时的特定 std::array 位置
- C++ 确保子类为常量提供自定义值
- 如何进行未定义的位移"correct"
- 确保内存映射页位于内存中
- 确保套装新鲜度的有效方法
- C ++类型特征:确保子类实现方法
- 是否确保 2 个连续的 std::chrono::steady_clock::now() 不相等?
- 如何确保用户在一行上提示输入一次时输入名字和姓氏?
- 确保无变体的型号安全
- 确保特征将 AVX 矢量化用于特定操作
- 确保所有构造函数调用相同的函数 c++ 设计模式
- 如何确保 Visual C++ 不允许作用域枚举访问?
- MPI_Bcast的困难:如何确保"correct"根正在广播