Skip to content

Assembly: Add the coefficient layouts to the loop cache key - #5573

Merged
pbrubeck merged 4 commits into
pbrubeck/interpolate-cachefrom
sghelichkhani/5566-coefficient-layout-key
Oct 29, 2026
Merged

pbrubeck merged 4 commits into
pbrubeck/interpolate-cachefrom
sghelichkhani/5566-coefficient-layout-key

Conversation

@sghelichkhani

Copy link
Copy Markdown
Contributor

Fixes wrong values from shared loops in #5566. A subfunction of a mixed Function is a view into the dat of its parent, and a Function on the same subspace has a dat of its own. Both get the same key, so one reuses the loop built for the other layout.

from firedrake import *

mesh = UnitSquareMesh(10, 10)
x, y = SpatialCoordinate(mesh)
W = FunctionSpace(mesh, "DG", 1) * FunctionSpace(mesh, "CG", 2)

u1 = Function(W.sub(0)).interpolate(x + 2*y)
v1 = Function(W.sub(1)).interpolate(sin(4*pi*x)*cos(4*pi*y))
print(assemble((u1 - v1)**2*dx))

w = Function(W)
u2, v2 = w.subfunctions
u2.assign(u1)
v2.assign(v1)
print(assemble((u2 - v2)**2*dx))  # should print the same number

Before this change it prints 2.9112 and then 0.1828, with it 2.9112 twice. It also fixes test_checkpoint_comm_disk_checkpointing_parallel and test_sub_comm_disk_checkpointing_parallel on 3 ranks, which fail on the base branch.

A related case I have not hit: ParloopBuilder builds a different loop when a coefficient shares its buffer with the output tensor, and that is not in the key either.

A subfunction of a mixed Function is a view into the dat of its parent,
and a Function on the same subspace has a dat of its own. Both have the
same space, parent and index, so they got the same key and shared a loop
that was built for the other layout, which gives wrong values.

@pbrubeck pbrubeck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I know that 5566 was still failing CI, should we add the example from the PR description to the tests? Or is it covered already by the existing tests?

Comment thread firedrake/assemble.py Resolved
Comment thread firedrake/assemble.py Resolved
@sghelichkhani

Copy link
Copy Markdown
Contributor Author

Done, I added the example as test_loops_shared_between_coefficient_layouts in test_assemble.py. It fails without the change to the key. The existing tests only caught this indirectly, through the disk checkpointing tests on 3 ranks.

@sghelichkhani

Copy link
Copy Markdown
Contributor Author

About the CI here: "Basic repository checks" fails because the base of this PR is your branch and not main or release (check-config: error: argument mode: invalid choice: 'INVALID_BASE_REF'), and "Test default" and "Build documentation" fail on #5566 itself as well, so the CI of this PR does not say much until the base passes. test_assemble.py passes locally on this branch, in serial and on 2 ranks.

@pbrubeck

Copy link
Copy Markdown
Contributor

About the CI here: "Basic repository checks" fails because the base of this PR is your branch and not main or release (check-config: error: argument mode: invalid choice: 'INVALID_BASE_REF'), and "Test default" and "Build documentation" fail on #5566 itself as well, so the CI of this PR does not say much until the base passes. test_assemble.py passes locally on this branch, in serial and on 2 ranks.

I meant CI failing on 5566. You need to add the base:main label

Comment thread firedrake/assemble.py
Comment on lines 1236 to 1237
# Subspaces of a mixed space compare equal, but the loops index the dat of their parent.
tuple((V, V.parent, V.index) for V in spaces),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are these still needed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The signature should include ufl-specific information, and the loop nuances should be captured by the axis_trees

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe they are only needed for the arguments?

@pbrubeck
pbrubeck merged commit 58bf9da into pbrubeck/interpolate-cache Oct 29, 2026
3 of 6 checks passed
@pbrubeck
pbrubeck deleted the sghelichkhani/5566-coefficient-layout-key branch October 29, 2026 12:11
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