2.2. Conductor-Worker Program Structure

Program file: 01conductorWorker.py

Example usage:

python run.py ./01conductorWorker.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.

Exercises:

2.2.1. Explore 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 conductor, {} of {} on {}"\
        .format(id, numProcesses, myHostName))
    else:
        print("Greetings from a worker, {} of {} on {}"\
        .format(id, numProcesses, myHostName))

########## Run the main function
main()

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 conductor, has the responsibility of handing out work and keeping track of results. We will see this in later examples.

Note

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

You have attempted of activities on this page