School of Computer Science. Carleton University, Ottawa ... the state array. ⢠Fix the internal structure of the queue
Concurrent Lock-Free and Wait-Free Algorithms
Medha Vasanth School of Computer Science Carleton University, Ottawa, Canada
[email protected] www.dehne.net
COMP 5704 Project Presentation
Outline Introduction Compare and Swap Building Blocks • Micheal and Scott Queue (MS Queue) • Kogan and Petrank Queue (KP Queue)
Fast-path Slow-path Approach Correctness Implementation and Results Questions COMP 5704 Project Presentation
Introduction Parallel Data Structures – FIFO Queue Tail
Head Dummy Node
Value 5 next
Lock Freedom progress
–
3
provides
7 null
global
program
Wait Freedom – ensures progress of every process Linearizability COMP 5704 Project Presentation
Compare and Swap (CAS)
Basic primitive CAS(mem_loc,exp_val,new_val) val mem_loc No
CAS Unsuccessful
If val==exp_val
Yes
Set mem_loc to new_val CAS Successful
COMP 5704 Project Presentation
Building Blocks Michael and Scott Queue (MS Queue) Enqueue: Tail pointing to the last node Tail
Head Dummy Node
Value 5 next
3 null
COMP 5704 Project Presentation
7 null
Building Blocks Michael and Scott Queue (MS Queue) Enqueue: Tail pointing to the last node Tail
Head Dummy Node
Value 5 next
3 null
Tail
Head Dummy Node
7 null
Value 5 next
3
COMP 5704 Project Presentation
7 null
Building Blocks Enqueue: Tail pointing to the second last node Head Dummy Node
Tail Value 5 next
3 null
COMP 5704 Project Presentation
Building Blocks Enqueue: Tail pointing to the second last node Head Dummy Node
Tail Value 5 next
Head 3 null
Dummy Node
COMP 5704 Project Presentation
Tail Value 5 next
3 null
Building Blocks Enqueue: Tail pointing to the second last node Head Dummy Node
Tail Value 5 next
Head 3 null
Dummy Node
Value 5 next
Tail
Head Dummy Node
Tail
Value 5 next
3
COMP 5704 Project Presentation
7 null
3 null
Building Blocks Dequeue: Head pointing to the first node Head Dummy Node
Tail Value 5 next
3
COMP 5704 Project Presentation
7 null
Building Blocks Dequeue: Head pointing to the first node Head
Tail
Dummy Node
Value 5 next
7 null
3
Head Tail Dummy Node
Value 3 next
7 null
COMP 5704 Project Presentation
Building Blocks Kogan and Petrank Queue (KP Queue) Extends the idea of the MS Queue Uses priority based helping with phase numbers Basic Idea • All threads performing the same operation realize that some operation is in progress • Update the state array • Fix the internal structure of the queue COMP 5704 Project Presentation
Building Blocks Enqueue : Step 1 head
tail
value: 200 enqTid: 0 deqTid: -1 next:
500 1 -1
300 4 -1 null
State of thread t5 phase: 9 pending: true enqueue: true next:
COMP 5704 Project Presentation
100 5 -1 null
Building Blocks Enqueue: Step 2 head value: 200 enqTid: 0 deqTid: -1 next:
tail 500 1 -1
300 4 -1
State of thread t5 phase: 9 pending: true enqueue: true next:
COMP 5704 Project Presentation
100 5 -1 null
Building Blocks Enqueue: Step 3 head value: 200 enqTid: 0 deqTid: -1 next:
tail 500 1 -1
300 4 -1
State of thread t5 phase: 9 pending: false enqueue: true next:
COMP 5704 Project Presentation
100 5 -1 null
Building Blocks Enqueue: Step 4 head value: 200 enqTid: 0 deqTid: -1 next:
tail 500 1 -1
300 4 -1
COMP 5704 Project Presentation
100 5 -1 null
Building Blocks Dequeue: Step 1 State of thread t1 phase: 9 pending: true enqueue: false next: head value: 200 enqTid: 0 deqTid: -1 next:
tail 500 1 -1
COMP 5704 Project Presentation
300 4 -1
100 5 -1 null
Building Blocks Dequeue: Step 2 State of thread t1 phase: 9 pending: true enqueue: false next: head value: 200 enqTid: 0 deqTid: 1 next:
tail 500 1 -1
COMP 5704 Project Presentation
300 4 -1
100 5 -1 null
Building Blocks Dequeue: Step 3 State of thread t1 phase: 9 pending: false true enqueue: false next: head value: 200 enqTid: 0 deqTid: 1 next:
tail 500 1 -1
COMP 5704 Project Presentation
300 4 -1
100 5 -1 null
Building Blocks Dequeue: Step 4 head value: 200 enqTid: 0 deqTid: 1 next:
500 1 -1
tail 300 4 -1
COMP 5704 Project Presentation
100 5 -1 null
Building Blocks Dequeue: Step 4 head value: 200 enqTid: 0 deqTid: 1 next:
tail
500 1 -1
300 4 -1
head
100 5 -1 null
tail
value:500 enqTid:1 deqTid-1 next:
300 4 -1
COMP 5704 Project Presentation
100 5 -1 null
Fast-path Slow-path Approach yes
Diagram taken from Kogan, Alex, and Erez Petrank. "A methodology for creating fast wait-free data structures." Proceedings of the 17th ACM SIGPLAN symposium on Principles and Practice of Parallel
Do I need to Help?
Programming. ACM, 2012.
Help slow op
no
Try to apply my op using Fast path (most (most X X times) times)
Success?
yes
return
no Try to apply my op using Slow path (Until successful) COMP 5704 Project Presentation
Fast-path Slow-path Approach Do I need to help? Helping Record of thread ti
State of thread tj
curTid: j lastPhase: 9 nextCheck: 0
phase: 9 pending: true enqueue: true next:
Fast path: MS Queue Slow path: KP Queue
COMP 5704 Project Presentation
Correctness Correctness can be achieved by proving the following properties
Linearizability : Execution of every operation has a set of linearization points.
Wait Freedom : Every thread completes its operation in O( F + D.n 2 ) steps.
COMP 5704 Project Presentation
Implementation and Results Java implementation with MPJ. Fast-path Slow-path Approach 380.00 340.00 Time in milliseconds
300.00
FH(50,50)
260.00
FH(20,20)
220.00
FH(10,10) FH(0,0)
180.00 140.00 100.00 4
8
12
16
No of Threads
COMP 5704 Project Presentation
Implementation and Results
Performance can be improved by changing constants. Scalable and as efficient as the lock-free MS Queue.
COMP 5704 Project Presentation
Questions ?
COMP 5704 Project Presentation
Questions
How does the CAS operation work? What is the difference between lock-freedom and wait-freedom? When does a thread execute on the slowpath?
COMP 5704 Project Presentation