How to do it...

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:

  1. 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.
  1. 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 
  1. Finally, the following Get_rank() function returns rank of the process calling it:
rank = comm.Get_rank() 
  1. Once evaluated, rank is printed:
print ("hello world from process ", rank)  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset