Skip to content

9. On the GPU ​

SELAH runs on the GPU through FUNDAL:

  • every kernel is written once, with FUNDAL's portable directives;
  • the stepper allocates its registers with dev_alloc.

In an OpenACC build both live in device memory. The caller does the same with its own field, and nothing crosses the bus during a step.

fortran
allocate(q(1:1, 0:NI+1, 0:2, 0:2, 1:NB))
q = 0._R_P
do b=1, NB
   do i=1, NI
      q(1,i,1,1,b) = sin(2._R_P*PI*((b-1)*NI + i - 0.5_R_P)*dx)
   enddo
enddo
call dev_alloc(fptr_dev=q_d, ubounds=ubound(q), lbounds=lbound(q), ierr=ierr)   ! the field on the device
call dev_memcpy_to_device(dst=q_d, src=q)
bx = box_interior(LAYOUT_V_FIRST, 1_I4P, NI, 1_I4P, 1_I4P, 1_I4P, NB)
call st%initialize(SCHEME_SSP_54, FORM_BUTCHER, lb=lbound(q), ub=ubound(q))      ! registers through FUNDAL: device

The caller's ghost fill and residual become device loops, written with the same macros SELAH uses (fundal.H: DEVICEVAR for OpenACC, DEVICEPTR/OMPLOOP for OpenMP; on the host they become plain loops). The residual's stencil is GIDEON's gideon_fd_d1_c2, callable inside the device loop (!$acc routine seq) on a private three-cell window:

fortran
subroutine fill_ghosts(u)
!< Periodic ghost cells, one device thread per block.
real(R_P), intent(inout) :: u(1:,0:,0:,0:,1:)
integer(I4P)             :: b

!$acc parallel loop independent DEVICEVAR(u)
!$omp OMPLOOP DEVICEPTR(u)
do b=1, NB
   u(1,0,1,1,b)    = u(1,NI,1,1,1 + modulo(b-2, NB))
   u(1,NI+1,1,1,b) = u(1,1,1,1,1 + modulo(b, NB))
enddo
endsubroutine fill_ghosts

subroutine residual(u, r, dx)
!< The central residual, one device thread per cell.
real(R_P), intent(in)    :: u(1:,0:,0:,0:,1:)
real(R_P), intent(inout) :: r(1:,0:,0:,0:,1:)
real(R_P), intent(in)    :: dx
integer(I4P)             :: b, i
real(R_P)                :: w(-1:1), d

!$acc parallel loop independent collapse(2) DEVICEVAR(u, r) private(w, d)
!$omp OMPLOOP collapse(2) DEVICEPTR(u, r) private(w, d)
do b=1, NB
   do i=1, NI
      w = [u(1,i-1,1,1,b), u(1,i,1,1,b), u(1,i+1,1,1,b)] ; call gideon_fd_d1_c2(w, dx, d) ; r(1,i,1,1,b) = -d
   enddo
enddo
endsubroutine residual

The step itself is chapter 4's, unchanged, called with the device array q_d.

Building it for the GPU ​

bash
fobis build --mode static-nvf     # or your own nvfortran build of SELAH with -acc
nvfortran -cpp -acc -gpu=cc80 -DDEV_OAC -DCOMPILER_NVF -DFUNDAL_NO_MPI \
          -I <selah mods> -I src/third_party/FUNDAL/src/lib gpu.F90 libselah.a -o gpu

Running it ​

The documentation is built on the host, so this is the host build's output; the device build prints the same error. The device runs are verified separately on an NVIDIA A30 (C5).

text
$ gpu
error at t = 0.25:  5.581E-07

Device pointers stay on the device

On a GPU, q_d, stage_state() and rhs_register() are device addresses. Read and write them only inside device loops or through FUNDAL's copies (dev_memcpy_to_device, dev_memcpy_from_device), never element by element from the host. Coefficient arrays passed to kernels must be device arrays too, and contiguous.

What you learned

The same stepper on device memory; the caller's loops on the device; one copy back at the end. Reference: Installation: backends, Cookbook: performance.

Next: 10. Production.

GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT, at your choice