0

我正在制作一个简单的程序,我正在尝试使用 mpi4py 从 8x8 矩阵发送 4 x 4 矩阵子矩阵。我的方法是使用子数组数据类型,但我不断收到分段错误。

我的代码如下:

import numpy as np
from mpi4py import MPI

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

sizes = (8,8)    # Matrix dimensions
subsizes = (4,4) # Sub-matrix dimensions
displs = (0, 4, 32, 36)

counts = (1, 1, 1, 1) 
starts = (0,0)

mynum = counts[rank]
glob = np.empty((8,8), dtype = 'f')
local = np.empty((4,4), dtype = 'f')

if rank == 0:  # Fill each quadrant with 0-3

    for row in range(0,4):
        for col in range(0,4):
            glob[row, col] = 0
    for row in range(0,4):
        for col in range(4,8):
            glob[row, col] = 1
    for row in range(4,8):
        for col in range(0,4):
            glob[row, col] = 2
    for row in range(4,8):
        for col in range(4,8):
            glob[row, col] = 3
    print glob

submatrixType = MPI.DOUBLE.Create_subarray(sizes, subsizes, starts, order =    MPI.ORDER_C)
submatrixType.Commit()

sendbuf = [glob, counts, displs, submatrixType]
recvbuf = [local, 16, MPI.DOUBLE]

comm.Scatterv(sendbuf, recvbuf, root = 0)
# or use
#comm.Scatterv(sendbuf, recvbuf, MPI.DOUBLE)

print rank, " " ,local

运行 4 个进程时,我的结果如下:

Proc 0: [[0. 0. 0. 0.] 
[1. 1. 1. 1.]
[0. 0. 0. 0.]
[1. 1. 1. 1.]]

Traceback (most recent call last):
Traceback (most recent call last):
  File "main.py", line 41, in <module>
      File "main.py", line 41, in <module>
    comm.Scatterv(sendbuf, local, root = 0)
comm.Scatterv(sendbuf, local, root = 0)
  File "Comm.pyx", line 454, in mpi4py.MPI.Comm.Scatterv (src/mpi4py.MPI.c:67458)
  File "Comm.pyx", line 454, in mpi4py.MPI.Comm.Scatterv (src/mpi4py.MPI.c:67458)
mpi4py.MPImpi4py.MPI.Exception: .Exception: MPI_ERR_TRUNCATE: message truncated
MPI_ERR_TRUNCATE: message truncated
Traceback (most recent call last):
  File "main.py", line 41, in <module>
    comm.Scatterv(sendbuf, local, root = 0)
  File "Comm.pyx", line 454, in mpi4py.MPI.Comm.Scatterv (src/mpi4py.MPI.c:67458)
mpi4py.MPI.Exception: MPI_ERR_TRUNCATE: message truncated

我对 Create_subarray 有错误的想法还是还有什么问题?

4

1 回答 1

0

问题在于您的“位移”。您已经正确地描述了具有“大小”、“子大小”和“开始”的子数组类型,但是当您替换此类型时,您已经将类型描述从实际二维数组的末尾移开。

替换其他类型通常不是问题,但是对于子数组,你有这个“原始数组”的概念会带来问题。

我快速替换了 VECTOR,但仍然出现内存损坏。

于 2015-04-23T16:47:57.407 回答