Distributed Algorithms (22903) Lecturer: Danny Hendler Lock-free stack algorithms.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Fast and Lock-Free Concurrent Priority Queues for Multi-Thread Systems Håkan Sundell Philippas Tsigas.
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Scalable Flat-Combining Based Synchronous Queues Danny Hendler, Itai Incze, Nir Shavit and Moran Tzafrir Presentation by Uri Golani.
Scalable and Lock-Free Concurrent Dictionaries
Universality of Consensus The Art of Multiprocessor Programming Spring 2007.
CPSC 668Set 18: Wait-Free Simulations Beyond Registers1 CPSC 668 Distributed Algorithms and Systems Fall 2006 Prof. Jennifer Welch.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Concurrent Queues and Stacks The Art of Multiprocessor Programming Spring 2007.
Scalable Synchronous Queues By William N. Scherer III, Doug Lea, and Michael L. Scott Presented by Ran Isenberg.
Linked Lists. Outline Why linked lists? Linked lists basics Implementation Basic primitives ­Searching ­Inserting ­Deleting.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Art of Multiprocessor Programming1 Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified.
Concurrent Queues and Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Concurrent Queues and Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09.
CS510 Advanced OS Seminar Class 10 A Methodology for Implementing Highly Concurrent Data Objects by Maurice Herlihy.
Concurrent Queues and Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Comparison Under Abstraction for Verifying Linearizability Daphna Amit Noam Rinetzky Mooly Sagiv Tom RepsEran Yahav Tel Aviv UniversityUniversity of Wisconsin.
A Dynamic Elimination-Combining Stack Algorithm Gal Bar-Nissan, Danny Hendler and Adi Suissa Department of Computer Science, BGU, January 2011 Presnted.
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.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Parallel Programming Philippas Tsigas Chalmers University of Technology Computer Science and Engineering Department © Philippas Tsigas.
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS Fall 2011 Prof. Jennifer Welch CSCE 668 Set 18: Wait-Free Simulations Beyond Registers 1.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
מרצה: יהודה אפק מגיש: ערן שרגיאן. OutlineOutline Quick reminder of the Stack structure. The Unbounded Lock-Free Stack. The Elimination Backoff Stack.
Object-Oriented Programming Simple Stack Implementation.
A Methodology for Creating Fast Wait-Free Data Structures Alex Koganand Erez Petrank Computer Science Technion, Israel.
Practical concurrent algorithms Mihai Letia Concurrent Algorithms 2012 Distributed Programming Laboratory Slides by Aleksandar Dragojevic.
CS510 Concurrent Systems Jonathan Walpole. A Methodology for Implementing Highly Concurrent Data Objects.
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Concurrent Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
Distributed Algorithms (22903) Lecturer: Danny Hendler The wait-free hierarchy and the universality of consensus This presentation is based on the book.
SkipLists and Balanced Search The Art Of MultiProcessor Programming Maurice Herlihy & Nir Shavit Chapter 14 Avi Kozokin.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
DECS: A Dynamic Elimination-Combining Stack Algorithm Gal Bar-Nissan, Danny Hendler, Adi Suissa 1 OPODIS 2011.
Concurrent Computing Seminar Introductory Lecture Instructor: Danny Hendler
Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects MAGED M. MICHAEL PRESENTED BY NURIT MOSCOVICI ADVANCED TOPICS IN CONCURRENT PROGRAMMING,
CS510 Concurrent Systems Tyler Fetters. A Methodology for Implementing Highly Concurrent Data Objects.
Scalable lock-free Stack Algorithm Wael Yehia York University February 8, 2010.
Distributed Algorithms (22903)
Review Array Array Elements Accessing array elements
Queues.
Stacks and Queues Chapter 4.
Data Structures Interview / VIVA Questions and Answers
Stacks.
Distributed Algorithms (22903)
Distributed Algorithms (22903)
Distributed Algorithms (22903)
Pointers and Linked Lists
Stacks.
Distributed Algorithms (22903)
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
Distributed Algorithms (22903)
Distributed Algorithms (22903)
Scalable lock-free Stack Algorithm
Multicore programming
Model Checking of a lock-free stack
Stacks CS-240 Dick Steflik.
Data Structures.
Presentation transcript:

Distributed Algorithms (22903) Lecturer: Danny Hendler Lock-free stack algorithms

2 Wait-freedom vs. lock-freedom Wait-freedom – each process completes its operation in a finite number of its own steps Lock-freedom – some process completes its operation after a finite number of steps is taken

33 The compare&swap operation Comare&swap(b,old,new) atomically v read from b if (v = old) { b new return success } else return failure; MIPS PowerPC DECAlpha Motorola 680x0 IBM 370 Sun SPARC 80X86

4 Treiber’s stack algorithm Push(int v, Stack S) 1.n := new NODE ;create node for new stack item 2.n.val := v ;write item value 3.do forever ;repeat until success 4. node top := S.top 5. n.next := top ;next points to current (LIFO order) 6. if compare&swap(S, top, n) ; try to add new item 7. return ; return if succeeded 8.od Top val next val next … val next

5 Treiber’s stack algorithm (cont’d) Pop(Stack S) 1.do forever 2. top := S.top 3. if top = null 4. return empty 5. if compare&swap(S, top, top.next) 6. return-val=top.val 7. free top 8. return return-val 9.od Top val next val next … val next

6 Treiber’s stack algorithm (cont’d) It is easily seen that the alg is linearizable and lock-free A disadvantage of the algorithms is that… It has a sequential bottleneck.

An elimination backoff stack algorithm (Hendler, Shavit and Yerushalmi, 2004) Key idea: Key idea: pairs of push/pop operations may collide and eliminate each other without accessing a central stack. Top val next val next … val next Central stack collision array

8 Collision scenarios Collision array push pop push Top val next val next … val next Central stack pop push

9 Elimination: challenges Collision array push pop push Top val next val next … val next Central stack pop push Prevent elimination chains: e.g., A collides with B, which collides with C… Prevent race conditions: e.g., A collides with B, which is already gone…

Data structures Each stack operation is represented by a ThreadInfo structure struct ThreadInfo { id ;the identifier of the thread performing the operation op ;a PUSH/POP opcode cell ;a cell structure spin ; duration to spin } Struct cell { ;a representation of stack item as in Treiber pnext ;pointer to the next cell pdata ;stack item } Location array p1p2 p3 pn Thread Info p4 collision array p1 p7

11 Elimination-backoff stack code void StackOp(ThreadInfo * p) { 1.if (TryPerformStackOP(p) == FALSE) ;if operation not applied to central stack 2. LesOp(p) ;try to eliminate operation by colliding with an opposite-type operation 3.return void LesOP(ThreadInfo * p) 1.while (1) 2. location[mypid]=p ;announce arrival 3. pos=GetPosition(p) ;get a random position at the collision array 4. him=collision[pos] ;read current value of that position 5. while (!compare&swap(&collision[pos],him,mypid);try to write own ID 6. him=collision[pos] ;continue till success 7. if (him != empty) ;if read an ID of another thread 8. q=location[him] ;read a pointer to the other thread’s info 9. if (q!=NULL && q->id=him && q->op != p->op) ;if may collide 10. if (compare&swap(&location[mypid],p,NULL) ;try to prevent unwanted collisions 11. if (TryCollision(p,q)==true) ;if collided successfully 12. return ;return code is already at ThreadInfo structure 13. else goto stack ;try to apply operation to central stack 14. else FinishCollision(p), return ;extract information and finish 15. delay (p->spin) ;Wait for other thread to collide with me 16. if (!compare&swap(&location[mypid],p,NULL) ;if someone collided with me 17. FinishCollision(p), return;Extract information and finish stack: if (TryPerformStackOp(p)==TRUE) return ;try to apply operation to central stack

12 Elimination-backoff stack code (cont’d) void TryCollision(ThreadInfo* p, ThreadInfo *q) 1.if (p->op==PUSH) 2. if (compare&swap(&location[him],q,p)) ;give my record to other thread 3. return TRUE 4. else 5. return FALSE 6.else 7. if (compare&swap(&location[him],q,NULL)) 8. p->cell=q->cell ;get pointer to PUSH operation’s cell 9. return TRUE 10. else 11. return FALSE void FinishCollision(ThreadInfo* p) 1.if (p->op==POP) 2. p->pcell=location[mypid]->pcell 3. location[mypid]=NULL

13 Elimination-backoff stack code (cont’d) Why is this implementation linearizable? Can a record be recycled once popped from stack?

© Herlihy-Shavit Recycling: Simple Solution Each thread has a free list of unused queue nodes Allocate node: pop from list Free node: push onto list Use CAS for atomicity Deal with underflow somehow …

© Herlihy-Shavit Why Recycling is Hard Free pool headtail Want to rediret head from grey to red zzz…

© Herlihy-Shavit Why Recycling is Hard Free pool zzz headtail

© Herlihy-Shavit Why Recycling is Hard Free pool Yawn! headtail

© Herlihy-Shavit Why Recycling is Hard Free pool CAS headtail OK, here I go!

© Herlihy-Shavit Final State Free pool What went wrong? headtail

© Herlihy-Shavit The Dreaded ABA Problem Head pointer has value A Thread reads value A headtail

© Herlihy-Shavit Dreaded ABA continued zzz Head pointer has value B Node A freed headtail

© Herlihy-Shavit Dreaded ABA continued Yawn! Head pointer has value A again Node A recycled & reinitialized headtail

© Herlihy-Shavit Dreaded ABA continued CAS succeeds because pointer matches even though pointer’s meaning has changed CAS headtail

© Herlihy-Shavit The Dreaded ABA Problem Is a result of CAS() semantics (Sun, Intel, AMD) Does not arise with Load- Locked/Store-Conditional (IBM)

© Herlihy-Shavit Dreaded ABA – A Solution Tag each pointer with a counter Unique over lifetime of node Pointer size vs word size issues Overflow? –Don’t worry be happy? –Bounded tags Other solutions exist (e.g. hazard pointers)