-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandalone.mjs
120 lines (108 loc) · 3.42 KB
/
standalone.mjs
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
import {
/*BinOpAddU32,
BinOpMinU32,
BinOpMaxU32,
BinOpAddF32,
BinOpMinF32,*/
BinOpMaxF32,
} from "./binop.mjs";
import { datatypeToTypedArray } from "./util.mjs";
if (typeof process !== "undefined" && process.release.name === "node") {
// running in Node
} 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";
export async function main(navigator) {
const adapter = await navigator.gpu?.requestAdapter();
const hasSubgroups = adapter.features.has("subgroups");
const canTimestamp = adapter.features.has("timestamp-query");
const device = await adapter?.requestDevice({
requiredFeatures: [
...(canTimestamp ? ["timestamp-query"] : []),
...(hasSubgroups ? ["subgroups"] : []),
],
});
if (!device) {
console.error("Fatal error: Device does not support WebGPU.");
}
const memsrcSize = 2 ** 20; // items, not bytes
const datatype = "f32";
const memsrcX32 = new (datatypeToTypedArray(datatype))(memsrcSize);
for (let i = 0; i < memsrcSize; i++) {
switch (datatype) {
case "u32":
memsrcX32[i] = i == 0 ? 11 : memsrcX32[i - 1] + 1; // trying to get u32s
break;
case "f32":
memsrcX32[i] = i + 42;
break;
}
}
const memdestBytes = 4;
// allocate/create buffers on the GPU to hold in/out data
const memsrcBuffer = device.createBuffer({
label: "memory source buffer (uint)",
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 primitive = new NoAtomicPKReduce({
device,
binop: BinOpMaxF32,
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,
});
await primitive.execute({ inputBuffer: memsrcBuffer });
// copy output back to host
const encoder = device.createCommandEncoder({
label: "copy result CPU->GPU encoder",
});
encoder.copyBufferToBuffer(
memdestBuffer,
0,
mappableMemdestBuffer,
0,
mappableMemdestBuffer.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();
if (primitive.validate) {
const errorstr = primitive.validate({
inputBuffer: memsrcX32,
outputBuffer: memdest,
});
if (errorstr == "") {
console.info("Validation passed");
} else {
console.error(`Validation failed: ${errorstr}`);
}
}
// currently no timing computation, that's fine
}