Skip to content

IBM: sparse per-step ib_heat path indexed by local body slot (no O(num_gbl_ibs) work per step) - #2330

Merged
sbryngelson merged 2 commits into
MFlowCode:masterfrom
sbryngelson:perf/ib-relax-sparse
Nov 21, 2026
Merged

sbryngelson merged 2 commits into
MFlowCode:masterfrom
sbryngelson:perf/ib-relax-sparse

Conversation

@sbryngelson

Copy link
Copy Markdown
Member

Contribution Policy

All contributions are expected to demonstrate:

  • A clear understanding of the codebase
  • Alignment with product direction
  • Thoughtful reasoning behind changes
  • Evidence of real-world usage or hands-on experience with the problem

If these expectations are not met, we would prefer to implement the changes ourselves rather than spend time reviewing low-effort submissions.

Summary

One commit on origin/master cd2180c. Whenever ib_exchange is true (a thermal_bc = 3 body, ib_heat_wrt, or ib_mass_relax > 0, which is now every moving body by default) each step did O(num_gbl_ibs) work on every rank: ib_heat(5, N) and ib_part(3, 32, N) indexed by global id, four device loops over 1..N, a device-to-host copy, an MPI_Allreduce and a host-to-device copy of ib_heat (40 N bytes each way), plus a replicated ib_mass_res(2, N) bank and a setup allreduce. For 1e5 or more bodies that is the bottleneck. This makes the per-step path O(bodies a rank holds):

  • ib_heat(7, cap) and ib_part are indexed by patch_ib slot (the rank's own bodies plus its neighbors', as s_build_ib_neighborhood and s_handoff_ib_ownership already maintain; ib_lookup_* maps a global id to a slot). cap = min(num_gbl_ibs, 2 x max over ranks of num_ibs + 64), allocated once. ib_shell stores the slot. The bank is patch_ib%mass_bank (already in the struct and in the restart record) and is advanced by the owner; ib_mass_res is removed.
  • With several ranks, s_ib_exchange_heat replaces the allreduce: one device-to-host copy of 7 reals per held body; each rank sends the nonzero partial sums of bodies it does not own to the owner (one nonblocking message per neighbor rank), the owner adds them in ascending source-rank order and steps the bank; the owner returns the totals and the bank release only to the ranks that sent. No collective, no loop over num_gbl_ibs. One rank keeps the device-only path with no host copy.
  • At an ownership handoff the bank moves with the body (s_ib_migrate_banks, one message per old-owner/new-owner pair, exact count).
  • s_update_ib_temperatures and s_conserve_ib_mass now run before s_wrap_periodic_ibs/s_handoff_ib_ownership in s_tvd_rk, because a handoff renumbers slots (this also removes a read of ghost_points%ib_patch_id after a compaction).
  • Handoff fix: a rank that appears in several ib_neighbor_ranks slots (periodic, few ranks) sent and received each arrival once per slot, so patch_ib could hold duplicate entries and num_ibs could exceed num_gbl_ibs. On master this is harmless to the results (everything is indexed by global id), but it overflows the slot arrays here, so the handoff now takes each sending rank once. It reproduces on master: the 4-body straddle case below at 8 ranks prints num_ibs > num_gbl_ibs after 108 handoffs (checked with a one-line print). It may merit its own PR; a handoff abort (@:PROHIBIT) now guards num_ibs > cap.

The restart record is unchanged. ib_heat_wrt output already came from per-owner patch_ib fields, so it has no allreduce to keep. Startup items that remain O(N) per rank and are not changed here: the ib_state reader scans all records unless file_per_process; s_check_every_patch_marked allreduces N counters once; patch_ib and the lookup arrays are sized by num_ib_patches_max_namelist; the handoff host-updates all of patch_ib each step.

Evidence

Timer: wall time per step of s_update_ib_temperatures plus s_conserve_ib_mass (benchmark-only instrumentation, not in the commit). It includes the O(local cells) shell and conserve kernels, identical in both versions. Case: periodic 2D box, N free-floating circles at 12.5 % area fraction, 4 cells per diameter, ib_mass_relax = 30. "dense" is the code before this PR (#2301 head), "sparse" this PR. These timings were taken before the last two rebases (the code path is unchanged by them).

MI300A (amdflang, OpenMP offload), 1 rank per APU, ms per step, dense / sparse:

N 1 rank 2 ranks 4 ranks
1000 0.30 / 0.19 0.70 / 1.77 0.70 / 0.99
3000 0.41 / 0.40 0.64 / 1.22 0.79 / 1.33
10000 0.44 / 0.46 0.99 / 1.14 1.34 / 1.96

At 2 and 4 ranks every rank is a neighbor of every other and holds all N bodies, so there is nothing to save and the sparse exchange (two message phases and an extra device copy) is slower than one allreduce, by 0.1 to 0.6 ms per step. The benefit appears only when ranks hold a fraction of the bodies. That needs many ranks; I could only run that on CPU (same compiler, no GPU), ms per step, dense / sparse:

N 16 ranks (4x4) 64 ranks (8x8)
1000 2.92 / 2.06 2.67 / 0.70
3000 6.85 / 6.90 4.32 / 2.13
10000 22.3 / 23.7 15.8 / 7.8
30000 53.2 / 16.4
100000 155.7 / 53.2

At 64 ranks each rank holds 9/64 of the bodies: the dense time stays at about 0.10 ms per N/64 and the sparse time at 0.035 to 0.05, about 3x less (the shared cell kernels bound the ratio). At 16 ranks it is a wash. The per-step byte figures for 1e5 bodies on 1000 ranks (about 4 MB plus an allreduce before, about 0.3 MB after) are arithmetic from the layout, not measured; no GPU run used more than 4 ranks.

Correctness: four prescribed moving circles on or crossing rank boundaries (handoffs, bank migration), 300 steps, ib_mass_relax = 30, final restart file compared with the code before this PR (current master). CPU, deterministic: bitwise identical (fields and ib_state, banks included) at 1, 2, 4, 8 and 16 ranks. The difference between 1 rank and 2/4/8 ranks is 5.7e-12 relative in both builds (partition round-off). On MI300A, where GPU atomics are not deterministic, two runs of the same binary differ by 1e-12 to 1e-9 and sparse versus dense across 1, 2 and 4 ranks falls inside that spread (banks agree to 1e-15). An earlier comparison against the #2301 head showed an 8.9e-10 field difference at 8 and 16 ranks; a per-step check of the exchanged totals and the bank release against an allreduce showed no difference above 1e-12 relative, and against current master the results are bitwise equal, so I believe that was round-off amplification, but I did not prove it.

Tests

No new test: the existing moving-body tests now use this path (ib_mass_relax defaults to on for them), including the ib_mass_relax restart checks and the 2-rank moving-sphere tests.

machine label result
MI300A (AFAR amdflang, OpenMP offload) IBM (110) 110 passed
MI300A AMR (112) 112 passed

Run on the commit before the final rebase onto cd2180c, which only changes AMR goldens. ./mfc.sh precheck passes. Not run: A100 / NVHPC (not reachable), Cray CCE and MI250X (CI covers them), the Chemistry label.

Limitations

  • Slower than one allreduce when every rank holds every body (2 to 4 ranks in 2D); faster only when each rank holds a fraction of the bodies.
  • The device-to-host copy is still O(bodies a rank holds) (56 B per held body), not O(shared bodies).
  • cap has a 2x margin set at start-up; clustering beyond it aborts at a handoff instead of growing.
  • At 8 ranks the force on one prescribed circle differs by 1.4e-2 from the 1-rank value, in the code before this PR too; it is a force-reduction issue, not part of this change.
  • GPU coverage is MI300A only; the many-rank scaling is CPU only; the 1000-rank figure is arithmetic.

This PR was made with Claude Code (AI).

@sbryngelson sbryngelson added the ci:phoenix Run the Phoenix (NVHPC GPU) jobs on this PR; push after adding label Nov 19, 2026
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Claude Code Review

Head SHA: c99e7cb

Files changed:

  • 5
  • src/common/m_constants.fpp
  • src/common/m_derived_types.fpp
  • src/simulation/m_data_output.fpp
  • src/simulation/m_ibm.fpp
  • src/simulation/m_time_steppers.fpp

Findings:

  • medium src/simulation/m_ibm.fpp (s_ib_exchange_heat, phase 1 and 2): a rank sends its partial only when any(ib_heat(1:5,i) /= 0), and the owner replies only to ranks that sent. A non-owner that holds cells of a body but has an all-zero partial (no surface or shell cells of it) never gets the totals. Its ib_heat(1:5) stays zero, so patch_ib%mdot, mdot_face, qdot and Twall are computed from local zeros. The old allreduce gave every rank the totals. Twall may be read there, for example by ghost points whose gp_surf(1,:) is 0. ib_heat(7) is not at risk, since a rank needs it only where ib_heat(5)>0, and that case is sent. Fix: have the owner reply to every rank that holds the body, or confirm that no consumer reads these rows on such ranks. This is a moderate-confidence concern from the diff alone; I did not verify the consumers.
  • medium src/simulation/m_ibm.fpp (handoff, s_ib_migrate_banks): mass_bank is advanced only on the device in s_update_ib_temperatures, in the num_procs > 1 branch and in the single-rank branch. The handoff then reads and writes the host patch_ib(j)%mass_bank, then does GPU_UPDATE(device='[patch_ib, num_ibs]'). If s_handoff_ib_ownership does not refresh patch_ib from the device first, the owner's stale host bank is sent to the new owner and also overwrites the device value. The earlier ib_mass_res array was never touched by the handoff. I could not see the start of the handoff routine in the prepared context. Confirm it does a GPU_UPDATE(host='[patch_ib]') before the bank is read at the "already hold changed owner" branch. Otherwise, add one.

@sbryngelson
sbryngelson marked this pull request as ready for review November 19, 2026 17:11
@sbryngelson
sbryngelson requested a review from a team as a code owner November 19, 2026 17:11
@sbryngelson
sbryngelson requested review from danieljvickers and removed request for a team November 19, 2026 17:11
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.28571% with 36 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.75%. Comparing base (0a22730) to head (f8a7221).
⚠️ Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
src/simulation/m_ibm.fpp 75.55% 15 Missing and 18 partials ⚠️
src/simulation/m_time_steppers.fpp 0.00% 0 Missing and 2 partials ⚠️
src/simulation/m_data_output.fpp 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2330      +/-   ##
==========================================
+ Coverage   65.72%   65.75%   +0.03%     
==========================================
  Files         111      111              
  Lines       30493    30647     +154     
  Branches     4663     4687      +24     
==========================================
+ Hits        20041    20152     +111     
- Misses       6692     6711      +19     
- Partials     3760     3784      +24     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sbryngelson

Copy link
Copy Markdown
Member Author

GPU measurements for this PR on MI250X (Cray compiler, OpenACC, one rank per GCD, 8 per node), 8 to 64 ranks. Dense is this PR's base (cd2180c), sparse is this head (c99e7cb); both also carry #2335 and #2340, without which this periodic moving-circle case does not run on either. Case: periodic 2D box, N free circles (moving_ibm = 2, 4 cells per diameter, 12.5 % area fraction), ib_mass_relax at its default (30). Milliseconds per step, mean over steps 5 to 30 (5 to 15 for N = 1e5); "relax" is s_update_ib_temperatures + s_conserve_ib_mass; "held" is the bodies a rank holds (own plus neighbors').

bodies ranks held per rank dense relax sparse relax dense step sparse step
1000 1 1000 0.31 0.33 23.4 23.5
3000 8 2250 0.62 0.43 110.9 111.4
10000 8 7500 1.06 0.61 115.6 116.1
30000 8 22500 1.74 1.03 132.7 132.0
3000 64 422 0.89 0.37 109.6 109.7
10000 64 1406 1.20 0.42 110.5 110.3
30000 64 4219 1.95 0.69 113.9 113.6
100000 32 28125 9.83 1.35 141.1 138.1
100000 64 14062 5.71 0.79 124.0 121.5

This comment was made with Claude Code (AI).

…_relax, lumped wall, ib_heat_wrt)

The per-step heat/mass table, its shard sums, the relax bank and the shell map are indexed by patch_ib slot, not global id, so a step costs O(bodies a rank holds). Several ranks combine a body's partial sums through its owner with point-to-point messages to neighbor ranks instead of an allreduce over every body. The bank is patch_ib%mass_bank, held by the owner and moved with the body at an ownership handoff.
@github-actions

Copy link
Copy Markdown

Lines of Code

File Lines Diff
src/simulation/m_ibm.fpp 3849 +144
src/simulation/m_data_output.fpp 1925 -3
Directory Lines Diff
simulation 41033 +141
total 61104 +141

On NVHPC GPU builds the conditional store in s_ib_shell_mass left the shell mass at zero, so the relax correction was never applied.
@sbryngelson

Copy link
Copy Markdown
Member Author

Fix pushed as f8a7221.

The 13 moving-body tests (and the fixed-body ib_mass_relax test) failed on NVIDIA GPUs with this PR while master passes them, on the Phoenix CI lanes and on an NVIDIA A100 80GB (NVHPC 25.5, OpenACC). The cause was one line in s_ib_shell_mass: if (ib_id > 0) g = ib_id left the shell mass at zero on NVHPC GPU builds, so ib_heat(5) was 0 and s_conserve_ib_mass never applied the correction (the relax was silently off; the failing candidates matched the relax-off results). The line is now g = max(ib_id, 0), which gives the same value wherever ib_id > 0, so it cannot change CPU or AMD results.

Tested on the NVIDIA A100 80GB, NVHPC 25.5, OpenACC, with the one-line fix on top of f3f4081: 09FDDC81, 04FB9E91, F1F38949, B7A370B8, 27CD0714, 5C04A681, 997E2B0E, 540C72F1, 30C84CA3, ED2D5C0A, FB3F47C0, 9C0CAF25, 50B98DBF and 5E746B78 all pass. The mechanism is not proven; my best reading is a compiler problem with a conditionally stored private scalar. I found no other conditionally assigned slot or index in the GPU loops of m_ibm.fpp, m_collisions.fpp or m_time_steppers.fpp: the other g = ib_id sites sit inside if (ib_id > 0) then blocks that also use g.

@sbryngelson
sbryngelson merged commit 1c831ba into MFlowCode:master Nov 21, 2026
56 of 59 checks passed
@sbryngelson

Copy link
Copy Markdown
Member Author

Independent NVIDIA verification of current master (aad80b3c3, which contains this PR).

  • Machine: NVIDIA A100 80GB (Georgia Tech wingtip)
  • Compiler: NVHPC 25.5, OpenACC, CI-style fat build (cc 70,75,80,89,90)
  • Flags: ./mfc.sh test -v --max-attempts 1 --no-build -o IBM --no-chemistry -j 2 -g <gpus> --gpu acc --mpi --no-debug --no-reldebug --no-single
  • Whole IBM label (chemistry excluded): 118 passed, 0 failed (one 1-GPU run, one 2-GPU run). 909 skipped by the filter, including the NVHPC<25.11 skip 124D7972 (Test: skip 124D7972 on NVHPC < 25.11 GPU builds (known difference, #2181) #2380).
  • Wall time of the full IBM label at -j 2 (machine shared, load varies): 829 s on one GPU, 661 s on two GPUs. Per-test times in the tables are single-test wall times including pre_process/simulation launch and golden compare, not per-step times.

Rows for the sparse ib_heat/relax path and the g = max(ib_id, 0) fix.

Test Result 1 GPU Result 2 GPUs (2-rank tests split across GPUs) Time 1 GPU / 2 GPUs (s)
09FDDC81 Adaptive dt PASS PASS 8.4 / 7.4
04FB9E91 Moving Circle ib_mass_relax PASS PASS 25.7 / 25.1
F1F38949 Moving Circle ib_mass_relax swept PASS PASS 25.0 / 20.4
B7A370B8 Moving Circle Two-way CFL restart PASS PASS 25.8 / 25.6
27CD0714 Moving Circle ib_uncover_fill PASS PASS 11.4 / 10.6
5C04A681 Rotating Circle PASS PASS 11.1 / 8.9
997E2B0E Translating Circle Body-Frame Force PASS PASS 11.8 / 9.0
540C72F1 Translating Spinning Circle PASS PASS 12.1 / 9.1
30C84CA3 Slip Circle Moving ib_slip_ccst PASS PASS 12.7 / 7.7
ED2D5C0A 1-fluid Periodic Bodies PASS PASS 13.9 / 11.1
FB3F47C0 2-fluid Periodic Bodies PASS PASS 13.4 / 12.3
9C0CAF25 Translating Circle 2 MPI Ranks PASS PASS 12.4 / 9.8
50B98DBF Centroid On Periodic Domain End 2 MPI Ranks PASS PASS 12.7 / 10.1
5E746B78 Fixed Circle Slip ib_mass_relax PASS PASS 9.6 / 8.2

Timing is reported only as measurements (above and the label wall times); no before/after comparison against a revert was made.

Not run: chemistry tests, OpenMP-offload build, CPU build, any NVHPC other than 25.5, before/after timing of the changes.

AI-assisted (Claude Code).

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

Labels

ci:phoenix Run the Phoenix (NVHPC GPU) jobs on this PR; push after adding

Development

Successfully merging this pull request may close these issues.

1 participant