I was looking into different sorting algorithms, and trying to think how to port them to GPUs when I got this idea of sorting without actually sorting. This is how my kernel looks:
__global__ void noSort(int *inarr, char *outarr, int size)
{
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx < size)
outarr[inarr[idx]] = 1;
}
Then at the host side, I am just printing the array indices where outarr[i] == 1. Now effectively, the above could be used to sort an integer list, and that too may be faster than algorithms which actually sort.
Is this legit?
Your example is essentially a specialized counting sort for inputs with unique keys (i.e. no duplicates). To make the code a proper counting sort you could replace the assignment
outarr[inarr[idx]] = 1withatomicAdd(inarr + idx, 1)so duplicate keys are counted. However, aside from the fact that atomic operations are fairly expensive, you still have the problem that the complexity of the method is proportional to the largest value in the input. Fortunately, radix sort solves both of these problems.Radix sort can be thought of as a generalization of counting sort that looks at only
Bbits of the input at a time. Since integers ofBbits can only take on values in the range[0,2^B)we can avoid looking at the full range of values.Now, before you go and implement radix sort on CUDA I should warn you that it has been studied extensively and extremely fast implementations are readily available. In fact, the Thrust library will automatically apply radix sort whenever possible.