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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

12.2. MEMORY BARRIERS 133CPU 1000 111000 111 A=1000 111000 111 B=2000 111wwwwwwwwwwwwwwww0000 11110000 1111A−>00000 1111B−>90000 11110000 11110000 1111B−>20000 11110000 1111A−>10000 11110000 1111A−>10000 1111rrrrrrrrrrrrrrrrrCPU 21st2ndThe CPU being busy doing adivision speculates on theLOAD of AFigure 12.13: Read Barrier Supplied, Take TwoOnce the divisions are completethe CPU can then perform theLOAD with immediate effect0000 11110000 1111 B−>20000 11110000 1111 A−>00000 11110000 11110000 11110000 1111DIVIDEDIVIDECPU 2Figure 12.14: Speculative Load12.2.12 Memory-Barrier Examples12.2.12.1 Locking ExamplesLOCK Followed by UNLOCK: A LOCK followedbyanUNLOCKmaynotbeassumedtobefullmemory barrier because it is possible for an accessprecedingtheLOCKtohappenaftertheLOCK,andan access following the UNLOCK to happen beforethe UNLOCK, and the two accesses can themselvesthen cross. For example, the following:1 *A = a;2 LOCK3 UNLOCK4 *B = b;might well execute in the following order:2 LOCK4 *B = b;1 *A = a;3 UNLOCKAgain, always remember that both LOCK andUNLOCK are permitted to let preceding operations“bleed in” to the critical section.Quick Quiz 12.11: <strong>What</strong> sequence of LOCK-UNLOCK operations would act as a full memorybarrier?Quick Quiz 12.12: <strong>What</strong> (if any) CPUs havememory-barrier instructions from which these semipermiablelocking primitives might be constructed?LOCK-Based Critical Sections: Although aLOCK-UNLOCK pair does not act as a full memorybarrier, these operations do affect memory ordering.Consider the following code:1 *A = a;2 *B = b;3 LOCK4 *C = c;5 *D = d;6 UNLOCK7 *E = e;8 *F = f;This could legimately execute in the following order,where pairs of operations on the same line indicatethat the CPU executed those operations concurrently:

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

Saved successfully!

Ooh no, something went wrong!