Implementing Sequentially Consistent Programs on Processor Consistent Platforms Lisa Higham and...

Post on 31-Mar-2015

222 views 2 download

transcript

Implementing Sequentially Consistent Programson Processor Consistent Platforms

Lisa Higham and Jalal Kawash

University of Calgary, Canada

American University of Sharjah, UAE

Outline

• Memory consistency models– Sequential consistency

– P-RAM

– Coherence

– PC-G

• Compiling from SC to PC-G– Good news

– Bad news

– Proof

Multi-Processor’s Computation

P P P P..……

Oo1o1o1o1

o2o2o2o2

o3o3o3o3

……

……..…..…

Sequential Consistency

P P P P..……

Switch

SharedMemory

Sequential Consistency: Example

P P P

x y

r(y) 4r(x) 2

w(x, 2)w(y, 1)

w(x, 3)w(y, 4)

w(x, 3)

w(y, 4)

r(y) 4

w(x, 2)

w(y, 1)

r(x) 2

0 0 0 0 0 03 42 1

Sequential Consistency: Definition

• A computation is sequentially consistent iff

a valid total order on O such that

(O, ) (O, )prog

P-RAM

Copies ofMemory

P

P

P

P

FIFOChannels

x y z x y z

x y z x y z

P-RAM: Example

w(x, 1)x 1x 1x 1

w(y, 2)

y 2y 2y 2P P

PP

P P P P

r(y) 2r(x) 0

r(x) 1r(y) 0

w(y, 2)w(x, 1)

0 0 0 021

0 0 0 021

0 0 0 021

0 0 0 021

r(x) 1r(y) 0r(y) 2r(x) 0

x y x y

x y x y

P-RAM Definition

A computation is P-RAM iff for each process p, a valid total order such that

(O|p O|w, ) (O|p O|w, )

Lp

∩prog

∩Lp

Coherence

x zy

P P P P…...

x 2 y 3

Coherence: Example

0 0

x y

P P P P

w(x, 2)r(y) 0 r(x) 0

w(y, 3)

2 3

w(x, 2)

r(y) 0

r(x) 0

w(y, 3)

P

Coherence: Definition

A computation is Coherent iff for each variable x, a valid total order such that

(O|x , ) (O|x , ) prog x

x

PC-G: P-RAM and Coherence

P-RAM CoherencePC-G

P-CG Definition

A computation is P-CG iff for each process p, a valid total order such that

– (O|p O|w, ) (O|p O|w, )

processes q, and variable x:

• (O|w ∩ O|x, ) = (O|w ∩ O|x, )

Lp

∩ prog∩

Lp

Lp Lq

P-CG vs. SC

• Algorithms are designed for SC machines

• Some of them work directly when run on

P-CG (e.g. Peterson 2)

• Most of the SC algorithms do not work on P-CG machines (e.g. test&set and Bakery algorithm)

• Can we transform an SC algorithm to an equivalent P-CG algorithm?

• Can we find a compiler that transforms any SC algorithm to an equivalent P-CG algorithm?

Program Transformation and Interpretation

ProgramP

Programα(P)

Transformationα Interpretation

D

C= {Computations of P on SC machines}D= {Computations of α(P) on M machines}E = {Interpretations of D on SC machines}

C EExecute P

on SC

Execute α(P)

on PC-G

Program Implementation

ProgramP

Programα(P)

Transformationα

C E

Interpretation

D

C= {SC Computations of P}D= {P-CG Computations of α(P)}E = {Interpretations of D}

If program P, α implements P, then α is a compiler from SC to PC-G

Execute P

on SC

Execute α(P)

on PC-G

Transformation Function α

m: a new multi-writer variable

Instruction α(Instruction)

write(sy, val) write(m, id(y)); write(sy, val); write(m, id(y))

read(sy) read(sy)

Results

• Claim 1: implements Lamport’s Bakery algorithm for

2 processes on PC-G

• Claim 2: is a compiler from SC to PC-G for any

program provided:• Only 2 processes• Only single-writer variables

Transformation Example

P P

w(x, 1)

r(y)

w(y, 4)

w(y, 2)

r(x)

Program

Under SC: if r(y) returns 4, then r(x) returns 1

Possible PC-G Views

P P

w(x, 1)w(y, 4)r(y) 4w(y, 2)

w(y, 4)w(y, 2)r(x) 0w(x, 1)

Under PC-G: r(y) returns 4 and r(x) may return 0

Transformation ExampleProgram(Program)

P P

w(x, 1)

r(x)

w(y, 4)

r(y)w(y, 2)w4(m, P)

w1(m, P)

w2(m, P)

w1(m, P)

w2(m, P)w3(m, P)

Transformation ExampleP P

r(x)

r(x) must return 1

w(x, 1) w(y, 4)

w(y, 2)

View for View for

w1(m, P)

w2(m, P)

w3(m, P)

w4(m, P)

w1(m, P)

w2(m, P)

If r(y) returns 4

w(x, 1) w(y, 4)

w(y, 2)

w1(m, P)

w2(m, P)

w3(m, P)

w4(m, P)

w1(m, P)

w2(m, P)

r(y)

m

m

m

m

m

m

Proof Sketch

P P

w1w

w2

..…

..…m1

m5

m2

m3

m4

m6

Program

P P

PC-G Views

m1

m5

m2

m3

m4

m6

ww1

w2

System View

m1

m5

m2

m3

m4

m6

ww1

w2

…..

m2

m3

m4

m6

m1

m5

Proof Sketch

• System view– Contains all reads and writes by both processes– Maintains program order– Is valid

Summary

Compiler:

• Only one additional variable

• Only writes to that variable

Provided:

• Two processors

• Single writer variables

Impossibilities

For more than 2 processors, there is no compiler from SC to PC-G that:

• Only adds write instructions (with any number of variables)

nor• Uses only one additional variable (with any

number of reads and writes)

Pros and Cons

• Restricted– 2 processes– Only single writer variables

• Valuable– ME Lamport’s Bakery algorithm– Wait-free test&set had no known solutions in

weak memory consistency

Wait-Free Test&setDefine test&set<(Ω, i)>

if (si = you and sj ≠ rst) the return 1

repeat

si choose

case sj is:

you, rst:

si me

me :

si you

choose:

si random (me, you)

end case

until (si ≠ sj)

if (si = me) then return 0

else return 1

m i

m i

m i

m i

m i

m i

m i

m i

Define reset<(Ω, i)>

si rst

Conclusions

• α works for any two-process program withsingle-writer variables

• α works for particular programs with > 2 processes (randomized wait-free n-process test&set)

• If there is a transformation that work for other cases, it must be more complicated:– Cannot be write-adding– Must use more than one additional variable

Thank You

?

m

m

m

m

m

m

Proof Sketch

P P

w1w

w2

..…

..…m1

m5

m2

m3

m4

m6

Program

P P

PC-G Views

m1

m5

m2

m3

m4

m6

..

m2

m3

m4

m6

m1

m5

w

w1

w2

System View

m1

m5

m2

m3

m4

m6

ww1

w2