Skip to content

Commit 515d544

Browse files
committed
stuff
1 parent 9cce4b8 commit 515d544

10 files changed

Lines changed: 210 additions & 175 deletions

File tree

packages/app/src/routes/app/(canvas)/+page.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import { EdgePathBundlingGPUFloydWarshall } from '@bachelor/core/edge-path-bundling/floyd-warshall/gpu';
1111
import { onMount } from 'svelte';
1212
import { ThetaSpanner } from '@bachelor/core/spanner/theta/gpu';
13+
import { GreedySpanner } from '@bachelor/core/spanner/greedy/gpu';
1314
1415
const { device } = getWebGPUState();
1516
const { canvas, context } = getCanvasState();
@@ -52,9 +53,9 @@
5253
epb = new EdgePathBundlingGPUFloydWarshall({
5354
device,
5455
graph,
55-
maxDistortion: 128,
56+
maxDistortion,
5657
edgeWeightFactor,
57-
spannerAlgorithm: ThetaSpanner,
58+
spannerAlgorithm: GreedySpanner,
5859
});
5960
6061
runGPU();

packages/app/src/routes/app/(canvas)/spanner/+page.svelte

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@
3737
const graph = await loadGraph(selectedGraph);
3838
3939
greedy = new GreedySpanner({ graph, device, maxDistortion: 2 });
40-
theta = new ThetaSpanner({ graph, device, maxDistortion: 128 });
40+
theta = new ThetaSpanner({ graph, device, maxDistortion: 2 });
4141
4242
runGPU();
4343
});
4444
4545
async function runGPU() {
4646
if (!greedy || !theta) return;
4747
48-
// console.time('greedy');
49-
// const greedySpanner = await greedy.compute();
50-
// console.timeEnd('greedy');
48+
const graph = await loadGraph(selectedGraph);
49+
50+
greedy = new GreedySpanner({ graph, device, maxDistortion: 2 });
51+
52+
console.time('greedy');
53+
const greedySpanner = await greedy.compute();
54+
console.timeEnd('greedy');
5155
5256
console.time('theta');
5357
const thetaSpanner = await theta.compute();
@@ -64,8 +68,8 @@
6468
// const isSame = JSON.stringify(spannerControl.toJSON()) === JSON.stringify(lol.toJSON());
6569
// console.log({ isSame });
6670
67-
console.log(thetaSpanner);
68-
drawGraph({ ctx: context, graph: thetaSpanner, drawLabels: false });
71+
console.log({ spannerControl });
72+
drawGraph({ ctx: context, graph: greedySpanner, drawLabels: false });
6973
}
7074
</script>
7175

packages/core/src/edge-path-bundling/floyd-warshall/gpu.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ export class EdgePathBundlingGPUFloydWarshall implements EdgePathBundling {
3636
}
3737

3838
async bundle() {
39-
if (!this.#spanner?.graph) {
40-
await this.#spanner.compute();
41-
}
39+
await this.#spanner.compute();
4240

4341
if (this.#maxDistortion !== this.#spanner.maxDistortion) {
4442
console.log('Max distortion changed. Recomputing Spanner');

packages/core/src/shortest-path/floyd-warshall/FloydWarshall.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import shader from './shader.wgsl?raw';
99
export type FloydWarshallParams = {
1010
graph: Graph;
1111
device: GPUDevice;
12-
edgeWeightFactor?: number;
12+
edgeWeightFactor: number;
1313
};
1414

1515
export class FloydWarshall {
1616
#device: GPUDevice;
17+
1718
#graph: Graph;
1819

20+
#edgeWeightFactor: number;
21+
1922
#shaderModule: GPUShaderModule;
2023
#pipeline: GPUComputePipeline;
2124
#bindGroup: GPUBindGroup;
@@ -34,11 +37,13 @@ export class FloydWarshall {
3437

3538
#pathsBufferCache: Map<number, GPUBuffer>;
3639

37-
constructor({ graph, device, edgeWeightFactor = 1 }: FloydWarshallParams) {
40+
constructor({ graph, device, edgeWeightFactor }: FloydWarshallParams) {
3841
this.#device = device;
3942

4043
this.#graph = graph;
4144

45+
this.#edgeWeightFactor = edgeWeightFactor;
46+
4247
this.#pathsBufferCache = new Map();
4348

4449
this.distanceMatrix = new AdjacencyMatrix(graph.nodes.size, Float32Array);
@@ -341,4 +346,8 @@ export class FloydWarshall {
341346
data: this.#uniformsBufferData,
342347
});
343348
}
349+
350+
get edgeWeightFactor() {
351+
return this.#edgeWeightFactor;
352+
}
344353
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
struct Edge {
2+
start: u32,
3+
end: u32,
4+
weight: f32,
5+
}
6+
7+
struct Uniforms {
8+
k: u32,
9+
max_distortion: f32,
10+
}
11+
12+
@group(0) @binding(0) var<uniform> uniforms: Uniforms;
13+
@group(0) @binding(1) var<storage, read_write> distance_matrix: array<f32>;
14+
@group(0) @binding(2) var<storage, read_write> distance_matrix_a: array<f32>;
15+
@group(0) @binding(3) var<storage, read> graph_edges: array<Edge>;
16+
@group(0) @binding(4) var<storage, read_write> spanner_edges: array<u32>;
17+
18+
override node_count: u32;
19+
20+
@compute @workgroup_size(8, 8)
21+
fn compute(
22+
@builtin(global_invocation_id) global_id : vec3<u32>,
23+
) {
24+
let x = global_id.x;
25+
let y = global_id.y;
26+
let n = node_count;
27+
28+
if (x >= n || y >= n) {
29+
let lol = spanner_edges[uniforms.k];
30+
let lol2 = distance_matrix_a[0];
31+
return;
32+
}
33+
34+
let edge = graph_edges[uniforms.k];
35+
36+
var value = min(
37+
distance_matrix_get(x, y),
38+
distance_matrix_get(x, edge.start) + edge.weight + distance_matrix_get(edge.end, y)
39+
);
40+
41+
value = min(
42+
value,
43+
distance_matrix_get(x, edge.end) + edge.weight + distance_matrix_get(edge.start, y)
44+
);
45+
46+
distance_matrix_set(x, y, value);
47+
}
48+
49+
// Matrix getters and setters
50+
51+
fn distance_matrix_get(x: u32, y: u32) -> f32 {
52+
return distance_matrix[get_matrix_index(x, y)];
53+
}
54+
55+
fn distance_matrix_set(x: u32, y: u32, value: f32) {
56+
distance_matrix[get_matrix_index(x, y)] = value;
57+
}
58+
59+
fn get_matrix_index(x: u32, y: u32) -> u32 {
60+
return x * node_count + y;
61+
}

0 commit comments

Comments
 (0)