parallel arrays and sorting

Herhut, Stephan A stephan.a.herhut at
Thu Apr 18 21:45:23 PDT 2013

Thanks for the overview of thrust. I had completely missed the issue of stability before. This needs careful consideration and I have not yet made up my mind. I understand the desire to have unstable sort for performance reasons, but it also introduces a form of non-determinism.

Histogram can also be expressed using scatter in out API. Assuming some array A of color values, you can do => 1).scatter( => bucket(v)), 0, (a,b) => a+b, numberOfBuckets)

So, essentially create a vector of 1's and then scatter those to your buckets based on their bucket value, using addition as the conflict resolution. Sorting the scatter indices indeed makes a lot of sense and an implementation would be free to first sort the vector of scatter indices to reduce synchronization cost. I also thought about an explicit version directly using sort and some other primitives of our API but I could not come up with a formulation that would benefit from sorting.

The array of structures (AoS) vs. structures of arrays (SoA) transformation is problematic in JavaScript. In C like languages one has a lot of structural information, in particular the fields and types of a structure are statically known and homogeneous across an array. In JavaScript, on the other hand, objects can be used like structures but in general there may be a lot more dynamism: Arrays may contain objects with varying labels, properties may have different types and, probably worst of all, the programmer can add new properties to objects at any time. So even though a structure may start out as a homogeneous array, this can change during its lifetime. Analyzing these data structures and tracking the 'anomalies' seems not straight forward to me and I am not convinced that this can be implemented efficiently.

However, there is also good news. The binary data proposal (see brings C like data types, including arrays of structures, to JavaScript and we have proposed an extension to Parallel JavaScript that makes use of binary data as storage format (see In such a setting, doing automatic AoS to SoA transformations is possible and our programming model does not preclude this.


From: Norm Rubin [mailto:nrubin at]
Sent: Thursday, April 11, 2013 11:36 AM
To: Herhut, Stephan A; es-discuss at<mailto:es-discuss at>
Cc: dev-tech-js-engine-rivertrail at<mailto:dev-tech-js-engine-rivertrail at>
Subject: RE: parallel arrays and sorting

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

// 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'}

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

From: Herhut, Stephan A [mailto:stephan.a.herhut at]
Sent: Thursday, April 11, 2013 1:09 PM
To: Norm Rubin; es-discuss at<mailto:es-discuss at>
Subject: RE: parallel arrays and sorting

Rick is travelling, so let me chime in.

We have discussed this back and forth but have not come to a conclusion. Generally, we agree that adding a sort primitive makes a lot of sense, in particular as the Array object in JavaScript already has a sort method. Also, as you too mentioned, implementing an efficient sort as a library function without knowing the details of the parallel hardware used is difficult, to say the least. So sort ticks all the boxes to become a primitive.

The other, and arguably more difficult question, is what a sort method should look like. If we take JavaScript's existing Array.sort, the sort method would get an (optional) comparator function. However, using a comparator would preclude the use of radix sort.
An alternative would be to implement sorting of primitive types only. This brings back more choice in sort algorithms but limits use. For such a design, we considered a function as optional argument to sort that, given a value from the ParallelArray to be sorted, returns a key used for comparison, which again needs to be a primitive. This would at least enable sorting of objects by a field and, at some runtime cost, sorting of general data.

The tradeoff between these two approaches, and probably other designs, is hard to judge without knowing what sort is used for. So we decided to wait for some good use cases before deciding on a specific design.

Sorry, no answers only further questions.

From: es-discuss-bounces at<mailto:es-discuss-bounces at> [mailto:es-discuss-bounces at] On Behalf Of Norm Rubin
Sent: Monday, April 08, 2013 7:46 AM
To: es-discuss at<mailto:es-discuss at>
Subject: parallel arrays and sorting

In comparing ParallelArrays (rivertrail) to the cuda thrust library,  I noticed  that Sorting using parallelArrays looks like a missing primitive  operation.

Sorting is  special because the good (high performance) algorithm on a gpu is radix sort, while the good (high performance) algorithm on the cpu is parallel merge sort,  The other way around radix sort of cpu, or parallel merge sort of a gpu is very slow and often worse than serial implementations

Sadly it is well past a jit to take the code for one flavor of sort and transform it into the other,  while  it would be pretty simple for a run-time to pick a good sort, if only it knew that a sort was going on.  Run times would not be required to do  so  here since they can always pick a slow sort.

I know this is a slippery road, since once you add another prim, adding more prims becomes ever easier  but sorting seems pretty important.   And array already has a sort, so adding a version that works on ParallelArrays does not seem so bad.

What do you guys think?

This email message is for the sole use of the intended recipient(s) and may contain confidential information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list