Skip to content

Scalar/vector/multi-component solvers: each in-place rewire appends another set of natural-BC kernels (right only by call order) #1069

Open

Description

@lmoresi

What happens

SNES_Scalar, SNES_Vector and SNES_MultiComponent have a narrow in-place rewire, _build fast path 2 → _setup_solver(_rewire_only=True). It runs when only the pointwise functions change. It re-registers every natural BC's boundary residual and Jacobian through UW_PetscDSSetBdResidual / UW_PetscDSSetBdJacobian (cython/petsc_compat.h).

Those helpers call PetscWeakFormAddBdResidual / PetscWeakFormAddBdJacobian, which append to the boundary's weak form. The interior kernels go through PetscDSSetResidual / PetscDSSetJacobian, which replace.

So after N rewires, each boundary key lists N+1 kernels, one per JIT module. Here is the verbose view from SNES_Vector after two rewires:

boundary_residual_f0
  (UW_Boundaries, 12) (0, 0) UWE81E4102_petsc_eqn_6, UWC379A158_petsc_eqn_6, UWA2BB86E0_petsc_eqn_6
boundary_jacobian_g0
  (UW_Boundaries, 12) (0, 0) UWE81E4102_petsc_eqn_7, UWC379A158_petsc_eqn_7, UWA2BB86E0_petsc_eqn_7

Why the answer stays right

PETSc 3.25 calls every entry: for (i = 0; i < n0; ++i) f0_func[i](..., out) in PetscFEIntegrateBdResidual_Basic and ..._BdJacobian_Basic, all into one output buffer. Our generated kernels assign, out[0] = ..., rather than add. So the stale kernels run, and the newest, called last, overwrites them.

Measured on the #1065 branch, a Poisson problem with a Robin flux -k T + 0.2:

  • k changed from -1 to -(1+x) through the narrow rewire;
  • the rewired solver matches one built fresh with -(1+x) to 1.1e-12 on the top;
  • the change of maths moves that value by 4.0e-2.

The vector projection likewise matches a fresh build to 3e-11 after each of two rewires. With unchanged maths, three rewires leave F and J bit-identical.

The registration code and the rewire path are identical on development; the branch differs only in #1065's preconditioner hunks.

Why it matters

  • Cost: after N rewires, every boundary quadrature point evaluates N+1 kernels for each term, and the old modules stay referenced.
  • Correctness by call order: the result is right only because PETSc calls the entries in registration order and each kernel assigns. A slot the new registration leaves NULL would keep a stale kernel that nothing overwrites. For example, SNES_Vector passes NULL for G2/G3 when the BC has no u_F1. That case is not demonstrated.

How often it fires

The narrow rewire runs when a constitutive parameter, a jit_route or a consistent_jacobian change alters the JIT key, but leaves the DM and the BCs alone.

These loops did not append:

  • a Stokes solve(picard=1) loop, because the Picard blend is one sticky rewire;
  • a Stokes forward plus gradient loop, because the forward key matched the cache, so no rewire ran.

The growth needs repeated changes that alter the JIT key between solves.

Proposed fix

Register boundary kernels with replace semantics on the rewire path: PetscWeakFormSetBdResidual / PetscWeakFormSetBdJacobian (with n = 1), or clear the boundary weak form before re-adding.

Probes: probe_rewire.py, probe_rewire_bc.py, probe_rewire_vec.py (W1 sweep, issue_sweep/robin_jac/). Found in the #1065 review (PR #1068).

Underworld development team with AI support from Claude Code

Activity

  1. added
    bug
    priority:low
    type:code
    on Nov 11, 2026
  2. lmoresi commented on Nov 11, 2026

    @lmoresi
    MemberAuthor

    The stale-slot hazard in the body can't be reached today.

    Whether a natural BC has the gradient term u_F1, and with it the G2/G3 kernels, is fixed when the BC is created: it is the fn_F field of the BC record, which a rewire does not change. A BC with fn_F registers G0–G3 on every rewire, even where a block is zero, and one without it registers NULL for G2/G3 every time. So no slot goes from filled to NULL across a rewire, and the newest registration always overwrites every older kernel.

    What remains is the cost (N+1 kernel calls per boundary quadrature point after N rewires) and a result that depends on call order. Priority stays low.

    Underworld development team with AI support from Claude Code

  3. added
    fixed-in-PR
    on Nov 16, 2026
  4. added a commit that references this issue on Nov 19, 2026
  5. added
    fixed-in-development
    and removed
    fixed-in-PR
    on Nov 19, 2026
  6. lmoresi commented on Nov 19, 2026

    @lmoresi
    MemberAuthor

    Fixed in development by #1211, merge commit 246d6ce. An in-place rewire now replaces the natural-BC kernels through PetscWeakFormSet* instead of appending another set: one kernel per key, where development reached 1, 2, 3, 4 kernels after repeated rewires. A NULL kernel is passed as a NULL array, which deletes the key. The issue stays open until the release.

    Underworld development team with AI support from Claude Code


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfixed-in-developmentpriority:lowtype:code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions