Lecture 9 : Concurrent Data Structures Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Slides:



Advertisements
Similar presentations
Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Advertisements

Ordered linked list implementation of a set
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Linked Lists: Locking, Lock- Free, and Beyond … Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)
Wait-Free Linked-Lists Shahar Timnat, Anastasia Braginsky, Alex Kogan, Erez Petrank Technion, Israel Presented by Shahar Timnat 469-+
Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
CS492B Analysis of Concurrent Programs Lock Basics Jaehyuk Huh Computer Science, KAIST.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Linked Lists: Locking, Lock-Free, and Beyond … The Art of Multiprocessor Programming Spring 2007.
Review Learn about linked lists
Linked Lists: Lazy and Non-Blocking Synchronization Based on the companion slides for The Art of Multiprocessor Programming (Maurice Herlihy & Nir Shavit)
Lecture 6-2 : Concurrent Queues and Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
1 Lecture 21: Transactional Memory Topics: consistency model recap, introduction to transactional memory.
Shared Counters and Parallelism Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
1 Lecture 7: Transactional Memory Intro Topics: introduction to transactional memory, “lazy” implementation.
1 Lecture 23: Transactional Memory Topics: consistency model recap, introduction to transactional memory.
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Art of Multiprocessor Programming1 Concurrent Hashing Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Who’s Afraid of a Big Bad Lock Nir Shavit Sun Labs at Oracle Joint work with Danny.
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
State Teleportation How Hardware Transactional Memory can Improve Legacy Data Structures Maurice Herlihy and Eli Wald Brown University.
Linked Lists: Optimistic, Lock-Free, … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by Pavol Černý,
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Concurrent Linked Lists and Linearizability Proofs Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified.
Hashing and Natural Parallism Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Advanced Locking Techniques
A Simple Optimistic skip-list Algorithm Maurice Herlihy Brown University & Sun Microsystems Laboratories Yossi Lev Brown University & Sun Microsystems.
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Concurrent Hashing and Natural Parallelism Chapter 13 in The Art of Multiprocessor Programming Instructor: Erez Petrank Presented by Tomer Hermelin.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Linked Lists: Locking, Lock-Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
SkipLists and Balanced Search The Art Of MultiProcessor Programming Maurice Herlihy & Nir Shavit Chapter 14 Avi Kozokin.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Linked Lists: Locking, Lock- Free, and Beyond … Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
6.852 Lecture 21 ● Techniques for highly concurrent objects – coarse-grained mutual exclusion – read/write locking – fine-grained locking (mutex and read/write)
Lecture 20: Consistency Models, TM
Linked Lists: Locking, Lock-Free, and Beyond …
Chapter 4 Unordered List.
Lecture 6-1 : Concurrent Data Structures (Concurrent Linked Lists)
Review Deleting an Element from a Linked List Deletion involves:
Minh, Trautmann, Chung, McDonald, Bronson, Casper, Kozyrakis, Olukotun
Linked Lists: Locking, Lock-Free, and Beyond …
Department of Computer Science, University of Rochester
CO890 CONCURRENCY & PARALLELISM
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists: Locking, Lock-Free, and Beyond …
Concurrent Data Structures Concurrent Algorithms 2017
Lock-Free concurrent algorithm for Linked lists: Implementation
Linked Lists: Locking, Lock-Free, and Beyond …
Concurrency and Multicore Programming
Linked Lists.
Lecture 22: Consistency Models, TM
Linked Lists: Locking, Lock-Free, and Beyond …
Multicore programming
Multicore programming
Introduction to C++ Linear Linked Lists
Chapter 4 Unordered List.
Multicore programming
Linked Lists: Locking, Lock-Free, and Beyond …
Lecture 6: Linked Data Structures
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Presentation transcript:

Lecture 9 : Concurrent Data Structures Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrent Data Structures We assume –shared-memory multiprocessors environment –concurrently execute multiple threads which communicate and synchronize through data structures in shared memory

Concurrent Data Structures : Issues Far more difficult to design than sequential ones –Correctness Primary source of difficulty is concurrency The steps of different threads can be interleaved arbitrarily –Scalability (high performance) 3

Lock Granularity Coarse grained locking –Simple & Easy –Sequential bottleneck Problem at most one thread does useful work Fine grained locking –Improve the problem of sequential bottleneck (i.e. multiple threades wait for a working thread) 4

Example Concurrent linked list –Coarse grained locking –Fine grained locking Scalable counter 5

6 List-Based Sets public interface Set { public boolean add(T x); public boolean remove(T x); public boolean contains(T x); } We assume data are sorted in list.

7 List Node public class Node { public T item; // item of interest public int key; // usually hash code public Node next; // reference to next node }

8 The List-Based Set abc Sorted with Sentinel nodes (min & max possible keys) -∞ +∞

9 Sequential List Based Set a c d a b c Add() Remove()

10 Sequential List Based Set a c d b a b c Add() Remove()

11 Course Grained Locking a b d

12 Course Grained Locking a b d c

13 honk! Course Grained Locking a b d c Simple but hotspot + bottleneck honk!

14 Coarse-Grained Synchronization Sequential bottleneck –Threads “stand in line” Adding more threads –Does not improve throughput –Struggle to keep it from getting worse So why even use a multiprocessor? –Well, some apps inherently parallel …

public class CoarseList { private Node head; private Node tail; private Lock lock = new ReentrantLock(); public CoarseList() { // Add sentinels to start and end head = new Node(Integer.MIN_VALUE); tail = new Node(Integer.MAX_VALUE); head.next = this.tail; } 15 public boolean add(T item) { Node pred, curr; int key = item.hashCode(); lock.lock(); try { pred = head; curr = pred.next; while (curr.key < key) { pred = curr; curr = curr.next; } if (key == curr.key) { return false; } else { Node node = new Node(item); node.next = curr; pred.next = node; return true; } } finally { lock.unlock(); } Coarse-Grained Synchronization (Linked List)

16 public boolean contains(T item) { Node pred, curr; int key = item.hashCode(); lock.lock(); try { pred = head; curr = pred.next; while (curr.key < key) { pred = curr; curr = curr.next; } return (key == curr.key); } finally { lock.unlock(); } public boolean remove(T item) { Node pred, curr; int key = item.hashCode(); lock.lock(); try { pred = this.head; curr = pred.next; while (curr.key < key) { pred = curr; curr = curr.next; } if (key == curr.key) pred.next = curr.next; return true; } else { return false; } } finally { lock.unlock(); }

17 Coarse-Grained Locking Easy, same as synchronized methods –“One lock to rule them all …” Simple, clearly correct –Deserves respect! Works poorly with contention

18 Performance Improvement For highly-concurrent objects Goal: –Concurrent access –More threads, more throughput

19 First: Fine-Grained Synchronization Instead of using a single lock.. Split object into –Independently-synchronized components Methods conflict when they access –The same component … –At the same time

20 Second: Optimistic Synchronization Search without locking … If you find it, lock and check … –OK: we are done –Oops: start over Evaluation –Usually cheaper than locking –Mistakes are expensive

21 Third: Lazy Synchronization Postpone hard work Removing components is tricky –Logical removal Mark component to be deleted –Physical removal Do what needs to be done

22 Fine-grained Locking Requires careful thought –“Do not meddle in the affairs of wizards, for they are subtle and quick to anger” Split object into pieces –Each piece has own lock –Methods that work on disjoint pieces need not exclude each other

23 Fine-grained Locking Use multiple locks of small granularity to protect different parts of the data structure Goal –To allow concurrent operations to proceed in parallel when they do not access the same parts of the data structure

24 Hand-over-Hand locking abc

25 Hand-over-Hand locking abc

26 Hand-over-Hand locking abc

27 Hand-over-Hand locking abc

28 Hand-over-Hand locking abc

29 Removing a Node abcd remove(b)

30 Removing a Node abcd remove(b)

31 Removing a Node abcd remove(b)

32 Removing a Node abcd remove(b)

33 Removing a Node abcd remove(b)

34 Removing a Node acd remove(b) Why do we need to always hold 2 locks?

35 Concurrent Removes abcd remove(c) remove(b)

36 Concurrent Removes abcd remove(b) remove(c)

37 Concurrent Removes abcd remove(b) remove(c)

38 Concurrent Removes abcd remove(b) remove(c)

39 Concurrent Removes abcd remove(b) remove(c)

40 Concurrent Removes abcd remove(b) remove(c)

41 Concurrent Removes abcd remove(b) remove(c)

42 Concurrent Removes abcd remove(b) remove(c)

Art of Multiprocessor Programming43 Uh, Oh acd remove(b) remove(c)

44 Uh, Oh acd Bad news, C not removed remove(b) remove(c)

45 Problem To delete node c –Swing node b’s next field to d Problem is, –Someone deleting b concurrently could direct a pointer to c ba cbac

46 Insight If a node is locked –No one can delete node’s successor If a thread locks –Node to be deleted –And its predecessor –Then it works

47 Hand-Over-Hand Again abcd remove(b)

48 Hand-Over-Hand Again abcd remove(b)

49 Hand-Over-Hand Again abcd remove(b)

50 Hand-Over-Hand Again abcd remove(b) Found it!

51 Hand-Over-Hand Again abcd remove(b) Found it!

52 Hand-Over-Hand Again acd remove(b)

53 Removing a Node abcd remove(b) remove(c)

54 Removing a Node abcd remove(b) remove(c)

55 Removing a Node abcd remove(b) remove(c)

56 Removing a Node abcd remove(b) remove(c)

57 Removing a Node abcd remove(b) remove(c)

58 Removing a Node abcd remove(b) remove(c)

59 Removing a Node abcd remove(b) remove(c)

60 Removing a Node abcd remove(b) remove(c)

61 Removing a Node abcd Must acquire Lock of b remove(c)

62 Removing a Node abcd Cannot acquire lock of b remove(c)

63 Removing a Node abcd Wait! remove(c)

64 Removing a Node abd Proceed to remove(b)

65 Removing a Node abd remove(b)

66 Removing a Node abd remove(b)

67 Removing a Node ad remove(b)

68 Removing a Node ad

69 Fine-Grained Synchronization: hand-over-hand locking Linked List public boolean add(T item) { int key = item.hashCode(); head.lock(); Node pred = head; try { Node curr = pred.next; curr.lock(); try { while (curr.key < key) { pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } if (curr.key == key) return false; Node newNode = new Node(item); newNode.next = curr; pred.next = newNode; return true; } finally { curr.unlock(); } } finally { pred.unlock(); }

70 public boolean contains(T item) { Node last = null, pred = null, curr = null; int key = item.hashCode(); head.lock(); try { pred = head; curr = pred.next; curr.lock(); try { while (curr.key < key) { pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } return (curr.key == key); } finally { curr.unlock(); } } finally { pred.unlock(); } public boolean remove(T item) { Node pred = null, curr = null; int key = item.hashCode(); head.lock(); try { pred = head; curr = pred.next; curr.lock(); try { while (curr.key < key) { pred.unlock(); pred = curr; curr = curr.next; curr.lock(); } if (curr.key == key) { pred.next = curr.next; return true; } return false; } finally { curr.unlock(); } } finally { pred.unlock(); }

71 Why does this work? To remove node e –Must lock e –Must lock e’s predecessor Therefore, if you lock a node –It can’t be removed –And neither can its successor

72 Adding Nodes To add node e –Must lock predecessor –Must lock successor Neither can be deleted –(Is successor lock actually required?)

73 Drawbacks Better than coarse-grained lock –Threads can traverse in parallel Still not ideal –Long chain of acquire/release –Inefficient

74 Improved method for concurrent linked list? Find nodes without locking Lock nodes Check that everything is OK Think about it…

Concurrent Hash Table Coarse grained locking Fine grained locking 75

Concurrent Counter Problem Concurrent Counter –Commonly used data structure –Has simple interface –Sequential bottleneck problem Design Scalable Concurrent Counter 76

Counter without lock 77

Counter with lock 78

Scalable concurrent lock? Perfermance vs Accuracy tradeoff 79