Python: Using mpi4py to bcast an array to other scripts with spawn
Question: I'm trying to write two scripts, one a master and one a worker, where the
master script will spawn multiple processes of the worker and then bcast a
numpy array to the worker spawns. From looking at the number of (vague)
tutorials online for mpi4py, I feel like I understand this concept, but any
test code I've written won't successfully send the array to the workers. I
don't get errors, but they never receive the array.

Can someone please give me a clear example of how to use mpi4py, spawn, and
bcast to broadcast an array to a number of spawned worker scripts? Thank you!

UPDATE: Example:

Master script:

    
    
    #! /usr/bin/env python
    # master test
    
    import sys,time
    from mpi4py import MPI
    import numpy as np
    
    comm2    = MPI.COMM_WORLD
    rank     = comm2.Get_rank()
    mpisize  = comm2.Get_size()
    
    initcomm = MPI.COMM_SELF.Spawn(sys.executable, \
                               args=['test2.py'], \
                               maxprocs=5)
    
    # Initialise the programs
    test = np.array([1,2,3], 'i')
    print("About to broadcast {0} from rank {1}".format(test, rank))
    initcomm.Bcast([test, MPI.INT], root=0)
    initcomm.Disconnect()
    

Worker script:

    
    
    #! /usr/bin/env python
    # child test
    
    import sys,time
    from mpi4py import MPI
    import numpy as np
    
    comm2    = MPI.COMM_WORLD
    rank     = comm2.Get_rank()
    mpisize  = comm2.Get_size()
    
    initcomm = MPI.Comm.Get_parent()
    
    test = np.zeros(3, 'i')
    print("Bcast coming to rank {0}".format(rank))
    initcomm.Bcast([test, MPI.INT], root=0)
    print("Received {0}".format(test))
    initcomm.Disconnect()
    


Answer: You need to change Bcast in the master script to:

    
    
    initcomm.Bcast([test, MPI.INT], root=MPI.ROOT)
    

If it's set to `0`, the master will think that the root is rank `0` in the
remote/worker group, and the program will hang.

