MPI#

Learning Objectives#

By the end of this lesson, learners will be able to:

  • Understand the concept of parallel computing and the role of MPI in facilitating communication between processes.

  • Import and utilize the mpi4py library in Python for parallel programming.

  • Execute a simple “Hello World” program using MPI and observe the behavior of multiple processes.

  • Implement a communicator in MPI to manage communication between different processes.

  • Identify the rank of a process and the total number of processes running in parallel.

  • Run MPI-based Python scripts in both serial and parallel modes using mpirun.

Import MPI4Py#

In this example we will kick off a set of processes using mpirun/mpiexec and get them to report in. The first step that needs to be taken is to import mpi4py, as below:

# mpi_hello.py
from mpi4py import MPI

MPI Hello World#

Programmes that make use of MPI use a communicator, an object that groups together a set of processes (ranks) and controls communications between them. The communicator understands the context of the program across all the different processes, whereas the rest of the code is only aware of its own process.

Let’s run a very simple program, first in serial then again in parallel using mpirun.

If we add the following to our file:

print("Hello world!")

and execute the file with python, we get the expected outcome.

$ python mpi_hello.py
Hello world!

Parallel Execution#

If we try and run the program on multiple processes, we get a slightly different result:

$ mpirun -n 2 python mpi_hello.py
Hello world!
Hello world!

Using the communicator#

We can see that the program is being run multiple times, but in order to see evidence of multiple processes we need to change our code. If we change the print statement to:

comm = MPI.COMM_WORLD
print(f"Hello from process {comm.Get_rank()}")

and run the program again with mpirun, we can get more insight into what’s going on:

$ mpirun -n 2 python mpi_hello.py
Hello from process 0
Hello from process 1

We can also use the communicator to get more information about the overall state of the program, such as how many processes it is running on. This can be achieved by adding the line:

print(f"MPI world size is {comm.Get_size()}")

We can use the if statement, if comm.Get_rank() == 0: if we only want to perform a task once, non-concurrently, such as:

if comm.Get_rank() == 0:
    print(f"MPI world size is {comm.Get_size()}")

Complete File#

Download complete mpi_hello.py file