
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 MPIThis 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 CThis 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().
6 thoughts on “Matrix Multiplication on Multi-Processors: MPI4PY”
어제 친구들과 회식 자리로강남가라오케추천다녀왔는데, 분위기도 좋고 시설도 깨끗해서 추천할 만했어요.
요즘 회식 장소 찾는 분들 많던데, 저는 지난주에강남가라오케추천코스로 엘리트 가라오케 다녀와봤습니다.
분위기 있는 술자리 찾을 땐 역시강남하퍼추천확인하고 예약하면 실패가 없더라고요.
회사 동료들이랑강남엘리트가라오케방문했는데, VIP룸 덕분에 프라이빗하게 즐길 수 있었어요.
신논현역 근처에서 찾다가강남룸살롱를 예약했는데, 접근성이 좋아서 만족했습니다.
술자리도 좋지만 요즘은강남셔츠룸가라오케이라고 불릴 만큼 서비스가 좋은 곳이 많더군요.