A run-time order, fast
Problem. The order of the operators is an input of the solver (PRISM's fdv_order), not a compile-time constant, and the loops over the grid must not pay for that choice at every point.
What the dispatcher costs
Every 1-D operator has a run-time order dispatcher, gideon_<data>_<op>_<bias>(order, q, [ds,] r): one select case(order) that calls the kernel of that order, and returns GIDEON_NAN for an order without one. It is device code like the kernels (!$acc routine seq), uniform across the points when the order is, so it does not diverge on a GPU. It is not free. Measured against the kernel (gideon_fd_d1_c(6, ...) against gideon_fd_d1_c6, notes/bench.md):
| RTX 4070, nvfortran OpenACC | one core, gfortran | one core, nvfortran | |
|---|---|---|---|
| dispatcher / kernel | 1.093 (1.093–1.104) | 1.183 (1.134–1.220) | 1.115 (1.102–1.136) |
The select case is evaluated at every call: no compiler hoisted it out of the loop, although the order is loop-invariant, and on the CPU it also blocks the vectorisation of the loop. The dispatcher is still cheaper than ADAM's run-time-s path, about 0.45 to 0.65 of it (1.093 × 0.532 on the GPU, 1.183 × 0.425 with gfortran), but the 9 to 18 % are avoidable.
Recipe: validate once, choose once
- Validate the order before the loop. Call the dispatcher once on a probe window of that order; a NaN result means no kernel exists (
ieee_is_nanfromieee_arithmetic). Inside a device loop there is no way to stop, so a bad order caught there is a field of NaN. - Hoist the choice. One
select caseoutside, one loop per order inside, each calling its kernel: the loop body is the kernel's, with its compile-time row and its vectorisation. - Keep the dispatcher where the loop body is not the hot spot, or where one loop per order would multiply a long body: it is the same arithmetic, bitwise.
A procedure pointer chosen once is not a substitute: the kernels of different orders have different window shapes, so they share no interface, and a procedure pointer is not device code.
Code
program cookbook_run_time_order
!< Cookbook, a run-time order: the order read at run time, validated once, then either dispatched at every point
!< (gideon_fd_d1_c, one select case per call) or chosen once outside the loop (one loop per order, each calling its
!< kernel). Both give the same values, bitwise; the second pays no dispatch inside the loop.
use, intrinsic :: ieee_arithmetic, only : ieee_is_nan
use gideon
implicit none
integer(I4P), parameter :: N = 64, G = 5 ! nodes 0..N-1, periodic; ghosts: the widest window, order 10
integer(I4P), parameter :: ORDERS(6) = [2, 3, 4, 6, 8, 10]
real(R_P), parameter :: PI = acos(-1._R_P)
real(R_P) :: u(-G:N-1+G), dd(0:N-1), dk(0:N-1), probe(-G:G), t, h
integer(I4P) :: order, i, k
h = 2*PI / N
u(0:N-1) = [(exp(sin(i*h)), i=0, N-1)]
u(-G:-1) = u(N-G:N-1) ; u(N:N-1+G) = u(0:G-1)
probe = 0._R_P
do k=1, size(ORDERS)
order = ORDERS(k) ! in a solver: read from the input
! validate once: an order without a kernel returns GIDEON_NAN (a quiet NaN), never a number
call gideon_fd_d1_c(order, probe(-(order/2):order/2), 1._R_P, t)
if (ieee_is_nan(t)) then
print '(A,I0,A)', 'order ', order, ': no kernel (GIDEON_NAN), refused before the loop'
cycle
endif
! (a) the dispatcher at every node
do i=0, N-1
call gideon_fd_d1_c(order, u(i-order/2:i+order/2), h, dd(i))
enddo
! (b) the choice hoisted out of the loop: one loop per order, the kernel inside
select case(order)
case(2) ; do i=0, N-1 ; call gideon_fd_d1_c2(u(i-1:i+1), h, dk(i)) ; enddo
case(4) ; do i=0, N-1 ; call gideon_fd_d1_c4(u(i-2:i+2), h, dk(i)) ; enddo
case(6) ; do i=0, N-1 ; call gideon_fd_d1_c6(u(i-3:i+3), h, dk(i)) ; enddo
case(8) ; do i=0, N-1 ; call gideon_fd_d1_c8(u(i-4:i+4), h, dk(i)) ; enddo
case(10) ; do i=0, N-1 ; call gideon_fd_d1_c10(u(i-5:i+5), h, dk(i)) ; enddo
endselect
print '(A,I0,A,L1,A,ES10.3)', 'order ', order, ': dispatched = hoisted bitwise ', all(dd == dk), &
', max error ', maxval(abs(dk - [(cos(i*h)*exp(sin(i*h)), i=0, N-1)]))
enddo
endprogram cookbook_run_time_orderorder 2: dispatched = hoisted bitwise T, max error 6.512E-03
order 3: no kernel (GIDEON_NAN), refused before the loop
order 4: dispatched = hoisted bitwise T, max error 7.590E-05
order 6: dispatched = hoisted bitwise T, max error 1.577E-06
order 8: dispatched = hoisted bitwise T, max error 4.818E-08
order 10: dispatched = hoisted bitwise T, max error 2.115E-09Order 3 has no centred kernel and is refused before any loop runs; every other order gives the same values through the dispatcher and through the hoisted kernels, to the last bit (gideon_test_dispatch asserts the same for all 332 kernels and their 64 dispatchers). On a GPU the hoisted form puts the !$acc parallel loop on each loop inside the cases, so every case launches its own kernel with its own compile-time row (On the GPU).
Reference. The measurement: Performance; the dispatchers' NaN contract: Run-time dispatch; every dispatcher with its window: API.