+ All Categories
Home > Documents > The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An...

The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An...

Date post: 07-Oct-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
29
1 © R. Guerraoui The Power of Registers Prof R. Guerraoui Distributed Programming Laboratory
Transcript
Page 1: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

1© R. Guerraoui

The Power of Registers

Prof R. GuerraouiDistributed Programming Laboratory

Page 2: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

2

Atomic execution

p1

p2

p3

write(1) - ok

read() - 1

read() - 1

Page 3: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

3

Atomic execution

p1

p2

p3

write(1) - ok

read() - 1

read() - 0

Page 4: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

4

Registers

Question 1: what objects can we implement with registers?

Question 2: what objects we cannot implement?

Page 5: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

5

Wait-free implementations of atomic objects

An atomic object is simply defined by its sequential specification; i.e., by how its operations should be implemented when there is no concurrency

Implementations should be wait-free: every process that invokes an operation eventually gets a reply (unless the process crashes)

Page 6: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

6

Counter (sequential spec)A counter has two operations inc() and read() and maintains an integer x init to 0

read():return(x)

inc():x := x + 1;return(ok)

Page 7: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

7

Naive implementationThe processes share one register Regread():

return(Reg.read())inc():

temp:= Reg.read()+1;Reg.write(temp);return(ok)

Page 8: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

8

Atomic execution?

p1

p2

p3

inc() - ok

read() - 1

inc() - ok

Page 9: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

9

Atomic implementationThe processes share an array of registers Reg1,..,n

inc():Regi.write(Regi.read() +1);return(ok)

Page 10: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

10

Atomic implementation

read():sum := 0;for j = 1 to n do

sum := sum + Regj.read();return(sum)

Page 11: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

11

Atomic execution?

p1

p2

p3

inc() - ok

read() - 2

inc() - ok

Page 12: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

12

Snapshot (sequential spec) A snapshot has operations update() and scan() and maintains an array x of size n

scan():return(x)

update(i,v):xi := v;return(ok)

Page 13: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

13

Very naive implementation Each process maintains an array of integer variables x init to 0,..,0

scan():return(x)

update(i,v):xi := v; return(ok)

Page 14: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

14

Atomic execution?

p1

p2

p3

update(1,1) - ok

collect() - 0,0,0

Page 15: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

15

Less naive implementation The processes share one array of N registers Reg1,..,Nscan():

for j = 1 to N do xj := Regj.read();

return(x) update(i,v):

Regi.write(v); return(ok)

Page 16: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

16

Atomic execution?

p1

p2

p3

update(1,1) - ok

collect() - 1,0,0

Page 17: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

17

Atomic execution?

p1

p2

p3

update(1,1) - ok

scan() - 1,0,2

update(3,2) - ok

Page 18: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

18

Atomic execution?

p1

p2

p3

scan() - 0,0,10

update(2,1) - ok

update(3,10) - ok

Page 19: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

19

Non-atomic vs atomic snapshot

What we implement here is some kind of regular snapshot:

A scan returns, for every index of the snapshot, the last written values or the value of any concurrent update

We call it collect

Page 20: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

20

Key idea for atomicity

To scan, a process keeps reading the entire snapshot (i.e., it collect), until two results are the same

This means that the snapshot did not change, and it is safe to return without violating atomicity

Page 21: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

21

Same value vs. Same timestamp

p1

p2

p3

scan() - 0,0,2

collect()-0,0,2

update(2,0)

collect()-0,0,2

update(2,1)

update(3,2)

update(2,0) update(2,1)

update(3,2)update(3,0)

Page 22: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

22

Enforcing atomicityThe processes share one array of N registers Reg1,..,N; each contains a value and a timestampWe use the following operation for modularitycollect():

for j = 1 to N do xj := Regj.read();

return(x)

Page 23: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

23

Enforcing atomicity (cont’d) scan():

temp1 := self.collect();while(true) do

temp2 := self.collect(); if (temp1 = temp2) then

return (temp1.val)temp1 := temp2;

update(i,v):ts := ts + 1;Regi.write(v,ts); return(ok)

Page 24: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

24

Wait-freedom?

p1

p2

p3

scan() - …

collect()-0,0,10

update(3,10) - ok

update(2,1) - ok

collect()-0,1,10

update(2,3) - ok

Page 25: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

25

Key idea for atomicity & wait-freedom

The processes share an array of registersReg1,..,N that contains each:

a value, a timestamp, anda copy of the entire array of values

Page 26: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

26

Key idea for atomicity & wait-freedom (cont’d)

To scan, a process keeps collecting and returns a collect if it did not change, or some collect returned by a concurrent scan

Timestamps are used to check if the collect changes or if a scan has been taken in the meantime

• To update, a process scans and writes the value, the new timestamp and the result of the scan

Page 27: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

27

Snapshot implementation Every process keeps a local timestamp ts

update(i,v):ts := ts + 1;Regi.write(v,ts,self.scan());return(ok)

Page 28: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

28

Snapshot implementation

scan():t1 := self.collect(); t2:= t1while(true) do

t3:= self.collect();if (t3 = t2) then return (t3);

for j = 1 to N doif(t3j,2 ≥ t1j,2+2) then

return (t3j,3)t2 := t3

Return the first value in each cell in t3

Page 29: The Power of Registers - dcl.epfl.ch · 5 Wait-free implementations of atomic objects An atomicobject is simply defined by its sequential specification; i.e., by how its operations

29

Possible execution?

p1

p2

p3

scan() - 0,0,3

update(3,2)-okupdate(3,1)-ok update(3,3)-ok


Recommended