MPI_Barrier 不在循环内工作
我对 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 和循环? (我只发现在不同任务中使用不同大小的循环来避免死锁的警告。) 如果有的话,我该怎么做才能强制任务在开始新的循环迭代之前相互等待? 如果没有,这段代码有什么问题?
谢谢!
I have running some tests on the MPI functions to understand how it works and have got a weird result with the MPI_Barrier: it does what everyone would expect if I use it in code like
int main(int argc, char *argv[])
{
<some code>
MPI_Barrier(MPI_COMM_WORLD);
<more code>
MPI_Barrier(MPI_COMM_WORLD);
<...>
}
but when I call it from inside a loop i get random results. To be specific, I have the following test code:
#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;
}
When i run it with 3 tasks i randomly get:
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
which is waht i shall expect, or just the oposite:
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
or something in between, like
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
Can anyone tell me if there is some conflict between MPI_Barrier and loops? (I have only found warnings to avoid deadlocks using loops of different sizes in different tasks.)
If there is one, what can i do to force tasks to wait for each other before starting a new iteration of the loop?
If there isn't, what is wrong with this code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它与你的循环无关。 MPI 没有暗示 IO 的顺序。如果您需要按顺序打印,则必须明确将它们发送到一个级别,然后将它们全部打印在那里。
It has nothing to do with your loops. There's no sequentialization of IO implied by MPI. If you need prints to come out in order, you'll have to explicitly send them to one rank, and print them all there.