我想写一个基于MPI的代码来使用python和MPI4py进行一些计算。但是,按照这个例子,我不能将numpy向量分散到核心中。这是代码和错误,有没有人可以帮助我?谢谢。
import numpy as np
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
n = 6
if rank == 0:
d1 = np.arange(1, n+1)
split = np.array_split(d1, size)
split_size = [len(split[i]) for i in range(len(split))]
split_disp = np.insert(np.cumsum(split_size), 0, 0)[0:-1]
else:
#Create variables on other cores
d1 = None
split = None
split_size = None
split_disp = None
split_size = comm.bcast(split_size, root = 0)
split_disp = comm.bcast(split_disp, root = 0)
d1_local = np.zeros(split_size[rank])
comm.Scatterv([d1, split_size, split_disp, MPI.DOUBLE], d1_local, root=0)
print('rank ', rank, ': ', d1_local)
错误结果是:
rank 2 : [ 2.47032823e-323]
rank 3 : [ 2.96439388e-323]
rank 0 : [ 4.94065646e-324 9.88131292e-324]
rank 1 : [ 1.48219694e-323 1.97626258e-323]
感谢。
答案 0 :(得分:1)
数据类型不正确。我应该指定数组的类型:
d1 = np.arange(1, n+1, dtype='float64')