Member-only story

Matrix Multiplication on Multiple Processors: MPI4PY

Afzal Badshah, PhD
3 min readMay 9, 2024

--

When dealing with large-scale matrix multiplication, distributing the workload among multiple processors can significantly speed up the computation. This is particularly beneficial when the matrices are large and the computation involves a substantial number of operations. Visit the detailed tutorial on MPI in Python here.

In this scenario, each processor handles a portion of the matrices, performing computations independently, and then the results are combined to obtain the final result. This parallelization technique leverages the capabilities of multiple processors to expedite the overall computation time.

Code:

from mpi4py import MPI
import numpy as np
# Function to perform matrix multiplication
def matrix_multiply(A, B):
C = np.zeros((A.shape[0], B.shape[1]))
for i in range(A.shape[0]):
for j in range(B.shape[1]):
for k in range(A.shape[1]):
C[i][j] += A[i][k] * B[k][j]
return C
# Initialize MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
# Master process
if rank == 0:
# Generate matrices A and B
A = np.random.rand(2, 2)
B = np.random.rand(2, 2)

# Split matrices for distribution
chunk_size = A.shape[0] // size
A_chunks =…

--

--

Afzal Badshah, PhD
Afzal Badshah, PhD

Written by Afzal Badshah, PhD

Dr Afzal Badshah focuses on academic skills, pedagogy (teaching skills) and life skills.

No responses yet