Companion slides for The Art of Multiprocessor...
Transcript of Companion slides for The Art of Multiprocessor...
![Page 1: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/1.jpg)
Foundations of Shared Memory
Companion slides for
The Art of Multiprocessor Programming
by Maurice Herlihy & Nir Shavit
Art of Multiprocessor Programming
![Page 2: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/2.jpg)
2
Last Lecture
• Defined concurrent objects using linearizability and sequential consistency
• Fact: implemented linearizable objects (Two thread FIFO Queue) in read-write memory without mutual exclusion
• Fact: hardware does not provide linearizable read-write memory
Art of Multiprocessor Programming
![Page 3: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/3.jpg)
3
Fundamentals
• What is the weakest form of
communication that supports mutual
exclusion?
• What is the weakest shared object that
allows shared-memory computation?
Art of Multiprocessor Programming
![Page 4: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/4.jpg)
4
Alan Turing
• Showed what is and is not computable on a
sequential machine.
• Still best model there is.
Art of Multiprocessor Programming
![Page 5: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/5.jpg)
5
Turing Computability
• Mathematical model of computation
• What is (and is not) computable
• Efficiency (mostly) irrelevant
0 1 1 0 1 01
Art of Multiprocessor Programming
![Page 6: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/6.jpg)
6
Shared-Memory
Computability?
• Mathematical model of concurrent computation
• What is (and is not) concurrently computable
• Efficiency (mostly) irrelevant
10011
Shared Memory
Art of Multiprocessor Programming
![Page 7: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/7.jpg)
7
Foundations of Shared Memory
To understand modern
multiprocessors we need to ask some
basic questions …
Art of Multiprocessor Programming
![Page 8: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/8.jpg)
8
Foundations of Shared Memory
To understand modern
multiprocessors we need to ask some
basic questions …
What is the weakest useful form of
shared memory?
Art of Multiprocessor Programming
![Page 9: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/9.jpg)
9
Foundations of Shared Memory
To understand modern
multiprocessors we need to ask some
basic questions …
What is the weakest useful form of
shared memory?What can it do?
Art of Multiprocessor Programming
![Page 10: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/10.jpg)
10
Register*
10011
Holds a
(binary) value
* A memory location: name is historical
Art of Multiprocessor Programming
![Page 11: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/11.jpg)
11
Register
Can be read
10011
Art of Multiprocessor Programming
10011
![Page 12: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/12.jpg)
10011
12
Register
Can be written
01100
Art of Multiprocessor Programming
![Page 13: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/13.jpg)
13
public interface Register<T> {
public T read();
public void write(T v);
}
Registers
Art of Multiprocessor Programming
![Page 14: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/14.jpg)
14
public interface Register<T> {
public T read();
public void write(T v);
}
Registers
Type of register
(usually Boolean or m-bit Integer)
Art of Multiprocessor Programming
![Page 15: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/15.jpg)
10011
15
Single-Reader/Single-Writer
Register
01100
10011
Art of Multiprocessor Programming
![Page 16: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/16.jpg)
16
Multi-Reader/Single-Writer Register
10011
Art of Multiprocessor Programming
1001101100
![Page 17: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/17.jpg)
10011
17
mumble
mumble
11011
Multi-Reader/Multi-Writer
Registermumble
1001110011
01010
Art of Multiprocessor Programming
![Page 18: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/18.jpg)
18
Jargon Watch
• SRSW
– Single-reader single-writer
• MRSW
– Multi-reader single-writer
• MRMW
– Multi-reader multi-writer
Art of Multiprocessor Programming
![Page 19: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/19.jpg)
19
Safe Register
write(1001)
read(1001)
OK if reads
and writes
don’t overlap
Art of Multiprocessor Programming
![Page 20: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/20.jpg)
20
Safe Register
write(1001)
Some valid value if
reads and writes do
overlap
read(????)
0000 1001 1111
$*&v
Art of Multiprocessor Programming
![Page 21: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/21.jpg)
21
Regular Register
write(0)
read(1)
write(1)
read(0)
• Single Writer
• Readers return:
– Old value if no overlap (safe)
– Old or one of new values if overlap
Art of Multiprocessor Programming
![Page 22: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/22.jpg)
22
Regular or Not?
write(0)
read(1)
write(1)
read(0)
Art of Multiprocessor Programming
![Page 23: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/23.jpg)
23
Regular or Not?
write(0)
read(1)
write(1)
read(0)
Overlap: returns new value
Art of Multiprocessor Programming
![Page 24: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/24.jpg)
24
Regular or Not?
write(0) write(1)
read(0)
Overlap: returns old value
Art of Multiprocessor Programming
![Page 25: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/25.jpg)
25
Regular or Not?
write(0)
read(1)
write(1)
read(0)
Art of Multiprocessor Programming
![Page 26: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/26.jpg)
26
Regular ≠ Linearizable
write(0)
read(1)
write(1)
read(0)
write(1) already
happened
can’t explain this!
Art of Multiprocessor Programming
![Page 27: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/27.jpg)
27
Atomic Register
write(1001)
read(1001)
Linearizable to sequential safe
register
write(1010)
read(1010)
read(1010)
Art of Multiprocessor Programming
![Page 28: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/28.jpg)
28
Atomic Register
write(1001)
read(1001)
write(1010)
read(1010)
read(1010)
Art of Multiprocessor Programming
![Page 29: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/29.jpg)
29
Register Space
MRMW
MRSW
SRSW
SafeRegular
Atomic
m-valued
Boolean
Art of Multiprocessor Programming
![Page 30: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/30.jpg)
30
Weakest Register
1
0 1
Single readerSingle writer
Safe Boolean register
Art of Multiprocessor Programming
![Page 31: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/31.jpg)
31
Weakest Register
Single readerSingle writer
Get correct reading if not during state
transition
flipflop0 1 0 0 1 0
Art of Multiprocessor Programming
![Page 32: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/32.jpg)
32
Results
• From SRSW safe Boolean register
– All the other registers
– Mutual exclusion
• But not everything!
– Consensus hierarchy
Foundations
of the field
The really cool stuff …
Art of Multiprocessor Programming
![Page 33: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/33.jpg)
33
Locking within Registers
• Not interesting to rely on mutual exclusion
in register constructions
• We want registers to implement mutual
exclusion!
• It’s cheating to use mutual exclusion to
implement itself!
Art of Multiprocessor Programming
![Page 34: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/34.jpg)
34
Definition
An object implementation is wait-free if
every method call completes in a finite
number of steps
No mutual exclusion
– Thread could halt in critical section
– Build mutual exclusion from registers
Art of Multiprocessor Programming
![Page 35: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/35.jpg)
Art of Multiprocessor
Programming
35
From Safe SRSW Boolean to
Atomic Snapshots
MRMW
MRSW
SRSW
SafeRegular
Atomic
M-valued
Boolean
Snapshot
![Page 36: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/36.jpg)
36
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
Art of Multiprocessor Programming
![Page 37: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/37.jpg)
37
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
Next
Art of Multiprocessor Programming
![Page 38: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/38.jpg)
38
public class SafeBoolMRSWRegister
implements Register<Boolean> {
public boolean read() { … }
public void write(boolean x) { … }
}
Register Names
Art of Multiprocessor Programming
![Page 39: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/39.jpg)
39
public class SafeBoolMRSWRegister
implements Register<Boolean> {
public boolean read() { … }
public void write(boolean x) { … }
}
Register Names
property
Art of Multiprocessor Programming
![Page 40: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/40.jpg)
40
public class SafeBoolMRSWRegister
implements Register<Boolean> {
public boolean read() { … }
public void write(boolean x) { … }
}
Register Names
property
type
Art of Multiprocessor Programming
![Page 41: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/41.jpg)
41
public class SafeBoolMRSWRegister
implements Register<Boolean> {
public boolean read() { … }
public void write(boolean x) { … }
}
(3)
Register Names
property
type
how many readers &
writers?
Art of Multiprocessor Programming
![Page 42: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/42.jpg)
42
Safe Boolean MRSW from
Safe Boolean SRSW
0
0
0
0
0
0
0
0
0
0
0
zzz
readers
writer
Art of Multiprocessor Programming
![Page 43: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/43.jpg)
43
Safe Boolean MRSW from
Safe Boolean SRSW
0
0
0
0
0
0
0
0
0
0
0
Let’s write 1!
Art of Multiprocessor Programming
![Page 44: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/44.jpg)
44
Safe Boolean MRSW from
Safe Boolean SRSW
0 or 1
1
0
0
0
0
0
0
0
0
0
Art of Multiprocessor Programming
![Page 45: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/45.jpg)
45
Safe Boolean MRSW from
Safe Boolean SRSW
1
0 or 1
1
0
0
0
0
0
0
0
1
Art of Multiprocessor Programming
![Page 46: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/46.jpg)
46
Safe Boolean MRSW from
Safe Boolean SRSW
1
1
1
1
0 or 1
0
0
0
0
0
1
Art of Multiprocessor Programming
![Page 47: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/47.jpg)
47
Safe Boolean MRSW from
Safe Boolean SRSW
1
1
1
1
1
1
1
1
1
1
Whew!
Art of Multiprocessor Programming
![Page 48: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/48.jpg)
48
Safe Boolean MRSW from
Safe Boolean SRSWpublic class SafeBoolMRSWRegister
implements Register<Boolean> {
private SafeBoolSRSWRegister[] r =
new SafeBoolSRSWRegister[N];
public void write(boolean x) {
for (int j = 0; j < N; j++)
r[j].write(x);
}
public boolean read() {
int i = ThreadID.get();
return r[i].read();
}}
Art of Multiprocessor Programming
![Page 49: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/49.jpg)
49
Safe Boolean MRSW from
Safe Boolean SRSWpublic class SafeBoolMRSWRegister
implements BooleanRegister {
private SafeBoolSRSWRegister[] r =
new SafeBoolSRSWRegister[N];
public void write(boolean x) {
for (int j = 0; j < N; j++)
r[j].write(x);
}
public boolean read() {
int i = ThreadID.get();
return r[i].read();
}}Each thread has own
safe SRSW register
Art of Multiprocessor Programming
![Page 50: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/50.jpg)
50
Safe Boolean MRSW from
Safe Boolean SRSWpublic class SafeBoolMRSWRegister
implements BooleanRegister {
private SafeBoolSRSWRegister[] r =
new SafeBoolSRSWRegister[N];
public void write(boolean x) {
for (int j = 0; j < N; j++)
r[j].write(x);
}
public boolean read() {
int i = ThreadID.get();
return r[i].read();
}}write method
Art of Multiprocessor Programming
![Page 51: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/51.jpg)
51
Safe Boolean MRSW from
Safe Boolean SRSWpublic class SafeBoolMRSWRegister
implements BooleanRegister {
private SafeBoolSRSWRegister[] r =
new SafeBoolSRSWRegister[N];
public void write(boolean x) {
for (int j = 0; j < N; j++)
r[j].write(x);
}
public boolean read() {
int i = ThreadID.get();
return r[i].read();
}}
Write each
thread’s register
one at a time
Art of Multiprocessor Programming
![Page 52: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/52.jpg)
52
Safe Boolean MRSW from
Safe Boolean SRSWpublic class SafeBoolMRSWRegister
implements BooleanRegister {
private SafeBoolSRSWRegister[] r =
new SafeBoolSRSWRegister[N];
public void write(boolean x) {
for (int j = 0; j < N; j++)
r[j].write(x);
}
public boolean read() {
int i = ThreadID.get();
return r[i].read();
}}
read method
Art of Multiprocessor Programming
![Page 53: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/53.jpg)
53
Safe Boolean MRSW from
Safe Boolean SRSWpublic class SafeBoolMRSWRegister
implements BooleanRegister {
private SafeBoolSRSWRegister[] r =
new SafeBoolSRSWRegister[N];
public void write(boolean x) {
for (int j = 0; j < N; j++)
r[j].write(x);
}
public boolean read() {
int i = ThreadID.get();
return r[i].read();
}}
Read my own
register
Art of Multiprocessor Programming
![Page 54: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/54.jpg)
54
10001000
1000
Safe Multi-Valued MRSW from
Safe Multi-Valued SRSW?
1011
1011
1011
any value in range1000
1000
1011
Art of Multiprocessor Programming
![Page 55: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/55.jpg)
55
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
Questions?
Art of Multiprocessor Programming
![Page 56: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/56.jpg)
56
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
Next
Art of Multiprocessor Programming
![Page 57: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/57.jpg)
57
Regular Boolean MRSW from
Safe Boolean MRSW
0
0
0
1
0
1
1
1
Art of Multiprocessor Programming
![Page 58: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/58.jpg)
58
Regular Boolean MRSW from
Safe Boolean MRSW
0
0
0
0
0
1
1
1
Art of Multiprocessor Programming
Uh, oh!
![Page 59: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/59.jpg)
59
Regular Boolean MRSW from
Safe Boolean MRSW
0
0
0
0
0
Last written:
0
Art of Multiprocessor Programming
![Page 60: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/60.jpg)
60
Regular Boolean MRSW from
Safe Boolean MRSWpublic class RegBoolMRSWRegister
implements Register<Boolean> {
private boolean old;
private SafeBoolMRSWRegister value;
public void write(boolean x) {
if (old != x) {
value.write(x);
old = x;
}}
public boolean read() {
return value.read();
}}
Art of Multiprocessor Programming
![Page 61: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/61.jpg)
61
Regular Boolean MRSW from
Safe Boolean MRSWpublic class RegBoolMRSWRegister
implements Register<Boolean> {
threadLocal boolean old;
private SafeBoolMRSWRegister value;
public void write(boolean x) {
if (old != x) {
value.write(x);
old = x;
}}
public boolean read() {
return value.read();
}}
Last bit this thread wrote(made-up syntax)
Art of Multiprocessor Programming
![Page 62: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/62.jpg)
62
Regular Boolean MRSW from
Safe Boolean MRSWpublic class RegBoolMRSWRegister
implements Register<Boolean> {
threadLocal boolean old;
private SafeBoolMRSWRegister value;
public void write(boolean x) {
if (old != x) {
value.write(x);
old = x;
}}
public boolean read() {
return value.read();
}}Actual value
Art of Multiprocessor Programming
![Page 63: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/63.jpg)
63
Regular Boolean MRSW from
Safe Boolean MRSWpublic class RegBoolMRSWRegister
implements Register<Boolean> {
threadLocal boolean old;
private SafeBoolMRSWRegister value;
public void write(boolean x) {
if (old != x) {
value.write(x);
old = x;
}}
public boolean read() {
return value.read();
}}
Is new value different
from last value I wrote?
Art of Multiprocessor Programming
![Page 64: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/64.jpg)
64
Regular Boolean MRSW from
Safe Boolean MRSWpublic class RegBoolMRSWRegister
implements Register<Boolean> {
threadLocal boolean old;
private SafeBoolMRSWRegister value;
public void write(boolean x) {
if (old != x) {
value.write(x);
old = x;
}}
public boolean read() {
return value.read();
}}
If so, change it
(otherwise don’t!)
Art of Multiprocessor Programming
![Page 65: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/65.jpg)
65
Regular Boolean MRSW from
Safe Boolean MRSWpublic class RegBoolMRSWRegister
implements Register<Boolean>{
threadLocal boolean old;
private SafeBoolMRSWRegister value;
public void write(boolean x) {
if (old != x) {
value.write(x);
old = x;
}}
public boolean read() {
return value.read();
}}
Overlap? What overlap?No problem
either Boolean value works
Art of Multiprocessor Programming
![Page 66: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/66.jpg)
66
Regular Multi-Valued MRSW from
Safe Multi-Valued MRSW?0101
0101
0101
Art of Multiprocessor Programming
Safe register can
return any value in
range when value
changes
Regular register can
return only old or new
when value changes
![Page 67: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/67.jpg)
67
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
Questions?
Art of Multiprocessor Programming
![Page 68: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/68.jpg)
68
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
Next
Art of Multiprocessor Programming
![Page 69: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/69.jpg)
69
Representing m Values
0 1 2 3 4 5 6 7
1 00001
Initially 0
Unary representation:
bit[i] means value i
0 0
Art of Multiprocessor Programming
![Page 70: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/70.jpg)
70
Writing m-Valued Register
1 00001
Write 5
Art of Multiprocessor Programming
0 1 2 3 4 5 6 7
![Page 71: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/71.jpg)
71
Writing m-Valued Register
1 10000
Write 5
Initially 0
Art of Multiprocessor Programming
0 1 2 3 4 5 6 7
![Page 72: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/72.jpg)
0 1 2 3 4 5 6 7
72
Writing m-Valued Register
1 10000
Write 5
5
0
Art of Multiprocessor Programming
![Page 73: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/73.jpg)
73
MRSW Regular m-valued from
MRSW Regular Booleanpublic class RegMRSWRegister implements Register{
RegBoolMRSWRegister[M] bit;
public void write(int x) {
this.bit[x].write(true);
for (int i=x-1; i>=0; i--)
this.bit[i].write(false);
}
public int read() {
for (int i=0; i < M; i++)
if (this.bit[i].read())
return i;
}}
Art of Multiprocessor Programming
![Page 74: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/74.jpg)
74
MRSW Regular m-valued from
MRSW Regular Booleanpublic class RegMRSWRegister implements Register{
RegBoolMRSWRegister[M] bit;
public void write(int x) {
bit[x].write(true);
for (int i=x-1; i>=0; i--)
bit[i].write(false);
}
public int read() {
for (int i=0; i < M; i++)
if (bit[i].read())
return i;
}}
Unary representation:
bit[i] means value i
Art of Multiprocessor Programming
![Page 75: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/75.jpg)
75
MRSW Regular m-valued from
MRSW Regular Booleanpublic class RegMRSWRegisterimplements Register {
RegBoolMRSWRegister[m] bit;
public void write(int x) {
bit[x].write(true);
for (int i=x-1; i>=0; i--)
bit[i].write(false);
}
public int read() {
for (int i=0; i < M; i++)
if (bit[i].read())
return i;
}}
set bit x
Art of Multiprocessor Programming
![Page 76: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/76.jpg)
76
MRSW Regular m-valued from
MRSW Regular Booleanpublic class RegMRSWRegisterimplements Register {
RegBoolMRSWRegister[m] bit;
public void write(int x) {
bit[x].write(true);
for (int i=x-1; i>=0; i--)
bit[i].write(false);
}
public int read() {
for (int i=0; i < M; i++)
if (bit[i].read())
return i;
}}
Clear bits
from higher
to lower
Art of Multiprocessor Programming
![Page 77: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/77.jpg)
77
MRSW Regular m-valued from
MRSW Regular Booleanpublic class RegMRSWRegisterimplements Register {
RegBoolMRSWRegister[m] bit;
public void write(int x) {
bit[x].write(true);
for (int i=x-1; i>=0; i--)
bit[i].write(false);
}
public int read() {
for (int i=0; i < M; i++)
if (bit[i].read())
return i;
}}
Scan from lower
to higher & return
first bit set
Art of Multiprocessor Programming
![Page 78: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/78.jpg)
78
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
Questions?
Art of Multiprocessor Programming
![Page 79: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/79.jpg)
79
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
Art of Multiprocessor Programming
![Page 80: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/80.jpg)
80
Road Map (Slight Detour)
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
SRSW Atomic
Art of Multiprocessor Programming
![Page 81: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/81.jpg)
Concurrent
Reading
81
SRSW Atomic From SRSW
Regular
1234
Regular writer
Regular
reader12345678
Instead of 5678…
When is this a problem?
Art of Multiprocessor Programming
![Page 82: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/82.jpg)
82
SRSW Atomic From SRSW
Regular
1234
Regular writer
Regular
reader56785678
time
write(5678)
read(5678)
Initially
1234
Art of Multiprocessor Programming
![Page 83: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/83.jpg)
83
SRSW Atomic From SRSW
Regular
1234
Regular writer
Regular
reader12345678
Instead of 5678…
time
write(5678)
read(1234)
Initially
1234
Art of Multiprocessor Programming
![Page 84: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/84.jpg)
84
SRSW Atomic From SRSW
Regular
1234
Regular writer
Regular
reader12345678
Instead of 5678…
time
write(5678)
read(1234)
Initially
1234
Reg read(5678)
Write 5678
happened Art of Multiprocessor Programming
![Page 85: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/85.jpg)
85
Timestamped Values
Writer writes
value and stamp
together
Reader saves last value & stamp read
returns new value only if stamp is higher
12341:4556782:00 56782:00
Art of Multiprocessor Programming
12341:4556782:00
![Page 86: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/86.jpg)
86
SRSW Atomic From SRSW
Regular writer
reader
1:45 1234 < 2:00 5678
So stick with 5678
time
write(2:00 5678)
read(1:45 1234)
1:45 1234
read(2:00 5678)
Art of Multiprocessor Programming
12341:45 56782:00
![Page 87: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/87.jpg)
87
Atomic Single-Reader to Atomic
Multi-Reader
1:45 1234
1:45 1234
1:45 1234
1:45 1234
stamp value
One per reader
Art of Multiprocessor Programming
![Page 88: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/88.jpg)
88
Another Scenario
1:45 1234
2:00 5678
1:45 1234
1:45 1234
stamp value
Writer starts write…
Art of Multiprocessor Programming
![Page 89: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/89.jpg)
89
Another Scenario
1:45 1234
2:00 5678
1:45 1234
1:45 1234
stamp valuereader
reads
2:00, 5678zzz…
1:45 1234
later
reader
Yellow was completely after Blue but read earlier value…not linearizable!
Art of Multiprocessor Programming
![Page 90: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/90.jpg)
90
Multi-Reader Redux
1:45 1234 1:45 1234
1:45 1234
1:45 1234 1:45 1234
1:45 1234
one per thread
1:45 1234
1:45 1234
1:45 1234
1 2 3
1
2
3
Art of Multiprocessor Programming
![Page 91: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/91.jpg)
91
Multi-Reader Redux
1:45 1234 1:45 1234
1:45 1234
1:45 1234 1:45 1234
1:45 1234
1:45 1234
1:45 1234
Writer writes column…
2:00 5678
2:00 5678
2:00 5678
1:45 1234
reader
reads row
2:00, 5678
1
1 2 3
1
2
3
2
Art of Multiprocessor Programming
![Page 92: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/92.jpg)
92
Multi-Reader Redux
1:45 1234 1:45 1234
1:45 1234
1:45 1234 1:45 1234
1:45 1234
1:45 1234
1:45 1234
2:00 5678 1:45 1234
reader writes column to notify others of what it read
1
1 2 3
1
2
3
2
2:00 5678
2:00 5678
2:00 5678
2:00 5678
zzz…after second write
2:00, 5678
Yellow reader will read new value in column written by earlier Blue reader
Art of Multiprocessor Programming
![Page 93: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/93.jpg)
93
Can’t Yellow Miss Blue’s Update?
… Only if Readers Overlap…
time
write(2:00 5678)
read(1:45 1234)
1:45
1234
read(2:00 5678)
In which case its
OK to read 1234
Art of Multiprocessor Programming
![Page 94: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/94.jpg)
94
Bad Case Only When Readers
Don’t Overlap
time
write(2:00 5678)
read(2:00 5678)
1:45
1234
read(2:00 5678)
In which case Blue will
complete writing 2:00
5678 to its
column
Art of Multiprocessor Programming
![Page 95: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/95.jpg)
95
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
Next
Art of Multiprocessor Programming
![Page 96: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/96.jpg)
96
Multi-Writer Atomic From Multi-
Reader Atomic
1:45 1234
1:45 1234
1:45 1234
1:45 1234
stamp value
Readers read all
and take max
(Lexicographic
like Bakery)
Each writer
reads all
then writes
Max+1
to its register
2:00 5678
2:15 XYZW
Max is 2:15, return XYZW
Art of Multiprocessor Programming
![Page 97: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/97.jpg)
97
Atomic Execution
Means it is Linearizable
time
write(1)
time
Read(max= 2) write(4)
write(2) write(3) Read(max = 3)
Read (max = 1) write(2) Read(max = 4)
Art of Multiprocessor Programming
![Page 98: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/98.jpg)
98
Linearization Points
time
write(1)
time
Read(max= 2) write(4)
write(2) write(3) Read(max = 3)
Read (max = 1) write(2) Read(max = 4)
Art of Multiprocessor Programming
![Page 99: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/99.jpg)
99
Linearization Points
time
write(1)
time
Look at Writes
First
write(4)
write(2) write(3)
write(2)
Art of Multiprocessor Programming
![Page 100: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/100.jpg)
100
Linearization Points
time
write(1)
time
write(4)
write(2) write(3)
write(2)
Order writes by
TimeStamp
Art of Multiprocessor Programming
![Page 101: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/101.jpg)
101
Linearization Points
time
write(1)
time
write(4)
write(2) write(3)
write(2)
Read(max= 2)
Read(max = 3)
Read (max = 1) Read(max = 4)
Order reads by
max stamp read
Art of Multiprocessor Programming
![Page 102: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/102.jpg)
102
Linearization Points
time
write(1)
time
write(4)
write(2) write(3)
write(2)
Read(max= 2)
Read(max = 3)
Read (max = 1) Read(max = 4)
Order reads by
max stamp read
Art of Multiprocessor Programming
![Page 103: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/103.jpg)
103
Linearization Points
time
write(1)
time
write(4)
write(2) write(3)
write(2)
Read(max= 2)
Read(max = 3)
Read (max = 1) Read(max = 4)
The linearization point depends on the
execution (not a line in the code)!
Art of Multiprocessor Programming
![Page 104: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/104.jpg)
104
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshot
Questions?
Art of Multiprocessor Programming
![Page 105: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/105.jpg)
105
Road Map
• SRSW safe Boolean
• MRSW safe Boolean
• MRSW regular Boolean
• MRSW regular
• MRSW atomic
• MRMW atomic
• Atomic snapshotNext
Art of Multiprocessor Programming
![Page 106: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/106.jpg)
106
Atomic Snapshot
updatescan
Art of Multiprocessor Programming
![Page 107: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/107.jpg)
107
Atomic Snapshot
• Array of SWMR atomic registers
• Take instantaneous snapshot of all
• Generalizes to MRMW registers …
Art of Multiprocessor Programming
![Page 108: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/108.jpg)
108
Snapshot Interface
public interface Snapshot {
public int update(int v);
public int[] scan();
}
Art of Multiprocessor Programming
![Page 109: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/109.jpg)
109
Snapshot Interface
public interface Snapshot {
public int update(int v);
public int[] scan();
}
Thread i writes v to its register
Art of Multiprocessor Programming
![Page 110: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/110.jpg)
110
Snapshot Interface
public interface Snapshot {
public int update(int v);
public int[] scan();
}
Instantaneous snapshot of all theads’ registers
Art of Multiprocessor Programming
![Page 111: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/111.jpg)
111
Atomic Snapshot
• Collect
– Read values one at a time
• Problem
– Incompatible concurrent collects
– Result not linearizable
Art of Multiprocessor Programming
![Page 112: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/112.jpg)
112
Clean Collects
• Clean Collect
– Collect during which nothing changed
– Can we make it happen?
– Can we detect it?
Art of Multiprocessor Programming
![Page 113: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/113.jpg)
113
Simple Snapshot
• Put increasing labels on each entry
• Collect twice
• If both agree,
– We’re done
• Otherwise,
– Try againx
yz
w
r
zx
=
Collect 2Collect 1
x
yz
w
r
zx
Problem: Scanner might not be
collecting a snapshot!
Art of Multiprocessor Programming
![Page 114: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/114.jpg)
114
Claim: We Must Use Labels
time
x y x y
z z
x z x z
Scanner
Updater
Updater
But scanner sees x and z
together!
x and z are never
in memory
together
w
Art of Multiprocessor Programming
![Page 115: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/115.jpg)
115
Must Use Labels
time
1,x 2,y 3,x 4,y
1,z 3,z
1,x 1,z 3,x 3,z
Scanner
Updater
Updater2,w
Scanner reads x and z with
different labels and recognizes
collect not clean
Art of Multiprocessor Programming
![Page 116: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/116.jpg)
116
Simple Snapshot
• Collect twice
• If both agree,
– We’re done
• Otherwise,
– Try again1
221
7
13
1812
=
Collect 2Collect 1
1
221
7
13
1812
Art of Multiprocessor Programming
![Page 117: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/117.jpg)
117
Simple Snapshot: Update
public class SimpleSnapshot implements Snapshot {
private AtomicMRSWRegister[] register;
public void update(int value) {
int i = Thread.myIndex();
LabeledValue oldValue = register[i].read();
LabeledValue newValue =
new LabeledValue(oldValue.label+1, value);
register[i].write(newValue);
}
Art of Multiprocessor Programming
![Page 118: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/118.jpg)
118
Simple Snapshot: Update
public class SimpleSnapshot implements Snapshot {
private AtomicMRSWRegister[] register;
public void update(int value) {
int i = Thread.myIndex();
LabeledValue oldValue = register[i].read();
LabeledValue newValue =
new LabeledValue(oldValue.label+1, value);
register[i].write(newValue);
}
One single-writer register per thread
Art of Multiprocessor Programming
![Page 119: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/119.jpg)
119
Simple Snapshot: Update
public class SimpleSnapshot implements Snapshot {
private AtomicMRSWRegister[] register;
public void update(int value) {
int i = Thread.myIndex();
LabeledValue oldValue = register[i].read();
LabeledValue newValue =
new LabeledValue(oldValue.label+1, value);
register[i].write(newValue);
}
Write each time with higher label
Art of Multiprocessor Programming
![Page 120: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/120.jpg)
120
Simple Snapshot: Collect
private LabeledValue[] collect() {
LabeledValue[] copy =
new LabeledValue[n];
for (int j = 0; j < n; j++)
copy[j] = this.register[j].read();
return copy;
}
Art of Multiprocessor Programming
![Page 121: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/121.jpg)
121
Simple Snapshot
private LabeledValue[] collect() {
LabeledValue[] copy =
new LabeledValue[n];
for (int j = 0; j < n; j++)
copy[j] = this.register[j].read();
return copy;
}
Just read each register into array
Art of Multiprocessor Programming
![Page 122: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/122.jpg)
122
Simple Snapshot: Scan
public int[] scan() {
LabeledValue[] oldCopy, newCopy;
oldCopy = collect();
collect: while (true) {
newCopy = collect();
if (!equals(oldCopy, newCopy)) {
oldCopy = newCopy;
continue collect;
}
return getValues(newCopy);
}}
Art of Multiprocessor Programming
![Page 123: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/123.jpg)
123
Simple Snapshot: Scan
public int[] scan() {
LabeledValue[] oldCopy, newCopy;
oldCopy = collect();
collect: while (true) {
newCopy = collect();
if (!equals(oldCopy, newCopy)) {
oldCopy = newCopy;
continue collect;
}
return getValues(newCopy);
}}
Collect once
Art of Multiprocessor Programming
![Page 124: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/124.jpg)
124
Simple Snapshot: Scan
public int[] scan() {
LabeledValue[] oldCopy, newCopy;
oldCopy = collect();
collect: while (true) {
newCopy = collect();
if (!equals(oldCopy, newCopy)) {
oldCopy = newCopy;
continue collect;
}
return getValues(newCopy);
}}
Collect once
Collect twice
Art of Multiprocessor Programming
![Page 125: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/125.jpg)
125
Simple Snapshot: Scan
public int[] scan() {
LabeledValue[] oldCopy, newCopy;
oldCopy = collect();
collect: while (true) {
newCopy = collect();
if (!equals(oldCopy, newCopy)) {
oldCopy = newCopy;
continue collect;
}
return getValues(newCopy);
}}
Collect once
Collect twice
On mismatch,
try again
Art of Multiprocessor Programming
![Page 126: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/126.jpg)
126
Simple Snapshot: Scan
public int[] scan() {
LabeledValue[] oldCopy, newCopy;
oldCopy = collect();
collect: while (true) {
newCopy = collect();
if (!equals(oldCopy, newCopy)) {
oldCopy = newCopy;
continue collect;
}
return getValues(newCopy);
}}
Collect once
Collect twice
On match, return
values
Art of Multiprocessor Programming
![Page 127: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/127.jpg)
127
Simple Snapshot
• Linearizable
• Update is wait-free
– No unbounded loops
• But Scan can starve
– If interrupted by concurrent update
Art of Multiprocessor Programming
![Page 128: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/128.jpg)
128
Wait-Free Snapshot
• Add a scan before every update
• Write resulting snapshot together with
update value
• If scan is continuously interrupted by
updates, scan can take the update’s
snapshot
Art of Multiprocessor Programming
![Page 129: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/129.jpg)
129
Wait-free Snapshot
If A’s scan observes that B moved
twice, then B completed an update
while A’s scan was in progress
time
Update
B
≠ ≠26
24
12
Collect
26
24
12
Collect
26
24
12
Collect
Art of Multiprocessor Programming
![Page 130: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/130.jpg)
130
Wait-free Snapshot
time
≠ ≠26
24
12
Collect
26
24
12
Collect
26
24
12
Collect
Update
A
B
Art of Multiprocessor Programming
![Page 131: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/131.jpg)
131
Wait-free Snapshot
time
≠ ≠26
24
12
Collect
26
24
12
Collect
26
24
12
Collect
A
B Scan Write
Update
Art of Multiprocessor Programming
![Page 132: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/132.jpg)
132
Wait-free Snapshot
time
≠ ≠26
24
12
Collect
26
24
12
Collect
26
24
12
Collect
A
B Scan Write
Update
Scan Write
B’s 1st update must have written during 1st collect
So scan of B’s second update must
be within interval of A’s scan
So A can steal
result of B’s scan
Art of Multiprocessor Programming
![Page 133: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/133.jpg)
133
Wait-free Snapshot
time
≠ ≠26
24
12
Collect
26
24
12
Collect
26
24
12
Collect
A
B Scan WriteScan Write
But no guarantee that scan
of B’s 1st update can be used…
Why?
Art of Multiprocessor Programming
![Page 134: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/134.jpg)
134
Once is not Enough
time
≠26
24
12
Collect
26
24
12
Collect
Update
A
B Scan Write
Why can’t A steal B’s scan?
Because another update
might have interfered
before the scan
Update
Art of Multiprocessor Programming
![Page 135: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/135.jpg)
135
Someone Must Move Twice
time
Update
B
≠ ≠26
24
12
Collect
26
24
12
Collect
26
24
12
Collect
If we collect n times…some thread
must move twice (pigeonhole principle)
Art of Multiprocessor Programming
![Page 136: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/136.jpg)
136
Scan is Wait-free
scan
update
So some thread must
have had clean collect
scan
update
scan
At
most
n-1
depth
Art of Multiprocessor Programming
![Page 137: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/137.jpg)
137
Wait-Free Snapshot Label
public class SnapValue {
public int label;
public int value;
public int[] snap;
}
Art of Multiprocessor Programming
![Page 138: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/138.jpg)
138
Wait-Free Snapshot Label
public class SnapValue {
public int label;
public int value;
public int[] snap;
}
Counter incremented
with each snapshot
Art of Multiprocessor Programming
![Page 139: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/139.jpg)
139
Wait-Free Snapshot Label
public class SnapValue {
public int label;
public int value;
public int[] snap;
}
Actual value
Art of Multiprocessor Programming
![Page 140: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/140.jpg)
140
Wait-Free Snapshot Label
public class SnapValue {
public int label;
public int value;
public int[] snap;
}
most recent snapshot
Art of Multiprocessor Programming
![Page 141: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/141.jpg)
141
Wait-Free Snapshot Label
11011110101000101100…00
label
value
Last
snapshot
Art of Multiprocessor Programming
![Page 142: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/142.jpg)
142
Wait-free Update
public void update(int value) {
int i = Thread.myIndex();
int[] snap = this.scan();
SnapValue oldValue = r[i].read();
SnapValue newValue =
new SnapValue(oldValue.label+1,
value, snap);
r[i].write(newValue);
}
Art of Multiprocessor Programming
![Page 143: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/143.jpg)
143
Wait-free Scan
public void update(int value) {
int i = Thread.myIndex();
int[] snap = this.scan();
SnapValue oldValue = r[i].read();
SnapValue newValue =
new SnapValue(oldValue.label+1,
value, snap);
r[i].write(newValue);
}
Take scan
Art of Multiprocessor Programming
![Page 144: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/144.jpg)
144
Wait-free Scan
public void update(int value) {
int i = Thread.myIndex();
int[] snap = this.scan();
SnapValue oldValue = r[i].read();
SnapValue newValue =
new SnapValue(oldValue.label+1,
value, snap);
r[i].write(newValue);
}
Take scan
Label value with scan
Art of Multiprocessor Programming
![Page 145: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/145.jpg)
145
Wait-free Scan
public int[] scan() {
SnapValue[] oldCopy, newCopy;
boolean[] moved = new boolean[n];
oldCopy = collect();
collect: while (true) {
newCopy = collect();
for (int j = 0; j < n; j++) {
if (oldCopy[j].label != newCopy[j].label) {
…
}}
return getValues(newCopy);
}}}
Art of Multiprocessor Programming
![Page 146: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/146.jpg)
146
Wait-free Scan
public int[] scan() {
SnapValue[] oldCopy, newCopy;
boolean[] moved = new boolean[n];
oldCopy = collect();
collect: while (true) {
newCopy = collect();
for (int j = 0; j < n; j++) {
if (oldCopy[j].label != newCopy[j].label) {
…
}}
return getValues(newCopy);
}}}
Keep track of who moved
Art of Multiprocessor Programming
![Page 147: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/147.jpg)
147
Wait-free Scan
public int[] scan() {
SnapValue[] oldCopy, newCopy;
boolean[] moved = new boolean[n];
oldCopy = collect();
collect: while (true) {
newCopy = collect();
for (int j = 0; j < n; j++) {
if (oldCopy[j].label != newCopy[j].label) {
…
}}
return getValues(newCopy);
}}}
Repeated double collect
Art of Multiprocessor Programming
![Page 148: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/148.jpg)
148
Wait-free Scan
public int[] scan() {
SnapValue[] oldCopy, newCopy;
boolean[] moved = new boolean[n];
oldCopy = collect();
collect: while (true) {
newCopy = collect();
for (int j = 0; j < n; j++) {
if (oldCopy[j].label != newCopy[j].label) {
…
}}
return getValues(newCopy);
}}}
If mismatch detected…
Art of Multiprocessor Programming
![Page 149: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/149.jpg)
149
Mismatch Detected
if (oldCopy[j].label != newCopy[j].label) {
if (moved[j]) { // second move
return newCopy[j].snap;
} else {
moved[j] = true;
oldCopy = newCopy;
continue collect;
}}}
return getValues(newCopy);
}}}
Art of Multiprocessor Programming
![Page 150: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/150.jpg)
150
Mismatch Detected
if (oldCopy[j].label != newCopy[j].label) {
if (moved[j]) {
return newCopy[j].snap;
} else {
moved[j] = true;
oldCopy = newCopy;
continue collect;
}}}
return getValues(newCopy);
}}}
If thread moved twice,
just steal its second
snapshot
Art of Multiprocessor Programming
![Page 151: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/151.jpg)
151
Mismatch Detected
if (oldCopy[j].label != newCopy[j].label) {
if (moved[j]) { // second move
return newCopy[j].snap;
} else {
moved[j] = true;
oldCopy = newCopy;
continue collect;
}}}
return getValues(newCopy);
}}}
Remember that
thread moved
Art of Multiprocessor Programming
![Page 152: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/152.jpg)
152
Observations
• Uses unbounded counters
– can be replaced with 2 bits
• Assumes SWMR registers
– for labels
– can be extended to MRMW
Art of Multiprocessor Programming
![Page 153: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/153.jpg)
153
Summary
• We saw we could implement MRMW multi
valued snapshot objects
• From SRSW binary safe registers (simple
flipflops)
• But what is the next step to attempt with
read-write registers?
Art of Multiprocessor Programming
![Page 154: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/154.jpg)
154
Grand Challenge
• Snapshot means
– Write any one array element
– Read multiple array elements
Art of Multiprocessor Programming
![Page 155: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/155.jpg)
155
Grand Challenge
Writes to
0 and 1
Writes to
1 and 2
What about
atomic writes to
multiple
locations?
Write many and
snapshot
Art of Multiprocessor Programming
![Page 156: Companion slides for The Art of Multiprocessor …cs.brown.edu/courses/cs176/lectures/chapter_04.pdf2 Last Lecture • Defined concurrent objects using linearizability and sequential](https://reader035.fdocuments.in/reader035/viewer/2022071008/5fc648c28f591066951e258b/html5/thumbnails/156.jpg)
156
This work is licensed under a Creative Commons Attribution-
ShareAlike 2.5 License.
• You are free:
– to Share — to copy, distribute and transmit the work
– to Remix — to adapt the work
• Under the following conditions:
– Attribution. You must attribute the work to “The Art of
Multiprocessor Programming” (but not in any way that suggests that
the authors endorse you or your use of the work).
– Share Alike. If you alter, transform, or build upon this work, you
may distribute the resulting work only under the same, similar or a
compatible license.
• For any reuse or distribution, you must make clear to others the
license terms of this work. The best way to do this is with a link
to
– http://creativecommons.org/licenses/by-sa/3.0/.
• Any of the above conditions can be waived if you get permission
from the copyright holder.
• Nothing in this license impairs or restricts the author's moral
rights.
Art of Multiprocessor Programming