From 2e9caa6431782fd55157c9dcbd17e4df892aa3d6 Mon Sep 17 00:00:00 2001 From: Spencer Bryngelson Date: Sat, 21 Nov 2026 16:27:19 -0500 Subject: [PATCH] GPU: also update num_procs and proc_rank on the default device before binding #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. --- src/common/m_mpi_common.fpp | 5 +++++ src/simulation/m_start_up.fpp | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/common/m_mpi_common.fpp b/src/common/m_mpi_common.fpp index 7a549f6bb6..386333d43c 100644 --- a/src/common/m_mpi_common.fpp +++ b/src/common/m_mpi_common.fpp @@ -138,6 +138,11 @@ contains proc_rank = 0 #endif + ! First device operation, on the default device: with NVHPC OpenMP offload a rank's first one must come before + ! omp_set_default_device moves it to another GPU, or its later allocations fail with CUDA_ERROR_INVALID_CONTEXT. The + ! bound device gets its own copy in s_initialize_mpi_domain. + $:GPU_UPDATE(device='[num_procs, proc_rank]') + end subroutine s_mpi_initialize !> Set up MPI I/O data views and variable pointers for parallel file output. diff --git a/src/simulation/m_start_up.fpp b/src/simulation/m_start_up.fpp index 6e21a22684..40e9799992 100644 --- a/src/simulation/m_start_up.fpp +++ b/src/simulation/m_start_up.fpp @@ -1104,7 +1104,7 @@ contains #endif #endif - ! After the device is bound: s_mpi_initialize ran earlier, so its update went to the default device + ! After the device is bound: the update in s_mpi_initialize went to the default device $:GPU_UPDATE(device='[num_procs, proc_rank]') if (proc_rank == 0) then