Skip to content

Fused Biot-Savart B and dB/dx in one coil pass - #233

Open
rogeriojorge wants to merge 7 commits into
perf/biot-savart-scanfrom
perf/fused-b-gradb
Open

rogeriojorge wants to merge 7 commits into
perf/biot-savart-scanfrom
perf/fused-b-gradb

Conversation

@rogeriojorge

@rogeriojorge rogeriojorge commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

The guiding-centre RHS now gets B and dB/dx from one pass over the coil filaments (analytic per-segment gradient, dB_i/dx_j = mu0 I/4pi sum [(dl x e_j)_i/r^3 - 3 (dl x r)_i r_j/r^5], accumulated in the same checkpointed scan step) instead of a forward-mode Jacobian of B. BiotSavart.gc_quantities and dB_by_dX use it; other fields fall back to jacfwd through a generic B_and_dB_by_dX. The chunk is min(chunk, n_coils), so a set smaller than a chunk runs as a single step without a scan.

Tests: dB/dx equals jacfwd(B) to 1e-12, tr(dB/dx) = 0 and curl B = 0 off the coils, and a loss through gc_quantities has the same coil gradient as the old jacfwd path. test_fields, test_dynamics and test_objective_functions pass.

GPU (RTX A4000, x64, jax.profiler trace, summed GPU kernel time of one run; the office host was shared, so wall times were too noisy to compare and are not used). gc_coils is 1000 guiding-centre markers on the LandremanPaulQA coils, fo_coils 100 full-orbit protons (B only):

workload main #208 this PR
gc_coils kernel time (ms) 800 954 365
fo_coils kernel time (ms) 241 245 237

The kernel writes 1/sqrt(r2)3 instead of r2-1.5, which was 30% slower for B alone on the A4000 (320 ms in fo_coils); the concatenated 15-channel sum and the checkpoint in the scan step cost nothing measurable. Candidates that were profiled and not kept: component-wise (no trailing axis of 3) layout, no checkpoint, dense sum without scan, and the #208 expression for B only. B, dB/dx and gc_quantities agree with main to 1e-15 relative (x64, 2048 points).

CPU (office, 8 pinned cores, loaded, gc_coils steady time, s): main 3.4 to 4.6, #208 7.4 to 10, this PR 6.5. On this machine the scan is slower than main on CPU; the memory reduction is unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant