问题描述
当我运行"Open MPI"程序时,它通常以我想知道的随机顺序分配等级有没有办法总是按顺序分配等级?
When i run an "Open MPI" program, it generally assigns ranks in random order I want to knowis there a way to always assign ranks in order?
所以代替这个
Hello, World. I am 2 of 3
Hello, World. I am 0 of 3
Hello, World. I am 1 of 3
我能得到这个吗
Hello, World. I am 0 of 3
Hello, World. I am 1 of 3
Hello, World. I am 2 of 3
编辑
这是代码
PROGRAM hello
INCLUDE 'mpif.h'
INTEGER*4 :: numprocs, rank, ierr
CALL MPI_INIT(ierr)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD, numprocs, ierr)
CALL MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
write(*,*) 'Hello World. I am', rank, 'of', numprocs
CALL MPI_FINALIZE(ierr)
END PROGRAM hello
我在运行时已在i5处理器(4个线程)上对其进行了测试
I have tested it on i5 processor (4 threads) when i run
mpirun -np 4 myprog
它按我想要的方式工作,按0-3的顺序打印,否则(如上图所示的3)就不会做(测试了100次)
it works as i want it to, ranks printed in order 0-3, otherwise (like with 3 as shown above) it just wont do it (tested it like 100 time)
推荐答案
在大多数MPI实现中,分配等级的顺序从来都不是随机的,通常有一些机制可以对其进行精确控制.随机的是不同等级的输出通过IO重定向机制到达MPI启动器(mpirun
,mpiexec
等)的顺序.由于通常会涉及缓冲,因此无法确定是否会发生以下情况:等级0在等级1之前输出一些文本,然后等级0的输出一定要在等级1的输出之前到达.保证文本输出排序的唯一可移植方法是让所有等级通过一个单独的等级来传递其消息. IO.
The order in which ranks are assigned is never random with most MPI implementations and usually there are mechanisms to precisely control it. What is random is the order in which the output from the different ranks arrives at the MPI launcher (mpirun
, mpiexec
, etc.) through the IO redirection mechanism. Due to the buffering that's usually involved, one could never be sure that if e.g. rank 0 outputs some text before rank 1, then the output from rank 0 would necessarily arrive before the output from rank 1. The only portable way to guarantee ordering of the text output is to have all ranks channel their messages through a single rank that does IO.
通过某些实现,可能可以执行诸如线性令牌传递或一系列障碍之类的事情,例如:
With some implementations it might be possible to do something like linear token passing or a sequence of barriers, e.g.:
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
for (int i = 0; i < size; i++)
{
if (i == rank)
printf("Hello world! I am %d of %d\n", rank, size);
MPI_Barrier(MPI_COMM_WORLD);
}
此类代码背后的原理是,MPI_Barrier
可能会在完成通信之前进行挂起的通信操作,包括那些带有重定向的标准输出的通信操作.仍然不能保证printf()
的输出会立即显示在mpirun
/mpiexec
的控制台输出中.
The rationale behind such code is that MPI_Barrier
could possibly progress pending communication operations before it completes, including those that carry the redirected standard output. Still there is no guarantee that the output from printf()
is immediately shown in the console output of mpirun
/mpiexec
.
这篇关于开放的MPI排名不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!