Skip to content

Performance: compilation, AMG and large systems

What runs compiled

A slot-composed solve runs as one compiled program where it can, rather than calling the Krylov iteration from eager Python and paying dispatch on every step. How much that is worth depends on the device: the eager cost is host-bound and so barely varies between machines, while the compiled cost is device-bound — the faster the GPU, the larger the ratio.

On an RTX 3070 at 13759 DOFs, bicgstab + jacobi 114.1 ms → 18.1 (6.3x), cg + jacobi 97.5 → 14.5, minres + jacobi 115.2 → 20.2, gmres + jacobi 398.4 → 183.2, fgmres + jacobi 536.3 → 300.8 (1.8x — jNO's own restart loop does more real arithmetic, so less of its time was dispatch).

On CPU, 1.8–4.2x; on a faster GPU, bicgstab + jacobi reached 16.6x. Same answers throughout. Nothing to switch on; write the slots as usual, and write them inline if you like (fem.solve(linear=jno.solve.cg(), precond=jno.precond.jacobi())) — equivalently configured specs share one compilation, so a solve in a loop compiles once.

These combinations stay eager, and are correct but not accelerated:

Slot Why
solve.chebyshev, precond.chebyshev measures spectrum bounds, then branches on what it measured
precond.amg unbuilt, precond.ams, precond.form assembles an auxiliary operator host-side (scipy / pyamg)
precond.jaxamg, solve.amg AmgX builds its hierarchy from the matrix values; unverified under a tracer
solve.lu, solve.dense, solve.amg one direct call — no per-iteration dispatch to remove
a bare callable in either slot jNO knows nothing about it, so it makes no assumption
a multi-device (shard=) solve already compiles itself, with the operator partitioned

First-run compilation cost

Compiling is not free the first time. Building a 13.8k-DOF 2-D Poisson problem issues ~209 XLA compilations totalling ~3.7 s — assembly evaluates reference-element expressions whose every distinct operation and shape is its own small program.

The cost is paid once per distinct mesh shape, and it is cached within a process: rebuilding the same problem costs ~320 ms, and rebuilding with freshly constructed term objects costs the same (the cache is keyed on shapes, not on object identity). A different mesh pays it again, so a remeshing loop (fem.adapt, a refinement study) pays it per iteration.

Across separate processes — running a script twice, a test suite, CI — nothing is reused by default. JAX can persist compilations to disk, which takes the same build from 4757 ms to 1143 ms (measured, 214 entries, 932 KB):

import jax
jax.config.update("jax_compilation_cache_dir", "~/.cache/jax")
jax.config.update("jax_persistent_cache_min_compile_time_secs", 0.0)  # REQUIRED here

The second line is not optional for this workload: the default threshold is 1.0 s and every one of these compilations is far below it, so with the cache directory alone nothing is ever written. jNO does not set either of these for you — a library should not start writing to a user's disk uninvited.

When AMG is worth it

jno.precond.amg has the best asymptotics on offer: Jacobi's iteration count grows as √n (79 → 166 → 288 at n = 3k → 12k → 47k on a 2-D Poisson), AMG's is O(1). Build the hierarchy once and reuse it — an unbuilt spec re-runs pyamg's host-side setup on every solve, and stays off the compiled path because that setup cannot be traced:

M = jno.precond.amg().build(fem.operator[0])       # or jno.precond.amg(reuse=True)
u = fem.solve(linear=jno.solve.cg(tol=1e-10), precond=M)

Per solve, against cg + jacobi at the same tolerance — the advantage grows with the problem, which is the √n-vs-O(1) law showing up directly:

DOFs cg + jacobi cg + amg (built) setup break-even
3,013 6.4 ms 4.4 ms 1.5x 135 ms 66 solves
18,289 13.6 ms 6.6 ms 2.1x 317 ms 45 solves
46,677 31.5 ms 10.0 ms 3.2x 303 ms 14 solves
95,061 80.3 ms 16.4 ms 4.9x 419 ms 7 solves

So AMG is for repeated solves against the same operator — a transient run, a parameter sweep, a Newton loop — where the setup amortises. For a single one-shot solve below ~100k DOFs, Jacobi still wins on wall clock: you would pay 419 ms of setup to save 64 ms. This is why AMG is not the default; the right choice depends on how many times you solve, which only you know.

Large systems on a GPU

Past a few hundred thousand DOFs, what stops a GPU solve is rarely the matrix. Three things bite first, and only the third is about size at all.

Assembly runs on the host, automatically. jNO's element loop allocates temporaries far larger than the matrix it produces, so assembling on the device runs out of memory at roughly a third of the size the finished operator would happily occupy. jno.fem(...) therefore builds on the CPU whenever a GPU is the default backend, and the solve moves the operator across:

fem = jno.fem([...])                     # assembled on the host, whatever the default backend
u = fem.solve(linear=jno.solve.fgmres(), precond=M)   # solved on the GPU

Nothing is given up for this. jax.device_put across backends is traceable and jax.grad differentiates through it, so topology optimisation, neural coefficients and trainable coordinates all still work across the split. Measured on a 9,970-DOF Poisson, assembly now costs 8 bytes of device memory — flat from n=242 to n=3,797 — against 104 MB before. Budget roughly 6 GB of host RAM per million DOFs; host memory, not the card, becomes the binding constraint.

It is not slower for a one-shot build, either — the host wins on every first call, because it also compiles faster:

assemble on host on device
Poisson, 43k DOFs 4.02 s 4.98 s
mixed N1E x Lagrange, 43k DOFs 9.28 s 11.00 s

The device does win on repeated assembly of a heavy form once compilation has amortised (5.60 s on the host against 3.69 s on the device, mixed N1E at 43k DOFs), which matters in a Newton or optimisation loop that reassembles many times. There is no jNO argument for this — jax.default_device is JAX's own way of saying where work goes, and an explicit one wins:

with jax.default_device(jax.devices("gpu")[0]):
    fem = jno.fem([...])                 # assemble on the device instead

Set the async allocator. XLA's default BFC allocator fragments and will refuse an allocation about a gigabyte below the card's actual free memory — on a GPU shared with a desktop this is the difference between running and not:

XLA_PYTHON_CLIENT_PREALLOCATE=false XLA_PYTHON_CLIENT_ALLOCATOR=cuda_async

A 2.70 GB operator that BFC rejected ran without complaint under cuda_async.

Sizing. A BCOO operator costs 16 bytes per nonzero in float64 (an 8-byte value plus two int32 indices), and peak device memory during a solve is about 1.3x that, the extra being SpMV scratch. Measured on an RTX 3070 (7.81 GiB usable, ~3 GB held by a desktop), on the fused real 2n block of a complex mixed N1E x Lagrange system at ~50 nonzeros per row:

DOFs nonzeros operator peak SpMV effective
1,215,312 60.3 M 0.90 GB 1.24 GB 7.9 ms 114 GB/s
2,216,184 113.3 M 1.69 GB 2.22 GB 15.0 ms 113 GB/s

Bandwidth holds flat across sizes, so a Krylov solve costs about iterations x SpMV: budget from the iteration count, not from the DOF count.

float32 is not a lever. The same 60.3 M-nonzero operator takes 8.05 ms in float64 and 9.09 ms in float32 — half the value bytes, and slightly slower. Indices stay int32 either way and the gather of x dominates, so halving precision buys about a quarter of the memory and none of the time. Reach for a coarser mesh or a better preconditioner instead.

High-order quadrilaterals and hexahedra need no matrix at all. A Q8 hexahedral operator stores 256 entries per DOF; jno.fem([...], matrix_free=True) stores none. A linear form's operator is linearised once and applied through the cells by sum factorisation (O(k^(d+1)) per cell), fem.operator is (A, b) with A a matvec (A @ v, A.T, A.diag()), and any Krylov slot solves it; a direct solver or a matrix-based preconditioner has nothing to factor and raises.

A nonlinear form is solved by Newton-Krylov, the tangent applied as a JVP of the residual and preconditioned by its sum-factorised diagonal, never assembled (Q8, 530k DOFs: 2.8 s, where the assembled tangent does not fit the card). On a time domain (u.t, u.tt) every time scheme marches it; with the diagonal GLL mass jno.solve.erk() takes one stiffness apply per stage (a 2.1M-DOF Q8 wave: 52 ms an RK4 step in float64).

quadrature="gll" puts the quadrature at the element's own nodes, which makes the mass matrix diagonal. Measured at 2.1M DOFs (Q8, float64, RTX 3070): 6.4 s to build, 11 ms per apply, 0.7 GB on the card, a Jacobi-CG solve in 1.2 s. See Spectral methods.

Preconditioners (jno.precond, for the iterative solvers):

Kind jno.precond
Diagonal / polynomial jacobi, chebyshev
Spectral spectral (for jno.spectral: the Fourier-mode block inverse, else fast diagonalisation of the separable part — exact for constant coefficients, 1–3 GMRES iterations)
Between jacobi and multigrid nystrom (randomized low-rank, SPD); fsai (factored sparse approximate inverse, SPD — applied as two sparse products, numeric setup in JAX; see FSAI)
Multigrid amg (algebraic, pyamg), jaxamg (GPU AmgX), gmg (geometric — a structured-grid V-cycle), ams (H(curl) auxiliary-space Maxwell), hypre (AMS / BoomerAMG via PETSc)
Domain decomposition schwarz (overlapping Schwarz on a METIS partition, two-level by default; needs the [metis] extra — see Overlapping Schwarz)
Coarse spaces / deflation coarse(space, parts=) — the correction Z (ZᵀAZ)⁻¹ Zᵀ on a subspace written as formulas, a dense or sparse array or a callable, combined with >> (Q >> M >> Q BNN, M >> Q A-DEF2); see Coarse spaces. Computed spaces live in jno.space (eigvecs(k), geneo(tau=, nev=) per Schwarz subdomain; reuse=k on the spec holding it recomputes it as the operator moves)
Incomplete factorization ilu (SuperLU spilu, complex-capable)
Block / saddle point block_diag / triangular (block / Schur), saddle, lsc, pcd
Composition M1 + M2, M1 @ M2, M1 >> M2 (multiplicative: M2 on the residual M1 left); form (weak-form auxiliary operator), inner (any solver as M⁻¹), real_equivalent (a complex system through its real block); reuse=True / reuse=k on any of them keeps its setup across solves