Submission links today:
Sorting small arrays quickly
for (int i = 1; i < data.length; ++i) {
for (int j = i; j > 0; --j) {
if (data[j-1] > data[j]) {
swap(data, j-1, j)
}
}
}
for (int i = 1; i < data.length; ++i) {
for (int j = i; j > 0; --j) {
if (data[j-1] > data[j]) {
swap(data, j-1, j)
}
}
}
if (data[i] > data[j]) {
swap(data, i, j)
}
for (int i = 1; i < data.length; ++i) {
for (int j = i; j > 0; --j) {
if (data[j-1] > data[j]) {swap(data, j-1, j)}}}
i = 1
i = 2
i = 3
Consider:
for (int m = data.length - 1; m > 0; --m) {
for (int i = 0; i < m; ++i) {
if (data[i] > data[i+1]) {
swap(data, i, i+1)
}
}
}
Can we make a sorting network corresponding to bubble sort?
m = 6
m = 5
m = 4
m = 3
m = 2
When fully parallelized, both are same sorting network
Make a more efficient sorting network for 4 elements
Vector
sHow could we use this sorting network to sort Vector
s with 4 lanes?
A new tool: VectorShuffle
VectorShuffle<Float> vs
stores an array of indices
vs
stores [1, 0, 3, 2]
vec
is FloatVector
, vec.rearrange(vs)
vec
stores [5, 4, 9, 7]
Question. What is the result of vec.rearrange(vs)
?
How to implement the following parallel comparitor operations?
vec = [7, 4, 5, 6]
, vs = [1, 0, 3, 2]
Original vector vec
, shuffle vs
, mask mask
is true
for all lanes corresponding to min
comparator
var swapped = vec.rearrange(vs);
vec = vec.blend(vec.min(swapped, mask)
.blend(vec.max(swapped, mask.not());
sorting-networks.zip
implements an optimal sorting network of size 8More shared data structures: linked lists!