| 1 |
/* ---------------------------------------------------------------------------- |
| 2 |
* SymForce - Copyright 2025, Skydio, Inc. |
| 3 |
* This source code is under the Apache 2.0 license found in the LICENSE file. |
| 4 |
* ---------------------------------------------------------------------------- */ |
| 5 |
|
| 6 |
#include <cub/cub.cuh> |
| 7 |
|
| 8 |
#include "sort_indices.h" |
| 9 |
|
| 10 |
namespace { |
| 11 |
|
| 12 |
__global__ void Arange(uint* values, size_t problem_size) { |
| 13 |
const uint global_thread_idx = blockIdx.x * blockDim.x + threadIdx.x; |
| 14 |
if (global_thread_idx >= problem_size) { |
| 15 |
return; |
| 16 |
} |
| 17 |
values[global_thread_idx] = global_thread_idx; |
| 18 |
} |
| 19 |
|
| 20 |
__global__ void TargetFromArgsort(uint* argsort, uint* target, size_t problem_size) { |
| 21 |
const uint global_thread_idx = blockIdx.x * blockDim.x + threadIdx.x; |
| 22 |
if (global_thread_idx >= problem_size) { |
| 23 |
return; |
| 24 |
} |
| 25 |
target[argsort[global_thread_idx]] = global_thread_idx; |
| 26 |
} |
| 27 |
|
| 28 |
} // namespace |
| 29 |
|
| 30 |
namespace caspar { |
| 31 |
|
| 32 |
size_t SortIndicesGetTmpNbytes(size_t problem_size) { |
| 33 |
size_t tmp_storage_bytes = 0; |
| 34 |
auto err = |
| 35 |
cub::DeviceRadixSort::SortPairs(nullptr, tmp_storage_bytes, (uint*)nullptr, (uint*)nullptr, |
| 36 |
(uint*)nullptr, (uint*)nullptr, (uint)problem_size); |
| 37 |
return tmp_storage_bytes; |
| 38 |
} |
| 39 |
|
| 40 |
void SortIndices(void* tmp_storage, size_t tmp_storage_bytes, const uint* keys_in, uint* sorted_out, |
| 41 |
uint* target_out, uint* argsort_out, size_t problem_size) { |
| 42 |
const size_t grid_size = (problem_size + 1023) / 1024; |
| 43 |
Arange<<<grid_size, 1024>>>(target_out, problem_size); |
| 44 |
cub::DeviceRadixSort::SortPairs(tmp_storage, tmp_storage_bytes, keys_in, sorted_out, target_out, |
| 45 |
argsort_out, (uint)problem_size); |
| 46 |
TargetFromArgsort<<<grid_size, 1024>>>(argsort_out, target_out, problem_size); |
| 47 |
} |
| 48 |
|
| 49 |
size_t SortKeysGetTmpNbytes(size_t problem_size) { |
| 50 |
size_t tmp_storage_bytes = 0; |
| 51 |
auto err = cub::DeviceRadixSort::SortKeys(nullptr, tmp_storage_bytes, (uint*)nullptr, |
| 52 |
(uint*)nullptr, (uint)problem_size); |
| 53 |
return tmp_storage_bytes; |
| 54 |
} |
| 55 |
|
| 56 |
void SortKeys(void* tmp_storage, size_t tmp_storage_bytes, const uint* keys_in, uint* sorted_out, |
| 57 |
size_t problem_size) { |
| 58 |
cub::DeviceRadixSort::SortKeys(tmp_storage, tmp_storage_bytes, keys_in, sorted_out, |
| 59 |
(uint)problem_size); |
| 60 |
} |
| 61 |
|
| 62 |
__global__ void SelectIndexKernel(const uint* const input, const uint* const selections, |
| 63 |
uint* const output, size_t problem_size) { |
| 64 |
const uint global_thread_idx = blockIdx.x * blockDim.x + threadIdx.x; |
| 65 |
if (global_thread_idx >= problem_size) { |
| 66 |
return; |
| 67 |
} |
| 68 |
output[global_thread_idx] = input[selections[global_thread_idx]]; |
| 69 |
} |
| 70 |
|
| 71 |
void SelectIndex(const uint* const input, const uint* const selections, uint* const output, |
| 72 |
size_t problem_size) { |
| 73 |
const size_t grid_size = (problem_size + 1023) / 1024; |
| 74 |
SelectIndexKernel<<<grid_size, 1024>>>(input, selections, output, problem_size); |
| 75 |
} |
| 76 |
|
| 77 |
} // namespace caspar |