Module 4: Parallel Programming: Shared Memory and Message Passing
  Lecture 7: Examples of Shared Memory and Message Passing Programming
 


Mutual Exclusion

  • Use LOCK/UNLOCK around critical sections
    • Updates to shared variable diff must be sequential
    • Heavily contended locks may degrade performance
    • Try to minimize the use of critical sections: they are sequential anyway and will limit speedup
    • This is the reason for using a local_diff instead of accessing gm->diff every time
    • Also, minimize the size of critical section because the longer you hold the lock, longer will be the waiting time for other processors at lock acquire

LOCK Optimization

LOCK (gm-> cost_lock );
if ( my_cost < gm->cost) {
gm->cost = my_cost ;
}
UNLOCK (gm-> cost_lock );
/* May lead to heavy lock
contention if everyone
tries to update at the

if ( my_cost < gm->cost) {
LOCK (gm-> cost_lock );
if ( my_cost < gm->cost) { /* make sure*/
gm->cost = my_cost ;
}
UNLOCK (gm-> cost_lock );
| } /* this works because gm->cost is
monotonically decreasing */
same time */