10.07.2015 Views

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

134 CHAPTER 12. ADVANCED SYNCHRONIZATIONThe CPU being busy doing adivision speculates on theLOAD of A0000 11110000 1111 B−>20000 11110000 1111 A−>00000 11110000 11110000 11110000 11110000 1111rrrrrrrrrrrrrDIVIDEDIVIDECPU 2The CPU being busy doing adivision speculates on theLOAD of AThe speculation is discardedand an updated value isretrievedFigure 12.15: Speculative Load and Barrier0000 11110000 1111 B−>20000 11110000 1111 A−>00000 11110000 11110000 11110000 1111A−>10000 1111DIVIDEDIVIDErrrrrrrrrrrrrrrrrCPU 2Figure 12.16: Speculative Load <strong>Can</strong>celled by Barrier3 LOCK1 *A = a; *F = f;7 *E = e;4 *C = c; *D = d;2 *B = b;6 UNLOCK# Ordering: legitimate or not?1 *A;*B;LOCK;*C;*D;UNLOCK;*E;*F;2 *A;{*B;LOCK;}*C;*D;UNLOCK;*E;*F;3 {*F;*A;}*B;LOCK;*C;*D;UNLOCK;*E;4 *A;*B;{LOCK;*C;}*D;{UNLOCK;*E;}*F;5 *B;LOCK;*C;*D;*A;UNLOCK;*E;*F;6 *A;*B;*C;LOCK;*D;UNLOCK;*E;*F;7 *A;*B;LOCK;*C;UNLOCK;*D;*E;*F;8 {*B;*A;LOCK;}{*D;*C;}{UNLOCK;*F;*E;}9 *B;LOCK;*C;*D;UNLOCK;{*F;*A;}*E;Table 12.2: Lock-Based Critical SectionsQuick Quiz 12.13: Given that operationsgrouped in curly braces are executed concurrently,which of the rows of Table 12.2 are legitimatereorderings of the assignments to variables “A”through “F” and the LOCK/UNLOCK operations?(The order in the code is A, B, LOCK, C, D, UN-LOCK, E, F.) Why or why not?Ordering with Multiple Locks: Code containingmultiple locks still sees ordering constraints fromthose locks, but one must be careful to keep trackof which lock is which. For example, consider thecode shown in Table 12.2, which uses a pair of locksnamed “M” and “Q”.CPU 1 CPU 2A = a; E = e;LOCK M; LOCK Q;B = b; F = f;C = c; G = g;UNLOCK M; UNLOCK Q;D = d; H = h;Table 12.3: Ordering With Multiple LocksIn this example, there are no guarantees as towhatordertheassignmentstovariables“A”through“H” will appear in, other than the constraints imposedby the locks themselves, as described in theprevious section.Quick Quiz 12.14: <strong>What</strong> are the constraints forTable 12.2?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!