Repository navigation
Share common subexpressions in the generated source kernels - #65
Merged
Merged
Conversation
build takes a cse keyword (default false) and only the :source kernels and coefficient functions pass cse = true. Common-subexpression elimination also hoists numeric literals, which turns a literal power u^2 into a libm NaNMath.pow call: 25 to 45 percent slower nonlinear residual and Jacobian assembly in benchmark case (iii). Tests check that the nonlinear and coefficient kernels keep literal_pow and that the CSE'd sources agree with the plain code generator. 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>
Common-subexpression elimination hoists literals into temporaries, which turns u^2 into a NaNMath.pow call (2x slower on a polynomial source). build substitutes the literal temporaries back and restores literal_pow before compiling. Tests check that no kernel, source or not, has a pow call on a hoisted exponent. 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>
cse hoists the calls in both branches of an ifelse in front of the conditional, so a guarded registered function or sqrt can throw where the plain code never reaches it. Sources containing ifelse compile without cse. 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>
The rewrite matched any function named pow; it now matches the NaNMath.pow function itself, so a registered user function that is called pow keeps its meaning. 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>
Base.literal_pow and NaNMath.pow are not public, which the QA group's explicit-imports check rejects. A call x^2 on a literal integer lowers to literal_pow by itself, and NaNMath.pow is recognised by its name and parent module. 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>
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A long manufactured source term (hundreds of additive sin, cos, exp and power products) is recompiled per sum term, so evaluating it dominates the right-hand side.
buildnow takes acsekeyword (default false), and only the:sourcekernels and coefficient functions passcse = true.Plain
cse = truealso hoists numeric literals into temporaries, sou^2becomes a libmNaNMath.pow(u, 2)call instead ofliteral_pow. That is 2x slower on a polynomial source and 25 to 45% slower on the nonlinear residual and Jacobian (benchmark case (iii)), which is why CSE is limited to sources and why a first attempt at CSE on every kernel was not opened as a PR (reproducer:build_function(2*a*g1*v1 + 2*a*g2*v2 + a^2*v1 + a^2*v2, a, g1, g2, v1, v2; expression = Val(true), cse = true)emits##cse#1 = 2andNaNMath.pow(a, ##cse#1); reported upstream to SymbolicUtils).buildtherefore substitutes the numeric-literal temporaries back and restoresx^n(which lowers toliteral_pow) before compiling (inline_literals). Sources containingifelsecompile without CSE, because CSE hoists both branches in front of the conditional and a guarded registered function orsqrtcould throw where the plain code never reaches it (a Codex finding, with a regression test). Mass, bilinear, nonlinear, tangent, strong and Dirichlet kernels are unchanged. Helpers are#comments, not docstrings.Measured, ns per call of the source function (Julia 1.12, Symbolics 7.48.0): polynomial squares K = 8: 51.3 plain, 106.8 raw CSE, 42.5 here; K = 2: 8.6, 30.4, 10.3; trig with squares K = 8: 1097, 245, 178; long without squares: 2486, 425, 411; ordinary source: 22.3, 10.2, 10.1; the
(1+u^2)-style nonlinear tangent (not changed in this PR): 1.6 plain, 11.7 raw CSE, 1.6 with inlining. End to end, n = 16, raw source A (master versus this PR, identical max error): polynomial squares K = 8 rhs per call 1.0 ms to 0.2 ms, solve 0.51 s to 0.30 s; trig with squares K = 8 rhs 15.3 ms to 1.3 ms, solve 2.12 s to 0.30 s; the long source (K = 8) rhs per call 10.2 ms to 1.9 ms at n = 8 and 40.5 ms to 7.4 ms at n = 16, solve 1.95 s to 0.37 s and 6.0 s to 1.14 s.benchmarks/assembly_cost.jlandconstruction.jlare unchanged within noise (case (iii) ratio 1.27 and 1.29 against master's 1.33), coefficient andfkernel calls allocate 0 B, cold firstdiscretizeis unchanged (33.1 s long source and 30.0 s ordinary against 33.5 s and 29.9 s), time to first assembly with the precompile workload is 0.59 and 0.62 s against 0.58 to 0.60 s on master.Tests (
test/Kernels/kernels.jl): "no kernel turns a literal power into a pow call on a hoisted exponent" walks the Expr of every kernel'sf,tangentandcoeffsfor case (iii) and the two-field case and fails if a^orpowhas a symbol exponent assigned from a number (it fails on the full-kernel CSE commit withNaNMath.pow(a, var"##cse#1")); "common-subexpression elimination of a source agrees with the plain code generator" compares values at 1e-12 on random inputs over an ordinary, a long, a parameter-and-time and a squares source, checks the CSE'd long source evaluates fewersincalls, and checks an emitted source against the symbolic integrand; plus a guarded-source test (branches stay lazy, aDomainErrorwithout the guard) and one that a user function namedpowkeeps its meaning. QA's explicit-imports check rejected a first version that used non-publicBase.literal_pow/NaNMath.pow; the final code emitsx^nand recognisespowby name and parent module.Gates: Julia 1.12 uninstrumented: Kernels, Assembly, Integration, Perf (all four gates), WeakForm, Solution, Space, DAE, QA, Convergence, NavierStokes, Frontend (23 files) all pass; Julia 1.10 with current versions: Kernels, Assembly, DAE; Julia 1.10 downgrade-style (Symbolics 7.39.2, SymbolicUtils 4.53.0, other deps at their floors): Kernels, Assembly, DAE, Integration, NavierStokes pass. The docs build exits 0 with no missing docs, Runic 1.13 is clean and Codex approved (three findings fixed:
ifelselaziness, thepowmatch by name, a first-pass issue). Relation to #64 (the exact substitute fallback that lets long raw sources reach the coefficient path): independent, andsrc/kernels.jlmerges cleanly with it; both append tests at the same place intest/Kernels/kernels.jl, so whichever merges second needs a trivial textual resolution there. If an upstream SymbolicUtils fix stops CSE from hoisting numeric literals,inline_literalscan be dropped.🤖 Posted by an AI agent · harness: Claude Code 2.1.318 · model: claude-sonnet-5-5
Review: unreviewed
Session: 43e03fde-3384-4386-bae9-03e81f19ba7c