我正在研究 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 上。
有什么办法可以解决吗?理想情况下,在此示例中,如果从进程接收到向量,则数组应填充进程的等级。