Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions docs/source/developer/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,21 @@ The binary is ``build/bin/x3d2_poisson_solver``. Its usage line is:
x3d2_poisson_solver nx ny nz [bc_x bc_y bc_z] [--repeat N]

``nx``, ``ny`` and ``nz`` are the global grid dimensions.
Each of ``bc_x`` and ``bc_y`` is ``periodic`` (the default) or ``dirichlet``.
The argument ``bc_z`` must be ``periodic``.
Each of ``bc_x``, ``bc_y`` and ``bc_z`` is ``periodic`` (the default) or ``dirichlet``.
A direction with ``dirichlet`` needs an odd grid size.
Here ``dirichlet`` is the velocity condition. The pressure gets homogeneous Neumann conditions in that direction.
``--repeat N`` sets the number of solves (default 1).

The driver supports four boundary condition cases.
The driver supports five boundary condition cases.
Each case has a three digit name.
A digit is ``1`` if the direction is ``dirichlet`` and ``0`` if it is ``periodic``.
The cases are ``000`` (all periodic), ``100`` (``bc_x`` is ``dirichlet``), ``010`` (``bc_y`` is ``dirichlet``) and ``110`` (``bc_x`` and ``bc_y`` are ``dirichlet``).
The OpenMP and CUDA backends solve all four cases.
The cases are ``000`` (all periodic), ``100`` (``bc_x`` is ``dirichlet``), ``010`` (``bc_y`` is ``dirichlet``), ``110`` (``bc_x`` and ``bc_y`` are ``dirichlet``) and ``111`` (all three are ``dirichlet``).
A ``dirichlet`` z needs ``dirichlet`` x and y as well: the cases ``001``, ``011`` and ``101`` are not supported.
The OpenMP and CUDA backends solve all five cases.
The ``010`` case runs on one MPI rank only.
On the CUDA backend, the ``100`` case on more than one rank needs the number of x cells to divide by the number of ranks.
On the CUDA backend, the ``110`` case on more than one rank needs the same for the number of x cells and the number of y cells.
On the CUDA backend, the ``110`` and ``111`` cases on more than one rank need the same for the number of x, y and z cells.
On the OpenMP backend, the ``110`` and ``111`` cases on more than one rank need the number of cells in a split direction to divide by the number of ranks along it.

The backend follows ``ENABLE_BACKEND``.
The ``OMP_TGT`` backend has no FFT Poisson solver, so the driver stops with that backend.
Expand Down
15 changes: 7 additions & 8 deletions examples/wind_turbine_alm/input.x3d
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ flow_case_name = 'wind_turbine'
! line model. Dimensional units: metres, m/s.
L_global = 8.8d0, 1.8d0, 3.4d0

! Same spacing as Incompact3d (nx = 641, ny = 129, nz = 321), with y and z
! periodic.
dims_global = 641, 128, 320
! Same grid as Incompact3d (nx = 641, ny = 129, nz = 321).
dims_global = 641, 129, 321

! The y walls need a single rank in y; z can be split.
nproc_dir = 1, 1, 1

! x: inflow (dirichlet) / convective outflow (dirichlet); y,z periodic.
! Incompact3d uses free-slip walls in y and z, but the FFT Poisson solver
! requires z periodic and a single MPI rank in non-periodic directions.
! x: inflow (dirichlet) / convective outflow (dirichlet); free-slip walls
! (neumann) in y and z.
BC_x = 'dirichlet', 'dirichlet'
BC_y = 'periodic', 'periodic'
BC_z = 'periodic', 'periodic'
BC_y = 'neumann', 'neumann'
BC_z = 'neumann', 'neumann'
/End

&solver_params
Expand Down
55 changes: 49 additions & 6 deletions src/backend/backend.f90
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module m_base_backend

use m_allocator, only: allocator_t
use m_common, only: dp, DIR_C, DIR_X, VERT, NULL_LOC, X_FACE, MPI_X3D2_DP, &
get_rdr_from_dirs
BC_DIRICHLET, get_rdr_from_dirs
use m_field, only: field_t
use m_logger, only: abort_run
use m_mesh, only: mesh_t
Expand Down Expand Up @@ -73,6 +73,7 @@ module m_base_backend
procedure :: field_shift => field_shift_base
procedure :: field_volume_integral => field_volume_integral_base
procedure :: field_set_face => field_set_face_base
procedure :: z_face_owned
procedure :: field_set_y_plane => field_set_y_plane_base
procedure :: field_set_abl_wall_stress => field_set_abl_wall_stress_base
procedure :: field_shift_blend_x_band => field_shift_blend_x_band_base
Expand Down Expand Up @@ -631,19 +632,22 @@ subroutine slice_max_sum_base(self, max_val, sum_val, f, &

end subroutine slice_max_sum_base

subroutine x_slice_weighted_sum_base(self, sum_val, f, i_slice, y_weights)
subroutine x_slice_weighted_sum_base(self, sum_val, f, i_slice, y_weights, &
z_weights)
!! Weighted sum of the x slice f(i_slice, :, :) of a DIR_X field: the
!! point with local y index j counts y_weights(j) times. Gives the flow
!! rate through an x face as the discrete divergence sees it, whose y
!! interpolation weights the boundary planes differently from the
!! interior ones. Caller is responsible for MPI_Allreduce across ranks.
!! point with local indices (j, k) counts y_weights(j)*z_weights(k) times,
!! with z_weights 1 when absent. Gives the flow rate through an x face as
!! the discrete divergence sees it, whose y and z interpolations weight
!! the boundary planes differently from the interior ones. Caller is
!! responsible for MPI_Allreduce across ranks.
implicit none

class(base_backend_t) :: self
real(dp), intent(out) :: sum_val
class(field_t), intent(in) :: f
integer, intent(in) :: i_slice
real(dp), intent(in) :: y_weights(:)
real(dp), optional, intent(in) :: z_weights(:)

sum_val = 0._dp
call not_implemented('x_slice_weighted_sum')
Expand Down Expand Up @@ -710,6 +714,8 @@ subroutine field_set_face_base(self, f, c_start, c_end, face, &
!! 'c_start' and 'c_end' for faces at opposite sides.
!! 'face' is one of X_FACE, Y_FACE, Z_FACE from common.f90
!! Optionally, bc_start/bc_end select the BC type (default BC_DIRICHLET).
!! z is decomposed, so a Z_FACE is set only on the ranks that hold that
!! global boundary (see z_face_owned).
implicit none

class(base_backend_t) :: self
Expand All @@ -724,6 +730,43 @@ subroutine field_set_face_base(self, f, c_start, c_end, face, &

end subroutine field_set_face_base

function z_weight_line(z_weights, nz) result(wz)
!! The z weights of x_slice_weighted_sum on the nz local z points:
!! z_weights when present, 1 otherwise.
implicit none

real(dp), optional, intent(in) :: z_weights(:)
integer, intent(in) :: nz
real(dp), allocatable :: wz(:)

if (present(z_weights)) then
if (size(z_weights) /= nz) &
call abort_run('x_slice_weighted_sum: one weight per z point needed')
wz = z_weights
else
allocate (wz(nz), source=1._dp)
end if
end function z_weight_line

subroutine z_face_owned(self, bc_start, bc_end, set_start, set_end)
!! Which global z faces this rank sets in field_set_face: a face that
!! carries a prescribed value (bc_start, bc_end optional, default
!! BC_DIRICHLET) and that this rank holds, the bottom face on the first
!! rank along z and the top face on the last one.
implicit none

class(base_backend_t) :: self
integer, optional, intent(in) :: bc_start, bc_end
logical, intent(out) :: set_start, set_end

set_start = self%mesh%par%nrank_dir(3) == 0
set_end = self%mesh%par%nrank_dir(3) == self%mesh%par%nproc_dir(3) - 1
if (present(bc_start)) &
set_start = set_start .and. bc_start == BC_DIRICHLET
if (present(bc_end)) set_end = set_end .and. bc_end == BC_DIRICHLET

end subroutine z_face_owned

subroutine field_set_y_plane_base(self, f, c, plane)
!! Set one interior y-plane of a DIR_X field to a constant.
!!
Expand Down
43 changes: 29 additions & 14 deletions src/backend/cuda/backend.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module m_cuda_backend
use m_allocator, only: allocator_t
use m_base_backend, only: base_backend_t, source_index_boxes, &
check_point_fields, check_labelled_fields, &
abl_wall_upper_plane, shifted_z_source
abl_wall_upper_plane, shifted_z_source, &
z_weight_line
use m_common, only: dp, sp, MPI_X3D2_DP, move_data_loc, get_rdr_from_dirs, &
RDR_X2Y, RDR_X2Z, RDR_Y2X, RDR_Y2Z, RDR_Z2X, RDR_Z2Y, &
RDR_C2X, RDR_C2Y, RDR_C2Z, RDR_X2C, RDR_Y2C, RDR_Z2C, &
Expand All @@ -32,6 +33,7 @@ module m_cuda_backend
labelled_axpy, sample_points, &
add_gaussian_sources, &
field_max_sum, field_set_y_face, &
field_set_z_face, &
field_set_x_face, field_set_y_plane, &
field_set_abl_wall_stress, &
field_get_x_band, field_blend_x_band, &
Expand Down Expand Up @@ -1516,35 +1518,38 @@ subroutine slice_max_sum_cuda(self, max_val, sum_val, f, i_slice, &
end subroutine slice_max_sum_cuda

attributes(global) subroutine x_slice_weighted_sum_kernel( &
sum_f, f, w, i_slice, n_i_pad, n_j)
!! Weighted sum of the x slice f(:, i_slice, :) of a DIR_X field, w(j)
!! on the points of y index j. One thread per (y, z) point; z is the
!! fast-varying component of the group index.
sum_f, f, w, wz, i_slice, n_i_pad, n_j)
!! Weighted sum of the x slice f(:, i_slice, :) of a DIR_X field,
!! w(j)*wz(k) on the point of y index j and z index k. One thread per
!! (y, z) point; z is the fast-varying component of the group index.
implicit none
real(dp), device, intent(inout) :: sum_f
real(dp), device, intent(in), dimension(:, :, :) :: f
real(dp), device, intent(in), dimension(:) :: w
real(dp), device, intent(in), dimension(:) :: w, wz
integer, value, intent(in) :: i_slice, n_i_pad, n_j
integer :: i, j, b, ierr

i = threadIdx%x
j = i + (blockIdx%y - 1)*blockDim%x
b = blockIdx%x + (blockIdx%y - 1)*n_i_pad

if (j <= n_j) ierr = atomicadd(sum_f, w(j)*f(i, i_slice, b))
if (j <= n_j) &
ierr = atomicadd(sum_f, w(j)*wz(blockIdx%x)*f(i, i_slice, b))
end subroutine x_slice_weighted_sum_kernel

subroutine x_slice_weighted_sum_cuda(self, sum_val, f, i_slice, y_weights)
subroutine x_slice_weighted_sum_cuda(self, sum_val, f, i_slice, y_weights, &
z_weights)
!! [[m_base_backend(module):x_slice_weighted_sum_base(subroutine)]]
implicit none
class(cuda_backend_t) :: self
real(dp), intent(out) :: sum_val
class(field_t), intent(in) :: f
integer, intent(in) :: i_slice
real(dp), intent(in) :: y_weights(:)
real(dp), optional, intent(in) :: z_weights(:)

real(dp), device, pointer, dimension(:, :, :) :: f_d
real(dp), device, allocatable :: sum_d, w_d(:)
real(dp), device, allocatable :: sum_d, w_d(:), wz_d(:)
integer :: dims(3), dims_padded(3)
type(dim3) :: blocks, threads

Expand All @@ -1562,14 +1567,15 @@ subroutine x_slice_weighted_sum_cuda(self, sum_val, f, i_slice, y_weights)
call abort_run('x_slice_weighted_sum: one weight per y point needed')

call resolve_field_t(f_d, f)
allocate (sum_d, w_d(dims(2)))
allocate (sum_d, w_d(dims(2)), wz_d(dims(3)))
sum_d = 0._dp
w_d = y_weights
wz_d = z_weight_line(z_weights, dims(3))

blocks = dim3(dims(3), (dims(2) - 1)/SZ + 1, 1)
threads = dim3(SZ, 1, 1)
call x_slice_weighted_sum_kernel<<<blocks, threads>>>( & !&
sum_d, f_d, w_d, i_slice, dims_padded(3), dims(2))
sum_d, f_d, w_d, wz_d, i_slice, dims_padded(3), dims(2))

! Rank-local value; caller is responsible for MPI_Allreduce.
sum_val = sum_d
Expand Down Expand Up @@ -1629,9 +1635,10 @@ subroutine field_set_face_cuda(self, f, c_start, c_end, face, &

real(dp), device, pointer, dimension(:, :, :) :: f_d
type(dim3) :: blocks, threads
integer :: dims(3)
integer :: dims(3), dims_padded(3)
integer :: bc_s, bc_e
real(dp) :: flow_rate_diff_val
logical :: set_start, set_end

if (f%dir /= DIR_X) then
call abort_run('Setting a field face is only supported for DIR_X &
Expand Down Expand Up @@ -1666,15 +1673,23 @@ subroutine field_set_face_cuda(self, f, c_start, c_end, face, &
! is left untouched so the two y-faces can differ (ABL: no-slip floor,
! free-slip lid). Both default to Dirichlet, preserving the historical
! behaviour of writing both faces.
dims_padded = self%allocator%get_padded_dims(DIR_C)
blocks = dim3((dims(1) - 1)/64 + 1, dims(3), 1)
threads = dim3(64, 1, 1)
call field_set_y_face<<<blocks, threads>>>( & !&
f_d, c_start, c_end, bc_s == BC_DIRICHLET, &
bc_e == BC_DIRICHLET, flow_rate_diff_val, &
dims(1), dims(2), dims(3))
dims(1), dims(2), dims_padded(3))

case (Z_FACE)
call abort_run('Setting Z_FACE is not yet supported.')
! z is decomposed: only the ranks holding a global z face set it
call self%z_face_owned(bc_start, bc_end, set_start, set_end)
dims_padded = self%allocator%get_padded_dims(DIR_C)
blocks = dim3((dims(1) - 1)/64 + 1, (dims(2) - 1)/SZ + 1, 1)
threads = dim3(64, 1, 1)
call field_set_z_face<<<blocks, threads>>>( & !&
f_d, c_start, c_end, set_start, set_end, &
dims(1), dims(2), dims(3), dims_padded(3))

case default
call abort_run('face is undefined.')
Expand Down
32 changes: 31 additions & 1 deletion src/backend/cuda/kernels/fieldops.f90
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,12 @@ attributes(global) subroutine field_set_y_face( &
integer :: i, j, b, n_mod, b_end

j = threadIdx%x + (blockIdx%x - 1)*blockDim%x ! from 1 to nx
b = blockIdx%y ! from 1 to nz
b = blockIdx%y ! from 1 to the local z count of the field

! DIR_X groups are ordered z fastest: b = (y_block - 1)*nz + z, so the
! bottom face lives in groups 1..nz and the top face nz*(n_y_blocks-1)+z.
! nz is the padded z count, the group stride: a cell field on a
! non-periodic z holds one z station fewer on the last rank.
n_mod = mod(ny - 1, SZ) + 1
b_end = b + (ny - 1)/SZ*nz

Expand All @@ -420,6 +422,34 @@ attributes(global) subroutine field_set_y_face( &
end if

end subroutine field_set_y_face

attributes(global) subroutine field_set_z_face( &
f, c_start, c_end, set_start, set_end, nx, ny, nz, nz_pad)
!! Set the bottom (local z = 1) and top (local z = nz) Z_FACE planes of a
!! DIR_X field where flagged. DIR_X groups are ordered z fastest,
!! b = (y_block - 1)*nz_pad + z, with nz_pad the padded z count. Launch with
!! blocks = dim3((nx - 1)/64 + 1, n_y_blocks, 1), threads = dim3(64, 1, 1).
implicit none

real(dp), device, intent(inout), dimension(:, :, :) :: f
real(dp), value, intent(in) :: c_start, c_end
logical, value, intent(in) :: set_start, set_end
integer, value, intent(in) :: nx, ny, nz, nz_pad

integer :: i, j, y_block, n_lane

j = threadIdx%x + (blockIdx%x - 1)*blockDim%x ! from 1 to nx
y_block = blockIdx%y

if (j <= nx) then
n_lane = min(SZ, ny - (y_block - 1)*SZ) ! lanes past ny are padding
do i = 1, n_lane
if (set_start) f(i, j, (y_block - 1)*nz_pad + 1) = c_start
if (set_end) f(i, j, (y_block - 1)*nz_pad + nz) = c_end
end do
end if

end subroutine field_set_z_face
attributes(global) subroutine field_plane_partial_sums( &
partial, f, nx, ny, nz)
!! First pass of field_plane_sums: partial(j, z) is the sum over x of
Expand Down
Loading
Loading