Skip to content

Mesh-move field transfer: remap_var_set can skip a collective on one rank, and ghosts are not synchronised #960

Open

Description

@lmoresi

Two rank-asymmetry risks in the mesh-move field transfer, found by review of #959. Both are from reading the code. Neither has been reproduced.

remap_var_set can skip a collective on one rank

src/underworld3/discretisation/remesh.py, the resampling loop in remap_var_set:

  • if target is None or target.size == 0: continue skips global_evaluate on a rank with no DOFs for that variable.
  • The surrounding except Exception: continue does the same for a failure on one rank only.

global_evaluate is collective, so the other ranks would wait for it. A rank that holds no DOFs of a variable is the likely trigger. This is the same class as #909, at a different site.

_write_var_data doesn't synchronise ghosts

Each rank writes its own global_evaluate result into var.vec, ghosts included. mesh.lvec should be clean, because update_lvec goes through the owner. Code that reads var._lvec directly (gradient_evaluation.py, field_projection.py:582) may see different values for a shared node on different ranks. The size of the disagreement hasn't been measured.

Underworld development team with AI support from Claude Code

Activity

  1. lmoresi commented on Nov 6, 2026

    @lmoresi
    MemberAuthor

    Measurements and an addition, from the review of #959.

    Part 1: #959 removes the target.size == 0 skip. Hang measured on a 1 x 2 box at np 2 and np 4 before that fix, and none after. The except Exception: continue in the same loop remains.

    Part 2 confirmed. After a mover call on an annulus (cell size 1/8, tanh thermal layer, 20 mover iterations), largest |ghost - owner| in T.vec:

    np 2 np 4 np 8
    REMESH_MONOTONE=clamp (default) 1.1e-3 1.4e-3 6.6e-2
    off 0.0 0.0 0.0

    So the clamp causes it. It bounds each value with a rank-local neighbour set, so a ghost and its owner get different bounds. T changes by about 0.5 across this move, and the gap at np 8 is large against that. mesh.lvec goes through the owner, so only direct var.vec / var._lvec readers are exposed. I haven't checked whether the clamped owner value is itself partition-dependent, which would be worse.

    Part 3, not reproduced. follow_metric decides moved per rank (meshing/smoothing/api.py, _state["moved"] = not np.allclose(...)), and _polish runs an allreduce only where it is True. From reading, a rank whose nodes moved less than allclose tolerates, or a rank with no vertices, would leave the others waiting. I couldn't make ranks disagree: a sector front and a local bump on a 4 x 1 box at np 4 both moved every rank's nodes by at least 2e-3. q.min() needs an empty-rank guard too, and cells_polish can be None. On an empty rank the mover's own early return (mmpde.py, the TODO(BUG) at cells_all is None) comes first.

    Underworld development team with AI support from Claude Code

  2. lmoresi commented on Nov 6, 2026

    @lmoresi
    MemberAuthor

    Part 2 goes further than ghosts: under the default clamp, the remapped field itself depends on the partition.

    Annulus, cell size 1/8, a tanh thermal layer, P2 T, and a prescribed mesh.deform that is the same at every rank count, so only the transfer can differ. Owner values compared with the serial run by DOF coordinate (890 of 890 matched):

    | Largest |T - serial| | np 2 | np 4 | np 8 |
    |---|---|---|---|
    | REMESH_MONOTONE=clamp (default) | 1.1e-3 | 1.3e-3 | 3.0e-4 |
    | off | 2.2e-16 | 2.2e-16 | 2.2e-16 |

    With the clamp off, the transfer is partition-independent to round-off. The review of #959 suggested the mechanism: the clamp bounds each value with a rank-local neighbour set. This run doesn't separate that from anything else inside the clamp. The fix is to make the clamp's neighbour set independent of the partition, or to ask whether the clamp is still needed where it applies. 086cad25 added it for a parallel free-slip leak, and I haven't re-measured whether that leak still occurs with the clamp off.

    Underworld development team with AI support from Claude Code

  3. lmoresi commented on Nov 10, 2026

    @lmoresi
    MemberAuthor

    Part 2 of this issue has the same root cause as #682, which stays open: the monotone clamp in global_evaluate bounds each value with a neighbour set from a rank-local kd-tree. Parts 1 and 3 are separate and stay here.

    On development a6eebb6, _apply_monotone_limit (function/functions_unit_system.py, about lines 694-720) builds its kd-tree from the calling rank's var.coords_nd and clamps against that rank's var.data. The TODO(parallel) there says so. This is the mechanism #682 isolated: a departure point bounded by the neighbourhood of the rank that asked for it. Here it shows as a ghost and its owner getting different bounds, and as a remapped field that depends on the partition.

    Probe: a P2 tanh front on a 1/24 simplex box. All 4000 query points are asked on rank 0, and the run is compared with serial.

    import numpy as np, sympy, underworld3 as uw
    mesh = uw.meshing.UnstructuredSimplexBox(minCoords=(0, 0), maxCoords=(1, 1), cellSize=1/24, qdegree=3)
    T = uw.discretisation.MeshVariable("T", mesh, 1, degree=2)
    c = np.asarray(T.coords_nd)
    T.data[:, 0] = np.tanh((c[:, 0] + 0.3 * c[:, 1] - 0.6) / 0.03)
    rng = np.random.default_rng(1)
    P = rng.uniform(0.02, 0.98, (4000, 2)) if uw.mpi.rank == 0 else np.zeros((0, 2))
    plain = np.asarray(uw.function.global_evaluate(T.sym, P)).reshape(-1)
    cl = np.asarray(uw.function.global_evaluate(T.sym, P, monotone="clamp")).reshape(-1)
    # rank 0 saves both and compares them with the np 1 run
    np plain vs np 1 clamp vs np 1 points the clamp moved
    1 - - 49 (max 5.0e-4)
    2 0 1.99 1719
    4 0 1.51 2696

    The field lies in [-1, 1]. Plain evaluation doesn't depend on the partition, and the clamped value is wrong by up to 2 when the asking rank is not the owner. PR #800 moves the bound onto the rank that evaluates the point. PR #982 makes the mesh-move remap unclamped by default. Neither is on development.

    Script: /mnt/nvme0n1p4/home/lmorese/hyperion-work/runs/2026-11-10-sweep/E/probes/p682.py.

    Underworld development team with AI support from Claude Code

  4. lmoresi commented on Nov 10, 2026

    @lmoresi
    MemberAuthor

    A related rank asymmetry in the same mover path: a move that leaves one rank's local coordinates unchanged hangs. It reproduces on development a6eebb6 at np 2, and on bundle A's heads at np 2 and 4.

    Where. _remesh_with_field_transfer_impl (discretisation/remesh.py, about lines 378-383 on a6eebb6) decides "moved" on each rank from its own coordinates:

    new_X = np.asarray(mesh.X.coords).copy()
    if np.array_equal(new_X, old_X):
        return False

    A rank whose local coordinates did not change returns False. The ranks that did move go on into remap_var_set, whose deform-back calls the collective _deform_mesh. gdb at np 2 shows the moving rank in UW_DMPlexCreateCoordinateSpaceWithinPlex → DMGetCoordinates → MPI_Allreduce, while the other rank has already returned.

    Reproduction. Move only interior nodes of the last rank that no other rank holds, as owned or ghost, so every other rank's local coordinates are bit-identical:

    R = size - 1
    allc = comm.allgather(c)                       # c = np.asarray(mesh.X.coords)
    others = np.vstack([allc[q] for q in range(size) if q != rank])
    c2 = c.copy()
    if rank == R:
        d, _ = cKDTree(others).query(c)
        mine = (d > 1e-12) & interior(c)           # nodes only this rank holds, off the walls
        c2[mine, 0] += 0.004 * np.sin(np.pi * c[mine, 0]) * np.sin(np.pi * c[mine, 1])
    mesh.deform(c2)                                # hangs

    This moved 168 nodes at np 2 (Poisson box) and 66 at np 4 (box with a corner fault). It hangs before any solve: on development a6eebb6 (Poisson, np 2), on d22d225 (12 of 12 runs) and on cce8112.

    Fix. Allreduce moved before the early return:

    moved = mesh.dm.comm.tompi4py().allreduce(not np.array_equal(new_X, old_X), op=MPI.LOR)

    Every rank then takes the same branch. That also makes the mover's _move_version bump on #1036 rank-uniform by construction.

    A real trigger, from reading the code: a smoothing pass in which every node a rank holds is pinned, or below the mover's skip threshold. I did not build one from a real mover; the probe above forces the condition through mesh.deform.

    Probe: p2.py onerank in /mnt/nvme0n1p4/home/lmorese/hyperion-work/runs/2026-11-10-bundleA/review2/ on hyperion. The full context is in #1036's round-2 review record (F1).

    Underworld development team with AI support from Claude Code

  5. lmoresi commented on Nov 13, 2026

    @lmoresi
    MemberAuthor

    Verdict on development 70b240c: LIVE, for Part 1 only. The except Exception: continue in remap_var_set is still there, and a forced rank-local failure in it first desynchronises the ranks and then hangs them. The one-rank-move hang is FIXED. Ghost disagreement under the default settings is FIXED: the clamp is now off by default. The opt-in clamp is still rank-local, which is #682 (DUPLICATE).

    These probes ran on H1 (review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH)), with development 70b240c's Python over a build whose compiled files are the same. Every MPI run went through the repository's MPI supervisor.

    The fixture is a 1 x 1 simplex box (gmsh, cell size 1/16) with two REMAP variables: T (P2, a tanh front of width 0.03) and S (P1, the linear field 2x - y). mesh.deform moves the interior nodes by 0.02 sin(pi x) sin(pi y) in x. That goes through remesh_with_field_transfer, so remap_var_set runs.

    Part 1: rank-local skip of a collective (LIVE)

    On 70b240c, the resampling loop in remap_var_set (discretisation/remesh.py, about lines 518-538) still has both skips:

    target = new_dof_coords.get(var, None)
    if target is None:          # set when var.coords_nd raised on this rank
        continue
    try:
        ... val = uw.function.global_evaluate(var.sym, target, monotone=REMESH_MONOTONE)
    except Exception as exc:
        ...
        continue

    #959 removed the target.size == 0 skip, but these two remain. Mutant, applied by import hook with no file edited: on rank 1 only, the first global_evaluate call from remap_var_set raises RuntimeError before it enters any collective.

    np 2, same fixture:

    MOVING nodes per rank [148, 145]
    MUTANT: rank 1 raises in remap_var_set
    === MPI SUPERVISOR: no output for 301 s ===
      rank 0 is in get_max_radius()      <- global_evaluate -> swarm.migrate -> points_in_domain (remesh.py:527)
      rank 1 is in nuke_coords_and_rebuild()  <- _deform_mesh(new_X) (remesh.py:544)
      both spinning in MPI_Allreduce; native frames did not move between samples
    

    Rank 1 skipped T and evaluated S. Rank 0 was still evaluating T, and rank 1's collectives for S completed against rank 0's for T, so the two ranks silently exchanged data that belonged to different variables. Rank 1 then went on to the deform-forward, while rank 0 entered S's global_evaluate, and both ranks hung there. So this failure first desynchronises the ranks and then hangs them. With a single variable it would go straight to a hang. With more variables, it could pair up evaluations of the wrong variables for longer before it hangs.

    I haven't found a natural rank-local raise in global_evaluate; the mutant forces one. [read] Any exception raised before the first collective on one rank only will do this, for example a var.coords_nd failure on that rank (the target is None branch).

    Fix, size S: agree on failure across ranks before skipping. Use one allreduce(LOR) of a per-rank failure flag after each global_evaluate attempt (or around the whole loop), then either skip the variable on every rank or raise on every rank. Treat target is None the same way. This touches only remesh.py, not solver defaults, petsc_generic_snes_solvers or rotated_bc.py.

    The one-rank-move hang from 2026-11-10 (FIXED)

    The early return now decides over all ranks (remesh.py line 387):

    moved = uw.mpi.comm.allreduce(not np.array_equal(new_X, old_X), op=uw.MPI.LOR)

    Probe: as on 2026-11-10, move only interior nodes on the last rank that no other rank holds as owned or ghost. Every other rank's coordinates stay bit-identical.

    np nodes moved per rank result
    2 [0, 128] deform returned; largest gap on a shared DOF 2.6e-15 (T), 2.2e-16 (S)
    4 [0, 0, 0, 59] deform returned; same

    Part 2: ghosts after the transfer (FIXED under the default)

    REMESH_MONOTONE now defaults to off (remesh.py lines 56-65), so the mesh-move remap no longer applies the clamp unless the environment variable asks for it. All ranks move. For each DOF coordinate held by more than one rank, I measured the largest gap in var.data between ranks. I also compared every value with the serial run, matched by coordinate (1293 of 1293 T DOFs, 340 of 340 S DOFs).

    np 2 np 4
    T: largest gap on a shared DOF 1.1e-16 4.4e-13
    S: largest gap on a shared DOF 0 2.4e-13
    T: largest |T - serial| 1.1e-16 4.4e-13
    S: largest |S - serial| 0 2.4e-13

    So under the default, ghosts and owners agree to round-off and the transfer doesn't depend on the partition. _write_var_data still writes ghosts directly. At the end of the transfer, _remesh_with_field_transfer_impl sets mesh._stale_lvec = True on every rank, so the next update_lvec rebuilds the mesh vector through the owner. [read]

    With the clamp opted in (REMESH_MONOTONE=clamp), the same fixture shows it is still rank-local:

    REMESH_MONOTONE=clamp np 1 np 2 np 4
    T: largest gap on a shared DOF - 0.45 0.40
    S: largest |S - exact| on a linear field 1.1e-2 4.0e-2 4.0e-2

    _apply_monotone_limit (function/functions_unit_system.py, about lines 695-725) still builds its kd-tree from the calling rank's var.coords_nd and bounds against that rank's var.data; the TODO(parallel) is unchanged. That is #682's root cause, which stays open there. It is no longer on this path by default.

    One unrelated observation, unexplained: with the clamp off, S, a linear P1 field, comes back 1.0e-5 from exact. That is the same at every rank count, so it doesn't depend on the partition. I have not looked into it.

    Size

    Part 1 is S and only touches remesh.py. Part 2 needs nothing here beyond #682, unless the clamp becomes the default again. Neither touches solver defaults, petsc_generic_snes_solvers or rotated_bc.py.

    Logs kept by the reviewer.

    Underworld development team with AI support from Claude Code

  6. added
    fixed-in-development
    on Nov 13, 2026
  7. lmoresi commented on Nov 13, 2026

    @lmoresi
    MemberAuthor

    Fixed in development by PR #1142 (merge commit 14134eb) for the remap path: remap_var_set decides each variable's skip and each variable's outcome collectively, so a failure on one rank (unreadable coordinates, or an error after global_evaluate's collectives, including inside the semi-Lagrangian history hook) fails that variable on every rank, keeps its old values everywhere, and remesh_with_field_transfer then raises the same error on every rank. A raise inside global_evaluate itself still leaves the other ranks waiting; that is a requirement on the point-locator rework. The issue stays open until development is released to main.

    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

    fixed-in-development

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions