python MPI sendrecv()传递python对象 [英] python MPI sendrecv() to pass a python object

查看:374
本文介绍了python MPI sendrecv()传递python对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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 idxidctat 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆