Okay, I just found out a really cool thing to do is sort any number of arrays by key by splitting up the operation like so:
thrust::sequence(indices.begin(), indices.end());
thrust::sort_by_key(keyvals.begin(), keyvals.end(), indices.begin());
foreach (vector_to_be_sorted)
thrust::scatter(data.begin(), data.end(), indices.begin(), sorted_data.begin());
Reading the documentation it says, “For each iterator i in the range [first, last), the value i is assigned to output[(map + (i - first))].”
Okay, let’s say we have some data.
keys = { 0, 7, 6, 4, 2, 3, 5, 1 };
data = { a, b, c, d, e, f, g, h };
Doing the initial sort, we get
keys = { 0, 1, 2, 3, 4, 5, 6, 7 };
indices = { 0, 7, 4, 5, 3, 6, 2, 1 };
In theory, data should look like this at the end :
data = { a, h, e, f, d, g, c, b };
But every time I try to do this on a piece of paper, I swear I’m doing it wrong.
From the description, it sounds
data[0] = a is mapped to indices[0] = 0
data[1] = b is mapped to indices[1] = 7
data[2] = c is mapped to indices[2] = 4
The one for data[2] does not look true at all!
Instead, it seems to work if
output[i] = data[indices[i]]
I think I’m just really confusing myself here…