/ concept-collection / turing-surface
Sign in
concept-collection / turing-surface
154 lines · 5.6 KBBlameHistoryRaw
1/**
2 * The two spherical-harmonic transforms, as external operations the .m can
3 * call: `synth` (spectral -> grid) and `analys` (grid -> spectral).
4 *
5 * numbl needs only their *type rule* in order to lower a call site. It gets
6 * that from a `.mtoc2.js` workspace file — numbl's sanctioned extension point
7 * for a JS-defined builtin (see `mtoc2UserFunctionsByName` in numbl's
8 * LoweringContext). The file is evaluated in a bare CommonJS sandbox with no
9 * imports available, so `transfer` builds numbl `Type` objects as plain
10 * literals, and the grid sizes are baked in by the generator below (a grid
11 * change recompiles anyway).
12 *
13 * The `emit`/`cBody` exports exist only because the loader's contract requires
14 * them; we never emit C. The actual implementation is supplied by the WGSL
15 * backend, which turns each of these calls into an ShtPlan encode.
16 *
17 * Spectral fields are carried as REAL 2 x nlm arrays (row 0 real part, row 1
18 * imaginary), matching the interleaved layout the GPU buffers already use.
19 * The IMEX update is real-linear, so no complex arithmetic is needed.
20 */
22export interface GridSizes {
23 /** Grid points, nlat*nphi. Grid fields are npts x 1 column vectors. */
24 npts: number;
25 /** Spectral coefficients. Spectral fields are 2 x nlm. */
26 nlm: number;
29const numericType = (rows: number, cols: number): string =>
30 `{ kind: "Numeric", elem: "double", isComplex: false, ` +
31 `dims: [${dim(rows)}, ${dim(cols)}], shape: [${rows}, ${cols}], sign: "unknown" }`;
33// numbl's tensorDouble() canonicalizes an extent of 1 to its shared DIM_ONE
34// singleton; mirror that so types compare equal to host-built ones.
35const dim = (n: number): string =>
36 n === 1 ? `{ kind: "exact", value: 1 }` : `{ kind: "exact", value: ${n} }`;
38/** Source for one transform's `.mtoc2.js`. With `multi`, the op maps each of
39 * N inputs to its own output — `[a, b] = synth(x, y)` — so the backend can
40 * run the group as one batched Legendre dispatch (or split it to whatever
41 * lane width the device supports; the syntax promises grouping intent, not
42 * a width). */
43function transformSource(
44 name: string,
45 inRows: number,
46 inCols: number,
47 outRows: number,
48 outCols: number,
49 multi = false,
50): string {
51 return `
52exports.name = ${JSON.stringify(name)};
54exports.transfer = function (argTypes, nargout) {
55 ${
56 multi
57 ? `if (argTypes.length < 1) {
58 throw new Error("${name} takes at least one argument");
59 }
60 if (nargout > 1 && nargout !== argTypes.length) {
61 throw new Error(
62 "${name}: each input produces one output, so " + argTypes.length +
63 " input(s) return " + argTypes.length + " output(s), but " +
64 nargout + " were requested -- write [a, b] = ${name}(x, y)"
65 );
66 }
67 if (nargout <= 1 && argTypes.length !== 1) {
68 throw new Error(
69 "${name}: " + argTypes.length + " inputs produce " + argTypes.length +
70 " outputs -- bind each one: [a, b] = ${name}(x, y)"
71 );
72 }`
73 : `if (argTypes.length !== 1) {
74 throw new Error("${name} takes exactly one argument, got " + argTypes.length);
75 }
76 if (nargout > 1) {
77 throw new Error("${name} returns one value, but " + nargout + " were requested");
78 }`
79 }
80 for (var i = 0; i < argTypes.length; i++) {
81 var a = argTypes[i];
82 if (!a || a.kind !== "Numeric" || a.isComplex) {
83 throw new Error("${name} requires real numeric arrays (argument " + (i + 1) + ")");
84 }
85 var s = a.shape;
86 if (!s || s.length !== 2 || s[0] !== ${inRows} || s[1] !== ${inCols}) {
87 throw new Error(
88 "${name} requires ${inRows}x${inCols} arrays, argument " + (i + 1) +
89 " is " + (s ? s.join("x") : "unknown shape")
90 );
91 }
92 }
93 var out = [];
94 for (var k = 0; k < Math.max(1, nargout); k++) {
95 out.push(${numericType(outRows, outCols)});
96 }
97 return out;
98};
100// Never called: this project executes the IR on WebGPU and emits no C.
101exports.emit = function () {
102 throw new Error("${name}: no C backend (this transform runs on WebGPU)");
103};
104exports.cBody = function () {
105 return "";
106};
107`;
110/**
111 * Workspace files that make `synth` / `analys` / `dtheta` / `dphi` /
112 * `dthetac` / `dphic` resolvable during lowering. `dtheta` and `dphi` (the
113 * surface's first partial derivatives, coefficients -> grid — see
114 * src/sht/deriv.ts) have exactly `synth`'s shape rule: both take spectral
115 * coefficients and produce a grid field. `dthetac` and `dphic` are their
116 * coefficient-space halves alone — the alpha^+/alpha^- shift and the i*m
117 * multiply, spectral -> spectral — which the six-transform Laplace-Beltrami
118 * scheme (docs/reduced-transforms.md) applies twice per
119 * matvec: to the field (gradient side) and to the analysed fluxes
120 * (divergence side, the same shift, not its transpose).
121 */
122export function externalOpFiles(g: GridSizes): { name: string; source: string }[] {
123 return [
124 {
125 name: 'synth.mtoc2.js',
126 source: transformSource('synth', 2, g.nlm, g.npts, 1, true),
127 },
128 {
129 name: 'analys.mtoc2.js',
130 source: transformSource('analys', g.npts, 1, 2, g.nlm, true),
131 },
132 {
133 name: 'dtheta.mtoc2.js',
134 source: transformSource('dtheta', 2, g.nlm, g.npts, 1),
135 },
136 {
137 name: 'dphi.mtoc2.js',
138 source: transformSource('dphi', 2, g.nlm, g.npts, 1),
139 },
140 {
141 name: 'dthetac.mtoc2.js',
142 source: transformSource('dthetac', 2, g.nlm, 2, g.nlm),
143 },
144 {
145 name: 'dphic.mtoc2.js',
146 source: transformSource('dphic', 2, g.nlm, 2, g.nlm),
147 },
148 ];
151/** Names the WGSL backend must implement as GPU encodes rather than kernels. */
152export const EXTERNAL_OPS = new Set([
153 'synth', 'analys', 'dtheta', 'dphi', 'dthetac', 'dphic',
154]);
moveopenescclose