|
3 | 3 | <html> |
4 | 4 | <head> |
5 | 5 | <meta charset="utf-8" /> |
6 | | - <title>Standalone WebGPU Scan/Reduce Primitive Test, with Configuration Pane</title> |
| 6 | + <title> |
| 7 | + Standalone WebGPU Scan/Reduce Primitive Test, with Configuration Pane |
| 8 | + </title> |
| 9 | + <link |
| 10 | + rel="stylesheet" |
| 11 | + href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css" |
| 12 | + /> |
7 | 13 | </head> |
8 | 14 |
|
9 | 15 | <body> |
10 | | - <div id="instructions"> |
11 | | - <p>Set your parameters in the pane and click "Start" to run and validate a WebGPU scan/reduce.</p> |
12 | | - </div> |
13 | | - <script src="scan_pane_example.mjs" type="module"></script> |
14 | | - <div id ="webgpu-results"></div> |
| 16 | + <p> |
| 17 | + This example is a self-contained use of the <code>scan</code> and |
| 18 | + <code>sort</code> primitives. Gridwise's scan encapsulates an exclusive |
| 19 | + scan, an inclusive scan, and a reduce. (A simpler scan-only example is |
| 20 | + <a href="scan_example">here</a>.) Set your parameters in the pane and |
| 21 | + click "Start" to run and validate a WebGPU scan/reduce/sort. |
| 22 | + <a |
| 23 | + href="https://github.com/gridwise-webgpu/gridwise/blob/main/examples/scan_pane_example.mjs" |
| 24 | + >The entire JS source file is in github.</a |
| 25 | + > |
| 26 | + While the entire file contains substantial WebGPU and input-output setup |
| 27 | + boilerplate, the important parts follow. |
| 28 | + </p> |
| 29 | + <p> |
| 30 | + The pane selects several parameters, here listed with default settings: |
| 31 | + </p> |
| 32 | + <pre><code class="language-javascript">const params = { |
| 33 | + /* defaults */ |
| 34 | + primitive: "exclusive", |
| 35 | + datatype: "u32", |
| 36 | + binop: "add", |
| 37 | + direction: "ascending", |
| 38 | + inputLength: 2 ** 20, |
| 39 | +};</code></pre> |
| 40 | + <p> |
| 41 | + Whenever you change a parameter, it triggers a call of |
| 42 | + <code>buildAndRun</code>. This function initializes an input array and |
| 43 | + sets up all WebGPU calls necessary to declare and run the primitive. |
| 44 | + Depending on the selected primitive, we declare <code>primitive</code> as |
| 45 | + either a sort or scan primitive: |
| 46 | + </p> |
| 47 | + <pre><code class="language-javascript">let primitive; |
| 48 | +switch (params.primitive) { |
| 49 | + case "sort_keys": |
| 50 | + primitive = new OneSweepSort({ |
| 51 | + device, |
| 52 | + datatype: params.datatype, |
| 53 | + direction: params.direction, |
| 54 | + copyOutputToTemp: true, |
| 55 | + }); |
| 56 | + break; |
| 57 | + default: |
| 58 | + primitive = new DLDFScan({ |
| 59 | + device, |
| 60 | + binop: makeBinOp({ op: params.binop, datatype: params.datatype }), |
| 61 | + type: params.primitive, |
| 62 | + datatype: params.datatype, |
| 63 | + }); |
| 64 | + break; |
| 65 | + }</code></pre> |
| 66 | + <p> |
| 67 | + We have declared buffers (using WebGPU's |
| 68 | + <code>device.createBuffer</code>). These are called |
| 69 | + <code>memsrcBuffer</code> and <code>memdestBuffer</code>. We then call the |
| 70 | + primitive's <code>execute</code> procedure (note that |
| 71 | + <code>execute</code> is <code>async</code>, and that the names of the |
| 72 | + arguments for <code>scan</code> and <code>sort</code> are different): |
| 73 | + </p> |
| 74 | + <pre><code class="language-javascript"> /* sort and scan have different arguments */ |
| 75 | +switch (params.primitive) { |
| 76 | + case "sort_keys": |
| 77 | + await primitive.execute({ |
| 78 | + keysInOut: memsrcBuffer, |
| 79 | + keysTemp: memdestBuffer, |
| 80 | + }); |
| 81 | + break; |
| 82 | + default: |
| 83 | + await primitive.execute({ |
| 84 | + inputBuffer: memsrcBuffer, |
| 85 | + outputBuffer: memdestBuffer, |
| 86 | + }); |
| 87 | + break; |
| 88 | +}</code></pre> |
| 89 | + <p> |
| 90 | + We then read back the result from the GPU and validate it. Click "Start" |
| 91 | + to run the selected primitive and the results should display below. The |
| 92 | + developer console will show the input and output arrays. |
| 93 | + </p> |
| 94 | + <hr /> |
| 95 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script> |
| 96 | + <script> |
| 97 | + hljs.highlightAll(); |
15 | 98 | </script> |
| 99 | + <script src="scan_pane_example.mjs" type="module"></script> |
| 100 | + <div id="webgpu-results"></div> |
16 | 101 | </body> |
17 | 102 | </html> |
0 commit comments