MPI随机播放

时间:2015-06-22 01:51:35

标签: mpi distributed openmpi mpi4py

我正致力于MPI流程之间的随机广播。基本上,我希望有一种随机分散的通信。我喜欢广播而不是发送和接收样式通信以实现所谓的速度提升,但是根据您的定义,我会得到一个轻微的“错误”或功能。

当我在下面运行时,

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)

我得到了不受欢迎的输出,

$ 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

请注意,等级3进程从4收到[7 7 7]。这不应该发生,因为等级4应该是广播[4 4 4]。你也可以在其他地方看到这个。

我正在使用OpenMPI(OpenRTE)1.6.5和Mpi4Py 1.3.1。在Ubuntu 15.04上。

有什么办法可以解决吗?理想情况下,在此示例中,如果从进程接收到向量,则应使用进程的等级填充数组。

0 个答案:

没有答案