🚀
浏览器端 GPU 排序
通过简洁的 TypeScript API 调用 WebGPU 计算着色器来运行 Bitonic 和 Radix 排序实现。
import { GPUContext, BitonicSorter } from 'webgpu-sorting';
// Initialize WebGPU context
const gpu = new GPUContext();
await gpu.initialize();
// Create sorter
const sorter = new BitonicSorter(gpu);
// Sort data on GPU
const data = new Uint32Array([5, 2, 8, 1, 9, 3, 7, 4, 6, 0]);
const { sortedData } = await sorter.sort(data);
console.log(sortedData); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]当满足以下条件时,GPU 排序更具优势:
使用交互式演示在你自己的硬件上测量交叉点,而非依赖固定的基准测试声明。
graph TB
subgraph CPU["CPU 侧"]
A[输入数组] --> B[生成数据]
B --> C[创建 GPU 缓冲区]
end
subgraph GPU["GPU 计算着色器"]
D[读取缓冲区] --> E[Bitonic/Radix Sort]
E --> F[并行遍历]
F --> G[写入已排序缓冲区]
end
subgraph Output
G --> H[回读至 CPU]
H --> I[验证与计时]
end
C --> D在架构文档中了解更多。