MPI(mpi4py)的python问题,客户端无法连接

时间:2011-05-30 16:26:18

标签: python network-programming client-server parallel-processing mpi

我得到的错误是:

追踪(最近一次通话):   文件“client2.py”,第14行,in     port = MPI.Lookup_name(服务,信息)   文件“Comm.pyx”,第1676行,mpi4py.MPI.Lookup_name(src / mpi4py.MPI.c:64562) mpi4py.MPI.Exception:MPI_ERR_NAME:无效的名称参数

我正在尝试使用mpi4py实现客户端/服务器模型。服务器可以Publish_name并等待客户端。但是,客户端无法使用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, info, port)
log("published service: '%s'", service)

root = 0
log('waiting for client connection...')
comm = MPI.COMM_WORLD.Accept(port, info, 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, info, port)

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

2 个答案:

答案 0 :(得分:1)

您希望服务器生成客户端以便它们可以进行通信。此外,您的发送/接收/ Bcast应该发送/ recv / bcast; mpi4py支持Send和send,Recv和recv等,但大写版本采用“常规”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, info, 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, info, 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, info, port)

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

但是在这里你还必须处理这样一个事实,即大多数MPI实现只会给stdin排名0,所以客户端没有很好的方法从终端获取输入(一般情况下,如何如果有多个客户,可以工作吗?)

答案 1 :(得分:1)

我需要使用ompi-server进行名称发布和名称查找。以下步骤执行服务器和客户端为我工作:

OMPI服务器

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