并发数据结构与多核编程 chapter_04

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Art of Multiprocessor Programming 18
Register Names
public class SafeBoolMRSWRegister implements Register<Boolean> { public boolean read() { … } public void write(boolean x) { … } }
5
Safe Register
Some valid value if reads and writes do overlap
$*&v
write(1001)
read(????)
0000
1001
Art of Multiprocessor Programming
1111
6
Regular Register
– Thread could halt in critical section – Build mutual exclusion from registers
Art of Multiprocessor Programming 24
Space of Registers
MRMW,MV,A
MRMW,MV,R
ຫໍສະໝຸດ Baidu
Simple Snapshot
• • • Put increasing labels on each entry Collect twice Problem: Scanner might not be collecting If both agree, a snapshot!
– We're done
Atomic Register
write(1001)
write(1010)
read(1010)
read(1001)
read(1010)
Art of Multiprocessor Programming
14
Weakest Register
Single writer
1
Single reader
0 1
Art of Multiprocessor Programming 22
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; }}
Single-Reader/Single-Writer Register
10011 01100 10011
Art of Multiprocessor Programming 1
Multi-Reader/Single-Writer Register
10011 01100 10011
Art of Multiprocessor Programming 2
Art of Multiprocessor Programming
21
Regular Boolean MRSW from Safe Boolean MRSW
public 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(); }}
MRSW
SRSW
M-valued Boolean
Safe Regular Atomic Snapshot
17
Art of Multiprocessor Programming
Road Map
• • • • • • • SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot
28
Atomic Snapshot
• Collect
– Read values one at a time
• Problem
– Incompatible concurrent collects – Result not linearizable
Art of Multiprocessor Programming
Safe Boolean register
Art of Multiprocessor Programming 15
Results
• From SRSW safe Boolean register
– All the other registers – Mutual exclusion
• But not everything!
Regular or Not?
write(0) read(1)
write(1) read(0)
Art of Multiprocessor Programming
8
Regular or Not?
write(0) read(1)
write(1) read(0)
Overlap: returns new value
Collect 1 x y z w r z x Art of Multiprocessor Programming
Collect 2
x
• Otherwise,
– Try again
=
y z w r z x 31
Claim: We Must Use Labels But scanner sees x and z
m-valued Boolean
Safe Regular Atomic
Art of Multiprocessor Programming
4
Safe Register
OK if reads and writes don't overlap
read(1001)
write(1001)
Art of Multiprocessor Programming
update
scan
Art of Multiprocessor Programming
26
Atomic Snapshot
• Array of MRSW atomic registers • Take instantaneous snapshot of all • Generalizes to MRMW registers …
– Consensus hierarchy
Foundations of the field
The really cool stuff …
Art of Multiprocessor Programming 16
From Safe SRSW Boolean to Atomic Snapshots
MRMW
Art of Multiprocessor Programming
27
Snapshot Interface
public interface Snapshot { public int update(int v); public int[] scan(); }
Art of Multiprocessor Programming
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 7
Multi-Reader/Multi-Writer Register
mumble
10011
mumble
10011
01010
mumble
10011 11011
Art of Multiprocessor Programming 3
Register Space
Multi-Reader Multi-Writer Multi-Reader Single-Writer Single-Reader Single-Writer MRMW MRSW SRSW
Art of Multiprocessor Programming
19
Safe Boolean MRSW from Safe Boolean SRSW
0 0 0 0 writer 0 zzz 0 0 0 readers
0
0
20
Art of Multiprocessor Programming
Safe Boolean MRSW from Safe Boolean SRSW
Art of Multiprocessor Programming
23
Definition
An object implementation is wait-free if every method call completes in a finite number of steps
No mutual exclusion
Art of Multiprocessor Programming
9
Regular or Not?
write(0)
write(1) read(0)
Overlap: returns old value
Art of Multiprocessor Programming
10
Regular or Not?
together!
x Scanner x Updater z Updater time w z y x y z x z
public 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(); }}
MRSW,MV,A
MRSW,MV,R
MRSW,B,A
SRSW,MV,A
MRSW,B,R
SRSW,MV,R
SRSW,B,A
MRSW,B,S
SRSW,B,R
MRSW,MV,S SRSW,MV,S
SRSW,B,S
Art of Multiprocessor Programming 25
Atomic Snapshot
write(0) read(1)
write(1) read(0)
Art of Multiprocessor Programming
11
Regular ≠ Linearizable
write(0) read(1)
write(1) read(0)
can't explain this! write(1) already happened
29
Clean Collects
• Clean Collect
– Collect during which nothing changed – Can we make it happen? – Can we detect it?
Art of Multiprocessor Programming
30
Art of Multiprocessor Programming 12
Atomic Register
write(1001)
write(1010)
read(1010)
read(1001)
read(1010)
Linearizable to sequential safe register
Art of Multiprocessor Programming 13
相关文档
最新文档