Member-only story

Introduction to Scatter Operation in MPI

Afzal Badshah, PhD
3 min readJun 24, 2024

--

In MPI (Message Passing Interface) programming, the scatter operation is a collective communication pattern used to distribute data from one process to multiple processes. It takes an array or list of data on the root process and divides it into smaller chunks, then scatters these chunks to all other processes in the communicator. Each process receives one chunk of the data, allowing for parallel processing on different subsets of the data.

Code:

from mpi4py import MPI  # Import MPI module from mpi4py library
comm = MPI.COMM_WORLD  # Initialize MPI communicator
size = comm.Get_size() # Get the size of the communicator (total number of processes)
rank = comm.Get_rank() # Get the rank of the current process
print("Total number of processes:", size) # Print the total number of processes
print("Rank of current process:", rank) # Print the rank of the current process
if rank == 0: # Check if the current process is the root process (rank 0)
data = [(i+1)**2 for i in range(size)] # Generate data to be…

--

--

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