2.2. Master-Worker Program Structure

Program file: 01masterWorker.py

Example usage:

python run.py ./01masterWorker.py 4

Here the 4 signifies the number of processes to start up in mpi.

run.py executes this program within mpirun using the number of processes given.


2.2.1. Dive into the code

What is different between this example and the previous one?

from mpi4py import MPI

def main():
    comm = MPI.COMM_WORLD
    id = comm.Get_rank()            #number of the process running the code
    numProcesses = comm.Get_size()  #total number of processes running
    myHostName = MPI.Get_processor_name()  #machine name running the code

    if id == 0:
        print("Greetings from the master, {} of {} on {}"\
        .format(id, numProcesses, myHostName))
        print("Greetings from a worker, {} of {} on {}"\
        .format(id, numProcesses, myHostName))

########## Run the main function

The answer to the above question illustrates what we can do with this pattern: based on the process id, we can have one process carry out something different than the others. This concept is used a lot as a means to coordinate activities, where one process, often called the master, has the responsibility of handing out work and keeping track of results. We will see this in later examples.


By convention, the master coordinating process is usually the process number 0.

You have attempted of activities on this page
Next Section - 3. Decomposition using parallel for loop patterns