【发布时间】:2018-10-02 18:21:48
【问题描述】:
我正在编写一个简单的 python 脚本来测试 mpi4py。具体来说,我想从给定的处理器(比如rank 0)广播一个标量和一个数组,以便所有其他处理器在后续步骤中都可以访问广播的标量和数组的值。
这是我到目前为止所做的:
from __future__ import division
from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
nproc = comm.Get_size()
rank = comm.Get_rank()
if rank==0:
scal = 55.0
mat = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr = np.ones(5)
result = 2*arr
comm.bcast([ result , MPI.DOUBLE], root=0)
comm.bcast( scal, root=0)
comm.bcast([ mat , MPI.DOUBLE], root=0)
for proc in range(1, 3):
if (rank == proc):
print "Rank: ", rank, ". Array is: ", result
print "Rank: ", rank, ". Scalar is: ", scal
print "Rank: ", rank, ". Matrix is: ", mat
但是,我收到以下错误:
NameError: name 'mat' is not defined
print "Rank: ", rank, ". Matrix is: ", mat
另外,在我的输出(print "Rank: ", rank, ". Scalar is: ", scal 和 print "Rank: ", rank, ". Array is: ", arr)中,我看不到 scal 和 array 的值。我在这里想念什么?我将非常感谢任何帮助。
【问题讨论】:
-
顺便说一句,如果你打算用 Python 进行并行处理,你也可以查看 Dask:dask.pydata.org/en/latest 如果你正在执行令人尴尬的并行计算,那么它会是 MPI4Py 的一个很好的替代方案。
-
@jcgiret:谢谢,我会检查一下:)
标签: python mpi cluster-computing hpc mpi4py