本文介绍了生成的进程可以与“主"进程通信吗? MPI通讯器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种使用MPI的方法使生成的进程与MPI_WORLD中的所有其他参与者进行通信,而不仅与生成该进程的父进程进行通信?

Is there a way using MPI to let spawned processes communicate with all other actors in the MPI_WORLD and not only with the parent that spawned the process?

现在我有两个主要的代理,即所谓的主"和从",它们运行以下代码(spawn.py):

Now I have two main agents, the so-called master and slave that run the following code (spawn.py):

# Spawn test: master and first slave
import sys

from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()

if rank == 0 : # master code
    print "i am the master on rank %i" % (rank)
    running = True
    while running :
        msg = comm.recv(source=MPI.ANY_SOURCE,tag=0)
        print "master received message: ", msg
        if msg == "Done" :
            running = False
    print "master is done"

if rank == 1 : # slave code
    no_spawn = 1
    print "I am a slave on rank %i, about the spawn lower slaves" % (rank)
    icomm = MPI.COMM_SELF.Spawn(sys.executable,args=["Cpi.py","ben"],maxprocs=no_spawn)
    comm.send("Test_comm",dest=0,tag=0)
    icomm.send("Test_icomm",dest=0,tag=0)
    isize =  icomm.Get_size()
    print "on slave, isize= %i" % (isize)
    rec = 0
    while rec <= (no_spawn-1) :
        msg = icomm.recv(source=MPI.ANY_SOURCE,tag=20)
        print "slave received message: %s (rec=%i)" % (msg, rec)
        rec = rec +1
    import time
    print "slave going to sleep\n"
    time.sleep(1)
    for i in range(no_spawn) :
        message = ("To spawn from slave",)
        icomm.send(message,dest=i,tag=0)
    for i in range(no_spawn) :
        message = ("Done",)
        icomm.send(message,dest=i,tag=0)

    msg = comm.recv(source=MPI.ANY_SOURCE,tag=0)
    print "slave received message: ", msg

    comm.send("Done",dest=0,tag=0)

    MPI.Finalize()

从站又产生了另外1个运行以下代码的进程(CPi.py,以mpi4py教程文件命名):

The slave, in turn, spawns 1 more processes that runs the following code (CPi.py, named after the mpi4py tutorial file):

#!/usr/bin/env python

import sys

from mpi4py import MPI
comm = MPI.COMM_WORLD
icomm = MPI.Comm.Get_parent()
irank = icomm.Get_rank()

print "Spawn irank=%i" % (irank)
message = "From_Spawn_%i"%(irank)
icomm.send(message,dest=0,tag=20)

running = True
while running :
    msg = icomm.recv(source=MPI.ANY_SOURCE,tag=0)
    print "Spawn on irank %i received message: %s " %(irank,msg)
    if msg[0] == "Done" :
        running = False

print "spawn %i sending a last msg to the master and the slave" % (irank)
comm.send(("To master from spawn",), dest=0,tag=0)
comm.send(("To slave from spawn",), dest=0,tag=0)

我可以使用comm通信器在主从服务器之间发送消息.在从属程序和生成的进程之间,我可以通过icomm通信器发送消息.但是我真正想要的是产生一个进程,并且该进程可以通过comm通信器与主服务器和从服务器进行通信.请参阅产生的过程的最后两行.那可能吗?生成的进程是否也可能监听从属服务器和主服务器使用的主计算机comm?它将发送到/收听哪个等级?

Between the master and slave I can send messages by using the comm communicator. Between the slave and the spawned process I can send messages over the icomm communicator. But what I really want is to spawn a process and that this process can communicate with both the master and slave over the comm communicator; see the last two lines of the spawned process. Is that possible? And would it be possible for the spawned process to listen as well to the main comm used by the slave and the master? Which rank would it be send to / listen to?

提供的代码不会终止,因为从属设备或主设备均未接收到由派生进程发送的最后两条消息. (我使用mpiexec -n 2 python spawn.py运行代码)

The provided code does not terminate because the last two messages send by the spawned process are neither received by the slave or the master. (I run the code with mpiexec -n 2 python spawn.py)

推荐答案

为使从属服务器生成与主服务器对话的进程,它需要使用MPI_CONNECT和MPI_ACCEPT之类的东西来创建另一个新的通信器.可以这样做,但是您必须使用从属设备在两者之间传输连接详细信息.

For the process spawned by the slave to talk to the master, it would need to create another new communicator using something like MPI_CONNECT and MPI_ACCEPT. It's possible to do, but you'd have to use the slave to transfer the connection details between the two.

在进行所有这些操作之前,请确保您不能仅仅通过更多的过程来开始工作,而是将不同的角色任意分配给不同的职位.在最好的情况下使用互通器会很痛苦,并且从正确数量的进程开始可能更简单.

Before you go through all of that, be sure that you can't just start your job with more process and arbitrarily assign different roles to different ranks. It's a pain to use intercommunicators under the best of circumstances and it's probably simpler to start out with the correct number of processes.

这篇关于生成的进程可以与“主"进程通信吗? MPI通讯器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:44