T6 On the GPU
Goal. The order-6 Laplacian and gradient of T5 at every node, inside one !$acc parallel loop, built with nvfortran -acc for an NVIDIA GPU; the same program built without OpenACC on the host, printing the same lines.
Concepts. Every GIDEON operator is pure, !$acc routine seq and !$omp declare target, with its coefficients compile-time constants (Conventions): a kernel is called from inside a compute region like any device routine, and nothing of GIDEON's is copied to the device, only your data. T5 for the gather; C6 Host and device for the verification of every kernel on the device.
The program
program gpu
!< Tutorial T6: the 3-D Laplacian and gradient of order 6 inside an OpenACC parallel loop. Built with nvfortran -acc the
!< loop runs on the GPU; built without OpenACC the directives are comments and it runs on the host. The output is the
!< same: the errors against the exact values, and the device result against a host loop.
use gideon
implicit none
integer(I4P), parameter :: S = 3 ! half-width of the order-6 stencils, the ghost nodes
integer(I4P), parameter :: NS(2) = [32, 64] ! nodes per direction
real(R_P), parameter :: PI = acos(-1._R_P)
real(R_P), allocatable :: x(:), phi(:,:,:) ! node coordinates; sin x sin y sin z, with ghosts
real(R_P), allocatable :: lap(:,:,:), grad(:,:,:,:) ! computed in the parallel loop
real(R_P), allocatable :: lap_h(:,:,:), grad_h(:,:,:,:) ! the same, by a host loop
real(R_P) :: qx(-S:S), qy(-S:S), qz(-S:S) ! the gathered stencils, private to each iteration
real(R_P) :: dxyz(3), h, elap(2), egrad(2), tol
logical :: agree(2)
integer(I4P) :: l, n, i, j, k
do l=1, 2
n = NS(l)
h = 2*PI / n
dxyz = h
allocate(x(-S:n-1+S), phi(-S:n-1+S,-S:n-1+S,-S:n-1+S))
allocate(lap(0:n-1,0:n-1,0:n-1), lap_h(0:n-1,0:n-1,0:n-1), grad(3,0:n-1,0:n-1,0:n-1), grad_h(3,0:n-1,0:n-1,0:n-1))
x = [(i*h, i=-S, n-1+S)]
do k=-S, n-1+S ; do j=-S, n-1+S ; do i=-S, n-1+S
phi(i,j,k) = sin(x(i)) * sin(x(j)) * sin(x(k))
enddo ; enddo ; enddo
!$acc data copyin(phi, dxyz) copyout(lap, grad)
!$acc parallel loop collapse(3) private(qx, qy, qz)
do k=0, n-1 ; do j=0, n-1 ; do i=0, n-1
qx = phi(i-S:i+S,j,k) ; qy = phi(i,j-S:j+S,k) ; qz = phi(i,j,k-S:k+S)
call gideon_fd_laplacian_c6(qx, qy, qz, dxyz, lap(i,j,k))
call gideon_fd_gradient_c6(qx, qy, qz, dxyz, grad(:,i,j,k))
enddo ; enddo ; enddo
!$acc end data
do k=0, n-1 ; do j=0, n-1 ; do i=0, n-1 ! the host's values, the reference of the device's
qx = phi(i-S:i+S,j,k) ; qy = phi(i,j-S:j+S,k) ; qz = phi(i,j,k-S:k+S)
call gideon_fd_laplacian_c6(qx, qy, qz, dxyz, lap_h(i,j,k))
call gideon_fd_gradient_c6(qx, qy, qz, dxyz, grad_h(:,i,j,k))
enddo ; enddo ; enddo
elap(l) = 0._R_P ; egrad(l) = 0._R_P
do k=0, n-1 ; do j=0, n-1 ; do i=0, n-1
elap(l) = max(elap(l), abs(lap(i,j,k) + 3*phi(i,j,k)))
egrad(l) = max(egrad(l), maxval(abs(grad(:,i,j,k) - [cos(x(i))*sin(x(j))*sin(x(k)), &
sin(x(i))*cos(x(j))*sin(x(k)), &
sin(x(i))*sin(x(j))*cos(x(k))])))
enddo ; enddo ; enddo
! device and host code are compiled apart, and a compiler may contract their multiply-adds differently: compare
! within a few ulps of the operator's scale, not bitwise
tol = 64 * epsilon(1._R_P)
agree(l) = maxval(abs(lap - lap_h)) <= tol * maxval(abs(lap_h)) / h**2 .and. &
maxval(abs(grad - grad_h)) <= tol * maxval(abs(grad_h)) / h
deallocate(x, phi, lap, lap_h, grad, grad_h)
enddo
print '(A)', 'order 6, sin x sin y sin z on a periodic grid of n^3 nodes, max error'
print '(A)', ' n Laplacian gradient parallel loop == host loop (64 eps)'
do l=1, 2
print '(I6,2ES12.3,L12)', NS(l), elap(l), egrad(l), agree(l)
enddo
print '(A,2F6.2)', 'observed orders, log2(e(32) / e(64)):', log(elap(1) / elap(2)) / log(2._R_P), &
log(egrad(1) / egrad(2)) / log(2._R_P)
endprogram gpu- The data region.
copyin(phi, dxyz)andcopyout(lap, grad): the field and the steps go in, the results come out. The rows are parameters folded into the device code. - The loop.
!$acc parallel loop collapse(3) private(qx, qy, qz): each iteration gathers its three stencils into its own private arrays and calls two kernels,routine seqcalls inside the parallel loop. The private arrays are the gather of T5 moved onto the device: no strided section ofphireaches a kernel. - The results.
lap(i,j,k)and the contiguous columngrad(:,i,j,k)are passed to the kernels' scalarlapand explicit-shapeg(3). - The host reference. The same loop without directives fills
lap_handgrad_h. Device and host code are compiled apart, and a compiler may contract their multiply-adds differently, so the program compares them within 64 ulps of the operator's scale, not bitwise. The output does not say where the loop ran: built either way, the program prints the same lines.
Build and run
On the host (gfortran ignores the !$acc directives without -fopenacc):
fobis build --mode examples-gnu && ./exe-examples/gpuOn the GPU the library must be compiled with -acc too: !$acc routine seq gives each kernel a device version only in an OpenACC compile. Against lib/nvf/static (built without -acc) nvfortran stops with NVFORTRAN-S-1061-Procedures called in a compute region must have acc routine information - gideon_fd_laplacian_c6. FoBiS builds the library and the program in one go; -f /dev/null sets the project's fobos aside, whose modes fix their compiler and flags:
fobis build -f /dev/null --compiler nvfortran \
--cflags "-c -acc -gpu=cc89 -Minfo=accel" --lflags "-acc -gpu=cc89" \
--src src/lib/ --src src/third_party/PENF/src/lib/ --src docs/examples/src/ \
--target docs/examples/src/gpu.F90 --build-dir exe-examples/acc/
CUDA_VISIBLE_DEVICES=1 NVCOMPILER_ACC_NOTIFY=1 ./exe-examples/acc/gpucc89 is the compute capability of an RTX 40 series (Ada); cc80 for an A100, cc90 for an H100. CUDA_VISIBLE_DEVICES picks the GPU, NVCOMPILER_ACC_NOTIFY=1 reports each kernel launch on the standard error.
Output
order 6, sin x sin y sin z on a periodic grid of n^3 nodes, max error
n Laplacian gradient parallel loop == host loop (64 eps)
32 3.051E-07 4.063E-07 T
64 4.789E-09 6.383E-09 T
observed orders, log2(e(32) / e(64)): 5.99 5.99This is the host build's output, the one scripts/run_examples.sh checks. Built as above with nvfortran 26.9 and run on an RTX 4070, the program printed the same lines, compared with diff, and the launch report on the standard error was (paths shortened):
launch CUDA kernel file=.../docs/examples/src/gpu.F90 function=gpu line=30 device=0 threadid=1 num_gangs=256 num_workers=1 vector_length=128 grid=256 block=128
launch CUDA kernel file=.../docs/examples/src/gpu.F90 function=gpu line=30 device=0 threadid=1 num_gangs=2048 num_workers=1 vector_length=128 grid=2048 block=128One launch per grid, -Minfo=accel reports the loop as !$acc loop gang, vector(128) collapse(3) and Local memory used for qx,qz,qy: the private stencils.
What to notice
- The kernels need nothing on the device but your data: no table to copy, no setup call. The compiler calls the
routine seqversions, whose coefficients are constants folded into the device code. - Device and host agree. In this build they agree bit for bit, every value of both grids (measured with nvfortran 26.9, default flags and
-fast). The program's tolerance is of the kind C6 verifies for every kernel, a few ulps of the operator's scale; the bitwise equality here is an observation of one compiler on one program, not a property to rely on. - The accuracy is T5's: order 5.99, the same errors to the digits printed.
Exercises
- The order at run time on the device:
gideon_fd_laplacian_c(order, qx(-s:s), qy(-s:s), qz(-s:s), dxyz, lap(i,j,k))withordera variable. Theselect casetakes the same branch in every iteration: no divergence, and 1.093 times the kernel's time on the RTX 4070 (Performance). - Time the parallel loop with
system_clock(notcpu_time, which counts the host's processor time). Addasyncto the loop and the timing measures the launch only, unless!$acc waitprecedes the second reading. - Count the values where
lap == lap_hwith-fastand with-gpu=cc89,nofma -Mnofma. - OpenMP offload: the kernels also carry
!$omp declare target. Write the loop as!$omp target teams distribute parallel do collapse(3) private(qx, qy, qz)with amapfor the arrays, and compile the library and the program with-mp=gpu. GIDEON's CI does not build this path.