66
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

  • View
    253

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Companion slides forThe Art of Multiprocessor

Programmingby Maurice Herlihy & Nir Shavit

Concurrent Skip Lists

Page 2: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Set Object Interface

• Collection of elements• No duplicates• Methods

– add() a new element– remove() an element– contains() if element is present

Page 3: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Many are Cold but Few are Frozen

• Typically high % of contains() calls• Many fewer add() calls• And even fewer remove() calls

– 90% contains()– 9% add()– 1% remove()

• Folklore?– Yes but probably mostly true

Page 4: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Concurrent Sets

• Balanced Trees?– Red-Black trees, AVL trees, …

• Problem: no one does this well …• … because rebalancing after add()

or remove() is a global operation

Page 5: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Skip Lists

2255

8877

9900

• Probabilistic Data Structure• No global rebalancing• Logarithmic-time search

Page 6: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Skip List Property

9900

• Each layer is sublist of lower-levels

Page 7: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Skip List Property

779900

• Each layer is sublist of lower-levels

Page 8: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Skip List Property

55 779900

• Each layer is sublist of lower-levels

Page 9: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Skip List Property

5588

779900

• Each layer is sublist of lower-levels

Page 10: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Skip List Property

2255

8877

9900

• Each layer is sublist of lower-levels• Lowest level is entire list

Page 11: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Skip List Property

2255

8877

9900

• Each layer is sublist of lower-levels• Not easy to preserve in concurrent

implementations …

Page 12: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Search

2255

8877

9900

contains(8) Too far

Page 13: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Search

2255

8877

9900

contains(8) OK

Page 14: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Search

2255

8877

9900

contains(8) Too far

Page 15: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Search

2255

8877

9900

contains(8) Too far

Page 16: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Search

2255

8877

9900

contains(8) Yes!

Page 17: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

77

Search

800

2255

99

contains(8)

Page 18: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

77

Logarithmic

800

2255

99

contains(8)

Log

N

Page 19: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Why Logarthimic

2255

8877

9900

• Property: Each pointer at layer i jumps over roughly 2i nodes

• Pick node heights randomly so property guaranteed probabilistically

2i 2i

Page 20: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Find() -- Sequential

int find(T x, Node<T>[] preds, Node<T>[] succs) { … }

Page 21: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Find() -- Sequential

int find(T x, Node<T>[] preds, Node<T>[] succs) { … }

object height(-1 if not there)

Page 22: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Find() -- Sequential

int find(T x, Node<T>[] preds, Node<T>[] succs) { … }

Object sought

object height(-1 if not there)

Page 23: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Find() -- Sequential

int find(T x, Node<T>[] preds, Node<T>[] succs) { … }

object sought

return predecessors

Object height(-1 if not there)

Page 24: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Find() -- Sequential

int find(T x, Node<T>[] preds, Node<T>[] succs) { … }

object sought

return predecessors

return successors

object height(-1 if not there)

Page 25: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Successful Search

2255

8877

9900

find(7, …)

prev

0

1

2

3

4

Page 26: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Successful Search

2255

8877

9900

find(7, …)

curr

0

1

2

3

4

prev

0

1

2

3

4

Page 27: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Unsuccessful Search

2255

8877

9900

find(6, …)

prev

0

1

2

3

4

Page 28: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Unsuccessful Search

2255

8877

9900

find(6, …)

curr

0

1

2

3

4

prev

0

1

2

3

4

Page 29: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Lazy Skip List

• Mix blocking and non-blocking techniques: – Use optimistic-lazy locking for add()

and remove()– Wait-free contains()

• Remember: typically lots of contains() calls but few add() and remove()

Page 30: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Review: Lazy List Remove

aa b c d

Page 31: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Review: Lazy List Remove

aa b c d

Present in list

Page 32: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Review: Lazy List Remove

aa b c d

Logically deleted

Page 33: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Review: Lazy List Remove

aa b c d

Physically deleted

Page 34: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Lazy Skip Lists

2255

8877

• Use a mark bit for logical deletion

990

0

00

0

0

00

Page 35: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

add(6)

• Create node of (random) height 4

2255

8877

99000

0

66

Page 36: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

add(6)

• find() predecessors

6

2255

8877

99000

0

0

66

Page 37: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

add(6)

• find() predecessors• Lock them 6

2255

8877

9900 00

0

0

66

Page 38: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

add(6)

• find() predecessors• Lock them• Validate

6

2255

8877

9900 00

0

0

66Optimistic approach

Page 39: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

add(6)

8877

99

2255

00

• find() predecessors• Lock them• Validate• Splice

0

66

0

0

0

Page 40: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

add(6)

• find() predecessors• Lock them• Validate• Splice• Unlock

8877

99

2255

0066

0

00

0

0

Page 41: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

remove(6)

8877

99

2255

0066

0

00 0

0

0

Page 42: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

remove(6)• find() predecessors

8877

99

2255

0066

0

00 0

0

0

Page 43: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

remove(6)• find() predecessors• Lock victim

8877

99

2255

0066

0

00 0

0

0

Page 44: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

8877

99

2255

0066

remove(6)• find() predecessors• Lock victim• Set mark (if not already set)

0

00

0

0

Logical remove…

Page 45: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

8877

99

2255

0066

remove(6)• find() predecessors• Lock victim• Set mark (if not already set)• Lock predecessors (ascending order) &

validate

0

01 0

0

0

Page 46: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

remove(6)• find() predecessors• Lock victim• Set mark (if not already set)• Lock predecessors (ascending order) &

validate • Physically remove

8877

99

2255

0066

0

01 0

0

0

Page 47: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

remove(6)• find() predecessors• Lock victim• Set mark (if not already set)• Lock predecessors (ascending order) &

validate • Physically remove

8877

99

2255

0066

0

01 0

0

0

Page 48: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

remove(6)• find() predecessors• Lock victim• Set mark (if not already set)• Lock predecessors (ascending order) &

validate • Physically remove

8877

99

2255

0066

0

01 0

0

0

Page 49: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

remove(6)• find() predecessors• Lock victim• Set mark (if not already set)• Lock predecessors (ascending order) &

validate • Physically remove

8877

99

2255

0066

0

01 0

0

0

Page 50: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

remove(6)• find() predecessors• Lock victim• Set mark (if not already set)• Lock predecessors (ascending order) &

validate • Physically remove

8877

99

2255

00

0

00

0

0

Page 51: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

contains(8)• Find() & not marked

8877

99

2255

0066

0

00 0

0

0

Page 52: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

contains(8)

8877

99

2255

0066

0

00 0

0

0

Node 6 removed while traversed

Page 53: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

contains(8)

8877

99

2255

00

66

0

00

0

0

Node removed while being traversed

Page 54: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

contains(8)

8877

99

2255

00

66

0

00

0

0

Prove: an unmarked node (like 8) remains reachable

Page 55: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

8877

99

2255

0066

remove(6): Linearization• Successful remove happens when bit is set

0

00

0

0

Logical remove…

Page 56: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Add: Linearization

8877

99

2255

00

• Successful add() at point when fully linked • Add fullyLinked bit to indicate this• Bit tested by contains()

0

0

66

0

0

0

Page 57: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

contains(7): Linearization

8877

99

2255

00

66

0

00

0

0

• When fully-linked unmarked node found• Pause while fullyLinked bit unset

Page 58: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

contains(7): Linearization

88

66

99

2255

00

77

0

0

1

0

0

• When do we linearize unsuccessful Search?

1So far OK…

Page 59: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

contains(7): Linearization

88

66

99

2255

00

77

0

00

0

• When do we linearize unsuccessful Search?

77

But what if a new 7 added concurrently?

Page 60: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

contains(7): Linearization

88

66

99

2255

00

77

0

00

0

• When do we linearize unsuccessful Search?

1

77

Prove: at some point 7was not in the skip list

Page 61: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

A Simple Experiment

• Each thread runs 1 million iterations, each either:– add()– remove()– contains()

• Item and method chosen in random from some distribution

Page 62: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Lazy Skip List: Performance

Multiprogramming

search

Throughput

Page 63: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Lazy Skip List: Performance

Multiprogramming

Higher contention

searchThroughput

Page 64: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Lazy Skip List: Performance

Multiprogramming

Unrealistic Contention

search

Throughput

Page 65: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

Summary

• Lazy Skip List– Optimistic fine-grained Locking

• Performs as well as the lock-free solution in “common” cases

• Simple

Page 66: Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists

Art of Multiprocessor Programming

© Herlihy Shavit 2007

           This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.

• You are free:– to Share — to copy, distribute and transmit the work – to Remix — to adapt the work

• Under the following conditions:– Attribution. You must attribute the work to “The Art of

Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work).

– Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.

• For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to– http://creativecommons.org/licenses/by-sa/3.0/.

• Any of the above conditions can be waived if you get permission from the copyright holder.

• Nothing in this license impairs or restricts the author's moral rights.