@graphty/webgpu-graph-algorithms 0.6.14 → 0.6.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/README.md +52 -52
  2. package/dist/browser.js +1 -1
  3. package/dist/chunks/{context-Bi6AhScG.js → context-VIvatQOo.js} +69 -34
  4. package/dist/chunks/context-VIvatQOo.js.map +1 -0
  5. package/dist/node.js +1 -1
  6. package/dist/src/accelerator.d.ts +5 -3
  7. package/dist/src/accelerator.d.ts.map +1 -1
  8. package/dist/src/accelerator.js +101 -5
  9. package/dist/src/accelerator.js.map +1 -1
  10. package/dist/src/algorithms/all-pairs.d.ts +41 -0
  11. package/dist/src/algorithms/all-pairs.d.ts.map +1 -0
  12. package/dist/src/algorithms/all-pairs.js +181 -0
  13. package/dist/src/algorithms/all-pairs.js.map +1 -0
  14. package/dist/src/algorithms/betweenness.d.ts +70 -0
  15. package/dist/src/algorithms/betweenness.d.ts.map +1 -0
  16. package/dist/src/algorithms/betweenness.js +538 -0
  17. package/dist/src/algorithms/betweenness.js.map +1 -0
  18. package/dist/src/algorithms/closeness.d.ts +15 -5
  19. package/dist/src/algorithms/closeness.d.ts.map +1 -1
  20. package/dist/src/algorithms/closeness.js +112 -26
  21. package/dist/src/algorithms/closeness.js.map +1 -1
  22. package/dist/src/algorithms/components.d.ts +9 -1
  23. package/dist/src/algorithms/components.d.ts.map +1 -1
  24. package/dist/src/algorithms/components.js +2 -2
  25. package/dist/src/algorithms/components.js.map +1 -1
  26. package/dist/src/algorithms/label-propagation.d.ts +31 -0
  27. package/dist/src/algorithms/label-propagation.d.ts.map +1 -0
  28. package/dist/src/algorithms/label-propagation.js +254 -0
  29. package/dist/src/algorithms/label-propagation.js.map +1 -0
  30. package/dist/src/algorithms/simple-symmetric.d.ts +88 -0
  31. package/dist/src/algorithms/simple-symmetric.d.ts.map +1 -0
  32. package/dist/src/algorithms/simple-symmetric.js +347 -0
  33. package/dist/src/algorithms/simple-symmetric.js.map +1 -0
  34. package/dist/src/algorithms/triangles.d.ts +34 -0
  35. package/dist/src/algorithms/triangles.d.ts.map +1 -0
  36. package/dist/src/algorithms/triangles.js +203 -0
  37. package/dist/src/algorithms/triangles.js.map +1 -0
  38. package/dist/src/constants.d.ts +53 -0
  39. package/dist/src/constants.d.ts.map +1 -1
  40. package/dist/src/constants.js +53 -0
  41. package/dist/src/constants.js.map +1 -1
  42. package/dist/src/index.d.ts +12 -3
  43. package/dist/src/index.d.ts.map +1 -1
  44. package/dist/src/index.js +8 -1
  45. package/dist/src/index.js.map +1 -1
  46. package/dist/src/kernel/prelude.d.ts.map +1 -1
  47. package/dist/src/kernel/prelude.js +4 -1
  48. package/dist/src/kernel/prelude.js.map +1 -1
  49. package/dist/src/kernels.d.ts +24 -6
  50. package/dist/src/kernels.d.ts.map +1 -1
  51. package/dist/src/kernels.js +373 -7
  52. package/dist/src/kernels.js.map +1 -1
  53. package/dist/src/memory/residency.js +15 -4
  54. package/dist/src/memory/residency.js.map +1 -1
  55. package/dist/src/primitives/coo-to-csr.d.ts +73 -0
  56. package/dist/src/primitives/coo-to-csr.d.ts.map +1 -0
  57. package/dist/src/primitives/coo-to-csr.js +183 -0
  58. package/dist/src/primitives/coo-to-csr.js.map +1 -0
  59. package/dist/src/primitives/frontier.d.ts +2 -0
  60. package/dist/src/primitives/frontier.d.ts.map +1 -1
  61. package/dist/src/primitives/frontier.js +2 -0
  62. package/dist/src/primitives/frontier.js.map +1 -1
  63. package/dist/src/primitives/group-by-key.d.ts +82 -0
  64. package/dist/src/primitives/group-by-key.d.ts.map +1 -0
  65. package/dist/src/primitives/group-by-key.js +147 -0
  66. package/dist/src/primitives/group-by-key.js.map +1 -0
  67. package/dist/src/types/accelerator.d.ts +19 -7
  68. package/dist/src/types/accelerator.d.ts.map +1 -1
  69. package/dist/src/types/algorithms.d.ts +4 -0
  70. package/dist/src/types/algorithms.d.ts.map +1 -1
  71. package/dist/src/types/all-pairs.d.ts +35 -0
  72. package/dist/src/types/all-pairs.d.ts.map +1 -0
  73. package/dist/src/types/all-pairs.js +8 -0
  74. package/dist/src/types/all-pairs.js.map +1 -0
  75. package/dist/src/types/betweenness.d.ts +35 -0
  76. package/dist/src/types/betweenness.d.ts.map +1 -0
  77. package/dist/src/types/betweenness.js +7 -0
  78. package/dist/src/types/betweenness.js.map +1 -0
  79. package/dist/src/types/community.d.ts +18 -0
  80. package/dist/src/types/community.d.ts.map +1 -0
  81. package/dist/src/types/community.js +5 -0
  82. package/dist/src/types/community.js.map +1 -0
  83. package/dist/src/types/structure.d.ts +27 -0
  84. package/dist/src/types/structure.d.ts.map +1 -0
  85. package/dist/src/types/structure.js +8 -0
  86. package/dist/src/types/structure.js.map +1 -0
  87. package/dist/src/wgsl/apsp-fw.wgsl.d.ts +25 -0
  88. package/dist/src/wgsl/apsp-fw.wgsl.d.ts.map +1 -0
  89. package/dist/src/wgsl/apsp-fw.wgsl.js +113 -0
  90. package/dist/src/wgsl/apsp-fw.wgsl.js.map +1 -0
  91. package/dist/src/wgsl/apsp-init.wgsl.d.ts +12 -0
  92. package/dist/src/wgsl/apsp-init.wgsl.d.ts.map +1 -0
  93. package/dist/src/wgsl/apsp-init.wgsl.js +26 -0
  94. package/dist/src/wgsl/apsp-init.wgsl.js.map +1 -0
  95. package/dist/src/wgsl/bc-backward.wgsl.d.ts +15 -0
  96. package/dist/src/wgsl/bc-backward.wgsl.d.ts.map +1 -0
  97. package/dist/src/wgsl/bc-backward.wgsl.js +34 -0
  98. package/dist/src/wgsl/bc-backward.wgsl.js.map +1 -0
  99. package/dist/src/wgsl/bc-edge-gather.wgsl.d.ts +12 -0
  100. package/dist/src/wgsl/bc-edge-gather.wgsl.d.ts.map +1 -0
  101. package/dist/src/wgsl/bc-edge-gather.wgsl.js +36 -0
  102. package/dist/src/wgsl/bc-edge-gather.wgsl.js.map +1 -0
  103. package/dist/src/wgsl/bc-finalize.wgsl.d.ts +21 -0
  104. package/dist/src/wgsl/bc-finalize.wgsl.d.ts.map +1 -0
  105. package/dist/src/wgsl/bc-finalize.wgsl.js +47 -0
  106. package/dist/src/wgsl/bc-finalize.wgsl.js.map +1 -0
  107. package/dist/src/wgsl/bc-forward-edge.wgsl.d.ts +15 -0
  108. package/dist/src/wgsl/bc-forward-edge.wgsl.d.ts.map +1 -0
  109. package/dist/src/wgsl/bc-forward-edge.wgsl.js +76 -0
  110. package/dist/src/wgsl/bc-forward-edge.wgsl.js.map +1 -0
  111. package/dist/src/wgsl/bc-forward.wgsl.d.ts +23 -0
  112. package/dist/src/wgsl/bc-forward.wgsl.d.ts.map +1 -0
  113. package/dist/src/wgsl/bc-forward.wgsl.js +106 -0
  114. package/dist/src/wgsl/bc-forward.wgsl.js.map +1 -0
  115. package/dist/src/wgsl/bc-gather.wgsl.d.ts +9 -0
  116. package/dist/src/wgsl/bc-gather.wgsl.d.ts.map +1 -0
  117. package/dist/src/wgsl/bc-gather.wgsl.js +20 -0
  118. package/dist/src/wgsl/bc-gather.wgsl.js.map +1 -0
  119. package/dist/src/wgsl/closeness-reduce.wgsl.d.ts +4 -1
  120. package/dist/src/wgsl/closeness-reduce.wgsl.d.ts.map +1 -1
  121. package/dist/src/wgsl/closeness-reduce.wgsl.js +8 -4
  122. package/dist/src/wgsl/closeness-reduce.wgsl.js.map +1 -1
  123. package/dist/src/wgsl/closeness-sweep.wgsl.d.ts +4 -2
  124. package/dist/src/wgsl/closeness-sweep.wgsl.d.ts.map +1 -1
  125. package/dist/src/wgsl/closeness-sweep.wgsl.js +12 -2
  126. package/dist/src/wgsl/closeness-sweep.wgsl.js.map +1 -1
  127. package/dist/src/wgsl/coo-emit.wgsl.d.ts +10 -0
  128. package/dist/src/wgsl/coo-emit.wgsl.d.ts.map +1 -0
  129. package/dist/src/wgsl/coo-emit.wgsl.js +33 -0
  130. package/dist/src/wgsl/coo-emit.wgsl.js.map +1 -0
  131. package/dist/src/wgsl/coo-scatter.wgsl.d.ts +15 -0
  132. package/dist/src/wgsl/coo-scatter.wgsl.d.ts.map +1 -0
  133. package/dist/src/wgsl/coo-scatter.wgsl.js +32 -0
  134. package/dist/src/wgsl/coo-scatter.wgsl.js.map +1 -0
  135. package/dist/src/wgsl/group-by-key-row.wgsl.d.ts +26 -0
  136. package/dist/src/wgsl/group-by-key-row.wgsl.d.ts.map +1 -0
  137. package/dist/src/wgsl/group-by-key-row.wgsl.js +146 -0
  138. package/dist/src/wgsl/group-by-key-row.wgsl.js.map +1 -0
  139. package/dist/src/wgsl/lpa-step.wgsl.d.ts +10 -0
  140. package/dist/src/wgsl/lpa-step.wgsl.d.ts.map +1 -0
  141. package/dist/src/wgsl/lpa-step.wgsl.js +35 -0
  142. package/dist/src/wgsl/lpa-step.wgsl.js.map +1 -0
  143. package/dist/src/wgsl/orient-flags.wgsl.d.ts +9 -0
  144. package/dist/src/wgsl/orient-flags.wgsl.d.ts.map +1 -0
  145. package/dist/src/wgsl/orient-flags.wgsl.js +21 -0
  146. package/dist/src/wgsl/orient-flags.wgsl.js.map +1 -0
  147. package/dist/src/wgsl/run-flags.wgsl.d.ts +8 -0
  148. package/dist/src/wgsl/run-flags.wgsl.d.ts.map +1 -0
  149. package/dist/src/wgsl/run-flags.wgsl.js +18 -0
  150. package/dist/src/wgsl/run-flags.wgsl.js.map +1 -0
  151. package/dist/src/wgsl/tri-intersect.wgsl.d.ts +11 -0
  152. package/dist/src/wgsl/tri-intersect.wgsl.d.ts.map +1 -0
  153. package/dist/src/wgsl/tri-intersect.wgsl.js +64 -0
  154. package/dist/src/wgsl/tri-intersect.wgsl.js.map +1 -0
  155. package/dist/webgpu-graph-algorithms.js +2828 -321
  156. package/dist/webgpu-graph-algorithms.js.map +1 -1
  157. package/package.json +5 -5
  158. package/src/accelerator.ts +130 -7
  159. package/src/algorithms/all-pairs.ts +228 -0
  160. package/src/algorithms/betweenness.ts +739 -0
  161. package/src/algorithms/closeness.ts +124 -32
  162. package/src/algorithms/components.ts +2 -2
  163. package/src/algorithms/label-propagation.ts +280 -0
  164. package/src/algorithms/simple-symmetric.ts +409 -0
  165. package/src/algorithms/triangles.ts +240 -0
  166. package/src/constants.ts +53 -0
  167. package/src/index.ts +20 -1
  168. package/src/kernel/prelude.ts +6 -0
  169. package/src/kernels.ts +411 -10
  170. package/src/memory/residency.ts +15 -4
  171. package/src/primitives/coo-to-csr.ts +251 -0
  172. package/src/primitives/frontier.ts +4 -0
  173. package/src/primitives/group-by-key.ts +209 -0
  174. package/src/types/accelerator.ts +26 -6
  175. package/src/types/algorithms.ts +5 -0
  176. package/src/types/all-pairs.ts +37 -0
  177. package/src/types/betweenness.ts +38 -0
  178. package/src/types/community.ts +18 -0
  179. package/src/types/structure.ts +28 -0
  180. package/src/wgsl/apsp-fw.wgsl.ts +112 -0
  181. package/src/wgsl/apsp-init.wgsl.ts +25 -0
  182. package/src/wgsl/bc-backward.wgsl.ts +33 -0
  183. package/src/wgsl/bc-edge-gather.wgsl.ts +35 -0
  184. package/src/wgsl/bc-finalize.wgsl.ts +46 -0
  185. package/src/wgsl/bc-forward-edge.wgsl.ts +75 -0
  186. package/src/wgsl/bc-forward.wgsl.ts +105 -0
  187. package/src/wgsl/bc-gather.wgsl.ts +19 -0
  188. package/src/wgsl/closeness-reduce.wgsl.ts +8 -4
  189. package/src/wgsl/closeness-sweep.wgsl.ts +12 -2
  190. package/src/wgsl/coo-emit.wgsl.ts +32 -0
  191. package/src/wgsl/coo-scatter.wgsl.ts +31 -0
  192. package/src/wgsl/group-by-key-row.wgsl.ts +145 -0
  193. package/src/wgsl/lpa-step.wgsl.ts +34 -0
  194. package/src/wgsl/orient-flags.wgsl.ts +20 -0
  195. package/src/wgsl/run-flags.wgsl.ts +17 -0
  196. package/src/wgsl/tri-intersect.wgsl.ts +63 -0
  197. package/dist/chunks/context-Bi6AhScG.js.map +0 -1
@@ -0,0 +1,28 @@
1
+ /**
2
+ * The result record of triangle counting (design 3.3 line 828, 8.5; design 17 line 5067). Design 3.3 declares
3
+ * `{ perNode, total }`; this package also returns the clustering coefficient and the transitivity, because they are
4
+ * an epilogue over the counts and degrees the call already holds and nothing else in the monorepo computes them.
5
+ * Types only.
6
+ */
7
+
8
+ import type { F32, U32 } from "@graphty/graph-format";
9
+
10
+ /**
11
+ * Triangle counting over the simple undirected graph underlying the snapshot: arc directions are ignored, parallel
12
+ * edges count once and self-loops not at all, so a directed snapshot and its undirected twin give the same answer.
13
+ * @public
14
+ */
15
+ export interface GpuTriangleResult {
16
+ /** The triangles each node lies in. */
17
+ readonly perNode: U32;
18
+ /** The triangles of the graph (each counted once). */
19
+ readonly total: number;
20
+ /**
21
+ * The local clustering coefficient of every node, `2 T(v) / (d(v) (d(v) - 1))` with `d` the node's number of
22
+ * distinct neighbours. It is 0 -- a defined value, not a missing one -- for a node with fewer than two
23
+ * neighbours, so a star graph returns all zeros.
24
+ */
25
+ readonly coefficient: F32;
26
+ /** The graph's transitivity, `3 x triangles / connected triples`; 0 when the graph has no connected triple. */
27
+ readonly transitivity: number;
28
+ }
@@ -0,0 +1,112 @@
1
+ /**
2
+ * The `apsp-fw` kernel body (design 8.7): one phase of one round of blocked Floyd-Warshall over `APSP_TILE x
3
+ * APSP_TILE` blocks of the row-major `n x n` matrix `dist`. Round `r` (`P.round`) runs three dispatches in order,
4
+ * the `PHASE` override choosing which:
5
+ *
6
+ * - PHASE 0, one workgroup: the pivot block `(r, r)`, staged in `tileA` and updated in place.
7
+ * - PHASE 1, `2 (B - 1)` workgroups (`B` = `P.blocks`): every other block of block row `r` (the first `B - 1`
8
+ * workgroups) and of block column `r` (the rest). The pivot block is staged in `tileA`, the block itself in
9
+ * `tileB`, updated in place.
10
+ * - PHASE 2, `(B - 1)^2` workgroups: every block `(i, j)` off the pivot row and column. Its two operands are the
11
+ * pivot-COLUMN block `(i, r)` in `tileA` and the pivot-ROW block `(r, j)` in `tileB`; its own cells are read,
12
+ * minimised over the 32 steps and written by one lane each, never shared, so they stay in registers.
13
+ *
14
+ * In-place updates are race-free because a cell is written only when the candidate is STRICTLY smaller: the cells
15
+ * every lane reads in step `k` (column `k` and row `k` of the tile) would be updated with `d + d[k][k]`, and the
16
+ * diagonal is `0` (weights are non-negative, which the driver checks) or `+Infinity` outside the matrix, so they are
17
+ * never written in step `k`. Every barrier is reached in uniform control flow: the early return keys on the workgroup
18
+ * id and uniforms only, the per-lane loops hold no barrier, and the lanes of an edge tile load `+Infinity` for a cell
19
+ * outside `n x n` (it offers no path; read from `P.infBits` because Tint refuses `+Infinity` as a constant) and
20
+ * store nothing there -- the matrix is exactly `n * n`, never padded, so the
21
+ * ceiling is `floor(sqrt(maxStorageBufferBindingSize / 4))`. Body only (spec 3.5, D9); the text is normative: the
22
+ * sabotage rows of test/helpers/sabotage.ts are textual edits of it.
23
+ */
24
+ export const apspFwWgsl = /* wgsl */ `
25
+ var<workgroup> tileA: array<f32, APSP_TILE * APSP_TILE>;
26
+ var<workgroup> tileB: array<f32, APSP_TILE * APSP_TILE>;
27
+
28
+ fn tile_row(block: vec2<u32>, c: u32) -> u32 { return block.x * APSP_TILE + c / APSP_TILE; }
29
+ fn tile_col(block: vec2<u32>, c: u32) -> u32 { return block.y * APSP_TILE + c % APSP_TILE; }
30
+
31
+ fn load_cell(block: vec2<u32>, c: u32) -> f32 {
32
+ let i = tile_row(block, c);
33
+ let j = tile_col(block, c);
34
+ if (i < P.n && j < P.n) { return dist[i * P.n + j]; }
35
+ return bitcast<f32>(P.infBits); // outside the matrix: no path through it
36
+ }
37
+
38
+ fn store_cell(block: vec2<u32>, c: u32, v: f32) {
39
+ let i = tile_row(block, c);
40
+ let j = tile_col(block, c);
41
+ if (i >= P.n || j >= P.n) { return; } // an edge tile stores nothing outside n x n
42
+ dist[i * P.n + j] = v;
43
+ }
44
+
45
+ @compute @workgroup_size(WG)
46
+ fn apsp_fw(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
47
+ let g = group_id(wid);
48
+ let r = P.round;
49
+ let others = max(P.blocks, 2u) - 1u; // the blocks of a strip, the pivot excluded
50
+ let cells = APSP_TILE * APSP_TILE;
51
+ var count = 1u;
52
+ var own = vec2<u32>(r, r); // the block this workgroup updates
53
+ var a = vec2<u32>(r, r); // staged in tileA
54
+ var b = vec2<u32>(r, r); // staged in tileB
55
+ if (PHASE == 1u) {
56
+ count = 2u * others;
57
+ let s = g % others;
58
+ let o = select(s, s + 1u, s >= r); // a strip skips the pivot block
59
+ own = select(vec2<u32>(o, r), vec2<u32>(r, o), g < others); // block row r first, then block column r
60
+ b = own;
61
+ }
62
+ if (PHASE == 2u) {
63
+ count = others * others;
64
+ let i = g / others;
65
+ let j = g % others;
66
+ own = vec2<u32>(select(i, i + 1u, i >= r), select(j, j + 1u, j >= r));
67
+ a = vec2<u32>(own.x, r); // the pivot-column block (i, r)
68
+ b = vec2<u32>(r, own.y); // the pivot-row block (r, j)
69
+ }
70
+ if (g >= count) { return; } // uniform: the workgroup id and uniforms only
71
+
72
+ for (var c = lid.x; c < cells; c = c + WG) {
73
+ tileA[c] = load_cell(a, c);
74
+ if (PHASE != 0u) { tileB[c] = load_cell(b, c); }
75
+ }
76
+ workgroupBarrier(); // every staged cell is visible
77
+
78
+ if (PHASE == 2u) {
79
+ for (var c = lid.x; c < cells; c = c + WG) {
80
+ let x = c / APSP_TILE;
81
+ let y = c % APSP_TILE;
82
+ var v = load_cell(own, c);
83
+ for (var kr = 0u; kr < APSP_TILE; kr = kr + 1u) {
84
+ v = min(v, tileA[x * APSP_TILE + kr] + tileB[kr * APSP_TILE + y]);
85
+ }
86
+ store_cell(own, c, v);
87
+ }
88
+ return;
89
+ }
90
+
91
+ let pivotRow = PHASE == 1u && own.x == r; // block row r reads d[x][k] from the pivot
92
+ for (var k = 0u; k < APSP_TILE; k = k + 1u) {
93
+ for (var c = lid.x; c < cells; c = c + WG) {
94
+ let x = c / APSP_TILE;
95
+ let y = c % APSP_TILE;
96
+ if (PHASE == 0u) {
97
+ let via = tileA[x * APSP_TILE + k] + tileA[k * APSP_TILE + y];
98
+ if (via < tileA[c]) { tileA[c] = via; }
99
+ } else {
100
+ let left = select(tileB[x * APSP_TILE + k], tileA[x * APSP_TILE + k], pivotRow);
101
+ let right = select(tileA[k * APSP_TILE + y], tileB[k * APSP_TILE + y], pivotRow);
102
+ let via = left + right;
103
+ if (via < tileB[c]) { tileB[c] = via; }
104
+ }
105
+ }
106
+ workgroupBarrier(); // step k is complete before step k + 1 reads
107
+ }
108
+ for (var c = lid.x; c < cells; c = c + WG) {
109
+ store_cell(own, c, select(tileB[c], tileA[c], PHASE == 0u));
110
+ }
111
+ }
112
+ `;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * The `apsp-init` kernel body (design 8.7): writes the arcs into the `n x n` distance matrix after `fill` has set
3
+ * every entry to `+Infinity`. One lane per ROW `u`: it walks `u`'s arcs and keeps the cheapest weight per target
4
+ * (`min`), so parallel arcs collapse to the cheapest one, then writes the diagonal zero LAST, so a self-loop never
5
+ * displaces it. Every arc of row `u` lands in row `u` of the matrix and only this lane writes that row, so there is
6
+ * no race and no atomic. Unweighted (`HAS_WEIGHTS` false) every arc costs 1. A directed snapshot writes its one
7
+ * direction; an undirected one stores both arcs of every edge, so both halves are written. The driver refuses
8
+ * negative and non-finite weights before any dispatch. Body only (spec 3.5, D9); the text is normative: the
9
+ * sabotage rows of test/helpers/sabotage.ts are textual edits of it.
10
+ */
11
+ export const apspInitWgsl = /* wgsl */ `
12
+ @compute @workgroup_size(WG)
13
+ fn apsp_init(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
14
+ let u = linear_id(wid, lid.x);
15
+ if (u >= P.n) { return; }
16
+ let rowBase = u * P.n;
17
+ let end = rowPtr[u + 1u];
18
+ for (var a = rowPtr[u]; a < end; a = a + 1u) {
19
+ let v = colIdx[a];
20
+ let w = select(1.0, weights[a], HAS_WEIGHTS);
21
+ dist[rowBase + v] = min(dist[rowBase + v], w); // parallel arcs collapse to the cheapest
22
+ }
23
+ dist[rowBase + u] = 0.0; // last: a self-loop never displaces the zero
24
+ }
25
+ `;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * The `bc-backward` kernel body (design 8.4 "each (w, s) PULLS over its successors", 8.10 "BC backward (successor
3
+ * pull)"): one level of a betweenness batch's dependency accumulation, one invocation per log entry `(w, s)` of the
4
+ * level's range `S[P.start .. P.start + P.count)`, which the host planned from the `ends` it read back. Each entry
5
+ * walks the out-arcs of `w` -- the rows the forward pass expanded -- and sums `sigma[s][w] / sigma[s][v] * (1 +
6
+ * delta[s][v])` over the successors `v` (`depth[s][v] == depth[s][w] + 1`), whose dependencies the previous (deeper)
7
+ * dispatch wrote, then writes `delta[s][w]` ONCE. No float is ever accumulated through an atomic, so the result is
8
+ * bitwise reproducible. The sources (depth 0) are never dispatched: their dependency stays 0, which is what keeps a
9
+ * source's own dependency out of its score. Grid-stride loop (`P.stride`). ponytail: one lane walks one row, so a
10
+ * vertex of degree above 65,535 exceeds llvmpipe's per-invocation loop cap (CLAUDE.md, Verified Platform Facts) and
11
+ * would need a workgroup-per-row form there; hardware adapters have no such cap. Body only; the sabotage rows of
12
+ * test/helpers/sabotage.ts are textual edits of it.
13
+ */
14
+ export const bcBackwardWgsl = /* wgsl */ `
15
+ @compute @workgroup_size(WG)
16
+ fn bc_backward(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
17
+ for (var i = linear_id(wid, lid.x); i < P.count; i = i + P.stride) {
18
+ let t = S[P.start + i]; // s * n + w
19
+ let w = t % P.n;
20
+ let base = t - w; // s * n
21
+ let succ = depthK[t] + 1u;
22
+ let sw = f32(sigmaK[t]);
23
+ var acc = 0.0;
24
+ for (var a = rowPtr[w]; a < rowPtr[w + 1u]; a = a + 1u) {
25
+ let v = base + colIdx[a];
26
+ if (depthK[v] == succ) { // v is a successor of w for source s
27
+ acc = acc + (sw / f32(sigmaK[v])) * (1.0 + deltaK[v]);
28
+ }
29
+ }
30
+ deltaK[t] = acc; // written once per (w, s)
31
+ }
32
+ }
33
+ `;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * The `bc-edge-gather` kernel body (design 8.4 "edge BC accumulates per arc from the same n x k deltas"): the per-arc
3
+ * twin of `bc-gather`, run once per batch after the backward sweep. One invocation per ARC (`P.count` arcs,
4
+ * grid-stride): it finds the row `w` that owns the arc by an upper-bound search over `rowPtr`, then adds over the
5
+ * batch's sources in order the term the backward pass summed -- `sigma[s][w] / sigma[s][v] * (1 + delta[s][v])`
6
+ * whenever `w` was reached and `depth[s][v] == depth[s][w] + 1` -- into `arcScores[arc]`. Each arc is written by one
7
+ * invocation: no atomic, a fixed order. Arc-parallel rather than row-parallel so a hub row is not one lane's loop
8
+ * (llvmpipe caps a shader loop at 65,535 iterations, and a 1,000-arc hub times 64 sources passed it). Body only;
9
+ * the sabotage rows of test/helpers/sabotage.ts are textual edits of it.
10
+ */
11
+ export const bcEdgeGatherWgsl = /* wgsl */ `
12
+ @compute @workgroup_size(WG)
13
+ fn bc_edge_gather(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
14
+ for (var a = linear_id(wid, lid.x); a < P.count; a = a + P.stride) {
15
+ var lo = 0u; // the row w with rowPtr[w] <= a < rowPtr[w + 1]
16
+ var hi = P.n;
17
+ loop {
18
+ if (lo >= hi) { break; }
19
+ let mid = (lo + hi) / 2u;
20
+ if (rowPtr[mid + 1u] <= a) { lo = mid + 1u; } else { hi = mid; }
21
+ }
22
+ let w = lo;
23
+ let nbr = colIdx[a];
24
+ var acc = arcScores[a];
25
+ for (var s = 0u; s < P.k; s = s + 1u) {
26
+ let base = s * P.n;
27
+ let dw = depthK[base + w];
28
+ if (dw != INVALID_INDEX && depthK[base + nbr] == dw + 1u) { // (w, nbr) is on a shortest path from s
29
+ acc = acc + (f32(sigmaK[base + w]) / f32(sigmaK[base + nbr])) * (1.0 + deltaK[base + nbr]);
30
+ }
31
+ }
32
+ arcScores[a] = acc;
33
+ }
34
+ }
35
+ `;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * The `bc-finalize` kernel body (design 8.4, 5.4): the one-lane bookkeeping of a betweenness source batch, two roles
3
+ * by `P.role`. The batch keeps ONE append-only claim log `S` -- every `(vertex, source)` pair the forward pass
4
+ * claims, packed as the index `s * n + v` into the `n x k` arrays -- and `ends`, the level boundaries into it: the
5
+ * entries at depth `L` are `S[ends[L] .. ends[L + 1])`. That log is Brandes' stack, so the backward pass walks the
6
+ * same ranges from the deepest level up and nothing is ever copied between levels.
7
+ *
8
+ * Role 1 seeds the batch: the k seed entries the host wrote into `S[0 .. k)` get depth 0 and one shortest path,
9
+ * `ends[0] = 0`, the append cursor `stackTop` (counters word 26) starts at k, the overflow flag (word 27) is cleared,
10
+ * `level` (word 11) is U32_MAX so the first boundary lands on 0, and `done` (word 15) is cleared.
11
+ *
12
+ * Role 0 is the level boundary, recorded before every forward level: it advances `level`, closes the level just
13
+ * claimed by writing `ends[level + 1] = stackTop`, publishes the new level's size in `frontierCount` (word 0) and sets
14
+ * `done` when that level is empty. A boundary that finds `done` set moves nothing, so the levels the host records
15
+ * past the end are no-ops. The forward kernels dispatch directly and read their range from `ends` (no indirect
16
+ * dispatch: design/decisions/2026-09-25-frontier-kernels-dispatch-directly.md). One lane; no barrier follows the
17
+ * early return of the others (spec 3.5 rule 1). Body only; the sabotage rows of test/helpers/sabotage.ts are
18
+ * textual edits of it.
19
+ */
20
+ export const bcFinalizeWgsl = /* wgsl */ `
21
+ @compute @workgroup_size(WG)
22
+ fn bc_finalize(@builtin(local_invocation_id) lid: vec3<u32>) {
23
+ if (lid.x != 0u) { return; } // one lane; no barrier follows (3.5 rule 1)
24
+ if (P.role == 1u) { // the seed of a batch
25
+ for (var i = 0u; i < P.k; i = i + 1u) {
26
+ let t = S[i];
27
+ depthK[t] = 0u; // the source is at depth 0
28
+ sigmaK[t] = 1u; // with one shortest path, itself
29
+ }
30
+ ends[0] = 0u;
31
+ atomicStore(&counters[26], P.k); // stackTop: the seeds are the log's first k entries
32
+ atomicStore(&counters[27], 0u); // sigmaOverflow
33
+ atomicStore(&counters[11], U32_MAX); // level: the first boundary brings it to 0
34
+ atomicStore(&counters[15], 0u); // done
35
+ return;
36
+ }
37
+ if (atomicLoad(&counters[15]) != 0u) { return; } // done: a no-op level the host recorded past the end
38
+ let level = atomicLoad(&counters[11]) + 1u;
39
+ let top = atomicLoad(&counters[26]);
40
+ ends[level + 1u] = top; // the level's entries end where the log ends now
41
+ let count = top - ends[level];
42
+ atomicStore(&counters[0], count); // frontierCount (the inspect seam reads it)
43
+ atomicStore(&counters[11], level);
44
+ atomicStore(&counters[15], select(0u, 1u, count == 0u)); // an empty level ends the batch
45
+ }
46
+ `;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * The `bc-forward-edge` kernel body (design 8.4 "the McLaughlin-Bader online switch to the edge-parallel form", 8.8
3
+ * row 7): one level of a betweenness batch's forward pass, edge-parallel -- every logical edge of the `edgeList` view
4
+ * for every source of the batch, grid-striding over the edges inside a loop over the k sources. For the edge `(u, x)`
5
+ * and source `s`, when `depth[s][u]` is the level the edge is relaxed toward `x` with EXACTLY the claim and the count
6
+ * of `bc-forward` (the pre-check, `atomicMin`, the winner appends `s * n + x` to the same claim log, every arc on a
7
+ * shortest path adds `sigma[s][u]`, a u32 wrap raises word 27); `UNDIRECTED` relaxes the other direction too, the
8
+ * edge list holding each undirected edge once. So the two forward bodies are interchangeable level by level and the
9
+ * backward pass cannot tell which ran. A workgroup does nothing when the level is empty (the done boundary). The
10
+ * winners of a strip are packed into the log with one global `atomicAdd` per strip; every barrier is in uniform
11
+ * control flow (the loop bounds are uniforms and the workgroup id). Body only; the sabotage rows of
12
+ * test/helpers/sabotage.ts are textual edits of it.
13
+ */
14
+ export const bcForwardEdgeWgsl = /* wgsl */ `
15
+ var<workgroup> wlive: u32; // 1 when the level has entries
16
+ var<workgroup> wwon: atomic<u32>; // the strip's winners
17
+ var<workgroup> wbase: u32; // where the strip's winners go in the log
18
+
19
+ fn claim(x: u32, next: u32) -> bool {
20
+ if (atomicLoad(&depthK[x]) != INVALID_INDEX) { return false; } // the pre-check of design 16.1
21
+ return atomicMin(&depthK[x], next) == INVALID_INDEX;
22
+ }
23
+
24
+ fn count_paths(origin: u32, x: u32, next: u32) {
25
+ if (atomicLoad(&depthK[x]) == next) { // every arc on a shortest path adds
26
+ let add = atomicLoad(&sigmaK[origin]);
27
+ let old = atomicAdd(&sigmaK[x], add);
28
+ if (old + add < old) { atomicOr(&counters[27], 1u); } // the u32 wrap, reported
29
+ }
30
+ }
31
+
32
+ @compute @workgroup_size(WG)
33
+ fn bc_forward_edge(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
34
+ let level = atomicLoad(&counters[11]);
35
+ if (lid.x == 0u) { wlive = select(0u, 1u, ends[level + 1u] > ends[level]); }
36
+ if (workgroupUniformLoad(&wlive) == 0u) { return; } // uniform: nothing below runs on an empty level
37
+ let next = level + 1u;
38
+ for (var s = 0u; s < P.k; s = s + 1u) {
39
+ let base = s * P.n;
40
+ for (var e0 = group_id(wid) * WG; e0 < P.count; e0 = e0 + P.stride) { // grid-stride over the edges
41
+ let e = e0 + lid.x;
42
+ var a = INVALID_INDEX; // the claims this lane won
43
+ var b = INVALID_INDEX;
44
+ if (e < P.count) {
45
+ let u = base + edgeSrc[e];
46
+ let x = base + edgeDst[e];
47
+ if (atomicLoad(&depthK[u]) == level) {
48
+ if (claim(x, next)) { a = x; }
49
+ count_paths(u, x, next);
50
+ }
51
+ if (UNDIRECTED) { // the other direction of an undirected edge
52
+ if (atomicLoad(&depthK[x]) == level) {
53
+ if (claim(u, next)) { b = u; }
54
+ count_paths(x, u, next);
55
+ }
56
+ }
57
+ }
58
+ let mine = select(0u, 1u, a != INVALID_INDEX) + select(0u, 1u, b != INVALID_INDEX);
59
+ var slot = 0u;
60
+ if (mine != 0u) { slot = atomicAdd(&wwon, mine); }
61
+ workgroupBarrier();
62
+ if (lid.x == 0u) {
63
+ wbase = atomicAdd(&counters[26], atomicLoad(&wwon)); // stackTop: one global atomic per strip
64
+ atomicStore(&wwon, 0u);
65
+ }
66
+ workgroupBarrier();
67
+ if (a != INVALID_INDEX) {
68
+ S[wbase + slot] = a;
69
+ slot = slot + 1u;
70
+ }
71
+ if (b != INVALID_INDEX) { S[wbase + slot] = b; }
72
+ }
73
+ }
74
+ }
75
+ `;
@@ -0,0 +1,105 @@
1
+ /**
2
+ * The `bc-forward` kernel body (design 8.4 "forward pass = BFS with sigma as array<atomic<u32>>", 8.10 "BC forward
3
+ * (tagged)", 16.1): one level of the tagged multi-source breadth-first search of a betweenness batch. The level's
4
+ * frontier is the range `S[ends[level] .. ends[level + 1])` of the claim log, every entry a packed `s * n + u`; the
5
+ * expansion is `closeness-sweep`'s block-mapped strip (each workgroup loads up to `WG` entries, scans their degrees
6
+ * in workgroup memory, and every lane strips the aggregate by an upper-bound binary search), fused with the claim, so
7
+ * no edge queue exists.
8
+ *
9
+ * For the arc `(u, x)` of the entry `(u, s)`, with `t = s * n + x`: the CLAIM -- a relaxed pre-check that skips the
10
+ * atomic when `t` is already claimed (design 16.1: a stale "unclaimed" costs one redundant atomic, a stale "claimed"
11
+ * cannot happen because a claim is never revoked), then `atomicMin(&depthK[t], level + 1)`, the invocation that
12
+ * observes INVALID_INDEX the unique winner, which appends `t` to the log; and, as a SEPARATE condition, the COUNT:
13
+ * every arc that reaches `t` at `level + 1` adds `sigma[s][u]` into `sigma[s][x]`, winner or not, which is what makes
14
+ * sigma the number of shortest paths rather than of claims. The add detects a u32 wrap from `atomicAdd`'s return
15
+ * value (`old + add < old`) and raises `sigmaOverflow` (counters word 27); the count is never clamped. The winners of
16
+ * a strip are packed into the log with one workgroup-memory counter and ONE global `atomicAdd` on `stackTop` (word
17
+ * 26) per strip. Uniformity (spec 3.5 rule 1): the range and the aggregate are `workgroupUniformLoad`s and the strip
18
+ * loop steps a uniform `p0`, so every barrier is in uniform control flow. The order of the log inside a level is
19
+ * not deterministic; nothing downstream depends on it (the counts are integers and every dependency is written by
20
+ * index). Body only; the sabotage rows of test/helpers/sabotage.ts are textual edits of it.
21
+ */
22
+ export const bcForwardWgsl = /* wgsl */ `
23
+ var<workgroup> sh: array<u32, WG>; // the block's degrees, then their inclusive scan
24
+ var<workgroup> rowStart: array<u32, WG>; // the first arc of each entry's row
25
+ var<workgroup> entryOf: array<u32, WG>; // each entry, s * n + u
26
+ var<workgroup> wstart: u32; // the level's first log index
27
+ var<workgroup> wcount: u32; // the level's entry count
28
+ var<workgroup> wwon: atomic<u32>; // the strip's winners
29
+ var<workgroup> wbase: u32; // where the strip's winners go in the log
30
+
31
+ @compute @workgroup_size(WG)
32
+ fn bc_forward(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
33
+ let level = atomicLoad(&counters[11]);
34
+ if (lid.x == 0u) {
35
+ let lo = ends[level];
36
+ wstart = lo;
37
+ wcount = ends[level + 1u] - lo;
38
+ }
39
+ let start = workgroupUniformLoad(&wstart);
40
+ let count = workgroupUniformLoad(&wcount); // uniform: the block loop below holds barriers
41
+ let next = level + 1u;
42
+ for (var b0 = group_id(wid) * WG; b0 < count; b0 = b0 + P.stride) { // grid-stride over blocks of WG entries
43
+ let i = b0 + lid.x;
44
+ var deg = 0u;
45
+ var first = 0u;
46
+ var entry = 0u;
47
+ if (i < count) { // guarded loads into locals (3.5 rule 1)
48
+ entry = S[start + i];
49
+ let u = entry % P.n;
50
+ first = rowPtr[u];
51
+ deg = rowPtr[u + 1u] - first;
52
+ }
53
+ sh[lid.x] = deg;
54
+ rowStart[lid.x] = first;
55
+ entryOf[lid.x] = entry;
56
+ workgroupBarrier();
57
+ for (var s = 1u; s < WG; s = s * 2u) { // Hillis-Steele inclusive scan of the degrees
58
+ var t = 0u;
59
+ if (lid.x >= s) { t = sh[lid.x - s]; }
60
+ workgroupBarrier();
61
+ sh[lid.x] = sh[lid.x] + t;
62
+ workgroupBarrier();
63
+ }
64
+ let aggregate = workgroupUniformLoad(&sh[WG - 1u]); // uniform; includes a barrier
65
+ for (var p0 = 0u; p0 < aggregate; p0 = p0 + WG) { // strip [0, aggregate) WG arcs at a time
66
+ let p = p0 + lid.x;
67
+ var won = false;
68
+ var claimed = 0u;
69
+ if (p < aggregate) {
70
+ var lo = 0u; // upper_bound: the first k with sh[k] > p owns arc p
71
+ var hi = WG;
72
+ loop {
73
+ if (lo >= hi) { break; }
74
+ let mid = (lo + hi) / 2u;
75
+ if (sh[mid] > p) { hi = mid; } else { lo = mid + 1u; }
76
+ }
77
+ let k = lo;
78
+ var exclusive = 0u;
79
+ if (k > 0u) { exclusive = sh[k - 1u]; }
80
+ let origin = entryOf[k]; // s * n + u
81
+ let x = (origin - (origin % P.n)) + colIdx[rowStart[k] + (p - exclusive)]; // s * n + x
82
+ if (atomicLoad(&depthK[x]) == INVALID_INDEX) { // the pre-check of design 16.1
83
+ won = atomicMin(&depthK[x], next) == INVALID_INDEX; // the claim: the one winner appends
84
+ }
85
+ if (atomicLoad(&depthK[x]) == next) { // the count: EVERY arc on a shortest path adds
86
+ let add = atomicLoad(&sigmaK[origin]);
87
+ let old = atomicAdd(&sigmaK[x], add);
88
+ if (old + add < old) { atomicOr(&counters[27], 1u); } // the u32 wrap, reported
89
+ }
90
+ claimed = x;
91
+ }
92
+ var slot = 0u;
93
+ if (won) { slot = atomicAdd(&wwon, 1u); }
94
+ workgroupBarrier();
95
+ if (lid.x == 0u) {
96
+ wbase = atomicAdd(&counters[26], atomicLoad(&wwon)); // stackTop: one global atomic per strip
97
+ atomicStore(&wwon, 0u);
98
+ }
99
+ workgroupBarrier();
100
+ if (won) { S[wbase + slot] = claimed; }
101
+ }
102
+ workgroupBarrier(); // sh, rowStart and entryOf are reused by the next block
103
+ }
104
+ }
105
+ `;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The `bc-gather` kernel body (design 8.4 "one gather kernel after the batch's last backward level", 8.10 "BC
3
+ * gather"): one invocation per vertex `w`, `bc[w] += delta[0][w] + ... + delta[k - 1][w]` in source order -- k reads,
4
+ * one write, no atomic, a fixed summation order, so the scores are bitwise reproducible across runs and adapters.
5
+ * A source's own dependency is 0 (the backward pass never visits depth 0), so nothing is skipped here. Grid-stride
6
+ * loop (`P.stride`). Body only; the sabotage rows of test/helpers/sabotage.ts are textual edits of it.
7
+ */
8
+ export const bcGatherWgsl = /* wgsl */ `
9
+ @compute @workgroup_size(WG)
10
+ fn bc_gather(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
11
+ for (var w = linear_id(wid, lid.x); w < P.n; w = w + P.stride) {
12
+ var acc = bc[w];
13
+ for (var s = 0u; s < P.k; s = s + 1u) {
14
+ acc = acc + deltaK[s * P.n + w];
15
+ }
16
+ bc[w] = acc;
17
+ }
18
+ }
19
+ `;
@@ -10,6 +10,9 @@
10
10
  * level 0's parity is 0), `flags[source_s] = 1` (level 0's `compact` turns the flags into the list; a seeded list
11
11
  * would be overwritten by a compaction of all-zero flags), `counters[0] = k` (not done) and `level = U32_MAX` (so
12
12
  * level 0's boundary accumulates nothing and brings the word to 0, and level 1's counts the distance-1 claims at 1).
13
+ * Role 2 is role 1 for a sampled run: source `s` of the batch is word `P.source + s` of the source list the host
14
+ * wrote after the per-node sums (`perSource[128 + P.bitsBase + ...]`), `P.n` is the list's length, and a node listed
15
+ * twice in one batch carries both bits (the seed ORs, so a duplicate runs twice).
13
16
  * No barrier follows the early return of the other lanes (3.5 rule 1). Body only (spec 3.5, D9); the text is
14
17
  * normative: the sabotage rows of test/helpers/sabotage.ts are textual edits of it.
15
18
  */
@@ -17,13 +20,14 @@ export const closenessReduceWgsl = /* wgsl */ `
17
20
  @compute @workgroup_size(WG)
18
21
  fn closeness_reduce(@builtin(local_invocation_id) lid: vec3<u32>) {
19
22
  if (lid.x != 0u) { return; } // one lane; no barrier follows (3.5 rule 1)
20
- if (P.role == 1u) { // the seed of a batch: P.source is its first source
23
+ if (P.role != 0u) { // the seed of a batch: P.source is its first source
21
24
  let k = min(32u, P.n - P.source);
22
25
  for (var s = 0u; s < k; s = s + 1u) {
23
- let v = P.source + s;
26
+ var v = P.source + s;
27
+ if (P.role == 2u) { v = atomicLoad(&perSource[128u + P.bitsBase + P.source + s]); } // a sampled run's list
24
28
  let bit = 1u << s;
25
- bits[v] = bit; // visited
26
- bits[P.bitsBase + v] = bit; // the frontier level 0 reads (region 1: level 0's parity is 0)
29
+ bits[v] = bits[v] | bit; // visited
30
+ bits[P.bitsBase + v] = bits[P.bitsBase + v] | bit; // the frontier level 0 reads (region 1: level 0's parity is 0)
27
31
  bits[3u * P.bitsBase + v] = 1u; // flags: level 0's compact turns them into the list
28
32
  }
29
33
  atomicStore(&counters[0], k); // not done
@@ -13,7 +13,9 @@
13
13
  * error), and are tallied per source in WORKGROUP memory -- one global `atomicAdd` per source per workgroup after the
14
14
  * strip loop, never one per arc. Uniformity (spec 3.5 rule 1): the guarded loads write locals, the scan and the
15
15
  * `workgroupUniformLoad` sit unconditionally after the guard, the strip loop is bounded by the uniform aggregate, and
16
- * the flush's barrier follows it in uniform control flow. Body only (spec 3.5, D9); the text is normative: the
16
+ * the flush's barrier follows it in uniform control flow. A sampled run (`P.perNode == 1`) also adds each claim's
17
+ * distance (`level + 1`, one per won bit) into the per-node sum of `x`, `perSource[128 + x]`: the distance from each
18
+ * of the batch's sources TO `x`, which is what a node's sampled closeness sums on an undirected graph. Body only (spec 3.5, D9); the text is normative: the
17
19
  * sabotage rows of test/helpers/sabotage.ts are textual edits of it.
18
20
  */
19
21
  export const closenessSweepWgsl = /* wgsl */ `
@@ -22,11 +24,16 @@ var<workgroup> rowStart: array<u32, WG>; // the first bound arc of each
22
24
  var<workgroup> rowOf: array<u32, WG>; // the frontier vertex of each entry (the source end of its arcs)
23
25
  var<workgroup> local: array<atomic<u32>, 32>; // this workgroup's fresh claims per source
24
26
  var<workgroup> wcount: u32; // the frontier list's length
27
+ var<workgroup> wdist: u32; // the distance of this level's claims
25
28
 
26
29
  @compute @workgroup_size(WG)
27
30
  fn closeness_sweep(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
28
- if (lid.x == 0u) { wcount = atomicLoad(&counters[0]); } // the frontier list's length (compact's total)
31
+ if (lid.x == 0u) {
32
+ wcount = atomicLoad(&counters[0]); // the frontier list's length (compact's total)
33
+ wdist = atomicLoad(&counters[11]) + 1u; // the level word: this level claims at level + 1
34
+ }
29
35
  let count = workgroupUniformLoad(&wcount); // uniform: the block loop below holds barriers
36
+ let dist = workgroupUniformLoad(&wdist);
30
37
  let nextBase = select(2u * P.bitsBase, P.bitsBase, P.mode == 1u); // the region that is next this level
31
38
  let frontierBase = 3u * P.bitsBase - nextBase; // the other one: the region that is the frontier
32
39
  for (var b0 = group_id(wid) * WG; b0 < count; b0 = b0 + P.stride) { // grid-stride over blocks of WG entries
@@ -74,6 +81,9 @@ fn closeness_sweep(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocat
74
81
  if (fresh != 0u) {
75
82
  atomicOr(&bits[nextBase + x], fresh);
76
83
  atomicStore(&bits[3u * P.bitsBase + x], 1u); // flags: x is in the next frontier list (compact reads it)
84
+ if (P.perNode == 1u) { // a sampled run: x's distance to each source won
85
+ atomicAdd(&perSource[128u + x], countOneBits(fresh) * dist);
86
+ }
77
87
  var b = fresh;
78
88
  loop { // one tally per set bit of fresh
79
89
  if (b == 0u) { break; }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The `coo-emit` kernel body (design 6 row 10, 8.6; the first step of the simple symmetric graph build): position `i`
3
+ * takes arc `a` -- `i` itself, or `order[i]` under INDEXED, which is how a sorted permutation is materialised -- and
4
+ * writes that arc's source, target and weight. Arc `2e` is logical edge `e` as declared and arc `2e + 1` its reverse,
5
+ * so every edge lands in both directions and the result is symmetric whatever the snapshot's directedness. A
6
+ * self-loop is dropped: both of its arcs are written as `INVALID_INDEX`, which sorts after every node index and which
7
+ * `run-flags` never marks. Weights are 1 without WEIGHTED. Body only (spec 3.5, D9); normative text.
8
+ */
9
+ export const cooEmitWgsl = /* wgsl */ `
10
+ @compute @workgroup_size(WG)
11
+ fn coo_emit(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
12
+ let i = linear_id(wid, lid.x);
13
+ if (i >= P.count) { return; } // no barrier follows
14
+ var a = i;
15
+ if (INDEXED) { a = order[i]; } // the arc this position takes
16
+ let e = a / 2u; // arc 2e is edge e as declared, arc 2e + 1 its reverse
17
+ let forward = (a % 2u) == 0u;
18
+ let u = edgeSrc[e];
19
+ let v = edgeDst[e];
20
+ var w = 1.0;
21
+ if (WEIGHTED) { w = edgeWeight[e]; }
22
+ if (u == v) { // a self-loop is dropped: both arcs sort last and never open a run
23
+ outSrc[i] = INVALID_INDEX;
24
+ outDst[i] = INVALID_INDEX;
25
+ outWeight[i] = 0.0;
26
+ return;
27
+ }
28
+ outSrc[i] = select(v, u, forward);
29
+ outDst[i] = select(u, v, forward);
30
+ outWeight[i] = w;
31
+ }
32
+ `;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The `coo-scatter` kernel body (design 6 row 10): the last step of `cooToCsr`, after the histogram of the sources
3
+ * and its exclusive scan into `rowPtr`. Two modes under SORTED_INPUT.
4
+ *
5
+ * SORTED_INPUT false is the design's cursor scatter: an arc reserves its slot with `atomicAdd` on its row's cursor
6
+ * (`cursors`, zeroed by the caller), so slots are handed out in race order and a row is NOT sorted by target even when
7
+ * the input was.
8
+ *
9
+ * SORTED_INPUT true takes arcs already ordered by source: arc `i` then sits at its own index minus its row's start
10
+ * inside the row, no cursor and no atomic, so the write is a pure function of the input and the input order survives
11
+ * into every row. The precondition is checked, not trusted: an arc whose source is below its predecessor's raises
12
+ * `cursors[0]`, the word the driver reads back and refuses. Body only (spec 3.5, D9); normative text.
13
+ */
14
+ export const cooScatterWgsl = /* wgsl */ `
15
+ @compute @workgroup_size(WG)
16
+ fn coo_scatter(@builtin(workgroup_id) wid: vec3<u32>, @builtin(local_invocation_id) lid: vec3<u32>) {
17
+ let i = linear_id(wid, lid.x);
18
+ if (i >= P.count) { return; } // no barrier follows
19
+ let s = src[i];
20
+ var slot = 0u;
21
+ if (SORTED_INPUT) {
22
+ if (i > 0u && s < src[i - 1u]) { atomicStore(&cursors[0], 1u); } // unsorted input: the flag the driver refuses
23
+ let within = i - rowPtr[s]; // the arc's place in its row
24
+ slot = rowPtr[s] + within;
25
+ } else {
26
+ slot = rowPtr[s] + atomicAdd(&cursors[s], 1u); // race order: the row is not sorted in this mode
27
+ }
28
+ colIdx[slot] = dst[i];
29
+ if (WEIGHTED) { outWeight[slot] = weight[i]; }
30
+ }
31
+ `;