Skip to content

Commit 6ab3292

Browse files
authored
Bitonic Sort Update 2 (#318)
* Changed default values and added auto sort at start * Added stepIndex and totalStep to execution Information and allowed for dynamic updating of sort speed * Scaffolding for global sort and renaming of controller elements from 'cell' to 'controller' to accord with Dat.gui's built in types * removed anys for greggman * Safety commit * Adjusted bindGroups and uniform placement and added different visualization of swap regions * Adjusted bindGroups, adjusted uniforms, and added a new visualization of swap regions * Removed pointerEvents from non-interactive gui elements and added atomic account of number of swaps performed during a sort * Made totalSwaps non-interactive :( * Condense information in multiple Gui elements down to single Gui elements * Added thread constraint * Added ability to constrain maximum number of threads/invocations per workgroup, which reflects in the execution information as well * Get rid of todos * Remove unused folder * removed references to threads in main.ts, removed references to removed portions of code, codeMirror for compute shader now displays the .ts file from which the compute shader is constructed rather than a version of the compute shader that takes an arbitrary number of invocationsPerWorkgroup * Removed all references to threads in bitonicCompute.ts * Added new descriptions for each of the settings elements * Implemented most ben-clayton naming suggestions * Fixed minor spacing issue to make prettier happy
1 parent f6e7e0d commit 6ab3292

File tree

2 files changed

+175
-86
lines changed

2 files changed

+175
-86
lines changed

src/sample/bitonicSort/computeShader.ts renamed to src/sample/bitonicSort/bitonicCompute.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export const computeArgKeys = ['width', 'height', 'algo', 'blockHeight'];
22

3-
export const NaiveBitonicCompute = (threadsPerWorkgroup: number) => {
4-
if (threadsPerWorkgroup % 2 !== 0 || threadsPerWorkgroup > 256) {
5-
threadsPerWorkgroup = 256;
3+
export const NaiveBitonicCompute = (workgroupSize: number) => {
4+
if (workgroupSize % 2 !== 0 || workgroupSize > 256) {
5+
workgroupSize = 256;
66
}
77
// Ensure that workgroupSize is half the number of elements
88
return `
@@ -15,7 +15,7 @@ struct Uniforms {
1515
}
1616
1717
// Create local workgroup data that can contain all elements
18-
var<workgroup> local_data: array<u32, ${threadsPerWorkgroup * 2}>;
18+
var<workgroup> local_data: array<u32, ${workgroupSize * 2}>;
1919
2020
// Define groups (functions refer to this data)
2121
@group(0) @binding(0) var<storage, read> input_data: array<u32>;
@@ -35,25 +35,25 @@ fn local_compare_and_swap(idx_before: u32, idx_after: u32) {
3535
return;
3636
}
3737
38-
// thread_id goes from 0 to threadsPerWorkgroup
39-
fn get_flip_indices(thread_id: u32, block_height: u32) -> vec2<u32> {
38+
// invoke_id goes from 0 to workgroupSize
39+
fn get_flip_indices(invoke_id: u32, block_height: u32) -> vec2<u32> {
4040
// Caculate index offset (i.e move indices into correct block)
41-
let block_offset: u32 = ((2 * thread_id) / block_height) * block_height;
41+
let block_offset: u32 = ((2 * invoke_id) / block_height) * block_height;
4242
let half_height = block_height / 2;
4343
// Calculate index spacing
4444
var idx: vec2<u32> = vec2<u32>(
45-
thread_id % half_height, block_height - (thread_id % half_height) - 1,
45+
invoke_id % half_height, block_height - (invoke_id % half_height) - 1,
4646
);
4747
idx.x += block_offset;
4848
idx.y += block_offset;
4949
return idx;
5050
}
5151
52-
fn get_disperse_indices(thread_id: u32, block_height: u32) -> vec2<u32> {
53-
var block_offset: u32 = ((2 * thread_id) / block_height) * block_height;
52+
fn get_disperse_indices(invoke_id: u32, block_height: u32) -> vec2<u32> {
53+
var block_offset: u32 = ((2 * invoke_id) / block_height) * block_height;
5454
let half_height = block_height / 2;
5555
var idx: vec2<u32> = vec2<u32>(
56-
thread_id % half_height, (thread_id % half_height) + half_height
56+
invoke_id % half_height, (invoke_id % half_height) + half_height
5757
);
5858
idx.x += block_offset;
5959
idx.y += block_offset;
@@ -73,20 +73,20 @@ const ALGO_LOCAL_FLIP = 1;
7373
const ALGO_LOCAL_DISPERSE = 2;
7474
const ALGO_GLOBAL_FLIP = 3;
7575
76-
// Our compute shader will execute specified # of threads or elements / 2 threads
77-
@compute @workgroup_size(${threadsPerWorkgroup}, 1, 1)
76+
// Our compute shader will execute specified # of invocations or elements / 2 invocations
77+
@compute @workgroup_size(${workgroupSize}, 1, 1)
7878
fn computeMain(
7979
@builtin(global_invocation_id) global_id: vec3<u32>,
8080
@builtin(local_invocation_id) local_id: vec3<u32>,
8181
@builtin(workgroup_id) workgroup_id: vec3<u32>,
8282
) {
8383
84-
let offset = ${threadsPerWorkgroup} * 2 * workgroup_id.x;
84+
let offset = ${workgroupSize} * 2 * workgroup_id.x;
8585
// If we will perform a local swap, then populate the local data
8686
if (uniforms.algo <= 2) {
8787
// Assign range of input_data to local_data.
8888
// Range cannot exceed maxWorkgroupsX * 2
89-
// Each thread will populate the workgroup data... (1 thread for every 2 elements)
89+
// Each invocation will populate the workgroup data... (1 invocation for every 2 elements)
9090
local_data[local_id.x * 2] = input_data[offset + local_id.x * 2];
9191
local_data[local_id.x * 2 + 1] = input_data[offset + local_id.x * 2 + 1];
9292
}
@@ -116,7 +116,7 @@ fn computeMain(
116116
}
117117
}
118118
119-
// Ensure that all threads have swapped their own regions of data
119+
// Ensure that all invocations have swapped their own regions of data
120120
workgroupBarrier();
121121
122122
if (uniforms.algo <= ALGO_LOCAL_DISPERSE) {

0 commit comments

Comments
 (0)