How to do it...

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:

  1. Import the mpi4py library:
from mpi4py import MPI 
  1. Define the communicator as comm and the rank parameter:
comm=MPI.COMM_WORLD 
rank = comm.rank 
print("my rank is %i" % (rank)) 
  1. 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) 
  1. In the same way, here, we define the process of rank equal to 5:
if rank==5: 
    data_send= "b" 
  1. 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) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset