【问题标题】:comm.bcast not working properlycomm.bcast 无法正常工作
【发布时间】:2017-12-09 17:42:13
【问题描述】:

我正在尝试使用以下代码在 python 上测试一个简单的 mpi 代码:

from scipy.sparse import csr_matrix
from mpi4py import MPI

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

if rank == 0:
    data = [1, 2, 3, 4, 5]
    indices = [1, 3, 2, 1, 0]
    indptr = [0, 2, 3, 4, 5]
    #A=csr_matrix((data,indices,indptr),shape=(4,4))                                                                                                              

data=comm.bcast(data, root=0)
indices=comm.bcast(indices, root=0)
indptr=comm.bcast(indptr, root=0)

print rank,data,indices,indptr

返回以下错误:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    data=comm.bcast(data, root=0)
NameError: name 'data' is not defined
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    data=comm.bcast(data, root=0)
NameError: name 'data' is not defined
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    data=comm.bcast(data, root=0)
NameError: name 'data' is not defined
0 [1, 2, 3, 4, 5] [1, 3, 2, 1, 0] [0, 2, 3, 4, 5]
-------------------------------------------------------
Primary job  terminated normally, but 1 process returned
a non-zero exit code.. Per user-direction, the job has been aborted.
-------------------------------------------------------
--------------------------------------------------------------------------
mpirun detected that one or more processes exited with non-zero status, thus causing
the job to be terminated. The first process to do so was:

  Process name: [[10263,1],1]
  Exit code:    1

错误似乎是由于我没有正确使用 comm.bcast,但这正是它在文档中的使用方式。

【问题讨论】:

    标签: python python-2.7 mpi mpi4py


    【解决方案1】:

    您在if 块中定义data。当if 块是false 时会发生什么?变量data 未定义。

    from scipy.sparse import csr_matrix
    from mpi4py import MPI
    
    comm=MPI.COMM_WORLD
    rank=comm.Get_rank()
    size=comm.Get_size()
    
    data = []
    indices = []
    indptr = []
    
    if rank == 0:
        data = [1, 2, 3, 4, 5]
        indices = [1, 3, 2, 1, 0]
        indptr = [0, 2, 3, 4, 5]
        #A=csr_matrix((data,indices,indptr),shape=(4,4))                                                                                                              
    
    data=comm.bcast(data, root=0)
    indices=comm.bcast(indices, root=0)
    indptr=comm.bcast(indptr, root=0)
    
    print rank,data,indices,indptr
    

    现在应该可以了。

    【讨论】:

    • 不是 comm.bcast 应该向所有 procs 发送数据、索引、indptr 吗?
    • 你的代码可以工作,但它没有做我想要的,打印应该至少在 if 块之外,以检查所有 procs 是否收到了数据、索引、indptr。通常 comm.bcast 不需要在 if 块中,因为它应该从 rank = 0 广播到所有可用的过程
    • 您可以将外部(if 块上方)的数据、索引、indptr 定义为空列表。
    • 行得通!!!没有实际将 comm.bcast 和 print 放入 if 块中。似乎它们应该被初始化....
    • 不是很pythonic ..你能编辑你的答案让我接受吗?
    猜你喜欢
    • 2016-12-01
    • 1970-01-01
    • 2016-09-01
    • 2012-07-11
    • 2018-04-08
    • 2017-04-20
    • 2018-10-02
    • 2016-09-04
    • 2010-10-06
    相关资源
    最近更新 更多