Sorting Networks A Lecture in CE Freshman Seminar Series: May 2007

advertisement
Sorting Networks
A Lecture in CE Freshman Seminar Series:
Ten Puzzling Problems in Computer Engineering
May 2007
Sorting Networks
Slide 1
About This Presentation
This presentation belongs to the lecture series entitled
“Ten Puzzling Problems in Computer Engineering,”
devised for a ten-week, one-unit, freshman seminar
course by Behrooz Parhami, Professor of Computer
Engineering at University of California, Santa Barbara.
The material can be used freely in teaching and other
educational settings. Unauthorized uses, including any
use for financial gain, are prohibited. © Behrooz Parhami
May 2007
Edition
Released
First
May 2007
Revised
Sorting Networks
Revised
Slide 2
Railroad Tracks and Switches
May 2007
Sorting Networks
Slide 3
Railroad Yards Have Many Tracks and Switches
May 2007
Sorting Networks
Slide 4
Rearranging Trains
Train cars
B
Engine
A
D
Track
C
Sorted order
D
C
B
A
Stub or lead
B
A
D
C
Stack or LIFO data structure in CE
B
A
D
C
Stub or lead
Stub or lead
C
A
B
D
Question: Is there an ordering that cannot be sorted using a stub?
Answer: Yes, if engine muse do the moving
Sorting algorithm
C
A
B
D
Queue or FIFO
May 2007
Sorting Networks
Siding
Slide 5
Rearrangement with Change of Direction
A
B
C
D
B
A
D
C
C
A
B
D
Turnaround loop
What types of sorting are possible
with a turnaround loop? A wye?
Complete the sorting process,
assuming that it is okay for the
engine to end up behind the train
D
Configuration after
the first few steps
in sorting the train
A
B
C
Wye
May 2007
Sorting Networks
Slide 6
Delivering Train Cars in a Specific Order
Cars in the train below have been sorted according to their delivery points.
However, it is still nontrivial to deposit car A in stub 1, car B in stub 2, and
car C in siding 3. Cars can be pulled or pushed by the engine.
2
C
B
A
D
3
1
2
C
B
A
D
3
1
2
C
B
1
A
3
Is there a better initial ordering of the cars for the deliveries in this puzzle?
May 2007
Sorting Networks
Slide 7
Train Passing Puzzle
The trains below must pass each other using a siding that can hold only
one car or one engine. Show how this can be done.
B
A
B
A
1
1
2
2
B
1
2
A
B
1
2
A
2
A
1
2
A
B
May 2007
Sorting Networks
Slide 8
Fast Combining or Reordering of Train Cars
C
A
B
D
Forming multiple trains
from incoming cars
C
A
B
D
Train reordering via a set of stubs
Cars are pushed or pulled by an engine
Alternatively, movement in one direction
may be achieved via sloped rails
Switches used to be adjusted manually,
but nowadays, electronic control is used
May 2007
Sorting Networks
Slide 9
Sorting Train Cars in Parallel
4
2
2
1
2
4
1
2
1
1
4
3
3
3
3
4
Target track
Unconditional exchange
2
1
2
4
3
1
1
2
3
3
4
May 2007
Track 2
Track 3
Track 4
Compare-exchange
4
The net in
stick diagram
schematic
Track 1
Track 1
Track 2
Track 3
Track 4
Is adding this compare-exchange
element sufficient for producing a
valid sorting network?
Sorting Networks
Slide 10
Validating a Sorting Network
a
min(a, b)
min(a, b, c, d)
Track 1
b
max(a, b)
????
Track 2
c
min(c, d)
????
Track 3
d
max(c, d)
max(a, b, c, d)
Track 4
In the example above, it was fairly easy
to show the validity of the sorting network.
Generally, it is much more difficult
Stick diagram schematic
How would one establish
the validity of this 16-input
sorting net?
More importantly, how
does one come up with this
design in the first place?
May 2007
Sorting Networks
Slide 11
The Zero-One Principle
A sorting net built of comparators is valid if it correctly sorts all 0-1 sequences
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
1
0
0
1
1
0
1
0
0
0
0
0
1
0
1
0
1
0
0
1
1
0
1
1
0
0
0
1
1
0
1
1
1
0
1
1
1
1
0
0
0
0
0
0
1
1
0
0
1
0
0
1
1
1
0
1
0
0
0
1
1
1
0
1
1
0
1
1
1
1
1
0
0
0
0
1
1
1
1
0
1
0
1
1
1
1
1
1
0
0
1
1
1
1
1
1
1
1
1
1
1
So, we can validate a sorting network using 2n rather than n! input patterns
n = 12:
May 2007
2n = 4096,
n! = 479,001,600 (thousands vs. half a billion)
Sorting Networks
Slide 12
A 16-Input Sorting Network
Use 4-input sorters, follow by (4, 4)-mergers, and end with an (8, 8)-merger
5
10
8
12
5
8
10
12
2
5
6
7
0
1
2
3
6
14
2
7
2
6
7
14
8
10
12
14
4
5
6
7
4
15
9
1
1
4
9
15
0
1
3
4
8
9
10
11
11
13
3
0
0
3
11
13
9
11
13
15
12
13
14
15
Using the 0-1 principle, we can validate this network via 16 + 25 + 81 tests
4-sorter tests
May 2007
(4, 4)-merger tests
Sorting Networks
(8, 8)-merger tests
Slide 13
Insertion Sort and Selection Sort
x0
x1
x2
y0
y1
y2
(n–1)-sorter
.
.
.
x n–2
x n–1
.
.
.
Insertion sort
x0
x1
x2
.
.
.
(n–1)-sorter
.
.
.
x n–2
x n–1
y n–2
y n–1
Insertion sort
y0
y1
y2
.
.
.
y n–2
y n–1
Selection sort
Selection sort
Parallel ins ertio n s ort = Parallel s election sort = Parallel bubble so rt!
C(n) = n(n – 1)/2
D(n ) = 2n – 3
Cost  Delay
= Q(n3)
3
4
0
5
2
1
3 3 0 0
4 0 3 3
0 4 4 4
5 5 2
2 5
1
0
3
2
4
1
5
0
2
3
1
4
0 0
2 1
1 2
3
0
1
2
3
4
5
Fig. 7.8 Sorting network based on insertion sort or selection sort.
May 2007
Sorting Networks
Slide 14
The Best Sorting Networks
Which
10-input
sorting
network
is better?
n = 6, 12 modules , 5 levels
n = 9, 25 modules , 8 levels
n ==
10,29
29 modules,Delay
9 levels = 9
Cost
Cost  Delay = 29  9 = 261
n = 10,=3131
mo dules,Delay
7 lev els= 7
Cost
Cost  Delay = 31  7 = 217
Criterion 1: The number of sticks or compare-exchange blocks (cost)
Criterion 2: The number of compare-exchanges in sequence (delay)
3: ,The
product
n =Criterion
12, 40 modules
8 levels
of cost and delay (cost-effectiveness)
The most cost-effective n-input sorting network may be
n = 16, 61 mo dules, 9 lev els
neither
the
fastest
design,
nor
the lowest-cost design
n = 16, 60 modules , 10 levels
May 2007
Sorting Networks
Slide 15
Electronic Sorting Networks
Electronic sorting networks are built of 2-sorters, building blocks that
can sort two inputs
a
min(a, b)
b
max(a, b)
x0
y0
3
2
1
1
x1
y1
2
3
3
2
x2
y2
5
1
2
3
x3
y3
1
5
5
5
Applications of sorting networks:
Directing information packets to their destinations in a network router
Connecting n processors to n memory modules in a parallel computer
May 2007
Sorting Networks
Slide 16
Download