Skip to content

T4 The order at run time ​

Goal. Choose the order when the program runs: what a dispatcher does, where its window bounds come from, what it returns for an order without a kernel, what it costs against the kernel, and how to avoid that cost.

Concepts. Run-time order and the API; the dispatcher of every kernel is in the last column of the table in Operators. The measured cost: Performance. T1 for the kernels.

gideon_fd_d1_c(order, q, ds, d) is one select case on order that calls gideon_fd_d1_c<order>. Its dummy is declared with bounds that are expressions of order, so the caller passes the whole window of that order and the kernel receives it with the offsets it expects:

DispatcherWindowOrders with a kernel
gideon_fd_d1_cq(-(order/2):order/2)2, 4, 6, 8, 10
gideon_fd_d3_cq(-(order/2+1):order/2+1)2, 4, 6, 8
gideon_fd_face_cq(1-(order/2):order/2)2, 4, 6, 8, 10
gideon_fv_face_urq(1:order)1 to 9
gideon_fv_face_ulq(-(order-1):0)1 to 9
gideon_fv_d1_urq(0:order)1 to 9
gideon_fv_d1_ulq(-(order):0)1 to 9

The program ​

fortran
program run_time_order
!< Tutorial T4: the order chosen when the program runs. The dispatcher gideon_fd_d1_c over orders 1 to 12, its window
!< bounds as expressions of the order, the NaN of an order without a kernel, and the same values from a choice made
!< once, outside the loop.
use, intrinsic :: ieee_arithmetic, only : ieee_is_nan
use gideon
implicit none
integer(I4P), parameter :: N = 64                ! nodes
integer(I4P), parameter :: G = 6                 ! ghost nodes: half of the largest order tried
real(R_P),    parameter :: PI = acos(-1._R_P)
real(R_P)               :: x(-G:N-1+G), u(-G:N-1+G), du(0:N-1), dk(0:N-1), h
integer(I4P)            :: order, s, i

h = 2*PI / N
x = [(i*h, i=-G, N-1+G)]
u(0:N-1) = exp(sin(x(0:N-1)))
u(-G:-1) = u(N-G:N-1)
u(N:N-1+G) = u(0:G-1)

print '(A)', 'gideon_fd_d1_c(order, u(i-s:i+s), h, d), s = order/2, d/dx exp(sin x), 64 nodes'
print '(A)', 'order  window        max error'
do order=1, 12
   s = order / 2
   do i=0, N-1
      call gideon_fd_d1_c(order, u(i-s:i+s), h, du(i)) ! the window's bounds follow the order
   enddo
   if (any(ieee_is_nan(du))) then
      print '(I5,A,I0,A,I0,A,T22,A)', order, '  q(', -s, ':', s, ')', 'no kernel: GIDEON_NAN'
   else
      print '(I5,A,I0,A,I0,A,T21,ES10.3)', order, '  q(', -s, ':', s, ')', maxval(abs(du - cos(x(0:N-1))*u(0:N-1)))
   endif
enddo

! the dispatcher calls the kernel: the same values, bitwise
order = 6
do i=0, N-1
   call gideon_fd_d1_c(order, u(i-3:i+3), h, du(i))
   call gideon_fd_d1_c6(u(i-3:i+3), h, dk(i))
enddo
print '(A,I0,A,I0)', 'gideon_fd_d1_c(6, ...) == gideon_fd_d1_c6: ', count(du == dk), ' of ', N

! the choice made once: one loop per order, the kernel inside (no select case per point)
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 default
   error stop 'run_time_order: order not supported by this program'
endselect
print '(A,I0,A,I0)', 'select case outside the loop == dispatcher:  ', count(du == dk), ' of ', N
endprogram run_time_order
  • Orders 1 to 12. The same loop for every order: s = order/2, the section u(i-s:i+s). Six ghost nodes cover the widest window tried, order 12.
  • An order without a kernel. The dispatcher writes GIDEON_NAN, a quiet NaN, into the result. The program tests it with ieee_is_nan; d /= d is the same test without the module, but compilers may fold it away under value-unsafe optimisations (-ffast-math).
  • The dispatcher is the kernel. Order 6 through gideon_fd_d1_c and through gideon_fd_d1_c6: compared bit for bit.
  • The choice made once. A select case outside the loop, one loop per order with the kernel inside: the same values, and no branch per point.

Build and run ​

bash
fobis build --mode examples-gnu && ./exe-examples/run_time_order

or against the library:

bash
fobis build --mode static-gnu
gfortran -I lib/gnu/static/mod docs/examples/src/run_time_order.F90 lib/gnu/static/libgideon.a -o run_time_order
./run_time_order

Output ​

text
gideon_fd_d1_c(order, u(i-s:i+s), h, d), s = order/2, d/dx exp(sin x), 64 nodes
order  window        max error
    1  q(0:0)        no kernel: GIDEON_NAN
    2  q(-1:1)       6.512E-03
    3  q(-1:1)       no kernel: GIDEON_NAN
    4  q(-2:2)       7.590E-05
    5  q(-2:2)       no kernel: GIDEON_NAN
    6  q(-3:3)       1.577E-06
    7  q(-3:3)       no kernel: GIDEON_NAN
    8  q(-4:4)       4.818E-08
    9  q(-4:4)       no kernel: GIDEON_NAN
   10  q(-5:5)       2.115E-09
   11  q(-5:5)       no kernel: GIDEON_NAN
   12  q(-6:6)       no kernel: GIDEON_NAN
gideon_fd_d1_c(6, ...) == gideon_fd_d1_c6: 64 of 64
select case outside the loop == dispatcher:  64 of 64

What to notice ​

  • The window follows the order, the result follows the kernel. Orders 2 to 10 give the errors of T1 at 64 nodes (6.512E-03 for order 2, 1.577E-06 for order 6), the kernels' own numbers.
  • An odd order has a window but no kernel. Order 3 passes q(-1:1), a legal window; the select case has no branch for it and returns GIDEON_NAN, as for order 12. No error stop: the dispatchers are device code, where it is not allowed, and ADAM's silent 0 (audit item C) looked like a valid derivative. A NaN propagates through every value computed from it, so one check on the result of a step finds it.
  • The dispatcher costs a branch per call. Measured against gideon_fd_d1_c6 on 224 points: 1.093 times the kernel's time on an RTX 4070, 1.183 with gfortran on one core, 1.115 with nvfortran (Performance). The compilers do not hoist the loop-invariant select case out of the loop, and on the CPU it blocks the loop's vectorisation. The last block of the program is the remedy: the same numbers, the branch taken once. The timings are not printed: they depend on the machine (exercise 1).

Exercises ​

  1. Time the dispatcher loop and the hoisted one on 222 nodes with system_clock (not cpu_time), five repetitions each, and compare the medians.
  2. Upwind faces at run time: gideon_fv_face_ul(order, u(i-order+1:i), f) for orders 1 to 10, on cell averages (T3). Which order returns GIDEON_NAN?
  3. Read the order from the command line (get_command_argument), the reason a run-time order exists.
  4. gideon_fd_d3_c reads a wider window than gideon_fd_d1_c of the same order. Write its section for node i from the table above and check it against gideon_fd_d3_c6.

Released under your choice of GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT.