Skip to content

add_particles_with_coordinates: a point on a partition seam is added once - #1272

Merged
lmoresi merged 5 commits into
developmentfrom
bugfix/add-particles-one-owner-per-point
Nov 20, 2026
Merged

lmoresi merged 5 commits into
developmentfrom
bugfix/add-particles-one-owner-per-point

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 20, 2026

Copy link
Copy Markdown
Member

What changed and why

UNFILED 20 from the parallel review desk's review of #1252. #1252 waits on this PR.

Swarm.add_particles_with_coordinates is given the same coordinate array on every rank and keeps the points Mesh.points_in_domain claims for that rank. A point on a partition seam lies in the cells of every rank on that seam, so each of them claimed it and added it. On a unit box at np 2 the review desk's ten points became eleven, or twelve with a band partition. It happens for a seam point inside the mesh and where a seam meets a wall. On development, points clamped onto a wall sat 1e-3 inside it and escaped; #1252 snaps them exactly onto the wall, so there they would duplicate too.

Now add_particles_with_coordinates asks Mesh._points_owned_here, which gives each claimed point to the lowest rank that claims it:

  • Only a point in a cell on a partition seam can be claimed by more than one rank, because cells of different ranks meet only there. The seam is read from the point star forest (Mesh._seam_points, a leaf here or a root with leaves elsewhere), not from coordinates.
  • Each rank gathers the claimed points in its seam cells. Two ranks hold the same point when its coordinates are identical, since they were given the same array; the point goes to the lowest of them.
  • A rank given a point no other rank was given keeps it, so pre-partitioned input is added as before.
  • A point no rank claims is added by none, as before.
  • Every rank reaches the seam-point reduction and the gather, a rank without cells included.

points_in_domain is unchanged: it stays a per-rank claim, and a seam point stays claimed by every rank on the seam. Its docstring now says the claim is not exclusive and points to _points_owned_here. Its other callers see no change.

What a user's run does now: at more than one rank, add_particles_with_coordinates adds each given point once. Before, a point on a seam was added once per rank on the seam, and migrate() kept every copy.

#1258 (global_evaluate's ownership, not merged) applies the same lowest-rank rule to its evaluation swarm after migration, where the question is containment of points already distributed. The two share the rule, not code, so no shared helper is introduced.

Test

tests/parallel/test_1260_add_particles_one_owner_per_point_parallel.py (level 1, tier B). The test points are the mesh's own seam vertices, found from the point star forest, plus cell centroids. The baseline is exact: every given point is in the swarm once, at the given coordinates.

case development, np 2 development, np 3
seam vertices inside the box 11 became 22 16 became 33
seam vertices on a wall 2 became 4 3 became 6
seam vertices and cell centroids 53 became 66 79 became 99
triangle with a rank without cells (no seam at np 2) 20 became 24
points given to rank 0 only (pre-partitioned) passes passes

The pre-partitioned case passes on development; it was added after a first version of this fix raised on a rank given an empty array before the gather, which hung the others (found by the swarm files with a rank holding no points). With the fix, all five pass on every rank at np 2, 3 and 4. scripts/test.sh runs the file at np 3 always, because the triangle leaves a rank empty only from three ranks.

The review desk's reproducer at np 2 and 3: 10 points give 10 after the add and 10 after migrate() (points_in_domain still claims the seam point on two ranks, as stated).

Cost

_points_owned_here against points_in_domain alone (the old path), uniform random points on a unit box of cell size 0.02, best of three, slowest rank:

1e4 points 1e6 points
np 1 0.0153 s, 0.0152 s 1.473 s, 1.445 s
np 4 0.0249 s, 0.0263 s (+6 %) 2.243 s, 2.346 s (+5 %)

What was run

On a 96-thread Linux workstation (single system image, Intel Xeon Gold 6240R, MPICH):

What Result
new test, np 2, 3, 4 4 passed on every rank
12 serial files using add_particles_with_coordinates or points_in_domain, with the collective guard scan 156 passed, 3 skipped, 4 xfailed
26 serial swarm files 183 passed, 1 skipped
12 parallel files using these paths (swarm advection and evaluation with an empty rank, swarm statistics, and others), np 2 all 12 pass on both ranks
collective guard scan 20 passed
scripts/check_test_coverage.py, scripts/check_deprecated_patterns.py OK

Not run

Above np 4; a band partition (the default partitions give both cases); 3-D meshes; meshes that are not straight-sided simplices (their interior claims carry no cell, so every claimed point there is compared, which is correct but untested); the full suite; macOS.

Underworld development team with AI support from Claude Code

On development at np 2, 11 interior seam vertices become 22 particles and
2 wall-and-seam vertices become 4; at np 3, 16 become 33 and 3 become 6,
and the triangle with a rank without cells gives 24 for 20.

Underworld development team with AI support from Claude Code
… to the lowest

Mesh._points_owned_here compares only the claimed points in cells on a
partition seam (from the point star forest): it gathers them, treats
identical coordinates as one point, and gives each to the lowest rank
claiming it. points_in_domain is unchanged and says its claim is not
exclusive.

Underworld development team with AI support from Claude Code
…ranks)

Underworld development team with AI support from Claude Code
On the previous commit a rank given an empty array raised in the seam
comparison before the gather, and the others waited (np 2, 3, 4).

Underworld development team with AI support from Claude Code
…eaches the gather

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 20, 2026 06:58

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@lmoresi

lmoresi commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Review from the Underworld development team with AI support from Claude Code (H1: review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH))
(H1 is the session that reviews pull requests and runs the parallel tests.)

Review of #1272 at 04db0a8

Verdict at 04db0a8: ready to merge on green from this review.

The fix does what it says. A point on a partition seam is now added once, at np 2, 3 and 4, in 2-D and 3-D, on simplex and quad meshes, with empty ranks, a rank with no cells, pre-partitioned input and mixed input. Nothing hung. No point was lost. Off the seams, head and development add the same points. There is one cost finding (worst case only) and two small notes. None blocks the merge.

Method

1. The new test file, head vs development

np head development (merge base)
2 5 passed 3 failed: interior seam 11 → 22, wall-and-seam 2 → 4, seam and cell 53 → 66
3 5 passed 4 failed: 16 → 33, 3 → 6, 79 → 99, triangle (rank without cells) 20 → 24
4 5 passed 4 failed: 19 → 40, 4 → 8, 103 → 128, triangle 24 → 34

The pre-partitioned test passes on development too. That is expected, because nothing there changed. The new test.sh line runs the file at np 3 under shard 6, with the other always-on np 3 runs. The np 2 and full-parallel passes also pick the file up through the tests/parallel/test_*.py glob. So CI runs it at np 2 and 3, and at 4 with --full-parallel.

2. The reproducer from the #1252 review (ten points on the walls of a unit simplex box, cell size 0.1)

Counts are after add_particles_with_coordinates and after migrate() (they are the same in every row).

np partition development, exact head, exact development, clamped head, clamped head + #1252, exact / clamped
1 – 10 10 10 10 10 / 10
2 default 11 10 10 10 10 / 10
2 band 12 10 10 10 10 / 10
3 default 11 10 10 10 10 / 10
3 band 10 10 10 10 10 / 10

On development, "clamped" points end 1e-3 inside the wall, away from the seam. That is why they are not duplicated. With #1252 merged they land exactly on the wall, and #1252 alone gives 12 at np 2 band (from the #1252 review). With this PR merged too, they are added once.

3. Case matrix: expected / head / development (total particles)

"Expected" is the set of distinct coordinates that some rank was given and also claims (points_in_domain on that rank's own input). For the "×2" rows it is twice that set. In every cell below, head has no lost point, no extra point and no duplicate beyond the expected count. Per-rank counts and return values agree with the totals, and migrate() leaves the counts unchanged.

The point set is the seam vertices, seam edge midpoints (and seam face centroids in 3-D), vertices held by 3 or more ranks, up to 40 cell centroids per rank, 200 random points and about 30 wall vertices.

config (cells per rank) shared array last rank passes none pre-partitioned (each rank passes what it claims) pre-partitioned, rank 0 passes none mixed (half the ranks pass the shared array) shared array ×2 seam points only ×2 1e-12 outside a wall within 1e-12 of a seam vertex
2-D np 2 default (119/123) 343/343/368 186/186/186 343/343/368 182/182/182 343/343/368 686/686/736 50/50/100 40/40/42 25/25/50
2-D np 2 band 341/341/364 176/176/176 341/341/364 188/188/188 341/341/364 682/682/728 46/46/92 40/40/42 23/23/46
2-D np 3 default 394/394/432 276/276/287 394/394/432 279/279/294 394/394/432 788/788/864 74/74/150 40/40/43 37/37/75
2-D np 3 band 412/412/468 285/285/320 412/412/468 292/292/313 412/412/468 824/824/936 112/112/224 40/40/44 56/56/112
2-D np 3 emptyband (121/121/0) 341/341/364 341/341/364 341/341/364 188/188/188 341/341/364 682/682/728 46/46/92 40/40/42 23/23/46
2-D np 4 default 441/441/488 338/338/360 441/441/488 346/346/372 441/441/488 882/882/976 90/90/184 40/40/44 45/45/92
2-D np 4 band 473/473/552 360/360/416 473/473/552 364/364/410 473/473/552 946/946/1104 158/158/316 40/40/46 79/79/158
2-D np 4 emptyband (87/73/82/0) 412/412/468 412/412/468 412/412/468 292/292/313 412/412/468 824/824/936 112/112/224 40/40/44 56/56/112
triangle np 3 (8/8/0) 133/133/140 133/133/140 133/133/140 65/65/65 133/133/140 266/266/280 14/14/28 9/9/10 7/7/14
triangle np 4 (6/5/5/0) 138/138/155 138/138/155 138/138/155 91/91/99 138/138/155 276/276/310 30/30/64 9/9/12 15/15/32
3-D np 2 default 402/402/495 253/253/253 402/402/495 242/242/242 402/402/495 804/804/990 186/186/372 63/63/97 93/93/186
3-D np 4 default (7 vertices on 3+ ranks) 566/566/761 468/468/572 566/566/761 461/461/551 566/566/761 1132/1132/1522 362/362/752 95/95/169 181/181/376
3-D np 4 band (15 vertices on 3+ ranks) 746/746/1131 639/639/903 746/746/1131 653/653/913 746/746/1131 1492/1492/2262 740/740/1510 156/156/304 370/370/755
quad np 2 default 339/339/360 187/187/187 339/339/360 173/173/173 339/339/360 678/678/720 42/42/84 40/40/42 21/21/42
quad np 4 default 377/377/420 289/289/311 377/377/420 293/293/315 377/377/420 754/754/840 82/82/168 40/40/44 41/41/84

Also run in every config, all matching on head:

  • Every rank passes zero points: 0 everywhere, no hang.
  • Rank 0 passes none.
  • Round-robin pre-partitioning, where a point given to a rank that does not hold it is dropped. Head and development drop the same points.
  • Points pushed out of a wall by 1e-15, 1e-9, 1e-6, 1e-3 and 0.5. The face-tolerance claims (up to 1e-6 here) are kept as on development, minus the seam duplicates. The 1e-3 and 0.5 points are rejected on both.
  • A swarm variable on a populated swarm keeps its values through an add (0 wrong values). The API attaches no data to the added points. Kept points keep their input order (coordinatesArray[valid]).

Development duplicates more than exact seam points. In 1e6 uniform random points at np 4, development added 46 extra particles in 2-D and 54 in 3-D. These are face-tolerance claims near a seam. Head added exactly 1e6. The same code path serves Swarm.read_timestep(migrate=True), which every rank calls with the full saved array. So swarm restarts gain the same fix.

4. Identical coordinates

  • Within one rank's array, duplicates are kept everywhere. On a seam, the owning rank keeps every copy ("×2" rows above). Off a seam, the one claiming rank keeps every copy.
  • The same coordinates from different ranks are one point. That holds on and off a seam: off a seam only one rank claims the point, so only one copy is ever added (development behaved the same there).

So the rule is consistent between seam and non-seam points. Section 5, finding 2 has the one corner where it shows.

5. Findings

  1. Medium: the worst case costs 2–4× when most points lie on seams. The gather is an allgather to every rank of every rank's claimed points in seam cells, followed by numpy.unique(axis=0) on the whole gathered array. Median of three timed adds after one warm-up, np 4, two rounds where given:
case (1e6 points, simplex 2-D, cell size 0.02, np 4) head (s) development (s) particles head / development
uniform 2.60, 3.56 2.56, 2.47 1,000,000 / 1,000,046
random in seam cells 7.04, 6.92 6.04, 5.55 1,000,000 / 1,000,591
on seam edges 7.40, 5.16 1.98, 3.05 1,000,000 / 2,000,030
2 vertices held by 3 ranks, repeated 5.66, 7.39 1.78, 1.64 1,000,000 / 3,000,000
band partition, on seam edges 5.58 1.76 1,000,000 / 2,000,000
band partition, in seam cells 6.79 5.55 1,000,000 / 1,000,628

At np 4, the share of claimed uniform points that are gathered was 8.2% (2-D simplex, cell size 0.02), 8.7% (quad 50×50) and 35.4% (3-D simplex, cell size 0.08). That share grows with rank count. Every rank receives the seam-cell points of every other rank, not only of its neighbours. This is not a blocker for the fix. A follow-up could exchange only with the ranks that share the seam (the point star forest names them), or route each point to one rank by a hash of its coordinates.

  1. Low: one point passed twice on one rank and once on another gives 2 or 1 particles, depending on which rank is lower (np 2, an interior seam vertex: rank 0 twice → 2, rank 1 twice → 1; development 3 both ways). This follows from "same coordinates from different ranks are one point" together with "keep duplicates within a rank". It is a corner case, and the result is still closer to the intent than development. A sentence in the add_particles_with_coordinates docstring would make the rule visible: same coordinates on different ranks are the same point; repeats within one rank's array are kept.

  2. Low: in scripts/test.sh the new block has no blank line before the next comment (# adapt(engine='nvb') ...). Cosmetic.

Collective safety (read): _points_owned_here has one rank-local branch, the closure test if located.shape[0] > 0 and seam_vertices.shape[0] > 0. No collective is inside it. Every rank reaches _seam_points() (an SF degree reduction), the count allgather and, unless the gathered count is zero everywhere (a reduced value), the point allgather. The early return at size 1 uses communicator geometry. _classify_points_in_domain keeps its collective get_max_radius() ahead of its early returns. The guard scan (tests/test_0052_collective_guard_scan.py) passes. The shape fix in 04db0a8 (reshape(-1, self.cdim) on the input) is what lets a rank given no points reach the gather. The all-empty, one-empty and no-cells runs above confirm it.

Other details checked in the diff: identity is exact float equality of the raw input rows, which is right because every rank holds the same array. group.reshape(-1) covers NumPy 2's inverse shape for unique(axis=0). closure_points returns plex point numbers padded with -1, the same numbering as the seam vertex set, and -1 never matches. On meshes where the claim leaves some points unlocated, those points are compared too. On the quad mesh here every claimed point was located.

Cost in the common case (uniform points), median of three after a warm-up. Other jobs were running on the machine, so the spread between rounds is about ±30%.

mesh, np, points head (s) development (s)
simplex 2-D, np 1, 1e4 0.017, 0.014 0.014, 0.014
simplex 2-D, np 1, 1e6 1.41, 1.40 1.42, 1.78
simplex 2-D, np 4, 1e4 0.049, 0.033 0.029, 0.046
simplex 2-D, np 4, 1e6 2.60, 3.56 2.56, 2.47
simplex 3-D, np 4, 1e5 0.81, 1.31 1.11, 0.76
simplex 3-D, np 4, 1e6 9.39, 9.23 9.19, 8.03
quad, np 1, 1e6 3.06 2.72
quad, np 4, 1e6 2.30 1.80

At np 1 the new code returns before any extra work. The np 1 differences are noise. At np 4 the overhead in the common case is within the noise, up to about +25%. That fits the author's +5–6%, given this load.

6. Callers and other tests (head)

  • add_particles_with_coordinates is the only caller of _points_owned_here. In the library it is also called by Swarm.read_timestep(migrate=True). points_in_domain itself is unchanged.
  • Parallel tests, np 2 and np 4, all passed: test_0020 (swarm values snapshot), 0755, 0756, 0757, 0760, 0765, 0766, 0767, 0777, 0790, 0795, 0796, 0974, 1068 (rotated nodal normal), 1093 (migration reaches the containing rank), 1213 (points_in_domain exact) and ptest_0762. ptest_003 (swarm projection) is a script, not a pytest module: under pytest it stops at collection (it builds objects at import), so I ran it as a script at np 2 and 4, and it finished on every rank.
  • Serial: 52 swarm, snapshot, reduction and guard-scan files with 4 workers: 561 passed, 4 skipped, 11 xfailed.

Run and not run

Run: everything above.

Not run:

  • The full parallel suite.
  • np above 4, so the gather's scaling with rank count is inferred from the gathered share, not measured.
  • Curved or spherical meshes.
  • A 3-D quad (hex) mesh.
  • Repeats of the worst-case timings on an idle machine.

Logs kept by the reviewer.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit 426736e into development Nov 20, 2026
10 checks passed
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.

3 participants