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.

F.9. CHAPTER 12: ADVANCED SYNCHRONIZATION 307take an instantaneous snapshot of its memory. Supposethat this machine takes such a snapshot atthe beginning of read_count()’s execution, and anothersnapshot at the end of read_count()’s execution.Then read_count() will access each thread’scounter at some time between these two snapshots,and will therefore obtain a result that is bounded bythose of the two snapshots, inclusive. The overallsum will therefore be bounded by the pair of sumsthat would have been obtained from each of the twosnapshots (again, inclusive).The expected error is therefore half of the differencebetween the pair of sums that would havebeen obtained from each of the two snapshots, thatis to say, half of the execution time of read_count()multiplied by the number of expected calls to inc_count() per unit time.Or, for those who prefer equations:ǫ = T rR i2(F.1)where ǫ is the expected error in read_count()’s returnvalue, T r is the time that read_count() takesto execute, and R i is the rate of inc_count() callsper unit time. (<strong>And</strong> of course, T r and R i shoulduse the same units of time: microseconds and callsper microsecond, seconds and calls per second, orwhatever, as long as they are the same units.)Wow!!! Figure 9.1 contains 69 lines of code,compared to only 42 in Figure 4.8. <strong>Is</strong> this extracomplexity really worth it?Answer:This of course needs to be decided on a caseby-casebasis. <strong>If</strong> you need an implementationof read_count() that scales linearly, then thelock-based implementation shown in Figure 4.8simply will not work for you. On the other hand,if calls to count_read() are sufficiently rare, thenthe lock-based version is simpler and might thus bebetter, although much of the size difference is dueto the structure definition, memory allocation, andNULL return checking.Of course, a better question is ”why doesn’t thelanguageimplementcross-threadaccessto__threadvariables?” After all, such an implementation wouldmake both the locking and the use of RCU unnecessary.This would in turn enable an implementationthat was even simpler than the one shown in Figure4.8, but with all the scalability and performancebenefits of the implementation shown in Figure 9.1!F.9 Chapter 12: AdvancedSynchronizationQuick Quiz 9.3:Hey!!! Line 45 of Figure 9.1 modifies a value ina pre-existing countarray structure! Didn’t yousay that this structure, once made available toread_count(), remained constant???Answer:Indeed I did say that. <strong>And</strong> it would be possible tomake count_register_thread() allocate a newstructure, much as count_unregister_thread()currently does.But this is unnecessary. Recall the derivation ofthe error bounds of read_count() that was basedon the snapshots of memory. Because new threadsstart with initial counter values of zero, the derivationholds even if we add a new thread partwaythrough read_count()’s execution. <strong>So</strong>, interestinglyenough, when adding a new thread, this implementationgets the effect of allocating a new structure,but without actually having to do the allocation.Quick Quiz 9.4:Quick Quiz 12.1:How on earth could the assertion on line 21 of thecode in Figure 12.3 on page 118 possibly fail???Answer:The key point is that the intuitive analysis missedis that there is nothing preventing the assignmentto C from overtaking the assignment to A as bothrace to reach thread2(). This is explained in theremainder of this section.Quick Quiz 12.2:Great... <strong>So</strong> how do I fix it?Answer:The easiest fix is to replace the barrier() online 12 with an smp_mb().Quick Quiz 12.3:<strong>What</strong> assumption is the code fragment in Figure12.4 making that might not be valid on realhardware?

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

Saved successfully!

Ooh no, something went wrong!