IsalanOnkar
IsalanOnkar

Reputation: 1

What is the difference between MPI broadcast, and just passing the broadcast data as input?

I'm trying to get a deeper understanding of how the comm.bcast method works in mpi4py. From what I understand it broadcasts the data to all of the processes.

However, upon accidental experimentation I discovered that if I pass the data as input into the method it still does the broadcasting, and doesn't change.

For example, the code below will execute as expected, regardless of whether I call comm.bcast or not:


input_data = [['a','f','d'], ['b','c', 'd']] 
def process_chunk(include_list, chunk): 
    print('i am processing chunk ', chunk) 
    return [x for x in chunk if x in include_list] 

def process_data(input_data): 
    from mpi4py import MPI 
    comm = MPI.COMM_WORLD 
    size = comm.Get_size() 
    rank = comm.Get_rank() 

    data = None 
    if rank == 0: 
        data = input_data 
        print('we will be scattering:', data)   

    include_list = ['a','d','f'] 
    ### if I comment out the next line, nothing changes 
    #include_list = comm.bcast(include_list, root=0) 
    data = comm.scatter(data, root=0) 
    data = process_chunk(include_list, data) 
    newData = comm.gather(data, root=0)   

    if rank == 0: 
        print('master:',newData)   
    return   

process_data(input_data) 

I run the above code using command mpirun -n 2 python3 process_chunk.py (where process_chunk.py is a file with the above code), and I get the following output:

we will be scattering: [['a', 'f', 'd'], ['b', 'c', 'd']]
i am processing chunk  ['a', 'f', 'd']
i am processing chunk  ['b', 'c', 'd']
master: [['a', 'f', 'd'], ['d']]

My question is: If I can just pass the necessary information (in this case include_list) as input to a particular method, and all ranks seem to receive it, then what is the use of comm.bcast?

Upvotes: 0

Views: 63

Answers (1)

Naveen Jingade
Naveen Jingade

Reputation: 1

If you are doing any operation on the root processors, like read operations, and you want to broadcast to all the processors, then it is handy. Now a days, with MPI-2 version, you can have parallel read and write operations, in such cases comm.bcast becomes obsolete. They got it new version for backward compatibility.

Upvotes: 0

Related Questions