1 / 140

Ordered linked list implementation of a set

Ordered linked list implementation of a set. By Lior Zibi. b. a. c. -∞. +∞. Discussion Topics. Defining the set and the linked list implementation. Some definitions. Algorithms: 1. Coarse grained synchronization. 2. Fine grained synchronization. 3. Optimistic synchronization.

eloise
Download Presentation

Ordered linked list implementation of a set

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ordered linked list implementation of a set By Lior Zibi b a c -∞ +∞

  2. Discussion Topics • Defining the set and the linked list implementation. • Some definitions. • Algorithms: • 1. Coarse grained synchronization. • 2. Fine grained synchronization. • 3. Optimistic synchronization. • 4. Lazy synchronization.

  3. Defining the linked list First, Set : • Unordered collection of items • No duplicates

  4. Defining the linked list Now the linked implements: public interface Set<T> { public booleanadd(T x); publicbooleanremove(T x); publicbooleancontains(T x); } public class Node { public T item; publicint key; public Node next; }

  5. b a c Defining the linked list -∞ +∞ Sorted with Sentinel nodes (min & max possible keys) 5

  6. Defining concurrent methods properties • Invariant: • Property that always holds. • Established because • True when object is created. • Truth preserved by each method • Each step of each method.

  7. Defining concurrent methods properties • Rep-Invariant: • The invariant on our concrete Representation = on the list. • Preserved by methods. • Relied on by methods. • Allows us to reason about each method in isolation without considering how they interact.

  8. Defining concurrent methods properties • Our Rep-invariant: • Sentinel nodes • tail reachable from head. • Sorted • No duplicates • Depends on the implementation.

  9. Defining concurrent methods properties • Abstraction Map: • S(List) = • { x | there exists a such that • a reachable from head and • a.item = x • } • Depends on the implementation.

  10. Abstract Data Types Example: S( ) = {a,b} b a a b • Concrete representation: • Abstract Type: • {a, b} 10

  11. Defining concurrent methods properties • Wait-free: Every call to the function finishes in a finite number of steps. Supposing the Scheduler is fair: • Starvation-free: every thread calling the method eventually returns.

  12. Algorithms • Next: going throw each algorithm. • 1. Describing the algorithm. • 2. Explaining why every step of the algorithm is needed. • 3. Code review. • 4. Analyzing each method properties. • 5. Advantages / Disadvantages. • 6. Presenting running times for my implementation of the algorithm. • + Example of proving correctness for Remove(x) in FineGrained. Hopefully… “Formal proofs of correctness lie beyond the scope of this book”

  13. 0.Sequential List Based Set Add() a d c Remove() a c b 13

  14. 0.Sequential List Based Set Add() a d c b Remove() a c b 14

  15. 1.Course Grained 1. Describing the algorithm: • Most common implementation today. a d b • Add(x) / Remove(x) / Contains(x): - Lock the entire list then perform the operation. 15

  16. 1.Course Grained 1. Describing the algorithm: • Most common implementation today c a d b • All methods perform operations on the list while holding the lock, so the execution is essentially sequential. 16

  17. 1.Course Grained 3. Code review: Add: 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(); } } Finding the place to add the item Adding the item if it wasn’t already in the list 17

  18. 1.Course Grained 3. Code review: Remove: 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(); } } Finding the item Removing the item Art of Multiprocessor Programming 18

  19. 1.Course Grained 3. Code review: Contains: 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(); } } Finding the item Returning true if found 19

  20. 1.Course Grained 4. Methods properties: • The implementation inherits its progress conditions from those of the Lock, and so assuming fair Scheduler: - If the Lock implementation is Starvation free Every thread will eventually get the lock and eventually the call to the function will return. • So our implementation of Insert, Remove and Contains is Starvation-free 20

  21. 1.Course Grained 5. Advantages / Disadvantages: Advantages: - Simple. - Obviously correct. Disadvantages: - High Contention. (Queue locks help) - Bottleneck! 21

  22. 1.Course Grained 6. Running times: • The tests were run on Aries – Supports 32 running threads. UltraSPARC T1 - Sun Fire T2000. • Total of 200000 operations. • 10% adds, 2% removes, 88% contains – normal work load percentages on a set. • Each time the list was initialized with 100 elements. • One run with a max of 20000 items in the list. Another with only 2000. 22

  23. 1.Course Grained 6. Running times: 23

  24. 2.Fine Grained 1. Describing the algorithm: • Split object into pieces • Each piece has own lock. • Methods that work on disjoint pieces need not exclude each other. 24

  25. 2.Fine Grained 1. Describing the algorithm: • Add(x) / Remove(x) / Contains(x): • Go throw the list, lock each node and release only after the lock of the next element has been acquired. • Once you have reached the right point of the list perform the Add / Remove / Contains operation. 25

  26. b d a c 2.Fine Grained 1. Describing the algorithm: illustrated Remove. remove(b) 26

  27. b d a c 2.Fine Grained 1. Describing the algorithm: illustrated Remove. remove(b) 27

  28. b d a c 2.Fine Grained 1. Describing the algorithm: illustrated Remove. remove(b) 28

  29. b d a c 2.Fine Grained 1. Describing the algorithm: illustrated Remove. remove(b) 29

  30. b d a c 2.Fine Grained 1. Describing the algorithm: illustrated Remove. remove(b) 30

  31. d a c 2.Fine Grained 1. Describing the algorithm: illustrated Remove. remove(b) 31

  32. 2.Fine Grained 2. Explaining why every step is needed. Why do we need to always hold 2 locks? 32

  33. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes 33

  34. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes 34

  35. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes 35

  36. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes 36

  37. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes 37

  38. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes 38

  39. Concurrent Removes b d a c 2. Explaining why every step is needed. remove(c) remove(b) 39

  40. Concurrent Removes b d a c 2. Explaining why every step is needed. remove(c) remove(b) 40

  41. d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes 41

  42. d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) Bad news, C not removed remove(b) Concurrent removes 42

  43. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes Now with 2 locks. 43

  44. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes Now with 2 locks. 44

  45. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes Now with 2 locks. 45

  46. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes Now with 2 locks. 46

  47. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes Now with 2 locks. 47

  48. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes Now with 2 locks. 48

  49. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes Now with 2 locks. 49

  50. b d a c 2.Fine Grained 2. Explaining why every step is needed. remove(c) remove(b) Concurrent removes Now with 2 locks. 50

More Related