2.4 KiB
WebGPU Bitonic Sort (Unsigned Integers)
This project implements a GPU-accelerated bitonic sort for unsigned integers using WebGPU compute shaders. It performs a local per-workgroup sort followed by a multi-pass global bitonic merge and measures execution time in the browser.
Overview
- Sorting algorithm: Bitonic sort
- Execution model: WebGPU compute pipelines
- Data type:
Uint32 - Element count:
131,072 - Workgroup size:
256 - Execution environment: Modern browser with WebGPU enabled
The implementation splits sorting into two stages:
-
Local sort Each workgroup sorts its local block of data.
-
Global bitonic merge Multiple compute passes perform the bitonic merge across all workgroups using precomputed
jandkpass parameters.
Structure
-
WebGpu.jsAbstraction layer for creating pipelines, bind groups, and buffers. -
Measure.jsSimple performance measurement utility with optional DOM output. -
shaders/localSort.wgslCompute shader for local (per-workgroup) sorting. -
shaders/bitonicSortUIntMultiPass.wgslCompute shader for multi-pass global bitonic merging.
How It Works
- Request a WebGPU adapter and device.
- Initialize index buffers and bitonic pass parameters (
jArray,kArray). - Run a local compute pass to partially sort data.
- Run a global bitonic merge with repeated dispatches.
- Measure execution time for multiple iterations.
- Read back the result buffer and verify correctness.
After completion, the result is validated on the CPU to ensure that all values are sorted correctly.
Requirements
- A browser with WebGPU support enabled (for example Chromium-based browsers with the appropriate flags).
- A GPU and driver that support WebGPU compute shaders.
- A static file server (due to module imports and shader loading).
Running
Serve the project directory via a local web server and open the page that loads this script.
Example:
python3 -m http.server
Then open:
http://localhost:8000
Output
- Timing results are written to the page via
Measure. - Final verification logs to the console:
There is error? false 131072
If true, the GPU sort produced incorrect results.
Purpose
This project serves as:
- A WebGPU compute benchmark
- A reference implementation of GPU-based bitonic sorting
- A foundation for GPU-accelerated data processing pipelines
License
MIT