-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmembw.html
209 lines (184 loc) · 7.33 KB
/
membw.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Measuring Peak Memory Bandwidth</title>
</head>
<body>
<div id="plot"></div>
<script src="./webgpufundamentals-timing-original.js"></script>
<script type="module">
// for uniform handling
import {
makeShaderDataDefinitions,
makeStructuredView,
} from "https://greggman.github.io/webgpu-utils/dist/1.x/webgpu-utils.module.js";
import * as Plot from "https://cdn.jsdelivr.net/npm/@observablehq/[email protected]/+esm";
const data = [];
const adapter = await navigator.gpu?.requestAdapter();
const canTimestamp = adapter.features.has("timestamp-query");
const device = await adapter?.requestDevice({
requiredFeatures: [...(canTimestamp ? ["timestamp-query"] : [])],
});
if (!device) {
fail("Fatal error: Device does not support WebGPU.");
}
const range = (min, max) => [...Array(max - min + 1).keys()].map(i => i + min);
const workgroupSizes = range(0,7).map((i) => 2 ** i);
const memsrcSizes = range(10,25).map((i) => 2 ** i);
for (const workgroupSize of workgroupSizes) {
for (const memsrcSize of memsrcSizes) {
const timingHelper = new TimingHelper(device);
const itemsPerWorkgroup = memsrcSize / workgroupSize;
const dispatchGeometry = [itemsPerWorkgroup, 1];
while (
dispatchGeometry[0] >
adapter.limits.maxComputeWorkgroupsPerDimension
) {
dispatchGeometry[0] /= 2;
dispatchGeometry[1] *= 2;
}
console.log(`itemsPerWorkgroup: ${itemsPerWorkgroup}
workgroup size: ${workgroupSize}
maxComputeWGPerDim: ${adapter.limits.maxComputeWorkgroupsPerDimension}
dispatchGeometry: ${dispatchGeometry}`);
const memsrc = new Uint32Array(memsrcSize);
for (let i = 0; i < memsrc.length; i++) {
memsrc[i] = i;
}
const memcpyModule = device.createShaderModule({
label: "copy large chunk of memory from memSrc to memDest",
code: /* wgsl */ `
/* output */
@group(0) @binding(0) var<storage, read_write> memDest: array<u32>;
/* input */
@group(0) @binding(1) var<storage, read> memSrc: array<u32>;
@compute @workgroup_size(${workgroupSize}) fn memcpyKernel(
@builtin(global_invocation_id) id: vec3u,
@builtin(num_workgroups) nwg: vec3u,
@builtin(workgroup_id) wgid: vec3u) {
let i = id.y * nwg.x * ${workgroupSize} + id.x;
memDest[i] = memSrc[i] + 1;
}
`,
});
const memcpyPipeline = device.createComputePipeline({
label: "memcpy compute pipeline",
layout: "auto",
compute: {
module: memcpyModule,
},
});
// create buffers on the GPU to hold data
// read-only inputs:
const memsrcBuffer = device.createBuffer({
label: "memory source buffer",
size: memsrc.byteLength,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
});
device.queue.writeBuffer(memsrcBuffer, 0, memsrc);
const memdestBuffer = device.createBuffer({
label: "memory destination buffer",
size: memsrc.byteLength,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
});
const mappableMemdstBuffer = device.createBuffer({
label: "mappable memory destination buffer",
size: memsrc.byteLength,
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
});
/** Set up bindGroups per compute kernel to tell the shader which buffers to use */
const memcpyBindGroup = device.createBindGroup({
label: "bindGroup for memcpy kernel",
layout: memcpyPipeline.getBindGroupLayout(0),
entries: [
{ binding: 0, resource: { buffer: memdestBuffer } },
{ binding: 1, resource: { buffer: memsrcBuffer } },
],
});
const encoder = device.createCommandEncoder({
label: "memcpy encoder",
});
const memcpyPass = timingHelper.beginComputePass(encoder, {
label: "memcpy compute pass",
});
memcpyPass.setPipeline(memcpyPipeline);
memcpyPass.setBindGroup(0, memcpyBindGroup);
// TODO handle not evenly divisible by wgSize
memcpyPass.dispatchWorkgroups(...dispatchGeometry);
memcpyPass.end();
// Encode a command to copy the results to a mappable buffer.
// this is (from, to)
encoder.copyBufferToBuffer(
memdestBuffer,
0,
mappableMemdstBuffer,
0,
mappableMemdstBuffer.size
);
// Finish encoding and submit the commands
const command_buffer = encoder.finish();
device.queue.submit([command_buffer]);
// Read the results
await mappableMemdstBuffer.mapAsync(GPUMapMode.READ);
const memdest = new Uint32Array(
mappableMemdstBuffer.getMappedRange().slice()
);
mappableMemdstBuffer.unmap();
let errors = 0;
for (let i = 0; i < memdest.length; i++) {
if (memsrc[i] + 1 != memdest[i]) {
if (errors < 5) {
console.log(
`Error ${errors}: i=${i}, src=${memsrc[i]}, dest=${memdest[i]}`
);
}
errors++;
}
}
if (errors > 0) {
console.log(`Memdest size: ${memdest.length} | Errors: ${errors}`);
} else {
console.log(`Memdest size: ${memdest.length} | No errors!`);
}
timingHelper.getResult().then((ns) => {
let bytesTransferred = 2 * memdest.byteLength;
console.log(
`Timing result: ${ns}; transferred ${bytesTransferred} bytes; bandwidth = ${
bytesTransferred / ns
} GB/s`
);
data.push({
time: ns,
bytesTransferred: bytesTransferred,
memsrcSize: memsrcSize,
bandwidth: bytesTransferred / ns,
workgroupSize: workgroupSize,
});
});
}
}
console.log(data);
function fail(msg) {
// eslint-disable-next-line no-alert
alert(msg);
}
const plot = Plot.plot({
color: { type: "ordinal", legend: true },
marks: [
Plot.lineY(data, {
x: "memsrcSize",
y: "bandwidth",
stroke: "workgroupSize",
tip: true,
}),
Plot.text(data, Plot.selectLast({x: "memsrcSize", y: "bandwidth", z: "workgroupSize", text: "workgroupSize", textAnchor: "start", dx: 3}))
],
x: {type: "log", label: "Copied array size (B)"},
y: {type: "log", label: "Achieved bandwidth (GB/s)"},
});
const div = document.querySelector("#plot");
div.append(plot);
</script>
</body>
</html>