MPI_Barrier not working inside a loop

与世无争的帅哥 提交于 2019-12-12 17:08:09

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/6840625/mpi-barrier-not-working-inside-a-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!