comparison networks sorting sorting binary values sorting arbitrary numbers implementing symmetric...

115
Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Upload: flora-hood

Post on 13-Jan-2016

241 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Comparison Networks

Sorting• Sorting binary values• Sorting arbitrary numbers• Implementing symmetric functions

Page 2: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Algorithms

)log ( nnO comparisons to sort n elements

)(mO comparisons required to merge two arrays of size m/2

Order of comparisons not fixed in advance.

Mergesort(array[1,…,n] of Integers):begin Mergesort(array[1,…,n/2]); Mergesort(array[n/2+1,…,n]); Merge(array[1,…,n/2], array[n/2+1,…,n]);end

Not readily implementable in hardware.

Example

Page 3: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Networks

Order of comparisons fixed in advance.Readily implementable in hardware.

)log ( scomparison ofnumber ,)(logdepth 22 nnOnO

CDBA

ABCD

SortingNetwork

Page 4: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Networks (binary values)

SortingNetwork

10010011

00001111

inputs outputs

sorted

Page 5: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Comparator (2-sorter)

x

y

min(x, y)

max(x, y)

inputs outputs

C

Page 6: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Comparator (2-sorter)

x

y

min(x, y)

max(x, y)

inputs outputs

Page 7: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Comparison Network

0

0

1

1

0

0

1

1

0

0

1

1

1

1

0

0

depth d

width n

Page 8: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Comparison Network

0

0

1

1

0

0

1

1

0

0

1

1

1

1

0

0

n / 2comparisonsper stage

d stages

Page 9: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting NetworkAny ideas?

Page 10: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Network

inputs outputs

n 1Sorting

Network...... n

...

Page 11: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Insertion Sort Network

inputs outputs

depth 2n 3

Page 12: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Batcher Sorting NetworkNext Lecture

)(logdepth 2 nO

Page 13: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

x, y can be values from any linearly ordered set,e.g., integers, reals, etc.

inputs outputs

x

y

min(x, y)

max(x, y)

Page 14: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Comparison function:

C(X,Y) =1 if X > Y,

0 otherwise.

Idea: use C(X,Y) to select the min and the max of X and Y.

X, Y: integers represented as m-bit binary strings.

Integer Comparator

Page 15: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

9

6

2

2

6

9

2

9

6

sorted6

9

2

Page 16: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

1

4

5

1

4

5

1

5

4

sorted4

5

1

Page 17: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

3

0

7

3

0

7

3

7

0

0

7

3

not sorted

How can we verify if a network sorts all possible input sequences?

Page 18: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

inputs outputs

Try all possible 0/1 sequences.

Page 19: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

0

0

0

0

0

0

0

0

0

0

0

0 000 000

inputs outputs

Try all possible 0/1 sequences.

Page 20: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

0

1

0

0

0

1

0

0

1

0

1

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001

Page 21: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

0

0

1

0

0

1

0

1

0

0

1

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001

Page 22: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

0

1

1

0

1

1

0

1

1

1

1

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011

Page 23: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

1

0

0

0

0

1

0

1

0

0

1

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011100 001

Page 24: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

1

1

0

0

1

1

0

1

1

1

1

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011100 001101 011

Page 25: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

1

0

1

1

0

1

1

1

0

0

1

1

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011100 001101 011110 101

not sorted!

Page 26: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

1

1

1

1

1

1

1

1

1

1

1

1

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011100 001101 011110 101111 111

not sorted!

Page 27: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

Page 28: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

inputs outputs

Try all possible 0/1 sequences.

Page 29: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

0

0

0

0

0

0

0

0

0

0

0

0 000 000

inputs outputs

Try all possible 0/1 sequences.

Page 30: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

0

1

0

0

0

1

0

0

1

0

1

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001

Page 31: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

0

0

1

0

0

1

0

1

0

1

0

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001

Page 32: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

0

1

1

0

1

1

0

1

1

1

1

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011

Page 33: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

1

0

0

0

0

1

0

1

0

1

0

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011100 001

Page 34: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

1

1

0

0

1

1

0

1

1

1

1

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011100 001101 011

Page 35: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

1

0

1

0

1

1

1

1

0

1

1

0

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011100 001101 011110 011

Page 36: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

1

1

1

1

1

1

1

1

1

1

1

1

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011100 001101 011110 011111 111

Page 37: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Arbitrary Numbers

inputs outputs

000 000

Try all possible 0/1 sequences.

001 001010 001011 011100 001101 011110 011111 111

all sorted!

Page 38: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Zero-One Principle

If a comparison network sorts all possible sequencesof 0’s and 1’s correctly, then it sorts all sequencesof arbitrary numbers correctly.

Page 39: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Lemma

0a

1a

Given

0b

1b

For a monotonically increasing function f,

Page 40: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Lemma

0a

1a

Given

0b

1b

For a monotonically increasing function f,

)( 0af

)( 1af

)( 0bf

)( 1bf

Page 41: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Proof: Lemma

),min( 10 aa

),max( 10 aa

0a

1a

Page 42: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Proof: Lemma

))(),(min( 10 afaf

))(),(max( 10 afaf

)( 0af

)( 1af

Page 43: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Proof: Lemma

f is monotonically increasing:

yx )()( yfxf

)( 0af

)( 1af

))(),(min( 10 afaf

))(),(max( 10 afaf

Page 44: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Proof: Lemma

f is monotonically increasing:

yx )()( yfxf

)),(min( 10 aaf

)),(max( 10 aaf

)( 0af

)( 1af

Page 45: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Proof: Lemma

f is monotonically increasing:

)( 0bf

)( 1bf

)( 0af

)( 1af

yx )()( yfxf

Page 46: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Generalization

0a

1a

2a

0b

1b

2b

0c

1c

2c

0d

1d

2d

Given

Page 47: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

For a monotonically increasing function f,

)( 0af

)( 1af

)( 2af

)( 0bf

)( 1bf

)( 2bf

)( 0cf

)( 1cf

)( 2cf

)( 0df

)( 1df

)( 2df

Generalization

(by induction)

Page 48: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Proof: Zero-One Principle

Suppose

b) there exists a sequence that it doesn’t sort, i.e., 110 ,...,, naaa

ji aa , such that ji aa but is placed before in the output.ja ia

a) the network sorts all sequences of 0’s and 1’s,

Define

f (x) =0 if

1 otherwiseiax

Page 49: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Proof: Zero-One Principle

0a

1a

1na

0a

1a

1na

ja

ia

SortingNetwork

......

...

Page 50: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Proof: Zero-One Principle

SortingNetwork

)( 0af

)( 1af

)( 1naf

)( 0af )( 1af

)( 1naf

)( jaf)( iaf

......

...

Page 51: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Proof: Zero-One Principle

SortingNetwork

)( 0af

)( 1af

)( 1naf

)( 0af )( 1af

)( 1naf

......

...

10

contradiction!

Page 52: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Batcher Sorting Network, n = 4

Page 53: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

n = 4

n = 4

Batcher Sorting Network, n = 8

Page 54: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Lemma 1

Any subsequence of a sorted sequence is a sorted sequence.

00011111

00111

sorted

sorted

Page 55: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Lemma 2

For a sorted sequence, the number of 0’s in the even subsequence is either equal to, or one greater than, the number of 0’s in the odd subsequence.

00000111

0

0

0

1

0

0

1

1

sorted

even odd

Page 56: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

denotes the the number of 0’s in y y

For two sorted sequences and : x x

11 OEOE xxxx

denotes the even subsequence of Ey y

denotes the odd subsequence of Oy y

Lemma 3

Page 57: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Lemma 3

00000111x

0

0

0

1

Ex

0

0

1

1

Ox

00011111

0

0

1

1

Ex¢

0

1

1

1

Ox¢

Page 58: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Lemma 3

1 OEO xxx

1 OEO xxx

For two sorted sequences and : x x

11 OEOE xxxx

(by Lemma 2)

(by Lemma 2)

Page 59: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Merge Network

0x

1x

2x

3x

0x

1x

2x

3x

sorted

sorted

Merge[4]

Merge[4]

sorted

0y

1y

2y

3y

4y

5y

6y

7y

Page 60: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Merge Network (pf.)

0x

1x

2x

3x

0x

1x

2x

3x

sorted

sorted

Merge[4]

Merge[4]

sorted

sorted

(by Lemma 1)

(by Lemma 1)

Page 61: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Merge Network (pf.)

Merge[4]

Merge[4]

sorted

sorted

Ex

Ox

Ex

Ox

OE xx OE xx and

differ by at most 1

By Lemma 3

Page 62: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Merge Network (pf.)

Merge[4]

Merge[4]

sorted

0y

1y

2y

3y

4y

5y

6y

7y

Ex

Ox

Ex

Ox

OE xx OE xx and

differ by at most 1

By Lemma 3

Page 63: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Merge Network (pf.)

Merge[4]

Merge[4]

Ex

Ox

Ex

Ox

OE xx OE xx and

differ by at most 1

By Lemma 3

0

0

1

1

0

0

0

1

0

0

0

0

0

1

1

1

Page 64: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Batcher Sorting Network

0x

1x

2x

3x

4x

Sort[4]

Sort[4]

0y

1y

2y

3y

4y

5y

6y

7y

5x

6x

7x

Merge[8] sorted

Page 65: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Merge[4]

Batcher Sorting Network, n = 4

Sort[2]

Sort[2]

Page 66: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sort[4]

Sort[4]

Batcher Sorting Network, n = 8

Merge[8]

Page 67: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting Networks

Batcher

n2log 1830depth

AKS (Chvátal)

)log1(log 2

1depth 22 nn

AKS (Ajtai, Komlós, Szemerédi) Network:),(logdepth nO based on expander graphs.

AKS better for 36592n

Page 68: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

Page 69: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

Page 70: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

Page 71: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

Page 72: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

Page 73: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

Page 74: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

Page 75: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

Page 76: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

Page 77: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

Page 78: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Asynchronous token routing device

inputs outputs

1 bit of memory

balancedtoken counts

Page 79: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

Snapshot

inputs outputs

1 bit of memory

x

y

2

yx

2

yx

Page 80: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

be

a a

a

ab b

b

c

c c

cd d

e

e

e

d

d

f

g f

g

f

f

g g

Data structure for multiprocessor coordination

step sequence

Counting Network

Page 81: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

3

1

3

0

2

2

2

1

2

2

2

1

2

2

2

1

step sequence

Counting Network

Execution trace: token counts on all wires

Page 82: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Counting NetworkTokens are assigned value based on the output wirenumber.

1, 5, 9, 13, . . .

2, 6, 10, 14, . . .

3, 7, 11, 15, . . .

4, 8, 12, 16, . . .

CountingNetwork

• High throughput• Low contention

Advantages

Page 83: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Comparator

x

y

min(x, y)

max(x, y)

inputs outputs

Page 84: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

x

y

inputs outputs

2

yx

2

yx

Page 85: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancer

7

2

inputs outputs

4

5

Page 86: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Balancing Network

3

1

3

0

2

2

2

1

2

2

2

1

2

2

2

1

depth d

width n

Page 87: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Smooth Sequences

BalancingNetwork

......

0x

1x

1nx

for anysmooth property: 1 ji xx ji,

Page 88: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Smooth Sequences

BalancingNetwork

4

3

3

3

4

3

3

3

...

for anysmooth property: 1 ji xx ji,

Page 89: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Step Sequences

0x

1x

1nx

BalancingNetwork

......

for anystep property: 10 ji xx ji

Page 90: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Step Sequences

BalancingNetwork

3

3

3

3

4

4

4

4

...

for anystep property: 10 ji xx ji

Page 91: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Counting Network

CountingNetwork

...

Balancing network with step output sequences:

0x

1x

1nx

...for all inputs

for any

10 ji xx

ji

Page 92: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting vs. Counting

Counts

BalancingNetwork

......

ComparisonNetwork

......

isomorphic

Sorts?

Page 93: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting vs. Counting

BalancingNetwork

...

Counts

...Comparison

Network...

...

isomorphic

Sorts?

Page 94: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting vs. Counting

BalancingNetwork

...

Counts

...Comparison

Network...

...

isomorphic

Sorts?

Page 95: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting vs. Counting

If a balancing network counts, then its isomorphiccomparison network sorts, but not vice-versa.

Theorem

Page 96: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting vs. Counting

• By 0/1 principle, we need only consider 0/1 inputs.

Counts Sorts

x

y

2

yx

2

yx

balancer

x

y

min(x, y)

max(x, y)

comparator

Page 97: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Sorting vs. Counting

• By 0/1 principle, we need only consider 0/1 inputs.• A step sequence of 0’s and 1’s is a sorted sequence of 0’s and 1’s.

Counts Sorts

comparator

1

0

0

1

1

0

0

1

balancer

Page 98: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

1

2

0

3

4

1

2

0

3

4

1

2

0

3

4

1

2

0

3

4

1

2

0

3

4

1

2

0

3

4

1

2

0

3

4

1

2

0

3

4

Insertion Sort: a network which sorts but doesn’t count.

Sorts Counts

Sorting vs. Counting

Page 99: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

16

0

16

0

0

0

0

32

0

0

8

8

16

0

0

12

4

12

4

0

8

8

12

2

2

10

5

10

5

2

7

8

10

5

2

9

8

8

5

2

Sorts Counts

Sorting vs. Counting

Page 100: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

be

a a

a

ab b

b

c

c c

cd d

e

e

e

d

d

f

g f

g

f

f

g g

Data structure for multiprocessor coordination

step sequence

Counting Network

Page 101: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

3

1

3

0

2

2

2

1

2

2

2

1

2

2

2

1

step sequence

Counting Network

Execution trace: token counts on all wires

Page 102: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Counting NetworkTokens are assigned value based on the output wirenumber.

1, 5, 9, 13, . . .

2, 6, 10, 14, . . .

3, 7, 11, 15, . . .

4, 8, 12, 16, . . .

CountingNetwork

• High throughput• Low contention

Advantages

Page 103: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Batcher Counting Network

n = 4inputs

Batcher sorting network also works as a counting network!

3

1

3

0

2

2

2

1

2

2

2

1

2

2

2

1

Page 104: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Lemma 1

Any subsequence of a step sequence is a step sequence.

33344444

33444

step

step

Page 105: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Lemma 2

33344444

3

3

4

4

even

3

4

4

4odd

step

For a step sequence, the sum of the even subsequence is either equal to, or one less than, the sum of the odd subsequence.

Sum of even subseq.: 3+3+4+4=14

Sum of odd subseq.: 3+4+4+4=15

Page 106: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Lemma 3

1 OEO xxx

1 OEO xxx

denotes the sum of y y

For two step sequences and : x x 11 OEOE xxxx

denotes the even subsequence of Ey y

denotes the odd subsequence of Oy y

Follows from:

(by Lemma 2)

(by Lemma 2)

Page 107: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

33344444

x

3

3

4

4

Ex

3

4

4

4

Ox

99999999

9

9

9

9

Ex¢

9

9

9

9

Ox¢

14Ex

15Ox

36Ex

36Ox

Lemma 3

Page 108: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Merge Network

0x

1x

2x

3x

0x

1x

2x

3x

step

step

Merge[4]

Merge[4]

step

0y

1y

2y

3y

4y

5y

6y

7y

Page 109: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Merge Network (pf.)

0x

1x

2x

3x

0x

1x

2x

3x

Merge[4]

Merge[4]

step

step

step

step

Page 110: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Merge Network (pf.)

Merge[4]

Merge[4]

step

step

Ex

Ox

Ex

Ox

OE xx OE xx and

differ by at most 1

By Lemma 3

Page 111: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

33344444

step

Merge Network (pf.)

Merging two step sequences whose sums differ by at most one:

3

3

4

4

OE xx

3

4

4

4

OE xx

Page 112: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

33434444

not quite step

Merge Network (pf.)

Merging two step sequences whose sums differ by at most one:

3

4

4

4

OE xx

3

3

4

4

OE xx

Page 113: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Merge Network (pf.)

Merge[4]

Merge[4]

step

0y

1y

2y

3y

4y

5y

6y

7y

Ex

Ox

Ex

Ox

Page 114: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Batcher Counting Network

0x

1x

2x

3x

4x

Count[4]

Count[4]

0y

1y

2y

3y

4y

5y

6y

7y

5x

6x

7x

Merge[8] step

Page 115: Comparison Networks Sorting Sorting binary values Sorting arbitrary numbers Implementing symmetric functions

Verifying that a Network Counts

A balancing network with m balancers is a counting network if and only if it satisfies the step property for all input sequences with sum .

Theorem

m2