Вы находитесь на странице: 1из 156

Foundations of Shared Memory

Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit
Art of Multiprocessor Programming

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

Alan Turing

Showed what is and is not computable on a sequential machine. Still best model there is.

Art of Multiprocessor Programming

Turing Computability

0 1 1 0 1 0

Mathematical model of computation What is (and is not) computable Efficiency (mostly) irrelevant
Art of Multiprocessor Programming 5

Shared-Memory Computability?
Shared Memory

10011

Mathematical model of concurrent computation What is (and is not) concurrently computable Efficiency (mostly) irrelevant
6

Art of Multiprocessor Programming

Foundations of Shared Memory


To understand modern multiprocessors we need to ask some basic questions

Art of Multiprocessor Programming

Foundations of Shared Memory


To understand modern What is the weakest useful form of multiprocessors we need to ask shared memory? some basic questions

Art of Multiprocessor Programming

Foundations of Shared Memory


To understand modern What is the weakest useful form of multiprocessors to ask What we canneed it do? shared memory? some basic questions

Art of Multiprocessor Programming

Foundations of Shared Memory


To understand modern What is the weakest useful form of multiprocessors to ask What we canneed it do? shared memory? What cant it do? some basic questions

Art of Multiprocessor Programming

10

Register *
Holds a (binary) value

10011
* A memory location: name is historical
Art of Multiprocessor Programming 11

Register 10011

Can be read

10011
Art of Multiprocessor Programming 12

Register
Can be written

01100

10011
13

Art of Multiprocessor Programming

Registers
public interface Register<T> { public T read(); public void write(T v); }

Art of Multiprocessor Programming

14

Registers
public interface Register<T> { public T read(); public void write(T v); }

Type of register (usually Boolean or m-bit Integer)


Art of Multiprocessor Programming 15

Single-Reader/Single-Writer Register

10011 01100 10011


16

Art of Multiprocessor Programming

Multi-Reader/Single-Writer Register

10011
01100

10011
17

Art of Multiprocessor Programming

Multi-Reader/Multi-Writer Register
mumble

10011
mumble

10011

01010

mumble

10011 11011
Art of Multiprocessor Programming 18

Jargon Watch
SRSW
Single-reader single-writer

MRSW
Multi-reader single-writer

MRMW
Multi-reader multi-writer

Art of Multiprocessor Programming

19

Safe Register
OK if reads and writes dont overlap
read(1001)

write(1001)

Art of Multiprocessor Programming

20

Safe Register
Some valid value if reads and writes do overlap
$*&v 1111
21

write(1001)

read(????)

0000

1001
Art of Multiprocessor Programming

Regular Register
write(0) write(1)

read(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 22

Regular or Not?
write(0) write(1)

read(1)

read(0)

Art of Multiprocessor Programming

23

Regular or Not?
write(0) write(1)

read(1)

read(0)

Overlap: returns new value

Art of Multiprocessor Programming

24

Regular or Not?
write(0) write(1)

read(0)

Overlap: returns old value

Art of Multiprocessor Programming

25

Regular or Not?
write(0) write(1)

read(1)

read(0)

Art of Multiprocessor Programming

26

Regular Linearizable
write(0) write(1)

read(1)

read(0)

explain this! write(1) already happened


Art of Multiprocessor Programming 27

Atomic Register
write(1001) write(1010) read(1010)

read(1001)

read(1010)

Linearizable to sequential safe register


Art of Multiprocessor Programming 28

Atomic Register
write(1001) write(1010) read(1010)

read(1001)

read(1010)

Art of Multiprocessor Programming

29

Register Space

MRMW
MRSW M-valued Boolean

SRSW
Safe

Regular Atomic
30

Art of Multiprocessor Programming

Weakest Register
Single writer

1 0 1

Single reader

Safe Boolean register


Art of Multiprocessor Programming 31

Weakest Register
Single writer
0 1 0

Single reader
0 1 0

flipflop

Get correct reading if not during state transition


Art of Multiprocessor Programming 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 33

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!

Art of Multiprocessor Programming

34

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
Art of Multiprocessor Programming 35

From Safe SRSW Boolean to Atomic Snapshots


MRMW MRSW SRSW Safe

M-valued Boolean

Regular Atomic Snapshot


36

Art of Multiprocessor Programming

Road Map
SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot
Art of Multiprocessor Programming 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

38

Register Names
public class SafeBoolMRSWRegister implements Register<Boolean> { public boolean read() { } public void write(boolean x) { } }

Art of Multiprocessor Programming

39

Register Names
public class SafeBoolMRSWRegister implements Register<Boolean> { public boolean read() { } public void write(boolean x) { } }

property

Art of Multiprocessor Programming

40

Register Names
public class SafeBoolMRSWRegister implements Register<Boolean> { public boolean read() { } public void write(boolean x) { } }

property

Size matters
Art of Multiprocessor Programming 41

Register Names
public class SafeBoolMRSWRegister implements Register<Boolean> { public boolean read() { } public void write(boolean x) { } }

property

type
(3)

How many readers & writers?


42

Art of Multiprocessor Programming

Safe Boolean MRSW from Safe Boolean SRSW


0 0 0 0 readers

zzz

0 0

writer
0 0

Art of Multiprocessor Programming

43

Safe Boolean MRSW from Safe Boolean SRSW


Lets Write 1!

0 0 0 0

0
44

Art of Multiprocessor Programming

Safe Boolean MRSW from Safe Boolean SRSW


0 or 1 0 0 0

1 0

0
45

Art of Multiprocessor Programming

Safe Boolean MRSW from Safe Boolean SRSW


1 0 0 1 1 0 or 1

0
46

Art of Multiprocessor Programming

Safe Boolean MRSW from Safe Boolean SRSW


1 0 0 1 1 0 or 1 0 0 1
47

Art of Multiprocessor Programming

Safe Boolean MRSW from Safe Boolean SRSW


1 1 1

Whew!
1

1 1

1
48

Art of Multiprocessor Programming

Safe Boolean MRSW from Safe Boolean SRSW


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(); }}
49

Art of Multiprocessor Programming

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(); Each thread }}

has own safe SRSW register


50

Art of Multiprocessor Programming

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(); write method }}
51

Art of Multiprocessor Programming

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() { Write each int i = ThreadID.get(); threads register return r[i].read(); one at a time }}
52

Art of Multiprocessor Programming

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); read } public boolean read() { int i = ThreadID.get(); return r[i].read(); }}

method

Art of Multiprocessor Programming

53

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(); }}

Read my own register


54

Art of Multiprocessor Programming

Q: Safe Multi-Valued MRSW Safe Multi-Valued SRSW?


1 0 1011 1

1000 0
1000

1011

Any value in range


1000
55

Art of Multiprocessor Programming

Road Map
SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic Questions? MRMW atomic Atomic snapshot
Art of Multiprocessor Programming 56

Road Map
SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot
Art of Multiprocessor Programming

Next

57

Regular Boolean MRSW from Safe Boolean MRSW


0 1

1 0

0 1 0 1

Art of Multiprocessor Programming

58

Regular Boolean MRSW from Safe Boolean MRSW


0 1 Regular cannot return 1

0 0

0 1 0 1

Art of Multiprocessor Programming

59

Regular Boolean MRSW from Safe Boolean MRSW


0

0 Last written: 0 0

0 0

Art of Multiprocessor Programming

60

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(); }}
Art of Multiprocessor Programming 61

Regular Boolean MRSW from Safe Boolean MRSW


public class RegBoolMRSWRegister implements Register<Boolean> { threadLocal boolean old; private SafeBoolMRSWRegister value; public void write(boolean x) { if (old != x) { value.write(x); old = x; Last bit this thread (made-up syntax) }} public boolean read() { return value.read(); }}
Art of Multiprocessor Programming

wrote

62

Regular Boolean MRSW from Safe Boolean MRSW


public 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 63

Regular Boolean MRSW from Safe Boolean MRSW


public class RegBoolMRSWRegister implements Register<Boolean> { threadLocal boolean old; private SafeBoolMRSWRegister value; public void write(boolean x) { if (old != x) { value.write(x); Is new value different old = x; from last value I wrote? }} public boolean read() { return value.read(); }}
Art of Multiprocessor Programming 64

Regular Boolean MRSW from Safe Boolean MRSW


public 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() { If so, change it return value.read(); }} (otherwise dont!)
Art of Multiprocessor Programming 65

Regular Boolean MRSW from Safe Boolean MRSW


public class RegBoolMRSWRegister implements Register<Boolean>{ threadLocal boolean old; private SafeBoolMRSWRegister value; public void write(boolean x) { if (old != x) { Overlap? No Overlap? value.write(x); No problem old = x; either Boolean value works }} public boolean read() { return value.read(); }}
Art of Multiprocessor Programming 66

Regular Multi-Valued MRSW from Safe Multi-Valued MRSW?


Safe register can return value in range other than old or new when value changes 0101 0101 0101
Multi-valued Regular register can return only old or new when value changes!
67

Art of Multiprocessor Programming

Road Map
SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Questions? Atomic snapshot
Art of Multiprocessor Programming 68

Road Map
SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot
Art of Multiprocessor Programming

Next

69

Writing M-Valued
Unary representation: bit[i] means value i

1 0 0 0 00 0
Initially 0

01234567

Art of Multiprocessor Programming

70

Writing M-Valued
Write 5

1 0 0 00 01234567

Art of Multiprocessor Programming

71

Writing M-Valued
Write 5

Initially 0

0 1 0 0 0 1 01234567

Art of Multiprocessor Programming

72

Writing M-Valued
Write 5

5 01234567

0 1 0 0 0 1

Art of Multiprocessor Programming

73

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; }}

Art of Multiprocessor Programming

74

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); } Unary representation:

bit[i] public int read() { for (int i=0; i < M; i++) if (this.bit[i].read()) return i; }}

means value i

Art of Multiprocessor Programming

75

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; }}

Set bit x

Art of Multiprocessor Programming

76

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; }}

Clear bits from higher to lower

Art of Multiprocessor Programming

77

MRSW Regular M-valued from MRSW Regular Boolean


public class RegMRSWRegisterimplements Register { RegBoolMRSWRegister[m] bit; public void write(int x) { Scan from this.bit[x].write(true); to higher & for (int i=x-1; i>=0; i--) this.bit[i].write(false); first bit } public int read() { for (int i=0; i < M; i++) if (this.bit[i].read()) return i; }}

lower return set

Art of Multiprocessor Programming

78

Road Map
SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Questions? Atomic snapshot
Art of Multiprocessor Programming 79

Road Map
SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot
Art of Multiprocessor Programming 80

Road Map (Slight Detour)


SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular SRSW Atomic MRSW atomic MRMW atomic Atomic snapshot
Art of Multiprocessor Programming 81

SRSW Atomic From SRSW Regular


Regular writer Regular reader

1234 5678

1234

Concurrent Reading When is this a problem?


Art of Multiprocessor Programming

Instead of 5678

82

SRSW Atomic From SRSW Regular


Regular writer Regular reader

1234 5678
Initially 1234

5678

Reg write(5678) Reg read(5678)

time
Art of Multiprocessor Programming 83

SRSW Atomic From SRSW Regular


Regular writer Regular reader

1234 5678
Initially 1234

1234

Instead of 5678
Reg write(5678) Reg read(1234)

time
Art of Multiprocessor Programming 84

SRSW Atomic From SRSW Regular


Regular writer Regular reader

1234 5678
Initially 1234

1234

Instead of 5678
Reg write(5678) Reg read(1234)

Reg read(5678)

Write time 5678 happened

Art of Multiprocessor Programming

85

Timestamped Values

1234 1:45 5678 2:00 Writer writes value and stamp together

2:00

5678

Reader saves last read (value,stamp) and returns new value only if higher stamp

Art of Multiprocessor Programming

86

writer

SRSW Atomic From SRSW Regular

reader 1:45 2:00 1:45 1234 1234 5678


1:45 1234 Less than 2:00 5678 So stick with 5678

Reg write(2:00 5678) read(2:00 5678)

read(1:45 1234)

time

old = 2:00 5678


Art of Multiprocessor Programming 87

>

Atomic Single Reader to Atomic Multi-Reader


stamp 1:45 1:45 1:45 1:45 value 1234 1234 1234 1234

One per reader

Art of Multiprocessor Programming

88

Another Scenario
Writer starts write stamp 2:00 1:45 1:45 1:45 value 5678 1234 1234 1234

Art of Multiprocessor Programming

89

Another Scenario
zzz stamp 2:00 1:45 1:45 1:45 value 5678 1234 1234 1234 2:00, 5678

reader reads
later reader

1:45 1234

Yellow was completely after Blue but read earlier valuenot linearizable!
Art of Multiprocessor Programming

90

Multi-Reader Redux
One per thread
1 1:45 1:45 1:45 1234 1234 1234 1:45 1:45 1:45 2 1234 1234 1234 1:45 1:45 1:45 3 1234 1234 1234 1 2 3

Art of Multiprocessor Programming

91

Writer writes column

Multi-Reader Redux

2:00, 5678

1 1 1:45 2:00 1:45 2:00 2:00 1:45 1234 5678 1234 5678 5678 1234 1:45 1:45 1:45 2 1234 1234 1234

reader reads row


3 1:45 1:45 1:45

2 1 2 3

1234 1234 1234

Art of Multiprocessor Programming

92

zzzafter second write

Multi-Reader Redux

2:00, 5678

reader writes column to 1 notify others of what it 2 read 1 2 3 1:45 1234 1:45 1234 2:00 5678 1:45 1234 1 2:00 5678 1:45 1234 1:45 1234 2:00 5678 1:45 1234 2 2:00 5678 1:45 1234 2:00 1:45 5678 1234 1:45 1234 3
Yellow reader will read new value in column written by earlier Blue reader
Art of Multiprocessor Programming 93

Cant Yellow Miss Blues Update? Only if Readers Overlap

1:45 1234

write(2:00 5678) read(2:00 5678) read(1:45 1234)

In which case time its OK to read 1234


Art of Multiprocessor Programming 94

Bad Case Only When Readers Dont Overlap


1:45 1234
write(2:00 5678)

read(2:00 5678)

In which case Blue will complete writing 2:00 5678 to its column

read(2:00 5678)

time

Art of Multiprocessor Programming

95

Road Map
SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic Next MRMW atomic Atomic snapshot
Art of Multiprocessor Programming 96

Multi-Writer Atomic From MultiReader Atomic


stamp
Each writer reads all then writes Max+1 to its register

value

1:45 1:45 2:00 1:45 2:15 1:45

1234 1234 5678 1234 XYZW 1234

Readers read all and take max (Lexicographic like Bakery)

Max is 2:15, return XYZW


Art of Multiprocessor Programming 97

Atomic Execution Means its Linearizable

write(1) write(2) Read (max = 1)

Read(max= 2) write(3) write(2)

write(4) Read(max = 3) Read(max = 4)

time time
Art of Multiprocessor Programming 98

Linearization Points

write(1) write(2) Read (max = 1)

Read(max= 2) write(3) write(2)

write(4) Read(max = 3) Read(max = 4)

time time
Art of Multiprocessor Programming 99

Linearization Points
Look at Writes First

write(1) write(2) write(3) write(2)

write(4)

time time
Art of Multiprocessor Programming 100

Linearization Points
Order writes by TimeStamp

write(1) write(2) write(3) write(2)

write(4)

time time
Art of Multiprocessor Programming 101

Linearization Points
Order reads by max stamp read

write(1) write(2) Read (max = 1)

Read(max= 2) write(3) write(2)

write(4) Read(max = 3) Read(max = 4)

time time
Art of Multiprocessor Programming 102

Linearization Points
Order reads by max stamp read

write(1) write(2) Read (max = 1)

Read(max= 2) write(3) write(2)

write(4) Read(max = 3) Read(max = 4)

time time
Art of Multiprocessor Programming 103

Linearization Points
The linearization point depends on the execution (not a line in the code)!

write(1) write(2) Read (max = 1)

Read(max= 2) write(3) write(2)

write(4) Read(max = 3) Read(max = 4)

time time
Art of Multiprocessor Programming 104

Road Map
SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic Questions? MRMW atomic Atomic snapshot
Art of Multiprocessor Programming 105

Road Map
SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Next Atomic snapshot
Art of Multiprocessor Programming 106

Atomic Snapshot

update

scan

Art of Multiprocessor Programming

107

Atomic Snapshot
Array of SWMR atomic registers Take instantaneous snapshot of all Generalizes to MRMW registers

Art of Multiprocessor Programming

108

Snapshot Interface

public interface Snapshot { public int update(int v); public int[] scan(); }

Art of Multiprocessor Programming

109

Snapshot Interface
Thread i writes v to its register
public interface Snapshot { public int update(int v); public int[] scan(); }

Art of Multiprocessor Programming

110

Snapshot Interface
Instantaneous snapshot of all theads registers public interface Snapshot { public int update(int v); public int[] scan(); }

Art of Multiprocessor Programming

111

Atomic Snapshot
Collect
Read values one at a time

Problem
Incompatible concurrent collects Result not linearizable

Art of Multiprocessor Programming

112

Clean Collects
Clean Collect
Collect during which nothing changed Can we make it happen? Can we detect it?

Art of Multiprocessor Programming

113

Simple Snapshot
Put increasing labels on each entry Collect Problem:twice Scanner might not be collecting a snapshot! If both agree,
Were done
Collect1 x y z w r z x Art of Multiprocessor Programming

Otherwise,
Try again

Collect2
x

y z w r z x 114

Scanner reads x==x and z==z Claim: We Must Use Labels


but as we said x and z were never in memory together
z x z

Scanner

Updater
Updater time

y
z w

y
z

x and z are never in memory together 115 Art of Multiprocessor Programming

Scanner reads x and z with Must Use Labels different labels and recognizes this is not a clean collect
Scanner 1,x 1,z 3,x 3,z

Updater
Updater time

1,x

2,y
1,z 2,w

3,x

4,y
3,z

Art of Multiprocessor Programming

116

Simple Snapshot
Collect twice If both agree,
Were done

Otherwise,
Try again

Collect1 1 22 1 7 13 18 12 Art of Multiprocessor Programming

Collect2
1

22 1 7 13 18 12 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

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 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 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

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

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 123

Simple Snapshot: Scan


public int[] scan() { LabeledValue[] oldCopy, newCopy; Collect oldCopy = collect(); collect: while (true) { newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; continue collect; }} return getValues(newCopy); }}}
Art of Multiprocessor Programming

once

124

Simple Snapshot: Scan


public int[] scan() { LabeledValue[] oldCopy, newCopy; Collect once oldCopy = collect(); collect: while (true) { Collect twice newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; continue collect; }} return getValues(newCopy); }}}
Art of Multiprocessor Programming 125

Simple Snapshot: Scan


public int[] scan() { LabeledValue[] oldCopy, newCopy; Collect once oldCopy = collect(); collect: while (true) { Collect twice newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; continue collect; }} return getValues(newCopy); try again }}}
Art of Multiprocessor Programming

On mismatch,
126

Simple Snapshot: Scan


public int[] scan() { LabeledValue[] oldCopy, newCopy; Collect once oldCopy = collect(); collect: while (true) { Collect twice newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; On match, return continue collect; values }} return getValues(newCopy); }}}
Art of Multiprocessor Programming 127

Simple Snapshot
Linearizable Update is wait-free
No unbounded loops

But Scan can starve


If interrupted by concurrent update

Art of Multiprocessor Programming

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 updates snapshot

Art of Multiprocessor Programming

129

Wait-free Snapshot
If As scan observes that B moved twice, then B completed an update while As scan was in progress
Collect Collect Collect 26 24 12

26 24 12 Update

26 24 12

B time

Art of Multiprocessor Programming

130

Wait-free Snapshot
A
Collect Collect Collect 26 24 12

26 24 12

Update

26 24 12

time
Art of Multiprocessor Programming 131

Wait-free Snapshot
A
Collect Collect Collect 26 24 12

26 24 12 Scan

Write

26 24 12

Update

time
Art of Multiprocessor Programming 132

Wait-free Snapshot
Bs 1st update must have written during 1st collect

Collect

Collect

Collect

26 24 12 Scan

Write

26 24 12 Scan

Write

26 24 12

So A can steal result of Bs scan So scan of Bs second update must be within interval of As scan time
Update
Art of Multiprocessor Programming 133

Wait-free Snapshot
A
Collect Collect Collect 26 24 12 Scan

Write

26 24 12 Scan

Write

26 24 12

But no guarantee that scan of Bs 1st update can be used Why? time
Art of Multiprocessor Programming 134

Once is not Enough


A
Collect Collect 26 24 12

Write

26 24 12

Scan

Update

time

Why cant A steal result of Bs scan Update Because another update might have interfered before the scan
Art of Multiprocessor Programming 135

Someone Must Move Twice


Collect Collect Collect 26 24 12

26 24 12 Update

26 24 12

B
time

If we collect n timessome thread must move twice (pigeonhole principle)


Art of Multiprocessor Programming 136

Scan is Wait-free
scan

At most n-1 depth

update
scan

update scan

So some thread must have had clean collect


137

Art of Multiprocessor Programming

Wait-Free Snapshot Label


public class SnapValue { public int label; public int value; public int[] snap; }

Art of Multiprocessor Programming

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

139

Wait-Free Snapshot Label


public class SnapValue { public int label; public int value; public int[] snap; }

Actual value

Art of Multiprocessor Programming

140

Wait-Free Snapshot Label


public class SnapValue { public int label; public int value; public int[] snap; }

most recent snapshot


Art of Multiprocessor Programming 141

Wait-Free Snapshot Label

1101111010100010110000

label
value
Art of Multiprocessor Programming

Last snapshot
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 143

Wait-free Scan
public void update(int value) { Take scan 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 144

Wait-free Scan
public void update(int value) { Take scan 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); Label value with scan }
Art of Multiprocessor Programming 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); }}}
146

Art of Multiprocessor Programming

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) { Keep track of who moved }} return getValues(newCopy); }}}
147

Art of Multiprocessor Programming

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

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 detectedlets expand here Art of Multiprocessor Programming

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

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; If thread moved twice, }}} just steal its second return getValues(newCopy); snapshot }}}

Art of Multiprocessor Programming

151

Mismatch Detected
if (oldCopy[j].label != newCopy[j].label) { if (moved[j]) { // second move return newCopy[j].snap; } else { moved[j] = true; Remember that oldCopy = newCopy; thread moved continue collect; }}} return getValues(newCopy); }}}

Art of Multiprocessor Programming

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

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

154

Grand Challenge
Snapshot means
Write any one array element Read multiple array elements

Art of Multiprocessor Programming

155

Grand Challenge
What about atomic writes to multiple locations?

Writes to 0 and 1

Writes to 1 and 2

Write many and snapshot

Art of Multiprocessor Programming

156

This work is licensed under a Creative Commons AttributionShareAlike 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

157

Вам также может понравиться