Skip to content

Commit c44457c

Browse files
committed
fix: use pointer components in grid_axis for correct OpenMP GPU device access
Change grid_axis cb/cc/spacing from allocatable to pointer components backed by the flat module arrays (x_cb/x_cc/dx etc.). GPU pointer attachment via GPU_ENTER_DATA(attach=) updates the device struct's pointer fields to point to the already-mapped device flat arrays, fixing CUDA_ERROR_ILLEGAL_ADDRESS in m_igr.fpp inline GPU_PARALLEL_LOOP bodies on NVHPC OpenMP target offload. Eliminates the early host sync, the duplicate GPU_UPDATE for struct members, and the OpenACC/OpenMP split in GPU_DECLARE for x/y/z.
1 parent 712657f commit c44457c

3 files changed

Lines changed: 24 additions & 47 deletions

File tree

src/common/m_derived_types.fpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ module m_derived_types
1515

1616
!> Derived type for a single spatial grid axis: cell-boundary, cell-center, per-cell spacing arrays, and minimum spacing scalar
1717
type grid_axis
18-
real(wp), allocatable, dimension(:) :: cb, cc, spacing
19-
real(wp) :: min_spacing = 0._wp
18+
real(wp), pointer, dimension(:) :: cb => null(), cc => null(), spacing => null()
19+
real(wp) :: min_spacing = 0._wp
2020
end type grid_axis
2121

2222
!> Derived type adding the field position (fp) as an attribute

src/simulation/m_global_parameters.fpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module m_global_parameters
5050

5151
!> @name Cell-boundary (cb), cell-center (cc), and spacing arrays per direction
5252
!> @{
53-
type(grid_axis), target :: x, y, z
53+
type(grid_axis) :: x, y, z
5454
!> @}
5555

5656
!> @name Flat GPU-accessible aliases for grid arrays (used in GPU kernels)
@@ -62,11 +62,7 @@ module m_global_parameters
6262
$:GPU_DECLARE(create='[dx, dy, dz, x_cc, y_cc, z_cc, x_cb, y_cb, z_cb]')
6363

6464
real(wp) :: dt !< Size of the time-step
65-
#if defined(MFC_OpenACC)
66-
$:GPU_DECLARE(create='[x%cb, y%cb, z%cb, x%cc, y%cc, z%cc, x%spacing, y%spacing, z%spacing, dt, m, n, p]')
67-
#elif defined(MFC_OpenMP)
6865
$:GPU_DECLARE(create='[x, y, z, dt, m, n, p]')
69-
#endif
7066

7167
!> @name Starting time-step iteration, stopping time-step iteration and the number of time-step iterations between successive
7268
!! solution backups, respectively
@@ -1224,37 +1220,34 @@ contains
12241220
$:GPU_UPDATE(device='[relax, relax_model, palpha_eps, ptgalpha_eps]')
12251221
12261222
! Allocating grid variables for the x-, y- and z-directions
1227-
@:ALLOCATE(x%cb(-1 - buff_size:m + buff_size))
1228-
@:ALLOCATE(x%cc(-buff_size:m + buff_size))
1229-
@:ALLOCATE(x%spacing(-buff_size:m + buff_size))
1230-
@:PREFER_GPU(x%cb)
1231-
@:PREFER_GPU(x%cc)
1232-
@:PREFER_GPU(x%spacing)
12331223
@:ALLOCATE(x_cb(-1 - buff_size:m + buff_size))
12341224
@:ALLOCATE(x_cc(-buff_size:m + buff_size))
12351225
@:ALLOCATE(dx(-buff_size:m + buff_size))
1226+
@:PREFER_GPU(x_cb)
1227+
@:PREFER_GPU(x_cc)
1228+
@:PREFER_GPU(dx)
1229+
x%cb => x_cb; x%cc => x_cc; x%spacing => dx
1230+
$:GPU_ENTER_DATA(attach='[x%cb, x%cc, x%spacing]')
12361231
12371232
if (n == 0) return
1238-
@:ALLOCATE(y%cb(-1 - buff_size:n + buff_size))
1239-
@:ALLOCATE(y%cc(-buff_size:n + buff_size))
1240-
@:ALLOCATE(y%spacing(-buff_size:n + buff_size))
1241-
@:PREFER_GPU(y%cb)
1242-
@:PREFER_GPU(y%cc)
1243-
@:PREFER_GPU(y%spacing)
12441233
@:ALLOCATE(y_cb(-1 - buff_size:n + buff_size))
12451234
@:ALLOCATE(y_cc(-buff_size:n + buff_size))
12461235
@:ALLOCATE(dy(-buff_size:n + buff_size))
1236+
@:PREFER_GPU(y_cb)
1237+
@:PREFER_GPU(y_cc)
1238+
@:PREFER_GPU(dy)
1239+
y%cb => y_cb; y%cc => y_cc; y%spacing => dy
1240+
$:GPU_ENTER_DATA(attach='[y%cb, y%cc, y%spacing]')
12471241
12481242
if (p == 0) return
1249-
@:ALLOCATE(z%cb(-1 - buff_size:p + buff_size))
1250-
@:ALLOCATE(z%cc(-buff_size:p + buff_size))
1251-
@:ALLOCATE(z%spacing(-buff_size:p + buff_size))
1252-
@:PREFER_GPU(z%cb)
1253-
@:PREFER_GPU(z%cc)
1254-
@:PREFER_GPU(z%spacing)
12551243
@:ALLOCATE(z_cb(-1 - buff_size:p + buff_size))
12561244
@:ALLOCATE(z_cc(-buff_size:p + buff_size))
12571245
@:ALLOCATE(dz(-buff_size:p + buff_size))
1246+
@:PREFER_GPU(z_cb)
1247+
@:PREFER_GPU(z_cc)
1248+
@:PREFER_GPU(dz)
1249+
z%cb => z_cb; z%cc => z_cc; z%spacing => dz
1250+
$:GPU_ENTER_DATA(attach='[z%cb, z%cc, z%spacing]')
12581251
12591252
end subroutine s_initialize_global_parameters_module
12601253
@@ -1335,15 +1328,18 @@ contains
13351328
if (ib) MPI_IO_IB_DATA%var%sf => null()
13361329
13371330
! Deallocating grid variables for the x-, y- and z-directions
1338-
@:DEALLOCATE(x%cb, x%cc, x%spacing)
1331+
$:GPU_EXIT_DATA(detach='[x%cb, x%cc, x%spacing]')
1332+
nullify (x%cb, x%cc, x%spacing)
13391333
@:DEALLOCATE(x_cb, x_cc, dx)
13401334
13411335
if (n == 0) return
1342-
@:DEALLOCATE(y%cb, y%cc, y%spacing)
1336+
$:GPU_EXIT_DATA(detach='[y%cb, y%cc, y%spacing]')
1337+
nullify (y%cb, y%cc, y%spacing)
13431338
@:DEALLOCATE(y_cb, y_cc, dy)
13441339
13451340
if (p == 0) return
1346-
@:DEALLOCATE(z%cb, z%cc, z%spacing)
1341+
$:GPU_EXIT_DATA(detach='[z%cb, z%cc, z%spacing]')
1342+
nullify (z%cb, z%cc, z%spacing)
13471343
@:DEALLOCATE(z_cb, z_cc, dz)
13481344
13491345
end subroutine s_finalize_global_parameters_module

src/simulation/m_start_up.fpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -924,15 +924,6 @@ contains
924924

925925
call s_populate_grid_variables_buffers()
926926

927-
! Sync flat grid aliases from struct members before module inits that use them (e.g. WENO coeff setup)
928-
x_cb = x%cb; x_cc = x%cc; dx = x%spacing
929-
if (n > 0) then
930-
y_cb = y%cb; y_cc = y%cc; dy = y%spacing
931-
end if
932-
if (p > 0) then
933-
z_cb = z%cb; z_cc = z%cc; dz = z%spacing
934-
end if
935-
936927
if (model_eqns == 3) call s_initialize_internal_energy_equations(q_cons_ts(1)%vf)
937928
if (ib) then
938929
if (cfl_dt .and. n_start > 0) then
@@ -1089,16 +1080,6 @@ contains
10891080
$:GPU_UPDATE(device='[acoustic_source, num_source]')
10901081
$:GPU_UPDATE(device='[sigma, surface_tension]')
10911082

1092-
$:GPU_UPDATE(device='[x%spacing, y%spacing, z%spacing, x%cb, x%cc, y%cb, y%cc, z%cb, z%cc]')
1093-
1094-
! Sync flat GPU aliases from struct members
1095-
dx = x%spacing; x_cc = x%cc; x_cb = x%cb
1096-
if (n > 0) then
1097-
dy = y%spacing; y_cc = y%cc; y_cb = y%cb
1098-
end if
1099-
if (p > 0) then
1100-
dz = z%spacing; z_cc = z%cc; z_cb = z%cb
1101-
end if
11021083
$:GPU_UPDATE(device='[dx, x_cc, x_cb]')
11031084
if (n > 0) then
11041085
$:GPU_UPDATE(device='[dy, y_cc, y_cb]')

0 commit comments

Comments
 (0)