Reputation: 10093
If I want to list each communicator's id of a variable, how could I do that? Below is an attempt to demonstrate this idea:
from mpi4py import MPI
comm = MPI.COMM_WORLD
obj = "I am an example. My ID is unique to each communicator."
mpi_id = 'rank %i has id %s'%(comm.rank, str(id(obj)))
comm.send(mpi_id, tag=11, dtest=comm.rank)
mpi_id_list = []
for i in range(comm.size):
mpi_id_list.append( comm.recv(source=i, tag=11))
print mpi_id_list
Upvotes: 0
Views: 1243
Reputation: 9817
In MPI, each comm.send(...,dest=x)
should be matched by a comm.recv(...)
executed by the process of rank x
. All messages can be sent to the process of rank 0 and the process 0 must receive all these messages. This operation is a collective operation called a reduction.
The following code can be executed on 4 processes by typing mpirun -np 4 main.py
from mpi4py import MPI
comm = MPI.COMM_WORLD
obj = "I am an example. My ID is unique to each communicator."
mpi_id = 'rank %i has id %s'%(comm.rank, str(id(obj)))
comm.send(mpi_id, tag=11, dest=0)
mpi_id_list = []
if comm.rank==0:
mpi_id_list = []
for i in range(comm.size):
mpi_id_list.append( comm.recv(source=i, tag=11))
print mpi_id_list
#broadcasting the list
mpi_id_list = comm.bcast(mpi_id_list, root=0)
#now, the list is the same on all processes.
print "rank "+str(comm.rank)+" has list "+str(mpi_id_list)
Notice that this example makes use of the collective operation comm.bcast()
to broadcast the resulting list to all processes. See https://mpi4py.scipy.org/docs/usrman/tutorial.html for mpi4py examples of different collective operations. For instance, you be tempted by the comm.allreduce()
operation:
list=comm.allreduce([mpi_id])
print list
Upvotes: 2