2017-07-05 39 views
0

我想测试一个简单的MPI代码,在蟒蛇用下面的代码:comm.bcast工作不正常

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,但这正是它在文档中的使用方式。

回答

1

您在if块中定义了dataif区块为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 

这现在应该工作。

+0

是不是comm.bcast应该发送数据,指数,indptr到所有特效? –

+0

你的代码有效,但它没有做我想要的,打印应该至少在if块之外,以检查是否所有procs都收到了数据,索引,indptr。一般来说,comm.bcast不需要在if块中,因为它应该从rank = 0广播到所有可用的procs –

+0

您可以将数据,索引,indptr外部(if块之上)定义为空列表。 –