/ concept-collection / turing-surface
Sign in
concept-collection / turing-surface
353 lines · 12.8 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 { lmIndex, type ShtConfig } from '../sht/layout.ts';
22import { HostBuffers, ModelPlan } from './plan.ts';
23import { inFunction, inFunctionAsync, inModel } from './errors.ts';
24import { CompiledModel, type Binding } from './compile.ts';
26export interface ModelParams {
27 [key: string]: number;
30export interface GpuModelOptions {
31 device: GPUDevice;
32 sht: ShtPlan;
33 cfg: ShtConfig;
34 /** Model source (.m text). */
35 source: string;
36 /** Parameter names the .m may take as arguments. */
37 paramNames: string[];
38 /** Spectral state names, in order (e.g. ['U', 'V']). */
39 state: string[];
40 /** Grid fields to render, in order (e.g. ['u', 'v']). */
41 view: string[];
42 /**
43 * The surface, as the .m may ask for it: `gx`, `gy`, `gz` are the embedding's
44 * Cartesian coordinates on the grid, and `Gx`, `Gy`, `Gz` the spherical-
45 * harmonic coefficients they were synthesized from. Omitted for a bare unit
46 * sphere, where the .m has no geometry to take.
47 */
48 geometry?: GeometryBuffers;
49 /**
50 * Iterations of the implicit solve the .m's `for` loop runs. A fixed scalar
51 * rather than a tunable one: the loop is unrolled into the op sequence, so
52 * the count is part of what compiles and changing it recompiles.
53 */
54 niter?: number;
57/** Host-supplied surface fields, in the layout the .m sees them. */
58export interface GeometryBuffers {
59 /** Grid coordinates, npts each. */
60 x: Float32Array;
61 y: Float32Array;
62 z: Float32Array;
63 /** Their spherical-harmonic coefficients, 2 x nlm each. */
64 X: Float32Array;
65 Y: Float32Array;
66 Z: Float32Array;
69/** Names the .m may take for the grid coordinates and for their coefficients. */
70export const GEOMETRY_GRID_NAMES = ['gx', 'gy', 'gz'] as const;
71export const GEOMETRY_SPECTRAL_NAMES = ['Gx', 'Gy', 'Gz'] as const;
73/** Laplace-Beltrami eigenvalues l(l+1), duplicated across re/im so the array
74 * matches the 2 x nlm spectral layout element for element. */
75export function eigenvalues(cfg: ShtConfig, nlm: number): Float32Array {
76 const lam = new Float32Array(2 * nlm);
77 for (let m = 0; m <= cfg.mmax; m++) {
78 for (let l = m; l <= cfg.lmax; l++) {
79 const i = lmIndex(cfg.lmax, l, m);
80 lam[2 * i] = l * (l + 1);
81 lam[2 * i + 1] = l * (l + 1);
82 }
83 }
84 return lam;
87export class GpuModel {
88 readonly paramNames: string[];
89 readonly state: string[];
90 readonly view: string[];
91 readonly npts: number;
92 readonly nlm: number;
94 #device: GPUDevice;
95 #host: HostBuffers;
96 #initPlan: ModelPlan;
97 #stepPlan: ModelPlan;
98 #readback: GPUBuffer;
99 /** Scratch holding a copy of the whole spectral state; see snapshotState. */
100 #stash: GPUBuffer;
101 /** Which function wrote the state most recently; see `read`. */
102 #lastRan: 'init' | 'step' = 'init';
103 #stashedRan: 'init' | 'step' = 'init';
104 #destroyed = false;
106 private constructor(init: {
107 device: GPUDevice;
108 host: HostBuffers;
109 initPlan: ModelPlan;
110 stepPlan: ModelPlan;
111 readback: GPUBuffer;
112 stash: GPUBuffer;
113 paramNames: string[];
114 state: string[];
115 view: string[];
116 npts: number;
117 nlm: number;
118 }) {
119 this.#device = init.device;
120 this.#host = init.host;
121 this.#initPlan = init.initPlan;
122 this.#stepPlan = init.stepPlan;
123 this.#readback = init.readback;
124 this.#stash = init.stash;
125 this.paramNames = init.paramNames;
126 this.state = init.state;
127 this.view = init.view;
128 this.npts = init.npts;
129 this.nlm = init.nlm;
130 }
132 static async create(opts: GpuModelOptions): Promise<GpuModel> {
133 const { device, sht, cfg, source, paramNames, state, view, geometry } = opts;
134 const npts = cfg.nlat * cfg.nphi;
135 const nlm = sht.nlm;
136 const niter = opts.niter ?? 0;
138 // What the .m may ask for by parameter name. Spectral state and the
139 // eigenvalues are 2 x nlm; the seeded perturbation is a grid field.
140 const bindings: Record<string, Binding> = {
141 lam: { kind: 'tensor', shape: [2, nlm] },
142 noise: { kind: 'tensor', shape: [npts, 1] },
143 npts: { kind: 'const', value: npts },
144 nlm: { kind: 'const', value: nlm },
145 niter: { kind: 'const', value: niter },
146 };
147 if (geometry) {
148 for (const g of GEOMETRY_GRID_NAMES) bindings[g] = { kind: 'tensor', shape: [npts, 1] };
149 for (const g of GEOMETRY_SPECTRAL_NAMES) bindings[g] = { kind: 'tensor', shape: [2, nlm] };
150 }
151 for (const s of state) bindings[s] = { kind: 'tensor', shape: [2, nlm] };
152 for (const p of paramNames) bindings[p] = { kind: 'param' };
154 // Parsing belongs to the file, not to either function.
155 const compiled = inModel(() => new CompiledModel(source, bindings, { npts, nlm }));
156 // Both functions return the new state first, then the rendered grid fields.
157 const nargout = state.length + view.length;
158 const initFn = inFunction('init', () => compiled.specialize('init', nargout));
159 const stepFn = inFunction('step', () => compiled.specialize('step', nargout));
160 compiled.finish();
162 // Only the state outputs feed back into the argument buffers; the grid
163 // fields are read for display and then overwritten next call.
164 const feedback = [...state, ...view.map(() => null)];
166 const host = new HostBuffers(device);
167 // The host owns the state and the inputs it uploads, whether or not a given
168 // function happens to take them as arguments — `init` does not read `U`, but
169 // it writes it, and `step` reads it back.
170 for (const s of state) host.ensure(s, 2 * nlm);
171 host.ensure('lam', 2 * nlm);
172 host.ensure('noise', npts);
173 if (geometry) {
174 for (const g of GEOMETRY_GRID_NAMES) host.ensure(g, npts);
175 for (const g of GEOMETRY_SPECTRAL_NAMES) host.ensure(g, 2 * nlm);
176 }
178 const initPlan = await inFunctionAsync('init', () =>
179 ModelPlan.create(device, sht, { fn: initFn, feedback }, host),
180 );
181 const stepPlan = await inFunctionAsync('step', () =>
182 ModelPlan.create(device, sht, { fn: stepFn, feedback }, host),
183 );
185 host.upload('lam', eigenvalues(cfg, nlm));
186 if (geometry) {
187 host.upload('gx', geometry.x);
188 host.upload('gy', geometry.y);
189 host.upload('gz', geometry.z);
190 host.upload('Gx', geometry.X);
191 host.upload('Gy', geometry.Y);
192 host.upload('Gz', geometry.Z);
193 }
195 const readback = device.createBuffer({
196 label: 'mgpu-readback',
197 size: 4 * Math.max(npts, 2 * nlm),
198 usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
199 });
200 const stash = device.createBuffer({
201 label: 'mgpu-state-stash',
202 size: 4 * state.length * 2 * nlm,
203 usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
204 });
206 return new GpuModel({
207 device, host, initPlan, stepPlan, readback, stash,
208 paramNames, state, view, npts, nlm,
209 });
210 }
212 setParams(params: ModelParams): void {
213 this.#initPlan.setParams(params);
214 this.#stepPlan.setParams(params);
215 }
217 /**
218 * Write a host-owned value directly — the spectral state, or one of the input
219 * fields. Lets a test set up an exact initial condition (a single spherical-
220 * harmonic mode, say) instead of going through `init`.
221 */
222 upload(name: string, data: Float32Array): void {
223 this.#host.upload(name, data);
224 }
226 /**
227 * Swap the surface under a running model. The geometry is data, not code —
228 * its shape in the bindings depends only on the grid — so changing it is six
229 * buffer writes and needs no recompile, and the simulation carries straight
230 * on. Only meaningful if the .m took the geometry as an argument.
231 */
232 uploadGeometry(geometry: GeometryBuffers): void {
233 const fields: [string, Float32Array][] = [
234 ['gx', geometry.x], ['gy', geometry.y], ['gz', geometry.z],
235 ['Gx', geometry.X], ['Gy', geometry.Y], ['Gz', geometry.Z],
236 ];
237 for (const [name, data] of fields) {
238 if (this.#host.get(name)) this.#host.upload(name, data);
239 }
240 }
242 /** Upload the seeded perturbation and run `init`. */
243 init(noise: Float32Array): void {
244 this.#host.upload('noise', noise);
245 const enc = this.#device.createCommandEncoder({ label: 'mgpu-init' });
246 this.#initPlan.encodeSteps(enc, 1);
247 this.#device.queue.submit([enc.finish()]);
248 this.#lastRan = 'init';
249 }
251 /**
252 * Copy the spectral state aside, so a batch of steps can run — to be timed —
253 * and then be undone with restoreState, leaving the simulation exactly where
254 * it was. Only the state is stashed: the grid view fields keep whatever the
255 * batch last wrote until a subsequent step recomputes them, so step before
256 * reading a view after a restore.
257 */
258 snapshotState(): void {
259 this.#stashedRan = this.#lastRan;
260 this.#copyState('save');
261 }
263 restoreState(): void {
264 this.#copyState('restore');
265 this.#lastRan = this.#stashedRan;
266 }
268 #copyState(dir: 'save' | 'restore'): void {
269 // A restore can land after a rebuild destroyed the buffers mid-await;
270 // there is nothing left to protect, so do not submit into destroyed state.
271 if (this.#destroyed) return;
272 const enc = this.#device.createCommandEncoder({ label: `mgpu-state-${dir}` });
273 let offset = 0;
274 for (const name of this.state) {
275 const slot = this.#host.get(name);
276 if (!slot) throw new Error(`state '${name}' has no host buffer`);
277 const bytes = 4 * slot.count;
278 if (dir === 'save') {
279 enc.copyBufferToBuffer(slot.buffer, 0, this.#stash, offset, bytes);
280 } else {
281 enc.copyBufferToBuffer(this.#stash, offset, slot.buffer, 0, bytes);
282 }
283 offset += bytes;
284 }
285 this.#device.queue.submit([enc.finish()]);
286 }
288 /**
289 * Advance `steps` timesteps. Synchronous — this only records commands and
290 * submits them; nothing is read back and nothing is awaited.
291 */
292 step(steps = 1): void {
293 const enc = this.#device.createCommandEncoder({ label: 'mgpu-step' });
294 this.#stepPlan.encodeSteps(enc, steps);
295 this.#device.queue.submit([enc.finish()]);
296 this.#lastRan = 'step';
297 }
299 /**
300 * The buffer currently holding a named value. Grid fields like `u` are
301 * produced by both functions, into separate buffers (only the spectral state
302 * is shared), so this resolves to whichever function ran most recently —
303 * which is what makes the first frame show the initial state rather than an
304 * unwritten buffer.
305 */
306 #locate(name: string): { buffer: GPUBuffer; count: number } | null {
307 const [first, second] =
308 this.#lastRan === 'init'
309 ? [this.#initPlan, this.#stepPlan]
310 : [this.#stepPlan, this.#initPlan];
311 const buffer = first.buffer(name) ?? second.buffer(name);
312 const count = first.elementCount(name) ?? second.elementCount(name);
313 if (!buffer || count === undefined) return null;
314 return { buffer, count };
315 }
317 /** The GPU buffer a named value would be read from right now — for encoding
318 * further GPU work against it (e.g. a display-grid synthesis of the state)
319 * without a CPU round trip. */
320 valueBuffer(name: string): GPUBuffer | null {
321 return this.#locate(name)?.buffer ?? null;
322 }
324 /** Read a named value back to the CPU. The only await in the whole loop. */
325 async read(name: string): Promise<Float32Array> {
326 const located = this.#locate(name);
327 if (!located) {
328 throw new Error(`read: the model has no value named '${name}'`);
329 }
330 const { buffer, count } = located;
331 const enc = this.#device.createCommandEncoder({ label: `mgpu-read-${name}` });
332 enc.copyBufferToBuffer(buffer, 0, this.#readback, 0, 4 * count);
333 this.#device.queue.submit([enc.finish()]);
334 await this.#readback.mapAsync(GPUMapMode.READ, 0, 4 * count);
335 const out = new Float32Array(this.#readback.getMappedRange(0, 4 * count).slice(0));
336 this.#readback.unmap();
337 return out;
338 }
340 /** What the .m compiled to, for display. */
341 describe(): { init: string[]; step: string[] } {
342 return { init: this.#initPlan.describe(), step: this.#stepPlan.describe() };
343 }
345 destroy(): void {
346 this.#destroyed = true;
347 this.#initPlan.destroy();
348 this.#stepPlan.destroy();
349 this.#host.destroy();
350 this.#readback.destroy();
351 this.#stash.destroy();
352 }
moveopenescclose