我正在使用消息传递接口(MPI)的主工作者结构,但每当我调用Receive函数时,我不需要按照发送顺序接收消息,而是需要接收从主服务器发送到服务器的最后一条消息。每个处理器并忽略以前的处理器!
我的问题是,如果有任何方法可以访问每个处理器的缓冲区并选择队列中的最后一条消息?
答案 0 :(得分:3)
不,你不能只是进入队列;但您可以测试以查看MPI_Probe或MPI_Iprobe是否存在更多消息,并且当有更多消息存在时,请继续接收并丢弃旧数据:
#!/usr/bin/env python
from mpi4py import MPI
import time
def waiter(comm, sendTask):
# wait for messages to be present
while not comm.Iprobe(source=sendTask, tag=1):
time.sleep(1)
# read all messages while more are available, discarding old
while comm.Iprobe(source=sendTask, tag=1):
lastMsg = comm.recv(source=sendTask, tag=1)
if lastMsg is None:
print "No messages pending"
else:
print "Last message was ", lastMsg
comm.Barrier()
def sender(comm, waitTask):
for msgno in range(5):
print "sending: ", msgno
comm.send(msgno, dest=waitTask, tag=1)
print "sending: ", -1
comm.send(-1, dest=waitTask, tag=1)
comm.Barrier()
if __name__== "__main__":
comm = MPI.COMM_WORLD
sendTask = 1
waitTask = 0
if comm.rank == waitTask:
waiter(comm, sendTask)
elif comm.rank == sendTask:
sender(comm, waitTask)
else:
comm.Barrier()
跑步给出
$ mpirun -np 2 ./readall.py
sending: 0
sending: 1
sending: 2
sending: 3
sending: 4
sending: -1
Last message was -1