Download presentation
Presentation is loading. Please wait.
Published byNeil Holmes Modified over 9 years ago
1
Foundations of Shared Memory Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit
2
© 2007 Herlihy & Shavit2 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
3
© 2007 Herlihy & Shavit3 Fundamentals What is the weakest form of communication that supports mutual exclusion? What is the weakest shared object that allows shared-memory computation?
4
© 2007 Herlihy & Shavit4 Alan Turing Helped us understand what is and is not computable on a sequential machine. Still best model available
5
© 2007 Herlihy & Shavit5 011010 1 Turing Machine Reads and Writes Infinite tape Finite State Controller
6
© 2007 Herlihy & Shavit6 Turing Computability Mathematical model of computation What is (and is not) computable Efficiency (mostly) irrelevant 011010 1
7
© 2007 Herlihy & Shavit7 Shared-Memory Computability? Mathematical model of concurrent computation What is (and is not) concurrently computable Efficiency (mostly) irrelevant 10011 Shared Memory
8
© 2007 Herlihy & Shavit8 Foundations of Shared Memory To understand modern multiprocessors we need to ask some basic questions …
9
© 2007 Herlihy & Shavit9 Foundations of Shared Memory To understand modern multiprocessors we need to ask some basic questions … What is the weakest useful form of shared memory?
10
© 2007 Herlihy & Shavit10 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?
11
© 2007 Herlihy & Shavit11 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? What can’t it do?
12
© 2007 Herlihy & Shavit12 Register 1001 1 Holds a (binary) value * * A memory location: name is historical
13
© 2007 Herlihy & Shavit13 Register Can be read 1001 1
14
© 2007 Herlihy & Shavit14 Register Can be written 1001 1 0110 0
15
© 2007 Herlihy & Shavit15 public interface Register { public T read(); public void write(T v); } Registers
16
© 2007 Herlihy & Shavit16 public interface Register { public T read(); public void write(T v); } Registers Type of register (usually Boolean or m-bit Integer)
17
© 2007 Herlihy & Shavit17 1001 1 Single-Reader/Single-Writer Register 0110 0 1001 1
18
© 2007 Herlihy & Shavit18 1001 1 Multi-Reader/Single-Writer Register 0110 0 1001 1
19
© 2007 Herlihy & Shavit19 mumble 11011 Multi-Reader/Multi-Writer Register mumble 1001 1 01010
20
© 2007 Herlihy & Shavit20 Jargon Watch SRSW –Single-reader single-writer MRSW –Multi-reader single-writer MRMW –Multi-reader multi-writer
21
© 2007 Herlihy & Shavit21 Safe Register write(1001) read(1001) OK if reads and writes don’t overlap (2)
22
© 2007 Herlihy & Shavit22 Safe Register write(1001) Some valid value if reads and writes do overlap read(????) 0000 1001 1111 $*&v
23
© 2007 Herlihy & Shavit23 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
24
© 2007 Herlihy & Shavit24 Regular or Not? write(0) read(1) write(1) read(0)
25
© 2007 Herlihy & Shavit25 Regular or Not? write(0) read(1) write(1) read(0) Overlap: returns new value
26
© 2007 Herlihy & Shavit26 Regular or Not? write(0)write(1) read(0) Overlap: returns old value
27
© 2007 Herlihy & Shavit27 Regular or Not? write(0) read(1) write(1) read(0) regular
28
© 2007 Herlihy & Shavit28 Regular ≠ Atomic write(0) read(1) write(1) read(0) write(1) already happened explain this!
29
© 2007 Herlihy & Shavit29 Atomic Register write(1001) read(1001) Linearizable to sequential safe register write(1010) read(1010)
30
© 2007 Herlihy & Shavit30 Atomic Register write(1001) read(1001) write(1010) read(1010)
31
© 2007 Herlihy & Shavit31 Register Space MRMW MRSW SRSW Safe Regular Atomic M-valued Boolean
32
© 2007 Herlihy & Shavit32 Weakest Register 1 0 1 1 Single readerSingle writer Safe Boolean register
33
© 2007 Herlihy & Shavit33 Weakest Register Single readerSingle writer Get correct reading if not during state transition flipflop 010 010
34
© 2007 Herlihy & Shavit34 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 … (2)
35
© 2007 Herlihy & Shavit35 Locking within Registers Not interesting to rely on mutual exclusion in register constructions We want registers to implement mutual exclusion! No fun to use mutual exclusion to implement itself!
36
© 2007 Herlihy & Shavit36 Wait-Free Implementations 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
37
© 2007 Herlihy & Shavit37 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot
38
© 2007 Herlihy & Shavit38 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Next
39
© 2007 Herlihy & Shavit39 public class SafeBoolMRSWRegister implements Register { public boolean read() { … } public void write(boolean x) { … } } (3) Register Names
40
© 2007 Herlihy & Shavit40 public class SafeBoolMRSWRegister implements Register { public boolean read() { … } public void write(boolean x) { … } } (3) Register Names property
41
© 2007 Herlihy & Shavit41 public class SafeBoolMRSWRegister implements Register { public boolean read() { … } public void write(boolean x) { … } } (3) Register Names property Size matters
42
© 2007 Herlihy & Shavit42 public class SafeBoolMRSWRegister implements Register { public boolean read() { … } public void write(boolean x) { … } } (3) Register Names property type How many readers & writers?
43
© 2007 Herlihy & Shavit43 Safe Boolean MRSW from Safe Boolean SRSW public class SafeBoolMRSWRegister implements Register { 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(); }} (2)
44
© 2007 Herlihy & Shavit44 Safe Boolean MRSW from Safe Boolean SRSW public 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(); }} (2) Each thread has own safe SRSW register
45
© 2007 Herlihy & Shavit45 Safe Boolean MRSW from Safe Boolean SRSW public 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(); }} (2) write method
46
© 2007 Herlihy & Shavit46 Safe Boolean MRSW from Safe Boolean SRSW public 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(); }} (2) Write each thread’s register one at a time
47
© 2007 Herlihy & Shavit47 Safe Boolean MRSW from Safe Boolean SRSW public 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(); }} (2) read method
48
© 2007 Herlihy & Shavit48 Safe Boolean MRSW from Safe Boolean SRSW public 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(); }} (2) Read my own register
49
© 2007 Herlihy & Shavit49 Safe Boolean MRSW from Safe Boolean SRSW 1 1 1 1 1 1 0 or 1 0 0 0 0 0 0 0 0 1 1
50
© 2007 Herlihy & Shavit50 1000 Q: Safe Multi-Valued MRSW Safe Multi-Valued SRSW? 1 1011 1 Any value in range 0 0 Yes, it works!Yes, it works! 1011
51
© 2007 Herlihy & Shavit51 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Questions?
52
© 2007 Herlihy & Shavit52 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Next
53
© 2007 Herlihy & Shavit53 Regular Boolean MRSW from Safe Boolean MRSW 1 1 0 0 0 0 Regular: if it changed, OK to read 0 or 1 Safe register can return 0 or 1 even if the same value is written Regular: But only old value if not changed
54
© 2007 Herlihy & Shavit54 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { 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(); }} (2)
55
© 2007 Herlihy & Shavit55 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { 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(); }} (2) Last bit this thread wrote (OK, we’re cheating here on Java syntax)
56
© 2007 Herlihy & Shavit56 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { 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(); }} (2) Actual value
57
© 2007 Herlihy & Shavit57 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { 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(); }} (2) Is new value different from last value I wrote?
58
© 2007 Herlihy & Shavit58 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { 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(); }} (2) If so, change it (otherwise don’t!)
59
© 2007 Herlihy & Shavit59 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { 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(); }} (2) Overlap? No Overlap? No problem either Boolean value works
60
© 2007 Herlihy & Shavit60 Regular Multi-Valued MRSW from Safe Multi-Valued MRSW? 1 1 0101 Multi- valued Regular register can return only old or new when value changes! Safe register can return value in range other than old or new when value changes No, it does not work!
61
© 2007 Herlihy & Shavit61 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Questions?
62
© 2007 Herlihy & Shavit62 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Next
63
© 2007 Herlihy & Shavit63 MRSW Regular M-valued from MRSW Regular Boolean public 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; }}
64
© 2007 Herlihy & Shavit64 MRSW Regular M-valued from MRSW Regular Boolean public 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; }} Unary representation: bit[i] means value i
65
© 2007 Herlihy & Shavit65 MRSW Regular M-valued from MRSW Regular Boolean public class RegMRSWRegisterimplements 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; }} (1) Set bit x
66
© 2007 Herlihy & Shavit66 MRSW Regular M-valued from MRSW Regular Boolean public class RegMRSWRegisterimplements 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; }} (1) Clear bits from higher to lower
67
© 2007 Herlihy & Shavit67 MRSW Regular M-valued from MRSW Regular Boolean public class RegMRSWRegisterimplements 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; }} (1) Scan from lower to higher & return first bit set
68
© 2007 Herlihy & Shavit68 Writing M-Valued 0 1 2 3 4 5 6 7 110000 Write 5 Initially 0
69
© 2007 Herlihy & Shavit69 Writing M-Valued 0 1 2 3 4 5 6 7 110000 Write 5 5 0
70
© 2007 Herlihy & Shavit70 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Questions?Questions?
71
© 2007 Herlihy & Shavit71 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot
72
© 2007 Herlihy & Shavit72 Road Map (Slight Detour) SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot SWSW Atomic
73
Concurrent Reading © 2007 Herlihy & Shavit73 SRSW Atomic From SRSW Regular 1234 Regular writer Regular reader 1234 5678 Instead of 5678… When is this a problem?
74
© 2007 Herlihy & Shavit74 SRSW Atomic From SRSW Regular 1234 Regular writer Regular reader 5678 time Reg write(5678) Reg read(5678) Initially 1234 Same as Atomic
75
© 2007 Herlihy & Shavit75 SRSW Atomic From SRSW Regular 1234 Regular writer Regular reader 1234 5678 Instead of 5678… time Reg write(5678) Reg read(1234) Initially 1234 Same as Atomic
76
© 2007 Herlihy & Shavit76 SRSW Atomic From SRSW Regular 1234 Regular writer Regular reader 1234 5678 Instead of 5678… time Reg write(5678) Reg read(1234) Initially 1234 Reg read(5678) not Atomic! Write 5678 happened
77
© 2007 Herlihy & Shavit77 Timestamped Values Writer writes value and stamp together Reader saves last read (value,stamp) and returns new value only if higher stamp 1234 1:45 5678 2:00 5678 2:00
78
© 2007 Herlihy & Shavit78 SRSW Atomic From SRSW Regular writer reader 1:45 1234 Less than 2:00 5678 So stick with 5678 time Reg write(2:00 5678) read(1:45 1234) 1:45 1234 read(2:00 5678) 1234 1:45 56785678 old = 2:00 > 5678 2:00 Same as Atomic
79
© 2007 Herlihy & Shavit79 Atomic Single Reader to Atomic Multi-Reader 1:451234 1:451234 1:4512341:451234 stampvalue One per reader
80
© 2007 Herlihy & Shavit80 Another Scenario 1:451234 2:005678 1:4512341:451234 stampvalue Writer starts write…
81
© 2007 Herlihy & Shavit81 Another Scenario 1:451234 2:005678 1:451234 1:45 12341234 stampvalue reader reads 2:00, 56782:00, 5678 zzz…zzz… 1:45 12341:45 1234 later reader Yellow was completely after blue but read earlier value…not linearizable!Yellow was completely after blue but read earlier value…not linearizable!
82
© 2007 Herlihy & Shavit82 Multi-Reader Redux 1:4512341:451234 1:451:45 1:451234 1:451:45 1:451:45 One per threadOne per thread 1:451234 1:451:45 1:45 12341234 123 1 2 3
83
© 2007 Herlihy & Shavit83 Multi-Reader Redux 1:4512341:4512341:4512341:4512341:4512341:4512341:4512341:451234 Writer writes column… 2:0056782:0056782:0056781:451234 reader reads row 2:00, 5678 1 123 1 2 3 2
84
© 2007 Herlihy & Shavit84 Multi-Reader Redux 1:4512341:4512341:4512341:4512341:4512341:4512341:451234 1:451:45 2:0056781:451234 reader writes column to notify others of what it read 1 123 1 2 3 2 2:0056782:0056782:0056782:005678 zzz…after second write 2:00, 5678 Yellow reader will read new value in column written by earlier Blue reader
85
© 2007 Herlihy & Shavit85 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
86
© 2007 Herlihy & Shavit86 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
87
© 2007 Herlihy & Shavit87 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Next
88
© 2007 Herlihy & Shavit88 Multi-Writer Atomic From Multi-Reader Atomic 1:451234 1:451234 1:4512341:451234 stampvalue Readers read all and take max (Lexicographic like Bakery) Each writer reads all then writes Max+1 to its register 2:005678 2:15XYZW Max is 2:15, return XYZW
89
© 2007 Herlihy & Shavit89 Atomic Execution Means its Linearizable time write(1) time (4) Read(max= 2)write(4) write(2) write(3)Read(max = 3) Read (max = 1) write(2)Read(max = 4)
90
© 2007 Herlihy & Shavit90 Linearization Points time write(1) time (4) Read(max= 2)write(4) write(2) write(3)Read(max = 3) Read (max = 1) write(2)Read(max = 4)
91
© 2007 Herlihy & Shavit91 Linearization Points time write(1) time Look at Writes First (4) write(4) write(2) write(3) write(2)
92
© 2007 Herlihy & Shavit92 Linearization Points time write(1) time (4) write(4) write(2) write(3) write(2) Order writes by TimeStamp
93
© 2007 Herlihy & Shavit93 Linearization Points time write(1) time (4) 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
94
© 2007 Herlihy & Shavit94 Linearization Points time write(1) time (4) 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
95
© 2007 Herlihy & Shavit95 Linearization Points time write(1) time (4) 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)!
96
© 2007 Herlihy & Shavit96 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Questions?
97
© 2007 Herlihy & Shavit97 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Next
98
© 2007 Herlihy & Shavit98 Atomic Snapshot update scan
99
© 2007 Herlihy & Shavit99 Atomic Snapshot Array of SWMR atomic registers Take instantaneous snapshot of all Generalizes to MRMW registers …
100
© 2007 Herlihy & Shavit100 Snapshot Interface public interface Snapshot { public int update(int v); public int[] scan(); } (2)
101
© 2007 Herlihy & Shavit101 Snapshot Interface public interface Snapshot { public int update(int v); public int[] scan(); } Thread i writes v to its register (2)
102
© 2007 Herlihy & Shavit102 Snapshot Interface public interface Snapshot { public int update(int v); public int[] scan(); } Instantaneous snapshot of all theads’ registers (2)
103
© 2007 Herlihy & Shavit103 Atomic Snapshot Collect –Read values one at a time Problem –Incompatible concurrent collects –Result not linearizable
104
© 2007 Herlihy & Shavit104 Clean Collects Clean Collect –Collect during which nothing changed –Can we make it happen? –Can we detect it?
105
© 2007 Herlihy & Shavit105 Simple Snapshot Put increasing labels on each entry Collect twice If both agree, –We’re done Otherwise, –Try again 1 22 1 7 13 18 12 = Collect2 Collect1 1 22 1 7 13 18 12
106
© 2007 Herlihy & Shavit106 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); } (1)
107
© 2007 Herlihy & Shavit107 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); } (1) One single-writer register per thread
108
© 2007 Herlihy & Shavit108 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); } (1) Write each time with higher label
109
© 2007 Herlihy & Shavit109 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; } (1)
110
© 2007 Herlihy & Shavit110 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; } (1) Just read each register into array
111
© 2007 Herlihy & Shavit111 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); }}} (1)
112
© 2007 Herlihy & Shavit112 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); }}} (1) Collect once
113
© 2007 Herlihy & Shavit113 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);}}}public int[] scan() { LabeledValue[] oldCopy, newCopy; oldCopy = collect(); collect: while (true) { newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; continue collect;}} return getValues(newCopy);}}}} (1) Collect once Collect twice
114
© 2007 Herlihy & Shavit114 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); }}} (1) Collect once Collect twice On mismatch, try again
115
© 2007 Herlihy & Shavit115 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); }}} (1) Collect once Collect twice On match, return values
116
© 2007 Herlihy & Shavit116 Simple Snapshot Linearizable Update is wait-free –No unbounded loops But Scan can starve –If interrupted by concurrent update
117
© 2007 Herlihy & Shavit117 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
118
© 2007 Herlihy & Shavit118 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
119
© 2007 Herlihy & Shavit119 Wait-free Snapshot time ≠ ≠ 26 24 12 Collect 26 24 12 Collect 26 24 12 Collect Update A B
120
© 2007 Herlihy & Shavit120 Wait-free Snapshot time ≠ ≠ 26 24 12 Collect 26 24 12 Collect 26 24 12 Collect A B ScanWrite Update
121
© 2007 Herlihy & Shavit121 Wait-free Snapshot time ≠ ≠ 26 24 12 Collect 26 24 12 Collect 26 24 12 Collect A B ScanWrite Update Scan Write B’s 1 st update must have written during 1 st 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
122
© 2007 Herlihy & Shavit122 Wait-free Snapshot time ≠ ≠ 26 24 12 Collect 26 24 12 Collect 26 24 12 Collect A B ScanWrite Scan Write But no guarantee that scan of B’s 1 st update can be used… Why?
123
© 2007 Herlihy & Shavit123 Once is not Enough time ≠ 26 24 12 Collect 26 24 12 Collect Update A B ScanWrite Why can’t A steal result of B’s scan Because another update might have interfered before the scan Update
124
© 2007 Herlihy & Shavit124 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 (Pigeon hole)
125
© 2007 Herlihy & Shavit125 Scan is Wait-free scan update So some thread must have had clean collect scan update scan At most n-1 depth
126
© 2007 Herlihy & Shavit126 Wait-Free Snapshot Label public class SnapValue { public int label; public int value; public int[] snap; } (2)
127
© 2007 Herlihy & Shavit127 Wait-Free Snapshot Label public class SnapValue { public int label; public int value; public int[] snap; } (2) Counter incremented with each snapshot
128
© 2007 Herlihy & Shavit128 Wait-Free Snapshot Label public class SnapValue { public int label; public int value; public int[] snap; } (2) Actual value
129
© 2007 Herlihy & Shavit129 Wait-Free Snapshot Label public class SnapValue { public int label; public int value; public int[] snap; } (2) most recent snapshot
130
© 2007 Herlihy & Shavit130 Wait-Free Snapshot Label 11011110101000101100…00 label value Last snapshot (3)
131
© 2007 Herlihy & Shavit131 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); } (2)
132
© 2007 Herlihy & Shavit132 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); } 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); } (2) Take scan
133
© 2007 Herlihy & Shavit133 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); } (2) Take scan Label value with scan
134
© 2007 Herlihy & Shavit134 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); }}} (2)
135
© 2007 Herlihy & Shavit135 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); }}} (2) Keep track of who moved
136
© 2007 Herlihy & Shavit136 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); }}} (2) Repeated double collect
137
© 2007 Herlihy & Shavit137 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); }}} (2) If mismatch detected…lets expand here…
138
© 2007 Herlihy & Shavit138 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); }}} (2)
139
© 2007 Herlihy & Shavit139 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 (2)
140
© 2007 Herlihy & Shavit140 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); }}} (2) Remember that thread moved
141
© 2007 Herlihy & Shavit141 Observations Uses unbounded counters –can be replaced with 2 bits Assumes SWMR registers –for labels –can be extended to MRMW
142
© 2007 Herlihy & Shavit142 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?
143
© 2007 Herlihy & Shavit143 Grand Challenge Snapshot means –Write any one array element –Read multiple array elements
144
© 2007 Herlihy & Shavit144 Grand Challenge Writes to 0 and 1 Writes to 1 and 2 What about atomic writes to multiple locations? Write many and snapshot
145
Art of Multiprocessor Programming 145 This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.Creative Commons Attribution-ShareAlike 2.5 License Y o u a r e f r e e : –to Share — to copy, distribute and transmit the work –to Remix — to adapt the work U n d e r t h e f o l l o w i n g c o n d i t i o n s : –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. F o r a n y r e u s e o r d i s t r i b u t i o n, y o u m u s t m a k e c l e a r t o o t h e r s t h e l i c e n s e t e r m s o f t h i s w o r k. T h e b e s t w a y t o d o t h i s i s w i t h a l i n k t o –http://creativecommons.org/licenses/by-sa/3.0/. A n y o f t h e a b o v e c o n d i t i o n s c a n b e w a i v e d i f y o u g e t p e r m i s s i o n f r o m t h e c o p y r i g h t h o l d e r. N o t h i n g i n t h i s l i c e n s e i m p a i r s o r r e s t r i c t s t h e a u t h o r ' s m o r a l r i g h t s.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.