Skip to content

[BUG] - swarm.write_timestep fails in parallel (np > 1) #151

Closed

Description

@bknight1

On latest dev branch. Script to recreate:


# %%
import underworld3 as uw
from underworld3.systems import Stokes
import numpy as np
import sympy
import os
from petsc4py import PETSc



# %%
res = 0.2
mesh = uw.meshing.UnstructuredSimplexBox(
    minCoords=(-1.0, 0.0),
    maxCoords=(1.0, 1.0),
    cellSize= res,
    regular=False,
    qdegree=3,
)

# %%
swarm = uw.swarm.Swarm(mesh=mesh)
material      = swarm.add_variable(name="material", size=1, dtype=PETSc.IntType)
swarm.populate(fill_param=4)


swarm.write_timestep(filename='swarm', swarmname='swarm', index=0, outputPath='./', swarmVars=[material]) 

Activity

  1. lmoresi commented on Apr 28, 2026

    @lmoresi
    Member

    @bknight1 — root cause found, fix in PR #152.

    Root cause

    `Swarm.save()` (and `SwarmVariable.save()` for the same reason) called

    ```python
    h5f.create_dataset("coordinates", data=points_data_copy)
    ```

    collectively on the parallel HDF5 path, but each rank passed its own local-sized array. In parallel HDF5 every rank must specify the same dataset shape on a collective `create_dataset`; passing different shapes leaves HDF5's internal metadata inconsistent and the collective close never synchronises, so the with-block exit hangs silently. Faulthandler stack dump confirmed both ranks stuck in `h5py.File.exit` → `close()`.

    Why this only surfaced now

    The bug is shape-dependent: it only manifests when ranks have unequal local particle counts. A perfectly balanced run (which is unusual but not impossible — e.g. some structured-mesh + populate-uniformly cases) would coincidentally specify the same shape on every rank and complete normally. Your reproducer triggers it because `UnstructuredSimplexBox` partitions don't balance evenly: at np=2 I see 990 vs 930 particles, which is enough shape mismatch to trip the collective.

    Fix

    Use the standard parallel-HDF5 collective pattern: `allgather` the per-rank sizes, create the dataset at the global shape, then each rank writes its own slice. Plus two latent bugs in the sequential fallback paths cleaned up along the way (a misplaced `comm.barrier()` and an undefined-variable `NameError`).

    Verification

    Your reproducer at np=2:

    • Pre-fix: hangs forever (15s faulthandler dump confirms close()).
    • Post-fix: completes in 0.11s.

    Also verified at np=4 (0.12s). New regression test (`tests/parallel/test_0790_swarm_write_timestep_mpi.py`) wraps your reproducer with a 60s pytest timeout so any return of the deadlock fails fast in CI rather than blocking.

    PR #152 ready for review.

    Underworld development team with AI support from Claude Code

  2. bknight1 commented on Apr 29, 2026

    @bknight1
    MemberAuthor

    Thanks @lmoresi, confirmed working.

  3. added 2 commits that reference this issue on May 1, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions