-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
419 lines (360 loc) · 12.2 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import {GPUBufferBindingType} from "../libs/webgpu/GPUEnum.js";
import {ReadbackBuffer} from '../libs/webgpu/ReadbackBuffer.js';
let MAX_THREAD_NUM = 0;
let MAX_GROUP_NUM = 0;
let logElement;
let selectBox;
let adapter;
let device;
let bitonicSortPipelne1;
let bitonicSortPipelne2;
let bitonicSortPipelne3;
let computeSSBOBindGroupLayout;
let computeUniformBindGroupLayout;
async function init() {
selectBox = document.getElementById('selectBox');
selectBox.disabled = true;
adapter = await navigator.gpu?.requestAdapter();
device = await adapter?.requestDevice({});
if (!adapter || !device) {
notSupportedDescription.style.display = "block";
contents.style.display = "none";
return;
}
notSupportedDescription.style.display = "none";
contents.style.display = "block";
MAX_THREAD_NUM = device.limits.maxComputeWorkgroupSizeX;
MAX_GROUP_NUM = 1 << (Math.log2(device.limits.maxComputeWorkgroupsPerDimension) << 0);
const maxNumElementsIndex = Math.log2(MAX_THREAD_NUM * MAX_GROUP_NUM) - 7;
for (let i = 0; i < maxNumElementsIndex; i++) {
const option = document.createElement('option');
option.text = '' + getLength(i);
selectBox.add(option);
}
selectBox.selectedIndex = 7;
selectBox.addEventListener('change', () => {
logElement.innerText = '';
if (getLength(selectBox.selectedIndex) >= 1 << 20) {
log(`\n注意\n大きな要素数を選択すると、CPUでのソート時間が増大し、ブラウザが数秒間フリーズしてみえることがあります。\n`);
}
selectBox.disabled = true;
requestAnimationFrame(() => requestAnimationFrame(() => compute()));
});
logElement = document.getElementById('log');
initializeComputeProgram();
compute();
}
const compute = async () => {
const length = getLength(selectBox.selectedIndex);
const arr = new Float32Array(length);
resetData(arr, length);
await computeCPU(arr.slice(0));
log(`\n----------\n`);
await computeGPU(arr.slice(0));
selectBox.disabled = false;
console.log(`---`);
};
const computeCPU = async (arr) => {
const now = performance.now();
arr.sort(
(a, b) => {
return a - b;
}
);
logElement.innerText = '';
log(`\nソート対象: ${arr.length}要素\n`);
log(`CPUでの実行時間: ${Math.round(performance.now() - now)} ms`);
log(`ソート結果の正当性チェック: ${validateSorted(arr) ? '成功' : '失敗'}`);
// console.log(arr);
};
const computeGPU = async (arr) => {
const now = performance.now();
const length = arr.length;
const threadgroupsPerGrid = Math.max(1, length / MAX_THREAD_NUM);
const result = new Float32Array(length);
const readbackBuffer = new ReadbackBuffer(result);
readbackBuffer.createBuffer(device);
const storageBuffer = device.createBuffer({
size: arr.byteLength,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
});
device.queue.writeBuffer(storageBuffer, 0, arr.buffer);
const storageBufferBindGroup = device.createBindGroup({
layout: computeSSBOBindGroupLayout,
entries: [{
binding: 0,
resource: {
buffer: storageBuffer,
offset: 0,
size: arr.byteLength,
},
}],
});
// compute
const commandEncoder = device.createCommandEncoder({});
const passEncoder = commandEncoder.beginComputePass();
passEncoder.setPipeline(bitonicSortPipelne1);
passEncoder.setBindGroup(0, storageBufferBindGroup);
passEncoder.dispatchWorkgroups(threadgroupsPerGrid, 1, 1);
let uniformBuffer;
if (threadgroupsPerGrid > 1) {
const numK3Dispatches = Math.log2(threadgroupsPerGrid);
const numK2Dispatches = (1 + numK3Dispatches) * numK3Dispatches / 2;
// console.log({numK3Dispatches, numK2Dispatches});
const MINIMUM_UNIFORM_OFFSET_ALIGNMENT = 256;
const elementsPerDispatch = MINIMUM_UNIFORM_OFFSET_ALIGNMENT / Uint32Array.BYTES_PER_ELEMENT;
const uniformBufferData = new Uint32Array(numK2Dispatches * elementsPerDispatch);
let offset = 0;
for (let k = MAX_THREAD_NUM * 2; k <= length; k <<= 1) {
for (let j = k >> 1; j >= MAX_THREAD_NUM; j >>= 1) {
uniformBufferData[offset] = k;
uniformBufferData[offset + 1] = j;
offset += elementsPerDispatch;
// console.log(k, j);
}
}
uniformBuffer = device.createBuffer({
size: uniformBufferData.byteLength,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.UNIFORM
});
device.queue.writeBuffer(uniformBuffer, 0, uniformBufferData.buffer);
const bytesPerDispatch = elementsPerDispatch * Uint32Array.BYTES_PER_ELEMENT;
const uniformBufferBindGroup = device.createBindGroup({
layout: computeUniformBindGroupLayout,
entries: [{
binding: 0,
resource: {
buffer: uniformBuffer,
offset: 0,
size: 4 * Uint32Array.BYTES_PER_ELEMENT,
},
}],
});
let dynamicIndex = 0;
for (let k = 0; k < numK3Dispatches; k += 1) {
passEncoder.setPipeline(bitonicSortPipelne2);
for (let j = 0; j <= k; j += 1) {
passEncoder.setBindGroup(1, uniformBufferBindGroup, [bytesPerDispatch * dynamicIndex]);
passEncoder.dispatchWorkgroups(threadgroupsPerGrid, 1, 1);
dynamicIndex += 1;
}
passEncoder.setPipeline(bitonicSortPipelne3);
passEncoder.dispatchWorkgroups(threadgroupsPerGrid, 1, 1);
}
}
passEncoder.end();
readbackBuffer.copyBufferToBuffer(commandEncoder, storageBuffer);
device.queue.submit([commandEncoder.finish()]);
// get result
await readbackBuffer.mapReadAsync();
log(`GPUでの実行時間: ${Math.round(performance.now() - now)} ms`);
log(`ソート結果の正当性チェック: ${validateSorted(result) ? '成功' : '失敗'}`);
storageBuffer.destroy();
readbackBuffer.destroy();
if (uniformBuffer) {
uniformBuffer.destroy();
}
};
const resetData = (arr, sortLength) => {
for (let i = 0; i < sortLength; i++) {
arr[i] = Math.random();
}
};
const validateSorted = (arr) => {
const length = arr.length;
for (let i = 0; i < length; i++) {
if (i !== length - 1 && arr[i] > arr[i + 1]) {
console.log('validation error:', i, arr[i], arr[i + 1]);
console.log(arr);
return false;
}
}
return true;
};
const initializeComputeProgram = () => {
// language=WGSL
const computeShaderWGSL1 = `
struct SSBO {
data : array<f32>
};
@binding(0) @group(0) var<storage, read_write> ssbo : SSBO;
var<workgroup> sharedData: array<f32, ${MAX_THREAD_NUM}>;
@compute @workgroup_size(${MAX_THREAD_NUM}, 1, 1)
fn main(
@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>,
@builtin(local_invocation_index) LocallInvocationIndex : u32,
@builtin(workgroup_id) WorkgroupID : vec3<u32>
) {
sharedData[LocallInvocationIndex] = ssbo.data[GlobalInvocationID.x];
workgroupBarrier();
var offset:u32 = WorkgroupID.x * ${MAX_THREAD_NUM}u;
var tmp:f32;
for (var k:u32 = 2u; k <= ${MAX_THREAD_NUM}u; k = k << 1u) {
for (var j:u32 = k >> 1u; j > 0u; j = j >> 1u) {
var ixj:u32 = (GlobalInvocationID.x ^ j) - offset;
if (ixj > LocallInvocationIndex) {
if ((GlobalInvocationID.x & k) == 0u) {
if (sharedData[LocallInvocationIndex] > sharedData[ixj]) {
tmp = sharedData[LocallInvocationIndex];
sharedData[LocallInvocationIndex] = sharedData[ixj];
sharedData[ixj] = tmp;
}
}
else
{
if (sharedData[LocallInvocationIndex] < sharedData[ixj]) {
tmp = sharedData[LocallInvocationIndex];
sharedData[LocallInvocationIndex] = sharedData[ixj];
sharedData[ixj] = tmp;
}
}
}
workgroupBarrier();
}
}
ssbo.data[GlobalInvocationID.x] = sharedData[LocallInvocationIndex];
}
`;
const gpuComputePipelineDescriptor1 = {
module: device.createShaderModule({code: computeShaderWGSL1}),
entryPoint: 'main',
};
computeSSBOBindGroupLayout = device.createBindGroupLayout({
entries: [{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: GPUBufferBindingType.storage
}
},
]
});
const computePipelineLayout1 = device.createPipelineLayout({
bindGroupLayouts: [computeSSBOBindGroupLayout],
});
bitonicSortPipelne1 = device.createComputePipeline({
layout: computePipelineLayout1,
compute: gpuComputePipelineDescriptor1
});
// language=WGSL
const computeShaderWGSL2 = `
struct SSBO {
data : array<f32>
};
@binding(0) @group(0) var<storage, read_write> ssbo : SSBO;
struct Uniforms {
numElements : vec4<u32>
};
@binding(0) @group(1) var<uniform> uniforms : Uniforms;
@compute @workgroup_size(${MAX_THREAD_NUM}, 1, 1)
fn main(
@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>
) {
var tmp:f32;
var ixj:u32 = GlobalInvocationID.x ^ uniforms.numElements.y;
if (ixj > GlobalInvocationID.x)
{
if ((GlobalInvocationID.x & uniforms.numElements.x) == 0u)
{
if (ssbo.data[GlobalInvocationID.x] > ssbo.data[ixj])
{
tmp = ssbo.data[GlobalInvocationID.x];
ssbo.data[GlobalInvocationID.x] = ssbo.data[ixj];
ssbo.data[ixj] = tmp;
}
}
else
{
if (ssbo.data[GlobalInvocationID.x] < ssbo.data[ixj])
{
tmp = ssbo.data[GlobalInvocationID.x];
ssbo.data[GlobalInvocationID.x] = ssbo.data[ixj];
ssbo.data[ixj] = tmp;
}
}
}
}
`;
const gpuComputePipelineDescriptor2 = {
module: device.createShaderModule({code: computeShaderWGSL2}),
entryPoint: 'main',
};
computeUniformBindGroupLayout = device.createBindGroupLayout({
entries: [{
binding: 0,
visibility: GPUShaderStage.COMPUTE,
buffer: {
type: GPUBufferBindingType.uniform,
hasDynamicOffset: true
}
},
]
});
const computePipelineLayout2 = device.createPipelineLayout({
bindGroupLayouts: [computeSSBOBindGroupLayout, computeUniformBindGroupLayout],
});
bitonicSortPipelne2 = device.createComputePipeline({
layout: computePipelineLayout2,
compute: gpuComputePipelineDescriptor2
});
// language=WGSL
const computeShaderWGSL3 = `
struct SSBO {
data : array<f32>
};
@binding(0) @group(0) var<storage, read_write> ssbo : SSBO;
struct Uniforms {
numElements : vec4<u32>
};
@binding(0) @group(1) var<uniform> uniforms : Uniforms;
var<workgroup> sharedData: array<f32, ${MAX_THREAD_NUM}>;
@compute @workgroup_size(${MAX_THREAD_NUM}, 1, 1)
fn main(
@builtin(global_invocation_id) GlobalInvocationID : vec3<u32>,
@builtin(local_invocation_index) LocallInvocationIndex : u32,
@builtin(workgroup_id) WorkgroupID : vec3<u32>
) {
sharedData[LocallInvocationIndex] = ssbo.data[GlobalInvocationID.x];
workgroupBarrier();
var offset:u32 = WorkgroupID.x * ${MAX_THREAD_NUM}u;
var tmp:f32;
for (var j:u32 = ${MAX_THREAD_NUM}u >> 1u; j > 0u; j = j >> 1u) {
var ixj:u32 = (GlobalInvocationID.x ^ j) - offset;
if (ixj > LocallInvocationIndex) {
if ((GlobalInvocationID.x & uniforms.numElements.x) == 0u) {
if (sharedData[LocallInvocationIndex] > sharedData[ixj]) {
tmp = sharedData[LocallInvocationIndex];
sharedData[LocallInvocationIndex] = sharedData[ixj];
sharedData[ixj] = tmp;
}
}
else
{
if (sharedData[LocallInvocationIndex] < sharedData[ixj]) {
tmp = sharedData[LocallInvocationIndex];
sharedData[LocallInvocationIndex] = sharedData[ixj];
sharedData[ixj] = tmp;
}
}
}
workgroupBarrier();
}
ssbo.data[GlobalInvocationID.x] = sharedData[LocallInvocationIndex];
}
`;
const gpuComputePipelineDescriptor3 = {
module: device.createShaderModule({code: computeShaderWGSL3}),
entryPoint: 'main',
};
bitonicSortPipelne3 = device.createComputePipeline({
layout: computePipelineLayout2,
compute: gpuComputePipelineDescriptor3
});
};
const getLength = (index) => {
return 1 << (index + 8);
};
const log = (str) => {
logElement.innerText += str + '\n';
};
window.addEventListener('DOMContentLoaded', init);