If a GPU is really able to calculate code in parallel. This sorting algorithm must be correct.
- Create a 2D comparison matrix
O(n)
values = [ 3, 1, 2 ]
# 3 1 2
comparisonMatrix = [ [ 0, 1, 1 ], # 3
[ 0, 0, 0 ], # 1
[ 0, 1, 0 ]] # 2
# Done on GPU
comparisonMatrix[rowIdx][columnIdx] = values[rowIdx] > values[columnIdx]
- Calculate row sums
O(n)
rowSums = [[ 1 ], # 3
[ 0 ], # 1
[ 2 ]] # 2
# Done on GPU
rowSums[rowIds] = comparisonMatrix[rowsIds][all]
- Mapping the initial
valuesto asortedArrayusing therowSumsarray as an index
O(1)
sortedValues = [ 1, 2, 3 ]
# Done on GPU
sortedValues[rowIdx] = values[rowSums[rowIdx]]
Total: O(n + n + 1) = O(n)
Counter argument:
GPU's have a finite number of cores so the big-O of iterating over an array is O(n/NUM_CORES) instead of O(1). But since hardware should not be included in mathmatics we should either assume NUM_CORES is 1 or infinate. Being infinate would cause this algorithm to be OK, while assuming 1 would cause GPU's to have no mathmatical impact on complexity.
Notes:
This is not a reasonable algorithm to run since memory is O(n^2) it is more of a proof.
Values are all different from eachother, otherwise this would cause two rowSums to be equivalent.
Although there are ways of doing these substeps faster, I stuck with the most strait forward ones.