Chapter 4

Sorting
•We will be sorting records based on some key.
•The records will be stored in “global” memory.
•We will use the following helper functions
4-1
Unlimited Parallelism Solution
• Use P=N-1 processors
• All odd processors compare their value with
the next processor (an even processor)
– Swap if needed
• Then all even processors compare their value
with the next odd processor.
– Swap if needed
• Continue until no swap
• Will need O(n) passes; each pass O(1)
• Swap is NOT local
4-2
Odd/Even Interchange to alphabetize a
list L of records on field x.
4-3
Limited Parallelism
• Use 26 way parallelism, one processor for each
letter
• Scatter a part of the array to each processor
– Each processor locally counts the number of items
beginning with each letter
• Do a reduce on the array of counts
– Now have how many items begin with each letter
• Indirectly have starting position of each letter in sorted
array
• Each processor grabs the items beginning with
that processor’s letter. Sort locally. Store in
proper position of global array
4-4
Fixed 26-way parallel solution to alphabetizing. The function
letRank(x) returns the 0-origin rank of the Latin letter x.
index
1. Gets values from
global L
2. Saves values
into global L –
Synchronization
concern
3.Synchronization
point
4-5
Scalable Parallelism – Batcher’s Bitonic
Sort
• Must have a power of 2 processes
• Each processor sorts its local records
– Some ascending, some descending (more later)
• Pairs merge in an elegant manner.
• Merging uses (p,d)
– p: which pairs merge
• Processor i and j merge if the bit pattern for i and j
differ only in bit p
– d: which direction – look at bit d of the process’ id
4-6
Merging
• A sequence is sorted if it is non-increasing or
non-decreasing.
• Merging will make a bitonic (not monotonic)
sequence (i.e. will go up then down, or down
then up)
4-7
Figure 4.6
4-8
Peril-L program using Batcher’s sort to alphabetize
records in L.
Setting up
Pointer Sort
>=
4-9
Peril-L program using Batcher’s sort to alphabetize records in
L. (cont.)
4-10