Starting faster radix
This commit is contained in:
+67
-171
@@ -1,6 +1,15 @@
|
||||
const static uint32_t digit_count = 4;
|
||||
const static uint32_t digit_count = 8;
|
||||
const static uint32_t value_count = 1 << digit_count;
|
||||
|
||||
// Thread count per work group
|
||||
static const uint32_t WG_THREAD_COUNT = 256;
|
||||
|
||||
// Elements managed per thread for histo build
|
||||
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;
|
||||
|
||||
struct Histogram
|
||||
{
|
||||
uint32_t[value_count] histogram;
|
||||
@@ -34,100 +43,59 @@ RWStructuredBuffer<uint32_t> dest_key_buffer;
|
||||
[vk::binding(1, 2)]
|
||||
RWStructuredBuffer<uint32_t> dest_value_buffer;
|
||||
|
||||
/*
|
||||
[vk::binding(0, 3)]
|
||||
RWStructuredBuffer<uint32_t> sort_count;
|
||||
[vk::binding(1, 3)]
|
||||
RWStructuredBuffer<IndirectArgs> block_count;
|
||||
*/
|
||||
|
||||
[vk::push_constant]
|
||||
uniform PassParams params;
|
||||
|
||||
uint32_t get_digit(uint32_t value, uint32_t digit_id, uint32_t descending)
|
||||
uint32_t extract_digit(uint32_t value, uint32_t digit_id, uint32_t descending)
|
||||
{
|
||||
let mask = ~(0xFFFFFFFF << digit_count);
|
||||
let digit = (value >> (digit_id * digit_count)) & mask;
|
||||
return select(descending != 0, digit ^ mask, digit);
|
||||
}
|
||||
|
||||
static const uint32_t split_size = 16;
|
||||
static const uint32_t thread_count = 256;
|
||||
static const uint32_t local_histo_count = thread_count / split_size;
|
||||
groupshared Atomic<uint32_t> local_histogram[value_count];
|
||||
|
||||
struct LocalHistogram
|
||||
{
|
||||
Atomic<uint32_t>[value_count] histogram;
|
||||
}
|
||||
|
||||
groupshared LocalHistogram[local_histo_count] local_histograms;
|
||||
|
||||
uint32_t histogram_index(uint32_t digit, uint32_t histo_index)
|
||||
{
|
||||
return digit * params.digit_stride + histo_index;
|
||||
}
|
||||
// groupshared uint32_t local_histogram[value_count];
|
||||
|
||||
[shader("compute")]
|
||||
[numthreads(256, 1, 1)]
|
||||
[numthreads(WG_THREAD_COUNT, 1, 1)]
|
||||
void histogram_pass(
|
||||
uint32_t3 location: SV_DispatchThreadID,
|
||||
uint32_t3 local_location: SV_GroupThreadID,
|
||||
uint32_t3 group_location: SV_GroupID, )
|
||||
{
|
||||
// First element managed by this thread in the data
|
||||
uint32_t base_offset = group_location.x * WG_HISTO_ELEMENT + local_location.x;
|
||||
|
||||
let element_count = sort_count[0];
|
||||
// Reset histogram
|
||||
let split_index = local_location.x / split_size;
|
||||
for (uint32_t i = 0; i < value_count; i++)
|
||||
// Zero out local_histogram
|
||||
if (local_location.x < value_count)
|
||||
{
|
||||
local_histograms[split_index].histogram[i].store(0);
|
||||
}
|
||||
if (location.x >= element_count)
|
||||
{
|
||||
|
||||
return;
|
||||
local_histogram[local_location.x] = 0;
|
||||
}
|
||||
|
||||
GroupMemoryBarrierWithGroupSync(); // Sync histo reset
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
|
||||
var value = key_buffer[location.x];
|
||||
value = get_digit(value, params.pass_index, params.descending);
|
||||
local_histograms[split_index].histogram[value].add(1);
|
||||
|
||||
GroupMemoryBarrierWithGroupSync(); // Sync histo build
|
||||
|
||||
// Aggregate all histograms and send to local memory
|
||||
// Binary tree sum
|
||||
/*
|
||||
var amount = 2;
|
||||
for (uint32_t d = local_histo_count / 2; d > 0; d >>= 1)
|
||||
for (var load = 0; load < WE_HISTO_ELEMENTS; load++)
|
||||
{
|
||||
if (local_location.x < d)
|
||||
{
|
||||
let a = location.x * amount;
|
||||
let b = a + amount - 1;
|
||||
|
||||
for (var v = 0; v < value_count; v++)
|
||||
{
|
||||
local_histograms[a * value_count + v].add(local_histograms[b * value_count + v].load());
|
||||
}
|
||||
}
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
amount >>= 1;
|
||||
// This access pattern coaleces reads
|
||||
uint32_t index = base_offset + load * WE_HISTO_ELEMENTS;
|
||||
uint32_t key = key_buffer[index];
|
||||
uint32_t digit = extract_digit(key, params.pass_index, params.descending);
|
||||
local_histogram[digit] += 1;
|
||||
GroupMemoryBarrierWithGroupSync(); // useful ?
|
||||
}
|
||||
*/
|
||||
// thread 0 sends to local memory
|
||||
if (local_location.x == 0)
|
||||
|
||||
// Write histogram out
|
||||
if (local_location.x < value_count)
|
||||
{
|
||||
|
||||
for (uint32_t v = 0; v < value_count; v++)
|
||||
{
|
||||
var sum = 0;
|
||||
for (uint32_t i = 0; i < local_histo_count; i++)
|
||||
{
|
||||
|
||||
sum += local_histograms[i].histogram[v].load();
|
||||
}
|
||||
histograms[histogram_index(v, group_location.x)] = sum;
|
||||
}
|
||||
histograms[local_location.x + group_location.x * params.digit_stride] = local_histogram[local_location.x].load();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,122 +104,60 @@ uint32_t div_ceil(uint32_t n, uint32_t q)
|
||||
return (n / q) + select(n % q == 0, 0, 1);
|
||||
}
|
||||
|
||||
groupshared uint32_t[16][16] block_sums;
|
||||
|
||||
void scan_digit(uint32_t lid_digit, uint32_t lid_histo)
|
||||
{
|
||||
let thread_block_size = div_ceil(block_count[0].x, 16);
|
||||
var running_sum = 0;
|
||||
for (uint32_t i = lid_histo * thread_block_size; i < min(thread_block_size * (lid_histo + 1), block_count[0].x); i++)
|
||||
{
|
||||
let current = histograms[histogram_index(lid_digit, i)];
|
||||
histograms[histogram_index(lid_digit, i)] = running_sum;
|
||||
running_sum += current;
|
||||
}
|
||||
|
||||
if (lid_histo == 15)
|
||||
{
|
||||
histograms[histogram_index(lid_digit, block_count[0].x)] = running_sum;
|
||||
}
|
||||
block_sums[lid_digit][lid_histo] = running_sum;
|
||||
}
|
||||
|
||||
void apply_digit(uint32_t lid_digit, uint32_t lid_histo)
|
||||
{
|
||||
let thread_block_size = div_ceil(block_count[0].x + 1, 16);
|
||||
var running_sum = 0;
|
||||
for (uint32_t i = lid_histo * thread_block_size; i < min(thread_block_size * (lid_histo + 1), block_count[0].x + 1); i++)
|
||||
{
|
||||
histograms[histogram_index(lid_digit, i)] += block_sums[lid_digit][lid_histo];
|
||||
}
|
||||
}
|
||||
groupshared uint32_t local_total_histogram[value_count];
|
||||
|
||||
[shader("compute")]
|
||||
[numthreads(16, 16, 1)]
|
||||
void running_sum_pass(
|
||||
[numthreads(256, 1, 1)]
|
||||
void prefix_sum_pass(
|
||||
uint32_t3 location: SV_DispatchThreadID,
|
||||
uint32_t3 local_location: SV_GroupThreadID,
|
||||
uint32_t3 group_location: SV_GroupID, )
|
||||
{
|
||||
scan_digit(local_location.x, local_location.y);
|
||||
|
||||
AllMemoryBarrierWithGroupSync();
|
||||
if (local_location.y == 0)
|
||||
if (local_location.x < value_count)
|
||||
{
|
||||
var running_sum = 0;
|
||||
for (uint32_t i = 0; i < 16; i++)
|
||||
{
|
||||
let current = block_sums[local_location.x][i];
|
||||
block_sums[local_location.x][i] = running_sum;
|
||||
running_sum += current;
|
||||
}
|
||||
return;
|
||||
}
|
||||
AllMemoryBarrierWithGroupSync();
|
||||
|
||||
apply_digit(local_location.x, local_location.y);
|
||||
|
||||
AllMemoryBarrierWithGroupSync();
|
||||
|
||||
if (local_location.x == 0 && local_location.y == 0)
|
||||
uint32_t running_sum = 0;
|
||||
for (uint32_t i = local_location.x; i < params.digit_stride - 1; i += value_count)
|
||||
{
|
||||
var running_sum = 0;
|
||||
for (uint32_t i = 0; i < value_count; i++)
|
||||
{
|
||||
let current = histograms[histogram_index(i, block_count[0].x)];
|
||||
histograms[histogram_index(i, block_count[0].x)] = running_sum;
|
||||
running_sum += current;
|
||||
}
|
||||
uint32_t current = histograms[i];
|
||||
histograms[i] = running_sum;
|
||||
running_sum += current;
|
||||
}
|
||||
|
||||
// Last histogram : into lds
|
||||
local_total_histogram[local_location.x] = running_sum;
|
||||
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
|
||||
// hillis-steel totaling
|
||||
for (uint32_t offset = 1; offset < value_count; offset *= 2)
|
||||
{
|
||||
if (local_location.x >= offset)
|
||||
{
|
||||
local_total_histogram[local_location.x] += local_total_histogram[local_location.x - offset];
|
||||
}
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
}
|
||||
|
||||
// Save last histogram in memory
|
||||
histograms[params.digit_stride - 1 + local_location.x] = local_total_histogram[local_location.x];
|
||||
}
|
||||
|
||||
groupshared uint32_t[256] keys;
|
||||
groupshared uint32_t[256] values;
|
||||
groupshared uint32_t[256] digit;
|
||||
static const uint32_t WE_SCATTER_ELEMENTS = 1;
|
||||
|
||||
groupshared uint32_t scatter_keys[WG_THREAD_COUNT * WG_HISTO_ELEMENT];
|
||||
groupshared uint32_t scatter_values[WG_THREAD_COUNT * WG_HISTO_ELEMENT];
|
||||
groupshared uint32_t scatter_values[WG_THREAD_COUNT * WG_HISTO_ELEMENT];
|
||||
|
||||
[shader("compute")]
|
||||
[numthreads(256, 1, 1)]
|
||||
[numthreads(WG_THREAD_COUNT, 1, 1)]
|
||||
void scatter_pass(
|
||||
uint32_t3 location: SV_DispatchThreadID,
|
||||
uint32_t3 local_location: SV_GroupThreadID,
|
||||
uint32_t3 group_location: SV_GroupID, )
|
||||
{
|
||||
|
||||
let element_count = sort_count[0];
|
||||
// Load kv
|
||||
if (location.x >= element_count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Gather
|
||||
keys[local_location.x] = key_buffer[location.x];
|
||||
values[local_location.x] = value_buffer[location.x];
|
||||
|
||||
// Compute digit
|
||||
digit[local_location.x] = get_digit(keys[local_location.x], params.pass_index, params.descending);
|
||||
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
|
||||
// Compute local offset
|
||||
var local_offset = 0;
|
||||
var local_digit = digit[local_location.x];
|
||||
for (uint32_t i = 0; i < local_location.x; i++)
|
||||
{
|
||||
|
||||
if (digit[i] == local_digit)
|
||||
{
|
||||
|
||||
local_offset += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Scatter
|
||||
// Compute global offset
|
||||
let digit_offset = local_digit * (block_count[0].x + 1);
|
||||
let global_loc = histograms[histogram_index(local_digit, block_count[0].x)] + histograms[histogram_index(local_digit, group_location.x)] + local_offset;
|
||||
|
||||
dest_key_buffer[global_loc] = keys[local_location.x];
|
||||
dest_value_buffer[global_loc] = values[local_location.x];
|
||||
}
|
||||
|
||||
[shader("compute")]
|
||||
@@ -261,15 +167,5 @@ void indirect_count_pass(
|
||||
uint32_t3 local_location: SV_GroupThreadID,
|
||||
uint32_t3 group_location: SV_GroupID, )
|
||||
{
|
||||
|
||||
let indirect = params.pass_index;
|
||||
if (indirect == 0)
|
||||
{
|
||||
sort_count[0] = key_buffer.getCount();
|
||||
}
|
||||
let div_ceil = (sort_count[0] / 256) + select(sort_count[0] % 256 == 0, 0, 1);
|
||||
block_count[0].x = div_ceil;
|
||||
block_count[0].y = 1;
|
||||
block_count[0].z = 1;
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user