How can I pass a the rank of a process as a tag to the mpi4py.MPI.COMM_WORLD.Send() function and correctly receive it with mpi4py.MPI.COMM_WORLD.Recv()?
I'm referring to the following code example for sending and receiving messages between two processes using Send and Recv functions
#passRandomDraw.py
import numpy
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
randNum = numpy.zeros(1)
if rank == 1:
randNum = numpy.random.random_sample(1)
print "Process", rank, "drew the number", randNum[0]
comm.Send(randNum, dest=0)
if rank == 0:
print "Process", rank, "before receiving has the number", randNum[0]
comm.Recv(randNum, source=1)
print "Process", rank, "received the number", randNum[0]
I want to pass the rank of the sending process as a tag so that the receiving process can identify it in case there are multiple senders. This is what I do
#passRandomDraw.py
import numpy
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
randNum = numpy.zeros(1)
rnk = -1 # EDIT
if rank == 1:
randNum = numpy.random.random_sample(1)
print "Process", rank, "drew the number", randNum[0]
comm.Send(randNum, dest=0, tag=rank) # EDIT
if rank == 0:
print "Process", rank, "before receiving has the number", randNum[0]
print "Sender rank:", rnk
comm.Recv(randNum, 1, rnk) # EDIT
print "Process", rank, "received the number", randNum[0]
print "Sender rank:", rnk # EDIT
I expect the value of rnk to be 1 for the receiving process (which has rank=0), but it is still -1.
Can someone tell me what I'm doing wrong here? Thanks!