/ concept-collection / turing-surface
Sign in
concept-collection / turing-surface
428 lines · 15.7 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 } from './plan.ts';
24import { inFunction, inFunctionAsync, inModel } from './errors.ts';
25import { CompiledModel, type Binding } from './compile.ts';
26import { modelLibs } from './libs.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 Vtx: Float32Array;
74 Vty: Float32Array;
75 Vtz: Float32Array;
76 Vpx: Float32Array;
77 Vpy: Float32Array;
78 Vpz: Float32Array;
81/** Names the .m may take for the grid coordinates and for their coefficients. */
82export const GEOMETRY_GRID_NAMES = ['gx', 'gy', 'gz'] as const;
83export const GEOMETRY_SPECTRAL_NAMES = ['Gx', 'Gy', 'Gz'] as const;
84/** Names the .m may take for the inverse metric quantities. */
85export const METRIC_GRID_NAMES = ['Vtx', 'Vty', 'Vtz', 'Vpx', 'Vpy', 'Vpz'] as const;
87/** Laplace-Beltrami eigenvalues l(l+1), duplicated across re/im so the array
88 * matches the 2 x nlm spectral layout element for element. */
89export function eigenvalues(cfg: ShtConfig, nlm: number): Float32Array {
90 const lam = new Float32Array(2 * nlm);
91 for (let m = 0; m <= cfg.mmax; m++) {
92 for (let l = m; l <= cfg.lmax; l++) {
93 const i = lmIndex(cfg.lmax, l, m);
94 lam[2 * i] = l * (l + 1);
95 lam[2 * i + 1] = l * (l + 1);
96 }
97 }
98 return lam;
101/**
102 * 1 where l < lmax-2, else 0, duplicated across re/im like `lam`. The
103 * theta/phi derivative recurrences (src/sht/derivCoeffs.ts) cannot exactly
104 * represent a derivative at the top two degrees of the band limit, so the
105 * surface Laplace-Beltrami correction filters them out wherever it
106 * re-differentiates a field (evolving_surface/notes/algos.tex Sec 6,
107 * "Miscellaneous implementation details").
108 */
109export function filterMask(cfg: ShtConfig, nlm: number): Float32Array {
110 const filt = new Float32Array(2 * nlm);
111 for (let m = 0; m <= cfg.mmax; m++) {
112 for (let l = m; l <= cfg.lmax; l++) {
113 const i = lmIndex(cfg.lmax, l, m);
114 const keep = l < cfg.lmax - 2 ? 1 : 0;
115 filt[2 * i] = keep;
116 filt[2 * i + 1] = keep;
117 }
118 }
119 return filt;
122/**
123 * Inner-product weights for the half-spectrum layout: 1 at m = 0, 2 at
124 * m > 0, duplicated across re/im like `lam`. The 2 x nlm state stores only
125 * m >= 0 of a real field (the m < 0 coefficients are conjugates), so the
126 * true L2 inner product on the sphere is sum(wlm .* a .* b) — which is what
127 * a Krylov solver's `dot` calls should compute if its scalars are to mean
128 * what they mean in the analysis.
129 */
130export function weightMask(cfg: ShtConfig, nlm: number): Float32Array {
131 const wlm = new Float32Array(2 * nlm);
132 for (let m = 0; m <= cfg.mmax; m++) {
133 for (let l = m; l <= cfg.lmax; l++) {
134 const i = lmIndex(cfg.lmax, l, m);
135 const w = m === 0 ? 1 : 2;
136 wlm[2 * i] = w;
137 wlm[2 * i + 1] = w;
138 }
139 }
140 return wlm;
143export class GpuModel {
144 readonly paramNames: string[];
145 readonly state: string[];
146 readonly view: string[];
147 readonly npts: number;
148 readonly nlm: number;
150 #device: GPUDevice;
151 #host: HostBuffers;
152 #initPlan: ModelPlan;
153 #stepPlan: ModelPlan;
154 #readback: GPUBuffer;
155 /** Scratch holding a copy of the whole spectral state; see snapshotState. */
156 #stash: GPUBuffer;
157 /** Which function wrote the state most recently; see `read`. */
158 #lastRan: 'init' | 'step' = 'init';
159 #stashedRan: 'init' | 'step' = 'init';
160 #destroyed = false;
162 private constructor(init: {
163 device: GPUDevice;
164 host: HostBuffers;
165 initPlan: ModelPlan;
166 stepPlan: ModelPlan;
167 readback: GPUBuffer;
168 stash: GPUBuffer;
169 paramNames: string[];
170 state: string[];
171 view: string[];
172 npts: number;
173 nlm: number;
174 }) {
175 this.#device = init.device;
176 this.#host = init.host;
177 this.#initPlan = init.initPlan;
178 this.#stepPlan = init.stepPlan;
179 this.#readback = init.readback;
180 this.#stash = init.stash;
181 this.paramNames = init.paramNames;
182 this.state = init.state;
183 this.view = init.view;
184 this.npts = init.npts;
185 this.nlm = init.nlm;
186 }
188 static async create(opts: GpuModelOptions): Promise<GpuModel> {
189 const { device, sht, cfg, source, paramNames, state, view, geometry, deriv } = opts;
190 const npts = cfg.nlat * cfg.nphi;
191 const nlm = sht.nlm;
192 const niter = opts.niter ?? 0;
194 // What the .m may ask for by parameter name. Spectral state, the
195 // eigenvalues and the top-mode filter are 2 x nlm; the seeded
196 // perturbation is a grid field.
197 const bindings: Record<string, Binding> = {
198 lam: { kind: 'tensor', shape: [2, nlm] },
199 filt: { kind: 'tensor', shape: [2, nlm] },
200 wlm: { kind: 'tensor', shape: [2, nlm] },
201 noise: { kind: 'tensor', shape: [npts, 1] },
202 npts: { kind: 'const', value: npts },
203 nlm: { kind: 'const', value: nlm },
204 niter: { kind: 'const', value: niter },
205 };
206 if (geometry) {
207 for (const g of GEOMETRY_GRID_NAMES) bindings[g] = { kind: 'tensor', shape: [npts, 1] };
208 for (const g of GEOMETRY_SPECTRAL_NAMES) bindings[g] = { kind: 'tensor', shape: [2, nlm] };
209 for (const g of METRIC_GRID_NAMES) bindings[g] = { kind: 'tensor', shape: [npts, 1] };
210 }
211 for (const s of state) bindings[s] = { kind: 'tensor', shape: [2, nlm] };
212 for (const p of paramNames) bindings[p] = { kind: 'param' };
214 // Parsing belongs to the file, not to either function.
215 const compiled = inModel(
216 () => new CompiledModel(source, bindings, { npts, nlm }, 'model.m', modelLibs),
217 );
218 // Both functions return the new state first, then the rendered grid fields.
219 const nargout = state.length + view.length;
220 const initFn = inFunction('init', () => compiled.specialize('init', nargout));
221 const stepFn = inFunction('step', () => compiled.specialize('step', nargout));
222 compiled.finish();
224 // Only the state outputs feed back into the argument buffers; the grid
225 // fields are read for display and then overwritten next call.
226 const feedback = [...state, ...view.map(() => null)];
228 const host = new HostBuffers(device);
229 // The host owns the state and the inputs it uploads, whether or not a given
230 // function happens to take them as arguments — `init` does not read `U`, but
231 // it writes it, and `step` reads it back.
232 for (const s of state) host.ensure(s, 2 * nlm);
233 host.ensure('lam', 2 * nlm);
234 host.ensure('filt', 2 * nlm);
235 host.ensure('wlm', 2 * nlm);
236 host.ensure('noise', npts);
237 if (geometry) {
238 for (const g of GEOMETRY_GRID_NAMES) host.ensure(g, npts);
239 for (const g of GEOMETRY_SPECTRAL_NAMES) host.ensure(g, 2 * nlm);
240 for (const g of METRIC_GRID_NAMES) host.ensure(g, npts);
241 }
243 const initPlan = await inFunctionAsync('init', () =>
244 ModelPlan.create(device, sht, { fn: initFn, feedback }, host, deriv),
245 );
246 const stepPlan = await inFunctionAsync('step', () =>
247 ModelPlan.create(device, sht, { fn: stepFn, feedback }, host, deriv),
248 );
250 host.upload('lam', eigenvalues(cfg, nlm));
251 host.upload('filt', filterMask(cfg, nlm));
252 host.upload('wlm', weightMask(cfg, nlm));
253 if (geometry) {
254 host.upload('gx', geometry.x);
255 host.upload('gy', geometry.y);
256 host.upload('gz', geometry.z);
257 host.upload('Gx', geometry.X);
258 host.upload('Gy', geometry.Y);
259 host.upload('Gz', geometry.Z);
260 host.upload('Vtx', geometry.Vtx);
261 host.upload('Vty', geometry.Vty);
262 host.upload('Vtz', geometry.Vtz);
263 host.upload('Vpx', geometry.Vpx);
264 host.upload('Vpy', geometry.Vpy);
265 host.upload('Vpz', geometry.Vpz);
266 }
268 const readback = device.createBuffer({
269 label: 'mgpu-readback',
270 size: 4 * Math.max(npts, 2 * nlm),
271 usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
272 });
273 const stash = device.createBuffer({
274 label: 'mgpu-state-stash',
275 size: 4 * state.length * 2 * nlm,
276 usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
277 });
279 return new GpuModel({
280 device, host, initPlan, stepPlan, readback, stash,
281 paramNames, state, view, npts, nlm,
282 });
283 }
285 setParams(params: ModelParams): void {
286 this.#initPlan.setParams(params);
287 this.#stepPlan.setParams(params);
288 }
290 /**
291 * Write a host-owned value directly — the spectral state, or one of the input
292 * fields. Lets a test set up an exact initial condition (a single spherical-
293 * harmonic mode, say) instead of going through `init`.
294 */
295 upload(name: string, data: Float32Array): void {
296 this.#host.upload(name, data);
297 }
299 /**
300 * Swap the surface under a running model. The geometry is data, not code —
301 * its shape in the bindings depends only on the grid — so changing it is six
302 * buffer writes and needs no recompile, and the simulation carries straight
303 * on. Only meaningful if the .m took the geometry as an argument.
304 */
305 uploadGeometry(geometry: GeometryBuffers): void {
306 const fields: [string, Float32Array][] = [
307 ['gx', geometry.x], ['gy', geometry.y], ['gz', geometry.z],
308 ['Gx', geometry.X], ['Gy', geometry.Y], ['Gz', geometry.Z],
309 ['Vtx', geometry.Vtx], ['Vty', geometry.Vty], ['Vtz', geometry.Vtz],
310 ['Vpx', geometry.Vpx], ['Vpy', geometry.Vpy], ['Vpz', geometry.Vpz],
311 ];
312 for (const [name, data] of fields) {
313 if (this.#host.get(name)) this.#host.upload(name, data);
314 }
315 }
317 /** Upload the seeded perturbation and run `init`. */
318 init(noise: Float32Array): void {
319 this.#host.upload('noise', noise);
320 const enc = this.#device.createCommandEncoder({ label: 'mgpu-init' });
321 this.#initPlan.encodeSteps(enc, 1);
322 this.#device.queue.submit([enc.finish()]);
323 this.#lastRan = 'init';
324 }
326 /**
327 * Copy the spectral state aside, so a batch of steps can run — to be timed —
328 * and then be undone with restoreState, leaving the simulation exactly where
329 * it was. Only the state is stashed: the grid view fields keep whatever the
330 * batch last wrote until a subsequent step recomputes them, so step before
331 * reading a view after a restore.
332 */
333 snapshotState(): void {
334 this.#stashedRan = this.#lastRan;
335 this.#copyState('save');
336 }
338 restoreState(): void {
339 this.#copyState('restore');
340 this.#lastRan = this.#stashedRan;
341 }
343 #copyState(dir: 'save' | 'restore'): void {
344 // A restore can land after a rebuild destroyed the buffers mid-await;
345 // there is nothing left to protect, so do not submit into destroyed state.
346 if (this.#destroyed) return;
347 const enc = this.#device.createCommandEncoder({ label: `mgpu-state-${dir}` });
348 let offset = 0;
349 for (const name of this.state) {
350 const slot = this.#host.get(name);
351 if (!slot) throw new Error(`state '${name}' has no host buffer`);
352 const bytes = 4 * slot.count;
353 if (dir === 'save') {
354 enc.copyBufferToBuffer(slot.buffer, 0, this.#stash, offset, bytes);
355 } else {
356 enc.copyBufferToBuffer(this.#stash, offset, slot.buffer, 0, bytes);
357 }
358 offset += bytes;
359 }
360 this.#device.queue.submit([enc.finish()]);
361 }
363 /**
364 * Advance `steps` timesteps. Synchronous — this only records commands and
365 * submits them; nothing is read back and nothing is awaited.
366 */
367 step(steps = 1): void {
368 const enc = this.#device.createCommandEncoder({ label: 'mgpu-step' });
369 this.#stepPlan.encodeSteps(enc, steps);
370 this.#device.queue.submit([enc.finish()]);
371 this.#lastRan = 'step';
372 }
374 /**
375 * The buffer currently holding a named value. Grid fields like `u` are
376 * produced by both functions, into separate buffers (only the spectral state
377 * is shared), so this resolves to whichever function ran most recently —
378 * which is what makes the first frame show the initial state rather than an
379 * unwritten buffer.
380 */
381 #locate(name: string): { buffer: GPUBuffer; count: number } | null {
382 const [first, second] =
383 this.#lastRan === 'init'
384 ? [this.#initPlan, this.#stepPlan]
385 : [this.#stepPlan, this.#initPlan];
386 const buffer = first.buffer(name) ?? second.buffer(name);
387 const count = first.elementCount(name) ?? second.elementCount(name);
388 if (!buffer || count === undefined) return null;
389 return { buffer, count };
390 }
392 /** The GPU buffer a named value would be read from right now — for encoding
393 * further GPU work against it (e.g. a display-grid synthesis of the state)
394 * without a CPU round trip. */
395 valueBuffer(name: string): GPUBuffer | null {
396 return this.#locate(name)?.buffer ?? null;
397 }
399 /** Read a named value back to the CPU. The only await in the whole loop. */
400 async read(name: string): Promise<Float32Array> {
401 const located = this.#locate(name);
402 if (!located) {
403 throw new Error(`read: the model has no value named '${name}'`);
404 }
405 const { buffer, count } = located;
406 const enc = this.#device.createCommandEncoder({ label: `mgpu-read-${name}` });
407 enc.copyBufferToBuffer(buffer, 0, this.#readback, 0, 4 * count);
408 this.#device.queue.submit([enc.finish()]);
409 await this.#readback.mapAsync(GPUMapMode.READ, 0, 4 * count);
410 const out = new Float32Array(this.#readback.getMappedRange(0, 4 * count).slice(0));
411 this.#readback.unmap();
412 return out;
413 }
415 /** What the .m compiled to, for display. */
416 describe(): { init: string[]; step: string[] } {
417 return { init: this.#initPlan.describe(), step: this.#stepPlan.describe() };
418 }
420 destroy(): void {
421 this.#destroyed = true;
422 this.#initPlan.destroy();
423 this.#stepPlan.destroy();
424 this.#host.destroy();
425 this.#readback.destroy();
426 this.#stash.destroy();
427 }
moveopenescclose