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 51R1R7 R6 R5 R4L0 R1 R2 R3Index LDEQ 0 DEQ 1 DEQ 2 DEQ 3Index RL−4 L−3 L−2L−8 L−7 L−6L−1L−5Enq 3RFigure 5.8: Hashed <strong>Do</strong>uble-Ended Queue With 12ElementsR4 R1 R2 R3DEQ 0 DEQ 1 DEQ 2 DEQ 31 struct pdeq {2 spinlock_t llock;3 int lidx;4 spinlock_t rlock;5 int ridx;6 struct deq bkt[DEQ_N_BKTS];7 };Index LEnq 3L1RIndex RFigure 5.9: Lock-Based <strong>Parallel</strong> <strong>Do</strong>uble-EndedQueue Data StructureIndex LR4L0R5R1R2L−2R3L−1DEQ 0 DEQ 1 DEQ 2 DEQ 3Index RFigure 5.7: Hashed <strong>Do</strong>uble-Ended Queue After Insertionsqueue holds a one-quarter slice of the full paralleldouble-ended queue.Figure 5.9 shows the corresponding C-languagedata structure, assuming an existing struct deqthat provides a trivially locked double-ended-queueimplementation. This data structure contains theleft-handlockonline2, theleft-handindexonline3,the right-hand lock on line 4, the right-hand indexon line 5, and, finally, the hashed array of simplelock-based double-ended queues on line 6. Ahigh-performance implementation would of courseuse padding or special alignment directives to avoidfalse sharing.Figure 5.10 shows the implementation of the enqueueand dequeue functions. 1 Discussion will focus1 One could easily create a polymorphic implementation inany number of languages, but doing so is left as an exerciseon the left-hand operations, as the right-hand operationsare trivially derived from them.Lines 1-13 show pdeq_dequeue_l(), which leftdequeuesand returns an element if possible, returningNULL otherwise. Line 6 acquires the left-handspinlock, and line 7 computes the index to be dequeuedfrom. Line 8 dequeues the element, and, ifline 9 finds the result to be non-NULL, line 10 recordsthe new left-hand index. Either way, line 11 releasesthe lock, and, finally, line 12 returns the element ifthere was one, or NULL otherwise.Lines 15-24 shows pdeq_enqueue_l(), which leftenqueuesthe specified element. Line 19 acquires theleft-hand lock, and line 20 picks up the left-handindex. Line 21 left-enqueues the specified elementonto the double-ended queue indexed by the lefthandindex. Line 22 updates the left-hand index,and finally line 23 releases the lock.As noted earlier, the right-hand operations arecompletely analogous to their left-handed counterparts.Quick Quiz 5.4: <strong>Is</strong> the hashed double-endedqueue a good solution? Why or why not?5.1.2.4 Compound <strong>Do</strong>uble-Ended QueueRevisitedThis section revisits the compound double-endedqueue, using a trivial rebalancing scheme that movesall the elements from the non-empty queue to thenow-empty queue.Quick Quiz 5.5: Move all the elements to thefor the reader.

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

Saved successfully!

Ooh no, something went wrong!