Skip to content

3. A field and a box ​

A block-structured solver stores a field as a pool of blocks. Each block has interior cells and a frame of ghost cells, and each cell has NV components. SELAH's kernels take such an array in one of two layouts, and a box: the range of cells they loop over. They write those cells, and nothing else.

fortran
program box_tour
!< Tutorial, chapter 3: a block-structured field, the box a kernel loops over, and what it leaves untouched.
use selah
implicit none
integer(I4P), parameter :: NV=2, NI=4, NJ=3, NK=2, NGC=2, NB=4   ! components, cells per block, ghosts, pool of blocks
real(R_P),    parameter :: SENTINEL=-9._R_P
real(R_P), allocatable  :: x(:,:,:,:,:), y(:,:,:,:,:)
type(box)               :: bx, part

! V_FIRST layout (v,i,j,k,b): the components innermost (the CPU layout); ghosts 1-NGC..N+NGC on i, j, k
allocate(x(1:NV, 1-NGC:NI+NGC, 1-NGC:NJ+NGC, 1-NGC:NK+NGC, 1:NB))
allocate(y, mold=x)
x = 1._R_P
bx = box_interior(LAYOUT_V_FIRST, NV, NI, NJ, NK, NGC, 3_I4P)   ! the interior of the first 3 blocks of the pool
print '(A,5I4)', 'lb (allocation lower bounds):', bx%lb
print '(A,5I4)', 'lo (loop start):             ', bx%lo
print '(A,5I4)', 'hi (loop end):               ', bx%hi
y = SENTINEL
call selah_axpy(bx, 2._R_P, x, y)                                 ! y = y + 2 x, in the box only
print '(A,I0,A,I0)', 'cells updated: ', count(y /= SENTINEL), ' of ', size(y)
print '(A,I0)', 'expected (2 components x 4x3x2 cells x 3 blocks): ', NV*NI*NJ*NK*3
print '(A,L1)', 'ghosts and block 4 untouched: ', all(y(:,:,:,:,4) == SENTINEL) .and. all(y(:,0,:,:,:) == SENTINEL)
part = box_components(bx, 2_I4P, 2_I4P)                           ! component 2 only: a partition
y = SENTINEL
call selah_copy(part, x, y)
print '(A,I0,A,I0)', 'component range 2..2: cells updated ', count(y /= SENTINEL), ', component 1 untouched: ', &
      merge(1, 0, all(y(1,:,:,:,:) == SENTINEL))
! V_LAST layout (b,i,j,k,v): the blocks innermost (ADAM's device layout); the same box, built for that layout
deallocate(x, y)
allocate(x(1:NB, 1-NGC:NI+NGC, 1-NGC:NJ+NGC, 1-NGC:NK+NGC, 1:NV))
allocate(y, mold=x)
x = 1._R_P ; y = SENTINEL
bx = box_interior(LAYOUT_V_LAST, NV, NI, NJ, NK, NGC, 3_I4P)
call selah_axpy(bx, 2._R_P, x, y)
print '(A,I0,A,I0)', 'V_LAST: component dimension ', component_dim(bx), ', cells updated ', count(y /= SENTINEL)
endprogram box_tour
  • The array. Ghost cells sit on the cell indices, 1-NGC : N+NGC; components and blocks start at 1. The pool (NB = 4 blocks) may be larger than the active set.
  • box_interior(layout, nv, ni, nj, nk, ngc, blocks) builds the box of the interior cells of the first blocks blocks, every component:
    • lb holds the array's lower bounds, which the kernels need to address it;
    • lo:hi is the loop range.
  • box_components(bx, v1, v2) narrows a box to the components v1..v2: a partition of a splitting scheme, or the passive scalars of a solver.
  • The layouts.
    • LAYOUT_V_FIRST is (v,i,j,k,b), components innermost: the CPU layout.
    • LAYOUT_V_LAST is (b,i,j,k,v), blocks innermost: ADAM's device layout, where consecutive device threads read consecutive blocks.
    • component_dim(bx) says which dimension holds the components.

Running it ​

text
$ box
lb (allocation lower bounds):   1  -1  -1  -1   1
lo (loop start):                1   1   1   1   1
hi (loop end):                  2   4   3   2   3
cells updated: 144 of 2688
expected (2 components x 4x3x2 cells x 3 blocks): 144
ghosts and block 4 untouched: T
component range 2..2: cells updated 72, component 1 untouched: 1
V_LAST: component dimension 5, cells updated 144

The kernel updated exactly the 144 interior values of the 3 active blocks. The ghost cells and the fourth block of the pool kept the sentinel bit for bit, and so did component 1 when the box was narrowed to component 2. This is the first clause of SELAH's stage contract: a kernel never writes a ghost cell. Ghost cells belong to the caller (see Concepts).

Explicit bounds

A field with ghost cells must reach the kernels with its own lower bounds. An assumed-shape dummy without bounds silently renumbers it from 1, and the interior is then off by NGC. Every SELAH dummy is declared q(bx%lb(1):, ..., bx%lb(5):).

What you learned

The field, the box (lb, lo, hi), the two layouts, component ranges; a kernel writes the box and nothing else. Reference: Concepts, Kernels.

Next: 4. The stepper.

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