Skip to content

Multigrid: fix transfer caches that ignore the element basis, and reuse transfer loops - #5548

Merged
pbrubeck merged 4 commits into
mainfrom
pbrubeck/mg-transfer-cache
Nov 26, 2026
Merged

pbrubeck merged 4 commits into
mainfrom
pbrubeck/mg-transfer-cache

Conversation

@pbrubeck

@pbrubeck pbrubeck commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #5288. The cached transfer loops also include the loops that copy the values at unrefined nodes on adaptive hierarchies. Without the cache, each of these copy loops cost about 5 ms per call.

Summary

Two commits, both in firedrake/mg.

1. Fix multigrid caches that ignore the element basis. Element variants with the same entity DoFs, e.g. DG2 equispaced and spectral, collided in two caches on the hierarchy:

 kernels.py  transfer_kernels
-  key: entity_dofs_key(topology) + entity_dofs_key(entity_dofs)   # DoF layout
+  key: Vf.finat_element, Vc.finat_element, coordinate element      # the basis

 utils.py    node maps
-  key: entity_dofs_key(entity_dofs)
+  key: entity_dofs_key(entity_dofs), entity_permutations_key(entity_permutations)

The cell-node map depends on the entity permutations, which differ between those variants. Each fix alone still gives O(1) errors. The kernel key uses FInAT elements, which hash by identity today and will hash by value with firedrakeproject/fiat#304. Both are correct, and the value-based hash also hits across construction routes.

2. Reuse multigrid transfer loops across calls. prolong, restrict and inject built and hashed a new pyop3 loop on every call. Now each loop is built once for each pair of function spaces and data layouts, and replayed with new Dats by name, as FormAssembler.parloops does:

prolong(coarse, fine)                         # likewise restrict, inject
  for each level step
    _transfer(_prolong_loop, coarse, fine)
      key = (builder, source space, source axes, target space, target axes)
      on a cache miss: loop = _prolong_loop(coarse, fine); store loop and Dat names
      loop(**{source_name: coarse.dat, target_name: fine.dat})

The loops are cached on hierarchy._shared_data_cache["transfer_loops"], next to the node maps, so direct calls benefit as well as TransferManager.

Evidence

test_grid_transfer_variants (new) prolongs and injects DG2 for both orders of equispaced/spectral on one hierarchy:

  • Before: prolong error 2.2e-01, inject error 2.3e-01 for the second variant (4 failed)
    After: 3.7e-16 and 1.1e-15 (4 passed)

test_grid_transfer_reuses_loops (new, 1 and 2 ranks) checks that a second pass of transfers builds no loops, and that a mixed subfunction gets its own loop:

  • Before: fails (no loop cache)
    After: passes

Timings for CG2 (minimum of repeats, 8×8 base mesh, 2 refinements):

before after
prolong 8.25 ms 0.20 ms
restrict 8.37 ms 0.32 ms
inject 8.40 ms 0.25 ms
repeat solve with pc_type: mg 0.326 s 0.039 s

Merge Danger

Door: two-way

Blast Radius: moderate

Only multigrid transfers change. The cached loops keep the Functions used to build them alive while the hierarchy lives, as FormAssembler does with its first tensor. assemble_prolongation_aij still builds its loop on every call, because it runs once per setup.


AI assistance: this PR was written with Claude Code (Claude Opus 5.5).

🤖 Generated with Claude Code

https://claude.ai/code/session_01UuSK81ZfWY2LFahyWYpdPT

@pbrubeck pbrubeck added the LLM used An LLM was used in the production of this PR label Oct 22, 2026
@pbrubeck
pbrubeck force-pushed the pbrubeck/mg-transfer-cache branch from 259070a to bd07717 Compare October 22, 2026 15:42
@pbrubeck pbrubeck changed the title Multigrid: fix transfer caches that ignore the element basis, and reuse transfer loops Multigrid: fix transfer caches that ignore the element basis, and reuse transfer loops and interpolations Oct 22, 2026
@pbrubeck
pbrubeck force-pushed the pbrubeck/mg-transfer-cache branch 2 times, most recently from 8bc643e to 0c9cf16 Compare October 26, 2026 16:21
@pbrubeck pbrubeck changed the title Multigrid: fix transfer caches that ignore the element basis, and reuse transfer loops and interpolations Multigrid: fix transfer caches that ignore the element basis, and reuse transfer loops Oct 26, 2026
@pbrubeck
pbrubeck force-pushed the pbrubeck/mg-transfer-cache branch from 0c9cf16 to aeb048d Compare October 27, 2026 11:51
pbrubeck and others added 3 commits November 26, 2026 12:14
Adaptive refinement leaves the coarse points of unrefined cells unsplit.
The values at fine nodes on those points are equal to the values at
their coarse nodes, so the transfer kernels do not need to evaluate them.

Split the owned fine nodes into refined and unrefined nodes. Run the
prolongation and restriction kernels over the refined nodes only. Then
copy the coarse values to the unrefined nodes during prolongation, and
add the values at the unrefined nodes to their coarse nodes during
restriction. Both copies are pyop3 loops through a node-to-node map,
which handle the halo reads and the reductions into ghost nodes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The multigrid transfer kernels were cached on the DoF layout of their
elements, so element variants with the same entity DoFs (e.g. DG2
equispaced and spectral) shared a kernel that tabulates the wrong basis.
Key the kernels on the FInAT elements instead.

The node maps of a hierarchy were cached on the entity DoFs alone, but
the cell-node map also depends on the entity permutations, which differ
between these variants. Add the entity permutations to the node map key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
prolong, restrict and inject built and hashed new pyop3 loops on every
call. Building the loops took about 8 ms per transfer, while executing
them took about 0.2 ms.

Build the transfer loops once for each pair of function spaces and data
layouts, and cache them on the hierarchy. Later calls will execute the
cached loops on the Dats of their own arguments, as FormAssembler does
for its parloops. On adaptive hierarchies, the cached loops also include
the loops that copy the values at unrefined nodes.

A repeat solve with pc_type mg (8x8 base mesh, 2 refinements, CG2) drops
from 0.33 s to 0.04 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pbrubeck
pbrubeck changed the base branch from main to pbrubeck/adaptive-multigrid November 26, 2026 13:36
@pbrubeck
pbrubeck force-pushed the pbrubeck/mg-transfer-cache branch from aeb048d to febc372 Compare November 26, 2026 13:36
@pbrubeck pbrubeck added the base:main Run this PR using a main (dev) build label Nov 26, 2026
@pbrubeck pbrubeck added base:main Run this PR using a main (dev) build and removed base:main Run this PR using a main (dev) build labels Nov 26, 2026
@pbrubeck
pbrubeck requested a review from connorjward November 26, 2026 16:45

@connorjward connorjward 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.

Otherwise consider this approved. Nice and simple with a big win.

Base automatically changed from pbrubeck/adaptive-multigrid to main November 26, 2026 18:00
Carry the iterator rename and review comments into the moved transfer loops.
Clarify the setup in the multigrid transfer tests.

Assisted-by: OpenAI Codex
@pbrubeck
pbrubeck merged commit 77cf3fa into main Nov 26, 2026
5 of 7 checks passed
@pbrubeck
pbrubeck deleted the pbrubeck/mg-transfer-cache branch November 26, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

base:main Run this PR using a main (dev) build LLM used An LLM was used in the production of this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants