简体   繁体   English

mpi4py - 分散和聚集循环的死锁

[英]mpi4py - deadlock with scatter & gather loop

Hi I am using mpi4py to play around with MPI.嗨,我正在使用 mpi4py 来玩 MPI。 My usecase is that I have a Python Queue object which holds tasks to be processed like such:我的用例是我有一个 Python 队列 object 包含要处理的任务,如下所示:

from queue import Queue
my_queue = Queue()
my_queue.put({'task': [1, 2, 3]})
# while True:
if comm.rank == 0:
  task = my_queue.get()
else:
  task = None
work = comm.scatter(task, root=0)
calc = do_calculation(work) # whatever calculation
result = comm.gather(calc, root=0)
if comm.rank == 0:
  print(result)

This is working just fine and if I keep appending tasks (so in the beginning I put 2 tasks into the queue and just copy the upper piece of code) it works as well.这工作得很好,如果我继续追加任务(所以一开始我将 2 个任务放入队列并复制上面的代码)它也可以工作。 My goal is now to let this piece of code run in an infinite loop and whenever something is put into the queue (with ie a seperate thread etc.) it should be processed (my_queue.get() should be blocking so that's not the problem).我现在的目标是让这段代码在无限循环中运行,并且无论何时将某些内容放入队列(即使用单独的线程等),它都应该被处理(my_queue.get() 应该是阻塞的,所以这不是问题)。

But when I try to wrap this piece of code in an infinite loop while True: (see comment) the program does not produce any output and just locks up (seems like a deadlock).但是当我尝试将这段代码包装在一个无限循环while True: (见评论)该程序不会产生任何 output 并且只是锁定(似乎是死锁)。

I have found the answer to my issue.我找到了我的问题的答案。 There was no deadlock.没有僵局。 The code functioned perfectly the only problem was that the buffer of stdout was not flushed.代码运行完美,唯一的问题是标准输出的缓冲区没有被刷新。 This resulted that the results were not printed to the console and I assumed that the code produced a deadlock.这导致结果没有打印到控制台,我假设代码产生了死锁。

Adding the line sys.stdout.flush() to manually flush the buffer prints the results to the console and resolved my issue.添加行sys.stdout.flush()以手动刷新缓冲区会将结果打印到控制台并解决我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM