5.1. Broadcast from conductor to workers

We will look at three types of data that can be created in the conductor and sent to the workers. Rather than use send and receive, we will use a special new function called bcast.

Note

In each code example, note how the conductor does one thing, and the workers do another, but all of the processes execute the bcast function.

5.1.1. Broadcast a dictionary

Program file: 08broadcast.py

Example usage:

python run.py ./08broadcast.py N

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

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

Exercise:

  • Run, using N = from 1 through 8 processes.

5.1.1.1. Explore the code

Find the place in this code where the data is being broadcast to all of the processes. Match the prints to the output you observe when you run it.

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 numProcesses > 1 :

        if id == 0:        # conductor
            #conductor: generate a dictionary with arbitrary data in it
            data = {'one': 1, 'two': 2, 'three': 3}
            print("Conductor Process {} of {} on {} broadcasts {}"\
            .format(id, numProcesses, myHostName, data))

        else :
            # worker: start with empty data
            data = {}
            print("Worker Process {} of {} on {} starts with {}"\
            .format(id, numProcesses, myHostName, data))

        #initiate and complete the broadcast
        data = comm.bcast(data, root=0)
        #check the result
        print("Process {} of {} on {} has {} after the broadcast"\
        .format(id, numProcesses, myHostName, data))

    else :
        print("Please run this program with the number of processes \
greater than 1")

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

5.1.2. Broadcast user input

Program file: 09broadcastUserInput.py

Example usage:

python run.py ./09broadcastUserInput.py N dataString

Here the N signifies the number of processes to start up in mpi, which must be greater than one. The dataString must be supplied and represents the string that will be broadcast from the conductor process to the workers.

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

For example, in this special instance, you can send a string with spaces and other special characters in it like this:

python run.py ./09broadcastUserInput.py 2 “helloworld!”

Warning

This program is unlike any of the others and takes in a second argument, as shown above.

Exercise:

  • Run, using N = from 1 through 8 processes, with a string of your choosing.

5.1.2.1. Explore the code

Find the place in this code where the data is being broadcast to all of the processes. Match the prints to the output you observe when you run it.

from mpi4py import MPI
import sys

# Determine if user provided a string to be broadcast.
# If not, quit with a warning.
def checkInput(id):
    numArguments = len(sys.argv)
    if numArguments == 1:
        #no extra argument was given- conductor warns and all exit
        if id == 0:
            print("Please add a string to be broadcast from conductor to workers")
        sys.exit()

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 numProcesses > 1 :
        checkInput(id)

        if id == 0:        # conductor
            #conductor: get the command line argument
            data = sys.argv[1]
            print("Conductor Process {} of {} on {} broadcasts \"{}\""\
            .format(id, numProcesses, myHostName, data))

        else :
            # worker: start with empty data
            data = 'No data'
            print("Worker Process {} of {} on {} starts with \"{}\""\
            .format(id, numProcesses, myHostName, data))

        #initiate and complete the broadcast
        data = comm.bcast(data, root=0)
        #check the result
        print("Process {} of {} on {} has \"{}\" after the broadcast"\
        .format(id, numProcesses, myHostName, data))

    else :
        print("Please run this program with the number of processes \
greater than 1")

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

5.1.3. Broadcast a list

This just shows that other data structures can also be broadcast.

Program file: 11broadcastList.py

Example usage:

python run.py ./11broadcastList.py N

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

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

Exercise:

  • Run, using N = from 1 through 8 processes.

5.1.3.1. Explore the code

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 numProcesses > 1 :

        if id == 0:        # conductor
            #conductor: generate a dictionary with arbitrary data in it
            data = list(range(numProcesses))
            print("Conductor Process {} of {} on {} broadcasts {}"\
            .format(id, numProcesses, myHostName, data))

        else :
            # worker: start with empty data
            data = []
            print("Worker Process {} of {} on {} starts with {}"\
            .format(id, numProcesses, myHostName, data))

        #initiate and complete the broadcast
        data = comm.bcast(data, root=0)

        #check the result
        print("Process {} of {} on {} has {} after the broadcast"\
        .format(id, numProcesses, myHostName, data))

    else :
        print("Please run this program with the number of processes greater than 1")

########## Run the main function
main()
You have attempted of activities on this page