Starting scatter pass

This commit is contained in:
2026-10-02 17:35:26 +02:00
parent 7dfcfe56e9
commit 9af63cf2bd
7 changed files with 101 additions and 36 deletions
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
+99 -30
View File
@@ -5,7 +5,7 @@ const static uint32_t value_count = 1 << digit_count;
static const uint32_t WG_THREAD_COUNT = 256;
// Elements managed per thread for histo build
static const uint32_t WE_HISTO_ELEMENTS = 1;
static const uint32_t WE_HISTO_ELEMENTS = 16;
// Elements managed per workgroup for histo build
static const uint32_t WG_HISTO_ELEMENT = WG_THREAD_COUNT * WE_HISTO_ELEMENTS;
@@ -175,10 +175,33 @@ void total_prefix_sum_pass(
select(local_location.x == 0, 0, local_prefix[local_location.x - 1]);
}
groupshared uint32_t keys[WG_THREAD_COUNT];
groupshared uint32_t values[WG_THREAD_COUNT];
groupshared uint32_t sub_radix[WG_THREAD_COUNT];
groupshared uint32_t digits[WG_THREAD_COUNT];
struct SortToken
{
uint32_t tk;
__init(uint32_t digit, uint32_t index)
{
tk = (digit << 16) | (index & 0xFFFF);
}
property uint32_t digit
{
get {
return tk >> 16;
}
}
property uint32_t index
{
get {
return tk & 0xFFFF;
}
}
}
groupshared SortToken sort_tokens[WG_HISTO_ELEMENT];
// groupshared uint32_t local_prefix[WG_THREAD_COUNT];
[shader("compute")]
[numthreads(WG_THREAD_COUNT, 1, 1)]
@@ -189,43 +212,89 @@ void scatter_pass(
{
uint32_t batch_index = 0;
// Load up a batch of values
keys[local_location.x] = key_buffer[batch_index * WG_THREAD_COUNT + local_location.x];
values[local_location.x] = value_buffer[batch_index * WG_THREAD_COUNT + local_location.x];
digits[local_location.x] = extract_digit(keys[local_location.x], params.pass_index, params.descending);
// Split radix sort on the batch
for (uint32_t split_index = 0; split_index < digit_count; split_index++)
for (uint32_t index = group_location.x * WG_HISTO_ELEMENT + local_location.x;
index < min(key_buffer.getCount(), (group_location.x + 1) * WG_HISTO_ELEMENT);
index += WG_THREAD_COUNT)
{
// Set subradix value
sub_radix[local_location.x] = select(((digits[local_location.x] >> split_index) & 1) != 0, 1 << 16, 1);
uint32_t key = key_buffer[index];
uint32_t digit = extract_digit(key, params.pass_index, params.descending);
// local_histogram[digit].add(1);
uint32_t local_index = index - group_location.x * WG_HISTO_ELEMENT;
sort_tokens[local_index] = SortToken(digit, local_index);
}
GroupMemoryBarrierWithGroupSync();
// Split sort
SortToken thread_tokens[WE_HISTO_ELEMENTS];
uint32_t thread_prefix[WE_HISTO_ELEMENTS];
for (uint32_t radix_pass = 0; radix_pass < 8; radix_pass++)
{
// Load tokens
for (uint32_t i = 0; i < WE_HISTO_ELEMENTS; i++)
{
thread_tokens[i] = sort_tokens[i + WE_HISTO_ELEMENTS * local_location.x];
}
// Compute local prefix
uint32_t sum = 0;
for (uint32_t i = 0; i < WE_HISTO_ELEMENTS; i++)
{
uint32_t bin_digit = ((thread_tokens[i].digit >> radix_pass) & 1) ^ 1;
thread_prefix[i] = sum;
sum += bin_digit;
}
// Publish sum
local_prefix[local_location.x] = sum;
GroupMemoryBarrierWithGroupSync();
// Perform hillis steele
for (uint32_t offset = 1; offset < WG_THREAD_COUNT; offset *= 2)
{
uint32_t offset_value = select(local_location.x >= offset, sub_radix[local_location.x - offset], 0);
uint32_t offset_value = select(local_location.x >= offset, local_prefix[local_location.x - offset], 0);
GroupMemoryBarrierWithGroupSync();
sub_radix[local_location.x] += offset_value;
local_prefix[local_location.x] += offset_value;
GroupMemoryBarrierWithGroupSync();
}
// Now radix is ready, scatter split
uint32_t key = keys[local_location.x];
uint32_t value = values[local_location.x];
uint32_t digit = digits[local_location.x];
uint32_t prefix = sub_radix[local_location.x];
uint32_t destination = select(((digit >> split_index) & 1) != 0, prefix >> 16, prefix & 0xFFFF);
GroupMemoryBarrierWithGroupSync();
// Scatter
keys[destination] = key;
values[destination] = value;
digits[destination] = digit;
uint32_t total_zeroes = local_prefix[WG_THREAD_COUNT - 1];
// Scatter sort tokens
uint32_t zero_rank = select(local_location.x == 0, 0, local_prefix[local_location.x - 1]);
for (uint32_t i = 0; i < WE_HISTO_ELEMENTS; i++)
{
uint32_t bin_digit = (thread_tokens[i].digit >> radix_pass) & 1;
uint32_t scatter_index = select(bin_digit == 0,
thread_prefix[i] + zero_rank,
total_zeroes + local_location.x - (thread_prefix[i] + zero_rank));
sort_tokens[scatter_index] = thread_tokens[i];
}
GroupMemoryBarrierWithGroupSync();
}
// Now values are sorted according to digit
// Scatter to main memory
// Compute local offset by boundary detection
if (local_location.x == 0 || sort_tokens[local_location.x].digit != sort_tokens[local_location.x - 1].digit)
{
local_prefix[sort_tokens[local_location.x].digit] = local_location.x;
}
GroupMemoryBarrierWithGroupSync();
// Scatter keys
uint32_t offset = local_location.x * WE_HISTO_ELEMENTS;
for (uint32_t i = 0; i < WE_HISTO_ELEMENTS; i++)
{
uint32_t digit = sort_tokens[offset + i].digit;
uint32_t index = sort_tokens[offset + i].index;
uint32_t group_offset = histograms[group_location.x + digit * params.histogram_count];
uint32_t digit_offset = histograms[(params.histogram_count) + digit * params.histogram_count];
uint32_t destination_index = group_offset + digit_offset + (local_location.x - local_prefix[digit]);
dest_key_buffer[destination_index] = key_buffer[index];
dest_value_buffer[destination_index] = value_buffer[index];
}
}
Binary file not shown.
BIN
View File
Binary file not shown.
+2 -6
View File
@@ -72,7 +72,7 @@ impl RadixSorter
const WG_THREAD_COUNT: usize = 256;
// Elements managed per thread for histo build
const WE_HISTO_ELEMENTS: usize = 1;
const WE_HISTO_ELEMENTS: usize = 16;
// Elements managed per workgroup for histo build
const WG_HISTO_ELEMENTS: usize = Self::WG_THREAD_COUNT * Self::WE_HISTO_ELEMENTS;
@@ -351,11 +351,7 @@ impl RadixSorter
//compute_pass.dispatch_workgroups_indirect(&self., 0);
compute_pass.set_pipeline(&self.scatter_pass);
compute_pass.set_immediates(0, bytes);
compute_pass.dispatch_workgroups(
self.max_size.div_ceil(Self::WG_THREAD_COUNT) as u32,
1,
1,
);
compute_pass.dispatch_workgroups(self.histo_block_count as u32, 1, 1);
}
// Even number of passes => data is in the user buffer