Let's first analyze the following Python code, which will introduce a typical deadlock problem. We have two processes—rank equal to 1 and rank equal to 5—that communicate with each other and both have the data sender and data receiver functionalities:
- Import the mpi4py library:
from mpi4py import MPI
- Define the communicator as comm and the rank parameter:
comm=MPI.COMM_WORLD rank = comm.rank print("my rank is %i" % (rank))
- The process of rank equal to 1 sends and receives data from the process of rank equal to 5:
if rank==1: data_send= "a" destination_process = 5 source_process = 5 data_received=comm.recv(source=source_process) comm.send(data_send,dest=destination_process) print ("sending data %s " %data_send + "to process %d" %destination_process) print ("data received is = %s" %data_received)
- In the same way, here, we define the process of rank equal to 5:
if rank==5: data_send= "b"
- The destination and sender processes are equal to 1:
destination_process = 1 source_process = 1 comm.send(data_send,dest=destination_process) data_received=comm.recv(source=source_process) print ("sending data %s :" %data_send + "to process %d" %destination_process) print ("data received is = %s" %data_received)