Skip to content

GPU: send num_procs and proc_rank to the device after the rank is bound to its GPU - #2371

Merged
sbryngelson merged 1 commit into
MFlowCode:masterfrom
sbryngelson:fix-num-procs-device-binding
Nov 21, 2026
Merged

sbryngelson merged 1 commit into
MFlowCode:masterfrom
sbryngelson:fix-num-procs-device-binding

Conversation

@sbryngelson

@sbryngelson sbryngelson commented Nov 21, 2026 •

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

In simulation, s_mpi_initialize ends with $:GPU_UPDATE(device='[num_procs, proc_rank]'), but it is called from s_initialize_mpi_domain before each rank is bound to its GPU (acc_set_device_num / omp_set_default_device). A rank bound to a device other than the default one never receives the values: its device copy of num_procs stays uninitialised. The only device reader, f_local_rank_owns_location (if (num_procs > 1)), then returns .true. on both ranks in the IBM collision kernels, so the contact force is applied by both ranks and summed.

Fix: do the update right after the device binding in s_initialize_mpi_domain and drop it from s_mpi_initialize. pre_process and post_process also call s_mpi_initialize, but GPU_UPDATE is a no-op there and nothing else in them reads the values on the device.

Evidence (hand test, NVIDIA A100 80GB (Georgia Tech wingtip), NVHPC 25.5, OpenACC, 2 ranks)

-g 0 2 maps rank 0 to GPU 0 and rank 1 to GPU 2 (-g 0 puts both ranks on GPU 0). Quantities are density (var 1656, D/cons.1.00.000100.dat) and energy (var 220, D/cons.4.01.000100.dat) of a 2-rank IBM collision case (a friction test from #2354). Golden values (Cray CPU build): 0.973596 / 2.50569.

Build -g density energy result
unpatched 0 0.973596 2.50569 pass
unpatched 0 2 0.973094 2.97088 FAIL
patched 0 0.973596 2.50569 pass
patched 0 2 0.973596 2.50569 pass
patched 2 0 0.973596 2.50569 pass
patched 0 3 0.973596 2.50569 pass
master (with #2353) + patch, 2-rank IBM tests 09FDDC81 04FB9E91 9C0CAF25 50B98DBF 5E746B78 0 1 (two GPUs) 5 passed, 0 failed
master (with #2353) + patch, same 5 tests 0 (one GPU) 5 passed, 0 failed
Direct measurement, NVHPC 25.11, examples/1D_sodshocktube, 2 ranks, throwaway kernel printing device vs host at the first step (unpatched) two GPUs rank 1 device num_procs/proc_rank = 0 / 0, host = 2 / 1; rank 0 fine confirms mechanism
Same measurement, both ranks on one GPU one GPU device = host as expected
Tuolumne (LLNL) MI300A, Cray CCE, --gpu mp, tests 09FDDC81 04FB9E91 9C0CAF25 50B98DBF (one GPU per rank, binding is a no-op) one GPU per rank 4 passed, 0 failed (unchanged)

The patched mappings agree to about 1e-12. The unpatched -g 0 2 result is reproducible to 1e-12 across repeats; --rdma-mpi did not change it and it persists with friction off, so it is not a halo or friction issue. The unpatched build only passes when both ranks share a GPU.

Not verified

  • No CI test covers this: CI GPU runners have few GPUs, so 2-rank tests run both ranks on the same GPU (or one per GPU on the default device), which hides the bug.
  • No H100/H200 two-GPU runs.
  • In the direct measurement, gq3_pts/gq3_wts and nvhpc_omp_state_dependent_eos matched the host everywhere, so num_procs and proc_rank are the only stale device values found.
  • The OpenMP-offload path (omp_set_default_device): checked by reading the code only, not run.
  • AMD GPUs and --rdma-mpi (the latter made no difference to the unpatched failure on the A100).
  • A regression test: it would need 2 ranks on 2 different GPUs, which CI cannot provide.

This PR was made with Claude Code (AI).

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Claude Code Review

Head SHA: 63649fa

Files changed:

  • 2
  • src/common/m_mpi_common.fpp
  • src/simulation/m_start_up.fpp

Findings:
No issues found. Checked in step 4: src/common/m_mpi_common.fpp and src/simulation/m_start_up.fpp. The GPU_UPDATE of num_procs and proc_rank now runs after device binding (acc_set_device_num / omp_set_default_device). Only simulation declares them with GPU_DECLARE (src/simulation/m_global_parameters.fpp:29), so removing the update from the shared routine does not affect pre_process or post_process.

@github-actions

Copy link
Copy Markdown

Lines of Code

File Lines Diff
src/common/m_mpi_common.fpp 1578 -1
src/simulation/m_start_up.fpp 1535 +1
Directory Lines Diff
common 12518 -1
simulation 41026 +1
total 61096 +0

@sbryngelson
sbryngelson marked this pull request as ready for review November 21, 2026 18:27
@sbryngelson
sbryngelson requested a review from a team as a code owner November 21, 2026 18:27
@sbryngelson
sbryngelson requested review from danieljvickers and removed request for a team November 21, 2026 18:27
@sbryngelson
sbryngelson merged commit 3d5509c into MFlowCode:master Nov 21, 2026
28 of 41 checks passed
@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.77%. Comparing base (4264341) to head (63649fa).
⚠️ Report is 22 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2371      +/-   ##
==========================================
+ Coverage   65.72%   65.77%   +0.04%     
==========================================
  Files         111      111              
  Lines       30549    30582      +33     
  Branches     4673     4680       +7     
==========================================
+ Hits        20079    20115      +36     
+ Misses       6699     6680      -19     
- Partials     3771     3787      +16     

☔ 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

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.

Check for #2371 (num_procs/proc_rank device update after the GPU binding): every 2-rank IBM test run with the ranks on two different GPUs.

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
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

Totals: 118/118 IBM-label tests pass on one GPU and on two GPUs; no failures, no skips beyond the filter. This covers OpenACC only; the OpenMP-offload path was not run in this table.

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).

sbryngelson added a commit that referenced this pull request Nov 22, 2026
… binding (#2384)

#2371 moved this update after each rank is bound to its GPU, which fixed the
bound device's copy under OpenACC. Under NVHPC OpenMP offload, a rank whose
first device operation comes after omp_set_default_device moves it to another
GPU then fails its first allocation with CUDA_ERROR_INVALID_CONTEXT (two ranks
on two GPUs). Keep the update after binding and restore the earlier one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant