art of multiprocessor programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming...
TRANSCRIPT
![Page 1: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/1.jpg)
Distributed Computing
![Page 2: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/2.jpg)
Background: Distributed Computing
• Distributed graph algorithms
• Large-scale parallel computation
• Concurrent data structures and
programming primitives
• Biological systems find(7)
insert(8)
![Page 3: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/3.jpg)
![Page 4: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/4.jpg)
The Plan
• Part 1: concurrency in shared memory
• Part 2: distributed graph algorithms
• Part 3: lower bounds & communication
complexity
![Page 5: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/5.jpg)
Companion slides for
The Art of Multiprocessor Programming
by Maurice Herlihy & Nir Shavit
![Page 6: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/6.jpg)
Intel x86 Instruction Set
⋮
⋮
![Page 7: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/7.jpg)
Modeling Concurrency
1001101110100 Shared Memory
![Page 8: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/8.jpg)
Modeling Concurrency
• n processes
• A run: sequence of interleaved process
steps
• Order chosen by adversary
![Page 9: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/9.jpg)
Modeling Concurrency
• Shared memory:
– Collection of objects
• Process step: choose a shared object
and interact with it
• For now assume: only read/write
![Page 10: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/10.jpg)
Modeling Concurrency
• Example: shared counter
myValue = counter++
myValue ← read(counter) write(counter, myValue+1)
Is this good?
![Page 11: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/11.jpg)
Wait-Free Implementation
• Every process finishes in a finite number
of its own steps
– Even if other processes very active
– Even if other processes die
!*@$
????
![Page 12: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/12.jpg)
Why wait-freedom?
![Page 13: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/13.jpg)
Asynchronous Interrupts
??? ???
![Page 14: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/14.jpg)
Heterogeneous Processors
??? ???
supercomputer supercomputer
toaster
![Page 15: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/15.jpg)
Fault-tolerance
??? ???
![Page 16: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/16.jpg)
A SIMPLE TASK: CONSENSUS
![Page 17: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/17.jpg)
Consensus: Each Thread has a
Private Input 32 19
21
![Page 18: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/18.jpg)
They Communicate
![Page 19: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/19.jpg)
They Agree on One Thread’s
Input 19 19
19
![Page 20: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/20.jpg)
Formally: Consensus
• Agreement:
– all threads decide the same value
• Validity:
– the common decision value is some
thread's input
![Page 21: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/21.jpg)
No Wait-Free Implementation of
Consensus using Registers ??? ???
![Page 22: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/22.jpg)
Formally
• Theorem
– There is no wait-free implementation of
n-thread consensus from read-write
registers
![Page 23: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/23.jpg)
Wait-Free Computation
• Either A or B “moves”
• Moving means
– Register read
– Register write
A moves B moves
![Page 24: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/24.jpg)
The Two-Move Tree
Initial
state
Final
states
![Page 25: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/25.jpg)
Decision Values
1 0 0 1 1 1
![Page 26: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/26.jpg)
Bivalent: Both Possible
1 1 1
bivalent
1 0 0
![Page 27: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/27.jpg)
Univalent: Single Value Possible
1 1 1
univalent
1 0 0
![Page 28: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/28.jpg)
x-valent: x Only Possible
Decision
0 1 1 1
1-valent
0 1
![Page 29: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/29.jpg)
Summary
• Wait-free computation is a tree
• Bivalent system states
– Outcome not fixed
• Univalent states
– Outcome is fixed
– May not be “known” yet
• 1-Valent and 0-Valent states
![Page 30: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/30.jpg)
Claim
• Some initial state is bivalent
![Page 31: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/31.jpg)
Claim
• Some initial state is bivalent
• Outcome depends on
– Chance
– Whim of the scheduler
![Page 32: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/32.jpg)
What if inputs differ?
1 0
![Page 33: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/33.jpg)
Solo execution by A
Must decide 0
0
![Page 34: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/34.jpg)
Solo execution by B
1
Must decide 1
![Page 35: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/35.jpg)
Bivalent initial state
0 1
1 0
![Page 36: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/36.jpg)
0-valent
Critical States
1-valent
critical
![Page 37: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/37.jpg)
From a Critical State
c
If A goes first,
protocol
decides 0
If B goes first,
protocol
decides 1
0-valent 1-valent
![Page 38: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/38.jpg)
Critical States
Claim: starting from a bivalent initial state
the protocol will reach a critical state
![Page 39: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/39.jpg)
Reaching Critical State
CA
CA
CB
c
CB
0-valent
1-valent
0-valent
0-valent 1-valent
initially bivalent
![Page 40: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/40.jpg)
Model Dependency
• So far, memory-independent!
• True for
– Registers
– Message-passing
– Carrier pigeons
– Any kind of asynchronous computation
![Page 41: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/41.jpg)
From a Critical State
c
If A goes first,
protocol
decides 0
If B goes first,
protocol
decides 1
0-valent 1-valent
![Page 42: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/42.jpg)
Possible Interactions
x.read() y.read() x.write() y.write()
x.read()
? ? ? ?
y.read()
? ? ? ?
x.write()
? ? ? ?
y.write()
? ? ? ?
![Page 43: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/43.jpg)
B reads some register
c
![Page 44: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/44.jpg)
B reads some register
A runs
solo,
eventually
decides 0 0
c
![Page 45: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/45.jpg)
B reads some register
A runs
solo,
eventually
decides 0
B reads x
0
c
![Page 46: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/46.jpg)
B reads some register
A runs
solo,
eventually
decides 0
B reads x
1
0
A runs
solo,
eventually
decides 1
c
![Page 47: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/47.jpg)
B reads some register
A runs
solo,
eventually
decides 0
B reads x
1
0
A runs
solo,
eventually
decides 1
c
States look
the same to A
![Page 48: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/48.jpg)
Possible Interactions
x.read() y.read() x.write() y.write()
x.read()
no no no no
y.read()
no no no no
x.write()
no no ? ?
y.write()
no no ? ?
![Page 49: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/49.jpg)
Writing Distinct Registers
c
![Page 50: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/50.jpg)
Writing Distinct Registers
A writes y c
![Page 51: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/51.jpg)
Writing Distinct Registers
A writes y c
B writes x
![Page 52: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/52.jpg)
Writing Distinct Registers
A writes y c
B writes x
0
![Page 53: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/53.jpg)
Writing Distinct Registers
A writes y B writes x c
B writes x
0
![Page 54: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/54.jpg)
Writing Distinct Registers
A writes y B writes x c
A writes y B writes x
0
![Page 55: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/55.jpg)
Writing Distinct Registers
A writes y B writes x c
A writes y B writes x
0 1
![Page 56: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/56.jpg)
Writing Distinct Registers
A writes y B writes x c
Same story
A writes y B writes x
0 1
![Page 57: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/57.jpg)
Possible Interactions
x.read() y.read() x.write() y.write()
x.read()
no no no no
y.read()
no no no no
x.write()
no no ? no
y.write()
no no no ?
![Page 58: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/58.jpg)
Writing Same Registers
States
look the
same to A
A writes x B writes x
1
A runs solo,
eventually
decides 1
c
0
A runs solo,
eventually
decides 0 A writes x
![Page 59: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/59.jpg)
Recap: Atomic Registers Can’t
Do Consensus
• If protocol exists
– It has a bivalent initial state
– Leading to a critical state
• What’s up with the critical state?
– Case analysis for each pair of methods
– As we showed, all lead to a contradiction
![Page 60: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/60.jpg)
Consensus Numbers
• An object X has consensus number n
– If it can be used to solve n-thread
consensus
• Take any number of instances of X
• together with atomic read/write registers
• and implement n-thread consensus
– But not (n+1)-thread consensus
![Page 61: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/61.jpg)
Consensus Numbers
• Read/write registers have consensus
number 1
![Page 62: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/62.jpg)
Intel x86 Instruction Set
⋮
⋮
![Page 63: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/63.jpg)
Read-Modify-Write Instructions
![Page 64: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/64.jpg)
Read-Modify-Write Instructions
• Test-And-Set: t&s(x)
– If x = 1 return 0
– If x = 0, set it to 1 and return 1
• Compare-And-Swap: cas(x, old, new)
– If x = old, set it to new, return “success”
– Else return value of x
![Page 65: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/65.jpg)
Consensus from Test&Set
• Two processes:
0
myval
1
0 winner
0
𝒗𝟎
![Page 66: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/66.jpg)
Consensus from Test&Set
• Two processes:
0
myval
1
0 winner
0 𝒗𝟎
![Page 67: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/67.jpg)
Consensus from Test&Set
• Two processes:
0
myval
1
0 winner
0
𝒗𝟎
test&set
![Page 68: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/68.jpg)
Consensus from Test&Set
• Two processes:
0
myval
1
1 winner
0
𝒗𝟎
output 𝒗𝟎
![Page 69: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/69.jpg)
Consensus from Test&Set
• Two processes:
0
myval
1
1 winner
1 𝒗𝟏
𝒗𝟎
![Page 70: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/70.jpg)
Consensus from Test&Set
• Two processes:
0
myval
1
1 winner
1
𝒗𝟏 𝒗𝟎
test&set
![Page 71: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/71.jpg)
Consensus from Test&Set
• Two processes:
0
myval
1
1 winner
1
𝒗𝟏 𝒗𝟎
![Page 72: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/72.jpg)
Consensus from Test&Set
• Two processes:
0
myval
1
1 winner
1
𝒗𝟏 𝒗𝟎
read
![Page 73: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/73.jpg)
Consensus Number of T&S
• We showed: ≥ 2
• Impossibility for 3:
– Almost the same as before…
![Page 74: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/74.jpg)
Consensus from CAS
• array myval[n]
• winner, initially ⊥
• process i:
– write 𝑣𝑖 to myval[i]
– res ← CAS(winner, ⊥, i)
– If(res = “success”) output 𝑣𝑖
– else read myval[res]
![Page 75: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/75.jpg)
Transactional Memory
atomic {
…
}
![Page 76: Art of Multiprocessor Programmingeventos.cmm.uchile.cl/.../46/2018/01/sharedmem1.pdf · programming primitives • Biological systems find(7) insert(8) The Plan • Part 1: concurrency](https://reader035.vdocument.in/reader035/viewer/2022081523/5fdc3190cc5edf623c505296/html5/thumbnails/76.jpg)