43
Stefan Schmid @ T-Labs, 2011 Network Algorithms Distributed Counting

Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Stefan Schmid @ T-Labs, 2011

Network Algorithms

Distributed Counting

Page 2: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Counting

Stefan Schmid @ T-Labs Berlin, 2013/4 2

Distributed Counting

Counter is a shared variable, supporting atomic

Test-and-Increment. The requesting processor

Gets the value and the counter is then incremented.

How to implement?

Applications?

E.g., load-balancing!

Send to server i (mod n)…

Or: renaming identifiers to {1,…,n}

Page 3: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Simple Solution

Stefan Schmid @ T-Labs Berlin, 2013/4

Simple Algo Send to some node v, v replies and increments.

3

“I have counter!”

Far away, congestion, …:

Not very scalable

Can I count in a

decentralized way??

Page 4: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

A crazy idea?

Stefan Schmid @ T-Labs Berlin, 2013/4 4

Batcher networks permute values: use it for balancing the counting?!

Page 5: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

A crazy idea?

Stefan Schmid @ T-Labs Berlin, 2013/4 5

Batcher networks permute values: use it for balancing the counting?!

Idea 1: replace comparators

with perfect balancers!

Page 6: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Balancer

Stefan Schmid @ T-Labs Berlin, 2013/4 6

Balancer (B)

Values at “in” are sent to upper and lower “out” in turns:

out1 out2

in1 in2

Simply a “toggler”:

Page 7: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Balancer Example

Stefan Schmid @ T-Labs Berlin, 2013/4 7

Assume this time of arrival:

7 6 4 2 1 5 3

Order at output?

Page 8: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Balancer Example

Stefan Schmid @ T-Labs Berlin, 2013/4 8

Assume this time of arrival:

7 6 4 2 1 5 3

The top wire has one more than the lower wire!

Coincidence?

1 3 5 7

2 4 6

Page 9: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Balancer Example

Stefan Schmid @ T-Labs Berlin, 2013/4 9

Assume this time of arrival:

7 6 4 2 1 5 3

The top wire has one more than the lower wire!

Coincidence?

1 3 5 7

2 4 6

No, will always be almost perfectly balanced!

Since first goes to top: top may have 1 more in case

of an odd number of requests…

Page 10: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

A crazy idea?

Stefan Schmid @ T-Labs Berlin, 2013/4 10

Batcher networks permute values: use it for balancing the counting?!

Idea 2: add counters at end!

Mini-Counters: distributed!

min

i-co

unte

rs

Page 11: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

A crazy idea?

Stefan Schmid @ T-Labs Berlin, 2013/4 11

Batcher networks permute values: use it for balancing the counting?!

Idea 3: to count, just send

a request through network!

(Routed by balancers…)

min

i-counte

rs

inc,inc,inc!

inc,inc!

Page 12: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

A crazy idea?

Stefan Schmid @ T-Labs Berlin, 2013/4 12

Batcher networks permute values: use it for balancing the counting?!

Idea 3: to count, just send

a request through network!

(Routed by balancers…)

min

i-counte

rs

inc,inc,inc!

inc,inc!

Page 13: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

A crazy idea?

Stefan Schmid @ T-Labs Berlin, 2013/4 13

Batcher networks permute values: use it for balancing the counting?!

Idea 3: to count, just send

a request through network!

(Routed by balancers…)

min

i-counte

rs

inc,inc,inc!

inc,inc!

Page 14: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

A crazy idea?

Stefan Schmid @ T-Labs Berlin, 2013/4 14

Batcher networks permute values: use it for balancing the counting?!

Idea 3: to count, just send

a request through network!

(Routed by balancers…)

min

i-counte

rs

inc,inc,inc!

inc,inc!

Page 15: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

The BCN: Overview

15

Bitonic Counting Network (BCN) 1. Take Batcher and replace comparators with balancers

2. When a node wants to count: sends message to an

arbitrary input wire

3. Message routed through the network

4. At outputs make “mini-counter”

5. The mini-counter of wire k replies the value “k+i*w”

to the initiator of the ith-message received.

… MN(w)

BCN(w/2)

BCN(w/2)

Mini-counter 1 Mini-counter 2

Mini-counter w-1 Mini-counter w

balancers

balancers

balancers

wid

th w

BCN(w)

Page 16: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Example

16

Bitonic Counter Network BCN(4):

min

i-counte

rs

2xBCN(2) M(4)

2xBSS(2)

MN(4)

Page 17: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Example

17

Bitonic Counter Network BCN(4):

min

i-counte

rs

11x Count!

Count!

2xBCN(2) M(4)

2xBSS(2)

MN(4)

Page 18: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Example

18

Bitonic Counter Network BCN(4):

Min

i-counte

rs

11x Count!

Count!

2xBCN(2) M(4)

2xBSS(2)

MN(4)

6

5

1

0

3

3

3 3

3

3

3

3

So depth / increment time? log2 n, as for sorting.

Page 19: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Example With Requests

19

General pattern:

2xBCN(2) M(4)

2xBSS(2)

MN(4)

Request propagation?

4 3 1

5

7 6 2

Page 20: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Example With Requests

20

General pattern:

2xBCN(2) M(4)

2xBSS(2)

MN(4)

Frequency distribution (2,2,2,1): called Step Property!

Formula for requests arriving at i-th counter from top?

4 3 1

5

7 6 2

4 1

3

6 2

7 5

5 1

6 2

3

7 4

5 1

7 3

4

6 2

# 2

# 2

# 2

# 1

Token t mod w = i, every i-th request!

Perfectly load balanced and consistent

Page 21: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Example With Requests

21

General pattern:

2xBCN(2) M(4)

2xBSS(2)

MN(4)

Frequency distribution (2,2,2,1): called Step Property!

Formula for requests arriving at i-th counter from top?

4 3 1

5

7 6 2

4 1

3

6 2

7 5

5 1

6 2

3

7 4

5 1

7 3

4

6 2

# 2

# 2

# 2

# 1

Token t mod w = i, every i-th request!

Perfectly load balanced and consistent

So what should mini-counter

i return upon its r-th request?

(r-1)*w + i

Page 22: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Analysis

Correctness In a quiescent state, the w output wires of a bitonic counting

network of width w have the step property.

Stefan Schmid @ T-Labs Berlin, 2013/4 22

Step Property

A sequence y0,…,yw-1 has the step property iff: For any i<j: 0 ≤ yi-yj ≤ 1

Quiescent State

No message in transit. Example:

8,8,8,8,7,7,7,7,7

Idea: if output wires

have this property,

exactly the values

1, …, r will be assigned

by the mini-counters!

8

8

8

7

Note: Step property implies that

values differ by at most 1, and

large values come first!

Page 23: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Balancer Properties

Stefan Schmid @ T-Labs Berlin, 2013/4 23

Balancer Properties

Let xi denote the number of messages consumed on input i,

where i={0,1}. Denote yi denote the number of messages on

output wire i. Then: (A1) x0+x1 ≥ y0+y1

(A2) In a quiescent state: x0+x1 = y0+y1

(A3) The number of messages sent to the upper output wire

is at most one higher than the number on the lower:

y0 = ceil((y0+y1)/2), y1 = floor((y0+y1)/2)

out1 out2

in1 in2

We assume that message is sent to upper first.

Page 24: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Some Facts (1)

Stefan Schmid @ T-Labs Berlin, 2013/4 24

Step Property Properties

If a sequence y0,y1,…,yw-1 has step property,

(B1) All subsequences have step property too

(B2) And even and odd subsequences satisfy:

Example: 7,7,7,6,6,6,6,6 = 51

25= floor(51/2) 26= ceil(51/2)

Page 25: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Some Facts (2)

Stefan Schmid @ T-Labs Berlin, 2013/4 25

More Step Properties

Given two sequences x0,x1,…,xw-1 and y0,y1,…,yw-1 with

the step property, (C1) If ∑ xi = ∑ yi, then xi=yi for any i

(C2) If ∑ xi = ∑ yi+1, then there exists a unique j

such that xj=yj+1, and xi=yi for any other i.

Example: x = 7,7,7,7,6,6,6,6 = 52

y = 7,7,7,6,6,6,6,6 = 51

Page 26: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Correctness

Stefan Schmid @ T-Labs Berlin, 2013/4 26

Correctness

The step property is maintained inductively!

Also by induction over balancers, s. lecture notes. Proof.

However, only correct in “quiescence state”.

But: Counters will always be unique and there

are no gaps (gap means request still in transit!),

just sometimes a larger counter is issued before

a smaller counter!

Page 27: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Correctness

Stefan Schmid @ T-Labs Berlin, 2013/4 27

Linearizability

A system is linearizable if the order in which values are assigned

reflects the real-time order in which they were requested: if there

is a pair of operations o1, o2, where operation o1 terminates before

operation o2 starts, and the logical order is “o2 before o1”, then a

distributed system is not linearizable.

o1.start o1.end o2.start o2.end

Is Bitonic Counting Network linearizable?

Page 28: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Not linearizable!

Stefan Schmid @ T-Labs Berlin, 2013/4 28

Zzz @1

@2 Zzz

Page 29: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Not linearizable!

Stefan Schmid @ T-Labs Berlin, 2013/4 29

Zzz @1

@2 Zzz

@3

What happens?

Page 30: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Not linearizable!

Stefan Schmid @ T-Labs Berlin, 2013/4 30

Zzz @1

@2 Zzz

@3

2!

Page 31: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Not linearizable!

Stefan Schmid @ T-Labs Berlin, 2013/4 31

Zzz @1

@2 Zzz

@3

2! @4

What happens now?

Page 32: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Not linearizable!

Stefan Schmid @ T-Labs Berlin, 2013/4 32

Zzz @1

@2 Zzz

@3

2! @4

0!

Page 33: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Not linearizable!

Stefan Schmid @ T-Labs Berlin, 2013/4 33

Zzz @1

@2 Zzz

@3

2! @4

0!

Request started and

finished earlier, but

got higher number...

Page 34: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

The BCN: Overview

34

Bitonic Counting Network (BCN) 1. Take Batcher and replace comparators with balancers

2. When a node wants to count: sends message to an

arbitrary input wire

3. Message routed through the network

4. At outputs make “mini-counter”

5. The mini-counter of wire k replies the value “k+i*w”

to the initiator of the ith-message received.

… MN(w)

BCN(w/2)

BCN(w/2)

Mini-counter 1 Mini-counter 2

Mini-counter w-1 Mini-counter w

balancers

balancers

balancers

wid

th w

BCN(w)

Page 35: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Difference Between Sorting and Counting?

Stefan Schmid @ T-Labs Berlin, 2013/4 35

Counting vs Sorting Any counting network C is also a sorting network I(C),

when replacing balancers with comparators.

Assume: want to sort binary vector {0,1}* in I(C).

Make a “request” on each wire of C if there is a “0” at corresponding wire of I(C).

Since C is a counting network, all requests end up at upper wires.

A comparator of I(C) will receive a “0” on its upper wire iff the corresponding

balancer in C receives a request on upper wire (correspondence

between requests and “0”); similarly for lower wire.

By induction: 0s always exit upper wires (like requests).

Proof.

0

1

1

0

r

r

r

r

r

r

r

r

0

1

0

1

0

0

1

1

0

0

1

1

The other direction is not true (already Odd/Even network)…

Page 36: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

End of Lecture

Stefan Schmid @ T-Labs Berlin, 2013/4 36

Page 37: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Backup: Proof

Stefan Schmid @ T-Labs Berlin, 2013/4 37

… MR(w)

balancers

inp

ut

wid

th w

Merger MR(w)

w/2

w

/2

ou

tpu

t y

x

x’

Page 38: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Backup: Proof

Stefan Schmid @ T-Labs Berlin, 2013/4 38

MR(w/2)

balancers

ou

tpu

t y

Recursive definition: MR(w) from two MR(w/2)

MR(w/2)

x0

xw/2-1

xw/2

xw-1

inp

ut

x

pairwise

balancers

recursive

definition

split

odd/even

Upper merges even subsequence, lower merges odd subsequence!

MR(w)

Page 39: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Backup: Proof

Stefan Schmid @ T-Labs Berlin, 2013/4 39

Example: MR(8)

even subsequence to top

odd subsequence to bottom

top merger to upper entry

lower merger to lower entry

Page 40: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Backup: Proof

Stefan Schmid @ T-Labs Berlin, 2013/4 40

Merger MR

If MR(w) has inputs x0,x1,…,xw/2-1 and xw,xw+1,…,xw-1

With step property, then also output y0,y1,…,yw-1

has step property.

… MR(w)

balancers w/2

w

/2

ou

tpu

t y

ste

p p

rop

ert

y

ste

p p

rop

ert

y

ste

p p

rop

ert

y

Page 41: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Backup: Proof by Induction

Stefan Schmid @ T-Labs Berlin, 2013/4 41

Base case w=2: Trivial as MR(2) is a balancer

9

26

18

17

Page 42: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Backup: Proof by Induction

Case w>2:

1. Let z0 zw/2-1 and z’0 z’w/2-1 be output of upper and lower MR(w/2)

2. Even&odd subsequences x0,x1,…,xw/2-1 and xw,xw+1,…,xw-1 resp.

must have step property too, see (B1)

3. And by Induction Hypothesis, z0 zw/2-1 and z’0 z’w/2-1 have step

property 4. Let Z = ∑ zi and Z’ = ∑ z’I , so by (B2) even and odd subsequences

satisfy:

MR(w/2)

balancers

ou

tpu

t y

MR(w/2)

x0

xw/2-1

xw/2

xw-1

inp

ut

x

y0

yw-1

z0

zw/2-1

z’0

z’w/2-1

Page 43: Network Algorithms Distributed Countingstefan/netalg13-9-count.pdf · o1.start o1.end o2.start o2.end Is Bitonic Counting Network linearizable? Not linearizable! Stefan Schmid @ T-Labs

Backup: Proof by Induction

Case w>2: MR(w/2)

balancers

ou

tpu

t y

MR(w/2)

x0

xw/2-1

xw/2

xw-1

inp

ut

x

y0

yw-1

z0

zw/2-1

z’0

z’w/2-1

5. So Z and Z’ differ by at most 1.

5.a. If Z and Z’ are the same, (C1) implies that zi = z’i ,

so output has step property too

5.b. If Z and Z’ differ by one, (C2) implies that there

exists a unique j where zj and z’j differ. Let l:=min(zj,z’j),

so output yi = l+1 for i<2j and yi = l for i>2j+1. The

final balancer ensures that y2j = l+1 and y2j+1 = l,

so also step property.