我正在制作一个简单的程序,我正在尝试使用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有错误的想法或者其他什么可能是错的?
答案 0 :(得分:0)
问题在于您的排名&#39;。您已经正确地描述了一种子阵列类型,其中包含&#39;尺寸&#39;,&#39; subsizes&#39;和&#39;启动&#39;但是当您取代此类型时,您可以使用&#39; ve将类型描述从实际的2d数组的末尾移开。
取代其他类型通常不是问题,但是对于子阵列,你有这个&#34;原始阵列&#34;这个概念会产生问题。
我为VECTOR做了快速替换,但我仍然有内存损坏。