I am trying to scatter matrix of size N x N column wise to different processes. Its expected that N % number_of_processes = 0. Input matrix:
A =
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]
]
When I run this with 2 processes, process P0 should receive columns 1-2: [[1,5,9,13], [2,6,10,14]], P1 should receive columns 3-4 [[3,7,11,15], [4,8,12,16]]. I transposed the matrix so each row of the new matrix is consists of columns, but when scattering still receiving rows in the order of original matrix.
from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
N = 4
#K = 10
if rank == 0:
A = np.random.random((N,N))/N*2
vector = np.random.random(N)
print("Rank: ", rank)
print("A: ", A)
print("Vector: ", vector)
else:
A=np.empty((N,N), dtype='float64')
vector = np.empty(N, dtype='float64')
# distributing vector to all processes
#comm.Bcast(vector, root = 0)
matrix_columns = np.empty((A.shape[0],A.shape[0]), dtype='float64')
# get columns from matrix
if rank == 0:
matrix_columns = np.transpose(A)
else:
matrix_columns = np.empty((A.shape[0],A.shape[0]), dtype='float64')
if rank == 0:
print("Columns >>>")
print(matrix_columns)
# distribute columns to all processes
received_columns = np.empty((matrix_columns.shape[0]//size,matrix_columns.shape[0]), dtype='float64')
comm.Scatter(matrix_columns, received_columns, root = 0)
print("My rank: ", rank, "received columns: ", received_columns)