我有使用comm.Scatterv
和comm.Gatherv
以下MWE以在給定數量的核(size
)沿着什麼軸mpi4py Scatterv函數分割一個numpy數組?
import numpy as np
from mpi4py import MPI
import matplotlib.pyplot as plt
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if rank == 0:
test = np.random.rand(411,48,52,40) #Create array of random numbers
outputData = np.zeros(np.shape(test))
split = np.array_split(test,size,axis = 0) #Split input array by the number of available cores
split_sizes = []
for i in range(0,len(split),1):
split_sizes = np.append(split_sizes, len(split[i]))
displacements = np.insert(np.cumsum(split_sizes),0,0)[0:-1]
plt.imshow(test[0,0,:,:])
plt.show()
else:
#Create variables on other cores
split_sizes = None
displacements = None
split = None
test = None
outputData = None
#Broadcast variables to other cores
test = comm.bcast(test, root = 0)
split = comm.bcast(split, root=0)
split_sizes = comm.bcast(split_sizes, root = 0)
displacements = comm.bcast(displacements, root = 0)
output_chunk = np.zeros(np.shape(split[rank])) #Create array to receive subset of data on each core, where rank specifies the core
print("Rank %d with output_chunk shape %s" %(rank,output_chunk.shape))
comm.Scatterv([test,split_sizes, displacements,MPI.DOUBLE],output_chunk,root=0) #Scatter data from test across cores and receive in output_chunk
output = output_chunk
plt.imshow(output_chunk[0,0,:,:])
plt.show()
print("Output shape %s for rank %d" %(output.shape,rank))
comm.Barrier()
comm.Gatherv(output,[outputData,split_sizes,displacements,MPI.DOUBLE], root=0) #Gather output data together
if rank == 0:
print("Final data shape %s" %(outputData.shape,))
plt.imshow(outputData[0,0,:,:])
plt.show()
這在原則隨機數和四維陣列之前,應該把它跨越size
核心分配一個四維陣列重組。我期望Scatterv
根據向量split_sizes
和displacements
中的起始整數和位移沿着軸0(長度411)進行劃分。但是,在與Gatherv
(mpi4py.MPI.Exception: MPI_ERR_TRUNCATE: message truncated
)重新組合時出現錯誤,並且每個內核上的output_chunk圖表顯示大多數輸入數據已丟失,因此看起來沿着第一個軸沒有發生拆分。
我的問題是:爲什麼不沿着第一個軸發生分裂,我怎麼知道分裂沿哪個軸發生,並且是否可以改變/指定哪個軸出現這種情況?
'comm.Scatterv'可能對'numpy'陣列,形狀,尺寸或步幅一無所知。它最多可以將'test'作爲一塊內存。事實上,它可能只是獲取指向數組對象的指針,而不是其數據緩衝區。這段代碼是否適用於1d數組?或'test.flatten()'? – hpaulj