MPI - Python Lab
MPI - Python Lab
Basic definitions
What is Multithreading?
• A single core(processor) in your computer can mimic multi(N)processor, This will allow you to run
multiple(N) programs concurrently(not exactly!).
• Multiple processor can share same memory space. So that a single variable can be updated or used by
multiple processor at the same time.
What is Multiprocessing?
• A single core(processor) is always single. So, for running program concurrently(exactly), you need to
use multiple physical cores(processors)
• Multiple processor doesn't share memory. So, we need to make them talk among themselves.
High Performance Computing(HPC)
• Uses both of this concept efficiently
Topics covered
• Using the mpi4py Python module
• Implementing point-to-point communication
• Avoiding deadlock problems
• Collective communication using a broadcast
• Collective communication using the scatter function
• Collective communication using the gather function
• Collective communication using Alltoall
• The reduction operation
• Optimizing communication
Installation Procedure
• The installation procedure of mpi4py on a Windows machine is as
follows:
• C:>pip install mpi4py
• Anaconda users must type the following:
• C:>conda install mpi4py
• This implies that the notation used to run the mpi4py examples
is as follows:
• C:>mpiexec -n x python mpi4py_script_name.py
• The mpiexec command is the typical way to start parallel jobs: x
is the total number of processes to use, while
mpi4py_script_name.py is the name of the script to be
executed.
Strengths of MPI
• Standardization: It is supported by all High-Performance
Computing (HPC) platforms.
• Portability: The changes applied to the source code are
minimal, which is useful if you decide to use the application
on a different platform that also supports the same standard.
• Performance: Manufacturers can create implementations
optimized for a specific type of hardware and get better
performance.
• Functionality: Over 440 routines are defined in MPI-3, but
many parallel programs can be written using fewer than
even 10 routines.
MPI basic functions
• Import the mpi4py library:
from mpi4py import MPI
• If we have a number (p of processes) that runs a program, then the
processes will have a rank that goes from 0 to p-1. In particular, in
order to assess the rank of each process, we must use the
COMM_WORLD MPI function in particular. This function is called a
communicator, as it defines its own set of all processes that can
communicate together:
comm = MPI.COMM_WORLD
• Finally, the following Get_rank() function returns rank of the
process calling it:
rank = comm.Get_rank()
SPMD model
• MPI belongs to the Single Program Multiple Data (SPMD) programming technique.
• SPMD is a programming technique in which all processes execute the same program, each
processes at the same time, but each process can operate on different data.
• At the same time, the processes can execute both the same instruction and different
instructions.
Modules of MPI
• Point-to-point communication
• Collective communication
• Topologies
Point-to-Point Communication
The mpi4py Python module enables point-to-point communication via two functions:
The Comm parameter, which is short for communicator, defines the group of
processes that
comm=MPI.COMM_WORLD destination_process = 8
data= "hello"
rank = comm.Get_rank()
print("my rank is : " , rank) comm.send(data,dest=destination_process)
if rank==0: print ("sending data %s :" %data + "to
data= 10000000 process %d"
%destination_process)
destination_process = 4 if rank==4:
data=comm.recv(source=0)
comm.send(data,dest=destination_proc
print ("data received is = %s" %data)
ess)
if rank==8:
print ("sending data %s " %data
+ "to data1=comm.recv(source=1)
print ("data1 received is = %s"
process %d" %data1)
%destination_process)
mpiexec -n 9 python
pointToPointCommunication.py
Example 3 - Output
This is the output that you'll get after you run
the script:
my rank is : 7
my rank is : 5
my rank is : 2
my rank is : 6
my rank is : 3
my rank is : 1
sending data hello :to process 8
my rank is : 0
sending data 10000000 to process 4
my rank is : 4
data received is = 10000000
my rank is : 8
data1 received is = hello
Example 4
Blocking Functions
The comm.send() and comm.recv() functions are blocking functions,
which means that they block the caller until the buffered data involved
can be used safely.