the art of multiprocessor programmin chapter 05 with japanese comment

168
The Relative Power of Synchronization Operations Companion slides for The Art of Multiprocessor Programming 第 5 第 by Maurice Herlihy & Nir Shavit 第第第第第 by 第第第第

Upload: kazuo-nemoto

Post on 15-Jan-2015

16.582 views

Category:

Technology


0 download

DESCRIPTION

「並行プログラミングの原理から実践まで」 日本語解説追記版。

TRANSCRIPT

Page 1: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

The Relative Power of Synchronization

Operations

Companion slides forThe Art of Multiprocessor Programming 第 5 章  by

Maurice Herlihy & Nir Shavit日本語追記 by 根本和郎

Page 2: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 2

並行プログラミングの原理から実践まで

• この powerpoint は 「並行プログラミングの原理から実践まで」第 5 章を読むための補助教材です。

• 本とはカバー範囲が異なります。またコードサンプルや図も本とは微妙に異なっているので併読時は注意しましょう。

Page 3: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 3

Last Lecture: Shared-Memory Computability

• Mathematical model of concurrent computation

• What is (and is not) concurrently computable• Efficiency (mostly) irrelevant

1001110011

Shared Memory

Page 4: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 4

Wait-Free Implementation• Every method call completes in

finite number of steps• Implies no mutual exclusion• 各メソッド呼び出しが有限数のステッ

プで完了する場合、その平行オブジェクト実装は無待機である。

(2)

Page 5: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 5

コンセンサス・プロトコル• コンセンサスオブジェクト

– public interface Consensus<T> {– T decide(T, value);– }

• 参加するオブジェクトは皆、 decide() という判定メソッドを持つ。

Page 6: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 6

From Weakest RegisterSRSW の最もシンプルなケース

1

0 11

Single readerSingle writer

Safe Boolean register

Page 7: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 7

All the way to a Wait-free Implementation of Atomic

Snapshots

MRMW

MRSW

SRSW

SafeRegular

Atomic

M-valued

Boolean

Snapshot

Page 8: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 8

Rationale for wait-freedom

• We wanted atomic registers to implement mutual exclusion

• So we couldn’t use mutual exclusion to implement atomic registers

• But wait, there’s more!• 選択排他で動作可能な、アトミックなレジス

ターがあれば良いのだが・・・• しかしそれがなくても、単にメソッドを

synchronized すること以上の Lock-free な何かがあるのではないか。

Page 9: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Why is Mutual Exclusion so wrong?

シンクロナイズドの排他制御のよくないところは・・

(2)

Page 10: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 10

Asynchronous Interrupts同期的だと一人に休まれると、一緒に働いている人も休まなくてはなら

ない

Swapped outback at

??? ???

(2)

Page 11: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 11

Heterogeneous Processors直接つながっている場合遅い性能の

処理に待たされることがある。??? ???

Yawn

( ひま )

(1)

Pentium

Pentium

286

Page 12: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 12

Fault-tolerance一人のダウンが致命的

??? ???

(2)

Page 13: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 13

Machine Level Instruction Granularity

Eugene Amdahl

(2)

• マシンごとでアクセスする粒度が一致しないかもしれない

Page 14: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 14

Basic Questions• Wait-Free synchronization might be

a good idea in principle• But how do you do it

– Systematically?– Correctly?– Efficiently?

• wait-Free な同期は、ここでは java.util.Concurrent を使いこなしましょう、という話しへの導入

Page 15: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 15

FIFO Queue: Enqueue Method

q.enq( )

Page 16: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 16

FIFO Queue: Dequeue Method

q.deq()/

Page 17: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 17

Two-Thread Wait-Free Queuepublic 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;}}

Page 18: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 18

What About Multiple Dequeuers?

Page 19: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 19

Grand Challenge

• Implement a FIFO queue– Wait-free (FIFO は再試行がない )– Linearizable– From atomic read-write registers– Multiple dequeuers

Only new aspect

(1)

Page 20: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 20

Consensus

• While you are ruminating( 反復思考 ) on the grand challenge…

• We will give you another puzzle– Consensus– Will be important …

Page 21: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 21

コンセンサス問題

• 分散システム上の全プロセス(または全正常プロセス)が同じ値( 0 または 1 )で合意する問題– 複数プロセスである処理を行っているとき、全体

の状況(進捗状態、分岐条件、終了条件)に関して全プロセスで合意が必要な場合あり

– 信頼性のための多重化システムでは、各サブシステムでの出力の合意として最終出力を決定

Page 22: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 22

Consensus: Each Thread has a Private Input

32 19

21

Page 23: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 23

They Communicate

Page 24: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 24

They Agree on One Thread’s Input

19 19

19

Page 25: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 25

Formally: Consensus

Consistent: all threads decide the same value

Valid: the common decision value is some thread's input

整合性がある : 全員が一致有効 : 多数決的に正しい

Page 26: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 26

No Wait-Free Implementation of

Consensus using Registers??? ???

• wait-Free な結合だと、障害時に同意形成できない

Page 27: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 27

Formally

• Theorem [adapted from Fischer, Lynch, Paterson]: There is no wait-free implementation of n-thread consensus, n>1, from read-write registers

• Implication: asynchronous computability fundamentally different from Turing computability

PODC2001 のInfluential paper

award

CAP 理論の元

Page 28: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 28

Proof Strategy• Assume otherwise• Reason about the properties of any such

protocol• Derive a contradiction ( 矛盾を派生しうる )• Quod Erat Demonstrandum• Suffices to prove for binary consensus

and n=2• バイナリコンセンサスとは、つまり並行処理

を 2 分岐探索に置換して評価する確率分布• この最小構成で検討してみる。

Page 29: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 29

Wait-Free Computation

• Either A or B “moves”• Moving means

– Register read– Register write

• 操作 A/B がある、 A は読む B は書く• 各スレッドは最大 1回の意思決定をする、つまり decide() メ

ソッド

A moves読む

B moves書く

Lemma = 補題 (dilemma: ジレンマ )

Page 30: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 30

A スレッドと B スレッドの話

• A スレッドと B スレッドが 2回ずつ動くモデルで考える。

Page 31: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 31

バイナリコンセンサス• 次ページで語ろうとしているのはバイナリコンセンサ

ス、• つまり 1/0 判定の最も基本的意思決定• 登場スレッドは A と Bのふたつ• 初期値は不定 (1 か 0)• 並行コンセンサスを逐次コンセンサスに線形化して考える

• 初期状態として A は 0 を、 Bは 1 を受け取ると仮定する

• Read と Write がどのようにインターリーブされるかで決まる

• 論理的分岐ではなく確率分布図のようなものとして考える

Page 32: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 32

The Two-Move TreeInitial state

この時は 1/0の 2 価の共有オブジェクト

Final states

(2)

A movesB moves

Page 33: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 33

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 と書く

• 下図を深く分析してはいけない、もっとざっくりしたもの。

Page 34: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 34

Bivalent: Both Possible

1 1 1

Bivalent(2価 ):2種類の状態を取りうる

1 0 0本ではここが 1 だが、それ

はそれで正しい図

Page 35: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 35

Univalent: Single Value Possible

1 1 1

univalentここから下は 1 価に固定される

1 0 0

Page 36: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 36

x-valent: x Only Possible Decision

0 1 1 1

1-valent

01

Page 37: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 37

Page 38: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 38

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• Wait-free は木構造の計算モデル• Bivalent の状態は不定• Univalent は固定した状態だが未観測

Page 39: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 39

まとめ

•下記の順で状態遷移していく– Bivalent 不定– Univalent 確定しているが未観測– 0-Valent ,1-Valent 確定

Page 40: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 40

Claim

• Some initial state is bivalent• Outcome depends on

– Chance– Whim(気まぐれ ) of the scheduler

• Multiprocessor gods do play dice …

• Lets prove this claim

Page 41: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 41

Both Inputs 0

Univalent: all executions must decide 0

00

(2) (1)

Page 42: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 42

Both Inputs 0

Including this solo execution by A

(1) (1)

0

Page 43: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 43

Both Inputs 1

All executions must decide 1

11

(2) (1)

Page 44: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 44

Both Inputs 1

Including this solo execution by B

(1) (1)

1

Page 45: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 45

What if inputs differ?

(2)

10

By Way of contradiction( 否定 ): If univalentall executions must decide on same value

Page 46: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 46

The Possible Executions

0 1

(2)

Include the solo execution by Athat decides 0

Page 47: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 47

The Possible Executions

0 1

(2)

Also include the solo execution by Bwhich we know decides 1

Page 48: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 48

Possible Executions Include

• Solo execution by A must decide 0

• Solo execution by B must decide 1

0 1

How univalent is that?(QED)

How univalent is that?(QED)

Page 49: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 49

0-valent

Critical States次のステップで状態が絞り込まれていく

ステートのこと

1-valent

critical

(3) (3)

Page 50: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 50

From a Critical StateA か Bの決定が先勝ちで決まる

状態

c

If A goes first, protocol decides 0

If B goes first, protocol decides 1

0-valent 1-valent

Page 51: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 51

Reaching Critical State相手側が決定しないうちはまだ

univalentこれはだいたいの概念図

CA

CA

CB

c

CB

univalent

univalent

univalent

univalent

0-valent 1-valent

initially bivalent

Page 52: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 52

Critical States

• Starting from a bivalent initial state

• The protocol can reach a critical state– Otherwise we could stay bivalent

forever– And the protocol is not wait-free

Page 53: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 53

Model Dependency• So far, memory-independent!• True for

– Registers– Message-passing– Carrier pigeons– Any kind of asynchronous

computation• メモリそのものに特殊性は要求しない

ので、レジスタ、伝書鳩、メッセージング、全ての非同期に適応可能

Page 54: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 54

Read-Write Memory

• Reads and/or writes• To same/different registers

Page 55: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 55

Completing the Proof

• Lets look at executions that:– Start from a critical state– Threads cause state to become

univalent by reading or writing to same/different registers

– End within a finite number of steps deciding either 0 or 1

• Show this leads to a contradiction

Page 56: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 56

Possible Interactions

x.read() y.read() x.write() y.write()

x.read() ? ? ? ?

y.read() ? ? ? ?

x.write() ? ? ? ?

y.write() ? ? ? ?

A reads xA reads y

Page 57: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 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

AContra

dictio

n

Page 58: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 58

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 59: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 59

Writing Distinct Registers

A writes y B writes x

10

c

The song is the same

A writes yB writes x

Contradict

ion

Page 60: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 60

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 61: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 61

Writing Same Registers

States look the same to

A

A writes x B writes x

1A runs solo, eventually decides 1

c

0

A runs solo, eventually decides 0 A writes x

Contradict

ion

Page 62: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 62

That’s All, Folks!

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 no

y.write() no no no noQED

Page 63: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 63

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 64: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 64

What Does Consensus have to do with Concurrent

Objects?

Page 65: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 65

Consensus Object

public interface Consensus { Object decide(object value);}

(4) (4)

Page 66: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 66

Concurrent Consensus Object

• We consider only one time objects: each thread can execute a method only once

• Linearizable to sequential consensus object in which – the thread who’s input was decided

on completed its method first

Page 67: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 67

Java Jargon Watch

• Define Consensus protocol as an abstract class

• We implement some methods• Leave you to do the rest …

Page 68: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 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)

Page 69: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 69

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

Generic Consensus Protocol

(4) (4)

Each thread’s proposed value

Page 70: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 70

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

Generic Consensus Protocol

(4) (4)

Propose a value

Page 71: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 71

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

Generic Consensus Protocol

(4) (4)

Decide a value: abstract method means subclass does the heavy

lifting (real work)

Page 72: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Can a FIFO Queue Implement Consensus?

Page 73: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 73

FIFO Consensus

proposed array

FIFO Queue with red and black balls

8

Coveted red ball Dreaded black ball

Page 74: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 74

Protocol: Write Value to Array

0 10

Page 75: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 75

0

Protocol: Take Next Item from Queue

0 18

Page 76: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 76

0 1

Protocol: Take Next Item from Queue

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 array8

• ハズレを引くことで、相手が有効だとわかる

Page 77: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 77

public class QueueConsensus extends ConsensusProtocol { private Queue queue; public QueueConsensus() { queue = new Queue(); queue.enq(Ball.RED); queue.enq(Ball.BLACK); } …}

Consensus Using FIFO Queue

Page 78: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 78

public class QueueConsensus extends ConsensusProtocol { private Queue queue; public QueueConsensus() { this.queue = new Queue(); this.queue.enq(Ball.RED); this.queue.enq(Ball.BLACK); } …}

Initialize Queue

8

Page 79: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 79

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?

Page 80: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 80

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

Who Won?

Race to dequeue first queue item

Page 81: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 81

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?

i = ThreadID.get();I win if I was

first

Page 82: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 82

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?

Other thread wins if I was second

Page 83: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 83

Why does this Work?

• If one thread gets the red ball• Then the other gets the black ball• Winner decides her own value• Loser can find winner’s value in

array– Because threads write array– Before dequeueing from queue

Page 84: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 84

Theorem

• We can solve 2-thread consensus using only– A two-dequeuer queue, and– Some atomic registers

• Dequeuer がふたつあっても、自分が黒を引いたときは他がアカを引き当てたと想定できるので協調的に動作できる。

Page 85: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 85

Implications( 関わり合い )

• Given– A consensus protocol from queue and

registers

• Assume there exists– A queue implementation from atomic

registers

• Substitution yields:– A wait-free consensus protocol from atomic

registers contradiction

(1) (1)

Page 86: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 86

Corollary( 結論 )

• It is impossible to implement – a two-dequeuer wait-free FIFO queue– from read/write memory.

• Wait free の 2 つの dequeuer は整合性がとれないので、構成不可能。

Page 87: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 87

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 88: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 88

Consensus Numbers

• Theorem– Atomic read/write registers have

consensus number 1

• Theorem– Multi-dequeuer FIFO queues have

consensus number at least 2

Page 89: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 89

Consensus Numbers Measure Synchronization Power

• Theorem– If you can implement X from Y– And X has consensus number c– Then Y has consensus number at

least c

Page 90: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 90

Synchronization Speed Limit

• Conversely– If X has consensus number c– And Y has consensus number d < c– Then there is no way to construct a

wait-free implementation of X by Y• This theorem will be very useful

– Unforeseen practical implications!• コンセンサス数は減らすことは出来な

Theoretical

Caveat: Certain

weird exceptions

exist不整合時は論理的停止する

Page 91: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 91

Earlier Grand Challenge

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

• What about– Write multiple array elements atomically– Scan any array elements

• Call this problem multiple assignment

Page 92: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 92

Multiple Assignment Theorem

• Atomic registers cannot implement multiple assignment

• Weird or what?– Single location write/multi location

read OK– Multi location write/multi location

read impossible

• 一つなら可能だが、 Array の一括更新はアトミックには出来ない

(1) (1)

Page 93: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 93

Proof Strategy  不明

• If we can write to 2/3 array elements– We can solve 2-consensus– Impossible with atomic registers

• Therefore– Cannot implement multiple

assignment with atomic registers

(1)

Page 94: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 94

Proof Strategy

• Take a 3-element array– A writes atomically to slots 0 and 1– B writes atomically to slots 1 and 2– Any thread can scan any set of

locations

(1)

Page 95: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 95

Double Assignment Interface

interface Assign2 { public void assign(int i1, int v1, int i2, int v2); public int read(int i);}

(4) (4)

Page 96: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 96

Double Assignment Interface

interface Assign2 { public void assign(int i1, int v1, int i2, int v2); public int read(int i);}

(4) (4)

Atomically assign value[i1]= v1

value[i2]= v2

Page 97: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 97

Double Assignment Interface

interface Assign2 { public void assign(int i1, int v1, int i2, int v2); public int read(int i);}

(4) (4)

Return i-th value

Page 98: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 98

Initially

Writes to 0 and

1

Writes to 1 and

2

A

B

• 一つのスレッドが二つ以上を書く場合の競合の解説

Page 99: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 99

Thread A wins if

A

B

(1)

Thread B didn’t move

Page 100: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 100

Thread A wins if

A

B

(1)

Thread B moved later

Page 101: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 101

Thread A loses if

A

B

(1)

Thread B moved earlier

先に書いた方の勝ち

Page 102: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 102

Multi-Consensus Codeclass 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)

Page 103: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 103

Multi-Consensus Codeclass 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

Page 104: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 104

Multi-Consensus Codeclass 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

Page 105: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 105

Multi-Consensus Codeclass 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)

Page 106: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 106

Multi-Consensus Codeclass 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

Page 107: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 107

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

Multi-Consensus Code

(4) (4)

Other thread didn’t move, so I

win

Page 108: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 108

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

Multi-Consensus Code

(4) (4)

Other thread moved later so I

win

Page 109: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 109

Multi-Consensus Codeclass 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.

Page 110: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 110

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

Multi-Consensus Code

(4) (4)

(1)

Other thread moved first, so I

lose

Page 111: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 111

Summary

• If a thread can assign atomically to 2 out of 3 array locations

• Then we can solve 2-consensus• Therefore

– No wait-free multi-assignment– From read/write registers

•従って 3つの Array でもコンセンサスプロトコルは成立する

Page 112: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 112

Read-Modify-Write Objects

• Method call– Returns object’s prior value x– Replaces x with mumble(x)

Page 113: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 113

public abstract class RMWRegister { private int value;

public int synchronized getAndMumble() { int prior = this.value; this.value = mumble(this.value); return prior; }}

Read-Modify-Write

(1)

Page 114: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 114

public abstract class RMWRegister { private int value;

public int synchronized getAndMumble() { int prior = this.value; this.value = mumble(this.value); return prior; }}

Read-Modify-Write

(1)

Return prior value

Page 115: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 115

public abstract class RMWRegister { private int value;

public int synchronized getAndMumble() { int prior = this.value; this.value = mumble(this.value); return prior; }}

Read-Modify-Write

(1)

Apply function to current value

Page 116: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 116

RMW Everywhere!

• Most synchronization instructions– are RMW methods

• The rest– Can be trivially transformed into RMW

methods

Page 117: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 117

public abstract class RMWRegister { private int value;

public int synchronized read() { int prior = this.value; this.value = this.value; return prior; }

}

Example: Read

(1)

Page 118: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 118

public abstract class RMW { private int value;

public int synchronized read() { int prior = this.value; this.value = this.value; return prior; }

}

Example: Read

(1)

Apply f(v)=v, the identity

function

Page 119: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 119

public abstract class RMWRegister { private int value;

public int synchronized getAndSet(int v) { int prior = this.value; this.value = v; return prior; } …}

Example: getAndSet

(1)

Page 120: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 120

public abstract class RMWRegister { private int value;

public int synchronized getAndSet(int v) { int prior = this.value; this.value = v; return prior; } …}

Example: getAndSet (swap)

(1)

F(x)=v is constant function

Page 121: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 121

public abstract class RMWRegister { private int value;

public int synchronized getAndIncrement() { int prior = this.value; this.value = this.value + 1; return prior; } …}

getAndIncrement

(1)

Page 122: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 122

public abstract class RMWRegister { private int value;

public int synchronized getAndIncrement() { int prior = this.value; this.value = this.value + 1; return prior; } …}

getAndIncrement

(1)

F(x) = x+1

Page 123: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 123

public abstract class RMWRegister { private int value;

public int synchronized getAndAdd(int a) { int prior = this.value; this.value = this.value + a; return prior; } …}

getAndAdd

(1)

Page 124: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 124

public abstract class RMWRegister { private int value;

public int synchronized getAndIncrement(int a) { int prior = this.value; this.value = this.value + a; return prior; } …}

Example: getAndAdd

(1)

F(x) = x+a

Page 125: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 125

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

compareAndSet

(1)

Page 126: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 126

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

compareAndSet

(1)

If value is what was expected, …

Page 127: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 127

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

compareAndSet

(1)

… replace it

Page 128: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 128

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

compareAndSet

(1)

Report success

Page 129: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 129

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

compareAndSet

(1)

Otherwise report failure

Page 130: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 130

public abstract class RMWRegister { private int value;

public void synchronized getAndMumble() { int prior = this.value; this.value = mumble(this.value); return prior; }}

Read-Modify-Write

(1)

Lets characterize F(x)…

Page 131: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 131

Definition

• A RMW method– With function mumble(x)– is non-trivial if there exists a

value v – Such that v ≠ mumble(v)

Page 132: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 132

Par Example

• Identity(x) = x– is trivial

• getAndIncrement(x) = x+1– is non-trivial

Page 133: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 133

Theorem

• Any non-trivial RMW object has consensus number at least 2

• No wait-free implementation of RMW registers from atomic registers

• Hardware RMW instructions not just a convenience

• ハードウェアによるアトミックレジスターもある、それにより利便性のみならず高速性が期待できる。

• RMW(Read-Modify-Write) は、この本による造語

Page 134: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 134

Reminder

• Subclasses of consensus have– propose(x) method

• which just stores x into proposed[i]• built-in method

– decide(object value) method• which determines winning value• customized, class-specific method

Page 135: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 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)

Page 136: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 136

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

Proof

(4)

Initialized to v

Page 137: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 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?

Page 138: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 138

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

Proof

(4)

Yes, return my input

Page 139: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 139

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

Proof

(4)

No, return other’s input

Page 140: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 140

Proof

• We have displayed– A two-thread consensus protocol– Using any non-trivial RMW object

• レジスター値の置換はアトミックに実行可能な証明。

Page 141: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 141

Interfering RMW

• Let F be a set of functions such that for all fi and fj, either

– Commute: fi(fj(v))=fj(fi(v))

– Overwrite: fi(fj(v))=fi(v)

• Claim: Any set of RMW objects that commutes or overwrites has consensus number exactly 2

Page 142: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 142

Examples

• “test-and-set” getAndSet(1) f(v)=1

• “swap” getAndSet(x) f(v,x)=x

• “fetch-and-inc” getAndIncrement() f(v)=v+1

Overwrite fi(fj(v))=fi(v)

Overwrite fi(fj(v))=fi(v)

Commute fi(fj(v))= fj(fi(v))• コンセンサス数が 2 ならば、アトミックに出来ると言っている。

Page 143: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 143

Meanwhile Back at the Critical State

c

0-valent 1-valent

A about to apply fA

B about to apply fB

Page 144: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 144

Maybe the Functions Commute

c

0-valent

A applies fA B applies fB

A applies fAB applies fB

0 1

C runs solo C runs solo

1-valent

Page 145: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 145

Maybe the Functions Commute

c

0-valent

A applies fA B applies fB

A applies fAB applies fB

0 1

C runs solo C runs solo

1-valent

These states look the same to CThese states look the same to C

Contradict

ion

Page 146: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 146

Maybe the Functions Overwrite

c

0-valent

A applies fA B applies fB

A applies fA

0

1

C runs solo

C runs solo

1-valent

Page 147: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 147

Maybe the Functions Overwrite

c

0-valent

A applies fA B applies fB

A applies fA

0

1

C runs solo

C runs solo

1-valent

These states look the same to CThese states look the same to C

Contradict

ion

Page 148: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 148

Impact

• Many early machines provided these “weak” RMW instructions– Test-and-set (IBM 360)– Fetch-and-add (NYU Ultracomputer)– Swap (Original SPARCs)

• We now understand their limitations– But why do we want consensus anyway?

Page 149: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 149

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

compareAndSet

(1)

Page 150: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 150

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

compareAndSet

(1)

replace value if its what we expected, …

Page 151: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 151

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

compareAndSet Has ∞ Consensus Number

(4)

Page 152: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 152

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

compareAndSet Has ∞ Consensus Number

(4)

Initialized to -1

Page 153: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 153

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

compareAndSet Has ∞ Consensus Number

(4)

Try to swap in my id

Page 154: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 154

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

compareAndSet Has ∞ Consensus Number

(4)

Decide winner’s

preference

Page 155: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 155

The Consensus Hierarchy

1 Read/Write Registers, Snapshots…

2 getAndSet, getAndIncrement, …

∞ compareAndSet,…

.

.

.

Page 156: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 156

Multiple Assignment

• Atomic k-assignment• Solves consensus for 2k-2 threads• Every even consensus number has

an object (can be extended to odd numbers)

Page 157: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 157

Lock-Freedom

• Lock-free: in an infinite execution infinitely often some method call finishes (obviously, in a finite number of steps)

• Pragmatic approach• Implies no mutual exclusion

Page 158: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 158

Lock-free と Wait-freeLock-free と Wait-free アルゴリズムとは、共有データに

ロックをかけて保護するアルゴリズムとは違い、複数のスレッドが同 時並行的に、ある対象データを壊すことなしに読み書きすることを可能にするアルゴリズムである。 Lock-free とはスレッドがロックしないことを意味しており、全てのステップにおいてシステムが必ず進行する。通常ロックを所有するスレッドは、コンテキストスイッチ が発生した時点で、全体の進行を阻止することがあるので、 Look-free ではミューテックスやセマフォといった、排他制御のためのプリミティブが使われていないということになる。 Wait-free とは、他のスレッドの動作に関係なく、スレッドがいかなる操作も有限のステップで操作を完了させられることを指す。全てのアルゴリズムを Lock-free にすることが出来るが、一部しか Wait-free にできない。 Wait-free なアルゴリズムは Lock-free である。

Page 159: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 159

Lock-free への書き換え• 例えば、銀行口座への預金プログラムを作るとする。それぞ

れのスレッドを ATM とする。預金預け入れをするには、現在の預金残高を読み出し、預入金額を足し算し、新しい預金残高を書き込むという処理が必要である。ロック方式のやり方の場合、 1 つ目の ATM が預金をするために、口座にロックをかけ、 2 つ目の ATM が同時に預金残高を変更できないようにロックをかける。さもないと、同時に処理してしまうと、最終的な預金残高に不整合が起きうる。この処理を Lock-free にするには、すべての預入要求を管理するスレッドを作り、そこに、 Wait-free のキューを作り、 ATM はそのキューに対して非同期にロックをかけることなく預入要求を入れ、預入要求を管理するスレッドはキューから順次取り出し、預金残高を更新する。このやり方の方が、わざわざ Lock-free の預金アルゴリズムを作るよりも、プログラミングは楽である。さらに、この手法は、キューが Wait-free であるので、 Lock-free なだけでなく、 Wait-free でもある。預金残高の書き換え処理を n 並列で行いたいなら、 n 個Wait-freeキューを作り、口座番号を n で割った余りでどのキューに入れるか決めるという方法で対応できる。

Page 160: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 160

Lock-free な口座入金• Wait-free な queue が ATM台数分ある。• 入金処理はそれぞれの queue が wait-free

で受け付ける。•対象のひとつの口座の更新は、 Lock-free

に cas で更新をかける、 cas更新は待たせないが、失敗することもある。

• したがって wait-free ではない、失敗したら再試行する必要があるためだ。

• しかし synchronozed メソッドよりは格段にスケールする。

Page 161: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 161

Lock-Free vs. Wait-free• Wait-Free: each method call takes

a finite number of steps to finish– Lock を使って、資源を排他管理する

• Lock-free: infinitely often some method call finishes– Lock なしは、常に成立するわけではない– compareAndSwap() でロックなし更新す

Page 162: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 162

Lock-Freedom

• Any wait-free implementation is lock-free.

• Lock-free is the same as wait-free if the execution is finite.

• Old saying: “Lock-free is to wait-free as deadlock-free is to lockout-free.”

Page 163: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 163

Lock-Freedom

• wait-free ならそれは lock-free. • Lock-free は wait-free と等価 .• すべてを Lock-free にできるわけで

はない . • Old saying: “Lock-free is to wait-

free as deadlock-free is to lockout-free.”

Page 164: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 164

Lock-Free Implementations

• Lock-free consensus is as impossible as wait-free consensus

• All the results we presented hold for lock-free algorithms also.

(2)

Page 165: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 165

There is More: Universality

• Consensus is universal• From n-thread consensus

– Wait-free/Lock-free– Linearizable– n-threaded– Implementation– Of any sequentially specified objectStay tu

ned…

Page 166: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 166

Java.util.concurrent を使おう

• ++ の操作は atomic ではないので危険int counter = 0; ….counter++;

import java.util.concurrent.atomic.AtomicInteger;AtomicInteger counter = new AtomicInteger(0);….counter.getAndIncrement();

• これなら複数スレッドから lock-free に更新可能

Page 167: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 167

Java.util.concurrent による並行制御Thread-1

Thread- 2

Thread- 3

Abstract Synchronized Queue

Abstract Synchronized Queue

Abstract Synchronized Queue

更新先

先に完成したものを準備しておいて一瞬で cas で切り替えることで

Lock をかけない

各スレッドからの要求を Queue で受けて、待たせない

Page 168: The Art of Multiprocessor Programmin Chapter 05 with Japanese comment

Art of Multiprocessor Programming 168

This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.

• You are free:– to Share — to copy, distribute and transmit the work – to Remix — to adapt the work

• Under the following conditions:– Attribution. You must attribute the work to “The Art of

Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work).

– Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.

• For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to– http://creativecommons.org/licenses/by-sa/3.0/.

• Any of the above conditions can be waived if you get permission from the copyright holder.

• Nothing in this license impairs or restricts the author's moral rights.