Skip to content

Optimise find_optimal_atomics#221

Merged
pbrubeck merged 2 commits into
mainfrom
pbrubeck/optimal-atomics
Feb 9, 2026
Merged

Optimise find_optimal_atomics#221
pbrubeck merged 2 commits into
mainfrom
pbrubeck/optimal-atomics

Conversation

@pbrubeck

@pbrubeck pbrubeck commented Feb 9, 2026

Copy link
Copy Markdown

The sum factorization algorithm involves finding the optimal set of common subexpressions via a brute-force search. Previously, the search spent most of the time comparing sets of "atomic" gem.Node instances (the so-called monomials). The set comparisons were hashing the same gem.Node instances over and over again. Instead, we make sure that the hashing only occurs when preprocessing all unique atomics into a list, and we solve the optimisation problem using the index into that list.

We also introduce a better initial guess by recursively sorting the subset of monomials that intersect with each other.

As a benchmark, we generate code for assembling

from firedrake import *
from firedrake.tsfc_interface import compile_form
V = FunctionSpace(UnitTetrahedronMesh(), "JM", 1)
v = TestFunction(V)
u = TrialFunction(V)
form = (inner(u, v) + inner(div(u), div(v))) * dx
kernels = compile_form(form, "stokes", parameters={"mode": "spectral"})

Notice that the (div, div) term is not integrated with dx(degree=0).

Before: 143 seconds
image

After: 79 seconds
image

Comment thread gem/coffee.py Outdated
Comment thread gem/coffee.py

@connorjward connorjward left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can have a think about further optimisations as a separate piece of work. Think this is fine to go.

@pbrubeck pbrubeck merged commit c09e1ed into main Feb 9, 2026
@pbrubeck pbrubeck deleted the pbrubeck/optimal-atomics branch February 9, 2026 21:13
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