SlideShare a Scribd company logo
1 of 168
Download to read offline
The Relative Power of Synchronization Operations Companion slides for The Art of Multiprocessor Programming  第 5 章  by Maurice Herlihy & Nir Shavit 日本語追記  by  根本和郎
並行プログラミングの原理から実践まで ,[object Object],[object Object]
[object Object],[object Object],[object Object],Last Lecture:  Shared-Memory Computability 10011 Shared Memory
Wait-Free Implementation ,[object Object],[object Object],[object Object],(2)
コンセンサス・プロトコル ,[object Object],[object Object],[object Object],[object Object],[object Object]
From Weakest Register SRSW の最もシンプルなケース 1 0 1 Single reader Single writer Safe Boolean register
All the way to a Wait-free Implementation of Atomic Snapshots MRMW MRSW SRSW Saf e Regular Atomic M-valued Boolean Snapshot
Rationale for wait-freedom ,[object Object],[object Object],[object Object],[object Object],[object Object]
Why is Mutual Exclusion so wrong? シンクロナイズドの排他制御のよくないところは・・・ (2)
Asynchronous Interrupts 同期的だと一人に休まれると、一緒に働いている人も休まなくてはならない ??? ??? (2) Swapped out back at
Heterogeneous  Processors 直接つながっている場合遅い性能の処理に待たされることがある。 ??? ??? Yawn ( ひま ) (1) Pentium Pentium 286
Fault-tolerance 一人のダウンが致命的 ??? ??? (2)
Machine Level Instruction Granularity Eugene Amdahl (2) ,[object Object]
Basic Questions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
FIFO Queue: Enqueue Method q.enq ( )
FIFO Queue: Dequeue Method q.deq()/
Two-Thread Wait-Free Queue public class  LockFreeQueue { int  head = 0, tail = 0;  Item[QSIZE] items; public void  enq(Item x) { while  (tail - head == QSIZE) {}; items[tail % QSIZE] = x; tail++; } public  Item deq() { while  (tail - head == 0) {} Item item = items[head % QSIZE]; head++;  return  item; }}
What About Multiple Dequeuers?
Grand Challenge ,[object Object],[object Object],[object Object],[object Object],[object Object],Only new aspect (1)
Consensus ,[object Object],[object Object],[object Object],[object Object]
コンセンサス問題 ,[object Object],[object Object],[object Object]
Consensus: Each Thread has a Private Input 32 19 21
They Communicate
They Agree on One Thread’s Input 19 19 19
Formally: Consensus ,[object Object],[object Object],[object Object],[object Object]
No Wait-Free Implementation of Consensus using Registers ??? ??? ,[object Object]
Formally ,[object Object],[object Object],PODC2001 の Influential paper award CAP 理論の元
Proof Strategy ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Wait-Free Computation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],A moves 読む B moves 書く Lemma =  補題  (dilemma: ジレンマ )
A スレッドと B スレッドの話 ,[object Object]
バイナリコンセンサス ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The Two-Move Tree Initial state この時は 1/0 の 2 価の共有オブジェクト Final states (2) A moves B moves
Decision Values コンセンサス数の決め方 すべてのスレッドが同じ値を共有するまで 不定 どちらかに確定 不定 0 に確定 どちらかに確定 0 に確定 1 0 0 1 1 1 不定 1 回目 2 回目 3 回目 4 回目 1 と読む 0 と読む 1 と書く 1 と書く 1 と読む 1 と読む 1 と書く 1 と書く 1 と書く 読む 1 と書く 1 と書く 0 と書く 1 と書く 読む 0 と書く 読む 0 と書く ,[object Object]
Bivalent: Both Possible 1 1 1 Bivalent(2 価 ) :2 種類の状態を取りうる 1 0 0 本ではここが 1 だが、それはそれで正しい図
Univalent: Single Value Possible 1 1 1 univalent ここから下は 1 価に固定される 1 0 0
x-valent: x Only Possible Decision 0 1 1 1 1-valent 0 1
 
Summary ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
まとめ ,[object Object],[object Object],[object Object],[object Object]
Claim ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Both Inputs 0 ,[object Object],0 0 (2) (1)
Both Inputs 0 ,[object Object],(1) (1) 0
Both Inputs 1 ,[object Object],1 1 (2) (1)
Both Inputs 1 ,[object Object],(1) (1) 1
What if inputs differ? (2) 1 0 By Way of contradiction ( 否定 ) :  If univalent all executions must decide on same value
The Possible Executions ,[object Object],[object Object],0 1 (2)
The Possible Executions ,[object Object],[object Object],0 1 (2)
Possible Executions Include ,[object Object],[object Object],0 1 How univalent is that? (QED)
Critical States 次のステップで状態が絞り込まれていくステートのこと 0-valent 1-valent critical (3) (3)
From a Critical State A か B の決定が先勝ちで決まる状態 c If A goes first, protocol decides 0 If B goes first, protocol decides 1 0-valent 1-valent
Reaching Critical State 相手側が決定しないうちはまだ univalent これはだいたいの概念図 C A C A C B c C B univalent univalent univalent univalent 0-valent 1-valent initially bivalent
Critical States ,[object Object],[object Object],[object Object],[object Object]
Model Dependency ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Read-Write Memory ,[object Object],[object Object]
Completing the Proof ,[object Object],[object Object],[object Object],[object Object],[object Object]
Possible Interactions ? ? ? ? y.write() ? ? ? ? x.write() ? ? ? ? y.read() ? ? ? ? x.read() y.write() x.write() y.read() x.read() A reads x A reads y
Some Thread Reads A runs solo, eventually decides 0 B reads x 1 0 A runs solo, eventually  decides 1 c States look the same to A Contradiction
Possible Interactions ? ? no no y.write() ? ? no no x.write() no no no no y.read() no no no no x.read() y.write() x.write() y.read() x.read()
Writing Distinct Registers A writes y B writes x 1 0 c The song is the same A writes y B writes x Contradiction
Possible Interactions ? no no no y.write() no ? no no x.write() no no no no y.read() no no no no x.read() y.write() x.write() y.read() x.read()
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 Contradiction
That’s All, Folks! QED no no no no y.write() no no no no x.write() no no no no y.read() no no no no x.read() y.write() x.write() y.read() x.read()
Recap: Atomic Registers Can’t Do Consensus  アトミックなレジスターのみではまだ足りない ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What Does Consensus have to do with Concurrent Objects?
Consensus Object public interface  Consensus { Object decide(object value); } (4) (4)
Concurrent Consensus Object ,[object Object],[object Object],[object Object]
Java Jargon Watch ,[object Object],[object Object],[object Object]
Generic Consensus Protocol abstract class  ConsensusProtocol  implements  Consensus { protected  Object[] proposed = new  Object[N]; private void  propose(Object value) { proposed[ThreadID.get()] = value; } abstract   public  Object  decide(object value); }} (4) (4)
Generic Consensus Protocol abstract class ConsensusProtocol implements Consensus { protected  Object[] proposed = new  Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object  decide(object value); }} (4) (4) Each thread’s proposed value
Generic Consensus Protocol abstract class ConsensusProtocol implements Consensus { protected Object[] proposed = new Object[N]; private void  propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object  decide(object value); }} (4) (4) Propose a value
Generic Consensus Protocol abstract class ConsensusProtocol implements Consensus { protected Object[] proposed = new Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract   public  Object  decide(object value); }} (4) (4) Decide a value: abstract method means subclass does the heavy lifting (real work)
Can a FIFO Queue Implement Consensus?
FIFO Consensus proposed  array FIFO Queue  with red and black balls Coveted red ball Dreaded black ball 8
Protocol: Write Value to Array 0 1 0
Protocol: Take Next Item from Queue 0 0 1 8
Protocol: Take Next Item from Queue 0 1 I got the coveted( 熱望 ) red ball, so I will decide my value I got the dreaded( 恐れる ) black ball, so I will decide the other’s value from the array ,[object Object],8
Consensus Using FIFO Queue public class  QueueConsensus extends  ConsensusProtocol { private Queue queue; public  QueueConsensus() { queue =  new  Queue(); queue.enq(Ball.RED); queue.enq(Ball.BLACK); } … }
Initialize Queue public class QueueConsensus extends ConsensusProtocol { private Queue queue; public QueueConsensus() { this .queue =  new  Queue(); this .queue.enq(Ball.RED); this .queue.enq(Ball.BLACK); } … } 8
Who Won? public class  QueueConsensus extends  ConsensusProtocol { private  Queue queue; … public  decide(object value) { propose(value); Ball ball =  this .queue.deq(); if  (ball == Ball.RED) return  proposed[i]; else return  proposed[1-i]; } }
Who Won? public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball =  this .queue.deq(); if (ball == Ball.RED) return proposed[i]; else return proposed[1-ij]; } } Race to dequeue first queue item
Who Won? public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball = this.queue.deq(); if  (ball == Ball.RED) return  proposed[i]; else return proposed[1-i]; } } i = ThreadID.get(); I win if I was first
Who Won? public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball = this.queue.deq(); if (ball == Ball.RED) return proposed[i]; else return  proposed[1-i]; } } Other thread wins if I was second
Why does this Work? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Theorem ,[object Object],[object Object],[object Object],[object Object]
Implications( 関わり合い ) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],contradiction (1) (1)
Corollary( 結論 ) ,[object Object],[object Object],[object Object],[object Object]
Consensus Numbers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Consensus Numbers ,[object Object],[object Object],[object Object],[object Object]
Consensus Numbers Measure Synchronization Power ,[object Object],[object Object],[object Object],[object Object]
Synchronization Speed Limit ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Theoretical Caveat: Certain weird exceptions exist 不整合時は論理的停止 する
Earlier Grand Challenge ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Multiple Assignment Theorem ,[object Object],[object Object],[object Object],[object Object],[object Object],(1) (1)
Proof Strategy  不明 ,[object Object],[object Object],[object Object],[object Object],[object Object],(1)
Proof Strategy ,[object Object],[object Object],[object Object],[object Object],(1)
Double Assignment Interface interface  Assign2 { public void  assign( int  i 1 ,  int  v 1 , int  i 2 ,  int  v 2 ); public int  read( int  i); }  (4) (4)
Double Assignment Interface interface Assign2 { public void  assign( int  i 1 ,  int  v 1 , int  i 2 ,  int  v 2 ); public int read(int i); }   (4) (4) Atomically assign  value[i 1 ]= v 1 value[i 2 ]= v 2
Double Assignment Interface interface Assign2 { public void assign(int i 1 , int v 1 , int i 2 , int v 2 ); public int  read( int  i); }   (4) (4) Return i-th value
Initially Writes to 0 and 1 Writes to 1 and 2 A B ,[object Object]
Thread  A  wins if A B (1) Thread B didn’t move
Thread  A  wins if A B (1) Thread B moved later
Thread  A  loses if A B (1) Thread B moved earlier 先に書いた方の勝ち
Multi-Consensus Code class  MultiConsensus  extends  …{ Assign2 a =  new  Assign2(3, EMPTY); public  Object decide(object value) { a.assign(i, i, i+1, i);  int  other = a.read((i+2) % 3); if  (other==EMPTY||other==a.read(1)) return  proposed[i];  else return  proposed[j];  ←j が突然出てくる ?   }} (4) (4)
Multi-Consensus Code class  MultiConsensus  extends  …{ Assign2 a =  new  Assign2(3, EMPTY); public  Object decide(object value) { a.assign(i, i, i+1, i);  int  other = a.read((i+2) % 3); if  (other==EMPTY||other==a.read(1)) return  proposed[i];  else return  proposed[j];  }} (4) (4) Extends ConsensusProtocol  Decide sets j=i-1 and proposes value
Multi-Consensus Code class MultiConsensus extends …{ Assign2 a =  new  Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i];  else return proposed[j];  }} (4) (4) Three slots initialized to EMPTY
Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i];  else return proposed[j];  }} (4) (4) Assign id 0 to entries 0,1 (or id 1 to entries 1,2)
Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int  other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i];  else return proposed[j];  }} (4) (4) Read the register my thread didn’t assign
Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if  (other==EMPTY ||other==a.read(1)) return proposed[i];  else return proposed[j];  }} (4) (4) Other thread didn’t move, so I win
Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY|| other==a.read(1)) return proposed[i];  else return proposed[j];  }} (4) (4) Other thread moved later so I win
Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return  proposed[i];  else return proposed[j];  }} (4) (4) OK, I win.
Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i];  else return  proposed[j];  }} (4) (4) Other thread moved first, so I lose (1)
Summary ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Read-Modify-Write Objects ,[object Object],[object Object],[object Object]
Read-Modify-Write public abstract class  RMWRegister { private int  value; public int synchronized  getAndMumble() { int  prior  =  this. value; this. value = mumble( this. value); return  prior; } } (1)
Read-Modify-Write public abstract class RMWRegister { private int value; public int synchronized  getAndMumble() { int  prior  =  this. value; this.value = mumble(this.value); return prior; } } (1) Return prior value
Read-Modify-Write public abstract class RMWRegister { private int value; public int synchronized  getAndMumble() { int prior  = this.value; this. value = mumble( this. value); return prior; } } (1) Apply function to current value
RMW Everywhere! ,[object Object],[object Object],[object Object],[object Object]
Example: Read public abstract class  RMWRegister { private int  value; public int synchronized  read() { int  prior  =  this. value; this. value =  this. value; return  prior; } } (1)
Example: Read public abstract class RMW { private int value; public int synchronized read() { int prior  = this.value; this. value =  this. value; return prior; } } (1) Apply  f(v)=v , the identity function
Example: getAndSet public abstract class  RMWRegister { private int  value; public int synchronized getAndSet( int  v) { int  prior  =  this. value; this. value = v; return  prior; } … } (1)
Example: getAndSet (swap) public abstract class RMWRegister { private int value; public int synchronized getAndSet(int v) { int prior  = this.value; this. value = v; return prior; } … } (1) F(x)=v is constant function
getAndIncrement public abstract class  RMWRegister { private int  value; public int synchronized getAndIncrement() { int  prior  =  this. value; this. value =  this .value + 1; return  prior; } … } (1)
getAndIncrement public abstract class RMWRegister { private int value; public int synchronized getAndIncrement() { int prior  = this.value; this. value =  this .value + 1; return prior; } … } (1) F(x) = x+1
getAndAdd public abstract class  RMWRegister { private int  value; public int synchronized getAndAdd( int  a) { int  prior  =  this. value; this. value =  this .value + a; return  prior; } … } (1)
Example: getAndAdd public abstract class RMWRegister { private int value; public int synchronized getAndIncrement(int a) { int prior  = this.value; this. value =  this .value + a; return prior; } … } (1) F(x) = x+a
compareAndSet public abstract class  RMWRegister { private int  value; public boolean synchronized compareAndSet( int  expected,  int  update) { int  prior =  this. value; if  ( this .value==expected) { this .value = update;  return true ; } return   false ; } … } (1)
compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int  expected,  int update) { int prior = this.value; if   ( this .value==expected )   { this.value = update; return true; } return false; } … } (1) If value is what was expected, …
compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected,  int  update ) { int prior = this.value; if (this.value==expected) { this .value = update;  return true; } return false; } … } (1) …  replace it
compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected,  int update) { int prior = this.value; if (this.value==expected) { this.value = update;   return true ; } return false; } … } (1) Report success
compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected,  int update) { int prior = this.value; if (this.value==expected) { this.value = update; return true; } return   false ; } … } (1) Otherwise report failure
Read-Modify-Write public abstract class RMWRegister { private int value; public void synchronized   getAndMumble() { int prior  = this.value; this.value =  mumble( this. value); return prior; } } (1) Lets characterize F(x)…
Definition ,[object Object],[object Object],[object Object],[object Object]
Par Example ,[object Object],[object Object],[object Object],[object Object]
Theorem ,[object Object],[object Object],[object Object],[object Object],[object Object]
Reminder ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Proof public class  RMWConsensus extends  ConsensusProtocol { private  RMWRegister r = v; public   Object  decide(object value) { propose(value); if  (r.getAndMumble() == v) return  proposed[i]; else return  proposed[j];  }} (4)
Proof public class RMWConsensus extends ConsensusProtocol { private  RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return proposed[i]; else return proposed[j];  }} (4) Initialized to  v
Proof public class RMWConsensus extends Consensus { private RMWRegister r = v; public Object decide(object value) { if  (r.getAndMumble() == v) return proposed[i]; else return proposed[j];  }} (4) Am I first?
Proof public class RMWConsensus extends ConsensusProtocol { private RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return  proposed[i]; else return proposed[j];  }} (4) Yes, return my input
Proof public class RMWConsensus extends ConsensusProtocol { private RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return proposed[i]; else return  proposed[j];  }} (4) No, return other’s input
Proof ,[object Object],[object Object],[object Object],[object Object]
Interfering RMW ,[object Object],[object Object],[object Object],[object Object]
Examples ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Meanwhile Back at the Critical State c 0-valent 1-valent A about to apply f A B about to apply f B
Maybe the Functions Commute c 0-valent A applies f A B applies f B A applies f A B applies f B 0 1 C runs solo C runs solo 1-valent
Maybe the Functions Commute c 0-valent A applies f A B applies f B A applies f A B applies f B 0 1 C runs solo C runs solo 1-valent These states look the same to C Contradiction
Maybe the Functions Overwrite c 0-valent A applies f A B applies f B A applies f A 0 1 C runs solo C runs solo 1-valent
Maybe the Functions Overwrite c 0-valent A applies f A B applies f B A applies f A 0 1 C runs solo C runs solo 1-valent These states look the same to C Contradiction
Impact ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
compareAndSet public abstract class  RMWRegister { private int  value; public boolean synchronized compareAndSet( int  expected,  int  update) { int  prior =  this. value; if  ( this .value==expected) { this .value = update;  return true ; } return   false ; } … } (1)
compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int  expected,  int update) { int prior = this.value; if   ( this .value==expected )   { this.value = update; return true; } return false; } … } (1) replace value if its what we expected, …
compareAndSet Has ∞ Consensus Number public class  RMWConsensus extends  ConsensusProtocol { private  AtomicInteger r = new  AtomicInteger(-1); public   Object  decide(object value) { propose(value); r.compareAndSet(-1,i); return  proposed[r.get()];  } } (4)
compareAndSet Has ∞ Consensus Number public class RMWConsensus extends ConsensusProtocol { private  AtomicInteger r = new  AtomicInteger(-1); public Object decide(object value) { propose(value) r.compareAndSet(-1,i); return proposed[r.get()];  } } (4) Initialized to  -1
compareAndSet Has ∞ Consensus Number public class RMWConsensus extends ConsensusProtocol { private AtomicInteger r = new AtomicInteger(-1); public Object decide(object value) { propose(value); r.compareAndSet(-1,i); return proposed[r.get()];  } } (4) Try to swap in my id
compareAndSet Has ∞ Consensus Number public class RMWConsensus extends ConsensusProtocol { private AtomicInteger r = new AtomicInteger(-1); public Object decide(object value) { propose(value); r.compareAndSet(-1,i); return  proposed[r.get()];  } } (4) Decide winner’s preference
The Consensus Hierarchy 1 Read/Write Registers, Snapshots… 2 getAndSet, getAndIncrement, … ∞   compareAndSet,… . . .
Multiple Assignment ,[object Object],[object Object],[object Object]
Lock-Freedom ,[object Object],[object Object],[object Object]
Lock-free と Wait-free ,[object Object]
Lock-free への書き換え ,[object Object]
Lock-free な口座入金 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Lock-Free vs. Wait-free ,[object Object],[object Object],[object Object],[object Object],[object Object]
Lock-Freedom ,[object Object],[object Object],[object Object]
Lock-Freedom ,[object Object],[object Object],[object Object],[object Object]
Lock-Free Implementations ,[object Object],[object Object],(2)
There is More: Universality ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Stay tuned…
Java.util.concurrent を使おう ,[object Object],int counter = 0;  … . counter++;  import java.util.concurrent.atomic.AtomicInteger; AtomicInteger counter = new AtomicInteger(0); … . counter.getAndIncrement(); ,[object Object]
Java.util.concurrent による並行制御 Thread-1 Thread- 2 Thread- 3 Abstract Synchronized Queue Abstract Synchronized Queue Abstract Synchronized Queue 更新先 先に完成したものを準備しておいて 一瞬で cas  で切り替えることで Lock をかけない 各スレッドからの要求を Queue で受けて、待たせない
              This work is licensed under a  Creative Commons Attribution- ShareAlike  2.5 License .  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

More Related Content

Similar to The Art of Multiprocessor Programmin Chapter 05 with Japanese comment.

CS221: HMM and Particle Filters
CS221: HMM and Particle FiltersCS221: HMM and Particle Filters
CS221: HMM and Particle Filterszukun
 
Propositional logic(part 3)
Propositional logic(part 3)Propositional logic(part 3)
Propositional logic(part 3)SURBHI SAROHA
 
Impossibility of Consensus with One Faulty Process - Papers We Love SF
Impossibility of Consensus with One Faulty Process - Papers We Love SFImpossibility of Consensus with One Faulty Process - Papers We Love SF
Impossibility of Consensus with One Faulty Process - Papers We Love SFHenryRobinson
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsKonrad Malawski
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#Rohit Rao
 
Abductive commonsense reasoning
Abductive commonsense reasoningAbductive commonsense reasoning
Abductive commonsense reasoningSan Kim
 
Quantum computing for CS students: the unitary circuit model
Quantum computing for CS students: the unitary circuit modelQuantum computing for CS students: the unitary circuit model
Quantum computing for CS students: the unitary circuit modelBruno Fedrici, PhD
 
Quasi succinct indices
Quasi succinct indicesQuasi succinct indices
Quasi succinct indicesHan Jiang
 
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVMState: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVMJonas Bonér
 
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfstateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfHiroshi Ono
 
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfstateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfHiroshi Ono
 
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfstateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfHiroshi Ono
 
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfstateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfHiroshi Ono
 
Class 36: Halting Problem
Class 36: Halting ProblemClass 36: Halting Problem
Class 36: Halting ProblemDavid Evans
 
1004_theorem_proving_2018.pptx on the to
1004_theorem_proving_2018.pptx on the to1004_theorem_proving_2018.pptx on the to
1004_theorem_proving_2018.pptx on the tofariyaPatel
 
アーベル論理でセッション型
アーベル論理でセッション型アーベル論理でセッション型
アーベル論理でセッション型Yoichi Hirai
 

Similar to The Art of Multiprocessor Programmin Chapter 05 with Japanese comment. (20)

Concur15slides
Concur15slidesConcur15slides
Concur15slides
 
CS221: HMM and Particle Filters
CS221: HMM and Particle FiltersCS221: HMM and Particle Filters
CS221: HMM and Particle Filters
 
Propositional logic(part 3)
Propositional logic(part 3)Propositional logic(part 3)
Propositional logic(part 3)
 
Chapter0
Chapter0Chapter0
Chapter0
 
Impossibility of Consensus with One Faulty Process - Papers We Love SF
Impossibility of Consensus with One Faulty Process - Papers We Love SFImpossibility of Consensus with One Faulty Process - Papers We Love SF
Impossibility of Consensus with One Faulty Process - Papers We Love SF
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applications
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#
 
Abductive commonsense reasoning
Abductive commonsense reasoningAbductive commonsense reasoning
Abductive commonsense reasoning
 
Os unit 3
Os unit 3Os unit 3
Os unit 3
 
Python unit 3 and Unit 4
Python unit 3 and Unit 4Python unit 3 and Unit 4
Python unit 3 and Unit 4
 
Quantum computing for CS students: the unitary circuit model
Quantum computing for CS students: the unitary circuit modelQuantum computing for CS students: the unitary circuit model
Quantum computing for CS students: the unitary circuit model
 
Quasi succinct indices
Quasi succinct indicesQuasi succinct indices
Quasi succinct indices
 
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVMState: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
State: You're Doing It Wrong - Alternative Concurrency Paradigms For The JVM
 
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfstateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
 
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfstateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
 
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfstateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
 
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdfstateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
stateyouredoingitwrongjavaone2009-090617031310-phpapp02.pdf
 
Class 36: Halting Problem
Class 36: Halting ProblemClass 36: Halting Problem
Class 36: Halting Problem
 
1004_theorem_proving_2018.pptx on the to
1004_theorem_proving_2018.pptx on the to1004_theorem_proving_2018.pptx on the to
1004_theorem_proving_2018.pptx on the to
 
アーベル論理でセッション型
アーベル論理でセッション型アーベル論理でセッション型
アーベル論理でセッション型
 

Recently uploaded

Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveKeep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveIES VE
 
From the origin to the future of Open Source model and business
From the origin to the future of  Open Source model and businessFrom the origin to the future of  Open Source model and business
From the origin to the future of Open Source model and businessFrancesco Corti
 
Oracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxOracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxSatishbabu Gunukula
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 
EMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarEMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarThousandEyes
 
Flow Control | Block Size | ST Min | First Frame
Flow Control | Block Size | ST Min | First FrameFlow Control | Block Size | ST Min | First Frame
Flow Control | Block Size | ST Min | First FrameKapil Thakar
 
Extra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdfExtra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdfInfopole1
 
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series  - Day 1UiPath Studio Web workshop series  - Day 1
UiPath Studio Web workshop series - Day 1DianaGray10
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInThousandEyes
 
UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3DianaGray10
 
My key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIMy key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIVijayananda Mohire
 
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Muhammad Tiham Siddiqui
 
Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.IPLOOK Networks
 
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptxEmil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptxNeo4j
 
The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)IES VE
 
How to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxHow to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxKaustubhBhavsar6
 
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptxGraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptxNeo4j
 
March Patch Tuesday
March Patch TuesdayMarch Patch Tuesday
March Patch TuesdayIvanti
 
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTxtailishbaloch
 
Automation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsAutomation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsDianaGray10
 

Recently uploaded (20)

Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveKeep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
 
From the origin to the future of Open Source model and business
From the origin to the future of  Open Source model and businessFrom the origin to the future of  Open Source model and business
From the origin to the future of Open Source model and business
 
Oracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxOracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptx
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
EMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarEMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? Webinar
 
Flow Control | Block Size | ST Min | First Frame
Flow Control | Block Size | ST Min | First FrameFlow Control | Block Size | ST Min | First Frame
Flow Control | Block Size | ST Min | First Frame
 
Extra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdfExtra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdf
 
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series  - Day 1UiPath Studio Web workshop series  - Day 1
UiPath Studio Web workshop series - Day 1
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
 
UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3UiPath Studio Web workshop Series - Day 3
UiPath Studio Web workshop Series - Day 3
 
My key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIMy key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAI
 
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)
 
Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.
 
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptxEmil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
 
The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)
 
How to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxHow to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptx
 
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptxGraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
 
March Patch Tuesday
March Patch TuesdayMarch Patch Tuesday
March Patch Tuesday
 
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
 
Automation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsAutomation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projects
 

The Art of Multiprocessor Programmin Chapter 05 with Japanese comment.

  • 1. The Relative Power of Synchronization Operations Companion slides for The Art of Multiprocessor Programming 第 5 章  by Maurice Herlihy & Nir Shavit 日本語追記 by 根本和郎
  • 2.
  • 3.
  • 4.
  • 5.
  • 6. From Weakest Register SRSW の最もシンプルなケース 1 0 1 Single reader Single writer Safe Boolean register
  • 7. All the way to a Wait-free Implementation of Atomic Snapshots MRMW MRSW SRSW Saf e Regular Atomic M-valued Boolean Snapshot
  • 8.
  • 9. Why is Mutual Exclusion so wrong? シンクロナイズドの排他制御のよくないところは・・・ (2)
  • 11. Heterogeneous Processors 直接つながっている場合遅い性能の処理に待たされることがある。 ??? ??? Yawn ( ひま ) (1) Pentium Pentium 286
  • 13.
  • 14.
  • 15. FIFO Queue: Enqueue Method q.enq ( )
  • 16. FIFO Queue: Dequeue Method q.deq()/
  • 17. Two-Thread Wait-Free Queue public class LockFreeQueue { int head = 0, tail = 0; Item[QSIZE] items; public void enq(Item x) { while (tail - head == QSIZE) {}; items[tail % QSIZE] = x; tail++; } public Item deq() { while (tail - head == 0) {} Item item = items[head % QSIZE]; head++; return item; }}
  • 18. What About Multiple Dequeuers?
  • 19.
  • 20.
  • 21.
  • 22. Consensus: Each Thread has a Private Input 32 19 21
  • 24. They Agree on One Thread’s Input 19 19 19
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32. The Two-Move Tree Initial state この時は 1/0 の 2 価の共有オブジェクト Final states (2) A moves B moves
  • 33.
  • 34. Bivalent: Both Possible 1 1 1 Bivalent(2 価 ) :2 種類の状態を取りうる 1 0 0 本ではここが 1 だが、それはそれで正しい図
  • 35. Univalent: Single Value Possible 1 1 1 univalent ここから下は 1 価に固定される 1 0 0
  • 36. x-valent: x Only Possible Decision 0 1 1 1 1-valent 0 1
  • 37.  
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45. What if inputs differ? (2) 1 0 By Way of contradiction ( 否定 ) : If univalent all executions must decide on same value
  • 46.
  • 47.
  • 48.
  • 50. From a Critical State A か B の決定が先勝ちで決まる状態 c If A goes first, protocol decides 0 If B goes first, protocol decides 1 0-valent 1-valent
  • 51. Reaching Critical State 相手側が決定しないうちはまだ univalent これはだいたいの概念図 C A C A C B c C B univalent univalent univalent univalent 0-valent 1-valent initially bivalent
  • 52.
  • 53.
  • 54.
  • 55.
  • 56. Possible Interactions ? ? ? ? y.write() ? ? ? ? x.write() ? ? ? ? y.read() ? ? ? ? x.read() y.write() x.write() y.read() x.read() A reads x A reads y
  • 57. Some Thread Reads A runs solo, eventually decides 0 B reads x 1 0 A runs solo, eventually decides 1 c States look the same to A Contradiction
  • 58. Possible Interactions ? ? no no y.write() ? ? no no x.write() no no no no y.read() no no no no x.read() y.write() x.write() y.read() x.read()
  • 59. Writing Distinct Registers A writes y B writes x 1 0 c The song is the same A writes y B writes x Contradiction
  • 60. Possible Interactions ? no no no y.write() no ? no no x.write() no no no no y.read() no no no no x.read() y.write() x.write() y.read() x.read()
  • 61. 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 Contradiction
  • 62. That’s All, Folks! QED no no no no y.write() no no no no x.write() no no no no y.read() no no no no x.read() y.write() x.write() y.read() x.read()
  • 63.
  • 64. What Does Consensus have to do with Concurrent Objects?
  • 65. Consensus Object public interface Consensus { Object decide(object value); } (4) (4)
  • 66.
  • 67.
  • 68. Generic Consensus Protocol abstract class ConsensusProtocol implements Consensus { protected Object[] proposed = new Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object decide(object value); }} (4) (4)
  • 69. Generic Consensus Protocol abstract class ConsensusProtocol implements Consensus { protected Object[] proposed = new Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object decide(object value); }} (4) (4) Each thread’s proposed value
  • 70. Generic Consensus Protocol abstract class ConsensusProtocol implements Consensus { protected Object[] proposed = new Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object decide(object value); }} (4) (4) Propose a value
  • 71. Generic Consensus Protocol abstract class ConsensusProtocol implements Consensus { protected Object[] proposed = new Object[N]; private void propose(Object value) { proposed[ThreadID.get()] = value; } abstract public Object decide(object value); }} (4) (4) Decide a value: abstract method means subclass does the heavy lifting (real work)
  • 72. Can a FIFO Queue Implement Consensus?
  • 73. FIFO Consensus proposed array FIFO Queue with red and black balls Coveted red ball Dreaded black ball 8
  • 74. Protocol: Write Value to Array 0 1 0
  • 75. Protocol: Take Next Item from Queue 0 0 1 8
  • 76.
  • 77. Consensus Using FIFO Queue public class QueueConsensus extends ConsensusProtocol { private Queue queue; public QueueConsensus() { queue = new Queue(); queue.enq(Ball.RED); queue.enq(Ball.BLACK); } … }
  • 78. Initialize Queue public class QueueConsensus extends ConsensusProtocol { private Queue queue; public QueueConsensus() { this .queue = new Queue(); this .queue.enq(Ball.RED); this .queue.enq(Ball.BLACK); } … } 8
  • 79. Who Won? public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball = this .queue.deq(); if (ball == Ball.RED) return proposed[i]; else return proposed[1-i]; } }
  • 80. Who Won? public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball = this .queue.deq(); if (ball == Ball.RED) return proposed[i]; else return proposed[1-ij]; } } Race to dequeue first queue item
  • 81. Who Won? public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball = this.queue.deq(); if (ball == Ball.RED) return proposed[i]; else return proposed[1-i]; } } i = ThreadID.get(); I win if I was first
  • 82. Who Won? public class QueueConsensus extends ConsensusProtocol { private Queue queue; … public decide(object value) { propose(value); Ball ball = this.queue.deq(); if (ball == Ball.RED) return proposed[i]; else return proposed[1-i]; } } Other thread wins if I was second
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95. Double Assignment Interface interface Assign2 { public void assign( int i 1 , int v 1 , int i 2 , int v 2 ); public int read( int i); } (4) (4)
  • 96. Double Assignment Interface interface Assign2 { public void assign( int i 1 , int v 1 , int i 2 , int v 2 ); public int read(int i); } (4) (4) Atomically assign value[i 1 ]= v 1 value[i 2 ]= v 2
  • 97. Double Assignment Interface interface Assign2 { public void assign(int i 1 , int v 1 , int i 2 , int v 2 ); public int read( int i); } (4) (4) Return i-th value
  • 98.
  • 99. Thread A wins if A B (1) Thread B didn’t move
  • 100. Thread A wins if A B (1) Thread B moved later
  • 101. Thread A loses if A B (1) Thread B moved earlier 先に書いた方の勝ち
  • 102. Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; ←j が突然出てくる ? }} (4) (4)
  • 103. Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) (4) Extends ConsensusProtocol Decide sets j=i-1 and proposes value
  • 104. Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) (4) Three slots initialized to EMPTY
  • 105. Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) (4) Assign id 0 to entries 0,1 (or id 1 to entries 1,2)
  • 106. Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) (4) Read the register my thread didn’t assign
  • 107. Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY ||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) (4) Other thread didn’t move, so I win
  • 108. Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY|| other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) (4) Other thread moved later so I win
  • 109. Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) (4) OK, I win.
  • 110. Multi-Consensus Code class MultiConsensus extends …{ Assign2 a = new Assign2(3, EMPTY); public Object decide(object value) { a.assign(i, i, i+1, i); int other = a.read((i+2) % 3); if (other==EMPTY||other==a.read(1)) return proposed[i]; else return proposed[j]; }} (4) (4) Other thread moved first, so I lose (1)
  • 111.
  • 112.
  • 113. Read-Modify-Write public abstract class RMWRegister { private int value; public int synchronized getAndMumble() { int prior = this. value; this. value = mumble( this. value); return prior; } } (1)
  • 114. Read-Modify-Write public abstract class RMWRegister { private int value; public int synchronized getAndMumble() { int prior = this. value; this.value = mumble(this.value); return prior; } } (1) Return prior value
  • 115. Read-Modify-Write public abstract class RMWRegister { private int value; public int synchronized getAndMumble() { int prior = this.value; this. value = mumble( this. value); return prior; } } (1) Apply function to current value
  • 116.
  • 117. Example: Read public abstract class RMWRegister { private int value; public int synchronized read() { int prior = this. value; this. value = this. value; return prior; } } (1)
  • 118. Example: Read public abstract class RMW { private int value; public int synchronized read() { int prior = this.value; this. value = this. value; return prior; } } (1) Apply f(v)=v , the identity function
  • 119. Example: getAndSet public abstract class RMWRegister { private int value; public int synchronized getAndSet( int v) { int prior = this. value; this. value = v; return prior; } … } (1)
  • 120. Example: getAndSet (swap) public abstract class RMWRegister { private int value; public int synchronized getAndSet(int v) { int prior = this.value; this. value = v; return prior; } … } (1) F(x)=v is constant function
  • 121. getAndIncrement public abstract class RMWRegister { private int value; public int synchronized getAndIncrement() { int prior = this. value; this. value = this .value + 1; return prior; } … } (1)
  • 122. getAndIncrement public abstract class RMWRegister { private int value; public int synchronized getAndIncrement() { int prior = this.value; this. value = this .value + 1; return prior; } … } (1) F(x) = x+1
  • 123. getAndAdd public abstract class RMWRegister { private int value; public int synchronized getAndAdd( int a) { int prior = this. value; this. value = this .value + a; return prior; } … } (1)
  • 124. Example: getAndAdd public abstract class RMWRegister { private int value; public int synchronized getAndIncrement(int a) { int prior = this.value; this. value = this .value + a; return prior; } … } (1) F(x) = x+a
  • 125. compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet( int expected, int update) { int prior = this. value; if ( this .value==expected) { this .value = update; return true ; } return false ; } … } (1)
  • 126. compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if ( this .value==expected ) { this.value = update; return true; } return false; } … } (1) If value is what was expected, …
  • 127. compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update ) { int prior = this.value; if (this.value==expected) { this .value = update; return true; } return false; } … } (1) … replace it
  • 128. compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if (this.value==expected) { this.value = update; return true ; } return false; } … } (1) Report success
  • 129. compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if (this.value==expected) { this.value = update; return true; } return false ; } … } (1) Otherwise report failure
  • 130. Read-Modify-Write public abstract class RMWRegister { private int value; public void synchronized getAndMumble() { int prior = this.value; this.value = mumble( this. value); return prior; } } (1) Lets characterize F(x)…
  • 131.
  • 132.
  • 133.
  • 134.
  • 135. Proof public class RMWConsensus extends ConsensusProtocol { private RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return proposed[i]; else return proposed[j]; }} (4)
  • 136. Proof public class RMWConsensus extends ConsensusProtocol { private RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return proposed[i]; else return proposed[j]; }} (4) Initialized to v
  • 137. Proof public class RMWConsensus extends Consensus { private RMWRegister r = v; public Object decide(object value) { if (r.getAndMumble() == v) return proposed[i]; else return proposed[j]; }} (4) Am I first?
  • 138. Proof public class RMWConsensus extends ConsensusProtocol { private RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return proposed[i]; else return proposed[j]; }} (4) Yes, return my input
  • 139. Proof public class RMWConsensus extends ConsensusProtocol { private RMWRegister r = v; public Object decide(object value) { propose(value); if (r.getAndMumble() == v) return proposed[i]; else return proposed[j]; }} (4) No, return other’s input
  • 140.
  • 141.
  • 142.
  • 143. Meanwhile Back at the Critical State c 0-valent 1-valent A about to apply f A B about to apply f B
  • 144. Maybe the Functions Commute c 0-valent A applies f A B applies f B A applies f A B applies f B 0 1 C runs solo C runs solo 1-valent
  • 145. Maybe the Functions Commute c 0-valent A applies f A B applies f B A applies f A B applies f B 0 1 C runs solo C runs solo 1-valent These states look the same to C Contradiction
  • 146. Maybe the Functions Overwrite c 0-valent A applies f A B applies f B A applies f A 0 1 C runs solo C runs solo 1-valent
  • 147. Maybe the Functions Overwrite c 0-valent A applies f A B applies f B A applies f A 0 1 C runs solo C runs solo 1-valent These states look the same to C Contradiction
  • 148.
  • 149. compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet( int expected, int update) { int prior = this. value; if ( this .value==expected) { this .value = update; return true ; } return false ; } … } (1)
  • 150. compareAndSet public abstract class RMWRegister { private int value; public boolean synchronized compareAndSet(int expected, int update) { int prior = this.value; if ( this .value==expected ) { this.value = update; return true; } return false; } … } (1) replace value if its what we expected, …
  • 151. compareAndSet Has ∞ Consensus Number public class RMWConsensus extends ConsensusProtocol { private AtomicInteger r = new AtomicInteger(-1); public Object decide(object value) { propose(value); r.compareAndSet(-1,i); return proposed[r.get()]; } } (4)
  • 152. compareAndSet Has ∞ Consensus Number public class RMWConsensus extends ConsensusProtocol { private AtomicInteger r = new AtomicInteger(-1); public Object decide(object value) { propose(value) r.compareAndSet(-1,i); return proposed[r.get()]; } } (4) Initialized to -1
  • 153. compareAndSet Has ∞ Consensus Number public class RMWConsensus extends ConsensusProtocol { private AtomicInteger r = new AtomicInteger(-1); public Object decide(object value) { propose(value); r.compareAndSet(-1,i); return proposed[r.get()]; } } (4) Try to swap in my id
  • 154. compareAndSet Has ∞ Consensus Number public class RMWConsensus extends ConsensusProtocol { private AtomicInteger r = new AtomicInteger(-1); public Object decide(object value) { propose(value); r.compareAndSet(-1,i); return proposed[r.get()]; } } (4) Decide winner’s preference
  • 155. The Consensus Hierarchy 1 Read/Write Registers, Snapshots… 2 getAndSet, getAndIncrement, … ∞ compareAndSet,… . . .
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167. Java.util.concurrent による並行制御 Thread-1 Thread- 2 Thread- 3 Abstract Synchronized Queue Abstract Synchronized Queue Abstract Synchronized Queue 更新先 先に完成したものを準備しておいて 一瞬で cas で切り替えることで Lock をかけない 各スレッドからの要求を Queue で受けて、待たせない
  • 168.

Editor's Notes

  1. We looked at shared memory computation and tried to understand what we could build using the weakest assumptions regarding our shared medium.
  2. We looked at wait-free implementations because they imply no mutual exclusion.
  3. We started with the weakest primitive we could think of.
  4. We built a collection of different synchronization objects all the way to an atomic snapshot. But what about constructing other objects like Queues. We know two thread queues can be implemented in a wait-free manner. What about queues with more than one enqueuer and dequeuer?
  5. Wait-free implementations imply that operatiosn of different threads interleave at the granularity of individual machine instructions accessing memory.
  6. As we will see later consensus can be formulated as a linearizable object in which threads in the sequential execution decide on the value of the first thread to complete.
  7. This is one of the most fundamental theorems of distributed computing, proved originally by Fischer, Lynch, and Paterson in the 1980’s. It says that there are problems that are easily solvable by reading and writing to memory on a single processor, a turing machine, but are not solvable on a multiprocessor. As we will see, this will have profound implications on multiprocessor hardware design. We will give here a proof of their theorem in the context of shared memory as proven by Herlihy.
  8. It suffices to prove the claim for two threads. Here is how we model computation histories.
  9. Imagine that all the executions still decide on one input or another…assume the claim is false and lets show the set of possible executions must then include two executions with different values.
  10. Since e
  11. A critical state is a bivalent state in which the next step by one thread will lead to a 0-valent state and the next step by the other will lead to a 1-valent state
  12. Why must there be such a critical state? Start from a bivalent state. If it is not critical, run A until about to make state 0-valent, if not a critical state, that is, if B’s step will not take the state to a 1-valent state, then run B until in is about to take system to a 1-valent state. If this state is not critical, run A again, then B, and so on. Since this is a wai-free execution, within a finite number of steps each will finish, so somewhere before they must reach a critical state.
  13. Tp prove that we cannot solve consensus using read-write registers, lets go over all the possible interactions among the threads and show that for each such execution they lead to a contradiction.
  14. This is the case in which some thread reads (and the other either reads or writes). Two possible executions, one in which A runs solo and decides 0 and another in which A runs after B takes its critical read step. Leads to a contradiction since A cannot tell that B has read…so two executions lead to the same state but the system is supposed to have two different valencies for it, a contradiction.
  15. Consensus can be formulated as a linearizable object in which threads in the sequential execution decide on the value of the first thread to complete.
  16. A linearizable object in which threads in the sequential execution decide on the value of the first thread to complete. The consensus objects we will consider will be one time objects, that is, each thread executes a method once.
  17. Note the use of a protected for proposed. A protected method is visible to inheriting classes, even not part of the same package. A package scope (default) method is not. That is the only difference between protected and package scope. method are restricted in accessing it. See example below: package one; public class A { protected int p; } //////////////////////////////// package two; import one. A; class B extends A { void myMethod() { p = 1; // ok A a = new A(); a.p = 1; // not ok, p would have to be public for this to work. } }
  18. The exceptions have to do with special non-deterministic objects or with how threads “bind” to the object or with the objects being “oblivious”. See chapter for details. The following is a summary from a paper on the subject by Hadzilacos et. al: Hadzilacos et al prove that if types can be nondeterministic and nonoblivious, and each process is required to bind permanently to at most one port of an object, then hrm is not robust. Concurrently with the rst appearance of our result [CHJT94], there appeared two papers proving the robustness of hrm in certain models [BGA94, PBN94]. Specically, Borowsky, Gafni, and Afek show that if types must be deterministic and oblivious, each process may bind to several ports of the same object, and the process-to-port binding is transient, then hrm is robust [BGA94]. Peterson, Bazzi, and Neiger show that if types must be deterministic, each process may bind to several ports of the same object, but the process-to-port binding is permanent, then hrm is robust [PBN94]. More nonrobustness results have appeared recently. Moran and Rappoport [MR96] strengthen our result by proving that hrm is not robust even if types must be deterministic, as long as each process must be permanently bound to at most one port of an object. Lo and Hadzilacos [LH97] prove that hrm is not robust if types can be nondeterministic, regardless of whether they are oblivious and of how processes may bind to ports. Schenk proves a result similar to the one in [LH97], but his result requires innite nondeterminism and applies only for a stronger denition of wait-free implementation [Sch96].
  19. To save space and make the code clearer we wrote that it extends consensus while it actually extends ConsensusProtocol and omitted that Decide sets j=i-1 and then proposes value. Assume this appears in all the code.
  20. NOTE: Lock-free is the same as wait-free if the execution is finite. “ Lock-free is to wait-free as deadlock-free is to lockout-free.” In other words, Lock-free and Deadlock-free are “stalinistic”, preferring group progress, while Wait-free and Lockout-free guarantee individual progress. Any wait-free implementation is lock-free.
  21. NOTE: Lock-free is the same as wait-free if the execution is finite. “ Lock-free is to wait-free as deadlock-free is to lockout-free.” In other words, Lock-free and Deadlock-free are “stalinistic”, preferring group progress, while Wait-free and Lockout-free guarantee individual progress. Any wait-free implementation is lock-free.
  22. NOTE: Lock-free is the same as wait-free if the execution is finite. “ Lock-free is to wait-free as deadlock-free is to lockout-free.” In other words, Lock-free and Deadlock-free are “stalinistic”, preferring group progress, while Wait-free and Lockout-free guarantee individual progress. Any wait-free implementation is lock-free.
  23. NOTE: Lock-free is the same as wait-free if the execution is finite. “ Lock-free is to wait-free as deadlock-free is to lockout-free.” In other words, Lock-free and Deadlock-free are “stalinistic”, preferring group progress, while Wait-free and Lockout-free guarantee individual progress. Any wait-free implementation is lock-free.
  24. Consensus can be formulated as a linearizable object in which threads in the sequential execution decide on the value of the first thread to complete.