本文介绍了MPI_Barrier不是一个循环内的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经运行的MPI函数一些测试,以了解它是如何工作的,并得到了与MPI_Barrier怪异的结果:它如果我用它在code像

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);
  <...>
}

但是当我把它从一个循环内我得到随机的结果。具体而言,我有以下的测试code:

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;
}

当我与3个任务运行它,我随机得到:

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

这是啥子我应期待,或只是oposite:

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

或介于两者之间,如

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和循环之间的一些冲突? (我只找到警告避免使用在不同的任务不同大小的循环死锁。)
如果有一个,我能做些什么开始循环的一个新的迭代之前强制任务等待对方?
如果没有,什么是错的这个code?

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?

谢谢!

推荐答案

它无关,与你的循环。有没有用MPI暗示IO的sequentialization。如果您需要打印才能出来,你必须明确地将其发送给一个等级,并将其打印都在那里。

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.

这篇关于MPI_Barrier不是一个循环内的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:35