Replace ADAM's FDV calls
Problem. Move a caller of ADAM's adam_fdv_operators_library (and of its FNL backend, adam_fnl_fdv_operators_library) to GIDEON: the call that replaces each routine, the window to pass, and what changes in the results.
The reference is ADAM develop at ff879596, copied verbatim in notes/adam-reference/ and compiled into the test gideon_test_adam, which calls every ADAM routine at every half-stencil notes/adam-fdv-audit.md, §5; Equivalence with ADAM).
The index map
ADAM's kernels declare an assumed-shape window with the node or cell at index 1, q(1-s:); GIDEON's an explicit-shape window with it at offset 0, q(-S:S). The caller's section does not change for the centred operators: both are called with u(i-s:i+s) for node or cell i, and the face kernels of both with u(i+1-s:i+s) for the face i+1/2. What changes:
The half-stencil becomes an order. ADAM's
sis a run-time argument; GIDEON's order is in the kernel's name, or the first argument of the dispatcher. The order of a window of half-widthdepends on the operator: ADAM routine GIDEON order compute_derivativeN_fd_centered,compute_derivativeN_fv_centeredfor N = 1, 2; for 3, 4; for 5, 6 compute_reconstruction_r_fd_centered,compute_reconstruction_r_fv_centeredcompute_reconstruction_r_fv_centered_dNfor N = 1; for 2, 3; for 4, 5 compute_avg_to_point,compute_point_to_avgcompute_reconstruction_r_fv_{r,l}upwind,compute_derivative1_fv_{r,l}upwindcompute_derivativeN_fv_{r,l}upwind, N = 2…5compute_reconstruction_r_fv_{r,l}upwind_dNthe 3-D operators The window is exact. An explicit-shape dummy reads exactly
HI − LO + 1elements: pass the window of the kernel, no more, no less. A section of the wrong length is not detected at run time, not even by-fcheck=all: the kernel reads the elements in order and returns the result of a shifted point. The window of each kernel is the caller's responsibility (Conventions); a checked build catches only a section that leaves the caller's array. ADAM's audit item B was caught the other way:-fcheck=boundsstopped inside the kernel, because an assumed-shape dummy carries the section's length, which an explicit-shape one does not.Fixed-size buffers. An ADAM caller that passes a whole buffer declared with
S_MAXbounds (ascompute_reconstruction_r_fd_centered_devexpects,q(1-FDV_S_MAX:FDV_S_MAX)) passes the section of the order instead:buf(1-s:s).The upwind sides are named by the stencil. ADAM's
rupwindd1 and face value read the cells on the right; itsrupwindd2–d5 and face derivatives those on the left;lupwindthe reverse (audit item F). GIDEON's_uris always the right side,_ulthe left.
Before and after
ADAM's calls (not compiled here; their equivalence is gideon_test_adam's) and their GIDEON replacements, for node or cell i, face i+1/2, half-stencil s:
| ADAM | GIDEON |
|---|---|
compute_derivativeN_fd_centered(s, ds, u(i-s:i+s), d) | gideon_fd_dN_c(p, u(i-s:i+s), ds, d), p from the table |
compute_reconstruction_r_fd_centered(s, u(i+1-s:i+s), f) and its _dev | gideon_fd_face_c(2*s, u(i+1-s:i+s), f) |
compute_derivativeN_fv_centered(s, ds, a(i-s:i+s), d) | gideon_fv_dN_c(p, a(i-s:i+s), ds, d) |
compute_reconstruction_r_fv_centered(s, a(i+1-s:i+s), f) | gideon_fv_face_c(2*s, a(i+1-s:i+s), f) |
compute_reconstruction_r_fv_centered_dN(s, a(i+1-s:i+s), f) | gideon_fv_face_dN_c(p, a(i+1-s:i+s), 1._R_P, f): ADAM's is unscaled (ds = 1 keeps it, the step gives the derivative |
compute_avg_to_point(s, a(i-s:i+s), v) | gideon_fv_point_c(2*s+2, a(i-s:i+s), v) |
compute_point_to_avg(s, u(i-s:i+s), v) | gideon_fd_average_c(2*s+2, u(i-s:i+s), v) |
compute_reconstruction_r_fv_rupwind(s, a(i:i+s), f) | gideon_fv_face_ur(s, a(i+1:i+s), f): ADAM's first element is unread |
compute_reconstruction_r_fv_lupwind(s, a(i+1-s:i), f) | gideon_fv_face_ul(s, a(i+1-s:i), f) (audit A) |
compute_derivative1_fv_rupwind(s, ds, a(i-1:i+s), d) | gideon_fv_d1_ur(s, a(i:i+s), ds, d): ADAM's first element is unread |
compute_derivative1_fv_lupwind(s, ds, a(i-s:i), d) | gideon_fv_d1_ul(s, a(i-s:i), ds, d) (audit A) |
compute_derivativeN_fv_rupwind(s, ds, a(i-s:i), d), N = 2…5 | gideon_fv_dN_ul(s+1-N, a(i-s:i), ds, d): the left side (F) |
compute_derivativeN_fv_lupwind(s, ds, a(i:i+s), d), N = 2…5 | gideon_fv_dN_ur(s+1-N, a(i:i+s), ds, d): the right side (F) |
compute_reconstruction_r_fv_rupwind_dN(s, a(i+1-s:i), f) | gideon_fv_face_dN_ul(s-N, a(i+1-s:i), 1._R_P, f): left side, unscaled |
compute_reconstruction_r_fv_lupwind_dN(s, a(i+1:i+s), f) | gideon_fv_face_dN_ur(s-N, a(i+1:i+s), 1._R_P, f): right side, unscaled |
compute_derivative6_fv_{r,l}upwind | gideon_fv_d6_ur, gideon_fv_d6_ul (ADAM returned 0: audit C) |
compute_gradient_{fd,fv}_centered_dev(s, dxyz, qsx, qsy, qsz, g) | gideon_{fd,fv}_gradient_c(2*s, qx, qy, qz, dxyz, g) |
compute_divergence_{fd,fv}_centered_dev(s, dxyz, qsx, qsy, qsz, div) | gideon_{fd,fv}_divergence_c(2*s, vx_x, vy_y, vz_z, dxyz, div) |
compute_curl_{fd,fv}_centered_dev(s, dxyz, qsx_y, qsx_z, qsy_x, qsy_z, qsz_x, qsz_y, c) | gideon_{fd,fv}_curl_c(2*s, vy_x, vz_x, vx_y, vz_y, vx_z, vy_z, dxyz, c): the same stencils in the same order |
compute_laplacian_fv_centered_dev(s, dxyz, qsx, qsy, qsz, lap) | gideon_fv_laplacian_c(2*s, qx, qy, qz, dxyz, lap) |
compute_laplacian_fd_centered_dev(s, dxyz, q, lap), a cube | gideon_fd_laplacian_c(2*s, qx, qy, qz, dxyz, lap): gather the three axes |
compute_{gradient,divergence,curl,laplacian}_{fd,fv}_centered(s, dxyz, q, r), a cube | the same GIDEON operator on the stencils through the point, e.g. f(i-s:i+s,j,k), f(i,j-s:j+s,k), f(i,j,k-s:k+s) |
the tables FD1_CC(m,s), FV1_CC(m,s) at a run-time s (device code included) | FD_D1(m,s), FV_FACE(m,s) of gideon_tables: the same layout, bit for bit (column s is the row FD_D1_S<s>, FV_FACE_S<s>, zero padded), with !$acc declare copyin |
FDV_S_MAX | GIDEON_S_MAX (5) |
the tables at a compile-time s, and FD0_CC(m,s) | the rows FD_FACE_S<s>(m), FD_D1_S<s>(m), FV_FACE_S<s>(m): compile-time constants, no !$acc declare copyin |
With s a compile-time constant, call the kernel of that order instead of the dispatcher (gideon_fd_d1_c6 for s = 3): the dispatcher costs 9 to 18 % more (A run-time order, fast). On the host, a non-contiguous section (f(i,j-s:j+s,k)) passed to an explicit-shape dummy is copied by the compiler; in device code, gather the stencils into local arrays first, as ADAM's FNL callers already do.
The program below makes each replacement on the data of a cubic, where every operator in it is exact, and prints the value next to the exact one (
program cookbook_from_adam
!< Cookbook, replace ADAM's FDV calls: the GIDEON call that replaces each ADAM family, on the data of a cubic, where
!< every operator below is exact: each line prints the value and the exact one. ADAM's half-stencil s is a run-time
!< integer here (s = 3), so the dispatchers are called; a compile-time s calls the kernel of that order. The ADAM call
!< each line replaces is in the comment above it (docs/cookbook/from-adam.md).
use gideon
implicit none
integer(I4P), parameter :: S = 3, I = 10 ! ADAM's half-stencil; the node or cell of the calls
real(R_P), parameter :: H = 0.1_R_P ! grid step
real(R_P) :: u(0:20), a(0:20) ! point values at x = j h; averages over [x - h/2, x + h/2]
real(R_P) :: g(3), x, r
integer(I4P) :: j
do j=0, 20
x = j*H
u(j) = p(x)
a(j) = x**3 + x*H**2/4 - 2*x ! the average of p over the cell centred at x
enddo
x = I*H
! compute_derivative1_fd_centered(s=s, ds=ds, q=u(i-s:i+s), dq_ds=d): the same section, order 2s
call gideon_fd_d1_c(2*S, u(I-S:I+S), H, r)
call show('fd d1 at node i', r, 3*x**2 - 2)
! compute_derivative3_fd_centered(s=s, ds=ds, q=u(i-s:i+s), d3q_ds3=d): order 2s - 2 for d3, d4; 2s - 4 for d5, d6
call gideon_fd_d3_c(2*S - 2, u(I-S:I+S), H, r)
call show('fd d3 at node i', r, 6._R_P)
! compute_reconstruction_r_fd_centered(s=s, q=u(i+1-s:i+s), qr=f): the face i+1/2
call gideon_fd_face_c(2*S, u(I+1-S:I+S), r)
call show('fd value at face i+1/2', r, p(x + H/2))
! compute_derivative2_fv_centered(s=s, ds=ds, q=a(i-s:i+s), d2q_ds2=d): the cell average of d2
call gideon_fv_d2_c(2*S, a(I-S:I+S), H, r)
call show('fv d2 over cell i', r, 6*x)
! compute_reconstruction_r_fv_centered_d1(s=s, q=a(i+1-s:i+s), qr=f): ADAM's is unscaled, d1 times h; ds = 1 keeps it
call gideon_fv_face_d1_c(2*S, a(I+1-S:I+S), 1._R_P, r)
call show('fv d1*h at face i+1/2', r, (3*(x + H/2)**2 - 2)*H)
! compute_avg_to_point(s=s, q=a(i-s:i+s), qp=v): order 2s + 2
call gideon_fv_point_c(2*S + 2, a(I-S:I+S), r)
call show('fv point value at cell i', r, p(x))
! compute_derivative1_fv_rupwind(s=s, ds=ds, q=a(i-1:i+s), dq_ds=d): ADAM's window starts one cell early, unread
call gideon_fv_d1_ur(S, a(I:I+S), H, r)
call show('fv d1 over cell i, right side', r, (p(x + H/2) - p(x - H/2))/H)
! compute_derivative1_fv_lupwind(s=s, ds=ds, q=a(i-s:i), dq_ds=d): wrong in ADAM for s >= 2 (audit A)
call gideon_fv_d1_ul(S, a(I-S:I), H, r)
call show('fv d1 over cell i, left side', r, (p(x + H/2) - p(x - H/2))/H)
! compute_derivative2_fv_rupwind(s=s, ds=ds, q=a(i-s:i), d2q_ds2=d): ADAM's d2-d5 "rupwind" read the left side (F)
call gideon_fv_d2_ul(S - 1, a(I-S:I), H, r)
call show('fv d2 over cell i, left side', r, 6*x)
! compute_derivative2_fv_lupwind(s=s, ds=ds, q=a(i:i+s), d2q_ds2=d): and "lupwind" the right side
call gideon_fv_d2_ur(S - 1, a(I:I+S), H, r)
call show('fv d2 over cell i, right side', r, 6*x)
! compute_gradient_fd_centered(s=s, dxyz=dxyz, q=f(i-s:i+s,j-s:j+s,k-s:k+s), gradient=g): one stencil per axis
call gideon_fd_gradient_c(2*S, u(I-S:I+S), u(I-S:I+S), u(I-S:I+S), [H, H, H], g)
call show('fd gradient, x component', g(1), 3*x**2 - 2)
contains
pure function p(x) result(v)
!< The cubic of the data.
real(R_P), intent(in) :: x
real(R_P) :: v
v = x**3 - 2*x
endfunction p
subroutine show(what, r, exact)
!< One line: the operator, its value, the exact one.
character(*), intent(in) :: what
real(R_P), intent(in) :: r, exact
print '(A,T32,2F14.8)', what, r, exact
endsubroutine show
endprogram cookbook_from_adamfd d1 at node i 1.00000000 1.00000000
fd d3 at node i 6.00000000 6.00000000
fd value at face i+1/2 -0.94237500 -0.94237500
fv d2 over cell i 6.00000000 6.00000000
fv d1*h at face i+1/2 0.13075000 0.13075000
fv point value at cell i -1.00000000 -1.00000000
fv d1 over cell i, right side 1.00250000 1.00250000
fv d1 over cell i, left side 1.00250000 1.00250000
fv d2 over cell i, left side 6.00000000 6.00000000
fv d2 over cell i, right side 6.00000000 6.00000000
fd gradient, x component 1.00000000 1.00000000What changes numerically
| ADAM | Change after the migration |
|---|---|
| FD and FV centred (face, face derivatives, cell derivatives), deconvolution, the 3-D operators (FNL and cube signatures) | none: bitwise (the same rows, the pair form, the step divided inside in ADAM's way) |
compute_reconstruction_r_fv_rupwind, compute_derivative1_fv_rupwind | none: bitwise |
compute_derivative{2..5}_fv_rupwind, compute_reconstruction_r_fv_rupwind_d{1..4} (the left side) | none: bitwise |
compute_derivative{2..5}_fv_lupwind, compute_reconstruction_r_fv_lupwind_d{1..4} (the right side) | round-off, within 64 ε of the scale: ADAM sums the farthest cell first |
compute_reconstruction_r_fv_lupwind, compute_derivative1_fv_lupwind, | fixed (audit A): ADAM's results were wrong, GIDEON's are exact to their order |
| a zero row: centred d3, d4 (FD and FV cell) and face d2, d3 at | no kernel: the call has no GIDEON equivalent, the dispatcher returns GIDEON_NAN (audit C) |
The audit items behind the changes (notes/adam-fdv-audit.md, measured with notes/adam-reference/check_upwind_d1.F90):
- A.
FV1_ULis stored in ascending cell order and read descending,FV1_UL(m,s)*q(1-m): the row is mirrored twice. Forthe left-side face value misses a linear function and the left-side d1 a quadratic one, and compute_derivative1_fv_lupwindconverges at first order for every. GIDEON generates every left-side row as the mirror of the right one, and a property test asserts it bitwise. - B.
compute_derivative1_fv_rupwinddeclaresq(0:)over[0:1+s]with the cell atq(1); ADAM's own test passedu(i:i+s), one element short, so the kernel read one element past the end and returned the derivative of cell. A caller that passed u(i:i+s)meaning cellgets cell from gideon_fv_d1_ur(s, u(i:i+s), ...): its results change, and are now those it meant. - C. Unsupported orders returned 0 from pure, device-callable code. GIDEON has no kernel for them (a missing symbol at compile time); its dispatchers return
GIDEON_NAN, a quiet NaN, for an order without a kernel. Check the order once, before the loop (A run-time order, fast). - F. ADAM's
rupwindandlupwindswap sides between d1 and d2–d5. A migrated call names the side its stencil lies on: check eachrupwind/lupwindcall against the table above, not by its name.