The MPI function
MPI_Comm_rank can be used to obtain the identification number of a member of an MPI team of processes. This identification number, known as a rank, runs from
N is the number of processes in the team. Typically, we tend to think of the process with rank
0 as being the master process, while processes with ranks
N-1 are the child or worker processes.
While each MPI process in the team starts with the same main function, it is possible to use the rank of a process to get each member of the MPI team to run different sections of code. Copy the code from the appropriate link below to create the executable
Try running the executable
mpi_sections using different numbers of processes (via
mpirun -np X).
In this example, there are three functions,
long_loop. These three functions are called from an
if block in the main function. The decision of which function to run is based on the rank of the MPI process. If the processes rank is
times_table is called. If the rank is
countdown is called. If the rank is
long_loop is called. If the rank is greater than
2, then the MPI process does nothing, and is ignored.