Lecture4

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: – I changed my ‘p’ dimensions at some point but ⇒  floor(n/p) = 322 x 322 x 1291 = 133,856,044 items forgot to update 0 ⇒  remainder = 3 x 3 x 1 extra items = 136,467,500 items my remainders. 136,361,875 The red text ⇒  one task gets ~2% more work/data fixes my 1.8% mistake. 1/31 –  e.g., var Data: [1..1291, 1..1291, 1..1291] real; –  n = 1291, p = 4 x 4 x 1 •  DistribuCng across a distributed memory machine where p is 18,688 or 299,008, the remainders and therefore imbalance could be even more significant… CSEP 524: Parallel ComputaCon Winter 2013: Chamberlain 10 Locks and RRWW Bugs… Where Were We? Recap: RRWW Bugs •  The following schedule is problemaCc: execuCng “totTime += myTime;” in parallel… !me Task 1 reg = read totTime reg = reg + myTime totTime = write reg Task 2 reg = read totTime reg = reg + myTime totTime = write reg 3.7 myTime 2.3 myTime 3.7 reg 2.3 reg 2.3 totTime CSEP 524: Parallel ComputaCon Winter 2013: Chamberlain 12 Fixing RRWW bugs with locks Pthreads pthread_mutex_t totTimeMutex; pthread_mutex_init(&totTimeMutex, NULL); create tasks … pthread_mutex_lock(&totTimeMutex); totTime += myTime; pthread_mutex_unlock(&totTimeMutex); … join tasks Chapel var totTime$: sync real = 0.0; coforall tid in 0..#numTasks { … totTime$ += myTime; … } pthread_mutex_destroy(&totTimeMutex); CSEP 524: Parallel ComputaCon Winter 2013: Chamberlain 13 PiYalls of Using Locks I Deadlock: Can occur when grabbing the same locks in different orders cobegin { { // task 1 const val = lock1$; lock2$ += 1; lock1$ = val + 1; } { // task 2 const val = lock2$; lock1$ += 1; lock2$ = val + 1; } } CSEP 524: Parallel ComputaCon Task 1 Task 2 val = lock1$.readFE(); val = lock2$.readFE() tmp = lock2$.readFE(); tmp = lock1$.readFE() (…blocks…) (…blocks…) (Zzzzz….) (Zzzzz….) Winter 2013: Chamberlain 14 PiYalls of Using Locks I Deadlock: Similarly trivial example in pthreads { } { // task 1 pthread_mutex_lock(&lock1); pthread_mutex_lock(&lock2); pthread_mutex_unlock(&lock2); pthread_mutex_unlock(&lock1); // task 2 pthread_mutex_lock(&lock2); pthread_mutex_lock(&lock1); pthread_mutex_unlock(&lock1); pthread_mutex_unlock(&lock2); } } CSEP 524: Parallel ComputaCon Winter 2013: Chamberlain 15 PiYalls of Using Locks II Livelock: –  Tasks are sCll execuCng… –  …but not making useful progress CSEP 524: Parallel ComputaCon Winter 2013: Chamberlain 16 Livelock Example var lock1$, lock2$, lock3$: sync int; // lock1$ = 1; // cobegin { { // task 1 do { var val = lock1$, val2 = 0; if (lock2$.isFull) { val2 = lock2$; lock1$ = val + 1; } while (val2 == 0); } { // task 2 do { var val = lock1$, val2 = 0; if (lock3$.isFull) { val2 = lock3$; lock1$ = val + 1; } while (val2 == 0); } } CSEP 524: Parallel ComputaCon Winter 2013: Chamberlain all start empty fill lock 1 lock3$ = val2 + 1; } lock2$ = val2 + 1; } 17 Wri3ng Deadlock-­‐Free Lock Code One technique: –  when requiring mulCple locks, take them in a specific order •  e.g., “always take lock1 before lock2” –  then release them in the opposite order –  ensures that you’ll never enter...
View Full Document

This document was uploaded on 04/04/2014.

Ask a homework question - tutors are online