| 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 <cooperative_groups.h> |
| 7 |
#include <cooperative_groups/memcpy_async.h> |
| 8 |
#include <cooperative_groups/reduce.h> |
| 9 |
|
| 10 |
#include "shared_indices.h" |
| 11 |
|
| 12 |
namespace cg = cooperative_groups; |
| 13 |
|
| 14 |
namespace caspar { |
| 15 |
|
| 16 |
template <typename T> |
| 17 |
__device__ void Swap(T& a, T& b) { |
| 18 |
T tmp = a; |
| 19 |
a = b; |
| 20 |
b = tmp; |
| 21 |
} |
| 22 |
|
| 23 |
// Batcher odd–even mergesort, sorts at most 1024 elements |
| 24 |
template <typename TVal, uint kArrayLength, typename TIdx = uint> |
| 25 |
__forceinline__ __device__ void Sort(TVal* const values, TIdx* const indices = nullptr) { |
| 26 |
__shared__ bool is_sorted; |
| 27 |
if (threadIdx.x == 0) |
| 28 |
is_sorted = true; |
| 29 |
|
| 30 |
__syncthreads(); |
| 31 |
if (threadIdx.x + 1 < kArrayLength) |
| 32 |
if (values[threadIdx.x] > values[threadIdx.x + 1]) |
| 33 |
is_sorted = false; |
| 34 |
__syncthreads(); |
| 35 |
if (is_sorted) |
| 36 |
return; |
| 37 |
|
| 38 |
#pragma unroll |
| 39 |
for (uint size = 2; size <= kArrayLength; size <<= 1) { |
| 40 |
uint stride = size / 2; |
| 41 |
uint offset = threadIdx.x & (stride - 1); |
| 42 |
{ |
| 43 |
uint pos = 2 * threadIdx.x - (threadIdx.x & (stride - 1)); |
| 44 |
if (pos + stride < kArrayLength) |
| 45 |
if (values[pos] > values[pos + stride]) { |
| 46 |
Swap<TVal>(values[pos], values[pos + stride]); |
| 47 |
if (indices) |
| 48 |
Swap<TIdx>(indices[pos], indices[pos + stride]); |
| 49 |
} |
| 50 |
stride >>= 1; |
| 51 |
} |
| 52 |
#pragma unroll |
| 53 |
for (; stride > 0; stride >>= 1) { |
| 54 |
if (size <= 32) |
| 55 |
__syncwarp(); |
| 56 |
else |
| 57 |
__syncthreads(); |
| 58 |
uint pos = 2 * threadIdx.x - (threadIdx.x & (stride - 1)); |
| 59 |
if (offset >= stride && pos < kArrayLength) |
| 60 |
if (values[pos - stride] > values[pos]) { |
| 61 |
Swap<TVal>(values[pos - stride], values[pos]); |
| 62 |
if (indices) |
| 63 |
Swap<TIdx>(indices[pos - stride], indices[pos]); |
| 64 |
} |
| 65 |
} |
| 66 |
if (size <= 16) |
| 67 |
__syncwarp(); |
| 68 |
else |
| 69 |
__syncthreads(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
template <int kArrayLength> |
| 74 |
__forceinline__ __device__ void MakeUnique(uint* const values, uint* const indices) { |
| 75 |
Sort<uint, kArrayLength>(values, indices); |
| 76 |
bool pred = false; |
| 77 |
if (threadIdx.x < kArrayLength - 1) |
| 78 |
pred = values[threadIdx.x] == values[threadIdx.x + 1]; |
| 79 |
__syncthreads(); |
| 80 |
if (pred) |
| 81 |
values[threadIdx.x + 1] = 0xFFFFFFFF; |
| 82 |
Sort<uint, kArrayLength>(values); |
| 83 |
} |
| 84 |
|
| 85 |
template <int kArrayLength> |
| 86 |
__forceinline__ __device__ uint GetNUnique(const uint* values) { |
| 87 |
__shared__ uint n_sorted; // last block might have less than 1024 elements |
| 88 |
if (threadIdx.x == 0) |
| 89 |
n_sorted = kArrayLength; |
| 90 |
__syncthreads(); |
| 91 |
if (threadIdx.x < kArrayLength - 1) |
| 92 |
if (values[threadIdx.x + 1] == 0xFFFFFFFF && values[threadIdx.x] != 0xFFFFFFFF) |
| 93 |
n_sorted = threadIdx.x + 1; |
| 94 |
__syncthreads(); |
| 95 |
return n_sorted; |
| 96 |
} |
| 97 |
|
| 98 |
__forceinline__ __device__ uint GetOrd(const uint* values, const uint length, const uint val) { |
| 99 |
const uint* lo = values; |
| 100 |
const uint* hi = values + length; |
| 101 |
while (lo < hi) { |
| 102 |
const uint* mid = lo + (hi - lo) / 2; |
| 103 |
if (*mid < val) |
| 104 |
lo = mid + 1; |
| 105 |
else |
| 106 |
hi = mid; |
| 107 |
} |
| 108 |
return static_cast<uint>(lo - values); |
| 109 |
} |
| 110 |
|
| 111 |
__global__ void SharedIndicesKernel(const uint* const __restrict__ indices, |
| 112 |
SharedIndex* const __restrict__ shared_indices_out, |
| 113 |
const uint size) { |
| 114 |
const auto block = cg::this_thread_block(); |
| 115 |
|
| 116 |
const auto gtrank = cg::this_grid().thread_rank(); |
| 117 |
const auto btrank = block.thread_rank(); |
| 118 |
const int start = block.group_index().x * block.group_dim().x; |
| 119 |
const int num = min(1024, size - start) * sizeof(uint); |
| 120 |
|
| 121 |
__shared__ uint indices_loc[1024]; |
| 122 |
cg::memcpy_async(block, indices_loc, indices + start, num); |
| 123 |
cg::wait(block); |
| 124 |
|
| 125 |
const uint val = gtrank < size ? indices_loc[btrank] : 0xFFFFFFFF; |
| 126 |
|
| 127 |
__shared__ uint values_sh[1024]; |
| 128 |
__shared__ uint indices_sh[1024]; |
| 129 |
values_sh[threadIdx.x] = val; |
| 130 |
indices_sh[threadIdx.x] = threadIdx.x; |
| 131 |
__syncthreads(); |
| 132 |
|
| 133 |
MakeUnique<1024>(values_sh, indices_sh); |
| 134 |
SharedIndex shared_index; |
| 135 |
if (gtrank < size) { |
| 136 |
const uint n_unique = GetNUnique<1024>(values_sh); |
| 137 |
shared_index.unique = values_sh[btrank]; |
| 138 |
shared_index.argsort = indices_sh[btrank]; |
| 139 |
shared_index.target = GetOrd(values_sh, n_unique, val); |
| 140 |
} else { |
| 141 |
shared_index.unique = 0xffffffff; |
| 142 |
shared_index.argsort = 0xffff; |
| 143 |
shared_index.target = 0xffff; |
| 144 |
}; |
| 145 |
if (gtrank < size) |
| 146 |
shared_indices_out[gtrank] = shared_index; |
| 147 |
} |
| 148 |
|
| 149 |
void SharedIndices(const uint* const indices, SharedIndex* const shared_indices_out, |
| 150 |
const uint problem_size) { |
| 151 |
const int n_blocks = (problem_size + 1024 - 1) / 1024; |
| 152 |
SharedIndicesKernel<<<n_blocks, 1024>>>(indices, shared_indices_out, problem_size); |
| 153 |
} |
| 154 |
|
| 155 |
} // namespace caspar |