[英]mpi4py scatter and gather with large numpy arrays
I am trying to parallelise some operations on a large numpy array using mpi4py.我正在尝试使用 mpi4py 在大型 numpy 数组上并行化一些操作。 I am currently using
numpy.array_split
to divide the array into chunks, followed by com.scatter
to send the array to different cores and then comm.gather
to collect the resulting arrays.我目前使用
numpy.array_split
到阵列成大块,随后划分com.scatter
到阵列发送到不同的核,然后comm.gather
收集所得阵列。 A minimal (not) working example is below:一个最小(非)工作示例如下:
import numpy as np
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if rank == 0:
test = np.random.rand(411,48,52,40)
test_chunks = np.array_split(test,size,axis=0)
else:
test_chunks = None
test_chunk = comm.scatter(test_chunks,root=0)
output_chunk = np.zeros([np.shape(test_chunk)[0],128,128,128])
for i in range(0,np.shape(test_chunk)[0],1):
print(i)
output_chunk[i,0:48,0:52,0:40] = test_chunk[i]
outputData = comm.gather(output_chunk,root=0)
if rank == 0:
outputData = np.concatenate(outputData,axis = 0)
Running this gives me the error:运行这给了我错误:
File "test_4d.py", line 23, in <module>
outputData = comm.gather(output_chunk,root=0)
File "Comm.pyx", line 869, in mpi4py.MPI.Comm.gather (src/mpi4py.MPI.c:73266)
File "pickled.pxi", line 614, in mpi4py.MPI.PyMPI_gather (src/mpi4py.MPI.c:33592)
File "pickled.pxi", line 146, in mpi4py.MPI._p_Pickle.allocv (src/mpi4py.MPI.c:28517)
File "pickled.pxi", line 95, in mpi4py.MPI._p_Pickle.alloc (src/mpi4py.MPI.c:27832)
SystemError: Negative size passed to PyString_FromStringAndSize
This error seems to result from the large size of the numpy arrays being collected by gather;这个错误似乎是由gather收集的大量numpy数组引起的; since scatter and gather send the arrays as a list of arrays, it appears easy to exceed the list size.
由于 scatter 和 gather 将数组作为数组列表发送,因此很容易超过列表大小。 One suggestion I have come across is to use comm.Scatter and comm.Gather.
我遇到的一个建议是使用 comm.Scatter 和 comm.Gather。 However, I am struggling to find clear documentation for these functions and so far have been unable to successfully implement them.
但是,我正在努力为这些功能找到清晰的文档,到目前为止还无法成功实现它们。 For example:
例如:
replacing替换
outputData = comm.gather(output_chunk,root=0)
with the line与线
outputData=comm.Gather(sendbuf[test_chunks,MPI.DOUBLE],recvbuf=output_chunk,MPI.DOUBLE],root=0)
gives the error:给出错误:
File "Comm.pyx", line 415, in mpi4py.MPI.Comm.Gather (src/mpi4py.MPI.c:66916)
File "message.pxi", line 426, in mpi4py.MPI._p_msg_cco.for_gather (src/mpi4py.MPI.c:23559)
File "message.pxi", line 355, in mpi4py.MPI._p_msg_cco.for_cco_send (src/mpi4py.MPI.c:22959)
File "message.pxi", line 111, in mpi4py.MPI.message_simple (src/mpi4py.MPI.c:20516)
File "message.pxi", line 51, in mpi4py.MPI.message_basic (src/mpi4py.MPI.c:19644)
File "asbuffer.pxi", line 108, in mpi4py.MPI.getbuffer (src/mpi4py.MPI.c:6757)
File "asbuffer.pxi", line 50, in mpi4py.MPI.PyObject_GetBufferEx (src/mpi4py.MPI.c:6093)
TypeError: expected a readable buffer object
or with the line:或与线:
outputData = comm.Gather(sendbuf=test_chunks, recvbuf=output_chunk,root=0)
gives the error:给出错误:
File "test_4d_2.py", line 24, in <module>
outputData = comm.Gather(sendbuf=test_chunks, recvbuf=output_chunk,root=0)
File "Comm.pyx", line 415, in mpi4py.MPI.Comm.Gather (src/mpi4py.MPI.c:66916)
File "message.pxi", line 426, in mpi4py.MPI._p_msg_cco.for_gather (src/mpi4py.MPI.c:23559)
File "message.pxi", line 355, in mpi4py.MPI._p_msg_cco.for_cco_send (src/mpi4py.MPI.c:22959)
File "message.pxi", line 111, in mpi4py.MPI.message_simple (src/mpi4py.MPI.c:20516)
File "message.pxi", line 60, in mpi4py.MPI.message_basic (src/mpi4py.MPI.c:19747)
TypeError: unhashable type: 'numpy.ndarray'
Furthermore, the input matrix, test
may also increase in size, which could cause similar problems for comm.scatter
.此外,输入矩阵
test
大小也可能增加,这可能会导致comm.scatter
出现类似问题。 Aside from the problems I already have with comm.Gather
, I am not sure how to set up comm.Scatter
, since recvbuf
is defined based on the size of test_chunk
, which is the output of comm.scatter
, so hence I can't specify recvbuf
within comm.Scatter
.除了
comm.Gather
已经遇到的问题,我不确定如何设置comm.Scatter
,因为recvbuf
是根据test_chunk
的大小定义的,它是comm.scatter
的输出,因此我不能指定recvbuf
内comm.Scatter
。
The solution is to use comm.Scatterv
and comm.Gatherv
which send and receive the data as a block of memory, rather than a list of numpy arrays, getting around the data size issue.解决方案是使用
comm.Scatterv
和comm.Gatherv
,它们将数据作为内存块而不是 numpy 数组列表发送和接收,从而解决数据大小问题。 comm.Scatterv
and comm.Gatherv
assume a block of data in C-order (row-major) in memory and it is necessary to specify two vectors, sendcounts
and displacements
. comm.Scatterv
和comm.Gatherv
假设内存中的数据块为 C 序(行主),并且需要指定两个向量, sendcounts
和displacements
。 Sendcounts
gives the integer value (index) for the positions at which to split the input data (ie the starting point of each vector to send to a given core), while displacements
gives the length of that vector. Sendcounts
给出分割输入数据的位置的整数值(索引)(即发送到给定核心的每个向量的起点),而displacements
给出该向量的长度。 Hence it is possible to vary the amount of data sent to each core.因此,可以改变发送到每个内核的数据量。 More details can be found here: http://materials.jeremybejarano.com/MPIwithPython/collectiveCom.html
更多细节可以在这里找到: http : //materials.jeremybejarano.com/MPIwithPython/collectiveCom.html
An example using comm.Scatterv
and comm.Gatherv
for a 2D matrix is given here: Along what axis does mpi4py Scatterv function split a numpy array?此处给出了使用
comm.Scatterv
和comm.Gatherv
用于 2D 矩阵的示例: mpi4py Scatterv 函数沿哪个轴拆分 numpy 数组?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.