user7938583
user7938583

Reputation:

comm.bcast not working properly

I am trying to test a simple mpi code on python with the following code :

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

which returns the following error:

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

It seems like the error is due to me not using comm.bcast properly, but that is exactly how its used in the docs.

Upvotes: 0

Views: 1158

Answers (1)

Salmaan P
Salmaan P

Reputation: 847

you are defining data in the if block. What happens when the if block is false? the variable data is not defined.

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

This should now work.

Upvotes: 1

Related Questions