Member-only story
MPI with Python: Calculating Squares of Array Elements Using Multiple Processors
In this lab tutorial, we will explore how to utilize multiple processors to compute the squares of elements in an array concurrently using the MPI (Message Passing Interface) library in Python, specifically using the mpi4py module. MPI is a widely-used standard for parallel computing in distributed memory systems. We’ll create a master-worker model where the master process distributes tasks to worker processes, each responsible for computing the square of a subset of the array elements. The detailed tutorial of MPI with a python can be visited here.
Code
from mpi4py import MPI
comm = MPI.COMM_WORLD # Initialize MPI communicator
rank = comm.Get_rank() # Get the rank of the current process (0 for master, 1+ for workers)
size = comm.Get_size() # Get the total number of processes# Define a simple task for each process (replace with your actual workload)
def calculate_square(number):
return number * numberif rank == 0: # Master process
data = [2, 4] # Sample data (modify with your data)
for i in range(1, size): # Send data to worker processes (skip rank 0 - master)
comm.send(data[i - 1], dest=i) # Receive results from workers
results = []
for i…