Foundations of Shared Memory - Chennai Mathematical...

145
Foundations of Shared Memory Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Transcript of Foundations of Shared Memory - Chennai Mathematical...

Page 1: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

Foundations of Shared Memory

Companion slides for The Art of Multiprocessor

Programming by Maurice Herlihy & Nir Shavit

Page 2: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 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

Page 3: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 3

Fundamentals

•  What is the weakest form of communication that supports mutual exclusion?

•  What is the weakest shared object that allows shared-memory computation?

Page 4: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 4

Alan Turing

•  Helped us understand what is and is not computable on a sequential machine.

•  Still best model available

Page 5: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 5

0 1 1 0 1 0 1

Turing Machine

Reads and Writes Infinite tape

Finite State Controller

Page 6: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 6

Turing Computability

•  Mathematical model of computation •  What is (and is not) computable •  Efficiency (mostly) irrelevant

0 1 1 0 1 0 1

Page 7: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 7

Shared-Memory Computability?

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

10011

Shared Memory

Page 8: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 8

Foundations of Shared Memory

To understand modern multiprocessors we need to ask

some basic questions …

Page 9: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 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?

Page 10: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 10

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?

Page 11: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 11

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?

Page 12: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 12

Register

10011

Holds a (binary) value

*

* A memory location: name is historical

Page 13: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 13

Register

Can be read 10011

10011

Page 14: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 14

Register

Can be written

10011 01100

Page 15: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 15

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

Registers

Page 16: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 16

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

Registers

Type of register (usually Boolean or m-bit

Integer)

Page 17: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 17

10011

Single-Reader/Single-Writer Register

01100

10011

Page 18: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 18

10011

Multi-Reader/Single-Writer Register

01100

10011

Page 19: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 19

mumble

mumble

11011

Multi-Reader/Multi-Writer Register mumble

10011

10011 10011

01010

Page 20: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 20

Jargon Watch

•  SRSW –  Single-reader single-writer

•  MRSW – Multi-reader single-writer

•  MRMW – Multi-reader multi-writer

Page 21: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 21

Safe Register

write(1001)

read(1001)

OK if reads and writes

don’t overlap

(2)

Page 22: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 22

Safe Register

write(1001)

Some valid value if reads and writes do

overlap

read(????)

0000 1001 1111

$*&v

Page 23: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 23

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

Page 24: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 24

Regular or Not?

write(0)

read(1)

write(1)

read(0)

Page 25: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 25

Regular or Not?

write(0)

read(1)

write(1)

read(0)

Overlap: returns new value

Page 26: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 26

Regular or Not?

write(0) write(1)

read(0)

Overlap: returns old value

Page 27: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 27

Regular or Not?

write(0)

read(1)

write(1)

read(0)

Page 28: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 28

Regular ≠ Atomic

write(0)

read(1)

write(1)

read(0)

write(1) already happened

explain this!

Page 29: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 29

Atomic Register

write(1001)

read(1001)

Linearizable to sequential safe register

write(1010)

read(1010)

read(1010)

Page 30: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 30

Atomic Register

write(1001)

read(1001)

write(1010)

read(1010)

read(1010)

Page 31: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 31

Register Space

MRMW

MRSW

SRSW

Safe Regular

Atomic

M-valued

Boolean

Page 32: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 32

Weakest Register

1

0 1

Single reader Single writer

Safe Boolean register

Page 33: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 33

Weakest Register

Single reader Single writer

Get correct reading if not during state transition

flipflop 0 1 0 0 1 0

Page 34: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 34

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)

Page 35: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 35

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!

Page 36: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 36

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

Page 37: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 37

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

Page 38: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 38

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

Next

Page 39: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 39

public class SafeBoolMRSWRegister

implements Register<Boolean> {

public boolean read() { … } public void write(boolean x) { … } }

(3)

Register Names

Page 40: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 40

public class SafeBoolMRSWRegister

implements Register<Boolean> {

public boolean read() { … } public void write(boolean x) { … } }

(3)

Register Names

property

Page 41: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 41

public class SafeBoolMRSWRegister

implements Register<Boolean> {

public boolean read() { … } public void write(boolean x) { … } }

(3)

Register Names

property

Size matters

Page 42: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 42

public class SafeBoolMRSWRegister

implements Register<Boolean> {

public boolean read() { … } public void write(boolean x) { … } }

(3)

Register Names

property

type

How many readers & writers?

Page 43: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 43

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

}}

(2)

Page 44: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 44

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

Page 45: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 45

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

Page 46: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 46

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

Page 47: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 47

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

Page 48: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 48

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

Page 49: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 49

Safe Boolean MRSW from Safe Boolean SRSW

1

11

1

0 or 1

0

0 0

0

0

1

Page 50: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 50

1000 1000

1000

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

1

1011 1

Any value in range 0

0

1011

Page 51: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 51

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

Questions?

Page 52: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 52

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

Next

Page 53: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 53

Regular Boolean MRSW from Safe Boolean MRSW

1

1

00

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

Page 54: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 54

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

}}

(2)

Page 55: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 55

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

}}

(2)

Last bit this thread wrote (OK, we’re cheating here on Java syntax)

Page 56: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 56

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

}}

(2)

Actual value

Page 57: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 57

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

}}

(2)

Is new value different from last value I wrote?

Page 58: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 58

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

}}

(2)

If so, change it (otherwise don’t!)

Page 59: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 59

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

}}

(2)

• Overlap? No Overlap? • No problem • either Boolean value works

Page 60: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 60

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

Page 61: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 61

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

Questions?

Page 62: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 62

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

Next

Page 63: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 63

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

Page 64: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 64

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

Page 65: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 65

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

Page 66: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 66

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

Page 67: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 67

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

Page 68: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 68

Writing M-Valued

0 1 2 3 4 5 6 7 1 1 0 0 0 0

Write 5

Initially 0

Page 69: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 69

Writing M-Valued

0 1 2 3 4 5 6 7 1 1 0 0 0 0

Write 5

5 0

Page 70: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 70

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

Questions?

Page 71: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 71

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

Page 72: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 72

Road Map (Slight Detour)

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

SWSW Atomic

Page 73: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

Concurrent Reading

© 2007 Herlihy & Shavit 73

SRSW Atomic From SRSW Regular

1234

Regular writer

Regular reader

1234 5678

Instead of 5678…

When is this a problem?

Page 74: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 74

SRSW Atomic From SRSW Regular

1234

Regular writer

Regular reader

5678 5678

time

Reg write(5678)

Reg read(5678)

Initially 1234

Page 75: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 75

SRSW Atomic From SRSW Regular

1234

Regular writer

Regular reader

1234 5678

Instead of 5678…

time

Reg write(5678)

Reg read(1234)

Initially 1234

Page 76: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 76

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)

Write 5678 happened

Page 77: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 77

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

Page 78: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 78

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

5678 old = 2:00

>

5678 2:00

Page 79: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 79

Atomic Single Reader to Atomic Multi-Reader

1:45 1234 1:45 1234

1:45 1234 1:45 1234

stamp value

One per reader

Page 80: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 80

Another Scenario

1:45 1234 2:00 5678

1:45 1234 1:45 1234

stamp value

Writer starts write…

Page 81: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 81

Another Scenario

1:45 1234 2:00 5678

1:45 1234 1:45 1234

stamp value reader reads

2:00, 5678 zzz…

1:45 1234

later reader

Yellow was completely after blue but read earlier value…not linearizable!

Page 82: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 82

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

Page 83: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 83

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

Page 84: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 84

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

Page 85: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 85

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

Page 86: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 86

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

Page 87: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 87

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

Next

Page 88: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 88

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

Page 89: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 89

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)

Page 90: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 90

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)

Page 91: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 91

Linearization Points

time

write(1)

time

Look at Writes First

(4)

write(4)

write(2) write(3)

write(2)

Page 92: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 92

Linearization Points

time

write(1)

time

(4)

write(4)

write(2) write(3)

write(2)

Order writes by TimeStamp

Page 93: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 93

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

Page 94: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 94

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

Page 95: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 95

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

Page 96: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 96

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot

Questions?

Page 97: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 97

Road Map

•  SRSW safe Boolean •  MRSW safe Boolean •  MRSW regular Boolean •  MRSW regular •  MRSW atomic •  MRMW atomic •  Atomic snapshot Next

Page 98: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 98

Atomic Snapshot

update scan

Page 99: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 99

Atomic Snapshot

•  Array of SWMR atomic registers •  Take instantaneous snapshot of all •  Generalizes to MRMW registers …

Page 100: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 100

Snapshot Interface

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

(2)

Page 101: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 101

Snapshot Interface

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

Thread i writes v to its register

(2)

Page 102: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 102

Snapshot Interface

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

Instantaneous snapshot of all theads’ registers

(2)

Page 103: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 103

Atomic Snapshot

•  Collect –  Read values one at a time

•  Problem –  Incompatible concurrent collects –  Result not linearizable

Page 104: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 104

Clean Collects

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

Page 105: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 105

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

Page 106: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 106

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)

Page 107: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 107

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

Page 108: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 108

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

Page 109: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 109

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)

Page 110: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 110

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

Page 111: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 111

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)

Page 112: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 112

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

Page 113: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 113

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

Page 114: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 114

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

Page 115: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 115

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

Page 116: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 116

Simple Snapshot

•  Linearizable •  Update is wait-free

– No unbounded loops •  But Scan can starve

–  If interrupted by concurrent update

Page 117: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 117

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

Page 118: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 118

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

Page 119: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 119

Wait-free Snapshot

time

≠ ≠ 26 24 12

Collect

26 24 12

Collect

26 24 12

Collect

Update

A

B

Page 120: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 120

Wait-free Snapshot

time

≠ ≠ 26 24 12

Collect

26 24 12

Collect

26 24 12

Collect

A

B Scan Write

Update

Page 121: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 121

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

Page 122: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 122

Wait-free Snapshot

time

≠ ≠ 26 24 12

Collect

26 24 12

Collect

26 24 12

Collect

A

B Scan Write Scan Write

But no guarantee that scan of B’s 1st update can be used… Why?

Page 123: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 123

Once is not Enough

time

≠ 26 24 12

Collect

26 24 12

Collect

Update

A

B Scan Write

Why can’t A steal result of B’s scan Because another update might have interfered before the scan

Update

Page 124: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 124

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)

Page 125: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 125

Scan is Wait-free

scan

update

So some thread must have had clean collect

scan

update scan

At most n-1

depth

Page 126: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 126

Wait-Free Snapshot Label

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

(2)

Page 127: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 127

Wait-Free Snapshot Label

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

(2)

Counter incremented with each snapshot

Page 128: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 128

Wait-Free Snapshot Label

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

(2)

Actual value

Page 129: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 129

Wait-Free Snapshot Label

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

(2)

most recent snapshot

Page 130: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 130

Wait-Free Snapshot Label

11011110101000101100…00

label

value Last

snapshot (3)

Page 131: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 131

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)

Page 132: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 132

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

Page 133: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 133

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

Page 134: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 134

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)

Page 135: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 135

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

Page 136: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 136

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

Page 137: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 137

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…

Page 138: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 138

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)

Page 139: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 139

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)

Page 140: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 140

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

Page 141: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 141

Observations

•  Uses unbounded counters –  can be replaced with 2 bits

•  Assumes SWMR registers –  for labels –  can be extended to MRMW

Page 142: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 142

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?

Page 143: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 143

Grand Challenge

•  Snapshot means – Write any one array element –  Read multiple array elements

Page 144: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

© 2007 Herlihy & Shavit 144

Grand Challenge

Writes to 0 and 1

Writes to 1 and 2

What about atomic writes to

multiple locations?

Write many and snapshot

Page 145: Foundations of Shared Memory - Chennai Mathematical Institutespsuresh/teaching/cprg15/lectures/... · 2018. 12. 7. · Foundations of Shared Memory Companion slides for The Art of

Art of Multiprocessor Programming

145

          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.