mpi - 如何访问发送到 MPI 中处理器的最后一条消息?

标签 mpi

我使用的是使用消息传递接口(interface)(MPI)的主从结构,但是每当我调用接收函数时,我需要接收从主节点发送到主节点的最后一条消息,而不是按照发送顺序接收消息。每个处理器并忽略前面的处理器!

我的问题是,是否有任何方法可以访问每个处理器的缓冲区并选择队列中的最后一条消息?

最佳答案

不,你不能只是查看队列;但您可以测试是否存在更多消息 MPI_ProbeMPI_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

关于mpi - 如何访问发送到 MPI 中处理器的最后一条消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29764318/

相关文章:

c - Ubuntu 上的 MPI 代码只使用了一个处理器

c - MPI基础类(class)

c - MPI Haversine 用于 C 中的距离计算

为包含动态数组的结构创建 MPI 类型

java - 需要对 MPI 进行一些澄清

algorithm - 所有 MPI 算法的详细信息?

c - 使用带有 MPI 的 master-worker 时,哪个大小的 block 将产生最佳性能?

c - 尝试读取大文件时 MPI_File_read_at_all 给出无效的计数参数

c++ - 将数组分散到仅工作任务

c - MPI_TYPE_CREATE_HVECTOR