Python:使用 mpi4py 通过 spawn 将数组广播到其他脚本

标签 python numpy parallel-processing mpi spawn

我正在尝试编写两个脚本,一个是主脚本,一个是工作脚本,其中主脚本将生成多个工作进程,然后将一个 numpy 数组广播到生成的工作进程。从在线查看 mpi4py 的(模糊)教程的数量来看,我觉得我理解了这个概念,但是我编写的任何测试代码都不会成功地将数组发送给工作人员。我没有收到错误,但他们从未收到数组。

谁能给我一个清楚的例子,说明如何使用 mpi4py、spawn 和 bcast 将数组广播到多个 spawned worker 脚本?谢谢!

更新:示例:

主脚本:

#! /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()

工作脚本:

#! /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()

最佳答案

您需要将主脚本中的 Bcast 更改为:

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

如果设置为0,master会认为root在remote/worker组中排名0,程序会挂起。

关于Python:使用 mpi4py 通过 spawn 将数组广播到其他脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20643410/

相关文章:

python - python 中的 3D 卷积

c# - 运行 Fixtures 但不是并行 NUnit 中的方法

parallel-processing - 在 Perl 6 中需要简单的并行示例

python - 在 python/pandas 中部分基于自身计算列

python - ASCII 文本可执行文件,带有 CRLF 行终止符

python - 缩放复杂 numpy 数组的实部

r - 是否有可能并行地做一个共同的情节(与 foreach)?

Python Cassandra 驱动程序仅插入一条记录

python - 使用 ffmpeg 和 python 2.7 在 Fedora 26 25 24 23 或 22 上安装 OpenCV 2.4.13 或 3.x 时出现 VideoCapture 不工作错误

python - 为什么 `arr.take(idx)` 比 `arr[idx]` 快