-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
255 lines (215 loc) · 10.8 KB
/
index.html
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
<html>
<body>
<script>
// Set up the WebGPU context and initialize the required resources
async function initWebGPU() {
const gpu = navigator.gpu;
const adapter = await gpu.requestAdapter();
const device = await adapter.requestDevice({
requiredLimits: {
maxBufferSize: adapter.limits.maxBufferSize,
maxStorageBufferBindingSize: adapter.limits.maxStorageBufferBindingSize,
maxComputeWorkgroupSizeX: 1024,
maxComputeInvocationsPerWorkgroup: 1024,
maxComputeWorkgroupStorageSize: adapter.limits.maxComputeWorkgroupStorageSize,
maxComputeWorkgroupsPerDimension: adapter.limits.maxComputeWorkgroupsPerDimension,
},
});
return device;
}
async function loadShader() {
// fix caching
const shaders_in_order = [
'bigint.wgsl',
'field.wgsl',
'curve.wgsl',
'storage.wgsl',
'pippenger.wgsl',
// 'pippenger_fake.wgsl',
'main.wgsl',
];
let shaderCode = "";
for (let file of shaders_in_order) {
const resp = await fetch(file);
shaderCode += await resp.text();
}
return shaderCode;
}
const LIMB_WIDTH = 16;
const BIGINT_SIZE = 256;
const NUM_LIMBS = BIGINT_SIZE / LIMB_WIDTH;
const WORKGROUP_SIZE = 64;
const NUM_INVOCATIONS = 4096;
const MSM_SIZE = WORKGROUP_SIZE * NUM_INVOCATIONS;
const ZERO_POINT = new Uint32Array([
...bigintToUint32Array(BigInt('0')),
...bigintToUint32Array(BigInt('0')),
...bigintToUint32Array(BigInt('1')),
]);
console.log(NUM_INVOCATIONS);
// Set up the input and output buffers for the matrices
async function setupBuffers(device, msm_size, in_points, in_scalars) {
const points = device.createBuffer({
size: msm_size * 3 * NUM_LIMBS * 4,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
});
const scalars = device.createBuffer({
size: msm_size * NUM_LIMBS * 4,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
});
const result = device.createBuffer({
size: NUM_INVOCATIONS * 3 * NUM_LIMBS * 4,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
});
device.queue.writeBuffer(points, 0, new Uint8Array(in_points.buffer));
device.queue.writeBuffer(scalars, 0, new Uint8Array(in_scalars.buffer));
return [points, scalars, result];
}
// Create a pipeline and bind groups for the shader
async function setupPipeline(device, shaderCode, msm_size, points, scalars, result) {
const shaderModule = device.createShaderModule({ code: shaderCode });
const bindGroupLayout = device.createBindGroupLayout({
entries: [
{ binding: 0, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
{ binding: 1, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
{ binding: 2, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
{ binding: 3, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
{ binding: 4, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
{ binding: 5, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } },
],
});
const pipelineLayout = device.createPipelineLayout({
bindGroupLayouts: [
bindGroupLayout
],
});
const computePipeline = device.createComputePipeline({
layout: pipelineLayout,
compute: { module: shaderModule, entryPoint: "main" },
});
const aggregatePipeline = device.createComputePipeline({
layout: pipelineLayout,
compute: { module: shaderModule, entryPoint: "aggregate" },
});
const mem = device.createBuffer({
size: MSM_SIZE * 3 * NUM_LIMBS * 4,
usage: GPUBufferUsage.STORAGE
});
const buffer1 = device.createBuffer({
size: 256 * NUM_INVOCATIONS * 3 * NUM_LIMBS * 4,
usage: GPUBufferUsage.STORAGE
});
const buffer2 = device.createBuffer({
size: 256 * NUM_INVOCATIONS * 3 * NUM_LIMBS * 4,
usage: GPUBufferUsage.STORAGE
});
const bindGroup = device.createBindGroup({
layout: bindGroupLayout,
entries: [
{ binding: 0, resource: { buffer: points } },
{ binding: 1, resource: { buffer: scalars } },
{ binding: 2, resource: { buffer: result } },
{ binding: 3, resource: { buffer: mem } },
{ binding: 4, resource: { buffer: buffer1 } },
{ binding: 5, resource: { buffer: buffer2 } },
],
});
return [computePipeline, aggregatePipeline, bindGroup];
}
// Run the compute pass and read back the results
async function runComputePass(device, pipeline, aggregatePipeline, bindGroup, msm_size, result, gpuReadBuffer) {
const commandEncoder = device.createCommandEncoder();
const computePass = commandEncoder.beginComputePass();
computePass.setPipeline(pipeline);
computePass.setBindGroup(0, bindGroup);
computePass.dispatchWorkgroups(NUM_INVOCATIONS);
computePass.end();
const aggregatePass = commandEncoder.beginComputePass();
aggregatePass.setPipeline(aggregatePipeline);
aggregatePass.setBindGroup(0, bindGroup);
aggregatePass.dispatchWorkgroups(1);
aggregatePass.end();
commandEncoder.copyBufferToBuffer(result, 0, gpuReadBuffer, 0, NUM_INVOCATIONS * 3 * NUM_LIMBS * 4);
device.queue.submit([commandEncoder.finish()]);
await device.queue.onSubmittedWorkDone();
await gpuReadBuffer.mapAsync(GPUBufferUsage.MAP_READ, 0, NUM_INVOCATIONS * 3 * NUM_LIMBS * 4);
const copyArrayBuffer = gpuReadBuffer.getMappedRange(0, NUM_INVOCATIONS * 3 * NUM_LIMBS * 4);
const data = copyArrayBuffer.slice();
const output = new Uint32Array(copyArrayBuffer);
return output;
}
function get_packed_point(x, y) {
const one = bigintToUint32Array(BigInt('1'));
return new Uint32Array([
...x,
...y,
...one
]);
}
const run = (async () => {
document.write("generating random points<br/>");
// Initialize WebGPU and set up matrices
const device = await initWebGPU();
const msm_size = MSM_SIZE;
const coord_x = bigintToUint32Array(BigInt('22304380549750642616165107876029345325911088198117424279971154895103981677948'));
const coord_y = bigintToUint32Array(BigInt('14354096399413720219912473247241970521073754194408414292017996939864946211566'));
const scalar = bigintToUint32Array(BigInt('115792089237316195423570985008687907853269984665640564039457584007913129639935'));
document.write("MSM Size: " + msm_size.toString() + "<br/>");
let in_points = new Uint32Array(msm_size * 3 * NUM_LIMBS);
let in_scalars = new Uint32Array(msm_size * NUM_LIMBS);
const curr = get_packed_point(coord_x, coord_y);
for (let i = 0; i < msm_size; i++) {
in_points.set(curr, i * 3 * NUM_LIMBS);
in_scalars.set(scalar, i * NUM_LIMBS);
}
document.write("writing data to webgpu<br/>");
// Set up the buffers
const [points, scalars, result] = await setupBuffers(device, msm_size, in_points, in_scalars);
// Load shader code
const shaderCode = await loadShader();
// Set up the pipeline and bind groups
const [computePipeline, aggregatePipeline, bindGroup] = await setupPipeline(device, shaderCode, msm_size, points, scalars, result);
// Set up the read buffer
const gpuReadBuffer = device.createBuffer({
size: NUM_INVOCATIONS * 3 * NUM_LIMBS * 4,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
});
document.write("starting shader computation.<br/>");
const start = performance.now();
// Run the compute pass and read back the results
const output = await runComputePass(device, computePipeline, aggregatePipeline, bindGroup, msm_size, result, gpuReadBuffer);
console.log(output);
const x = uint32ArrayToBigint(output.slice(0, 16));
const y = uint32ArrayToBigint(output.slice(16, 32));
const z = uint32ArrayToBigint(output.slice(32, 48));
document.write("finished in (ms): " + (performance.now() - start).toString() + "<br/>");
document.write("Output (in jacobian form):<br/>");
document.write('x: 0x' + x.toString(16) + "<br/>");
document.write('y: 0x' + y.toString(16) + "<br/>");
document.write('z: 0x' + z.toString(16) + "<br/>");
});
function bigintToUint32Array(bigint) {
// Convert the BigInt to a hex string
const hexString = bigint.toString(16);
// Pad the hex string with leading zeros, if necessary
const paddedHexString = hexString.padStart(BIGINT_SIZE/4, '0');
// Split the padded hex string into an array of 16-bit values
const uint32Array = new Uint32Array(paddedHexString.length / 4);
for (let i = 0; i < paddedHexString.length; i += 4) {
uint32Array[i / 4] = parseInt(paddedHexString.slice(i, i + 4), 16);
}
return uint32Array.reverse();
}
function uint32ArrayToBigint(uint32Array) {
// Convert the Uint16Array to a hex string
let hexString = '';
for (const uint32 of uint32Array) {
hexString = uint32.toString(16).padStart(4, '0') + hexString;
}
// Convert the hex string to a BigInt
return BigInt('0x' + hexString);
}
</script>
<button onclick="javascript:run()">Run it!</button>
</body>
</html>