MPI4Py导致发送错误/

2024-09-26 18:17:32 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能告诉我为什么这个最小工作示例(MWE)抱怨TypeError: expected a writeable buffer object?在

MWE:

#!/usr/bin/env python
from mpi4py import MPI

# MPI Initialization
rank = MPI.COMM_WORLD.Get_rank()
comm = MPI.COMM_WORLD

if __name__ == '__main__':
   a = True
   if rank == 0:
      a = False
      comm.Send ( [ a, MPI.BOOL ], 1, 111 )
   if rank == 1:
      comm.Recv ([ a, MPI.BOOL], 0, 111 )

错误:

^{pr2}$

Tags: 示例worldifobjectusrbufferboolexpected
3条回答

我不知道为什么我会得到以上的错误,所以如果有人知道,请回答,我会接受。也就是说,如果我使用这种样式,我可以让代码工作(旧代码注释掉):

MWE:

#!/usr/bin/env python
from mpi4py import MPI

# MPI Initialization
rank = MPI.COMM_WORLD.Get_rank()
comm = MPI.COMM_WORLD

if __name__ == '__main__':
   a = True
   if rank == 0:
      a = False
      # comm.Send ( [ a, MPI.BOOL ], dest=1, tag=111 )
      comm.send ( a, dest=1, tag=111 )
   if rank == 1:
      # comm.Recv ([ a, MPI.BOOL], dest=0, tag=111 )
      a = comm.recv (source=0, tag=111 )

我不是MPI专家,但是我想知道numpy中的boolean数据类型是否与C中的boolean数据类型不兼容?也许这就是导致错误的原因。(不是证据,而是一些证据:http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars-built-in和{a2})

puk,正如您所提到的,一个解决方案是使用sendsend和recv(带小写的s和r)函数(http://mpi4py.scipy.org/docs/usrman/tutorial.html)将数据作为python对象传输。”在幕后,“mpi4py为此使用了pickle,因此可以发送任何通用的python对象。在

我回答的主要原因是发布一个使用整数数组的替代解决方案,0代表真,1代表假:

#!/usr/bin/env python
import numpy as np
from mpi4py import MPI

# MPI Initialization
rank = MPI.COMM_WORLD.Get_rank()
comm = MPI.COMM_WORLD

if __name__ == '__main__':
    a=np.array([0,])
    if rank == 0:
        a[0]=1
        comm.Send( [ a, MPI.INT ], 1, tag=111 )
        print rank,a
    if rank == 1:
        comm.Recv([ a, MPI.INT], 0, tag=111 )
        print rank,a

如果有人想利用更快的(根据mpi4py文档)numpy数组。在

MPI的大写函数接受类似黄油的对象,比如python中的NumPy数组。小写函数使用pickle来发送对象。在

http://mpi4py.readthedocs.org/en/latest/tutorial.html

相关问题 更多 >

    热门问题