【发布时间】:2011-05-30 16:26:17
【问题描述】:
我得到的错误是:
Traceback(最近一次调用最后一次): 文件“client2.py”,第 14 行,在 端口 = 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)
【问题讨论】:
-
我尝试了 mpirun 和 mpiexec:mpirun -np 1 python server.py 和 mpiexec -np 1 python server.py
标签: python network-programming client-server parallel-processing mpi