MPI Random Broadcasting

158 views Asked by At

I am working on random broadcasting between MPI processes. Basically, I want to have a sort of randomized decentralized communication. I like broadcasting as opposed to send and receive style communication for the supposed speed increase, but I get a slight "bug", or feature depending on your definition.

When I run the below,

from mpi4py import MPI
import random
import numpy

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

def stoch_action(n):
    assert n >= 1
    sample = numpy.array(range(n))
    if rank == 0:
        sample = numpy.array(random.sample(range(size), n))
        print "0 Master Rank", rank, "took sample", sample

    comm.Bcast([sample, MPI.INT], root=0)
    print "1 Rank", rank, "got sample", sample

    if rank in sample:
        print "2 Rank", rank, "recognizes duty"
        stoch_out = numpy.array([rank, rank, rank])
        stoch_root = rank
    else:
        stoch_out = numpy.array([0, 0, 0])
        stoch_root = random.sample(sample, 1)[0]
        print "2 Rank", rank, "without duty", stoch_root
    comm.Bcast([stoch_out, MPI.INT],
               root=stoch_root)
    print "3 Rank", rank, "recieved", stoch_out, "from", stoch_root


stoch_action(3)

I get the undesired output,

$ mpiexec -n 8 python stoch.py | sort -n
0 Master Rank 0 took sample [0 7 4]
1 Rank 0 got sample [0 7 4]
1 Rank 1 got sample [0 7 4]
1 Rank 2 got sample [0 7 4]
1 Rank 3 got sample [0 7 4]
1 Rank 4 got sample [0 7 4]
1 Rank 5 got sample [0 7 4]
1 Rank 6 got sample [0 7 4]
1 Rank 7 got sample [0 7 4]
2 Rank 0 recognizes duty
2 Rank 1 without duty 7
2 Rank 2 without duty 7
2 Rank 3 without duty 4
2 Rank 4 recognizes duty
2 Rank 5 without duty 0
2 Rank 6 without duty 7
2 Rank 7 recognizes duty
3 Rank 0 recieved [0 0 0] from 0
3 Rank 1 recieved [7 7 7] from 7
3 Rank 2 recieved [0 0 0] from 7
3 Rank 3 recieved [7 7 7] from 4
3 Rank 4 recieved [4 4 4] from 4
3 Rank 5 recieved [7 7 7] from 0
3 Rank 6 recieved [0 0 0] from 7
3 Rank 7 recieved [7 7 7] from 7

Notice that rank 3 process received [7 7 7] from 4. This shouldn't happen as rank 4 should be broadcasting [4 4 4]. You can see this in other places as well.

I am using OpenMPI (OpenRTE) 1.6.5 and Mpi4Py 1.3.1. On Ubuntu 15.04.

Is there any way to fix it? Ideally, in this example, if a vector is received from a process, the array should be filled with the rank of the process.

0

There are 0 answers