Skip to content

Evaluate long raw symbolic sources once per quadrature point - #64

Merged
xtalax merged 3 commits into
masterfrom
perf/source-linearity-substitute
Nov 5, 2026
Merged

xtalax merged 3 commits into
masterfrom
perf/source-linearity-substitute

Conversation

@xtalax-droid

Copy link
Copy Markdown
Collaborator

A long raw symbolic :source (for example the strong residual of a manufactured solution) was evaluated by the per-shape-function fallback kernel, once per quadrature point and per test function, because compile_coeffs rejected it as non-linear.

Cause: the linearity check isequal(expand(ex - linear), 0) compares f*V with (∂ex/∂V)*V, and expand does not cancel a/b^p - a/b^p term by term, so a source of a few hundred terms leaves a residue of thousands of characters, source.coeffs === nothing, and the fallback runs (src/assembly.jl:499). A source registered with @register_symbolic is opaque and never reaches the check.

Fix (src/kernels.jl): for :source only, also accept ex(w = 0) == 0, provided every call in ex that depends on the test slots is a +, * or /. Soundness: for a source ex is the whole integrand (f*V) and w the test slots. The existing depends check on the coefficients, which follows immediately, rejects unless every ∂ex/∂w_i is free of w and of the trial slots, so ex = ex(w=0) + Σ (∂ex/∂w_i) w_i exactly, and a zero constant term makes it linear. Nonlinear forms (V^2, V*W, V*U, V*X + 1) are still rejected by that check. The arithmetic-only walk exists because Symbolics gives sign(V) a zero derivative and sign(0) = 0 (a Codex finding), so a hand-built V + sign(V) would otherwise be accepted with a wrong coefficient; it cannot occur in sources built by weak_form, where V only appears as f*V.

Tests (test/Kernels/kernels.jl): a manufactured-solution residual source (K = 4 terms) has coeffs !== nothing and its assembled load vector matches the coeffs = false path to 1e-12; on master it fails with source.coeffs !== nothing evaluating nothing !== nothing. A second testset pins the rejected forms, including the sign case, which fails without the guard.

Measured with a long-source reproducer (heat equation, P2 triangles, manufactured solution with K = 8 terms, 112 additive terms, 11,814 characters; A raw source, B registered with cse = true, C ordinary source), same session and box:

n = 8 (289 dofs) master this PR
A rhs per call 10.1 ms 1.7 ms
A solve to t = 0.05 1.89 s 0.34 s
A/B rhs ratio 27x 4.7x
max nodal error 1.294e-4 1.294e-4

n = 4: A rhs 2.5 ms to 0.4 ms, solve 0.56 s to 0.10 s, error 1.903e-3 unchanged. C (ordinary source) is unchanged. benchmarks/assembly_cost.jl cases (i) and (iii) (ratios 1.6 and 1.3 before and after) and benchmarks/construction.jl are unchanged within noise. The remaining A/B gap is raw expression evaluation against CSE'd code, which the follow-up (cse = true in the codegen) addresses.

Gates (Julia 1.12): Perf 28/28 (all four gates, uninstrumented, including gate 2, no per-cell allocation), Kernels 355, Assembly 55 + 14, Integration 304, Frontend 753, Convergence 26; Kernels and Assembly also pass on lts. Runic 1.13 clean; Codex approved (round 1 found the sign hole, closed in the second commit).

Risks: the arithmetic-only walk calls depends per node on the source tree (not measurable at 11.8k characters; a pathologically deep tree would pay a quadratic cost, only after the cheap expand check has failed); the fallback is :source only, :mass and :bilinear are untouched.


🤖 Posted by an AI agent · harness: Claude Code 2.1.318 · model: claude-sonnet-5-5
Review: unreviewed
Session: 43e03fde-3384-4386-bae9-03e81f19ba7c

t and others added 2 commits November 5, 2026 06:27
compile_coeffs rejected a long raw :source because expand(ex - linear) does not cancel a/b^p - a/b^p term by term, so the kernel evaluated f once per quadrature point and test function. For :source, also accept ex(w = 0) == 0: the next check makes the coefficients free of the test slots, so ex is affine in them and a zero constant term makes it linear.

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-sonnet-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
…est slots

A function such as sign has a zero derivative and vanishes at 0 without being linear, so the ex(w = 0) == 0 fallback now also requires that every call depending on the test slots is a +, * or /.

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-sonnet-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
master 37d1ad3... master / 37d1ad3...
construction/heat P1 10x10 4.96 ± 0.99 ms 4.18 ± 0.54 ms 1.19 ± 0.28
construction/heat P1 20x20 7.12 ± 1.2 ms 7.28 ± 1 ms 0.979 ± 0.22
time_to_load 3.99 ± 0.21 s 3.94 ± 0.014 s 1.01 ± 0.054
Memory benchmarks
master 37d1ad3... master / 37d1ad3...
construction/heat P1 10x10 0.0393 M allocs: 2.11 MB 0.0393 M allocs: 2.11 MB 1
construction/heat P1 20x20 0.0702 M allocs: 4.73 MB 0.0702 M allocs: 4.73 MB 1
time_to_load 0.2 k allocs: 11.8 kB 0.2 k allocs: 11.8 kB 1

Documenter fails the build with :missing_docs on a docstring for an internal
function that is not in the manual (FiniteElementMethod.arithmetic_in), as in
the Documentation check on the PR. Internal helpers in this file use comments.

Agent-Harness: Claude Code 2.1.312
Agent-Model: claude-sonnet-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
@xtalax
xtalax merged commit 2516af6 into master Nov 5, 2026
48 checks passed
xtalax-droid pushed a commit that referenced this pull request Nov 5, 2026
Resolve the test/Kernels/kernels.jl import conflict by keeping both sides: build from this branch and AssemblyContext, assemble_linear and the expanded Ferrite and LinearAlgebra imports from master (#64).

Agent-Harness: Claude Code 2.1.318
Agent-Model: claude-haiku-5-5
Agent-Session: 43e03fde-3384-4386-bae9-03e81f19ba7c
Agent-Review: unreviewed
Co-authored-by: Claude Code <agent@noreply.juliahub.com>
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.

2 participants