Let's start our journey to the MPI library by examining the classic code of a program that prints the phrase Hello, world! on each process that is instantiated:
- Import the mpi4py library:
from mpi4py import MPI
In MPI, the processes involved in the execution of a parallel program are identified by a sequence of non-negative integers called ranks.
- 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()
- Once evaluated, rank is printed:
print ("hello world from process ", rank)