python MPI sendrecv()传递python对象 [英] python MPI sendrecv() to pass a python object
问题描述
我正在尝试使用mpi4py的sendrecv()传递字典obj.
I am trying to use mpi4py's sendrecv() to pass a dictionary obj.
from mpi4py import MPI
comm=MPI_COMM_WORLD
rnk=comm.Get_rank()
size=comm.Get_size()
idxdict={1:2}
buffer=None
comm.sendrecv(idxdict,dest=(rnk+1)%size,sendtag=rnk,recvobj=buffer,source=(rnk-1+size)%size,recvtag=(rnk-1+size)%size)
idxdict=buffer
如果我在最后一步打印idxidct
,我会得到一堆"None",因此字典idxdict
不会在内核之间传递.如果我使用字典作为缓冲区:buffer={}
,则存在类型错误:TypeError: expected a writeable buffer object
.
If I print idxidct
at the last step, I will get a bunch of "None"s, so the dictionary idxdict
is not passed between cores. If I use a dictionary as buffer: buffer={}
, then there is typeerror:TypeError: expected a writeable buffer object
.
我做错了什么?非常感谢您的帮助.
What did I do wrong? Many thanks for your help.
推荐答案
我认为此处的文档具有误导性; sendrecv返回接收到的缓冲区,并且在我所看到的全部不使用接收对象参数(至少在较旧的版本1.2.x中).因此,您上面的代码不起作用(尽管实际上确实发生了接收),但以下代码却起作用:
I believe the documentation is misleading here; sendrecv returns the received buffer, and doesn't use the receive object argument at all that I can see (at least in older versions, 1.2.x). So your above code doesn't work (although the receive does in fact happen), but the below does:
from mpi4py import MPI
comm=MPI.COMM_WORLD
rnk=comm.Get_rank()
size=comm.Get_size()
idxdict={1:2}
buffer = comm.sendrecv(sendobj=idxdict,dest=(rnk+1)%size,source=(rnk-1+size)%size)
print "idxdict = ", idxdict
print "buffer = ", buffer
这篇关于python MPI sendrecv()传递python对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!