Skip to content

Batched tokamak solves on the host or on NVIDIA GPUs - #1

Merged
tomneiser merged 8 commits into
masterfrom
gpu-batch
Nov 15, 2026
Merged

tomneiser merged 8 commits into
masterfrom
gpu-batch

Conversation

@tomneiser

@tomneiser tomneiser commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Adds solve_dke_tokamak_batch(ops; device=:cpu | :cuda | :auto), which solves many flux surfaces or scan points at once. The cases must share the grids, stencils and operator options; the field, the plasma and the electric field may differ. The single-case API is unchanged, and the batched solver takes plain Float64 operators only (no dual numbers).

How it works

  • Lockstep GMRES (src/batch/batched_gmres.jl): all cases take their Krylov steps together and the small Hessenberg problems stay on the host. Converged cases are frozen. A case that meets a non-finite number stops as :failed and is solved again by solve_dke_tokamak.
  • Batched operator (src/batch/batched_operator.jl): array-generic (broadcasts, mul!, batched GEMM), so the same code runs on Array and CuArray. The right-hand sides, row scalings and constraints are formed on the device from small per-case tables.
  • Preconditioner (src/batch/batched_preconditioner.jl, assembly_maps.jl, block_coarse.jl): the two-level preconditioner of solve_dke_tokamak.
    • Assembly: every matrix entry is a fixed combination of per-case ingredients, so all cases are assembled by one sparse product.
    • Plane matrices: dense block-banded elimination along the pitch angle, one pitch angle and its poloidal ring per block. Planes below 2500 unknowns use cuDSS uniform batches.
    • Coarse matrices: dense block elimination in batched cuBLAS kernels.
  • CUDA support is a weak-dependency extension (ext/YanccCUDAExt.jl, CUDA + CUDSS). CI and the CPU package never load it. Cases are chunked to fit device memory, and each chunk frees its device memory when it finishes.

Results

Solves per second for D + C + e, nx = 6, rtol = 1e-6, 6–7 iterations per case, on a Perlmutter GPU node (one process per GPU) and a CPU node:

nalpha × ntheta unknowns one A100 four A100 CPU node (128 single-thread processes)
65 × 41 48 000 163 644 69
257 × 61 282 000 18.0 72 17

Two processes per GPU under CUDA MPS raise the node numbers to 743 and 80.

The GPU numbers leave out building the operators on the host (about 1.4 s per 1024 cases on 32 threads). A solve on its own does not gain from a GPU. On a CPU, one solve_dke_tokamak per core is faster than device = :cpu, which is there for testing. benchmark/ reproduces the table, and the README has a new section on this.

Tests and CI

  • test/test_batch.jl runs in the default suite. It checks the batched operator, the assembled matrices, the block solvers, the GMRES edge cases and whole batched solves against the single-case code.
  • test/test_cuda_ext.jl is opt-in with YANCC_TEST_GPU=1. It compares every device result with the host path, with scalar indexing forbidden.
  • Pkg.test(): 1388/1388. Full suite with the GPU tests on an A100: 1519/1519.
  • CI now runs the tests on two threads, so the threaded loops over cases are exercised.
  • codecov.yml leaves ext/YanccCUDAExt.jl out of coverage, since CI has no GPU. A few lines that Julia counts but never marks as run carry COV_EXCL_LINE.

Master (the ForwardDiff extension) is merged in.

🤖 Generated with Claude Code

tomneiser and others added 5 commits November 13, 2026 13:15
solve_dke_tokamak_batch solves many flux surfaces or scan points that share
the grids and options at once. All cases take their Krylov steps together
(lockstep GMRES), and the matrices of the two-level preconditioner are
assembled for all cases from fixed maps of per-case ingredients, then
factored and solved as batches.

- array-generic batched operator, GMRES and preconditioner in src/batch,
  running on host arrays and, through the CUDA + CUDSS weak-dependency
  extension, on the device (device = :cuda)
- coarse matrices, and plane matrices from 6000 unknowns up, by dense block
  elimination in batched cuBLAS kernels; smaller planes by cuDSS uniform
  batches
- cases are chunked to fit device memory; a case whose batched
  factorization fails is solved again on the host
- tests against the single-case solver in the default suite; GPU tests
  opt-in with YANCC_TEST_GPU=1; throughput benchmarks in benchmark/

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The dense block elimination now handles block-banded chains, so the plane
matrices can take one pitch angle (with its poloidal ring) per block, coupled
to the blocks within the stencil reach either side, instead of block
tridiagonal blocks of two. The batched inverses, the slowest kernels, then do
a quarter of the work. The coarse matrices keep block tridiagonal blocks.

Whole batched solves on one A100 go from 10.1 to 13.1 solves/s at
257 x 61 x 6 x 3 (cuDSS for the planes: 6.0) and from 45 to 57 at 129 x 41;
at 65 x 41 blocks now match cuDSS, so the GPU uses them from 2500 unknowns a
plane up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… chunks

The host work of each chunk limited the GPU throughput, most of it
not physics:

- each chunk now frees its device memory (operator, preconditioner
  factors including cuDSS's, Krylov basis) as it finishes, so a young
  collection replaces the full garbage collection that took a fifth of
  a chunk
- the right hand sides, row scalings and moment constraints are
  products of factors in pitch angle, theta and speed; they are formed
  on the device from small per-case tables instead of full arrays per
  case on the host (the batched path no longer calls DKEBorder or
  dke_rhs; tests check them against those)

A chunk takes 0.62 s instead of 0.92 s at 65 x 41 (95 cases) and
0.84 s instead of 1.10 s at 257 x 61 (12 cases). A GPU node solves
644 and 72 cases per second with one process per GPU, 743 and 80
with two under MPS (was 473 and 55).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conflicts in Project.toml, README.md and test/runtests.jl resolved by
keeping both sides: the CUDA and ForwardDiff extensions, the GPU and
automatic differentiation rows and sections, and both test files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 14, 2026

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

tomneiser and others added 3 commits November 14, 2026 09:42
…tension

GMRES: an exhausted Krylov space, no iterations, a right hand side and a
residual that are not finite, stagnation under a preconditioner that
changes between calls. Host seams: batched GEMM, default solvers, device
selection, chunks over threads; the preconditioner built from the
operators. CI runs the tests on two threads so that the threaded loops
run, and Codecov ignores ext/YanccCUDAExt.jl, which needs a GPU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lver

Rounding differs with the number of threads (CI now runs on two) and can
move a case across rtol, as the plane block tests already allow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d run

The defaults of the solvers were folded at compile time in the test, so
it now calls them through an inference barrier. The local declarations
and the closing lines of threaded and timed blocks are counted as code by
Julia 1.11 but never marked as run; they carry COV_EXCL_LINE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tomneiser
tomneiser merged commit 2135cbf into master Nov 15, 2026
4 checks passed
@tomneiser
tomneiser deleted the gpu-batch branch November 15, 2026 18:55
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