Python 2.7 mpi4py-MPI_ERR_TRUNCATE:消息被截断
我将python中OPENMPI示例中的ring_c.c代码转换为实验mpi4py。这是我的密码Python 2.7 mpi4py-MPI_ERR_TRUNCATE:消息被截断,python-2.7,openmpi,mpi4py,Python 2.7,Openmpi,Mpi4py,我将python中OPENMPI示例中的ring_c.c代码转换为实验mpi4py。这是我的密码 from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() next_proc = (rank + 1) % size prev_proc = (rank + size - 1) % size tag = 2 message = 10 if 0 == rank:
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
next_proc = (rank + 1) % size
prev_proc = (rank + size - 1) % size
tag = 2
message = 10
if 0 == rank:
comm.send(message, dest=next_proc, tag=tag)
while(1):
message = comm.recv(message, source=prev_proc, tag=tag)
comm.Recv_init
if 0 == rank:
message = message - 1
print "Process %d decremented value: %d\n" %(rank, message)
comm.send(message, dest=next_proc, tag=tag)
if 0 == message:
print "Process %d exiting\n" %(rank)
break;
if 0 == rank:
message = comm.recv(message, source=prev_proc, tag=tag)
例如,当我通过mpiexec为任意数量的进程运行它时
mpiexec -n 10 python ring_py.py
它给出以下输出和错误:
Process 0 decremented value: 9
Process 0 decremented value: 8
Process 0 decremented value: 7
Process 0 decremented value: 6
Process 0 decremented value: 5
Process 0 decremented value: 4
Traceback (most recent call last):
File "ring_py.py", line 20, in <module>
message = comm.recv(message, source=prev_proc, tag=tag)
File "MPI/Comm.pyx", line 1192, in mpi4py.MPI.Comm.recv (src/mpi4py.MPI.c:106889)
File "MPI/msgpickle.pxi", line 287, in mpi4py.MPI.PyMPI_recv (src/mpi4py.MPI.c:42965)
mpi4py.MPI.Exception: MPI_ERR_TRUNCATE: message truncated
进程0递减值:9
进程0递减值:8
进程0递减值:7
进程0递减值:6
进程0递减值:5
进程0递减值:4
回溯(最近一次呼叫最后一次):
文件“ring_py.py”,第20行,在
message=comm.recv(message,source=prev_proc,tag=tag)
文件“MPI/Comm.pyx”,第1192行,在mpi4py.MPI.Comm.recv(src/mpi4py.MPI.c:106889)中
文件“MPI/msgpickle.pxi”,第287行,在mpi4py.MPI.PyMPI_recv(src/mpi4py.MPI.c:42965)中
mpi4py.MPI.Exception:MPI\u ERR\u TRUNCATE:消息已截断
几点观察
- 如果我将消息更改为6或50,它总是在进程0处抛出相同的错误递减值:4
- 如果我将消息值更改为4或更小,它将抛出相同的错误,而不做任何其他操作
- 进程的数量对代码的输出没有影响,除了执行时间
- 我使用的是MacBook Air 2012型号,带有macOS Sierra和Intel i7核心处理器
- 我在Python2.7中通过PIP安装了mpi4py,其版本为2.0.0
Jayant我尝试了您的代码,但在以下位置出错:
message = comm.recv(message, source=prev_proc, tag=tag)
声明:
TypeError:应为可写缓冲区对象
在or之后,我成功地尝试了:
message = comm.recv( source=prev_proc, tag=tag)
多亏了弗朗西斯,我才解开了这个谜。我知道Python是区分大小写的,但我忽略了一个事实,即有两组不同的函数用于发送和接收消息。Send/Recv使用Numpy数组,而Send/Recv使用引擎盖下的pickle 因此,第一个版本,即Numpy版本可以是:
from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
next_proc = (rank + 1) % size
prev_proc = (rank + size - 1) % size
tag = 2
message = np.array([0,])
message[0] = 10
if 0 == rank:
print "Process %d sending %d to %d, tag %d (%d processes in ring)\n" %(rank, message, next_proc, tag, size)
comm.Send([message, MPI.INT], dest=next_proc, tag=tag)
while(1):
comm.Recv([message, MPI.INT], source=prev_proc, tag=tag)
if 0 == rank:
message = message - 1
print "Process %d decremented value: %d\n" %(rank, message)
comm.Send([message, MPI.INT], dest=next_proc, tag=tag)
if 0 == message[0]:
print "Process %d exiting\n" %(rank)
break;
from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
next_proc = (rank + 1) % size
prev_proc = (rank + size - 1) % size
tag = 2
message = 10
if 0 == rank:
print "Process %d sending %d to %d, tag %d (%d processes in ring)\n" %(rank, message, next_proc, tag, size)
comm.send(message, dest=next_proc, tag=tag)
while(1):
message = comm.recv(source=prev_proc, tag=tag)
if 0 == rank:
message = message - 1
print "Process %d decremented value: %d\n" %(rank, message)
comm.send(message, dest=next_proc, tag=tag)
if 0 == message:
print "Process %d exiting\n" %(rank)
break;
第二个版本,即pickle版本可以是:
from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
next_proc = (rank + 1) % size
prev_proc = (rank + size - 1) % size
tag = 2
message = np.array([0,])
message[0] = 10
if 0 == rank:
print "Process %d sending %d to %d, tag %d (%d processes in ring)\n" %(rank, message, next_proc, tag, size)
comm.Send([message, MPI.INT], dest=next_proc, tag=tag)
while(1):
comm.Recv([message, MPI.INT], source=prev_proc, tag=tag)
if 0 == rank:
message = message - 1
print "Process %d decremented value: %d\n" %(rank, message)
comm.Send([message, MPI.INT], dest=next_proc, tag=tag)
if 0 == message[0]:
print "Process %d exiting\n" %(rank)
break;
from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
next_proc = (rank + 1) % size
prev_proc = (rank + size - 1) % size
tag = 2
message = 10
if 0 == rank:
print "Process %d sending %d to %d, tag %d (%d processes in ring)\n" %(rank, message, next_proc, tag, size)
comm.send(message, dest=next_proc, tag=tag)
while(1):
message = comm.recv(source=prev_proc, tag=tag)
if 0 == rank:
message = message - 1
print "Process %d decremented value: %d\n" %(rank, message)
comm.send(message, dest=next_proc, tag=tag)
if 0 == message:
print "Process %d exiting\n" %(rank)
break;
两个版本将提供相同的输出。根据MPI教程,不同之处在于它们的执行时间,该教程说Numpy版本会更快