How to scattering a numpy array in python using co

2019-01-29 11:56发布

问题:

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')