Skip to content

gem: simplifications supporting Jacobian product cancellation#255

Open
pbrubeck wants to merge 1 commit into
mainfrom
pbrubeck/cancel-jacobian
Open

gem: simplifications supporting Jacobian product cancellation#255
pbrubeck wants to merge 1 commit into
mainfrom
pbrubeck/cancel-jacobian

Conversation

@pbrubeck

@pbrubeck pbrubeck commented Jul 9, 2026

Copy link
Copy Markdown

Description

GEM-side companion of firedrakeproject/ufl#68, improving the code generated for PhysicallyMapped (zany) elements:

  • gem.py: fold Indexed(Identity, (i, j)) into Delta(i, j), so the Kronecker deltas introduced by the UFL Jacobian-product cancellation participate in delta elimination and unroll to 0/1 constants.
  • optimise.py: lower Delta via a Literal identity table in replace_delta, since Indexed(Identity, ...) now folds back into Delta (this previously made replace_delta a no-op, leaking deltas into code generation).
  • coffee.py: iterate argument factorisation to a fixpoint, so subexpressions factorised in one round are grouped as common factors in the next (resolving the long-standing TODO in factorise_atomics), and group monomials sharing the same rest coefficient, so c*a1 + c*a2 + c*a3 becomes c*(a1 + a2 + a3). For the Johnson-Mercier div-div term this turns the element tensor accumulation into optimal rank-1 updates.

Benchmark (JM mass + div-div, spectral mode, with the companion PRs): 3D flops 763527 -> 444574 (-42%), compile time 3.62s -> 1.81s (-51%), generated code 323kB -> 143kB (-56%).

🤖 Generated with Claude Code

- Fold Indexed(Identity, (i, j)) into Delta(i, j), so that Kronecker
  deltas introduced by UFL participate in delta elimination.  Lower
  Delta via a Literal identity table in replace_delta, as
  Indexed(Identity, ...) now folds back into Delta.
- COFFEE: iterate argument factorisation to a fixpoint, so that
  factorised subexpressions are grouped as common factors in
  subsequent rounds, and group monomials that share the same rest
  coefficient, so that c*a1 + c*a2 becomes c*(a1 + a2).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@pbrubeck pbrubeck force-pushed the pbrubeck/cancel-jacobian branch from 1989f22 to 05b4c0f Compare July 11, 2026 14:14
@pbrubeck pbrubeck marked this pull request as ready for review July 12, 2026 14:14
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