1// The WebGPU solvers, by manifest id.
2//
3// A solver whose runtime is "webgpu" is not a MATLAB file, so it cannot be
4// looked up on disk the way the numbl and MATLAB ones are; it is a module
5// in this repository that implements the small interface below against the
6// TypeScript form of the problem (src/problems/laplace2d/problem.ts). One
7// instance is built per id and kept: the shader compilation and pipeline
8// creation it does belong outside the timed runs.
10import type { Laplace2dProblem } from "../problems/laplace2d/problem";
11import { MfsGpu } from "./mfs-gpu/solver";
13export interface WebgpuSolver {
14 /** One full solve at resolution n, ending when the device has finished
15 * and the values have been read back. */
16 run(
17 prob: Laplace2dProblem,
18 n: number,
19 wantGrid: boolean
20 ): Promise<{ uEval: Float64Array; uGrid: Float64Array | null }>;
21 /** The adapter and how WebGPU was reached, for the result file. */
22 readonly adapter: string;
23 readonly via: string;
24}
26const factories: Record<string, () => Promise<WebgpuSolver>> = {
27 "mfs-gpu": () => MfsGpu.create(),
28};
30const built = new Map<string, Promise<WebgpuSolver>>();
32export function isWebgpuSolver(id: string): boolean {
33 return id in factories;
34}
36export function getWebgpuSolver(id: string): Promise<WebgpuSolver> {
37 const make = factories[id];
38 if (!make) throw new Error(`no WebGPU solver named ${id}`);
39 let p = built.get(id);
40 if (!p) {
41 p = make();
42 built.set(id, p);
43 }
44 return p;
45}