微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MPImpi4py的python问题,客户端无法连接

如何解决MPImpi4py的python问题,客户端无法连接

| 我得到的错误是: 追溯(最近一次通话):   文件\“ client2.py \”,第14行,在     端口= MPI.Lookup_name(服务,信息)   在mpi4py.MPI.Lookup_name中,文件\“ Comm.pyx \”,行1676(src / mpi4py.MPI.c:64562) mpi4py.MPI.Exception:MPI_ERR_NAME:无效的名称参数 我正在尝试使用mpi4py实现客户端/服务器模型。服务器可以发布名称并等待客户端。但是,客户端无法使用api中所述的必要方法,如下所示:
#! /usr/bin/env python

from mpi4py import MPI

rank = MPI.COMM_WORLD.Get_rank()

def log(msg,*args):
    if rank == 0:
        print msg % args

info = MPI.INFO_NULL
service = \'pyeval\'
log(\"looking-up service \'%s\'\",service)
port = MPI.Lookup_name(service,info) // PROBLEM HERE !
log(\"service located  at port \'%s\'\",port)

root = 0
log(\'waiting for server connection...\')
comm = MPI.COMM_WORLD.Connect(port,info,root)
log(\'server connected...\')

while True:
    done = False
    if rank == root:
        try:
            message = raw_input(\'pyeval>>> \')
            if message == \'quit\':
                message = None
                done = True
        except EOFError:
            message = None
            done = True
        comm.Send(message,dest=0,tag=0)
   else:
        message = None
    done = MPI.COMM_WORLD.Bcast(done,root)
    if done:
        break

log(\'disconnecting server...\')
comm.disconnect()
我还将发布服务器端代码,因为它可能会有所帮助:
#! /usr/bin/env python

from mpi4py import MPI

rank = MPI.COMM_WORLD.Get_rank()

def log(msg,*args):
    if rank == 0:
        print msg % args


log(\'\')

info = MPI.INFO_NULL

port = MPI.Open_port(info)
log(\"opened port: \'%s\'\",port)

service = \'pyeval\'
MPI.Publish_name(service,port)
log(\"published service: \'%s\'\",service)

root = 0
log(\'waiting for client connection...\')
comm = MPI.COMM_WORLD.Accept(port,root)
log(\'client connected...\')

while True:
    done = False
    if rank == root:
        message = comm.Recv(source=0,tag=0)
        if message is None:
            done = True
        else:
            try:
                print \'eval(%r) -> %r\' % (message,eval(message))
            except StandardError:
                print \"invalid expression: %s\" % message
    done = MPI.COMM_WORLD.Bcast(done,root)
    if done:
        break

log(\'disconnecting client...\')
comm.disconnect()

log(\'upublishing service...\')
MPI.Unpublish_name(service,port)

log(\'closing port...\')
MPI.Close_port(port)
    

解决方法

        您希望服务器生成客户端,以便它们可以通信。另外,您的Send / Recv / Bcasts应该是send / recv / bcast; mpi4py支持发送和发送,Recv和recv等,但是大写版本使用\“ regular \” C / C ++ / Fortran参数,而小写版本则更具pythonic功能。 因此,以下代码对我成功运行了-client.py:
#! /usr/bin/env python
from mpi4py import MPI

rank = MPI.COMM_WORLD.Get_rank()

def log(msg,*args):
    if rank == 0:
        print msg % args

info = MPI.INFO_NULL
service = \"pyeval\"
log(\"looking-up service \'%s\'\",service)
port = MPI.Lookup_name(service)
log(\"service located  at port \'%s\'\",port)

root = 0
log(\'waiting for server connection...\')
comm = MPI.COMM_WORLD.Connect(port,info,root)
log(\'server connected...\')

while True:
    done = False
    if rank == root:
        try:
            message = raw_input(\'pyeval>>> \')
            if message == \'quit\':
                message = None
                done = True
        except EOFError:
            message = None
            done = True
        comm.send(message,dest=0,tag=0)
    else:
        message = None
    done = MPI.COMM_WORLD.bcast(done,root)
    if done:
        break

log(\'disconnecting server...\')
comm.Disconnect()
和server.py:
#! /usr/bin/env python

from mpi4py import MPI

rank = MPI.COMM_WORLD.Get_rank()

def log(msg,*args):
    if rank == 0:
        print msg % args

log(\'\')

info = MPI.INFO_NULL

port = MPI.Open_port(info)
log(\"opened port: \'%s\'\",port)

service = \'pyeval\'
MPI.Publish_name(service,port)
log(\"published service: \'%s\'\",service)

MPI.COMM_WORLD.Spawn(\"./client.py\",maxprocs=1)

root = 0
log(\'waiting for client connection...\')
comm = MPI.COMM_WORLD.Accept(port,root)
log(\'client connected...\')

while True:
    done = False
    if rank == root:
        message = comm.recv(source=0,tag=0)
        if message is None:
            done = True
        else:
            try:
                print \'eval(%r) -> %r\' % (message,eval(message))
            except StandardError:
                print \"invalid expression: %s\" % message
    done = MPI.COMM_WORLD.bcast(done,root)
    if done:
        break

log(\'disconnecting client...\')
comm.Disconnect()

log(\'upublishing service...\')
MPI.Unpublish_name(service,port)

log(\'closing port...\')
MPI.Close_port(port)
但是在这里,您还必须处理一个事实,即大多数MPI实现只会将stdin的等级设置为0,因此客户端没有一种很好的方式来从终端获取输入(通常,这将如何工作)如果有多个客户?)     ,        我需要使用ompi-server进行名称发布和名称查找。执行服务器和客户端的以下步骤对我有用: 多功能服务器
rm -f /tmp/ompi-server.txt
killall ompi-server
ompi-server -r /tmp/ompi-server.txt
服务器
mpiexec --ompi-server file:/tmp/ompi-server.txt -n 1 python server.py
客户
mpiexec --ompi-server file:/tmp/ompi-server.txt -n 1 python client.py
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。