How to scattering a numpy array in python using comm.Scatterv

断了今生、忘了曾经 提交于 2019-12-18 09:38:48

问题


I am tring to write a MPI-based code to do some calculation using python and MPI4py. However, following the example, I CANNOT scatter a numpy vector into cores. Here is the code and errors, is there anyone can help me? Thanks.

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)

And the error result is:

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]

Thanks.


回答1:


The data type is not correct. I should specify the type of the array:

d1 = np.arange(1, n+1, dtype='float64')


来源:https://stackoverflow.com/questions/39329492/how-to-scattering-a-numpy-array-in-python-using-comm-scatterv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!