gpt4 book ai didi

c - MPI_Barrier 不在循环内工作

转载 作者:行者123 更新时间:2023-12-04 06:19:02 28 4
gpt4 key购买 nike

我对 MPI 函数运行了一些测试以了解它是如何工作的,并且使用 MPI_Barrier 得到了一个奇怪的结果:如果我在代码中使用它,它会做每个人都期望的事情

int main(int argc, char *argv[])
{
<some code>
MPI_Barrier(MPI_COMM_WORLD);
<more code>
MPI_Barrier(MPI_COMM_WORLD);
<...>
}

但是当我从循环内部调用它时,我会得到随机结果。具体来说,我有以下测试代码:
#include "mpi.h" 
#include <stdio.h>

int main(int argc, char *argv[])
{
int i, rb, rank, nprocs;

MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);

i=0;
while(i<5)
{
rb=MPI_Barrier(MPI_COMM_WORLD);
printf("Itartion %d. I am %d of %d. MPIBarrierRes: %d\n", i, rank, nprocs, rb);
i++;
}
MPI_Finalize();
return 0;
}

当我用 3 个任务运行它时,我随机得到:
Itartion 0.  I am 0 of 3. MPIBarrierRes: 0
Itartion 0. I am 2 of 3. MPIBarrierRes: 0
Itartion 0. I am 1 of 3. MPIBarrierRes: 0
Itartion 1. I am 0 of 3. MPIBarrierRes: 0
Itartion 1. I am 1 of 3. MPIBarrierRes: 0
Itartion 1. I am 2 of 3. MPIBarrierRes: 0
Itartion 2. I am 0 of 3. MPIBarrierRes: 0
Itartion 2. I am 1 of 3. MPIBarrierRes: 0
Itartion 2. I am 2 of 3. MPIBarrierRes: 0
Itartion 3. I am 0 of 3. MPIBarrierRes: 0
Itartion 3. I am 1 of 3. MPIBarrierRes: 0
Itartion 3. I am 2 of 3. MPIBarrierRes: 0
Itartion 4. I am 0 of 3. MPIBarrierRes: 0
Itartion 4. I am 1 of 3. MPIBarrierRes: 0
Itartion 4. I am 2 of 3. MPIBarrierRes: 0

这是我所期望的,或者只是相反:
Itartion 0.  I am 2 of 3. MPIBarrierRes: 0
Itartion 1. I am 2 of 3. MPIBarrierRes: 0
Itartion 2. I am 2 of 3. MPIBarrierRes: 0
Itartion 3. I am 2 of 3. MPIBarrierRes: 0
Itartion 4. I am 2 of 3. MPIBarrierRes: 0
Itartion 0. I am 0 of 3. MPIBarrierRes: 0
Itartion 1. I am 0 of 3. MPIBarrierRes: 0
Itartion 2. I am 0 of 3. MPIBarrierRes: 0
Itartion 3. I am 0 of 3. MPIBarrierRes: 0
Itartion 4. I am 0 of 3. MPIBarrierRes: 0
Itartion 0. I am 1 of 3. MPIBarrierRes: 0
Itartion 1. I am 1 of 3. MPIBarrierRes: 0
Itartion 2. I am 1 of 3. MPIBarrierRes: 0
Itartion 3. I am 1 of 3. MPIBarrierRes: 0
Itartion 4. I am 1 of 3. MPIBarrierRes: 0

或者介于两者之间的东西,比如
Itartion 0.  I am 1 of 3. MPIBarrierRes: 0
Itartion 0. I am 0 of 3. MPIBarrierRes: 0
Itartion 1. I am 0 of 3. MPIBarrierRes: 0
Itartion 0. I am 2 of 3. MPIBarrierRes: 0
Itartion 1. I am 1 of 3. MPIBarrierRes: 0
Itartion 2. I am 0 of 3. MPIBarrierRes: 0
Itartion 1. I am 2 of 3. MPIBarrierRes: 0
Itartion 2. I am 1 of 3. MPIBarrierRes: 0
Itartion 3. I am 0 of 3. MPIBarrierRes: 0
Itartion 2. I am 2 of 3. MPIBarrierRes: 0
Itartion 3. I am 1 of 3. MPIBarrierRes: 0
Itartion 4. I am 0 of 3. MPIBarrierRes: 0
Itartion 3. I am 2 of 3. MPIBarrierRes: 0
Itartion 4. I am 1 of 3. MPIBarrierRes: 0
Itartion 4. I am 2 of 3. MPIBarrierRes: 0

谁能告诉我 MPI_Barrier 和循环之间是否存在冲突? (我只发现了在不同任务中使用不同大小的循环避免死锁的警告。)
如果有,我可以做些什么来强制任务在开始新的循环迭代之前相互等待?
如果没有,这段代码有什么问题?

谢谢!

最佳答案

它与您的循环无关。 MPI 没有暗示 IO 的顺序化。如果您需要按顺序打印出来,则必须明确地将它们发送到一个等级,并在那里全部打印出来。

关于c - MPI_Barrier 不在循环内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6840625/

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