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.

5.1. PARTITIONING EXERCISES 49P1Lock LHeader LLock RHeader RLock LHeader LLock R0 Header RP4P2Lock LLock RHeader L0 1 Header RP3Lock LHeader LLock R0 1 2 Header RFigure 5.3: Dining Philosophers Problem, PartitionedOne approach is shown in Figure 5.3, which includesfour philosophers rather than five to betterillustrate the partion technique. Here the upper andrightmost philosophers share a pair of forks, whilethe lower and leftmost philosophers share anotherpair of forks. <strong>If</strong> all philosophers are simultaneouslyhungry, at least two will be able to eat concurrently.In addition, as shown in the figure, the forks cannow be bundled so that the pair are picked up andputdownsimultaneously, simplifying theacquisitionand release algorithms.Quick Quiz 5.1: <strong>Is</strong> there a better solution to theDining Philosophers Problem?This is an example of “horizontal parallelism”[Inm85] or “data parallelism”, so named becausethere is no dependency among the philosophers.In a data-processing system, a given itemof data would pass through only one of a replicatedset of software components.Quick Quiz 5.2: <strong>And</strong> in just what sense can this“horizontal parallelism” be said to be “horizontal”?5.1.2 <strong>Do</strong>uble-Ended QueueA double-ended queue is a data structure containinga list of elements that may be inserted or removedfrom either end [Knu73]. <strong>It</strong> has been claimed thata lock-based implementation permitting concurrentoperations on both ends of the double-ended queueis difficult [Gro07]. This section shows how a partitioningdesign strategy can result in a reasonablyLock LHeader LLock R0 1 2 3 Header RFigure 5.4: <strong>Do</strong>uble-Ended Queue With Left- andRight-Hand Lockssimple implementation, looking at three general approachesin the following sections.5.1.2.1 Right- and Left-Hand LocksOneseeminglystraightforwardapproachwouldbetohave a left-hand lock for left-hand-end enqueue anddequeue operations along with a right-hand lock forright-hand-end operations, as shown in Figure 5.4.However, the problem with this approach is thatthe two locks’ domains must overlap when there arefewer than four elements on the list. This overlapis due to the fact that removing any given elementaffects not only that element, but also its left- andright-hand neighbors. These domains are indicatedby color in the figure, with blue indicating the domainoftheleft-handlock,redindicatingthedomainof the right-hand lock, and purple indicating overlappingdomains. Although it is possible to createan algorithm that works this way, the fact that ithas no fewer than five special cases should raise abig red flag, especially given that concurrent activityat the other end of the list can shift the queuefrom one special case to another at any time. <strong>It</strong> isfar better to consider other designs.5.1.2.2 Compound <strong>Do</strong>uble-Ended QueueOne way of forcing non-overlapping lock domains isshown in Figure 5.5. Two separate double-ended

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

Saved successfully!

Ooh no, something went wrong!