/ concept-collection / turing-surface
Sign in
concept-collection / turing-surface
352 lines · 11.9 KBBlameHistoryRaw
1/**
2 * WGSL Legendre-transform kernels, modeled on leg_m_kernel / ileg_m_kernel
3 * in SHT/cuda_legendre.gen.cu (non-Ishioka fp32 path: SHTNS disables the
4 * Ishioka recurrence for fp32 because it loses too much accuracy).
5 *
6 * Synthesis: F_m(theta_i) = sum_{l=m..lmax} Q_lm * ytilde_l^m(theta_i)
7 * - one thread per latitude, one workgroup row per m (workgroup_id.y).
8 * Analysis: Q_lm = sum_i w_i * G_m(theta_i) * ytilde_l^m(theta_i)
9 * - one workgroup per m; threads own latitudes (strided); per-l pair
10 * workgroup tree reduction (portable stand-in for the CUDA warp
11 * shuffles).
12 *
13 * The associated Legendre functions are generated on the fly by the
14 * standard 3-term recurrence over l (coefficients a,b precomputed on the
15 * host in f64), with the SHTNS fp32 rescaling scheme for sin(theta)^m
16 * underflow (see common.ts).
17 */
18import { RESCALE_WGSL } from './common.ts';
20export interface LegParams {
21 lmax: number;
22 mmax: number;
23 nlat: number;
24 wgSynth: number; // workgroup size for synthesis (threads over latitude)
25 wgAnalys: number; // workgroup size for analysis (power of two)
26 /** Use subgroup reductions in the analysis kernel (needs the `subgroups` feature). */
27 subgroups?: boolean;
28 /** l-pairs accumulated before the span is reduced (subgroup path only). */
29 spanPairs?: number;
30 /**
31 * Fold north/south latitude pairs onto one recurrence (halves Legendre work).
32 * Needs an equator-symmetric grid with even nlat, which the Gauss grid is.
33 */
34 parity?: boolean;
37const BINDINGS = /* wgsl */ `
38@group(0) @binding(0) var<storage, read> ab: array<vec2f>; // (a_l^m, b_l^m) per lm
39@group(0) @binding(1) var<storage, read> amm: array<f32>; // seed per m
40@group(0) @binding(2) var<storage, read> ctstw: array<f32>; // [ct | st | w], each NLAT
41`;
43export function legSynthWGSL(p: LegParams): string {
44 const half = p.parity === true;
45 return /* wgsl */ `
46${RESCALE_WGSL}
47const LMAX: u32 = ${p.lmax}u;
48const NLAT: u32 = ${p.nlat}u;
49const NLAT_2: u32 = ${p.nlat / 2}u;
50${BINDINGS}
51@group(0) @binding(3) var<storage, read> qlm: array<vec2f>;
52@group(0) @binding(4) var<storage, read_write> fm: array<vec2f>; // [(m)*NLAT + ilat]
54@compute @workgroup_size(${p.wgSynth})
55fn leg_synth(@builtin(global_invocation_id) gid: vec3u,
56 @builtin(workgroup_id) wid: vec3u) {
57 let ilat = gid.x;
58 let m = wid.y;
59 if (ilat >= ${half ? 'NLAT_2' : 'NLAT'}) { return; }
61 let ct = ctstw[ilat];
62 let st = ctstw[NLAT + ilat];
63 let base = m * (LMAX + 1u) - (m * (m - 1u)) / 2u; // lm index of (l=m, m)
65 var seed = sinpow_rescaled(st, m);
66 var y0 = seed.y0 * amm[m];
67 var ny = seed.ny;
68 var y1: f32 = 0.0;
69 if (m < LMAX) {
70 y1 = ab[base + 1u].x * ct * y0;
71 }
73${
74 half
75 ? ` // Parity folding: ytilde_l^m(-x) = (-1)^(l-m) ytilde_l^m(x) and the Gauss
76 // grid is symmetric, so one recurrence serves a north/south pair. y0 always
77 // carries even (l-m) and y1 odd, so summing them apart gives
78 // F_m(north) = accE + accO, F_m(south) = accE - accO.
79 var accE = vec2f(0.0);
80 var accO = vec2f(0.0);`
81 : ` var acc = vec2f(0.0);`
82 }
83 var l = m;
84 loop {
85 if (ny == 0) {
86${
87 half
88 ? ` accE += y0 * qlm[base + (l - m)];
89 if (l + 1u <= LMAX) {
90 accO += y1 * qlm[base + (l + 1u - m)];
91 }`
92 : ` acc += y0 * qlm[base + (l - m)];
93 if (l + 1u <= LMAX) {
94 acc += y1 * qlm[base + (l + 1u - m)];
95 }`
96 }
97 } else if (abs(y0) > RESCALE_THR) {
98 ny += 1;
99 y0 *= INV_SCALE;
100 y1 *= INV_SCALE;
101 }
102 if (l + 2u > LMAX) { break; }
103 // Advance (y_l, y_{l+1}) to (y_{l+2}, y_{l+3}).
104 //
105 // Written in exactly the shape leg_analys uses below — both coefficients
106 // fetched unconditionally, the new y0 carried in a temporary rather than
107 // assigned and then read back by the y1 update. The shorter form,
108 //
109 // let c0 = ab[base + (l + 2u - m)];
110 // y0 = c0.x * ct * y1 + c0.y * y0;
111 // if (l + 3u <= LMAX) { ... y1 = c1.x * ct * y0 + c1.y * y1; }
112 //
113 // says the same thing and is what this was, but NVIDIA's Vulkan compiler
114 // (driver 590.48, Blackwell) mis-compiles it: c0 reads as (0, 0) on the
115 // first iteration, so y_{l+2} comes out exactly zero and every later term
116 // follows a different solution of the recurrence, reaching ~1e11 by l = 63.
117 // leg_analys, doing the same arithmetic in this shape, was correct on the
118 // same driver. See scripts/diagnose-leg.ts, which is how that was found.
119 let a0 = ab[base + (l + 2u - m)];
120 var a1 = vec2f(0.0);
121 if (l + 3u <= LMAX) {
122 a1 = ab[base + (l + 3u - m)];
123 }
124 let t0 = a0.x * ct * y1 + a0.y * y0;
125 y1 = a1.x * ct * t0 + a1.y * y1;
126 y0 = t0;
127 l += 2u;
128 }
129${
130 half
131 ? ` fm[m * NLAT + ilat] = accE + accO;
132 fm[m * NLAT + (NLAT - 1u - ilat)] = accE - accO;`
133 : ` fm[m * NLAT + ilat] = acc;`
134 }
136`;
139export function legAnalysWGSL(p: LegParams): string {
140 const half = p.parity === true;
141 // parity folding leaves only the northern half of the grid to walk
142 const K = Math.ceil((half ? p.nlat / 2 : p.nlat) / p.wgAnalys);
143 // With subgroups, the per-l-pair reduction is one subgroupAdd plus a combine
144 // across subgroups: 2 barriers instead of 1 + log2(wgAnalys). This is what
145 // SHTNS's CUDA kernel does with warp shuffles. `red` then holds one partial
146 // per subgroup; WebGPU guarantees subgroup size >= 4, so wgAnalys/4 is a safe
147 // upper bound on how many there can be.
148 const sg = p.subgroups === true;
149 // Reduce once per span of l-pairs rather than once per pair. The l-loop is
150 // serial, so its barriers are the critical path: at lmax=127 the m=0
151 // workgroup paid 2 of them 64 times over. SHTNS amortizes the same way
152 // (LSPAN_A = 16, or 32 for fp32), staging a whole span before reducing.
153 // Partials for the span live in registers and are combined in one batch.
154 const nsubMax = Math.max(1, p.wgAnalys / 4); // WebGPU guarantees subgroup size >= 4
155 // 16 pairs = 32 l-values, which is what SHTNS uses for fp32 (LSPAN_A). Clamped
156 // so `red` stays within 8 KB of workgroup storage, since nsubMax has to assume
157 // the smallest legal subgroup and would otherwise oversize it badly.
158 const pairs = sg
159 ? Math.max(1, Math.min(p.spanPairs ?? 16, Math.floor(8192 / (nsubMax * 16))))
160 : 1;
161 const redLen = sg ? nsubMax * pairs : p.wgAnalys;
162 return /* wgsl */ `${sg ? 'enable subgroups;\n' : ''}
163${RESCALE_WGSL}
164const LMAX: u32 = ${p.lmax}u;
165const NLAT: u32 = ${p.nlat}u;
166const WG: u32 = ${p.wgAnalys}u;
167const K: u32 = ${K}u;
168const NLAT_2: u32 = ${p.nlat / 2}u;
169const PAIRS: u32 = ${pairs}u;
170${BINDINGS}
171@group(0) @binding(3) var<storage, read> fm: array<vec2f>; // [(m)*NLAT + ilat]
172@group(0) @binding(4) var<storage, read_write> qout: array<vec2f>;
174var<workgroup> red: array<vec4f, ${redLen}>;
176@compute @workgroup_size(${p.wgAnalys})
177fn leg_analys(@builtin(local_invocation_id) lid3: vec3u,
178 @builtin(workgroup_id) wid: vec3u${
179 sg
180 ? ',\n @builtin(subgroup_size) sgSize: u32,\n @builtin(subgroup_invocation_id) sgLane: u32'
181 : ''
182 }) {
183 let lid = lid3.x;
184 let m = wid.x;
185 let base = m * (LMAX + 1u) - (m * (m - 1u)) / 2u;
187 // per-thread recurrence state for K latitudes
188 var y0v: array<f32, ${K}>;
189 var y1v: array<f32, ${K}>;
190 var nyv: array<i32, ${K}>;
191 var ctv: array<f32, ${K}>;
192${
193 half
194 ? ` // Transpose of the synthesis folding: splitting the latitude sum into
195 // hemispheres gives Q_lm = sum_north w_i * ytilde * (G_north +/- G_south),
196 // with + for even (l-m) and - for odd -- which the loop already routes
197 // through y0 and y1 respectively.
198 var wpv: array<vec2f, ${K}>;
199 var wmv: array<vec2f, ${K}>;`
200 : ` var wfv: array<vec2f, ${K}>;`
201 }
203 for (var k = 0u; k < K; k++) {
204 let lat = lid + k * WG;
205 var ct: f32 = 0.0;
206 var st: f32 = 0.0;
207${
208 half
209 ? ` var wp = vec2f(0.0);
210 var wm = vec2f(0.0);
211 if (lat < NLAT_2) {
212 ct = ctstw[lat];
213 st = ctstw[NLAT + lat];
214 let w = ctstw[2u * NLAT + lat]; // Gauss weight (incl. 2*pi/nphi)
215 let gN = fm[m * NLAT + lat];
216 let gS = fm[m * NLAT + (NLAT - 1u - lat)];
217 wp = (gN + gS) * w;
218 wm = (gN - gS) * w;
219 }`
220 : ` var wf = vec2f(0.0);
221 if (lat < NLAT) {
222 ct = ctstw[lat];
223 st = ctstw[NLAT + lat];
224 wf = fm[m * NLAT + lat] * ctstw[2u * NLAT + lat]; // Gauss weight (incl. 2*pi/nphi)
225 }`
226 }
227 ctv[k] = ct;
228 let seed = sinpow_rescaled(st, m);
229 y0v[k] = seed.y0 * amm[m];
230 nyv[k] = seed.ny;
231 y1v[k] = 0.0;
232 if (m < LMAX) {
233 y1v[k] = ab[base + 1u].x * ct * y0v[k];
234 }
235${half ? ' wpv[k] = wp;\n wmv[k] = wm;' : ' wfv[k] = wf;'}
236 }
238 var l = m;
239${
240 sg
241 ? ` // Accumulate up to PAIRS l-pairs into registers, then reduce the whole span
242 // at once: 2 barriers per span instead of 2 per pair.
243 loop {
244 let lstart = l;
245 var npairs = 0u;
246 var last = false;
247 let sub = lid / sgSize;
248 for (var jj = 0u; jj < PAIRS; jj++) {
249 var c0 = vec2f(0.0);
250 var c1 = vec2f(0.0);
251 for (var k = 0u; k < K; k++) {
252 if (nyv[k] == 0) {
253${
254 half
255 ? ` c0 += wpv[k] * y0v[k]; // even (l-m): hemispheres add
256 c1 += wmv[k] * y1v[k]; // odd (l-m): hemispheres subtract`
257 : ` c0 += wfv[k] * y0v[k];
258 c1 += wfv[k] * y1v[k];`
259 }
260 } else if (abs(y0v[k]) > RESCALE_THR) {
261 nyv[k] += 1;
262 y0v[k] *= INV_SCALE;
263 y1v[k] *= INV_SCALE;
264 }
265 }
266 // subgroupAdd needs no barrier, so the per-subgroup partial can go
267 // straight to shared memory; only the cross-subgroup combine below has
268 // to wait, and it waits once for the whole span.
269 let part = subgroupAdd(vec4f(c0, c1));
270 if (sgLane == 0u) { red[sub * PAIRS + jj] = part; }
271 npairs = jj + 1u;
272 if (l + 2u > LMAX) { last = true; break; }
273 let a0 = ab[base + (l + 2u - m)];
274 var a1 = vec2f(0.0);
275 if (l + 3u <= LMAX) {
276 a1 = ab[base + (l + 3u - m)];
277 }
278 for (var k = 0u; k < K; k++) {
279 let t0 = a0.x * ctv[k] * y1v[k] + a0.y * y0v[k];
280 y0v[k] = t0;
281 y1v[k] = a1.x * ctv[k] * t0 + a1.y * y1v[k];
282 }
283 l += 2u;
284 }
286 workgroupBarrier();
287 if (lid == 0u) {
288 let nsub = (WG + sgSize - 1u) / sgSize;
289 for (var jj = 0u; jj < npairs; jj++) {
290 var tot = vec4f(0.0);
291 for (var i = 0u; i < nsub; i++) { tot += red[i * PAIRS + jj]; }
292 let ll = lstart + 2u * jj;
293 qout[base + (ll - m)] = tot.xy;
294 if (ll + 1u <= LMAX) {
295 qout[base + (ll + 1u - m)] = tot.zw;
296 }
297 }
298 }
299 workgroupBarrier(); // red is reused by the next span
301 if (last) { break; }
302 }`
303 : ` loop {
304 var c0 = vec2f(0.0);
305 var c1 = vec2f(0.0);
306 for (var k = 0u; k < K; k++) {
307 if (nyv[k] == 0) {
308${
309 half
310 ? ` c0 += wpv[k] * y0v[k]; // even (l-m): hemispheres add
311 c1 += wmv[k] * y1v[k]; // odd (l-m): hemispheres subtract`
312 : ` c0 += wfv[k] * y0v[k];
313 c1 += wfv[k] * y1v[k];`
314 }
315 } else if (abs(y0v[k]) > RESCALE_THR) {
316 nyv[k] += 1;
317 y0v[k] *= INV_SCALE;
318 y1v[k] *= INV_SCALE;
319 }
320 }
321 // workgroup tree reduction of (c0, c1)
322 red[lid] = vec4f(c0, c1);
323 workgroupBarrier();
324 var s = WG / 2u;
325 while (s > 0u) {
326 if (lid < s) { red[lid] += red[lid + s]; }
327 workgroupBarrier();
328 s = s >> 1u;
329 }
330 if (lid == 0u) {
331 qout[base + (l - m)] = red[0].xy;
332 if (l + 1u <= LMAX) {
333 qout[base + (l + 1u - m)] = red[0].zw;
334 }
335 }
336 if (l + 2u > LMAX) { break; }
337 let a0 = ab[base + (l + 2u - m)];
338 var a1 = vec2f(0.0);
339 if (l + 3u <= LMAX) {
340 a1 = ab[base + (l + 3u - m)];
341 }
342 for (var k = 0u; k < K; k++) {
343 let t0 = a0.x * ctv[k] * y1v[k] + a0.y * y0v[k];
344 y0v[k] = t0;
345 y1v[k] = a1.x * ctv[k] * t0 + a1.y * y1v[k];
346 }
347 l += 2u;
348 }`
349 }
351`;
moveopenescclose