
Programming Using the
Message-Passing
Paradigm II
Dr. Cem Özdo
˘
gan
LOGIK
MPI: the Message
Passing Interface
Starting and Terminating
the MPI Library
Communicators
Getting Information
Sending and Receiving
Messages
Avoiding Deadlocks
Sending and Receiving
Messages Simultaneously
6.18
Avoiding Deadlocks IV
•
Note that the deadlock still remains even when we have
only two processes.
•
Thus, when pairs of processes need to exchange data, the
above method leads to an unsafe program
.
•
The above example can be made safe, by rewriting:
1 i n t a [ 1 0 ] , b [ 1 0 ] , np , myra nk ;
2 MPI_Status s t a tus ;
3 . . .
4 MPI_Comm_size(MPI_COMM_WORLD, &np ) ;
5 MPI_Comm_rank (MPI_COMM_WORLD, &myrank ) ;
6 i f ( myrank%2 == 1) {
7 MPI_Send ( a , 10 , MPI_INT , ( myrank +1)%np , 1 ,MPI_COMM_WORLD) ;
8 MPI_Recv ( b , 10 , MPI_INT , ( myrank−1+np )%np , 1 , MPI_COMM_WORLD) ;
9 }
10 els e {
11 MPI_Recv ( b,1 0 , MPI_INT , ( myrank−1+np )%np ,1 ,MPI_COMM_WORLD) ;
12 MPI_Send ( a,10 , MPI_INT , ( myrank +1)%np , 1 ,MPI_COMM_WORLD) ;
13 }
14 . . .
•
This version partitions the processes into two groups.
•
One consists of the odd-numbered processes and the
other of the even-numbered processes.