Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15."— Presentation transcript:

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

2 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.

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

4 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)

5 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.

6 … 01ir-2r-1 … Picture taken from: http://www.clker.com/cliparts/6/b/5/0/11971056571557644369biswajyotim_Bin.svg.med.png

7

8 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.

9 Leaf to rootRoot to leaf

10

11 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.

12 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.

13 Sift Up

14 Sift Down

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

16 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.

17 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.

18 Concurrent Heap

19 Concurrent Heap – removeMin()

20

21 Concurrent Heap – add() Node is initialized with BUSY tag. The owner is the calling thread.

22 Concurrent Heap – add() The node was moved up by a removeMin().

23

24

25

26 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.

27 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.

28

29 Skiplist-Based Unbounded Queue

30

31 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).


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

Similar presentations


Ads by Google