Skip to content

Commit

Permalink
added getDeviceAndAdapter, fixed performance.now() units
Browse files Browse the repository at this point in the history
  • Loading branch information
John Owens committed Nov 5, 2024
1 parent 05d4c2f commit 189a0be
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions both_webgpu/both.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
async function main(navigator) {
async function getDeviceAndAdapter(navigator) {
const adapter = await navigator.gpu.requestAdapter();
const hasSubgroups = adapter.features.has("subgroups");
const canTimestamp = adapter.features.has("timestamp-query");
Expand All @@ -8,13 +8,23 @@ async function main(navigator) {
...(hasSubgroups ? ["subgroups"] : []),
],
});
return {
adapter: adapter,
device: device,
};
}

async function main(navigator) {
const { adapter, device } = await getDeviceAndAdapter(navigator);

if (!device) {
fail("Fatal error: Device does not support WebGPU.");
throw new Error("Fatal error: Device does not support WebGPU.");
}
console.log("I am main! (WebGPU)");
if (typeof process !== "undefined") {
console.log(" Process release name:", process.release.name);
} else {
console.log(" I'm probably running in a web browser.");
}

const workgroupSize = 64;
Expand Down Expand Up @@ -121,10 +131,10 @@ async function main(navigator) {
// Finish encoding and submit the commands
const commandBuffer = encoder.finish();
await device.queue.onSubmittedWorkDone();
const passStartTime = performance.now();
const passStartTimeMS = performance.now();
device.queue.submit([commandBuffer]);
await device.queue.onSubmittedWorkDone();
const passEndTime = performance.now();
const passEndTimeMS = performance.now();

// Read the results
await mappableMemdstBuffer.mapAsync(GPUMapMode.READ);
Expand All @@ -150,11 +160,11 @@ async function main(navigator) {
}

let bytesTransferred = 2 * memdest.byteLength;
let ns = passEndTime - passStartTime;
let ns = (passEndTimeMS - passStartTimeMS) * 1000000.0;
console.log(
`Timing result: ${ns} ns; transferred ${bytesTransferred} bytes; bandwidth = ${
bytesTransferred / ns
} GB/s`
);
}
export { main };
export { main, getDeviceAndAdapter };

0 comments on commit 189a0be

Please sign in to comment.