Python MPI4Py通信屏障()未在MSMPI上阻塞?
在Windows10上的MSMPI上使用MPI4PY 3.0.0在Python 3.7.0中实现并行算法时,我遇到了GathereV无法收集所有内容的问题。。。当检查各种位的打印时,它似乎在以错误的顺序执行操作 我编写了一段代码,重复了这个问题:Python MPI4Py通信屏障()未在MSMPI上阻塞?,python,windows,mpi4py,ms-mpi,Python,Windows,Mpi4py,Ms Mpi,在Windows10上的MSMPI上使用MPI4PY 3.0.0在Python 3.7.0中实现并行算法时,我遇到了GathereV无法收集所有内容的问题。。。当检查各种位的打印时,它似乎在以错误的顺序执行操作 我编写了一段代码,重复了这个问题: from mpi4py import MPI from time import sleep import random comm = MPI.COMM_WORLD rank = comm.Get_rank() if rank == 0:
from mpi4py import MPI
from time import sleep
import random
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
if rank == 0:
sleep(2)
print("head finished sleeping")
comm.Barrier()
sleep(random.uniform(0, 2))
print(rank, 'finished sleeping ')
comm.Barrier()
if rank == 0:
print("All done!")
如果我正确理解通信屏障()
这应该会产生
head finished sleeping
2 finished sleeping
0 finished sleeping
3 finished sleeping
1 finished sleeping
4 finished sleeping
All done!
中间的部分按顺序排列,对吗?但是当我实际运行mpiexec-n5python.\blocking_test.py
时,我得到如下结果:
2 finished sleeping
1 finished sleeping
3 finished sleeping
head finished sleeping
0 finished sleeping
All done!
4 finished sleeping
我是否误解了通信屏障()的用法,或者我的环境是否有问题?它们以错误的顺序打印的原因是收集消息的MPI后端。所有子进程的标准输出流都没有直接连接到终端窗口,因为这在多台计算机上是不可能的 相反,MPI后端收集来自每个进程的所有消息。然后,它使用标准MPI调用在0级后端收集这些消息。正是在这种交流中,信息的顺序变得混乱 通常,在MPI过程中,标准输出不具有优先级,因此以正确的顺序打印输出几乎不费吹灰之力。通常,输出保存在正在运行的进程的输出缓冲区中。仅当发生以下事件时(可能更多),才会打印输出: 因此,您可以在打印时刷新标准输出:
1) print('my message'); sys.stdout.flush()
2) print('my message on newer version of python', flush=True)
然而,在实践中很难让它正常工作。如果多个MPI进程同时发生刷新事件。然后,多个进程将向排名0发送消息。因此,存在一种竞争条件,它本质上决定了事物的打印顺序。因此,为了使事情按正确的顺序进行,您需要混合应用同步和睡眠调用,以便很少调用刷新事件,从而避免竞争条件
我怀疑发生在您身上的是,输出只是在流程结束时刷新的。因为它同时发生在所有进程上,所以您看到的是这场通信竞赛的结果
我希望这能有所帮助。可能的副本
1) print('my message'); sys.stdout.flush()
2) print('my message on newer version of python', flush=True)