-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstandalone.mjs
More file actions
228 lines (205 loc) · 6.43 KB
/
standalone.mjs
File metadata and controls
228 lines (205 loc) · 6.43 KB
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
import { BinOpAdd } from "./binop.mjs";
import { datatypeToTypedArray } from "./util.mjs";
let webgpuCreate;
const isDeno = typeof Deno !== "undefined";
const isNode =
!isDeno && typeof process !== "undefined" && process.release.name === "node";
if (isNode) {
// running in Node
// if we were not in conditional code, the following works:
// import { globals, create } from 'webgpu';
let webgpuGlobals;
async function loadWebGPU() {
const module = await import("webgpu");
webgpuGlobals = module.globals;
webgpuCreate = module.create;
}
await loadWebGPU();
Object.assign(globalThis, webgpuGlobals);
} else if (isDeno) {
/* empty */
} else {
// running in Chrome
// eslint-disable-next-line no-unused-vars
const urlParams = new URL(window.location.href).searchParams;
}
// import primitive only, no test suite
import { NoAtomicPKReduce } from "./reduce.mjs";
import { HierarchicalScan } from "./scan.mjs";
import { DLDFScan } from "./scandldf.mjs";
export async function main(navigator) {
const adapter = await navigator.gpu?.requestAdapter();
const hasSubgroups = adapter.features.has("subgroups");
const hasTimestampQuery = adapter.features.has("timestamp-query");
const device = await adapter?.requestDevice({
requiredFeatures: [
...(hasTimestampQuery ? ["timestamp-query"] : []),
...(hasSubgroups ? ["subgroups"] : []),
],
});
if (!device) {
console.error("Fatal error: Device does not support WebGPU.");
}
const inputLength = 2 ** 25; // items, not bytes
const datatype = "u32";
const memsrcX32 = new (datatypeToTypedArray(datatype))(inputLength);
for (let i = 0; i < inputLength; i++) {
switch (datatype) {
case "u32":
memsrcX32[i] = i === 0 ? 11 : memsrcX32[i - 1] + 1; // trying to get u32s
break;
case "f32":
memsrcX32[i] = i & 0x10 ? i - 42 : i + 42;
break;
}
}
console.log("in", memsrcX32);
// eslint-disable-next-line no-unused-vars
const reducePrimitive = new NoAtomicPKReduce({
device,
binop: new BinOpAdd({ datatype }),
datatype: datatype,
gputimestamps: true, //// TODO should work without this
// inputBuffer and outputBuffer are Reduce-specific names
// inputBuffer: { buffer: memsrcBuffer, offset: 0 },
// inputBuffer: memsrcBuffer,
// outputBuffer: memdestBuffer,
});
// eslint-disable-next-line no-unused-vars
const iscanPrimitive = new HierarchicalScan({
device,
binop: new BinOpAdd({ datatype }),
type: "inclusive",
datatype: datatype,
gputimestamps: true, //// TODO should work without this
});
// eslint-disable-next-line no-unused-vars
const escanPrimitive = new HierarchicalScan({
device,
binop: new BinOpAdd({ datatype }),
type: "exclusive",
datatype: datatype,
gputimestamps: true, //// TODO should work without this
});
const dldfscanPrimitive = new DLDFScan({
device,
binop: new BinOpAdd({ datatype }),
type: "exclusive",
datatype: datatype,
gputimestamps: true, //// TODO should work without this
});
// const primitive = iscanPrimitive;
// const primitive = escanPrimitive;
// const primitive = reducePrimitive;
const primitive = dldfscanPrimitive;
let memdestBytes;
switch (primitive.constructor.name) {
case "NoAtomicPKReduce":
memdestBytes = 4;
break;
default:
if (
primitive.constructor.name === "DLDFScan" &&
primitive.type === "reduce"
) {
memdestBytes = 4;
} else {
memdestBytes = memsrcX32.byteLength;
}
break;
}
// allocate/create buffers on the GPU to hold in/out data
const memsrcBuffer = device.createBuffer({
label: `memory source buffer (${datatype})`,
size: memsrcX32.byteLength,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
});
device.queue.writeBuffer(memsrcBuffer, 0, memsrcX32);
const memdestBuffer = device.createBuffer({
label: "memory destination buffer",
size: memdestBytes,
usage:
GPUBufferUsage.STORAGE |
GPUBufferUsage.COPY_SRC |
GPUBufferUsage.COPY_DST /* COPY_DST necessary for initialization */,
});
const mappableMemdestBuffer = device.createBuffer({
label: "mappable memory destination buffer",
size: memdestBytes,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
const memdestDebugBuffer = device.createBuffer({
label: "memory destination debug buffer",
size: memdestBytes,
usage:
GPUBufferUsage.STORAGE |
GPUBufferUsage.COPY_SRC |
GPUBufferUsage.COPY_DST /* COPY_DST necessary for initialization */,
});
const mappableMemdestDebugBuffer = device.createBuffer({
label: "mappable memory debug destination buffer",
size: memdestBytes,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
await primitive.execute({
inputBuffer: memsrcBuffer,
outputBuffer: memdestBuffer,
debugBuffer: memdestDebugBuffer,
});
// copy output back to host
const encoder = device.createCommandEncoder({
label: "copy result CPU->GPU encoder",
});
encoder.copyBufferToBuffer(
memdestBuffer,
0,
mappableMemdestBuffer,
0,
mappableMemdestBuffer.size
);
encoder.copyBufferToBuffer(
memdestDebugBuffer,
0,
mappableMemdestDebugBuffer,
0,
mappableMemdestDebugBuffer.size
);
const commandBuffer = encoder.finish();
device.queue.submit([commandBuffer]);
// Read the results
await mappableMemdestBuffer.mapAsync(GPUMapMode.READ);
const memdest = new (datatypeToTypedArray(datatype))(
mappableMemdestBuffer.getMappedRange().slice()
);
mappableMemdestBuffer.unmap();
await mappableMemdestDebugBuffer.mapAsync(GPUMapMode.READ);
const memdebug = new (datatypeToTypedArray(datatype))(
mappableMemdestDebugBuffer.getMappedRange().slice()
);
mappableMemdestDebugBuffer.unmap();
console.log("out", memdest);
console.log("memdebug", memdebug);
if (primitive.validate) {
const errorstr = primitive.validate({
inputBuffer: memsrcX32,
outputBuffer: memdest,
debugBuffer: memdebug,
});
if (errorstr === "") {
console.info("Validation passed");
} else {
console.error(`Validation failed:\n${errorstr}`);
}
}
// currently no timing computation, that's fine
}
if (isNode) {
const navigator = {
gpu: webgpuCreate([
"enable-dawn-features=use_user_defined_labels_in_backend,disable_symbol_renaming",
]),
};
main(navigator);
} else if (isDeno) {
main(navigator);
}