Skip to content

GPU: update num_procs and proc_rank on the default device too (fixes NVHPC OpenMP multi-GPU after #2371) - #2384

Merged
sbryngelson merged 1 commit into
MFlowCode:masterfrom
sbryngelson:gpu-bind-omp-context
Nov 22, 2026
Merged

sbryngelson merged 1 commit into
MFlowCode:masterfrom
sbryngelson:gpu-bind-omp-context

Conversation

@sbryngelson

Copy link
Copy Markdown
Member

AI-assisted (Claude Code). Follow-up to #2371.

What

Restores the GPU_UPDATE(device='[num_procs, proc_rank]') at the end of s_mpi_initialize that #2371 removed, and keeps #2371's update after each rank is bound to its GPU in s_initialize_mpi_domain. Each rank now updates the default device first and its bound device second.

Why

Since #2371, NVHPC OpenMP offload runs with more than one GPU fail at the first step:

Accelerator Fatal Error: call to cuMemAlloc returned error 201 (CUDA_ERROR_INVALID_CONTEXT): Invalid context

This is what turns the Phoenix gpu-omp benchmark red on every PR since #2371 merged: the benchmark builds current master for its baseline, and viscous_weno5_sgb_acoustic (2 ranks on 2 GPUs) fails there, while the PR side, built against an older master, passes.

My reading, not confirmed against the NVHPC runtime: with #2371 a rank's first device operation comes after omp_set_default_device moved it to another GPU, and under NVHPC OpenMP that leaves the later allocations without a valid context. Before #2371 the first operation went to the default device.

How it was checked

wingtip, NVIDIA A100, NVHPC 25.5, 2 ranks with -g 2 3 (one GPU per rank). The benchmark case is benchmarks/viscous_weno5_sgb_acoustic/case.py -- --gbpp 1 --steps 5. The collision tests are the five 2-rank IBM tests from #2371 (09FDDC81, 04FB9E91, 9C0CAF25, 50B98DBF, 5E746B78).

Build OpenMP benchmark, 2 GPUs OpenMP benchmark, 1 GPU OpenACC benchmark, 2 GPUs Collision tests, 2 GPUs
master (with #2371) CUDA_ERROR_INVALID_CONTEXT passes
master with #2371 reverted passes passes
this PR passes passes 5 of 5 pass, under both OpenMP and OpenACC

The collision tests are the ones #2371 fixed (contact forces applied twice when the second rank's device copy of num_procs was uninitialised), so that fix still holds.

./mfc.sh precheck passes.

Not checked

  • AMD (Cray or amdflang) OpenMP and other NVHPC versions. On Frontier the binding is per-rank visible-device, so omp_set_default_device moves nothing there.
  • The root cause inside the NVHPC OpenMP runtime.

… binding

MFlowCode#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.
@sbryngelson
sbryngelson requested a review from a team as a code owner November 21, 2026 22:07
@sbryngelson
sbryngelson requested review from danieljvickers and removed request for a team November 21, 2026 22:07
@sbryngelson
sbryngelson marked this pull request as draft November 21, 2026 22:07
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Claude Code Review

Head SHA: 2e9caa6

Files changed:

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

Findings:

  • medium src/common/m_mpi_common.fpp: The new $:GPU_UPDATE(device='[num_procs, proc_rank]') is in s_mpi_initialize, which is in src/common. pre_process and post_process also call it (src/pre_process/m_start_up.fpp:643, src/post_process/m_start_up.fpp:974). num_procs and proc_rank have GPU_DECLARE(create=...) only in src/simulation/m_global_parameters.fpp:29. They are plain host integers in the pre_process and post_process m_global_parameters.fpp. CMake passes the OpenACC/OpenMP flags and defines to those targets too (CMakeLists.txt lines 197, 209, 242). In a GPU build, an OpenACC update device on a variable with no device copy is a runtime "variable not present" error, so pre_process and post_process could abort at startup. OpenMP target update on an unmapped variable is probably a no-op. Suggested fix: guard the update with #ifdef MFC_SIMULATION, or move it into simulation's s_initialize_mpi_domain/start-up before omp_set_default_device. Confidence: medium. I could not build with a GPU compiler to confirm.
  • The m_start_up.fpp hunk only changes a comment. Its new wording is consistent with the added update.

@github-actions

Copy link
Copy Markdown

Lines of Code

File Lines Diff
src/common/m_mpi_common.fpp 1579 +1
Directory Lines Diff
common 12519 +1
total 61231 +1

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.80%. Comparing base (4264341) to head (2e9caa6).
⚠️ Report is 30 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2384      +/-   ##
==========================================
+ Coverage   65.72%   65.80%   +0.07%     
==========================================
  Files         111      111              
  Lines       30549    30681     +132     
  Branches     4673     4692      +19     
==========================================
+ Hits        20079    20190     +111     
+ Misses       6699     6692       -7     
- Partials     3771     3799      +28     

☔ 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
sbryngelson marked this pull request as ready for review November 22, 2026 04:43
@sbryngelson
sbryngelson merged commit 398733e into MFlowCode:master Nov 22, 2026
41 of 59 checks passed
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