我有一个小的测试代码充当任务场,主进程将任务发送给从属。
我想知道是否有一种简单的方法可以让主人从任务列表中分享工作。例如,使用主人自己的发送和接收,但我不确定这是否会干净利落。
代码如下:
###!/usr/bin/env python
from mpi4py import MPI
import numpy
import sys
import os
import time
comm=MPI.COMM_WORLD
rank=comm.Get_rank()
size = comm.Get_size()
#print(rank,size)
if rank == 0:
tasks=([StopIteration] * (size-1))+[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
status=MPI.Status()
while tasks:
comm.recv(source=MPI.ANY_SOURCE, status=status)
data=tasks.pop()
print("master sending data ",data," to rank", status.Get_source())
comm.send(obj=data, dest=status.Get_source())
print("master: done send. Data left: ",tasks)
else:
print("slave", rank," entering loop")
for task in iter(lambda: comm.sendrecv(dest=0), StopIteration):
print("slave ", rank," recvd data", task)
print("slave ", rank," going to sleep")
time.sleep((task+0.5)*2*1.4)
欢迎任何明智的建议!
由于