@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.
- package/README.md +52 -52
- package/dist/browser.js +1 -1
- package/dist/chunks/{context-Bi6AhScG.js → context-VIvatQOo.js} +69 -34
- package/dist/chunks/context-VIvatQOo.js.map +1 -0
- package/dist/node.js +1 -1
- package/dist/src/accelerator.d.ts +5 -3
- package/dist/src/accelerator.d.ts.map +1 -1
- package/dist/src/accelerator.js +101 -5
- package/dist/src/accelerator.js.map +1 -1
- package/dist/src/algorithms/all-pairs.d.ts +41 -0
- package/dist/src/algorithms/all-pairs.d.ts.map +1 -0
- package/dist/src/algorithms/all-pairs.js +181 -0
- package/dist/src/algorithms/all-pairs.js.map +1 -0
- package/dist/src/algorithms/betweenness.d.ts +70 -0
- package/dist/src/algorithms/betweenness.d.ts.map +1 -0
- package/dist/src/algorithms/betweenness.js +538 -0
- package/dist/src/algorithms/betweenness.js.map +1 -0
- package/dist/src/algorithms/closeness.d.ts +15 -5
- package/dist/src/algorithms/closeness.d.ts.map +1 -1
- package/dist/src/algorithms/closeness.js +112 -26
- package/dist/src/algorithms/closeness.js.map +1 -1
- package/dist/src/algorithms/components.d.ts +9 -1
- package/dist/src/algorithms/components.d.ts.map +1 -1
- package/dist/src/algorithms/components.js +2 -2
- package/dist/src/algorithms/components.js.map +1 -1
- package/dist/src/algorithms/label-propagation.d.ts +31 -0
- package/dist/src/algorithms/label-propagation.d.ts.map +1 -0
- package/dist/src/algorithms/label-propagation.js +254 -0
- package/dist/src/algorithms/label-propagation.js.map +1 -0
- package/dist/src/algorithms/simple-symmetric.d.ts +88 -0
- package/dist/src/algorithms/simple-symmetric.d.ts.map +1 -0
- package/dist/src/algorithms/simple-symmetric.js +347 -0
- package/dist/src/algorithms/simple-symmetric.js.map +1 -0
- package/dist/src/algorithms/triangles.d.ts +34 -0
- package/dist/src/algorithms/triangles.d.ts.map +1 -0
- package/dist/src/algorithms/triangles.js +203 -0
- package/dist/src/algorithms/triangles.js.map +1 -0
- package/dist/src/constants.d.ts +53 -0
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +53 -0
- package/dist/src/constants.js.map +1 -1
- package/dist/src/index.d.ts +12 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +8 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/kernel/prelude.d.ts.map +1 -1
- package/dist/src/kernel/prelude.js +4 -1
- package/dist/src/kernel/prelude.js.map +1 -1
- package/dist/src/kernels.d.ts +24 -6
- package/dist/src/kernels.d.ts.map +1 -1
- package/dist/src/kernels.js +373 -7
- package/dist/src/kernels.js.map +1 -1
- package/dist/src/memory/residency.js +15 -4
- package/dist/src/memory/residency.js.map +1 -1
- package/dist/src/primitives/coo-to-csr.d.ts +73 -0
- package/dist/src/primitives/coo-to-csr.d.ts.map +1 -0
- package/dist/src/primitives/coo-to-csr.js +183 -0
- package/dist/src/primitives/coo-to-csr.js.map +1 -0
- package/dist/src/primitives/frontier.d.ts +2 -0
- package/dist/src/primitives/frontier.d.ts.map +1 -1
- package/dist/src/primitives/frontier.js +2 -0
- package/dist/src/primitives/frontier.js.map +1 -1
- package/dist/src/primitives/group-by-key.d.ts +82 -0
- package/dist/src/primitives/group-by-key.d.ts.map +1 -0
- package/dist/src/primitives/group-by-key.js +147 -0
- package/dist/src/primitives/group-by-key.js.map +1 -0
- package/dist/src/types/accelerator.d.ts +19 -7
- package/dist/src/types/accelerator.d.ts.map +1 -1
- package/dist/src/types/algorithms.d.ts +4 -0
- package/dist/src/types/algorithms.d.ts.map +1 -1
- package/dist/src/types/all-pairs.d.ts +35 -0
- package/dist/src/types/all-pairs.d.ts.map +1 -0
- package/dist/src/types/all-pairs.js +8 -0
- package/dist/src/types/all-pairs.js.map +1 -0
- package/dist/src/types/betweenness.d.ts +35 -0
- package/dist/src/types/betweenness.d.ts.map +1 -0
- package/dist/src/types/betweenness.js +7 -0
- package/dist/src/types/betweenness.js.map +1 -0
- package/dist/src/types/community.d.ts +18 -0
- package/dist/src/types/community.d.ts.map +1 -0
- package/dist/src/types/community.js +5 -0
- package/dist/src/types/community.js.map +1 -0
- package/dist/src/types/structure.d.ts +27 -0
- package/dist/src/types/structure.d.ts.map +1 -0
- package/dist/src/types/structure.js +8 -0
- package/dist/src/types/structure.js.map +1 -0
- package/dist/src/wgsl/apsp-fw.wgsl.d.ts +25 -0
- package/dist/src/wgsl/apsp-fw.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/apsp-fw.wgsl.js +113 -0
- package/dist/src/wgsl/apsp-fw.wgsl.js.map +1 -0
- package/dist/src/wgsl/apsp-init.wgsl.d.ts +12 -0
- package/dist/src/wgsl/apsp-init.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/apsp-init.wgsl.js +26 -0
- package/dist/src/wgsl/apsp-init.wgsl.js.map +1 -0
- package/dist/src/wgsl/bc-backward.wgsl.d.ts +15 -0
- package/dist/src/wgsl/bc-backward.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/bc-backward.wgsl.js +34 -0
- package/dist/src/wgsl/bc-backward.wgsl.js.map +1 -0
- package/dist/src/wgsl/bc-edge-gather.wgsl.d.ts +12 -0
- package/dist/src/wgsl/bc-edge-gather.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/bc-edge-gather.wgsl.js +36 -0
- package/dist/src/wgsl/bc-edge-gather.wgsl.js.map +1 -0
- package/dist/src/wgsl/bc-finalize.wgsl.d.ts +21 -0
- package/dist/src/wgsl/bc-finalize.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/bc-finalize.wgsl.js +47 -0
- package/dist/src/wgsl/bc-finalize.wgsl.js.map +1 -0
- package/dist/src/wgsl/bc-forward-edge.wgsl.d.ts +15 -0
- package/dist/src/wgsl/bc-forward-edge.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/bc-forward-edge.wgsl.js +76 -0
- package/dist/src/wgsl/bc-forward-edge.wgsl.js.map +1 -0
- package/dist/src/wgsl/bc-forward.wgsl.d.ts +23 -0
- package/dist/src/wgsl/bc-forward.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/bc-forward.wgsl.js +106 -0
- package/dist/src/wgsl/bc-forward.wgsl.js.map +1 -0
- package/dist/src/wgsl/bc-gather.wgsl.d.ts +9 -0
- package/dist/src/wgsl/bc-gather.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/bc-gather.wgsl.js +20 -0
- package/dist/src/wgsl/bc-gather.wgsl.js.map +1 -0
- package/dist/src/wgsl/closeness-reduce.wgsl.d.ts +4 -1
- package/dist/src/wgsl/closeness-reduce.wgsl.d.ts.map +1 -1
- package/dist/src/wgsl/closeness-reduce.wgsl.js +8 -4
- package/dist/src/wgsl/closeness-reduce.wgsl.js.map +1 -1
- package/dist/src/wgsl/closeness-sweep.wgsl.d.ts +4 -2
- package/dist/src/wgsl/closeness-sweep.wgsl.d.ts.map +1 -1
- package/dist/src/wgsl/closeness-sweep.wgsl.js +12 -2
- package/dist/src/wgsl/closeness-sweep.wgsl.js.map +1 -1
- package/dist/src/wgsl/coo-emit.wgsl.d.ts +10 -0
- package/dist/src/wgsl/coo-emit.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/coo-emit.wgsl.js +33 -0
- package/dist/src/wgsl/coo-emit.wgsl.js.map +1 -0
- package/dist/src/wgsl/coo-scatter.wgsl.d.ts +15 -0
- package/dist/src/wgsl/coo-scatter.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/coo-scatter.wgsl.js +32 -0
- package/dist/src/wgsl/coo-scatter.wgsl.js.map +1 -0
- package/dist/src/wgsl/group-by-key-row.wgsl.d.ts +26 -0
- package/dist/src/wgsl/group-by-key-row.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/group-by-key-row.wgsl.js +146 -0
- package/dist/src/wgsl/group-by-key-row.wgsl.js.map +1 -0
- package/dist/src/wgsl/lpa-step.wgsl.d.ts +10 -0
- package/dist/src/wgsl/lpa-step.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/lpa-step.wgsl.js +35 -0
- package/dist/src/wgsl/lpa-step.wgsl.js.map +1 -0
- package/dist/src/wgsl/orient-flags.wgsl.d.ts +9 -0
- package/dist/src/wgsl/orient-flags.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/orient-flags.wgsl.js +21 -0
- package/dist/src/wgsl/orient-flags.wgsl.js.map +1 -0
- package/dist/src/wgsl/run-flags.wgsl.d.ts +8 -0
- package/dist/src/wgsl/run-flags.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/run-flags.wgsl.js +18 -0
- package/dist/src/wgsl/run-flags.wgsl.js.map +1 -0
- package/dist/src/wgsl/tri-intersect.wgsl.d.ts +11 -0
- package/dist/src/wgsl/tri-intersect.wgsl.d.ts.map +1 -0
- package/dist/src/wgsl/tri-intersect.wgsl.js +64 -0
- package/dist/src/wgsl/tri-intersect.wgsl.js.map +1 -0
- package/dist/webgpu-graph-algorithms.js +2828 -321
- package/dist/webgpu-graph-algorithms.js.map +1 -1
- package/package.json +5 -5
- package/src/accelerator.ts +130 -7
- package/src/algorithms/all-pairs.ts +228 -0
- package/src/algorithms/betweenness.ts +739 -0
- package/src/algorithms/closeness.ts +124 -32
- package/src/algorithms/components.ts +2 -2
- package/src/algorithms/label-propagation.ts +280 -0
- package/src/algorithms/simple-symmetric.ts +409 -0
- package/src/algorithms/triangles.ts +240 -0
- package/src/constants.ts +53 -0
- package/src/index.ts +20 -1
- package/src/kernel/prelude.ts +6 -0
- package/src/kernels.ts +411 -10
- package/src/memory/residency.ts +15 -4
- package/src/primitives/coo-to-csr.ts +251 -0
- package/src/primitives/frontier.ts +4 -0
- package/src/primitives/group-by-key.ts +209 -0
- package/src/types/accelerator.ts +26 -6
- package/src/types/algorithms.ts +5 -0
- package/src/types/all-pairs.ts +37 -0
- package/src/types/betweenness.ts +38 -0
- package/src/types/community.ts +18 -0
- package/src/types/structure.ts +28 -0
- package/src/wgsl/apsp-fw.wgsl.ts +112 -0
- package/src/wgsl/apsp-init.wgsl.ts +25 -0
- package/src/wgsl/bc-backward.wgsl.ts +33 -0
- package/src/wgsl/bc-edge-gather.wgsl.ts +35 -0
- package/src/wgsl/bc-finalize.wgsl.ts +46 -0
- package/src/wgsl/bc-forward-edge.wgsl.ts +75 -0
- package/src/wgsl/bc-forward.wgsl.ts +105 -0
- package/src/wgsl/bc-gather.wgsl.ts +19 -0
- package/src/wgsl/closeness-reduce.wgsl.ts +8 -4
- package/src/wgsl/closeness-sweep.wgsl.ts +12 -2
- package/src/wgsl/coo-emit.wgsl.ts +32 -0
- package/src/wgsl/coo-scatter.wgsl.ts +31 -0
- package/src/wgsl/group-by-key-row.wgsl.ts +145 -0
- package/src/wgsl/lpa-step.wgsl.ts +34 -0
- package/src/wgsl/orient-flags.wgsl.ts +20 -0
- package/src/wgsl/run-flags.wgsl.ts +17 -0
- package/src/wgsl/tri-intersect.wgsl.ts +63 -0
- package/dist/chunks/context-Bi6AhScG.js.map +0 -1
package/dist/node.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { W as WebGpuGraphError, G as GpuContext } from "./chunks/context-
|
|
1
|
+
import { W as WebGpuGraphError, G as GpuContext } from "./chunks/context-VIvatQOo.js";
|
|
2
2
|
const INSTALL_HINT = "install the optional peer dependency webgpu@0.4.0";
|
|
3
3
|
const instances = /* @__PURE__ */ new Map();
|
|
4
4
|
function isDawnModule(loaded) {
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* P5's `fruchtermanReingold` and `springElectrical` (the two other layout members of spec 9.3, landed together once
|
|
8
8
|
* both models were green, P5 PD-19), P7's seven algorithm members (spec 8.2, 8.3; M8b-T8, PD-14) and P8's four
|
|
9
9
|
* traversal members (spec 8.4; P8-T13 PD-16, PD-19: `breadthFirstSearch`, `sssp`, `bellmanFord`,
|
|
10
|
-
* `closenessCentrality`, each taking the seam's own option type)
|
|
10
|
+
* `closenessCentrality`, each taking the seam's own option type), `allPairsShortestPath` (design 8.7), P11's
|
|
11
|
+
* `triangleCount` and `labelPropagation`, and nothing else: the CPU-side dispatchers
|
|
11
12
|
* (`accelerated()`, `createSimulation()`) test `acc.betweennessCentrality !== undefined` /
|
|
12
13
|
* `acc.fruchtermanReingold !== undefined` and route to the CPU when the member is absent (spec 2.4 row "method
|
|
13
14
|
* missing"), so a method the GPU does not implement must not exist here -- never a throwing stub. The remaining
|
|
@@ -19,8 +20,9 @@ import { type AcceleratorOptions, type GpuAccelerator } from "./types/accelerato
|
|
|
19
20
|
* Spec 3.3 createAccelerator, verbatim: the object implementing AlgorithmAccelerator & LayoutAccelerator
|
|
20
21
|
* structurally; P3's forceAtlas2, release and dispose, P5's fruchtermanReingold and springElectrical (the same
|
|
21
22
|
* `{ ...o, ...options.layout }` shape as forceAtlas2) plus P7's seven algorithm members and P8's four traversal
|
|
22
|
-
* members, each a delegation to its algorithm with `ctx.assertReady()` first.
|
|
23
|
-
*
|
|
23
|
+
* members and the two betweenness members, each a delegation to its algorithm with `ctx.assertReady()` first. Only
|
|
24
|
+
* the betweenness members consult the accelerator's algorithm defaults (`algorithms.betweenness` supplies `sources` /
|
|
25
|
+
* `k` when a call names neither). One per call (the app creates one and injects
|
|
24
26
|
* it, spec 2.4); `kind` is "webgpu"; `options` is a frozen deep copy; `forceAtlas2(o)` is
|
|
25
27
|
* `createForceAtlas2(ctx, { ...o, ...options.layout })`, so the GPU tuning given here wins over anything the
|
|
26
28
|
* CPU-typed option object carries (spec 3.3: tuning never comes from the caller of the accelerator method);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accelerator.d.ts","sourceRoot":"","sources":["../../src/accelerator.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"accelerator.d.ts","sourceRoot":"","sources":["../../src/accelerator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAeH,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAK/C,OAAO,EACH,KAAK,kBAAkB,EAIvB,KAAK,cAAc,EAGtB,MAAM,wBAAwB,CAAC;AA6GhC;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,cAAc,CAkQ/F"}
|
package/dist/src/accelerator.js
CHANGED
|
@@ -7,19 +7,25 @@
|
|
|
7
7
|
* P5's `fruchtermanReingold` and `springElectrical` (the two other layout members of spec 9.3, landed together once
|
|
8
8
|
* both models were green, P5 PD-19), P7's seven algorithm members (spec 8.2, 8.3; M8b-T8, PD-14) and P8's four
|
|
9
9
|
* traversal members (spec 8.4; P8-T13 PD-16, PD-19: `breadthFirstSearch`, `sssp`, `bellmanFord`,
|
|
10
|
-
* `closenessCentrality`, each taking the seam's own option type)
|
|
10
|
+
* `closenessCentrality`, each taking the seam's own option type), `allPairsShortestPath` (design 8.7), P11's
|
|
11
|
+
* `triangleCount` and `labelPropagation`, and nothing else: the CPU-side dispatchers
|
|
11
12
|
* (`accelerated()`, `createSimulation()`) test `acc.betweennessCentrality !== undefined` /
|
|
12
13
|
* `acc.fruchtermanReingold !== undefined` and route to the CPU when the member is absent (spec 2.4 row "method
|
|
13
14
|
* missing"), so a method the GPU does not implement must not exist here -- never a throwing stub. The remaining
|
|
14
15
|
* algorithm members arrive one per shipped algorithm from P9.
|
|
15
16
|
*/
|
|
17
|
+
import { allPairsShortestPath } from "./algorithms/all-pairs.js";
|
|
16
18
|
import { bellmanFord } from "./algorithms/bellman-ford.js";
|
|
19
|
+
import { betweennessCentrality, edgeBetweennessCentrality } from "./algorithms/betweenness.js";
|
|
17
20
|
import { breadthFirstSearch } from "./algorithms/bfs.js";
|
|
18
21
|
import { closenessCentrality } from "./algorithms/closeness.js";
|
|
19
22
|
import { connectedComponents } from "./algorithms/components.js";
|
|
23
|
+
import { labelPropagation } from "./algorithms/label-propagation.js";
|
|
20
24
|
import { pageRank, personalizedPageRank } from "./algorithms/pagerank.js";
|
|
21
25
|
import { eigenvectorCentrality, hits, katzCentrality } from "./algorithms/spectral.js";
|
|
22
26
|
import { sssp } from "./algorithms/sssp.js";
|
|
27
|
+
import { triangleCount } from "./algorithms/triangles.js";
|
|
28
|
+
import { WebGpuGraphError } from "./errors.js";
|
|
23
29
|
import { createForceAtlas2 } from "./layouts/forceatlas2.js";
|
|
24
30
|
import { createFruchtermanReingold } from "./layouts/fruchterman-reingold.js";
|
|
25
31
|
import { createSpringElectrical } from "./layouts/spring-electrical.js";
|
|
@@ -36,6 +42,25 @@ function copyBetweenness(defaults) {
|
|
|
36
42
|
}
|
|
37
43
|
return Object.freeze(copy);
|
|
38
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* A betweenness call's options with the accelerator's `algorithms.betweenness` defaults applied: the defaults supply
|
|
47
|
+
* `sources` / `k` only when the call names neither, so a call's own sampling always wins whole. The defaults serve
|
|
48
|
+
* graphs of every size, so they are fitted to this one: a default `sources` list keeps only the indices below
|
|
49
|
+
* `nodeCount` (and then wins over a default `k`), and a default `k` of `nodeCount` or more runs every vertex.
|
|
50
|
+
* @param defaults - the frozen defaults, if any
|
|
51
|
+
* @param options - the call's options
|
|
52
|
+
* @param nodeCount - the snapshot's vertex count
|
|
53
|
+
* @returns the options the driver runs with
|
|
54
|
+
*/
|
|
55
|
+
function withBetweennessDefaults(defaults, options, nodeCount) {
|
|
56
|
+
if (defaults === undefined || options?.sources !== undefined || options?.k !== undefined) {
|
|
57
|
+
return options;
|
|
58
|
+
}
|
|
59
|
+
if (defaults.sources !== undefined) {
|
|
60
|
+
return { ...options, sources: defaults.sources.filter((v) => v < nodeCount) };
|
|
61
|
+
}
|
|
62
|
+
return { ...options, k: defaults.k !== undefined && defaults.k < nodeCount ? defaults.k : undefined };
|
|
63
|
+
}
|
|
39
64
|
/**
|
|
40
65
|
* Frozen copy of the algorithms record, one level deeper for `betweenness`.
|
|
41
66
|
* @param algorithms - the caller's record
|
|
@@ -73,8 +98,9 @@ function freezeOptions(options) {
|
|
|
73
98
|
* Spec 3.3 createAccelerator, verbatim: the object implementing AlgorithmAccelerator & LayoutAccelerator
|
|
74
99
|
* structurally; P3's forceAtlas2, release and dispose, P5's fruchtermanReingold and springElectrical (the same
|
|
75
100
|
* `{ ...o, ...options.layout }` shape as forceAtlas2) plus P7's seven algorithm members and P8's four traversal
|
|
76
|
-
* members, each a delegation to its algorithm with `ctx.assertReady()` first.
|
|
77
|
-
*
|
|
101
|
+
* members and the two betweenness members, each a delegation to its algorithm with `ctx.assertReady()` first. Only
|
|
102
|
+
* the betweenness members consult the accelerator's algorithm defaults (`algorithms.betweenness` supplies `sources` /
|
|
103
|
+
* `k` when a call names neither). One per call (the app creates one and injects
|
|
78
104
|
* it, spec 2.4); `kind` is "webgpu"; `options` is a frozen deep copy; `forceAtlas2(o)` is
|
|
79
105
|
* `createForceAtlas2(ctx, { ...o, ...options.layout })`, so the GPU tuning given here wins over anything the
|
|
80
106
|
* CPU-typed option object carries (spec 3.3: tuning never comes from the caller of the accelerator method);
|
|
@@ -227,17 +253,87 @@ export function createAccelerator(ctx, options) {
|
|
|
227
253
|
ctx.assertReady();
|
|
228
254
|
return await bellmanFord(ctx, gs, source, o);
|
|
229
255
|
},
|
|
256
|
+
/**
|
|
257
|
+
* Betweenness centrality on the device (spec 8.4): exact, or sampled through `sources` / `k` (the call's own,
|
|
258
|
+
* else the accelerator's `algorithms.betweenness` defaults), the unscaled sum over the sources run.
|
|
259
|
+
* `endpoints: true` is refused.
|
|
260
|
+
* @param gs - the snapshot
|
|
261
|
+
* @param o - the seam's `BetweennessAcceleratorOptions`
|
|
262
|
+
* @returns the f32 scores with `sourcesUsed` and `sigmaOverflow`
|
|
263
|
+
*/
|
|
264
|
+
async betweennessCentrality(gs, o) {
|
|
265
|
+
ctx.assertReady();
|
|
266
|
+
return await betweennessCentrality(ctx, gs, withBetweennessDefaults(frozen.algorithms?.betweenness, o, gs.nodeCount));
|
|
267
|
+
},
|
|
268
|
+
/**
|
|
269
|
+
* Edge betweenness on the device (spec 8.4): one score per edge, arcs summed and halved when undirected; sampling
|
|
270
|
+
* and defaults as `betweennessCentrality`.
|
|
271
|
+
* @param gs - the snapshot
|
|
272
|
+
* @param o - the seam's `BetweennessAcceleratorOptions`
|
|
273
|
+
* @returns the f32 per-edge scores with `sourcesUsed` and `sigmaOverflow`
|
|
274
|
+
*/
|
|
275
|
+
async edgeBetweennessCentrality(gs, o) {
|
|
276
|
+
ctx.assertReady();
|
|
277
|
+
return await edgeBetweennessCentrality(ctx, gs, withBetweennessDefaults(frozen.algorithms?.betweenness, o, gs.nodeCount));
|
|
278
|
+
},
|
|
230
279
|
/**
|
|
231
280
|
* Closeness centrality on the device (spec 8.4; P8-T13): the bit-parallel multi-source sweep, or one `sssp`
|
|
232
281
|
* per source when `weighted`. `maxIterations` / `tolerance` are refused when defined (P8 PD-25).
|
|
233
282
|
* @param gs - the snapshot
|
|
234
|
-
* @param o -
|
|
235
|
-
* @returns the f32 scores with `precision: "f32"` (spec 9.7)
|
|
283
|
+
* @param o - `weighted`, and a sampled run's `sources` (undirected snapshots only)
|
|
284
|
+
* @returns the f32 scores with `precision: "f32"` (spec 9.7) and `sourcesUsed`
|
|
236
285
|
*/
|
|
237
286
|
async closenessCentrality(gs, o) {
|
|
238
287
|
ctx.assertReady();
|
|
239
288
|
return await closenessCentrality(ctx, gs, o);
|
|
240
289
|
},
|
|
290
|
+
/**
|
|
291
|
+
* All-pairs shortest paths on the device (design 8.7): blocked Floyd-Warshall, `E_TOO_LARGE` above the device's
|
|
292
|
+
* storage-binding ceiling. The seam passes `SsspOptions`; neither of its keys has an all-pairs meaning, so a
|
|
293
|
+
* defined `cutoff` (it would change what `+Infinity` means) or `weights` (a per-arc override is a different
|
|
294
|
+
* matrix from the snapshot's resident column) is `E_UNSUPPORTED { option }`, never silently dropped.
|
|
295
|
+
* @param gs - the snapshot
|
|
296
|
+
* @param o - the seam's `SsspOptions`; both keys refused when defined
|
|
297
|
+
* @returns the row-major `n x n` distances and `n` (spec 3.3 line 835)
|
|
298
|
+
*/
|
|
299
|
+
async allPairsShortestPath(gs, o) {
|
|
300
|
+
ctx.assertReady();
|
|
301
|
+
for (const key of ["cutoff", "weights"]) {
|
|
302
|
+
if (o?.[key] !== undefined) {
|
|
303
|
+
throw new WebGpuGraphError("E_UNSUPPORTED", `allPairsShortestPath: ${key} is not supported`, {
|
|
304
|
+
option: key,
|
|
305
|
+
hint: "all-pairs shortest paths runs over the snapshot's own weights with no cutoff",
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return await allPairsShortestPath(ctx, gs);
|
|
310
|
+
},
|
|
311
|
+
/**
|
|
312
|
+
* Triangle counting with the clustering coefficient and the transitivity (design 8.5; P11).
|
|
313
|
+
* @param gs - the snapshot
|
|
314
|
+
* @returns perNode, total, coefficient and transitivity
|
|
315
|
+
*/
|
|
316
|
+
async triangleCount(gs) {
|
|
317
|
+
ctx.assertReady();
|
|
318
|
+
return await triangleCount(ctx, gs);
|
|
319
|
+
},
|
|
320
|
+
/**
|
|
321
|
+
* Label propagation (design 8.6; P11): `maxIterations` and `weighted` are honoured, `tolerance` is refused when
|
|
322
|
+
* defined (a label propagation stops at a fixed point, not below a tolerance).
|
|
323
|
+
* @param gs - the snapshot
|
|
324
|
+
* @param o - the seam's placeholder `HitsOptionsLike`
|
|
325
|
+
* @returns the labels dense in first-seen order, the community count and groups()
|
|
326
|
+
*/
|
|
327
|
+
async labelPropagation(gs, o) {
|
|
328
|
+
ctx.assertReady();
|
|
329
|
+
if (o?.tolerance !== undefined) {
|
|
330
|
+
throw new WebGpuGraphError("E_UNSUPPORTED", "labelPropagation: tolerance has no meaning here", {
|
|
331
|
+
option: "tolerance",
|
|
332
|
+
hint: "label propagation stops at a fixed point or after maxIterations passes",
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
return await labelPropagation(ctx, gs, { maxIterations: o?.maxIterations, weighted: o?.weighted });
|
|
336
|
+
},
|
|
241
337
|
/**
|
|
242
338
|
* Destroys every device buffer recorded for the snapshot (spec 4.5); delegates to ctx.release.
|
|
243
339
|
* @param s - the snapshot the app is done with
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accelerator.js","sourceRoot":"","sources":["../../src/accelerator.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"accelerator.js","sourceRoot":"","sources":["../../src/accelerator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,qBAAqB,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAC/F,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AACrE,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAC1E,OAAO,EAAE,qBAAqB,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AACvF,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,mCAAmC,CAAC;AAC9E,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AA6CxE;;;;;GAKG;AACH,SAAS,eAAe,CAAC,QAA6B;IAClD,MAAM,I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MG;QACH,KAAK,CAAC,gBAAgB,CAAC,EAAiB,EAAE,CAAmB;YACzD,GAAG,CAAC,WAAW,EAAE,CAAC;YAClB,IAAI,CAAC,EAAE,SAAS,KAAK,SAAS,EAAE,CAAC;gBAC7B,MAAM,IAAI,gBAAgB,CAAC,eAAe,EAAE,iDAAiD,EAAE;oBAC3F,MAAM,EAAE,WAAW;oBACnB,IAAI,EAAE,wEAAwE;iBACjF,CAAC,CAAC;YACP,CAAC;YACD,OAAO,MAAM,gBAAgB,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,aAAa,EAAE,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;QACvG,CAAC;QACD;;;WAGG;QACH,OAAO,CAAC,CAAgB;YACpB,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QACnB,CAAC;QACD;;WAEG;QACH,OAAO;YACH,GAAG,CAAC,OAAO,EAAE,CAAC;QAClB,CAAC;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* All-pairs shortest paths on the device (design 8.7, 3.3 lines 813 and 835, 9.7): blocked Floyd-Warshall over
|
|
3
|
+
* `APSP_TILE x APSP_TILE` tiles of ONE `n x n` f32 matrix. `fill` sets the matrix to `+Infinity`, `apsp-init` writes
|
|
4
|
+
* the arcs (the cheapest of parallel arcs) and the diagonal zero, then `B = ceil(n / APSP_TILE)` rounds each run the
|
|
5
|
+
* three `apsp-fw` phases in order: the pivot block, the pivot row and column, everything else. The whole sweep --
|
|
6
|
+
* `3 B` dispatches -- is recorded into ONE compute pass: WebGPU runs the dispatches of a pass in order and makes each
|
|
7
|
+
* one's writes visible to the next, so nothing is read back until the matrix is done. Above
|
|
8
|
+
* `APSP_MAX_DISPATCHES_PER_SUBMIT` dispatches the sweep is split into further submits and `signal` is checked
|
|
9
|
+
* between them -- but no binding WebGPU devices offer today reaches that (a 4 GiB binding is 32,767 nodes, 3,072
|
|
10
|
+
* dispatches), so in practice the sweep is one submit: `signal` is checked before it and after it, never during,
|
|
11
|
+
* and `onProgress` fires once. The result is bitwise reproducible (no atomics, and a cell is only ever written by one
|
|
12
|
+
* lane per dispatch), exact for hop counts, and the minimum of f32 path sums for weights.
|
|
13
|
+
*
|
|
14
|
+
* The ceiling: the matrix is exactly `n * n` (never padded to the tile) and is bound as ONE storage binding, so
|
|
15
|
+
* `n <= floor(sqrt(limit / 4))` with `limit` the smaller of the device's `maxStorageBufferBindingSize` and
|
|
16
|
+
* `maxBufferSize`. A context's default `limits: "raise"` takes the adapter's own limits: 23,170 nodes on a hardware
|
|
17
|
+
* adapter under Dawn (a 2 GiB binding), 5,792 on lavapipe or under `limits: "default"` (the 128 MiB spec default).
|
|
18
|
+
* Above it the call throws `E_TOO_LARGE` naming the node count, the ceiling, the limit it read and
|
|
19
|
+
* `GpuContextOptions.limits` as the way to raise it; the rows are never windowed. Refused before any device work: a negative weight (`E_UNSUPPORTED
|
|
20
|
+
* allPairs.negativeWeights` -- Floyd-Warshall's in-place tile update is race-free only while the diagonal stays 0)
|
|
21
|
+
* and a NaN or infinite one (`allPairs.nonFiniteWeights`). `weighted` defaults to "the snapshot has weights";
|
|
22
|
+
* `weighted: false` on a weighted snapshot computes hop counts. The empty graph returns an empty matrix without a
|
|
23
|
+
* dispatch. `allPairsWithTuning` is what the tests drive; nothing public exposes it.
|
|
24
|
+
*/
|
|
25
|
+
import { type GraphSnapshot } from "@graphty/graph-format";
|
|
26
|
+
import { type GpuContext } from "../context.js";
|
|
27
|
+
import { type ApspOptions, type GpuApspResult } from "../types/all-pairs.js";
|
|
28
|
+
import { type GpuRunOptions } from "../types/run.js";
|
|
29
|
+
/**
|
|
30
|
+
* All-pairs shortest paths on the device (design 8.7, 3.3 line 813): blocked Floyd-Warshall over 32 x 32 tiles of one
|
|
31
|
+
* `n x n` f32 matrix. `dist[i * n + j]` is the distance from `i` to `j`, `+Infinity` when unreachable, `0` on the
|
|
32
|
+
* diagonal. `E_TOO_LARGE` above `floor(sqrt(maxStorageBufferBindingSize / 4))` nodes (23,170 at a 2 GiB binding, the
|
|
33
|
+
* usual hardware adapter under the context's default `limits: "raise"`; 5,792 at the 128 MiB spec default);
|
|
34
|
+
* `E_UNSUPPORTED` for a negative or non-finite weight unless `weighted: false`.
|
|
35
|
+
* @param ctx - the context whose device runs the kernels
|
|
36
|
+
* @param s - the snapshot (uploaded through ctx.residency, or found there)
|
|
37
|
+
* @param options - `weighted` (default: the snapshot has weights), plus dest (a Float32Array of length n * n) / signal / onProgress
|
|
38
|
+
* @returns the row-major `n x n` distances and `n`
|
|
39
|
+
*/
|
|
40
|
+
export declare function allPairsShortestPath(ctx: GpuContext, s: GraphSnapshot, options?: ApspOptions & GpuRunOptions): Promise<GpuApspResult>;
|
|
41
|
+
//# sourceMappingURL=all-pairs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"all-pairs.d.ts","sourceRoot":"","sources":["../../../src/algorithms/all-pairs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAG3D,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,eAAe,CAAC;AAOhD,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC7E,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAC;AA8KrD;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAChC,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,aAAa,EAChB,OAAO,CAAC,EAAE,WAAW,GAAG,aAAa,GACtC,OAAO,CAAC,aAAa,CAAC,CAExB"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* All-pairs shortest paths on the device (design 8.7, 3.3 lines 813 and 835, 9.7): blocked Floyd-Warshall over
|
|
3
|
+
* `APSP_TILE x APSP_TILE` tiles of ONE `n x n` f32 matrix. `fill` sets the matrix to `+Infinity`, `apsp-init` writes
|
|
4
|
+
* the arcs (the cheapest of parallel arcs) and the diagonal zero, then `B = ceil(n / APSP_TILE)` rounds each run the
|
|
5
|
+
* three `apsp-fw` phases in order: the pivot block, the pivot row and column, everything else. The whole sweep --
|
|
6
|
+
* `3 B` dispatches -- is recorded into ONE compute pass: WebGPU runs the dispatches of a pass in order and makes each
|
|
7
|
+
* one's writes visible to the next, so nothing is read back until the matrix is done. Above
|
|
8
|
+
* `APSP_MAX_DISPATCHES_PER_SUBMIT` dispatches the sweep is split into further submits and `signal` is checked
|
|
9
|
+
* between them -- but no binding WebGPU devices offer today reaches that (a 4 GiB binding is 32,767 nodes, 3,072
|
|
10
|
+
* dispatches), so in practice the sweep is one submit: `signal` is checked before it and after it, never during,
|
|
11
|
+
* and `onProgress` fires once. The result is bitwise reproducible (no atomics, and a cell is only ever written by one
|
|
12
|
+
* lane per dispatch), exact for hop counts, and the minimum of f32 path sums for weights.
|
|
13
|
+
*
|
|
14
|
+
* The ceiling: the matrix is exactly `n * n` (never padded to the tile) and is bound as ONE storage binding, so
|
|
15
|
+
* `n <= floor(sqrt(limit / 4))` with `limit` the smaller of the device's `maxStorageBufferBindingSize` and
|
|
16
|
+
* `maxBufferSize`. A context's default `limits: "raise"` takes the adapter's own limits: 23,170 nodes on a hardware
|
|
17
|
+
* adapter under Dawn (a 2 GiB binding), 5,792 on lavapipe or under `limits: "default"` (the 128 MiB spec default).
|
|
18
|
+
* Above it the call throws `E_TOO_LARGE` naming the node count, the ceiling, the limit it read and
|
|
19
|
+
* `GpuContextOptions.limits` as the way to raise it; the rows are never windowed. Refused before any device work: a negative weight (`E_UNSUPPORTED
|
|
20
|
+
* allPairs.negativeWeights` -- Floyd-Warshall's in-place tile update is race-free only while the diagonal stays 0)
|
|
21
|
+
* and a NaN or infinite one (`allPairs.nonFiniteWeights`). `weighted` defaults to "the snapshot has weights";
|
|
22
|
+
* `weighted: false` on a weighted snapshot computes hop counts. The empty graph returns an empty matrix without a
|
|
23
|
+
* dispatch. `allPairsWithTuning` is what the tests drive; nothing public exposes it.
|
|
24
|
+
*/
|
|
25
|
+
import { APSP_MAX_DISPATCHES_PER_SUBMIT, APSP_TILE, F32_INF_BITS } from "../constants.js";
|
|
26
|
+
import { WebGpuGraphError } from "../errors.js";
|
|
27
|
+
import { CommandBatch } from "../kernel/batch.js";
|
|
28
|
+
import { plan1d, plan2d } from "../kernel/dispatch.js";
|
|
29
|
+
import { APSP_PARAMS, FILL_PARAMS, graphBindings, graphOverrides, kernelSpec } from "../kernels.js";
|
|
30
|
+
import { assertWholeCore } from "../primitives/core-shape.js";
|
|
31
|
+
import { assertDeviceComputes } from "../primitives/verify.js";
|
|
32
|
+
import { algorithmScope } from "./scope.js";
|
|
33
|
+
import { aborted, bindingOf, checkDest } from "./sssp.js";
|
|
34
|
+
const ALGORITHM = "allPairsShortestPath";
|
|
35
|
+
/** The rounds one submit holds by default: three dispatches per round. */
|
|
36
|
+
const DEFAULT_ROUNDS_PER_SUBMIT = Math.floor(APSP_MAX_DISPATCHES_PER_SUBMIT / 3);
|
|
37
|
+
/**
|
|
38
|
+
* The largest node count whose `n x n` f32 matrix fits the device (design 8.7): `floor(sqrt(limit / 4))` over the
|
|
39
|
+
* smaller of `maxStorageBufferBindingSize` and `maxBufferSize`, corrected so the float square root can never
|
|
40
|
+
* overshoot by one.
|
|
41
|
+
* @internal
|
|
42
|
+
* @param limits - the device limits
|
|
43
|
+
* @returns the ceiling and the limit that set it
|
|
44
|
+
*/
|
|
45
|
+
export function allPairsCeiling(limits) {
|
|
46
|
+
const binding = limits.maxStorageBufferBindingSize;
|
|
47
|
+
const bufferSize = limits.maxBufferSize;
|
|
48
|
+
const limitName = bufferSize < binding ? "maxBufferSize" : "maxStorageBufferBindingSize";
|
|
49
|
+
const limit = Math.min(binding, bufferSize);
|
|
50
|
+
let maxNodes = Math.floor(Math.sqrt(limit / 4));
|
|
51
|
+
while (4 * maxNodes * maxNodes > limit) {
|
|
52
|
+
maxNodes -= 1;
|
|
53
|
+
}
|
|
54
|
+
while (4 * (maxNodes + 1) * (maxNodes + 1) <= limit) {
|
|
55
|
+
maxNodes += 1;
|
|
56
|
+
}
|
|
57
|
+
return { maxNodes, limit, limitName };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* All-pairs shortest paths with the test knobs; `allPairsShortestPath` is this with an empty tuning.
|
|
61
|
+
* @internal
|
|
62
|
+
* @param ctx - the context whose device runs the kernels
|
|
63
|
+
* @param s - the snapshot (uploaded through ctx.residency, or found there)
|
|
64
|
+
* @param options - `weighted`, plus dest (a Float32Array of length n * n) / signal / onProgress (rounds done of `ceil(n / 32)`)
|
|
65
|
+
* @param tuning - the knobs
|
|
66
|
+
* @returns the row-major `n x n` distances and `n`
|
|
67
|
+
*/
|
|
68
|
+
export async function allPairsWithTuning(ctx, s, options, tuning) {
|
|
69
|
+
ctx.assertReady();
|
|
70
|
+
const n = s.nodeCount;
|
|
71
|
+
const roundsPerSubmit = tuning.roundsPerSubmit ?? DEFAULT_ROUNDS_PER_SUBMIT;
|
|
72
|
+
if (!Number.isInteger(roundsPerSubmit) || roundsPerSubmit < 1 || roundsPerSubmit > DEFAULT_ROUNDS_PER_SUBMIT) {
|
|
73
|
+
throw new WebGpuGraphError("E_INVALID_ARGUMENT", `${ALGORITHM}: roundsPerSubmit must be an integer in [1, ${DEFAULT_ROUNDS_PER_SUBMIT}]`, {
|
|
74
|
+
argument: "roundsPerSubmit",
|
|
75
|
+
value: roundsPerSubmit,
|
|
76
|
+
expected: `an integer in [1, ${DEFAULT_ROUNDS_PER_SUBMIT}]`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const { maxNodes, limit, limitName } = allPairsCeiling(ctx.caps.limits);
|
|
80
|
+
if (n > maxNodes) {
|
|
81
|
+
throw new WebGpuGraphError("E_TOO_LARGE", `${ALGORITHM}: ${n} nodes need a ${4 * n * n}-byte distance matrix in one storage binding; this device's ${limitName} of ${limit} bytes holds at most ${maxNodes} nodes -- raise it through GpuContextOptions.limits`, {
|
|
82
|
+
needed: 4 * n * n,
|
|
83
|
+
limit,
|
|
84
|
+
path: "allPairs.matrix",
|
|
85
|
+
algorithm: ALGORITHM,
|
|
86
|
+
nodes: n,
|
|
87
|
+
maxNodes,
|
|
88
|
+
limitName,
|
|
89
|
+
hint: `raise ${limitName} through GpuContextOptions.limits`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
const dist = checkDest(ALGORITHM, options?.dest, n * n) ?? new Float32Array(n * n);
|
|
93
|
+
const weighted = (options?.weighted ?? s.weights !== null) && s.weights !== null && !s.flags.allWeightsOne;
|
|
94
|
+
if (weighted && !s.flags.nonNegativeWeights) {
|
|
95
|
+
throw new WebGpuGraphError("E_UNSUPPORTED", `${ALGORITHM}: a negative weight is not supported`, {
|
|
96
|
+
feature: "allPairs.negativeWeights",
|
|
97
|
+
hint: "the blocked Floyd-Warshall sweep needs non-negative weights; pass weighted: false for hop counts",
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (weighted && !s.flags.finiteWeights) {
|
|
101
|
+
throw new WebGpuGraphError("E_UNSUPPORTED", `${ALGORITHM}: a NaN or infinite weight has no shortest path`, {
|
|
102
|
+
feature: "allPairs.nonFiniteWeights",
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
if (options?.signal?.aborted) {
|
|
106
|
+
throw aborted(ALGORITHM);
|
|
107
|
+
}
|
|
108
|
+
if (n === 0) {
|
|
109
|
+
return { dist, n };
|
|
110
|
+
}
|
|
111
|
+
await assertDeviceComputes(ctx);
|
|
112
|
+
const core = ctx.residency.core(s);
|
|
113
|
+
assertWholeCore(core, s.arcCount, ctx.caps.limits.maxStorageBufferBindingSize, ALGORITHM);
|
|
114
|
+
const blocks = Math.ceil(n / APSP_TILE);
|
|
115
|
+
const scope = algorithmScope(ctx, ALGORITHM, Math.min(roundsPerSubmit, blocks) + 2);
|
|
116
|
+
try {
|
|
117
|
+
const wg = ctx.workgroupSize;
|
|
118
|
+
const bytes = 4 * n * n;
|
|
119
|
+
const matrix = bindingOf(scope.scratch(bytes, "dist"), bytes);
|
|
120
|
+
await ctx.allocator.check();
|
|
121
|
+
const weightsBinding = weighted ? undefined : null;
|
|
122
|
+
const fill = await ctx.pipelines.kernel(kernelSpec("fill"));
|
|
123
|
+
const init = await ctx.pipelines.kernel(kernelSpec("apsp-init", graphOverrides(core, null, weightsBinding)));
|
|
124
|
+
const phases = await Promise.all([0, 1, 2].map((phase) => ctx.pipelines.kernel(kernelSpec("apsp-fw", { PHASE: phase }))));
|
|
125
|
+
const graph = graphBindings(core, null, weightsBinding);
|
|
126
|
+
const others = blocks - 1;
|
|
127
|
+
const phasePlans = [plan2d(1, ctx.caps), plan2d(2 * others, ctx.caps), plan2d(others * others, ctx.caps)];
|
|
128
|
+
for (let first = 0; first < blocks; first += roundsPerSubmit) {
|
|
129
|
+
const batch = new CommandBatch(ctx, `${ALGORITHM}/sweep`);
|
|
130
|
+
const pass = batch.pass("apsp");
|
|
131
|
+
if (first === 0) {
|
|
132
|
+
const fillParams = scope.params(FILL_PARAMS, { count: n * n, value: F32_INF_BITS, mode: 0, pad0: 0 });
|
|
133
|
+
fill.dispatch(pass, fill.bind({ dst: matrix, P: fillParams.binding }), plan1d(n * n, wg, ctx.caps), [
|
|
134
|
+
fillParams.offset,
|
|
135
|
+
]);
|
|
136
|
+
const initParams = scope.params(APSP_PARAMS, { n, round: 0, blocks, infBits: F32_INF_BITS });
|
|
137
|
+
init.dispatch(pass, init.bind({ ...graph, dist: matrix, P: initParams.binding }), plan1d(n, wg, ctx.caps), [initParams.offset]);
|
|
138
|
+
}
|
|
139
|
+
const last = Math.min(first + roundsPerSubmit, blocks);
|
|
140
|
+
for (let round = first; round < last; round++) {
|
|
141
|
+
const params = scope.params(APSP_PARAMS, { n, round, blocks, infBits: F32_INF_BITS });
|
|
142
|
+
for (let phase = 0; phase < 3; phase++) {
|
|
143
|
+
const kernel = phases[phase];
|
|
144
|
+
kernel.dispatch(pass, kernel.bind({ dist: matrix, P: params.binding }), phasePlans[phase], [
|
|
145
|
+
params.offset,
|
|
146
|
+
]);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
batch.endPass();
|
|
150
|
+
scope.flush();
|
|
151
|
+
const submitted = batch.submit();
|
|
152
|
+
await submitted.readback;
|
|
153
|
+
ctx.assertReady();
|
|
154
|
+
options?.onProgress?.(last, blocks);
|
|
155
|
+
if (options?.signal?.aborted) {
|
|
156
|
+
throw aborted(ALGORITHM, submitted.id);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
await ctx.readback.read(matrix.buffer, bytes, dist);
|
|
160
|
+
ctx.assertReady();
|
|
161
|
+
return { dist, n };
|
|
162
|
+
}
|
|
163
|
+
finally {
|
|
164
|
+
scope.dispose();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* All-pairs shortest paths on the device (design 8.7, 3.3 line 813): blocked Floyd-Warshall over 32 x 32 tiles of one
|
|
169
|
+
* `n x n` f32 matrix. `dist[i * n + j]` is the distance from `i` to `j`, `+Infinity` when unreachable, `0` on the
|
|
170
|
+
* diagonal. `E_TOO_LARGE` above `floor(sqrt(maxStorageBufferBindingSize / 4))` nodes (23,170 at a 2 GiB binding, the
|
|
171
|
+
* usual hardware adapter under the context's default `limits: "raise"`; 5,792 at the 128 MiB spec default);
|
|
172
|
+
* `E_UNSUPPORTED` for a negative or non-finite weight unless `weighted: false`.
|
|
173
|
+
* @param ctx - the context whose device runs the kernels
|
|
174
|
+
* @param s - the snapshot (uploaded through ctx.residency, or found there)
|
|
175
|
+
* @param options - `weighted` (default: the snapshot has weights), plus dest (a Float32Array of length n * n) / signal / onProgress
|
|
176
|
+
* @returns the row-major `n x n` distances and `n`
|
|
177
|
+
*/
|
|
178
|
+
export function allPairsShortestPath(ctx, s, options) {
|
|
179
|
+
return allPairsWithTuning(ctx, s, options, {});
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=all-pairs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"all-pairs.js","sourceRoot":"","sources":["../../../src/algorithms/all-pairs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAIH,OAAO,EAAE,8BAA8B,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE1F,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACpG,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAG/D,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE1D,MAAM,SAAS,GAAG,sBAAsB,CAAC;AAEzC,0EAA0E;AAC1E,MAAM,yBAAyB,GAAG,IAAI,CAAC,KAAK,CAAC,8BAA8B,GAAG,CAAC,CAAC,CAAC;AAWjF;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAAC,MAAiF;IAK7G,MAAM,OAAO,GAAG,MAAM,CAAC,2BAA2B,CAAC;IACnD,MAAM,UAAU,GAAG,MAAM,CAAC,aAAa,CAAC;IACxC,MAAM,SAAS,GAAG,UAAU,GAAG,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,6BAA6B,CAAC;IACzF,MAAM,KA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,MAAM,CAAC,MAAM;qBAChB,CAAC,CAAC;gBACP,CAAC;YACL,CAAC;YACD,KAAK,CAAC,OAAO,EAAE,CAAC;YAChB,KAAK,CAAC,KAAK,EAAE,CAAC;YACd,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;YACjC,MAAM,SAAS,CAAC,QAAQ,CAAC;YACzB,GAAG,CAAC,WAAW,EAAE,CAAC;YAClB,OAAO,EAAE,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YACpC,IAAI,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;gBAC3B,MAAM,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC,EAAE,CAAC,CAAC;YAC3C,CAAC;QACL,CAAC;QACD,MAAM,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;QACpD,GAAG,CAAC,WAAW,EAAE,CAAC;QAClB,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;IACvB,CAAC;YAAS,CAAC;QACP,KAAK,CAAC,OAAO,EAAE,CAAC;IACpB,CAAC;AACL,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,oBAAoB,CAChC,GAAe,EACf,CAAgB,EAChB,OAAqC;IAErC,OAAO,kBAAkB,CAAC,GAAG,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC,CAAC;AACnD,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Betweenness and edge betweenness on the device (design 8.4 "Betweenness (A7)", 3.3 lines 811-812, 9.7): Brandes'
|
|
3
|
+
* algorithm as McLaughlin-Bader run it, k sources at a time.
|
|
4
|
+
*
|
|
5
|
+
* A source batch keeps four `n x k` arrays -- `depthK`, `sigmaK` (u32 shortest-path counts), `deltaK` (f32
|
|
6
|
+
* dependencies) and the claim log `S` -- plus `ends`, the level boundaries into the log. Every array is indexed by
|
|
7
|
+
* `s * n + v`, and a log entry IS that index, so one u32 names a `(vertex, source)` pair: `4 n k` bytes can never
|
|
8
|
+
* reach 2^32 because each array is one storage binding. The forward pass is a tagged breadth-first search: every
|
|
9
|
+
* level is `bc-finalize` (the boundary: `ends[level + 1] = stackTop`, `done` on an empty level) and then ONE forward
|
|
10
|
+
* dispatch, either `bc-forward` (block-mapped over the level's range of the log) or `bc-forward-edge` (every edge of
|
|
11
|
+
* the `edgeList` view for every source), both claiming, counting and appending with the same rules. Levels are
|
|
12
|
+
* recorded `MAX_LEVELS_PER_SUBMIT` per submit with one small readback (the counters block and the `ends` prefix), and
|
|
13
|
+
* the log's `ends` is known on the host when the forward phase ends, so the backward pass is planned exactly: one
|
|
14
|
+
* `bc-backward` dispatch per level from the deepest to depth 1, each writing `delta[s][w]` once by pulling over the
|
|
15
|
+
* successors; then `bc-gather` adds each vertex's k dependencies into `bc` (and `bc-edge-gather` each arc's k terms
|
|
16
|
+
* into `arcScores`). Nothing accumulates a float through an atomic, so the scores are bitwise reproducible.
|
|
17
|
+
*
|
|
18
|
+
* The batch size k is planned from the device limits at 16 bytes per (node, source) -- the three arrays design 4.7
|
|
19
|
+
* counts plus the 4-byte log entry it omits -- as `min(floor(maxStorageBufferBindingSize / 4n), floor(0.25 x
|
|
20
|
+
* maxBufferSize / 16n), 64)`, at least 1 (`planBatchSize`); a graph whose single source does not fit one binding is
|
|
21
|
+
* E_TOO_LARGE. The forward form is chosen per batch: the first batch runs `bc-forward`; a later one runs the
|
|
22
|
+
* edge-parallel form when the previous batch's level count -- the MAXIMUM depth over its sources, the only depth
|
|
23
|
+
* figure the host has, which over-estimates the median of design 8.4's rule and so errs toward the frontier form --
|
|
24
|
+
* is below `BC_EDGE_PARALLEL_GAMMA x log2(n)`.
|
|
25
|
+
*
|
|
26
|
+
* The host applies the CPU package's convention after the readback (`@graphty/algorithms` betweenness.ts): the vertex
|
|
27
|
+
* scores are halved on an undirected snapshot (the gather counts each unordered pair from both ends); the edge scores
|
|
28
|
+
* are folded with `foldArcs(s, perArc, "sum")` and halved the same way, because an undirected edge's two arcs carry
|
|
29
|
+
* the pairs that cross it in each direction. Over every source both arcs hold the same sum, but over a sample they do
|
|
30
|
+
* not: on the path 0-1-2 with the one source 2, arc 1->0 collects 1 and arc 0->1 collects 0, so keeping either arc
|
|
31
|
+
* alone would lose the pairs crossing the other way. `normalized`
|
|
32
|
+
* divides both by `(n - 1)(n - 2)` directed or half that undirected, when positive. A sampled run (`sources` or `k`)
|
|
33
|
+
* is the UNSCALED sum over the sources run, reported beside `sourcesUsed`. `endpoints: true` is refused: the CPU's
|
|
34
|
+
* endpoints branch (`algorithms/src/algorithms/centrality/betweenness.ts`, `predecessors.length === 0 && w !==
|
|
35
|
+
* source`) can never fire for a vertex on the Brandes stack, so there is no behaviour to be in parity with.
|
|
36
|
+
* Betweenness is breadth-first on both packages; weights are ignored. Parallel edges are distinct shortest paths
|
|
37
|
+
* here (each arc adds to sigma), where the CPU package refuses them or, with `allowParallelEdges`, collapses them to
|
|
38
|
+
* one: on a multigraph the two packages' scores differ.
|
|
39
|
+
*/
|
|
40
|
+
import { type GraphSnapshot } from "@graphty/graph-format";
|
|
41
|
+
import { type GpuContext } from "../context.js";
|
|
42
|
+
import { type BetweennessAcceleratorOptions } from "../types/accelerator.js";
|
|
43
|
+
import { type GpuBetweennessResult, type GpuEdgeScoresResult } from "../types/betweenness.js";
|
|
44
|
+
import { type GpuRunOptions } from "../types/run.js";
|
|
45
|
+
/**
|
|
46
|
+
* Betweenness centrality on the device (spec 3.3 line 811, design 8.4): Brandes' algorithm, k sources per batch.
|
|
47
|
+
* Exact over every vertex by default; `sources` or `k` gives the SAMPLED form, whose scores are the unscaled sum over
|
|
48
|
+
* the sources run (`sourcesUsed` beside them; multiply by `n / sourcesUsed` for the estimator of the full sum). The
|
|
49
|
+
* CPU package's convention: halved on an undirected snapshot, `normalized` divides by `(n - 1)(n - 2)` directed or
|
|
50
|
+
* half that undirected. Weights are ignored (breadth-first on both packages). `endpoints: true` is E_UNSUPPORTED.
|
|
51
|
+
* `sigmaOverflow` is true when some pair has more than 2^32 shortest paths: the scores are then wrong.
|
|
52
|
+
* @param ctx - the context whose device runs the kernels
|
|
53
|
+
* @param s - the snapshot (uploaded through ctx.residency, or found there)
|
|
54
|
+
* @param options - `normalized`, `endpoints`, `sources`, `k`, plus dest (a Float32Array of length n) / signal / onProgress (sources done, sources total)
|
|
55
|
+
* @returns the f32 scores, `iterations` (the source batches run), `converged: true`, `sourcesUsed`, `sigmaOverflow`
|
|
56
|
+
*/
|
|
57
|
+
export declare function betweennessCentrality(ctx: GpuContext, s: GraphSnapshot, options?: BetweennessAcceleratorOptions & GpuRunOptions): Promise<GpuBetweennessResult>;
|
|
58
|
+
/**
|
|
59
|
+
* Edge betweenness centrality on the device (spec 3.3 line 812, design 8.4): the per-arc terms of the same batches,
|
|
60
|
+
* folded to one score per edge with `foldArcs(s, perArc, "sum")` and halved on an undirected snapshot (the two arcs
|
|
61
|
+
* carry the pairs crossing the edge in each direction, so their sum counts each unordered pair twice; this is the CPU
|
|
62
|
+
* package's number, and it holds for a sample too). `normalized`, sampling, `endpoints` and `sigmaOverflow` as in
|
|
63
|
+
* `betweennessCentrality`.
|
|
64
|
+
* @param ctx - the context whose device runs the kernels
|
|
65
|
+
* @param s - the snapshot (uploaded through ctx.residency, or found there)
|
|
66
|
+
* @param options - as `betweennessCentrality`, dest a Float32Array of length edgeCount
|
|
67
|
+
* @returns the f32 per-edge scores, `sourcesUsed`, `sigmaOverflow`
|
|
68
|
+
*/
|
|
69
|
+
export declare function edgeBetweennessCentrality(ctx: GpuContext, s: GraphSnapshot, options?: BetweennessAcceleratorOptions & GpuRunOptions): Promise<GpuEdgeScoresResult>;
|
|
70
|
+
//# sourceMappingURL=betweenness.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"betweenness.d.ts","sourceRoot":"","sources":["../../../src/algorithms/betweenness.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,OAAO,EAAsB,KAAK,aAAa,EAAY,MAAM,uBAAuB,CAAC;AASzF,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,eAAe,CAAC;AAShD,OAAO,EAAE,KAAK,6BAA6B,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,EAAE,KAAK,oBAAoB,EAAE,KAAK,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAE9F,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAgoBrD;;;;;;;;;;;GAWG;AACH,wBAAgB,qBAAqB,CACjC,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,aAAa,EAChB,OAAO,CAAC,EAAE,6BAA6B,GAAG,aAAa,GACxD,OAAO,CAAC,oBAAoB,CAAC,CAE/B;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,yBAAyB,CACrC,GAAG,EAAE,UAAU,EACf,CAAC,EAAE,aAAa,EAChB,OAAO,CAAC,EAAE,6BAA6B,GAAG,aAAa,GACxD,OAAO,CAAC,mBAAmB,CAAC,CAE9B"}
|