Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit
Turing Computability
0 1 1 0 1 0
Shared-Memory Computability
cache cache cache
shared memory
Consensus Hierarchy
1 Read/Write Registers, Snapshots 2 getAndSet, getAndIncrement, . . .
compareAndSet,
Art of Multiprocessor Programming 4
no
compareAndSet,
Art of Multiprocessor Programming 5
Hypothesis
yes?
yes? yes?
1 Read/Write Registers, Snapshots 2 getAndSet, getAndIncrement, . . .
compareAndSet,
Art of Multiprocessor Programming 6
Theorem: Universality
Consensus is universal From n-thread consensus build a
Wait-free Linearizable n-threaded implementation Of any sequentially specified object
Proof Outline
A universal construction
From n-consensus objects And atomic registers
10
Push:5, Pop:null
11
Invocation
public class Invoc { public String method; public Object[] args; }
12
Invocation
public class Invoc { public String method; public Object[] args; }
Method name
13
Invocation
public class Invoc { public String method; public Object[] args; }
Arguments
14
15
OK, 4
Art of Multiprocessor Programming 16
Response
public class Response { public Object value; }
17
Response
public class Response { public Object value; }
Return value
18
Then Wait-Free: each method call takes a finite number of steps to finish
Art of Multiprocessor Programming 20
Nave Idea
Consensus object stores reference to cell with current state Each thread creates new cell
computes outcome, tries to switch pointer to its outcome
Sadly, no
consensus objects can be used once only
Art of Multiprocessor Programming 21
Nave Idea
enq deq
22
Nave Idea
Concurrent Object
enq
Decide which to apply using consensus
head
deq ?
Art of Multiprocessor Programming 23
Queue based consensus public T decide(T value) { propose(value); Ball ball = queue.deq(); if (ball == Ball.RED) return proposed[i]; else return proposed[1-i]; }
deq
tail
enq
enq
enq
25
Universal Construction
Object represented as
Initial Object State A Log: a linked list of the method calls
26
Universal Construction
Object represented as
Initial Object State A Log: a linked list of the method calls
Basic Idea
Use one-time consensus object to decide next pointer All threads update actual next pointer based on decision
Challenges
Lock-free means we need to worry what happens if a thread stops in the middle
Art of Multiprocessor Programming 28
33
Universal Object
next
node
tail
decideNext (Consensus Object)
3
head
Universal Object
node
tail
3
head
The Solution
node tail
Threads find head by finding Max of nodes pointed to by head array
1
head
Universal Object
public class Universal { private Node[] head; private Node tail = new Node(); tail.seq = 1; for (int j=0; j < n; j++){ head[j] = tail }
39
Universal Object
public class Universal { private Node[] head; private Node tail = new Node(); tail.seq = 1; for (int j=0; j < n; j++){ head[j] = tail }
40
Universal Object
public class Universal { private Node[] head; private Node tail = new Node(); tail.seq = 1; for (int j=0; j < n; j++){ head[j] = tail }
Universal Object
public class Universal { private Node[] head; private Node tail = new Node(); tail.seq = 1; for (int j=0; j < n; j++){ head[j] = tail }
42
43
44
Universal Application
public Response apply(Invoc invoc) { int i = ThreadID.get(); Node prefer = new node(invoc); Set next field to consensus while (prefer.seq == 0) { winner Node before = Node.max(head); Node after = before.decideNext.decide(prefer); before.next = after; after.seq = before.seq + 1; head[i] = after; } }
Art of Multiprocessor Programming 54
null
enq( )
enq( )
deq()
Return
57
58
59
Correctness
List defines linearized sequential history Thread returns its response based on list order
65
Lock-freedom
Lock-free because
A thread moves forward in list Can repeatedly fail to win consensus on real head only if another succeeds Consensus winner adds node and completes within a finite number of steps
66
Wait-free Construction
Lock-free construction + announce array Stores (pointer to) node in announce
If a thread doesnt append its node Another thread will see it in array and help append it
67
Helping
Announcing my intention
Guarantees progress Even if the scheduler hates me My method call will complete
68
Wait-free Construction
announce
tail
1
head
4
i
69
70
Announce array
Art of Multiprocessor Programming 71
73
Main Loop
Non-zero sequence # means success Thread keeps helping append nodes Until its own node is appended
77
Main Loop
while (announce[i].seq == 0) { Node before = head[i]; Node help = announce[(before.seq + 1 % n)]; if (help.seq == 0) prefer = help; else prefer = announce[i];
78
Main Loop
while (announce[i].seq == 0) { Node before = head[i]; Node help = announce[(before.seq + 1 % n)]; if (help.seq == 0) prefer = help; else prefer = until announce[i]; Keep trying my cell gets a sequence number
79
Main Loop
while (announce[i].seq == 0) { Node before = head[i]; Node help = announce[(before.seq + 1 % n)]; if (help.seq == 0) prefer = help; else prefer = announce[i]; Possible end of list
80
Main Loop
while (announce[i].seq == 0) { Node before = head[i]; Node help = announce[(before.seq + 1 % n)]; if (help.seq == 0) prefer = help; else prefer = announce[i];
Whom do I help?
81
Altruism
Choose a thread to help If that thread needs help
Try to append its node Otherwise append your own
Worst case
Everyone tries to help same pitiful loser Someone succeeds
Art of Multiprocessor Programming 82
Help!
When last node in list has sequence number k All threads check
Whether thread k+1 mod n wants help If so, try to append her node first
Art of Multiprocessor Programming 83
Help!
First time after thread k+1 announces
No guarantees
85
Helping
Thread 4: Help me! Max head +1 = N+4
announce
4
tail
N+2 N+3
head
86
announce
3 4
tail
Help 3 Help 4
N+2 N+3
head
87
88
89
90
91
92
93
95
96
Shared-Memory Computability
98
99
100
CompareAndSet is Universal
public class RMWRegister { private int value; public boolean compareAndSet(int expected, int update) { int prior = this.value; if (this.value == expected) { this.value = update; return true; } return false; }}
Art of Multiprocessor Programming 101
CompareAndSet is Universal
public class RMWRegister { private int value; public boolean compareAndSet(int expected, int update) { int prior = this.value; if (this.value == expected) { this.value = update; return true; } return false; Consensus number }}
Art of Multiprocessor Programming
102
CompareAndSet is Universal
public class RMWRegister { private int value; public boolean compareAndSet(int expected, int update) { int prior = this.value; if (this.value == expected) { this.value = update; return true; } return false; Universal for any number of threads }}
Art of Multiprocessor Programming 103
On Older Architectures
IBM 360
testAndSet (getAndSet)
NYU UltraComputer
getAndAdd (fetchAndAdd)
Neither universal
Except for 2 threads
104
On Newer Architectures
Intel x86, Itanium, SPARC
compareAndSet (CAS, CMPXCHG)
All universal
For any number of threads
Trend is clear
Art of Multiprocessor Programming 105
Practical Implications
Any architecture that does not provide a universal primitive has inherent limitations You cannot avoid locking for concurrent data structures
106
Shared-Memory Computability
Principles to Practice
We saw how to define concurrent objects We discussed the computational power of our machine instructions Lets use these foundations as a basis for understanding the real world
108
109