【发布时间】:2019-02-01 03:54:06
【问题描述】:
我在使用 MS-MPI 结束程序时遇到问题。
所有返回值似乎都很好,但我必须在 cmd 中按 ctrl + c 来结束它(它看起来不像还在计算,所以退出条件看起来很好)。
我想使用 N 个进程运行一个程序。当其中一个找到解决方案时,它应该将 flag 设置为 false,将其发送给所有其他人,然后在下一次迭代中他们将全部停止并且程序结束。
该程序实际上做了一些更高级的计算,为了清楚起见,我正在开发简化版本。我只是想确保沟通正常。
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
//sets as 0 -> (N-1) depending on number of processes running
int c = world_rank;
bool flag = true;
while (flag) {
std::cout << "process: " << world_rank << " value: " << c << std::endl;
c += world_size;
//dummy condition just to test stop
if (c == 13) {
flag = false;
}
MPI_Barrier(MPI_COMM_WORLD);
//I have also tried using MPI_Bcast without that if
if(!flag) MPI_Bcast(&flag, 1, MPI_C_BOOL, world_rank, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
} //end of while
MPI_Finalize();
return 0;
}
我认为它的工作原理: 它从定义其 c 和标志的每个进程开始,然后在每个(while)传递时将其 c 递增一个固定数字。然后当它到达停止条件时,它将标志设置为假并将其发送到所有剩余的进程。当我用 4 个进程运行它时得到什么:
进程:0 值:0
进程:2 值:2
进程:1 值:1
进程:3 值:3
进程:1 值:5
进程:3 值:7
进程:0 值:4
进程:2 值:6
进程:3 值:11
进程:1 值:9
进程:2 值:10
进程:0 值:8
进程:3 值:15
进程:2 值:14
进程:0 值:12
(我对那些额外的值很好)
但在那之后我必须用 ctrl + c 手动终止它。在 1 个进程上运行时,它从 1 到 12 平稳地退出并退出。
【问题讨论】: