# Matrix Multiplication on Multi-Processors: MPI4PY

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 = [A[i:i+chunk_size] for i in range(0, A.shape[0], chunk_size)]
# Send parts of A and B to worker processes
for i in range(1, size):
comm.send(A_chunks[i-1], dest=i, tag=1)
comm.send(B, dest=i, tag=2)
# Calculate its own part of multiplication
C_partial = matrix_multiply(A_chunks[0], B)
# Collect results from worker processes
for i in range(1, size):
C_partial += comm.recv(source=i, tag=3)
# Print the resulting matrix
print("Resulting matrix C:")
print(C_partial)
# Worker processes
else:
# Receive matrix chunks from master
A_chunk = comm.recv(source=0, tag=1)
B = comm.recv(source=0, tag=2)
# Perform multiplication
C_partial = matrix_multiply(A_chunk, B)
# Send back the result to master
comm.send(C_partial, dest=0, tag=3)
```

**Explanation**

**Import MPI Module and Initialize MPI Environment**

`from mpi4py import MPI`

This line imports the MPI module from the mpi4py package, enabling the use of MPI functionalities.

```
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
```

These lines initialize the MPI environment. `MPI.COMM_WORLD`

creates a communicator object representing all processes in the MPI world. `comm.Get_rank()`

returns the rank of the current process in the communicator, and `comm.Get_size()`

returns the total number of processes in the communicator.

**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
```

This function `matrix_multiply`

takes two matrices `A`

and `B`

as input and returns their multiplication `C`

. It initializes an empty matrix `C`

with dimensions derived from the multiplication of matrices `A`

and `B`

. Then, it performs matrix multiplication using nested loops to iterate through rows and columns of matrices `A`

and `B`

, computing each element of matrix `C`

.

**Master Process**

```
if rank == 0:
A = np.random.rand(2, 2)
B = np.random.rand(2, 2)
```

In the master process (rank 0), random matrices `A`

and `B`

of size 2×2 are generated.

```
chunk_size = A.shape[0] // size
A_chunks = [A[i:i+chunk_size] for i in range(0, A.shape[0], chunk_size)]
```

The matrices `A`

is split into chunks based on the total number of processes (`size`

). Each chunk is of size `chunk_size`

, and the list `A_chunks`

contains these chunks.

```
for i in range(1, size):
comm.send(A_chunks[i-1], dest=i, tag=1)
comm.send(B, dest=i, tag=2)
```

Parts of matrices `A`

and `B`

are sent to worker processes using `comm.send()`

. Each chunk of `A`

along with the entire matrix `B`

is sent to a different worker process.

`C_partial = matrix_multiply(A_chunks[0], B)`

The master process calculates its own partial result of matrix multiplication using the first chunk of matrix `A`

.

```
for i in range(1, size):
C_partial += comm.recv(source=i, tag=3)
```

The master process receives partial results from each worker process using `comm.recv()`

, aggregates them, and stores the final result in `C_partial`

.

```
print("Resulting matrix C:")
print(C_partial)
```

Finally, the resulting matrix `C`

is printed.

**Worker Processes**

```
else:
A_chunk = comm.recv(source=0, tag=1)
B = comm.recv(source=0, tag=2)
```

In the worker processes (rank != 0), chunks of matrix `A`

and entire matrix `B`

are received from the master process using `comm.recv()`

.

`C_partial = matrix_multiply(A_chunk, B)`

Each worker process performs matrix multiplication using its received chunk of matrix `A`

and matrix `B`

.

`comm.send(C_partial, dest=0, tag=3)`

The resulting partial matrix `C_partial`

is sent back to the master process using `comm.send()`

.

## Material

Download the programs (code), covering the MPI4Py.

## 36 thoughts on “Matrix Multiplication on Multi-Processors: MPI4PY”

п»їbest mexican online pharmacies: mexico pharmacy – mexican border pharmacies shipping to usa

buying from online mexican pharmacy

http://cmqpharma.com/# mexican border pharmacies shipping to usa

mexican mail order pharmacies

indianpharmacy com [url=http://indiapharmast.com/#]reputable indian pharmacies[/url] reputable indian online pharmacy

https://foruspharma.com/# buying prescription drugs in mexico online

canada pharmacy reviews: best online canadian pharmacy – canadian pharmacy service

legitimate canadian online pharmacies: canadian pharmacies online – recommended canadian pharmacies

indian pharmacy paypal: indian pharmacy – Online medicine order

indianpharmacy com [url=https://indiapharmast.com/#]top 10 pharmacies in india[/url] world pharmacy india

world pharmacy india: reputable indian pharmacies – buy prescription drugs from india

http://canadapharmast.com/# onlinecanadianpharmacy

ed drugs online from canada: canadian pharmacy price checker – canadian neighbor pharmacy

top 10 online pharmacy in india [url=http://indiapharmast.com/#]top 10 online pharmacy in india[/url] indian pharmacy paypal

best online pharmacies in mexico: best online pharmacies in mexico – reputable mexican pharmacies online

medication from mexico pharmacy: purple pharmacy mexico price list – mexico pharmacy

http://foruspharma.com/# mexican drugstore online

indian pharmacies safe: india pharmacy – pharmacy website india

www canadianonlinepharmacy [url=https://canadapharmast.online/#]online canadian pharmacy[/url] canadian pharmacy com

indian pharmacy online: top 10 pharmacies in india – indian pharmacy

pharmacy website india: online pharmacy india – buy medicines online in india

https://amoxildelivery.pro/# where to get amoxicillin over the counter

amoxicillin over counter [url=https://amoxildelivery.pro/#]cost of amoxicillin[/url] buying amoxicillin in mexico

https://amoxildelivery.pro/# order amoxicillin online no prescription

http://paxloviddelivery.pro/# Paxlovid buy online

https://paxloviddelivery.pro/# paxlovid pill

amoxicillin pharmacy price [url=https://amoxildelivery.pro/#]how much is amoxicillin[/url] amoxicillin buy canada

https://clomiddelivery.pro/# how to buy generic clomid online

https://amoxildelivery.pro/# purchase amoxicillin online without prescription

doxycycline costs uk [url=https://doxycyclinedelivery.pro/#]doxycycline tablets cost[/url] generic doxycycline 200 mg

http://clomiddelivery.pro/# can you buy cheap clomid prices

https://ciprodelivery.pro/# cipro for sale

cheap doxycycline online [url=http://doxycyclinedelivery.pro/#]doxycycline canada price[/url] cheap doxycycline tablets

http://ciprodelivery.pro/# cipro for sale

https://clomiddelivery.pro/# how to buy clomid without rx

how to get clomid without a prescription [url=https://clomiddelivery.pro/#]where buy generic clomid no prescription[/url] where can i buy cheap clomid without rx

http://doxycyclinedelivery.pro/# doxycycline online no prescription

https://amoxildelivery.pro/# how much is amoxicillin

https://amoxildelivery.pro/# amoxicillin 500 capsule

doxycycline tablets online [url=https://doxycyclinedelivery.pro/#]doxycycline 50 mg cap[/url] buy doxycycline medicine

http://paxloviddelivery.pro/# paxlovid generic

http://paxloviddelivery.pro/# Paxlovid over the counter

where can i buy doxycycline capsules [url=http://doxycyclinedelivery.pro/#]doxycycline 40 mg coupon[/url] 100 doxycycline

https://paxloviddelivery.pro/# п»їpaxlovid

http://clomiddelivery.pro/# can i order cheap clomid prices

cheap amoxicillin 500mg [url=http://amoxildelivery.pro/#]amoxacillian without a percription[/url] order amoxicillin online