We ran the example with a total number of processes equal to 9. So, in the comm communicator group, we have nine tasks that can communicate with each other:
comm=MPI.COMM_WORLD
Also, to identify a task or processes inside the group, we use their rank value:
rank = comm.rank
We have two sender processes and two receiver processes. The process of rank equal to 0 sends numerical data to the receiver process of rank equal to 4:
if rank==0: data= 10000000 destination_process = 4 comm.send(data,dest=destination_process)
Similarly, we must specify the receiver process of rank equal to 4. We also note that the comm.recv statement must contain, as an argument, the rank of the sender process:
if rank==4: data=comm.recv(source=0)
For the other sender and receiver processes (the process of rank equal to 1 and the process of rank equal to 8, respectively), the situation is the same, the only difference being the type of data.
In this case, for the sender process, we have a string that is to be sent:
if rank==1: destination_process = 8 data= "hello" comm.send(data,dest=destination_process)
For the receiver process of rank equal to 8, the rank of the sender process is pointed out:
if rank==8: data1=comm.recv(source=1)
The following diagram summarizes the point-to-point communication protocol in mpi4py:
As you can see, it describes a two-step process, consisting of sending some DATA from one task (sender) and another task (receiver) receiving this data. The sending task must specify the data to be sent and its destination (the receiver process), while the receiving task has to specify the source of the message to be received.
To run the script, we shall use 9 processes:
C:>mpiexec -n 9 python pointToPointCommunication.py
This is the output that you'll get after you run the script:
my rank is : 7
my rank is : 5
my rank is : 2
my rank is : 6
my rank is : 3
my rank is : 1
sending data hello :to process 8
my rank is : 0
sending data 10000000 to process 4
my rank is : 4
data received is = 10000000
my rank is : 8
data1 received is = hello