Is there any way to sort this data the way I want without two kernels?

I have an array of ints. The values are either greater than or equal to 0 and the other set of values is -1.

So the data looks like :

``````0 1 2 3 -1 -1 -1 -1 -1 5 6 6 -1 -1 -1
``````

I want this to be sorted so that all the positive numbers are first and then the -1’s are last. I’d prefer to have the positive numbers be ordered least to greatest but I did begrudgingly use largest to least last time I did this.

Is there a way to get this data the way that I want with just one kernel or just one call to a thrust algorithm?

How about interpreting the data temporarily as unsigned int during the sorting?
With your prerequisites about the data any sorting algorithm with a “less than” comparison would give you the desired order then.

That’s so smart! -1 would be the largest unsigned! Oh poop, that’s amazingly brilliant! Thank you so much.

The unsigned int suggestion is a good one. Should be fast because in thrust or cub it would allow radix sorting.

In the more general case (suppose this were float rather than int) you could create a custom functor for thrust::sort that simply treated -1 as a very large number.

if (a<0) a = 1000000;
if (b<0) b = 1000000;
return (a>b);

Ha, I hadn’t even considered a floating point version. Too bad there’s not an unsigned floating type, huh? It’d be nice to have the mantissa or exponent be longer by that one bit.

Also, do you guys know of any ways of finding out the number of positive values in the array? I’ve been using thrust::find() but I’m not sure if there’s a better way. Basically, I just thrust::find() the first -1 value which then tells me, “Oh hey, there’s blah blah blah positive values.”