0

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)
AmacOS
  • 185
  • 11
  • 2
    it seems `np.transpose(A)` took a shorcut and did not transpose the matrix (!) Instead, it hacked the "metadata" to tell python `matrix_columns` should be interpreted as a Fortran matrix (instead of a C matrix like `A`). This can be evidenced by `print(A.flags)` and `print(matrix_columns.flags)`. – Gilles Gouaillardet Oct 06 '22 at 10:55
  • 2
    see the answer with 10 votes at https://stackoverflow.com/questions/58279082/time-complexity-of-numpy-transpose – Gilles Gouaillardet Oct 06 '22 at 10:56

0 Answers0