Norm Rubin (2013-04-11T18:35:46.000Z)
github at esdiscuss.org (2013-07-12T02:26:57.376Z)
Good set of questions about sort, thanks for looking at this carefully. You guys have been great about responding to my emails. One sorting application that I would expect is physics particle simulation, where the code might sort objects based on distance from the viewer. This could be used so that far away objects get rendered smaller. Another might be computing a histogram over a visual image - sort the data first so nearby values will go into the same bucket, without needing atomic synchronization. Sorting with an optional compare seems ok to me- even if implementations can only optimize for some comparisons One interesting question you brought up as part of sorting by key is pretty tricky. In general parallel data arrays would like to be stored in transposed order from classic cpu styles. We usually call this array of structures or structures of arrays Do we sort an array of objects each with multiple fields stored one object after another or do we sort multiple arrays, with all the values of field1 followed by all the values of field2 etc I'd hope we allow implementations to reorder the data within a parallelArray as they like without needing to expose the layout to developers. Thrust, on the other hand, pushes the layout to the developer and thus has a significant set of transpose routines. Just for a point of reference there are 8 versions of sort in the thrust library. As you can see, Thrust never aimed to be a minimal set it just gained operations as applications appeared. AMD has a similar library called bolt which uses the same interface. Two different parallel libraries with the same routines does suggest that this set is enough to do useful work. Internally thrust notices that the compare is `<` on primitive types and uses radix sort (on the gpu). Thurst includes: - Stable and unstable forms - Ascending only or with a comparison function - Single array or a pair of keys and values 1) Sort(array) - sorts the array into ascending order, not guaranteed to be stable 2) Sort (array)- with a comparison function 3) Sort by key (keys, values) returns both the reordered keys and the reordered values, sorted by the array keys. This one is kind of confusing so here is an example ```cpp // an example of key sorting #include <thrust/sort.h> const int N = 6; int keys[N] = { 1, 4, 2, 8, 5, 7}; char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'}; thrust::sort_by_key(keys, keys + N, values, thrust::greater<int>()); // keys is now { 8, 7, 5, 4, 2, 1} // values is now {'d', 'f', 'e', 'b', 'c', 'a'} ``` - `sort.h`: http://docs.thrust.googlecode.com/hg/sort_8h.html - `thrust::sort_by_key`: http://docs.thrust.googlecode.com/hg/group__sorting.html#ga2bb765aeef19f6a04ca8b8ba11efff24 - `thrust::greater`: http://docs.thrust.googlecode.com/hg/structthrust_1_1greater.html 4) Sort by key with a comparison function 5-8) the stable forms Based on the thrust primitives, one parallel array sort might be Sort an array of objects Besides the array the arguments might be - a flag indicating if the sort must be stable - an optional user compare function that defaulted to < on primitive types