priority queues dan dvorin based on ‘the art of multiprocessor programming’, by herlihy &...

31
Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Upload: edward-burke

Post on 17-Jan-2018

231 views

Category:

Documents


0 download

DESCRIPTION

Methods  add()  removeMin() – removes the top prioritized item.

TRANSCRIPT

Page 1: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Priority Queues

Dan Dvorin

Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Page 2: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Definition A set of items. Each element has a score (indicating its

priority). Bounded range: the scores are taken from a

discrete set of items. Unbounded range: scores are taken from a

large set.

Page 3: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Methods add() removeMin() – removes the top prioritized

item.

Page 4: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Reminder Linearizability – “requires that each method

call appear to take effect at some instant between its invocation and its response.” (Art of Multiprocessor programming, chapter 15)

Quiescent consistency – “requires that in any execution, when all pending method calls complete, the values they return are consistent with some valid execution of the object.” (Art of Multiprocessor programming, chapter 15)

Page 5: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Array-Based Bounded Priority Queue An array of bins. Bin: a pool of arbitrary items. Supports put(x)

and get(x) which returns an arbitrary item. Items of score i will be in the i-th bin.

Page 6: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

…0 1 i r-2 r-1

Picture taken from: http://www.clker.com/cliparts/6/b/5/0/11971056571557644369biswajyotim_Bin.svg.med.png

Page 7: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15
Page 8: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Tree-Based Bounded Queue m leaves (Bins). m-1internal nodes which are bounded

counters. Counters keep track of the number of items in

the leaves of their left subtree.

Page 9: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Leaf to rootRoot to leaf

Page 10: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15
Page 11: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Properties The SimpleTree algorithm is not linearizable:

threads may overtake each other. It is quiescently consistent. Lock-free if the counters and bins are lock

free.

Page 12: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Unbounded Heap-Based Queue Each node holds an item and a score. The root holds the minimal scored item, which

is ‘first’ in the queue. A child’s score is no smaller than its parent’s. The (binary) heap is implemented with an

array – children of entry i are entries 2i and 2i+1.

Page 13: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Sift Up

Page 14: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Sift Down

Page 15: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Concurrent Heap - Overview We want to allow concurrent calls to add() and

removeMin(). Sifting up/down steps should be interleaved

between concurrent calls. Linearizable

Page 16: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Concurrent Heap Uses heapLock, to synchronize atomic

modifications to the heap’s fields. HeapNode now has a lock field, a tag field,

and an owner.

Page 17: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Concurrent Heap A node is with a BUSY tag, while it is being

sifted up by an add() call. The owner of a node is decided on its

initialization. add() sifts nodes up, while removeMin() sifts

down. removeMin() is not delayed by an add() call. It

can “snatch” a node underneath an add(). In that case, add() simply searches for its

node up the branch.

Page 18: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Concurrent Heap

Page 19: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Concurrent Heap – removeMin)(

Page 20: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Concurrent Heap – removeMin)(

Page 21: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Concurrent Heap – add)(

•Node is initialized with BUSY tag.•The owner is the calling thread.

Page 22: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Concurrent Heap – add)(

The node was moved up by a removeMin().

Page 23: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15
Page 24: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15
Page 25: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15
Page 26: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Skiplist-Based Unbounded Queue Drawback of the heap – requires constant

rebalancing. We’ll use a skiplist instead. Reminder: a collection of ordered lists. Each

list has a level. Bottom level contains all nodes, each higher level is a sublist of the lower level list.

Page 27: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Skiplist-Based Unbounded Queue Items will be sorted by priority, not by value. Removing an item is done lazily – first the

node is marked as removed, and later is physically removed.

removeMin() scans the bottom level list for the first unmarked node and tries to mark it. On failure, it continues down the list.

On success, skiplist’s remove() is called to remove the node physically.

Page 28: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15
Page 29: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Skiplist-Based Unbounded Queue

Page 30: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Skiplist-Based Unbounded Queue

Page 31: Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Properties Lock free. Quiescently consistent: if an item x was

present before the start of a removeMin() call, then the item returned will have a score less than or equal to that of x.

Not linearizable: a thread might add a higher priority item, and then a lower priority one, and the traversing thread might find and return the later inserted lower priority item (This behavior is quiescently consistent though. It is possible to reorder add() calls concurrent with the removeMin() calls).