/ concept-collection / turing-surface-cache
concept-collection / turing-surface-cache
turing-surface-cache / src / mgpu / model.ts
469 lines · 18.3 KBBlameHistoryRaw
1/**
2 * A .m model, compiled and running on the GPU.
3 *
4 * A model file is ordinary MATLAB: it defines an `init` function that builds the
5 * initial spectral state and a `step` function that advances it one timestep.
6 * Each is specialized for the current grid and compiled into a ModelPlan, and
7 * both operate on the same state buffers (see HostBuffers).
8 *
9 * Both functions return the new state followed by the grid fields the app
10 * renders, so their signatures say exactly what they produce:
11 *
12 * function [U, V, u, v] = init(noise, a, b)
13 * function [U, V, u, v] = step(U, V, lam, a, b, D1, D2, dt)
14 *
15 * The host supplies the things that are precomputation rather than algorithm:
16 * the grid, the Laplace-Beltrami eigenvalues, the seeded initial noise, and the
17 * parameter values. Each argument is matched to the .m's declared parameter
18 * name, so the file documents its own interface.
19 */
20import { ShtPlan } from '../sht/sht.ts';
21import type { DerivPlan } from '../sht/deriv.ts';
22import { lmIndex, type ShtConfig } from '../sht/layout.ts';
23import { HostBuffers, ModelPlan, type Randnfun3Lambda } from './plan.ts';
24import { MODE_BUFFER } from './randnfun3.ts';
25import { inFunction, inFunctionAsync, inModel } from './errors.ts';
26import { CompiledModel, type Binding } from './compile.ts';
28export interface ModelParams {
29 [key: string]: number;
32export interface GpuModelOptions {
33 device: GPUDevice;
34 sht: ShtPlan;
35 cfg: ShtConfig;
36 /** Model source (.m text). */
37 source: string;
38 /** Parameter names the .m may take as arguments. */
39 paramNames: string[];
40 /** Spectral state names, in order (e.g. ['U', 'V']). */
41 state: string[];
42 /** Grid fields to render, in order (e.g. ['u', 'v']). */
43 view: string[];
44 /**
45 * The surface, as the .m may ask for it: `gx`, `gy`, `gz` are the embedding's
46 * Cartesian coordinates on the grid, and `Gx`, `Gy`, `Gz` the spherical-
47 * harmonic coefficients they were synthesized from. Omitted for a bare unit
48 * sphere, where the .m has no geometry to take.
49 */
50 geometry?: GeometryBuffers;
51 /** Computes `dtheta`/`dphi` for the .m's surface Laplace-Beltrami
52 * correction. Omitted for a bare unit sphere, same as `geometry`. */
53 deriv?: DerivPlan;
54 /**
55 * Iterations of the implicit solve the .m's `for` loop runs. A fixed scalar
56 * rather than a tunable one: the loop is unrolled into the op sequence, so
57 * the count is part of what compiles and changing it recompiles.
58 */
59 niter?: number;
62/** Host-supplied surface fields, in the layout the .m sees them. */
63export interface GeometryBuffers {
64 /** Grid coordinates, npts each. */
65 x: Float32Array;
66 y: Float32Array;
67 z: Float32Array;
68 /** Their spherical-harmonic coefficients, 2 x nlm each. */
69 X: Float32Array;
70 Y: Float32Array;
71 Z: Float32Array;
72 /** Inverse metric quantities (src/geom/metric.ts), grid space, npts each —
73 * the Algorithm-4 (12-transform) Laplace-Beltrami path. */
74 Vtx: Float32Array;
75 Vty: Float32Array;
76 Vtz: Float32Array;
77 Vpx: Float32Array;
78 Vpy: Float32Array;
79 Vpz: Float32Array;
80 /** Flux-form metric weights (src/geom/metric.ts computeFluxMetric), grid
81 * space, npts each — the six-transform Laplace-Beltrami scheme of
82 * docs/reduced-transforms.md. */
83 p1: Float32Array;
84 p2: Float32Array;
85 q2: Float32Array;
86 r: Float32Array;
87 /** The same weights with the round sphere subtracted (Geometry.dp1/dq2/jinv)
88 * — the sphere-split form of the flux divergence, which keeps r off the
89 * round-sphere part of the operator. */
90 dp1: Float32Array;
91 dq2: Float32Array;
92 jinv: Float32Array;
93 /** Mean-J preconditioner scale (Geometry.Jhat): folded into every
94 * setParams upload as the 'jhat' uniform, so a .m that takes jhat is
95 * never left with the zero a missing parameter would default to. An
96 * explicit jhat in the params wins (jhat: 1 pins the plain round-sphere
97 * preconditioner, for A/B). */
98 Jhat: number;
101/** Names the .m may take for the grid coordinates and for their coefficients. */
102export const GEOMETRY_GRID_NAMES = ['gx', 'gy', 'gz'] as const;
103export const GEOMETRY_SPECTRAL_NAMES = ['Gx', 'Gy', 'Gz'] as const;
104/** Names the .m may take for the inverse metric quantities (Algorithm 4). */
105export const METRIC_GRID_NAMES = ['Vtx', 'Vty', 'Vtz', 'Vpx', 'Vpy', 'Vpz'] as const;
106/** Names the .m may take for the flux-form metric weights (six-transform
107 * scheme). A model asks for whichever set its loop uses; both are uploaded. */
108export const FLUX_METRIC_GRID_NAMES = ['p1', 'p2', 'q2', 'r', 'dp1', 'dq2', 'jinv'] as const;
110/** Laplace-Beltrami eigenvalues l(l+1), duplicated across re/im so the array
111 * matches the 2 x nlm spectral layout element for element. */
112export function eigenvalues(cfg: ShtConfig, nlm: number): Float32Array {
113 const lam = new Float32Array(2 * nlm);
114 for (let m = 0; m <= cfg.mmax; m++) {
115 for (let l = m; l <= cfg.lmax; l++) {
116 const i = lmIndex(cfg.lmax, l, m);
117 lam[2 * i] = l * (l + 1);
118 lam[2 * i + 1] = l * (l + 1);
119 }
120 }
121 return lam;
124/**
125 * 1 where l < lmax-2, else 0, duplicated across re/im like `lam`. The
126 * theta/phi derivative recurrences (src/sht/derivCoeffs.ts) cannot exactly
127 * represent a derivative at the top two degrees of the band limit, so the
128 * surface Laplace-Beltrami correction filters them out wherever it
129 * re-differentiates a field (evolving_surface/notes/algos.tex Sec 6,
130 * "Miscellaneous implementation details").
131 */
132export function filterMask(cfg: ShtConfig, nlm: number): Float32Array {
133 const filt = new Float32Array(2 * nlm);
134 for (let m = 0; m <= cfg.mmax; m++) {
135 for (let l = m; l <= cfg.lmax; l++) {
136 const i = lmIndex(cfg.lmax, l, m);
137 const keep = l < cfg.lmax - 2 ? 1 : 0;
138 filt[2 * i] = keep;
139 filt[2 * i + 1] = keep;
140 }
141 }
142 return filt;
145export class GpuModel {
146 readonly paramNames: string[];
147 readonly state: string[];
148 readonly view: string[];
149 readonly npts: number;
150 readonly nlm: number;
152 #device: GPUDevice;
153 #host: HostBuffers;
154 #initPlan: ModelPlan;
155 #stepPlan: ModelPlan;
156 /** Current geometry's mean-J scale; 1 with no geometry (the sphere). */
157 #jhat = 1;
158 #readback: GPUBuffer;
159 /** Scratch holding a copy of the whole spectral state; see snapshotState. */
160 #stash: GPUBuffer;
161 /** Which function wrote the state most recently; see `read`. */
162 #lastRan: 'init' | 'step' = 'init';
163 #stashedRan: 'init' | 'step' = 'init';
164 #destroyed = false;
166 private constructor(init: {
167 device: GPUDevice;
168 host: HostBuffers;
169 initPlan: ModelPlan;
170 stepPlan: ModelPlan;
171 readback: GPUBuffer;
172 stash: GPUBuffer;
173 paramNames: string[];
174 state: string[];
175 view: string[];
176 npts: number;
177 nlm: number;
178 }) {
179 this.#device = init.device;
180 this.#host = init.host;
181 this.#initPlan = init.initPlan;
182 this.#stepPlan = init.stepPlan;
183 this.#readback = init.readback;
184 this.#stash = init.stash;
185 this.paramNames = init.paramNames;
186 this.state = init.state;
187 this.view = init.view;
188 this.npts = init.npts;
189 this.nlm = init.nlm;
190 }
192 static async create(opts: GpuModelOptions): Promise<GpuModel> {
193 const { device, sht, cfg, source, paramNames, state, view, geometry, deriv } = opts;
194 const npts = cfg.nlat * cfg.nphi;
195 const nlm = sht.nlm;
196 const niter = opts.niter ?? 0;
198 // What the .m may ask for by parameter name. Spectral state, the
199 // eigenvalues and the top-mode filter are 2 x nlm; the seeded
200 // perturbation is a grid field.
201 const bindings: Record<string, Binding> = {
202 lam: { kind: 'tensor', shape: [2, nlm] },
203 filt: { kind: 'tensor', shape: [2, nlm] },
204 noise: { kind: 'tensor', shape: [npts, 1] },
205 npts: { kind: 'const', value: npts },
206 nlm: { kind: 'const', value: nlm },
207 niter: { kind: 'const', value: niter },
208 };
209 if (geometry) {
210 for (const g of GEOMETRY_GRID_NAMES) bindings[g] = { kind: 'tensor', shape: [npts, 1] };
211 for (const g of GEOMETRY_SPECTRAL_NAMES) bindings[g] = { kind: 'tensor', shape: [2, nlm] };
212 for (const g of METRIC_GRID_NAMES) bindings[g] = { kind: 'tensor', shape: [npts, 1] };
213 for (const g of FLUX_METRIC_GRID_NAMES) bindings[g] = { kind: 'tensor', shape: [npts, 1] };
214 // Mean-J preconditioner scale (Geometry.Jhat): a uniform, not a const,
215 // so swapping the surface updates it with no recompile. The session
216 // folds the current geometry's value into every setParams call.
217 bindings['jhat'] = { kind: 'param' };
218 // The wavelength of the seeded random field (src/mgpu/randnfun3.ts).
219 // A uniform like jhat, not a const: changing it redraws the field
220 // without recompiling the step.
221 bindings['lam3'] = { kind: 'param' };
222 }
223 for (const s of state) bindings[s] = { kind: 'tensor', shape: [2, nlm] };
224 for (const p of paramNames) bindings[p] = { kind: 'param' };
226 // Parsing belongs to the file, not to either function.
227 const compiled = inModel(() => new CompiledModel(source, bindings, { npts, nlm }));
228 // Both functions return the new state first, then the rendered grid fields.
229 const nargout = state.length + view.length;
230 const initFn = inFunction('init', () => compiled.specialize('init', nargout));
231 const stepFn = inFunction('step', () => compiled.specialize('step', nargout));
232 compiled.finish();
234 // Only the state outputs feed back into the argument buffers; the grid
235 // fields are read for display and then overwritten next call.
236 const feedback = [...state, ...view.map(() => null)];
238 const host = new HostBuffers(device);
239 // The host owns the state and the inputs it uploads, whether or not a given
240 // function happens to take them as arguments — `init` does not read `U`, but
241 // it writes it, and `step` reads it back.
242 for (const s of state) host.ensure(s, 2 * nlm);
243 host.ensure('lam', 2 * nlm);
244 host.ensure('filt', 2 * nlm);
245 host.ensure('noise', npts);
246 if (geometry) {
247 for (const g of GEOMETRY_GRID_NAMES) host.ensure(g, npts);
248 for (const g of GEOMETRY_SPECTRAL_NAMES) host.ensure(g, 2 * nlm);
249 for (const g of METRIC_GRID_NAMES) host.ensure(g, npts);
250 for (const g of FLUX_METRIC_GRID_NAMES) host.ensure(g, npts);
251 }
253 const initPlan = await inFunctionAsync('init', () =>
254 ModelPlan.create(device, sht, { fn: initFn, feedback }, host, deriv),
255 );
256 const stepPlan = await inFunctionAsync('step', () =>
257 ModelPlan.create(device, sht, { fn: stepFn, feedback }, host, deriv),
258 );
260 host.upload('lam', eigenvalues(cfg, nlm));
261 host.upload('filt', filterMask(cfg, nlm));
262 if (geometry) {
263 host.upload('gx', geometry.x);
264 host.upload('gy', geometry.y);
265 host.upload('gz', geometry.z);
266 host.upload('Gx', geometry.X);
267 host.upload('Gy', geometry.Y);
268 host.upload('Gz', geometry.Z);
269 host.upload('Vtx', geometry.Vtx);
270 host.upload('Vty', geometry.Vty);
271 host.upload('Vtz', geometry.Vtz);
272 host.upload('Vpx', geometry.Vpx);
273 host.upload('Vpy', geometry.Vpy);
274 host.upload('Vpz', geometry.Vpz);
275 host.upload('p1', geometry.p1);
276 host.upload('p2', geometry.p2);
277 host.upload('q2', geometry.q2);
278 host.upload('r', geometry.r);
279 host.upload('dp1', geometry.dp1);
280 host.upload('dq2', geometry.dq2);
281 host.upload('jinv', geometry.jinv);
282 }
284 const readback = device.createBuffer({
285 label: 'mgpu-readback',
286 size: 4 * Math.max(npts, 2 * nlm),
287 usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
288 });
289 const stash = device.createBuffer({
290 label: 'mgpu-state-stash',
291 size: 4 * state.length * 2 * nlm,
292 usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
293 });
295 const gpu = new GpuModel({
296 device, host, initPlan, stepPlan, readback, stash,
297 paramNames, state, view, npts, nlm,
298 });
299 if (geometry) gpu.#jhat = geometry.Jhat;
300 return gpu;
301 }
303 setParams(params: ModelParams): void {
304 const merged = { jhat: this.#jhat, ...params };
305 this.#initPlan.setParams(merged);
306 this.#stepPlan.setParams(merged);
307 }
309 /**
310 * Write a host-owned value directly — the spectral state, or one of the input
311 * fields. Lets a test set up an exact initial condition (a single spherical-
312 * harmonic mode, say) instead of going through `init`.
313 */
314 upload(name: string, data: Float32Array): void {
315 this.#host.upload(name, data);
316 }
318 /**
319 * Swap the surface under a running model. The geometry is data, not code —
320 * its shape in the bindings depends only on the grid — so changing it is six
321 * buffer writes and needs no recompile, and the simulation carries straight
322 * on. Only meaningful if the .m took the geometry as an argument.
323 */
324 uploadGeometry(geometry: GeometryBuffers): void {
325 const fields: [string, Float32Array][] = [
326 ['gx', geometry.x], ['gy', geometry.y], ['gz', geometry.z],
327 ['Gx', geometry.X], ['Gy', geometry.Y], ['Gz', geometry.Z],
328 ['Vtx', geometry.Vtx], ['Vty', geometry.Vty], ['Vtz', geometry.Vtz],
329 ['Vpx', geometry.Vpx], ['Vpy', geometry.Vpy], ['Vpz', geometry.Vpz],
330 ['p1', geometry.p1], ['p2', geometry.p2],
331 ['q2', geometry.q2], ['r', geometry.r],
332 ['dp1', geometry.dp1], ['dq2', geometry.dq2], ['jinv', geometry.jinv],
333 ];
334 for (const [name, data] of fields) {
335 if (this.#host.get(name)) this.#host.upload(name, data);
336 }
337 // The new surface's preconditioner scale takes effect on the next
338 // setParams (the session re-applies its params after a swap).
339 this.#jhat = geometry.Jhat;
340 }
342 /** The wavelength this model's `init` asked `randnfun3` for, or null if it
343 * seeds some other way. The session resolves it and draws the modes. */
344 get randnfun3Lambda(): Randnfun3Lambda | null {
345 return this.#initPlan.randnfun3Lambda;
346 }
348 /**
349 * Upload the seeded initial data and run `init`.
350 *
351 * Both inputs are optional in the sense that a .m uses one or the other:
352 * `modes` is the random field's coefficient table for a model that calls
353 * `randnfun3`, `noise` the plain grid field for one that takes `noise`
354 * directly (the analytic test models inject exact initial conditions that
355 * way). Only what the plan actually bound is uploaded.
356 */
357 async init(noise: Float32Array, modes: Float32Array | null): Promise<void> {
358 if (this.#host.get('noise')) this.#host.upload('noise', noise);
359 // Sized to the wavelength, so this may reallocate and rebind.
360 if (modes) this.#initPlan.uploadRandnfun3Table(this.#host, modes);
361 // Submitted in pieces: a fine seed wavelength makes the mode sum long
362 // enough that one submission would stall the browser's compositor.
363 await this.#initPlan.submitYielding('mgpu-init');
364 this.#lastRan = 'init';
365 }
367 /**
368 * Copy the spectral state aside, so a batch of steps can run — to be timed —
369 * and then be undone with restoreState, leaving the simulation exactly where
370 * it was. Only the state is stashed: the grid view fields keep whatever the
371 * batch last wrote until a subsequent step recomputes them, so step before
372 * reading a view after a restore.
373 */
374 snapshotState(): void {
375 this.#stashedRan = this.#lastRan;
376 this.#copyState('save');
377 }
379 restoreState(): void {
380 this.#copyState('restore');
381 this.#lastRan = this.#stashedRan;
382 }
384 #copyState(dir: 'save' | 'restore'): void {
385 // A restore can land after a rebuild destroyed the buffers mid-await;
386 // there is nothing left to protect, so do not submit into destroyed state.
387 if (this.#destroyed) return;
388 const enc = this.#device.createCommandEncoder({ label: `mgpu-state-${dir}` });
389 let offset = 0;
390 for (const name of this.state) {
391 const slot = this.#host.get(name);
392 if (!slot) throw new Error(`state '${name}' has no host buffer`);
393 const bytes = 4 * slot.count;
394 if (dir === 'save') {
395 enc.copyBufferToBuffer(slot.buffer, 0, this.#stash, offset, bytes);
396 } else {
397 enc.copyBufferToBuffer(this.#stash, offset, slot.buffer, 0, bytes);
398 }
399 offset += bytes;
400 }
401 this.#device.queue.submit([enc.finish()]);
402 }
404 /**
405 * Advance `steps` timesteps. Synchronous — this only records commands and
406 * submits them; nothing is read back and nothing is awaited.
407 */
408 step(steps = 1): void {
409 const enc = this.#device.createCommandEncoder({ label: 'mgpu-step' });
410 this.#stepPlan.encodeSteps(enc, steps);
411 this.#device.queue.submit([enc.finish()]);
412 this.#lastRan = 'step';
413 }
415 /**
416 * The buffer currently holding a named value. Grid fields like `u` are
417 * produced by both functions, into separate buffers (only the spectral state
418 * is shared), so this resolves to whichever function ran most recently —
419 * which is what makes the first frame show the initial state rather than an
420 * unwritten buffer.
421 */
422 #locate(name: string): { buffer: GPUBuffer; count: number } | null {
423 const [first, second] =
424 this.#lastRan === 'init'
425 ? [this.#initPlan, this.#stepPlan]
426 : [this.#stepPlan, this.#initPlan];
427 const buffer = first.buffer(name) ?? second.buffer(name);
428 const count = first.elementCount(name) ?? second.elementCount(name);
429 if (!buffer || count === undefined) return null;
430 return { buffer, count };
431 }
433 /** The GPU buffer a named value would be read from right now — for encoding
434 * further GPU work against it (e.g. a display-grid synthesis of the state)
435 * without a CPU round trip. */
436 valueBuffer(name: string): GPUBuffer | null {
437 return this.#locate(name)?.buffer ?? null;
438 }
440 /** Read a named value back to the CPU. The only await in the whole loop. */
441 async read(name: string): Promise<Float32Array> {
442 const located = this.#locate(name);
443 if (!located) {
444 throw new Error(`read: the model has no value named '${name}'`);
445 }
446 const { buffer, count } = located;
447 const enc = this.#device.createCommandEncoder({ label: `mgpu-read-${name}` });
448 enc.copyBufferToBuffer(buffer, 0, this.#readback, 0, 4 * count);
449 this.#device.queue.submit([enc.finish()]);
450 await this.#readback.mapAsync(GPUMapMode.READ, 0, 4 * count);
451 const out = new Float32Array(this.#readback.getMappedRange(0, 4 * count).slice(0));
452 this.#readback.unmap();
453 return out;
454 }
456 /** What the .m compiled to, for display. */
457 describe(): { init: string[]; step: string[] } {
458 return { init: this.#initPlan.describe(), step: this.#stepPlan.describe() };
459 }
461 destroy(): void {
462 this.#destroyed = true;
463 this.#initPlan.destroy();
464 this.#stepPlan.destroy();
465 this.#host.destroy();
466 this.#readback.destroy();
467 this.#stash.destroy();
468 }