diff --git a/docs/source/developer/build.rst b/docs/source/developer/build.rst index 5fb1b06405..aa3ac4f668 100644 --- a/docs/source/developer/build.rst +++ b/docs/source/developer/build.rst @@ -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. diff --git a/examples/wind_turbine_alm/input.x3d b/examples/wind_turbine_alm/input.x3d index 8255f0738e..3e8e534b13 100644 --- a/examples/wind_turbine_alm/input.x3d +++ b/examples/wind_turbine_alm/input.x3d @@ -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 diff --git a/src/backend/backend.f90 b/src/backend/backend.f90 index ecb5c0eab7..84c1db53d6 100644 --- a/src/backend/backend.f90 +++ b/src/backend/backend.f90 @@ -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 @@ -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 @@ -631,12 +632,14 @@ 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 @@ -644,6 +647,7 @@ subroutine x_slice_weighted_sum_base(self, sum_val, f, i_slice, y_weights) 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') @@ -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 @@ -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. !! diff --git a/src/backend/cuda/backend.f90 b/src/backend/cuda/backend.f90 index b76f1d3503..86281c0f22 100644 --- a/src/backend/cuda/backend.f90 +++ b/src/backend/cuda/backend.f90 @@ -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, & @@ -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, & @@ -1516,14 +1518,14 @@ 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 @@ -1531,10 +1533,12 @@ attributes(global) subroutine x_slice_weighted_sum_kernel( & 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 @@ -1542,9 +1546,10 @@ subroutine x_slice_weighted_sum_cuda(self, sum_val, f, i_slice, y_weights) 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 @@ -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<<>>( & !& - 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 @@ -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 & @@ -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<<>>( & !& 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<<>>( & !& + 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.') diff --git a/src/backend/cuda/kernels/fieldops.f90 b/src/backend/cuda/kernels/fieldops.f90 index e443fd7b40..c12852efc5 100644 --- a/src/backend/cuda/kernels/fieldops.f90 +++ b/src/backend/cuda/kernels/fieldops.f90 @@ -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 @@ -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 diff --git a/src/backend/cuda/kernels/spectral_processing.f90 b/src/backend/cuda/kernels/spectral_processing.f90 index effa9f02a9..54c0ce23c4 100644 --- a/src/backend/cuda/kernels/spectral_processing.f90 +++ b/src/backend/cuda/kernels/spectral_processing.f90 @@ -216,9 +216,11 @@ attributes(global) subroutine memcpy3D_with_transpose_back( & end subroutine memcpy3D_with_transpose_back - attributes(global) subroutine transpose_xyz_to_zxy(dst, src, nx, ny, nz) + attributes(global) subroutine transpose_xyz_to_zxy(dst, src, nx, ny, nz, & + fold_z) !! Transpose: src(nx, ny, nz) → dst(nz, nx, ny) - !! Used before R2C FFT for 110 case to put Z (periodic) in fast dim. + !! Used before R2C FFT for the 110 and 111 cases to put Z in the fast + !! dim. With fold_z (111), Z is folded on the way (see fold_destination). !! !! Launch: blocks=dim3(nz, (ny-1)/tpb+1, 1), threads=dim3(tpb,1,1) !! Each thread handles one (k,j) pair, loops over i. @@ -227,23 +229,29 @@ attributes(global) subroutine transpose_xyz_to_zxy(dst, src, nx, ny, nz) real(dp), device, intent(out), dimension(:, :, :) :: dst ! (nz, nx, ny) real(dp), device, intent(in), dimension(:, :, :) :: src ! (nx, ny, nz) integer, value, intent(in) :: nx, ny, nz + logical, value, intent(in) :: fold_z - integer :: i, j, k + integer :: i, j, k, kd k = blockIdx%x ! nz j = (blockIdx%y - 1)*blockDim%x + threadIdx%x ! ny + kd = k + if (fold_z) kd = fold_destination(k, nz) + if (j <= ny) then do i = 1, nx - dst(k, i, j) = src(i, j, k) + dst(kd, i, j) = src(i, j, k) end do end if end subroutine transpose_xyz_to_zxy - attributes(global) subroutine transpose_zxy_to_xyz(dst, src, nx, ny, nz) + attributes(global) subroutine transpose_zxy_to_xyz(dst, src, nx, ny, nz, & + fold_z) !! Transpose back: src(nz, nx, ny) -> dst(nx, ny, nz) - !! Used after C2R FFT for 110 case. + !! Used after C2R FFT for the 110 and 111 cases, undoing the Z fold of + !! transpose_xyz_to_zxy with fold_z. !! !! Launch: blocks=dim3(nz, (ny-1)/tpb+1, 1), threads=dim3(tpb,1,1) implicit none @@ -251,15 +259,19 @@ attributes(global) subroutine transpose_zxy_to_xyz(dst, src, nx, ny, nz) real(dp), device, intent(out), dimension(:, :, :) :: dst ! (nx, ny, nz) real(dp), device, intent(in), dimension(:, :, :) :: src ! (nz, nx, ny) integer, value, intent(in) :: nx, ny, nz + logical, value, intent(in) :: fold_z - integer :: i, j, k + integer :: i, j, k, ks k = blockIdx%x ! nz j = (blockIdx%y - 1)*blockDim%x + threadIdx%x ! ny + ks = k + if (fold_z) ks = fold_destination(k, nz) + if (j <= ny) then do i = 1, nx - dst(i, j, k) = src(k, i, j) + dst(i, j, k) = src(ks, i, j) end do end if @@ -1399,27 +1411,31 @@ attributes(global) subroutine pack_zslab_to_alltoall( & end subroutine pack_zslab_to_alltoall attributes(global) subroutine unpack_110_yslab( & - dst, recvbuf, nx, ny_l, nz_l, nproc & + dst, recvbuf, nx, ny_l, nz_l, nproc, fold_z & ) !! Unpacks the MPI_Alltoall receive buffer into the padded real view !! (2*(nz/2+1), nx, ny_l) of the cuFFTMp Y-slab descriptor: data !! received from source rank s reconstructs the global Z range - !! [s*nz_l+1, (s+1)*nz_l]. + !! [s*nz_l+1, (s+1)*nz_l]. With fold_z (111), Z is folded on the way + !! (see fold_destination), since this is where Z is first whole. implicit none real(dp), device, intent(inout), dimension(:, :, :) :: dst ! (2*(nz/2+1), nx, ny_l) real(dp), device, intent(in), dimension(:, :, :, :) :: recvbuf ! (nz_l, nx, ny_l, nproc) integer, value, intent(in) :: nx, ny_l, nz_l, nproc + logical, value, intent(in) :: fold_z - integer :: i, j, k, s + integer :: i, j, k, s, kd k = blockIdx%x ! nz_l j = (blockIdx%y - 1)*blockDim%x + threadIdx%x ! ny_l if (j <= ny_l) then do s = 0, nproc - 1 + kd = s*nz_l + k + if (fold_z) kd = fold_destination(kd, nproc*nz_l) do i = 1, nx - dst(s*nz_l + k, i, j) = recvbuf(k, i, j, s + 1) + dst(kd, i, j) = recvbuf(k, i, j, s + 1) end do end do end if @@ -1427,26 +1443,30 @@ attributes(global) subroutine unpack_110_yslab( & end subroutine unpack_110_yslab attributes(global) subroutine pack_110_yslab( & - sendbuf, src, nx, ny_l, nz_l, nproc & + sendbuf, src, nx, ny_l, nz_l, nproc, fold_z & ) !! Inverse of unpack_110_yslab: packs the Y-slab real view into the !! MPI_Alltoall send buffer, selecting for each destination rank s the - !! Z range it originally owned in the physical Z-slab layout. + !! Z range it originally owned in the physical Z-slab layout, and + !! undoing the Z fold with fold_z. implicit none real(dp), device, intent(out), dimension(:, :, :, :) :: sendbuf ! (nz_l, nx, ny_l, nproc) real(dp), device, intent(in), dimension(:, :, :) :: src ! (2*(nz/2+1), nx, ny_l) integer, value, intent(in) :: nx, ny_l, nz_l, nproc + logical, value, intent(in) :: fold_z - integer :: i, j, k, s + integer :: i, j, k, s, ks k = blockIdx%x ! nz_l j = (blockIdx%y - 1)*blockDim%x + threadIdx%x ! ny_l if (j <= ny_l) then do s = 0, nproc - 1 + ks = s*nz_l + k + if (fold_z) ks = fold_destination(ks, nproc*nz_l) do i = 1, nx - sendbuf(k, i, j, s + 1) = src(s*nz_l + k, i, j) + sendbuf(k, i, j, s + 1) = src(ks, i, j) end do end do end if @@ -1685,4 +1705,22 @@ attributes(device) integer function fold_index(i, n) result(src) end function fold_index + attributes(device) integer function fold_destination(i, n) result(dst) + !! Where entry i of a non-periodic line of n points lands when the line + !! is gathered by fold_index: the inverse permutation. The odd entries + !! fill the first half in order, the even entries the second half in + !! reverse. Used where a kernel moves the whole line in one pass and + !! the fold rides along with it, as the Z fold of the 111 case does. + implicit none + + integer, value, intent(in) :: i, n + + if (mod(i, 2) == 1) then + dst = (i + 1)/2 + else + dst = n - i/2 + 1 + end if + + end function fold_destination + end module m_cuda_spectral diff --git a/src/backend/cuda/poisson_fft.f90 b/src/backend/cuda/poisson_fft.f90 index 1129d7b0c9..e90fa891b2 100644 --- a/src/backend/cuda/poisson_fft.f90 +++ b/src/backend/cuda/poisson_fft.f90 @@ -50,7 +50,7 @@ module m_cuda_poisson_fft type, abstract, extends(poisson_fft_t) :: cuda_poisson_fft_t !! FFT based Poisson solver, the state and the plain transform that !! every boundary condition case shares. Each case extends this type - !! with its own buffers, see cuda_poisson_000_t to cuda_poisson_110_t. + !! with its own buffers, see cuda_poisson_000_t to cuda_poisson_11x_t. !> Local domain sized array storing the spectral equivalence constants complex(dp), device, allocatable, dimension(:, :, :) :: waves_dev @@ -153,10 +153,20 @@ module m_cuda_poisson_fft procedure :: undo_periodicity => undo_periodicity_x_cuda end type cuda_poisson_100_t - type, extends(cuda_poisson_fft_t) :: cuda_poisson_110_t - !! Non-periodic in x and y, periodic in z + type, extends(cuda_poisson_fft_t) :: cuda_poisson_11x_t + !! Non-periodic in x and y, and in z either periodic (110) or not (111). + !! Like Incompact3d's poisson_11x, both cases share the (nz/2+1, nx, ny) + !! spectral layout and postprocess. 111 folds z as well, inside the + !! kernels that move the field into and out of the transform buffer + !! (the transpose on one rank, the Y-slab unpack and pack on several), + !! where z is whole, so the r2c transform stands in for a cosine + !! transform; waves_set and the half-angle az, bz of a non-periodic z do + !! the rest. !> Transposed real workspace (nz, nx, ny), single rank only real(dp), device, allocatable, dimension(:, :, :) :: r_dev_110 + !> Fold z on the way into the transform buffer, and unfold on the way + !> out (111) + logical :: fold_z = .false. contains procedure :: init_case => init_case_110 procedure :: create_plans => create_plans_110 @@ -167,7 +177,7 @@ module m_cuda_poisson_fft procedure :: undo_periodicity => undo_periodicity_xy_cuda procedure :: redistribute_110_to_yslab procedure :: redistribute_110_to_zslab - end type cuda_poisson_110_t + end type cuda_poisson_11x_t ! Explicit C interfaces for cuFFT functions that nvfortran has trouble with interface @@ -378,10 +388,10 @@ pure function cuda_fft_decomp_reason(bc_code, cdims, nproc_dir) & reason = '' nproc = product(nproc_dir) if ((bc_code == FFT_BC_010 .or. bc_code == FFT_BC_100 & - .or. bc_code == FFT_BC_110) & + .or. fft_bc_is_11x(bc_code)) & .and. nproc > 1 .and. (nproc_dir(1) /= 1 .or. nproc_dir(2) /= 1)) then - reason = 'The 010, 100 and 110 cases on multiple ranks need a 1D z & - &decomposition, nproc_dir must be [1, 1, nproc].' + reason = 'The 010, 100, 110 and 111 cases on multiple ranks need a 1D & + &z decomposition, nproc_dir must be [1, 1, nproc].' else if (bc_code == FFT_BC_010 .and. nproc > 1 & .and. mod(cdims(2), nproc_dir(3)) /= 0) then reason = 'The 010 case on multiple ranks needs the number of y & @@ -390,11 +400,11 @@ pure function cuda_fft_decomp_reason(bc_code, cdims, nproc_dir) & .and. mod(cdims(1), nproc_dir(3)) /= 0) then reason = 'The 100 case on multiple ranks needs the number of x & &cells to divide by the number of ranks.' - else if (bc_code == FFT_BC_110 .and. nproc > 1 & + else if (fft_bc_is_11x(bc_code) .and. nproc > 1 & .and. (mod(cdims(1), nproc_dir(3)) /= 0 & .or. mod(cdims(2), nproc_dir(3)) /= 0)) then - reason = 'The 110 case on multiple ranks needs the number of x & - &and y cells to divide by the number of ranks.' + reason = 'The 110 and 111 cases on multiple ranks need the number of & + &x and y cells to divide by the number of ranks.' end if end function cuda_fft_decomp_reason @@ -425,7 +435,7 @@ pure subroutine cuda_fft_spectral_dims(bc_code, dims_glob, dims_loc, & n_sp_st(2) = dims_loc(1)/nproc_dir(3)*nrank_dir(3) n_sp_st(3) = 0 - else if (bc_code == FFT_BC_110) then + else if (fft_bc_is_11x(bc_code)) then ! R2C Z-transpose: spectral array is (nz/2+1, nx, ny) on 1 rank. ! dim1 = Z R2C modes, dim2 = X spectrum, dim3 = Y full spectrum. ! On P ranks the cuFFTMp Y-slab transform shuffles the spectral @@ -459,8 +469,9 @@ end subroutine cuda_fft_spectral_dims pure subroutine cuda_fft_plan_setup(bc_code, dims_glob, dims_loc, fft_n, & fft_n_loc, fw_type, bw_type) !! The cuFFT plan dimensions and types of a BC class. The 100 case - !! transposes X<->Y and runs the FFT on (ny, nx, nz), the 110 case - !! transposes to (nz, nx, ny) with R2C along Z, and the 000 and 010 cases + !! transposes X<->Y and runs the FFT on (ny, nx, nz), the 110 and 111 + !! cases transpose to (nz, nx, ny) with R2C along Z, and the 000 and 010 + !! cases !! run it on (nx, ny, nz). fft_n holds the global plan dimensions and !! fft_n_loc the local extents of the first two. Shared by the solver !! init and the x3d2-memcheck probe. @@ -475,8 +486,8 @@ pure subroutine cuda_fft_plan_setup(bc_code, dims_glob, dims_loc, fft_n, & ! 100: transpose X<->Y, FFT on (ny, nx, nz) fft_n = [dims_glob(2), dims_glob(1), dims_glob(3)] fft_n_loc = [dims_loc(2), dims_loc(1)] - else if (bc_code == FFT_BC_110) then - ! 110: transpose to (nz, nx, ny), R2C along Z + else if (fft_bc_is_11x(bc_code)) then + ! 110, 111: transpose to (nz, nx, ny), R2C along Z fft_n = [dims_glob(3), dims_glob(1), dims_glob(2)] fft_n_loc = [dims_glob(3), dims_loc(1)] else @@ -513,8 +524,8 @@ subroutine create_cuda_poisson_fft(poisson_fft, mesh, xdirps, ydirps, & allocate (cuda_poisson_010_t :: poisson_fft) case ('100') allocate (cuda_poisson_100_t :: poisson_fft) - case ('110') - allocate (cuda_poisson_110_t :: poisson_fft) + case ('110', '111') + allocate (cuda_poisson_11x_t :: poisson_fft) case default call abort_run('Requested BCs are not supported in FFT-based Poisson & &solver!') @@ -638,10 +649,9 @@ subroutine init_cuda(self, mesh, xdirps, ydirps, zdirps, lowmem) ! cuFFT is a fallback taken inside it, so unlike the decomposition checks ! above this one cannot move ahead of the plans. The 010, 100 and 110 ! cases need cuFFTMp on more than one rank. - if ((self%bc_code == '010' .or. self%bc_code == '100' & - .or. self%bc_code == '110') & + if (self%bc_code /= '000' & .and. mesh%par%nproc > 1 .and. (.not. self%use_cufftmp)) then - call abort_run('The 010, 100 and 110 cases on multiple ranks need & + call abort_run('The 010, 100, 110 and 111 cases on multiple ranks need & &cuFFTMp, plain cuFFT cannot decompose the transform.') end if @@ -716,9 +726,20 @@ end subroutine init_case_100 subroutine init_case_110(self, mesh) implicit none - class(cuda_poisson_110_t) :: self + class(cuda_poisson_11x_t) :: self type(mesh_t), intent(in) :: mesh + self%fold_z = .not. self%periodic_z + + ! The Y-slab redistribution assumes every rank holds nz/nproc cells. + ! The mesh splits the vertices, so a non-periodic z (111) has that only + ! when its cell count divides by the number of ranks. + if (mesh%par%nproc > 1 & + .and. mod(self%nz_glob, mesh%par%nproc_dir(3)) /= 0) then + call abort_run('The 110 and 111 cases on multiple ranks need the & + &number of z cells to divide by the number of ranks.') + end if + call self%init_dim2_mirror(mesh) end subroutine init_case_110 @@ -794,7 +815,7 @@ subroutine create_plans_110(self, mesh, fft_n, fft_n_loc, fw_plan_type, & !! MPI_Alltoall (see redistribute_110_to_yslab). implicit none - class(cuda_poisson_110_t) :: self + class(cuda_poisson_11x_t) :: self type(mesh_t), intent(in) :: mesh integer, intent(in) :: fft_n(3), fft_n_loc(2) integer, intent(in) :: fw_plan_type, bw_plan_type @@ -855,7 +876,7 @@ subroutine yslab_view_110(self, d_dev) !! of the descriptor allocation. implicit none - class(cuda_poisson_110_t), intent(in) :: self + class(cuda_poisson_11x_t), intent(in) :: self real(dp), device, pointer, dimension(:, :, :), intent(out) :: d_dev type(cudaXtDesc), pointer :: descriptor @@ -869,10 +890,11 @@ subroutine yslab_view_110(self, d_dev) end subroutine yslab_view_110 subroutine fft_forward_110_cuda(self, f) - !! Forward FFT for 110 case: transpose (nx,ny,nz)->(nz,nx,ny) then R2C + !! Forward FFT for the 110 and 111 cases: transpose + !! (nx,ny,nz)->(nz,nx,ny), folding z in 111, then R2C implicit none - class(cuda_poisson_110_t) :: self + class(cuda_poisson_11x_t) :: self class(field_t), intent(in) :: f real(dp), device, pointer :: padded_dev(:, :, :), d_dev(:, :, :) @@ -901,7 +923,8 @@ subroutine fft_forward_110_cuda(self, f) threads = dim3(tpb, 1, 1) call transpose_xyz_to_zxy<<>>( & !& - self%r_dev_110, padded_dev, self%nx_loc, self%ny_loc, self%nz_loc & + self%r_dev_110, padded_dev, self%nx_loc, self%ny_loc, self%nz_loc, & + self%fold_z & ) ! R2C FFT on transposed data @@ -918,10 +941,11 @@ subroutine fft_forward_110_cuda(self, f) end subroutine fft_forward_110_cuda subroutine fft_backward_110_cuda(self, f) - !! Backward FFT for 110 case: C2R then transpose (nz,nx,ny)->(nx,ny,nz) + !! Backward FFT for the 110 and 111 cases: C2R then transpose + !! (nz,nx,ny)->(nx,ny,nz), unfolding z in 111 implicit none - class(cuda_poisson_110_t) :: self + class(cuda_poisson_11x_t) :: self class(field_t), intent(inout) :: f real(dp), device, pointer :: padded_dev(:, :, :), d_dev(:, :, :) @@ -965,7 +989,8 @@ subroutine fft_backward_110_cuda(self, f) threads = dim3(tpb, 1, 1) call transpose_zxy_to_xyz<<>>( & !& - padded_dev, self%r_dev_110, self%nx_loc, self%ny_loc, self%nz_loc & + padded_dev, self%r_dev_110, self%nx_loc, self%ny_loc, self%nz_loc, & + self%fold_z & ) end if end subroutine fft_backward_110_cuda @@ -1019,7 +1044,7 @@ subroutine redistribute_110_to_yslab(self, zslab, yslab) !! forward transform. See the comment above pack_zslab_to_alltoall. implicit none - class(cuda_poisson_110_t) :: self + class(cuda_poisson_11x_t) :: self real(dp), device, dimension(:, :, :), intent(in) :: zslab real(dp), device, dimension(:, :, :), intent(inout) :: yslab @@ -1040,7 +1065,7 @@ subroutine redistribute_110_to_yslab(self, zslab, yslab) call redistribution_alltoall(self, nz_l*nx*ny_l, 'Y') call unpack_110_yslab<<>>( & !& - yslab, self%r_a2a_recv_dev, nx, ny_l, nz_l, nproc & + yslab, self%r_a2a_recv_dev, nx, ny_l, nz_l, nproc, self%fold_z & ) end subroutine redistribute_110_to_yslab @@ -1049,7 +1074,7 @@ subroutine redistribute_110_to_zslab(self, yslab, zslab) !! Inverse of redistribute_110_to_yslab, after the backward transform. implicit none - class(cuda_poisson_110_t) :: self + class(cuda_poisson_11x_t) :: self real(dp), device, dimension(:, :, :), intent(in) :: yslab real(dp), device, dimension(:, :, :), intent(inout) :: zslab @@ -1064,7 +1089,7 @@ subroutine redistribute_110_to_zslab(self, yslab, zslab) call redistribution_launch_config(ny_l, nz_l, blocks, threads) call pack_110_yslab<<>>( & !& - self%r_a2a_send_dev, yslab, nx, ny_l, nz_l, nproc & + self%r_a2a_send_dev, yslab, nx, ny_l, nz_l, nproc, self%fold_z & ) call redistribution_alltoall(self, nz_l*nx*ny_l, 'Z') @@ -1781,7 +1806,7 @@ subroutine fft_postprocess_110_cuda(self) !! more launches. implicit none - class(cuda_poisson_110_t) :: self + class(cuda_poisson_11x_t) :: self type(cudaXtDesc), pointer :: descriptor complex(dp), device, dimension(:, :, :), pointer :: c_dev @@ -2009,7 +2034,7 @@ end subroutine undo_periodicity_y_cuda subroutine enforce_periodicity_xy_cuda(self, f_out, f_in) implicit none - class(cuda_poisson_110_t) :: self + class(cuda_poisson_11x_t) :: self class(field_t), intent(inout) :: f_out class(field_t), intent(in) :: f_in @@ -2041,7 +2066,7 @@ end subroutine enforce_periodicity_xy_cuda subroutine undo_periodicity_xy_cuda(self, f_out, f_in) implicit none - class(cuda_poisson_110_t) :: self + class(cuda_poisson_11x_t) :: self class(field_t), intent(inout) :: f_out class(field_t), intent(in) :: f_in diff --git a/src/backend/omp/backend.f90 b/src/backend/omp/backend.f90 index 5c944f5d34..c93adef076 100644 --- a/src/backend/omp/backend.f90 +++ b/src/backend/omp/backend.f90 @@ -5,7 +5,7 @@ module m_omp_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 + abl_wall_upper_plane, z_weight_line use m_common, only: dp, MPI_X3D2_DP, get_dirs_from_rdr, move_data_loc, & DIR_X, DIR_Y, DIR_Z, DIR_C, NULL_LOC, & X_FACE, Y_FACE, Z_FACE, VERT, BC_DIRICHLET @@ -1341,7 +1341,8 @@ subroutine slice_max_sum_omp(self, max_val, sum_val, f, i_slice, & end subroutine slice_max_sum_omp - subroutine x_slice_weighted_sum_omp(self, sum_val, f, i_slice, y_weights) + subroutine x_slice_weighted_sum_omp(self, sum_val, f, i_slice, y_weights, & + z_weights) !! [[m_base_backend(module):x_slice_weighted_sum_base(subroutine)]] implicit none class(omp_backend_t) :: self @@ -1349,8 +1350,10 @@ subroutine x_slice_weighted_sum_omp(self, sum_val, f, i_slice, y_weights) 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) :: sum_p + real(dp) :: sum_p, sum_z + real(dp), allocatable :: wz(:) integer :: dims(3), y_stride, i, k, y_block, z if (f%dir /= DIR_X) & @@ -1365,17 +1368,20 @@ subroutine x_slice_weighted_sum_omp(self, sum_val, f, i_slice, y_weights) call abort_run('x_slice_weighted_sum: i_slice out of range') if (size(y_weights) /= dims(2)) & call abort_run('x_slice_weighted_sum: one weight per y point needed') + wz = z_weight_line(z_weights, dims(3)) sum_p = 0._dp ! y-block is the fast-varying component of the group index - !$omp parallel do collapse(2) reduction(+:sum_p) private(k) + !$omp parallel do reduction(+:sum_p) private(k, sum_z) do z = 1, dims(3) + sum_z = 0._dp do y_block = 1, (dims(2) - 1)/SZ + 1 k = y_stride*(z - 1) + y_block do i = 1, min(SZ, dims(2) - (y_block - 1)*SZ) - sum_p = sum_p + y_weights(i + (y_block - 1)*SZ)*f%data(i, i_slice, k) + sum_z = sum_z + y_weights(i + (y_block - 1)*SZ)*f%data(i, i_slice, k) end do end do + sum_p = sum_p + wz(z)*sum_z end do !$omp end parallel do @@ -1468,7 +1474,20 @@ subroutine field_set_face_omp(self, f, c_start, c_end, face, & end do !$omp end parallel do case (Z_FACE) - call abort_run('Setting Z_FACE is not yet supported.') + ! Every y-block of the first and the last local z station + call self%z_face_owned(bc_start, bc_end, set_start, set_end) + n_y_blocks = (dims(2) - 1)/SZ + 1 + y_stride = y_group_stride(self) + k_end = (dims(3) - 1)*y_stride + !$omp parallel do collapse(2) private(i_mod) + do k = 1, n_y_blocks + do j = 1, dims(1) + i_mod = min(SZ, dims(2) - (k - 1)*SZ) ! lanes past ny are padding + if (set_start) f%data(1:i_mod, j, k) = c_start + if (set_end) f%data(1:i_mod, j, k_end + k) = c_end + end do + end do + !$omp end parallel do case default call abort_run('face is undefined.') end select diff --git a/src/backend/omp/poisson_fft.f90 b/src/backend/omp/poisson_fft.f90 index 4c9c745932..924b6e7048 100644 --- a/src/backend/omp/poisson_fft.f90 +++ b/src/backend/omp/poisson_fft.f90 @@ -25,7 +25,7 @@ module m_omp_poisson_fft type, abstract, extends(poisson_fft_t) :: omp_poisson_fft_t !! FFT based Poisson solver, the state and the plain transform that !! every boundary condition case shares. Each case extends this type - !! with its own buffers, see omp_poisson_000_t to omp_poisson_110_t. + !! with its own buffers, see omp_poisson_000_t to omp_poisson_11x_t. complex(dp), allocatable, dimension(:, :, :) :: c_x !> 2decomp processor grid. p_row splits y in the x-pencil, p_col splits z. integer :: p_row = 1, p_col = 1 @@ -97,8 +97,16 @@ module m_omp_poisson_fft procedure :: undo_periodicity => undo_periodicity_x_omp end type omp_poisson_100_t - type, extends(omp_poisson_fft_t) :: omp_poisson_110_t - !! Non-periodic in x and y, periodic in z + type, extends(omp_poisson_fft_t) :: omp_poisson_11x_t + !! Non-periodic in x and y, and in z either periodic (110) or not (111). + !! Like Incompact3d's poisson_11x, both cases share the (nz/2+1, nx, ny) + !! spectral layout and postprocess. 111 folds z as well, inside the + !! copies into and out of the transform buffer where z is whole, so the + !! r2c transform stands in for a cosine transform; waves_set and the + !! half-angle az, bz of a non-periodic z do the rest. + !> Position of each z entry in r_tr: the fold for 111, the identity for + !! 110. + integer, allocatable :: z_dst(:) !> Physical space decomposition owned by the FFT, whose ph%xsz is the !! (nz, nx_loc, ny_loc) layout the transform reads. type(decomp_info), pointer :: ph => null() @@ -121,7 +129,7 @@ module m_omp_poisson_fft procedure :: fft_postprocess => fft_postprocess_110_omp procedure :: enforce_periodicity => enforce_periodicity_xy_omp procedure :: undo_periodicity => undo_periodicity_xy_omp - end type omp_poisson_110_t + end type omp_poisson_11x_t private :: init_omp, init_buffers, init_buffers_010, init_buffers_100, & init_buffers_110, fold_index, fold_destinations @@ -146,8 +154,8 @@ subroutine create_omp_poisson_fft(poisson_fft, mesh, xdirps, ydirps, & allocate (omp_poisson_010_t :: poisson_fft) case ('100') allocate (omp_poisson_100_t :: poisson_fft) - case ('110') - allocate (omp_poisson_110_t :: poisson_fft) + case ('110', '111') + allocate (omp_poisson_11x_t :: poisson_fft) case default call abort_run('Requested BCs are not supported in FFT-based Poisson & &solver!') @@ -284,15 +292,23 @@ end subroutine init_buffers_100 subroutine init_buffers_110(self, mesh, dims) implicit none - class(omp_poisson_110_t) :: self + class(omp_poisson_11x_t) :: self type(mesh_t), intent(in) :: mesh integer, intent(in) :: dims(3) - ! ph%xsz is exactly (nz, nx_loc, ny_loc) for the 110 fft plan, on one + integer :: k + + ! ph%xsz is exactly (nz, nx_loc, ny_loc) for the 11x fft plan, on one ! rank as much as many. self%ph => decomp_2d_fft_get_ph() allocate (self%r_tr(self%ph%xsz(1), self%ph%xsz(2), self%ph%xsz(3))) + if (self%periodic_z) then + self%z_dst = [(k, k=1, self%nz_glob)] + else + self%z_dst = fold_destinations(self%nz_glob) + end if + if (mesh%par%nproc > 1) then ! Cell-dims decomposition of the solver field (x whole, y split by ! p_row, z split by p_col). decomp_main cannot serve here because @@ -301,20 +317,21 @@ subroutine init_buffers_110(self, mesh, dims) call decomp_info_init(dims(1), dims(2), dims(3), self%dc) if (.not. all(self%dc%zsz == [self%ph%xsz(2), self%ph%xsz(3), & self%ph%xsz(1)])) then - call abort_run('The 110 case cell-dims decomposition does not match & + call abort_run('The 11x case cell-dims decomposition does not match & &the FFT physical decomposition.') end if ! The mesh splits the vertices, and the last rank of a non-periodic ! direction has one cell fewer than vertices, while dc splits the - ! cells directly. The two can hand a rank different y ranges (257 - ! cells on 2 ranks are 129 + 128 for the mesh but 128 + 129 for dc), - ! and the copies between the solver field and r_xp assume they - ! agree. A cell count divisible by p_row always agrees. + ! cells directly. The two can hand a rank different y (or, in 111, z) + ! ranges (257 cells on 2 ranks are 129 + 128 for the mesh but + ! 128 + 129 for dc), and the copies between the solver field and r_xp + ! assume they agree. A cell count divisible by the ranks along that + ! direction always agrees. if (.not. all(self%dc%xsz == [self%nx_loc, self%ny_loc, & self%nz_loc])) then - call abort_run('The OpenMP 110 Poisson solver needs the y cell & - &count to be divisible by the number of ranks & - &along y, or split along z instead.') + call abort_run('The OpenMP 11x Poisson solver needs the y and z & + &cell counts to be divisible by the number of ranks & + &along them.') end if allocate (self%r_xp(self%dc%xsz(1), self%dc%xsz(2), self%dc%xsz(3))) @@ -403,8 +420,9 @@ end subroutine fft_forward_100_omp subroutine fft_forward_110_omp(self, f_in) !! Forward FFT for the non-periodic x and y case. Transposes the !! physical field (nx, ny, nz) to (nz, nx, ny) so that the r2c - !! transform runs along the periodic z direction, then transforms - !! with the plan set up in init. + !! transform runs along z, then transforms with the plan set up in + !! init. The transpose places z through z_dst, which folds a + !! non-periodic z (111) at no extra cost. !! !! On multiple ranks the solver field's x-pencil is redistributed !! through the cell-dims decomposition dc to reach the FFT's x-pencil. @@ -413,7 +431,7 @@ subroutine fft_forward_110_omp(self, f_in) !! y-pencil of dc where the whole y range is local. implicit none - class(omp_poisson_110_t) :: self + class(omp_poisson_11x_t) :: self class(field_t), intent(in) :: f_in integer :: i, j, k @@ -424,7 +442,7 @@ subroutine fft_forward_110_omp(self, f_in) do k = 1, self%nz_loc do j = 1, self%ny_loc do i = 1, self%nx_loc - self%r_tr(k, i, j) = f_in%data(i, j, k) + self%r_tr(self%z_dst(k), i, j) = f_in%data(i, j, k) end do end do end do @@ -465,7 +483,7 @@ subroutine fft_forward_110_omp(self, f_in) do k = 1, self%dc%zsz(3) do j = 1, self%dc%zsz(2) do i = 1, self%dc%zsz(1) - self%r_tr(k, i, j) = self%r_zp(i, j, k) + self%r_tr(self%z_dst(k), i, j) = self%r_zp(i, j, k) end do end do end do @@ -531,7 +549,7 @@ end subroutine fft_backward_100_omp subroutine fft_backward_110_omp(self, f_out) !! Backward FFT for the non-periodic x and y case, undoing the - !! transpose applied by fft_forward_110_omp. Only the unpadded + !! transpose (and z fold) applied by fft_forward_110_omp. Only the unpadded !! extents are written, which is all undo_periodicity_xy_omp goes on !! to read. !! @@ -540,7 +558,7 @@ subroutine fft_backward_110_omp(self, f_out) !! redistributing back to the solver field's x-pencil. implicit none - class(omp_poisson_110_t) :: self + class(omp_poisson_11x_t) :: self class(field_t), intent(inout) :: f_out integer :: i, j, k @@ -553,7 +571,7 @@ subroutine fft_backward_110_omp(self, f_out) do k = 1, self%nz_loc do j = 1, self%ny_loc do i = 1, self%nx_loc - f_out%data(i, j, k) = self%r_tr(k, i, j) + f_out%data(i, j, k) = self%r_tr(self%z_dst(k), i, j) end do end do end do @@ -563,7 +581,7 @@ subroutine fft_backward_110_omp(self, f_out) do k = 1, self%dc%zsz(3) do j = 1, self%dc%zsz(2) do i = 1, self%dc%zsz(1) - self%r_zp(i, j, k) = self%r_tr(k, i, j) + self%r_zp(i, j, k) = self%r_tr(self%z_dst(k), i, j) end do end do end do @@ -704,7 +722,7 @@ subroutine fft_postprocess_110_omp(self) !! where dim3 (Y modes) is always whole. implicit none - class(omp_poisson_110_t) :: self + class(omp_poisson_11x_t) :: self if (self%p_col == 1) then call process_spectral_110( & @@ -870,7 +888,7 @@ subroutine enforce_periodicity_xy_omp(self, f_out, f_in) !! range is local. implicit none - class(omp_poisson_110_t) :: self + class(omp_poisson_11x_t) :: self class(field_t), intent(inout) :: f_out class(field_t), intent(in) :: f_in @@ -924,7 +942,7 @@ subroutine undo_periodicity_xy_omp(self, f_out, f_in) !! still local, in the y-pencil. implicit none - class(omp_poisson_110_t) :: self + class(omp_poisson_11x_t) :: self class(field_t), intent(inout) :: f_out class(field_t), intent(in) :: f_in diff --git a/src/backend/omptgt/backend.f90 b/src/backend/omptgt/backend.f90 index 4c1483f811..a0cb61083e 100644 --- a/src/backend/omptgt/backend.f90 +++ b/src/backend/omptgt/backend.f90 @@ -24,7 +24,8 @@ module m_omptgt_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, MPI_X3D2_DP, move_data_loc, get_dirs_from_rdr, & DIR_X, DIR_Y, DIR_Z, DIR_C, VERT, NULL_LOC, & X_FACE, Y_FACE, Z_FACE, BC_DIRICHLET @@ -54,6 +55,7 @@ module m_omptgt_backend volume_integral, slice_max_sum, & x_slice_weighted_sum, & field_set_x_face, field_set_y_face, & + field_set_z_face, & field_set_y_plane, & field_set_abl_wall_stress, & field_get_x_band, & @@ -1187,7 +1189,7 @@ subroutine slice_max_sum_omptgt(self, max_val, sum_val, f, i_slice, & end subroutine slice_max_sum_omptgt subroutine x_slice_weighted_sum_omptgt(self, sum_val, f, i_slice, & - y_weights) + y_weights, z_weights) !! [[m_base_backend(module):x_slice_weighted_sum_base(subroutine)]] implicit none @@ -1196,6 +1198,7 @@ subroutine x_slice_weighted_sum_omptgt(self, sum_val, f, i_slice, & 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), pointer, contiguous, dimension(:, :, :) :: f_d integer :: n, n_i, n_groups_pad, n_j @@ -1213,7 +1216,8 @@ subroutine x_slice_weighted_sum_omptgt(self, sum_val, f, i_slice, & ! Rank-local value; caller is responsible for MPI_Allreduce. call resolve_field_t(f_d, f) - call x_slice_weighted_sum(sum_val, f_d, y_weights, size(f_d, 2), & + call x_slice_weighted_sum(sum_val, f_d, y_weights, & + z_weight_line(z_weights, n_i), size(f_d, 2), & i_slice, n_i, n_groups_pad, n_j) end subroutine x_slice_weighted_sum_omptgt @@ -1301,6 +1305,7 @@ subroutine field_set_face_omptgt(self, f, c_start, c_end, face, & integer :: dims(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 & @@ -1333,7 +1338,11 @@ subroutine field_set_face_omptgt(self, f, c_start, c_end, face, & bc_e == BC_DIRICHLET, size(f_d, 2), & dims(1), dims(2), dims(3), n_yb_pad(self)) 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) + call field_set_z_face(f_d, c_start, c_end, set_start, set_end, & + size(f_d, 2), dims(1), dims(2), dims(3), & + n_yb_pad(self)) case default call abort_run('face is undefined.') end select diff --git a/src/backend/omptgt/kernels/fieldops.f90 b/src/backend/omptgt/kernels/fieldops.f90 index 2c909b4f3c..359dd64025 100644 --- a/src/backend/omptgt/kernels/fieldops.f90 +++ b/src/backend/omptgt/kernels/fieldops.f90 @@ -524,25 +524,25 @@ subroutine slice_max_sum(max_f, sum_f, f, n_f, i_slice, n_i, & end subroutine slice_max_sum - subroutine x_slice_weighted_sum(sum_f, f, w, n_f, i_slice, n_i, & + subroutine x_slice_weighted_sum(sum_f, f, w, wz, n_f, i_slice, n_i, & n_groups_pad, n_j) - !! Sum of the slice f(:, i_slice, :) with the weight w(j) on the points - !! of stacked index j. + !! Sum of the slice f(:, i_slice, :) with the weight w(j)*wz(k) on the + !! point of stacked index j and repeated index k. real(dp), intent(out) :: sum_f integer, intent(in) :: n_f, i_slice, n_i, n_groups_pad, n_j - real(dp), intent(in) :: f(SZ, n_f, n_groups_pad*n_i), w(n_j) + real(dp), intent(in) :: f(SZ, n_f, n_groups_pad*n_i), w(n_j), wz(n_i) integer :: i, b_i, b_j sum_f = 0._dp !$omp target teams distribute parallel do collapse(3) & - !$omp reduction(+:sum_f) map(tofrom:sum_f) map(to:w) & + !$omp reduction(+:sum_f) map(tofrom:sum_f) map(to:w, wz) & !$omp OMP_DEVICE_ADDR(f) do b_j = 1, (n_j - 1)/SZ + 1 do b_i = 1, n_i do i = 1, SZ if (i + (b_j - 1)*SZ <= n_j) then - sum_f = sum_f + w(i + (b_j - 1)*SZ) & + sum_f = sum_f + w(i + (b_j - 1)*SZ)*wz(b_i) & *f(i, i_slice, b_j + (b_i - 1)*n_groups_pad) end if end do @@ -729,6 +729,32 @@ subroutine field_set_y_face(f, c_start, c_end, set_start, set_end, n_f, & end subroutine field_set_y_face + subroutine field_set_z_face(f, c_start, c_end, set_start, set_end, n_f, & + nx, ny, nz, n_yb_pad) + !! Sets the bottom (z = 1) and top (z = nz) faces where flagged. + integer, intent(in) :: n_f, nx, ny, nz, n_yb_pad + real(dp), intent(inout) :: f(SZ, n_f, n_yb_pad*nz) + real(dp), intent(in) :: c_start, c_end + logical, intent(in) :: set_start, set_end + + integer :: i, j, y_block + + !$omp target teams distribute parallel do collapse(3) OMP_DEVICE_ADDR(f) + do y_block = 1, (ny - 1)/SZ + 1 + do j = 1, nx + do i = 1, SZ + ! Lanes past ny in the last y-block are padding + if (i + (y_block - 1)*SZ <= ny) then + if (set_start) f(i, j, y_block) = c_start + if (set_end) f(i, j, y_block + (nz - 1)*n_yb_pad) = c_end + end if + end do + end do + end do + !$omp end target teams distribute parallel do + + end subroutine field_set_z_face + subroutine field_set_y_face_from_field(f, f_start, n_f, nx, ny, nz, & n_yb_pad) !! Copies the bottom and top y planes of f_start onto those of f. diff --git a/src/case/base_case.f90 b/src/case/base_case.f90 index 8a4199911c..ab1b93e9fe 100644 --- a/src/case/base_case.f90 +++ b/src/case/base_case.f90 @@ -220,16 +220,17 @@ subroutine compute_outflow_params(self, out_vel, flow_rate_diff) flow_rate_diff = flow_rate_in - flow_rate_out end subroutine compute_outflow_params - function outflow_flow_rate_correction(self, u, out_vel, y_weights) & - result(flow_rate_diff) + function outflow_flow_rate_correction(self, u, out_vel, y_weights, & + z_weights) result(flow_rate_diff) !! The flow_rate_diff for the X_FACE stamp of field_set_face_from_field !! that makes the flow rate out equal the flow rate in exactly, as the !! pressure projection sees them. The x staggered derivative sums to !! (u(nx) - u(1))/dx over the cells, so only the two face planes enter, - !! each weighted over y by y_weights: the case passes the weights of the - !! y interpolation (1/2 on a non-periodic boundary plane, 1 elsewhere), - !! with 0 on any plane it overwrites with the same value on both faces - !! after the stamp. The stamp is linear in u, + !! each weighted over y by y_weights and over z by z_weights (1 when + !! absent): the case passes the weights of the y and z interpolations + !! (1/2 on a non-periodic boundary plane, 1 elsewhere), with 0 on any + !! plane it overwrites with the same value on both faces after the + !! stamp. The stamp is linear in u, !! u(nx) <- (1 - c)*u(nx) + c*u(nx - 1) + flow_rate_diff, so the sums of !! the planes before it give the outlet flow rate after it. Call it with !! u as it is about to be stamped, once bc_start_u_x holds this substep's @@ -240,6 +241,7 @@ function outflow_flow_rate_correction(self, u, out_vel, y_weights) & class(field_t), intent(in) :: u real(dp), intent(in) :: out_vel real(dp), intent(in) :: y_weights(:) + real(dp), optional, intent(in) :: z_weights(:) real(dp) :: flow_rate_diff integer :: dims(3), nx, ierr @@ -248,13 +250,18 @@ function outflow_flow_rate_correction(self, u, out_vel, y_weights) & dims = self%solver%mesh%get_dims(VERT) nx = dims(1) call self%solver%backend%x_slice_weighted_sum( & - sums(1), self%bc_start_u_x, 1, y_weights) - call self%solver%backend%x_slice_weighted_sum(sums(2), u, nx, y_weights) + sums(1), self%bc_start_u_x, 1, y_weights, z_weights) call self%solver%backend%x_slice_weighted_sum( & - sums(3), u, nx - 1, y_weights) + sums(2), u, nx, y_weights, z_weights) + call self%solver%backend%x_slice_weighted_sum( & + sums(3), u, nx - 1, y_weights, z_weights) ! The correction is added to every point of the outlet plane, so it ! moves the weighted flow rate by the total weight of the plane - sums(4) = sum(y_weights)*real(dims(3), dp) + if (present(z_weights)) then + sums(4) = sum(y_weights)*sum(z_weights) + else + sums(4) = sum(y_weights)*real(dims(3), dp) + end if call MPI_Allreduce(MPI_IN_PLACE, sums, 4, MPI_X3D2_DP, MPI_SUM, & MPI_COMM_WORLD, ierr) diff --git a/src/case/wind_turbine.f90 b/src/case/wind_turbine.f90 index 96c76e8031..978c54f776 100644 --- a/src/case/wind_turbine.f90 +++ b/src/case/wind_turbine.f90 @@ -1,16 +1,14 @@ module m_case_wind_turbine !! Case for wind-turbine simulations (the `two_turbines` setup): - !! uniform inflow and convective outflow in x, periodic in y and z. + !! uniform inflow and convective outflow in x; y and z each either periodic + !! or bounded by free-slip walls (BC = 'neumann', 'neumann', Incompact3d + !! ncl = 1). Incompact3d's two_turbines example has walls in y and z. !! !! The case owns a polymorphic turbine forcing model (turbine_model_t), set up !! by m_turbine_setup from the `iturbine` switch. It drives the model from its !! forcings()/postprocess() hooks. !! - !! The inflow/outflow boundary treatment mirrors m_case_cylinder. BCs are - !! dirichlet/periodic/periodic for the same reason cylinder uses them: the FFT - !! Poisson solver requires z periodic and only supports a single rank for - !! non-periodic directions. The two_turbines case uses free-slip walls - !! in y and z (wind-tunnel walls), which the FFT solver cannot currently do. + !! The inflow/outflow boundary treatment mirrors m_case_cylinder. use m_mpi, only: MPI_COMM_WORLD, MPI_IN_PLACE, MPI_SUM, & MPI_Allreduce @@ -18,7 +16,8 @@ module m_case_wind_turbine use m_base_backend, only: base_backend_t use m_base_case, only: base_case_t use m_common, only: dp, MPI_X3D2_DP, get_argument, DIR_C, DIR_X, & - VERT, X_FACE, BC_DIRICHLET + VERT, X_FACE, Y_FACE, Z_FACE, BC_DIRICHLET, & + BC_NEUMANN, BC_PERIODIC use m_config, only: wind_turbine_config_t use m_field, only: field_t use m_mesh, only: mesh_t @@ -32,6 +31,11 @@ module m_case_wind_turbine type, extends(base_case_t) :: case_wind_turbine_t type(wind_turbine_config_t) :: wt_cfg class(turbine_model_t), pointer :: turbine => null() + !> Free-slip walls in y and in z + logical :: y_walls = .false., z_walls = .false. + !> Weights of the local y and z points in the outlet flow rate, see + !> wall_weights + real(dp), allocatable :: flow_rate_y_weights(:), flow_rate_z_weights(:) contains procedure :: define_BC => define_BC_wind_turbine procedure :: initial_conditions => initial_conditions_wind_turbine @@ -67,6 +71,17 @@ function case_wind_turbine_init(backend, mesh, host_allocator) & call abort_run('wind_turbine requires BC_x = ''dirichlet'', & &''dirichlet''.', collective=.true.) + ! Free-slip walls (Incompact3d ncl = 1) or periodic, in y and in z + flow_case%y_walls = walls_or_periodic(mesh, 2, 'BC_y') + flow_case%z_walls = walls_or_periodic(mesh, 3, 'BC_z') + ! field_set_face stamps the local y planes, so the y walls must be the + ! planes of every rank + if (flow_case%y_walls .and. mesh%par%nproc_dir(2) /= 1) & + call abort_run('wind_turbine: y walls need y not decomposed & + &(nproc_dir(2) = 1).', collective=.true.) + flow_case%flow_rate_y_weights = wall_weights(mesh, 2, flow_case%y_walls) + flow_case%flow_rate_z_weights = wall_weights(mesh, 3, flow_case%z_walls) + call flow_case%wt_cfg%read(nml_file=get_argument(1)) call flow_case%case_init(backend, mesh, host_allocator) @@ -82,6 +97,42 @@ function case_wind_turbine_init(backend, mesh, host_allocator) & flow_case%solver, flow_case%io_mgr) end function case_wind_turbine_init + logical function walls_or_periodic(mesh, dir, name) result(walls) + !! Whether direction dir has free-slip walls ('neumann' at both ends), + !! stopping the run unless it is that or periodic at both ends. + type(mesh_t), intent(in) :: mesh + integer, intent(in) :: dir + character(len=*), intent(in) :: name + + walls = all(mesh%grid%BCs_global(dir, :) == BC_NEUMANN) + if (.not. (walls .or. all(mesh%grid%BCs_global(dir, :) == BC_PERIODIC))) & + call abort_run('wind_turbine requires '//name//' = ''periodic'', & + &''periodic'' or ''neumann'', ''neumann''.', & + collective=.true.) + end function walls_or_periodic + + function wall_weights(mesh, dir, walls) result(w) + !! Weights of the local vertices of direction dir in the outlet flow + !! rate, as the discrete divergence sees it: its interpolation along a + !! non-periodic direction gives the global boundary planes half the + !! weight of the others (trapezoid). u is not stamped on a free-slip + !! wall, so the wall planes keep their 1/2. + type(mesh_t), intent(in) :: mesh + integer, intent(in) :: dir + logical, intent(in) :: walls + real(dp), allocatable :: w(:) + + integer :: dims(3) + + dims = mesh%get_dims(VERT) + allocate (w(dims(dir)), source=1._dp) + if (walls) then + if (mesh%par%nrank_dir(dir) == 0) w(1) = 0.5_dp + if (mesh%par%nrank_dir(dir) == mesh%par%nproc_dir(dir) - 1) & + w(dims(dir)) = 0.5_dp + end if + end function wall_weights + subroutine seed_rng(nrank, nproc, iter) !! Deterministic seeding of the intrinsic RNG, distinct for every !! (rank, start iteration) pair. A fresh run starts at iter = 0. @@ -203,44 +254,38 @@ subroutine define_BC_wind_turbine(self) end subroutine define_BC_wind_turbine subroutine apply_BC_wind_turbine(self, u, v, w) - !! Pre-correction (called per substep after the integrator step): - !! enforce the inflow Dirichlet and convective outflow BCs, then apply - !! an exact uniform mass correction to the outlet, following - !! Incompact3d's pre_correc. - !! - !! Steps: + !! Pre-correction (called per substep after the integrator step), + !! following Incompact3d's pre_correc: !! 1. cx = uxmax*gdt/dx from the post-integration field. - !! 2. Convective outflow at x=nx and Dirichlet inflow at x=1 for u,v,w - !! (no correction yet): u(nx) = u(nx) - cx*(u(nx) - u(nx-1)). - !! 3. Measure the inlet mean (from the prescribed Dirichlet plane) and - !! the outlet mean AFTER the convective update, then shift the outlet - !! plane of u uniformly so mean(u_outlet) = mean(u_inlet) exactly - !! (Incompact3d: bxxn = bxxn - ut + ut1). Applied to u only. + !! 2. Dirichlet inflow at x=1 and convective outflow at x=nx for u,v,w: + !! u(nx) = u(nx) - cx*(u(nx) - u(nx-1)), with a uniform shift of u on + !! the outlet that makes the flow rate out equal the flow rate in. + !! Incompact3d balances the plain means over the planes; here the + !! planes are weighted as the discrete divergence sees them (see + !! wall_weights), the same thing for periodic y and z, and exact with + !! walls, where the plain mean leaves a uniform divergence the + !! Neumann-x projection cannot remove. + !! 3. Free-slip walls: v = 0 on the y walls and w = 0 on the z walls. + !! The tangential components take the even-symmetry closure, and the + !! Neumann pressure BC gives no correction on the wall planes. implicit none class(case_wind_turbine_t) :: self class(field_t), intent(inout) :: u, v, w - integer :: dims(3), nx, ierr - real(dp) :: out_vel, ny_nz, udif, max_discard, flow_rate_diff_discard - real(dp) :: fl_sums(2) - integer :: gdims(3) + real(dp) :: out_vel, flow_rate_diff - dims = self%solver%mesh%get_dims(VERT) - nx = dims(1) - ! Use global y-z plane size so the mass-flux average is correct - ! regardless of MPI decomposition in y or z. - gdims = self%solver%mesh%get_global_dims(VERT) - ny_nz = real(gdims(2)*gdims(3), dp) - - ! Convective speed from the already-integrated velocity field. The - ! flow-rate imbalance is not used: the mass correction below measures the - ! outlet after the convective update instead. - call self%compute_outflow_params(out_vel, flow_rate_diff_discard) - - ! Dirichlet inflow + convective outflow (no mass correction yet). + ! Convective speed from the already-integrated velocity field + call self%compute_outflow_params(out_vel) + + flow_rate_diff = self%outflow_flow_rate_correction( & + u, out_vel, self%flow_rate_y_weights, & + self%flow_rate_z_weights) call self%solver%backend%field_set_face_from_field( & u, self%bc_start_u_x, out_vel, X_FACE, & - bc_start=BC_DIRICHLET, bc_end=BC_DIRICHLET) + bc_start=BC_DIRICHLET, bc_end=BC_DIRICHLET, & + flow_rate_diff=flow_rate_diff) + ! The flow-rate correction balances the streamwise flux, so only u + ! takes it call self%solver%backend%field_set_face_from_field( & v, self%bc_start_v_x, out_vel, X_FACE, & bc_start=BC_DIRICHLET, bc_end=BC_DIRICHLET) @@ -248,18 +293,12 @@ subroutine apply_BC_wind_turbine(self, u, v, w) w, self%bc_start_w_x, out_vel, X_FACE, & bc_start=BC_DIRICHLET, bc_end=BC_DIRICHLET) - ! Exact mass conservation (Incompact3d bxxn = bxxn - ut + ut1): - ! ut1 = mean of the prescribed inlet plane (bc_start_u_x at x=1), - ! ut = mean of the outlet plane AFTER the convective update (u at x=nx). - call self%solver%backend%slice_max_sum( & - max_discard, fl_sums(1), self%bc_start_u_x, 1) - call self%solver%backend%slice_max_sum( & - max_discard, fl_sums(2), u, nx) - call MPI_Allreduce(MPI_IN_PLACE, fl_sums, 2, MPI_X3D2_DP, MPI_SUM, & - MPI_COMM_WORLD, ierr) - udif = (fl_sums(1) - fl_sums(2))/ny_nz - - call self%solver%backend%field_add_const_x_face(u, udif, at_end=.true.) + ! Impermeable walls, stamped last so they win on the edges where they + ! meet the x faces, as in Incompact3d + if (self%y_walls) & + call self%solver%backend%field_set_face(v, 0._dp, 0._dp, Y_FACE) + if (self%z_walls) & + call self%solver%backend%field_set_face(w, 0._dp, 0._dp, Z_FACE) end subroutine apply_BC_wind_turbine ! Forcings: advance the turbine model and accumulate its momentum source. diff --git a/src/memcheck/context.f90 b/src/memcheck/context.f90 index 8c28727b0d..96b51f4817 100644 --- a/src/memcheck/context.f90 +++ b/src/memcheck/context.f90 @@ -5,7 +5,7 @@ module m_memcheck_context checkpoint_config_t use m_logger, only: abort_run use m_mesh, only: classify_boundaries, global_cell_dims, FFT_BC_000, & - FFT_BC_010, FFT_BC_100, FFT_BC_110 + FFT_BC_010, FFT_BC_100, fft_bc_is_11x use m_cuda_memcheck_device, only: cuda_memcheck_device_t implicit none @@ -141,7 +141,8 @@ subroutine classify_bc(ctx) ctx%bc_is_000 = ctx%bc_code == FFT_BC_000 ctx%bc_is_010 = ctx%bc_code == FFT_BC_010 ctx%bc_is_100 = ctx%bc_code == FFT_BC_100 - ctx%bc_is_110 = ctx%bc_code == FFT_BC_110 + ! 111 runs the 110 solver with a z fold, on the same buffers and plans + ctx%bc_is_110 = fft_bc_is_11x(ctx%bc_code) ctx%cdims = global_cell_dims(ctx%gdims, periodic) end subroutine classify_bc diff --git a/src/mesh.f90 b/src/mesh.f90 index f9014702e8..2404399d1d 100644 --- a/src/mesh.f90 +++ b/src/mesh.f90 @@ -17,6 +17,7 @@ module m_mesh integer, parameter :: FFT_BC_010 = 10 integer, parameter :: FFT_BC_100 = 100 integer, parameter :: FFT_BC_110 = 110 + integer, parameter :: FFT_BC_111 = 111 integer, parameter :: FFT_BC_UNSUPPORTED = -1 ! The mesh class stores all the information about the global and local (due to domain decomposition) mesh @@ -151,9 +152,6 @@ subroutine decomposition_generic(grid, par) nproc_y = par%nproc_dir(2) nproc_z = par%nproc_dir(3) - ! Define number of cells and vertices in each direction - grid%vert_dims = grid%global_vert_dims/par%nproc_dir - ! A 3D array corresponding to each region in the global domain allocate (global_ranks(nproc_x, nproc_y, nproc_z)) @@ -162,12 +160,49 @@ subroutine decomposition_generic(grid, par) shape=[nproc_x, nproc_y, nproc_z]) call par%compute_rank_pos_from_global(global_ranks) - call grid%copy_vert2cell_dims(par) - par%n_offset(:) = grid%vert_dims(:)*par%nrank_dir(:) + ! Split each direction as 2decomp does, so that both decompositions + ! hand a rank the same range + do i = 1, 3 + call split_points(grid%global_vert_dims(i), par%nproc_dir(i), & + par%nrank_dir(i), grid%periodic_BC(i), & + grid%vert_dims(i), par%n_offset(i)) + end do + call grid%copy_vert2cell_dims(par) end subroutine + pure subroutine split_points(n_vert, nproc, nrank, periodic, n_loc, offset) + !! The vertices of rank nrank (0-based) when n_vert vertices are split + !! over nproc ranks, and their offset, as 2decomp splits them. The cells + !! are split as evenly as possible, the last mod(n_cell, nproc) ranks + !! taking one more, and a non-periodic direction (one cell fewer than + !! vertices) puts its extra vertex on the last rank. A non-periodic + !! direction of P*m cells therefore gives every rank m cells. + implicit none + + integer, intent(in) :: n_vert, nproc, nrank + logical, intent(in) :: periodic + integer, intent(out) :: n_loc, offset + + integer :: n_cell, n_base, n_short + + n_cell = n_vert + if (.not. periodic) n_cell = n_vert - 1 + + n_base = n_cell/nproc + n_short = nproc - mod(n_cell, nproc) ! ranks with n_base cells + if (nrank < n_short) then + n_loc = n_base + offset = nrank*n_base + else + n_loc = n_base + 1 + offset = n_short*n_base + (nrank - n_short)*(n_base + 1) + end if + if (.not. periodic .and. nrank == nproc - 1) n_loc = n_loc + 1 + + end subroutine split_points + pure function get_dims(self, data_loc) result(dims) !! Getter for local domain dimensions class(mesh_t), intent(in) :: self @@ -400,14 +435,28 @@ subroutine classify_boundaries(bc_x, bc_y, bc_z, periodic, bc_code, & if (present(bcs_global)) bcs_global = codes - ! The four classes the Poisson FFT solver supports, all periodic in z. + ! The classes the Poisson FFT solver supports: the four periodic in z, + ! and 111, non-periodic everywhere. bc_code = FFT_BC_UNSUPPORTED if (periodic(3)) then if (periodic(1) .and. periodic(2)) bc_code = FFT_BC_000 if (periodic(1) .and. (.not. periodic(2))) bc_code = FFT_BC_010 if ((.not. periodic(1)) .and. periodic(2)) bc_code = FFT_BC_100 if ((.not. periodic(1)) .and. (.not. periodic(2))) bc_code = FFT_BC_110 + else if (.not. any(periodic)) then + bc_code = FFT_BC_111 end if end subroutine classify_boundaries + pure logical function fft_bc_is_11x(bc_code) + !! Whether the FFT_BC_* class bc_code runs the 11x Poisson solver: 110 + !! and 111 share its spectral layout, plans and buffers, as in + !! Incompact3d's poisson_11x. + implicit none + + integer, intent(in) :: bc_code + + fft_bc_is_11x = bc_code == FFT_BC_110 .or. bc_code == FFT_BC_111 + end function fft_bc_is_11x + end module m_mesh diff --git a/src/poisson_fft.f90 b/src/poisson_fft.f90 index 4929d12027..f23f37adf8 100644 --- a/src/poisson_fft.f90 +++ b/src/poisson_fft.f90 @@ -2,13 +2,20 @@ module m_poisson_fft use m_common, only: dp, pi, CELL use m_field, only: field_t use m_mesh, only: mesh_t, geo_t, FFT_BC_000, FFT_BC_010, FFT_BC_100, & - FFT_BC_110, FFT_BC_UNSUPPORTED - use m_tdsops, only: dirps_t + FFT_BC_110, FFT_BC_111, FFT_BC_UNSUPPORTED + use m_tdsops, only: dirps_t, tdsops_t use m_logger, only: log_warn, abort_run implicit none - private :: fill_stretched_diagonals + private :: fill_stretched_diagonals, interp_transfer + + !> The 1D factors of waves_set along one spectral dimension, see + !! spectral_line. The _im factors differ from the _re ones only along a + !! non-periodic r2c direction. + type, private :: spectral_line_t + real(dp), allocatable, dimension(:) :: k2_re, k2_im, t_re, t_im + end type spectral_line_t !> Stretching matrix kinds of fill_stretched_diagonals integer, parameter, private :: STRETCH_MAT_BOTTOM = 1, STRETCH_MAT_ODD = 2, & @@ -52,8 +59,9 @@ module m_poisson_fft logical :: lowmem = .false. contains !! Each backend extends this type once per boundary condition case - !! (000, 010, 100, 110), and each case type supplies its own transforms, - !! spectral postprocess and periodicity reorder. + !! (000, 010, 100, and 11x for both 110 and 111), and each case type + !! supplies its own transforms, spectral postprocess and periodicity + !! reorder. procedure(fft_forward), deferred :: fft_forward procedure(fft_backward), deferred :: fft_backward procedure(fft_postprocess), deferred :: fft_postprocess @@ -63,6 +71,7 @@ module m_poisson_fft procedure :: solve_poisson procedure :: stretching_matrix procedure :: waves_set + procedure, private :: spectral_line procedure :: get_km procedure :: get_km_re procedure :: get_km_im @@ -137,6 +146,8 @@ pure function fft_bc_class(bc_code) result(fft_bc) fft_bc = FFT_BC_100 case ('110') fft_bc = FFT_BC_110 + case ('111') + fft_bc = FFT_BC_111 case default fft_bc = FFT_BC_UNSUPPORTED end select @@ -146,7 +157,9 @@ pure function spectral_dirs(bc_code) result(dirs) !! The physical direction held in each dimension of the spectral array. !! The r2c transform runs along dim1, so a case with x non-periodic !! permutes a periodic direction into dim1 ahead of the FFT: 100 swaps - !! x and y, 110 brings z to the front. + !! x and y, 110 brings z to the front. 111 has no periodic direction + !! and keeps the 110 layout, with z folded so that the r2c transform + !! stands in for a cosine transform (see waves_set). implicit none character(len=3), intent(in) :: bc_code @@ -155,7 +168,7 @@ pure function spectral_dirs(bc_code) result(dirs) select case (bc_code) case ('100') dirs = [2, 1, 3] - case ('110') + case ('110', '111') dirs = [3, 1, 2] case default dirs = [1, 2, 3] @@ -295,8 +308,8 @@ subroutine stretching_matrix(self, geo, xdirps, ydirps, zdirps, n_blk, & type(dirps_t), intent(in) :: xdirps, ydirps, zdirps integer, dimension(3), optional, intent(in) :: n_blk, blk_st - real(dp) :: temp, a0, a1 - integer :: i, j, k, ix, iz + real(dp) :: a0, a1 + integer :: i, k, ix, iz integer :: n(3), st(3) n = [self%nx_spec, self%ny_spec, self%nz_spec] @@ -308,35 +321,18 @@ subroutine stretching_matrix(self, geo, xdirps, ydirps, zdirps, n_blk, & allocate (self%trans_y_re(n(2)), self%trans_y_im(n(2))) allocate (self%trans_z_re(n(3)), self%trans_z_im(n(3))) - do i = 1, n(1) - temp = real(self%exs(i + st(1)), kind=dp)*geo%d(1) - self%trans_x_re(i) = 2*(xdirps%interpl_v2p%a*cos(temp*0.5_dp) & - + xdirps%interpl_v2p%b*cos(temp*1.5_dp) & - + xdirps%interpl_v2p%c*cos(temp*2.5_dp) & - + xdirps%interpl_v2p%d*cos(temp*3.5_dp)) & - /(1._dp + 2*xdirps%interpl_v2p%alpha*cos(temp)) - self%trans_x_im(i) = self%trans_x_re(i) - end do - - do j = 1, n(2) - temp = real(self%eys(j + st(2)), kind=dp)*geo%d(2) - self%trans_y_re(j) = 2*(ydirps%interpl_v2p%a*cos(temp*0.5_dp) & - + ydirps%interpl_v2p%b*cos(temp*1.5_dp) & - + ydirps%interpl_v2p%c*cos(temp*2.5_dp) & - + ydirps%interpl_v2p%d*cos(temp*3.5_dp)) & - /(1._dp + 2*ydirps%interpl_v2p%alpha*cos(temp)) - self%trans_y_im(j) = self%trans_y_re(j) - end do - - do k = 1, n(3) - temp = real(self%ezs(k + st(3)), kind=dp)*geo%d(3) - self%trans_z_re(k) = 2*(zdirps%interpl_v2p%a*cos(temp*0.5_dp) & - + zdirps%interpl_v2p%b*cos(temp*1.5_dp) & - + zdirps%interpl_v2p%c*cos(temp*2.5_dp) & - + zdirps%interpl_v2p%d*cos(temp*3.5_dp)) & - /(1._dp + 2*zdirps%interpl_v2p%alpha*cos(temp)) - self%trans_z_im(k) = self%trans_z_re(k) - end do + self%trans_x_re = interp_transfer( & + xdirps%interpl_v2p, & + real(self%exs(st(1) + 1:st(1) + n(1)), kind=dp)*geo%d(1)) + self%trans_y_re = interp_transfer( & + ydirps%interpl_v2p, & + real(self%eys(st(2) + 1:st(2) + n(2)), kind=dp)*geo%d(2)) + self%trans_z_re = interp_transfer( & + zdirps%interpl_v2p, & + real(self%ezs(st(3) + 1:st(3) + n(3)), kind=dp)*geo%d(3)) + self%trans_x_im = self%trans_x_re + self%trans_y_im = self%trans_y_re + self%trans_z_im = self%trans_z_re if (trim(geo%stretching(2)) == 'bottom') then self%stretched_y_sym = .false. @@ -553,18 +549,31 @@ subroutine fill_stretched_diagonals(self, a_re, a_im, matrix_kind, a0, a1, & end subroutine fill_stretched_diagonals subroutine waves_set(self, geo, xdirps, ydirps, zdirps) - !! Spectral equivalence constants - !! - !! Ref. JCP 228 (2009), 5989–6015, Sec 4 + !! Spectral equivalence constants: the modified wavenumber of the + !! discrete div(grad) on each spectral entry, + !! k2x*(ty*tz)**2 + k2y*(tx*tz)**2 + k2z*(tx*ty)**2, + !! where t is the transfer function of the staggered interpolation. The + !! expression is symmetric in the three directions, so it is built from + !! the 1D factors of each spectral dimension (spectral_line), whatever + !! direction spectral_dirs puts there. + !! + !! The real and imaginary parts differ only when the r2c direction (dim1) + !! is non-periodic, as in the 111 case. The r2c transform of the folded + !! line, with the half-shift twiddle of the postprocess, leaves the cosine + !! mode k in the real part of entry k and the mode N - k in its imaginary + !! part, so the imaginary part takes the factors of the mirrored mode. + !! Incompact3d's waves does the same for bcz == 1. + !! + !! Ref. JCP 228 (2009), 5989–6015, Sec 4 implicit none class(poisson_fft_t) :: self type(geo_t), intent(in) :: geo type(dirps_t), intent(in) :: xdirps, ydirps, zdirps - integer :: i, j, k, ix, iy, iz - real(dp) :: rlexs, rleys, rlezs, xtt, ytt, ztt, xt1, yt1, zt1 - complex(dp) :: xt2, yt2, zt2, xyzk + type(spectral_line_t) :: l1, l2, l3 + integer :: i, j, k, dirs(3) + real(dp) :: w_re, w_im call wave_numbers( & self%ax, self%bx, self%kx, self%exs, self%k2x, & @@ -584,131 +593,92 @@ subroutine waves_set(self, geo, xdirps, ydirps, zdirps) zdirps%stagder_v2p%a, zdirps%stagder_v2p%b, zdirps%stagder_v2p%alpha & ) - ! The spectral array is laid out as spectral_dirs gives, e.g. for 110 - ! it is (nz/2 + 1, nx, ny): dim1 indexes the z r2c modes, dim2 the x - ! modes and dim3 the y modes. Each layout keeps its own loop nest with - ! the mode numbers written out. Mapping the indices through - ! spectral_dirs in one shared loop stops the compiler from vectorising - ! the inner loop and hoisting the other two directions out of it, which - ! changes waves by a few ulp under -ffast-math. - select case (self%bc_code) - case ('110') - do k = 1, self%nz_spec - do j = 1, self%ny_spec - do i = 1, self%nx_spec - iz = i + self%sp_st(1) - ix = j + self%sp_st(2) - iy = k + self%sp_st(3) - - rlexs = real(self%exs(ix), kind=dp)*geo%d(1) - rleys = real(self%eys(iy), kind=dp)*geo%d(2) - rlezs = real(self%ezs(iz), kind=dp)*geo%d(3) - - xtt = 2*(xdirps%interpl_v2p%a*cos(rlexs*0.5_dp) & - + xdirps%interpl_v2p%b*cos(rlexs*1.5_dp) & - + xdirps%interpl_v2p%c*cos(rlexs*2.5_dp) & - + xdirps%interpl_v2p%d*cos(rlexs*3.5_dp)) - ytt = 2*(ydirps%interpl_v2p%a*cos(rleys*0.5_dp) & - + ydirps%interpl_v2p%b*cos(rleys*1.5_dp) & - + ydirps%interpl_v2p%c*cos(rleys*2.5_dp) & - + ydirps%interpl_v2p%d*cos(rleys*3.5_dp)) - ztt = 2*(zdirps%interpl_v2p%a*cos(rlezs*0.5_dp) & - + zdirps%interpl_v2p%b*cos(rlezs*1.5_dp) & - + zdirps%interpl_v2p%c*cos(rlezs*2.5_dp) & - + zdirps%interpl_v2p%d*cos(rlezs*3.5_dp)) - - xt1 = 1._dp + 2*xdirps%interpl_v2p%alpha*cos(rlexs) - yt1 = 1._dp + 2*ydirps%interpl_v2p%alpha*cos(rleys) - zt1 = 1._dp + 2*zdirps%interpl_v2p%alpha*cos(rlezs) - - xt2 = self%k2x(ix)*((ytt/yt1)*(ztt/zt1))**2 - yt2 = self%k2y(iy)*((xtt/xt1)*(ztt/zt1))**2 - zt2 = self%k2z(iz)*((xtt/xt1)*(ytt/yt1))**2 - - xyzk = xt2 + yt2 + zt2 - self%waves(i, j, k) = xyzk - end do - end do - end do - case ('100') - do k = 1, self%nz_spec - do j = 1, self%ny_spec - do i = 1, self%nx_spec - iy = i + self%sp_st(1) - ix = j + self%sp_st(2) - iz = k + self%sp_st(3) - - rlexs = real(self%exs(ix), kind=dp)*geo%d(1) - rleys = real(self%eys(iy), kind=dp)*geo%d(2) - rlezs = real(self%ezs(iz), kind=dp)*geo%d(3) - - xtt = 2*(xdirps%interpl_v2p%a*cos(rlexs*0.5_dp) & - + xdirps%interpl_v2p%b*cos(rlexs*1.5_dp) & - + xdirps%interpl_v2p%c*cos(rlexs*2.5_dp) & - + xdirps%interpl_v2p%d*cos(rlexs*3.5_dp)) - ytt = 2*(ydirps%interpl_v2p%a*cos(rleys*0.5_dp) & - + ydirps%interpl_v2p%b*cos(rleys*1.5_dp) & - + ydirps%interpl_v2p%c*cos(rleys*2.5_dp) & - + ydirps%interpl_v2p%d*cos(rleys*3.5_dp)) - ztt = 2*(zdirps%interpl_v2p%a*cos(rlezs*0.5_dp) & - + zdirps%interpl_v2p%b*cos(rlezs*1.5_dp) & - + zdirps%interpl_v2p%c*cos(rlezs*2.5_dp) & - + zdirps%interpl_v2p%d*cos(rlezs*3.5_dp)) - - xt1 = 1._dp + 2*xdirps%interpl_v2p%alpha*cos(rlexs) - yt1 = 1._dp + 2*ydirps%interpl_v2p%alpha*cos(rleys) - zt1 = 1._dp + 2*zdirps%interpl_v2p%alpha*cos(rlezs) - - xt2 = self%k2x(ix)*((ytt/yt1)*(ztt/zt1))**2 - yt2 = self%k2y(iy)*((xtt/xt1)*(ztt/zt1))**2 - zt2 = self%k2z(iz)*((xtt/xt1)*(ytt/yt1))**2 - - xyzk = xt2 + yt2 + zt2 - self%waves(i, j, k) = xyzk - end do + dirs = spectral_dirs(self%bc_code) + call self%spectral_line(dirs(1), geo, xdirps, ydirps, zdirps, & + self%nx_spec, self%sp_st(1), .true., l1) + call self%spectral_line(dirs(2), geo, xdirps, ydirps, zdirps, & + self%ny_spec, self%sp_st(2), .false., l2) + call self%spectral_line(dirs(3), geo, xdirps, ydirps, zdirps, & + self%nz_spec, self%sp_st(3), .false., l3) + + do k = 1, self%nz_spec + do j = 1, self%ny_spec + do i = 1, self%nx_spec + w_re = l1%k2_re(i)*(l2%t_re(j)*l3%t_re(k))**2 & + + l2%k2_re(j)*(l1%t_re(i)*l3%t_re(k))**2 & + + l3%k2_re(k)*(l1%t_re(i)*l2%t_re(j))**2 + w_im = l1%k2_im(i)*(l2%t_im(j)*l3%t_im(k))**2 & + + l2%k2_im(j)*(l1%t_im(i)*l3%t_im(k))**2 & + + l3%k2_im(k)*(l1%t_im(i)*l2%t_im(j))**2 + self%waves(i, j, k) = cmplx(w_re, w_im, kind=dp) end do end do + end do + + end subroutine waves_set + + subroutine spectral_line(self, dir, geo, xdirps, ydirps, zdirps, n, st, & + is_r2c, line) + !! The 1D factors of waves_set along the spectral dimension that holds + !! direction dir: the squared modified wavenumber k2 and the + !! interpolation transfer function t of the n local modes from offset st. + !! Along the r2c dimension (is_r2c) of a non-periodic direction, the + !! imaginary factors are those of the mirrored mode N - k (see + !! waves_set). The mean mode has no mirror; its imaginary part carries + !! no data, so it keeps its own factors. + implicit none + + class(poisson_fft_t), intent(in) :: self + integer, intent(in) :: dir, n, st + type(geo_t), intent(in) :: geo + type(dirps_t), intent(in) :: xdirps, ydirps, zdirps + logical, intent(in) :: is_r2c + type(spectral_line_t), intent(out) :: line + + complex(dp), allocatable, dimension(:) :: k2, e + real(dp), allocatable, dimension(:) :: t + integer :: i, m, n_glob + logical :: periodic + + select case (dir) + case (1) + k2 = self%k2x; e = self%exs; periodic = self%periodic_x + t = interp_transfer(xdirps%interpl_v2p, real(e, kind=dp)*geo%d(1)) + case (2) + k2 = self%k2y; e = self%eys; periodic = self%periodic_y + t = interp_transfer(ydirps%interpl_v2p, real(e, kind=dp)*geo%d(2)) case default - do k = 1, self%nz_spec - do j = 1, self%ny_spec - do i = 1, self%nx_spec - ix = i + self%sp_st(1) - iy = j + self%sp_st(2) - iz = k + self%sp_st(3) - - rlexs = real(self%exs(ix), kind=dp)*geo%d(1) - rleys = real(self%eys(iy), kind=dp)*geo%d(2) - rlezs = real(self%ezs(iz), kind=dp)*geo%d(3) - - xtt = 2*(xdirps%interpl_v2p%a*cos(rlexs*0.5_dp) & - + xdirps%interpl_v2p%b*cos(rlexs*1.5_dp) & - + xdirps%interpl_v2p%c*cos(rlexs*2.5_dp) & - + xdirps%interpl_v2p%d*cos(rlexs*3.5_dp)) - ytt = 2*(ydirps%interpl_v2p%a*cos(rleys*0.5_dp) & - + ydirps%interpl_v2p%b*cos(rleys*1.5_dp) & - + ydirps%interpl_v2p%c*cos(rleys*2.5_dp) & - + ydirps%interpl_v2p%d*cos(rleys*3.5_dp)) - ztt = 2*(zdirps%interpl_v2p%a*cos(rlezs*0.5_dp) & - + zdirps%interpl_v2p%b*cos(rlezs*1.5_dp) & - + zdirps%interpl_v2p%c*cos(rlezs*2.5_dp) & - + zdirps%interpl_v2p%d*cos(rlezs*3.5_dp)) - - xt1 = 1._dp + 2*xdirps%interpl_v2p%alpha*cos(rlexs) - yt1 = 1._dp + 2*ydirps%interpl_v2p%alpha*cos(rleys) - zt1 = 1._dp + 2*zdirps%interpl_v2p%alpha*cos(rlezs) - - xt2 = self%k2x(ix)*((ytt/yt1)*(ztt/zt1))**2 - yt2 = self%k2y(iy)*((xtt/xt1)*(ztt/zt1))**2 - zt2 = self%k2z(iz)*((xtt/xt1)*(ytt/yt1))**2 - - xyzk = xt2 + yt2 + zt2 - self%waves(i, j, k) = xyzk - end do - end do - end do + k2 = self%k2z; e = self%ezs; periodic = self%periodic_z + t = interp_transfer(zdirps%interpl_v2p, real(e, kind=dp)*geo%d(3)) end select + n_glob = size(k2) - end subroutine waves_set + allocate (line%k2_re(n), line%k2_im(n), line%t_re(n), line%t_im(n)) + do i = 1, n + line%k2_re(i) = real(k2(st + i), kind=dp) + line%t_re(i) = t(st + i) + m = st + i + if (is_r2c .and. (.not. periodic) .and. m > 1) m = n_glob - m + 2 + line%k2_im(i) = real(k2(m), kind=dp) + line%t_im(i) = t(m) + end do + + end subroutine spectral_line + + pure elemental real(dp) function interp_transfer(interp, w) result(t) + !! Transfer function of the staggered (v2p) interpolation at the scaled + !! wavenumber w = k*d, the factor waves_set and stretching_matrix apply + !! to the wavenumbers of the other two directions. + implicit none + + type(tdsops_t), intent(in) :: interp + real(dp), intent(in) :: w + + t = 2*(interp%a*cos(w*0.5_dp) + interp%b*cos(w*1.5_dp) & + + interp%c*cos(w*2.5_dp) + interp%d*cos(w*3.5_dp)) & + /(1._dp + 2*interp%alpha*cos(w)) + + end function interp_transfer subroutine wave_numbers(a, b, k, e, k2, n, L, d, periodic, c_a, c_b, c_alpha) implicit none diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2fbeaa95b3..53742232d9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -415,6 +415,8 @@ function(define_backend_tests backend) define_test(unit/test_scalar_product.f90 ${np} ${backend}) define_test(unit/test_sum_intox.f90 ${np} ${backend}) define_test(unit/test_boundary_planes.f90 ${serial} ${backend}) + # z split over two ranks: a Z_FACE belongs to one rank only + define_test(unit/test_boundary_planes.f90 2 ${backend}) define_test(unit/test_abl_seed.f90 ${serial} ${backend}) # Two ranks in z must draw different noise. define_test(unit/test_channel_noise.f90 ${serial} ${backend}) @@ -473,7 +475,7 @@ function(define_backend_tests backend) define_verification_test(verification/test_fft.f90 1 ${backend}) endif() if((is_omp AND WITH_2DECOMPFFT) OR ${backend} STREQUAL cuda) - foreach(bc ppp pdp dpp ddp pdp_odd dpp_odd ddp_odd) + foreach(bc ppp pdp dpp ddp ddd pdp_odd dpp_odd ddp_odd ddd_odd) set(X3D2_TEST_NAME_SUFFIX _${bc}) define_verification_test(verification/test_poisson_bc.f90 1 ${backend} --bc ${bc}) endforeach() @@ -533,6 +535,14 @@ function(define_backend_tests backend) set(X3D2_TEST_NAME_SUFFIX _ddp_ysplit) define_verification_test(verification/test_poisson_bc.f90 2 ${backend} --bc ddp --nproc_dir 1,2,1) endif() + # The 111 case folds z where it is whole: the z-pencil of the cell-dims + # decomposition (OpenMP) or the cuFFTMp Y-slab (CUDA) once z is split + set(X3D2_TEST_NAME_SUFFIX _ddd) + define_verification_test(verification/test_poisson_bc.f90 2 ${backend} --bc ddd) + if(is_omp) + set(X3D2_TEST_NAME_SUFFIX _ddd_ysplit) + define_verification_test(verification/test_poisson_bc.f90 2 ${backend} --bc ddd --nproc_dir 1,2,1) + endif() # The backend's factory must stop on a BC code it has no case type for set(X3D2_TEST_NAME_SUFFIX _unsupported_bc) define_test(unit/test_poisson_fft_abort.f90 ${serial} ${backend} unsupported_bc) diff --git a/tests/unit/test_boundary_planes.f90 b/tests/unit/test_boundary_planes.f90 index 3af0674858..76ad6248e1 100644 --- a/tests/unit/test_boundary_planes.f90 +++ b/tests/unit/test_boundary_planes.f90 @@ -13,17 +13,20 @@ program test_boundary_planes !! orderings coincide when ny <= SZ, so ny here is deliberately larger than !! SZ on both backends (SZ is 32 on CUDA, 16 on OMP) and is not a multiple !! of either, which also exercises the partially-filled last y-block. - use m_mpi, only: MPI_Init, MPI_Finalize + !! + !! z is split over all the ranks and bounded by free-slip walls, so a + !! Z_FACE belongs to the first or the last rank only, and the last rank + !! holds one cell fewer than vertices. Every check is rank-local. + use m_mpi, only: MPI_COMM_WORLD, MPI_Init, MPI_Finalize, MPI_Comm_size use m_allocator, only: allocator_t use m_backend_runtime, only: backend_runtime_t, SZ => backend_sz use m_base_backend, only: base_backend_t use m_common, only: dp, DIR_X, DIR_C, VERT, CELL, X_FACE, Y_FACE, & - BC_DIRICHLET, BC_NEUMANN + Z_FACE, BC_DIRICHLET, BC_NEUMANN use m_field, only: field_t use m_mesh, only: mesh_t - implicit none type(backend_runtime_t), target :: runtime @@ -34,8 +37,8 @@ program test_boundary_planes ! ny is not a multiple of either backend's SZ, so the last y-block is ! partially filled and the group orderings do not coincide. - integer, parameter :: dims_global(3) = [16, 65, 8] - integer, parameter :: nproc_dir(3) = [1, 1, 1] + integer, parameter :: dims_global(3) = [16, 65, 9] + integer :: nproc_dir(3), nproc real(dp), parameter :: lengths(3) = [1._dp, 1._dp, 1._dp] character(len=9), parameter :: bc_periodic(2) = ['periodic ', 'periodic '] character(len=9), parameter :: bc_slip(2) = ['neumann ', 'neumann '] @@ -49,11 +52,13 @@ program test_boundary_planes logical :: all_pass call MPI_Init(ierr) + call MPI_Comm_size(MPI_COMM_WORLD, nproc, ierr) + nproc_dir = [1, 1, nproc] all_pass = .true. tolerance = 2000._dp*epsilon(1._dp) mesh = mesh_t(dims_global, nproc_dir, lengths, & - bc_periodic, bc_slip, bc_periodic) + bc_periodic, bc_slip, bc_slip) dims = mesh%get_dims(VERT) call runtime%init(mesh) @@ -286,6 +291,80 @@ program test_boundary_planes end if end block + ! --- x_slice_weighted_sum with z weights ---------------------------------- + ! A weight per local z station on top of the y weights, the trapezoid the + ! wind turbine case passes at z walls + block + real(dp), allocatable :: weights(:), z_weights(:) + real(dp) :: got, want + + call backend%set_field_data(f, data) + allocate (weights(dims(2)), z_weights(dims(3))) + weights = 1._dp + weights(1) = 0.5_dp + do k = 1, dims(3) + z_weights(k) = 1._dp + 0.1_dp*real(k, dp) + end do + + call backend%x_slice_weighted_sum(got, f, dims(1), weights, z_weights) + want = 0._dp + do k = 1, dims(3) + do j = 1, dims(2) + want = want + weights(j)*z_weights(k)*data(dims(1), j, k) + end do + end do + if (abs(got - want) > tolerance*abs(want)) then + print *, 'FAIL: x_slice_weighted_sum with z weights, relative error=', & + abs(got - want)/abs(want) + all_pass = .false. + else + print *, 'PASS: x_slice_weighted_sum with z weights' + end if + end block + + ! --- field_set_face Z_FACE: only the ranks holding a global z face -------- + ! A non-Dirichlet bottom face is left alone, as for Y_FACE. On a cell field + ! the top face is the last cell, one below the padded vertex count on the + ! last rank. + block + integer :: cell_dims(3), loc_dims(3), iloc + logical :: first_z, last_z + + first_z = mesh%par%nrank_dir(3) == 0 + last_z = mesh%par%nrank_dir(3) == mesh%par%nproc_dir(3) - 1 + cell_dims = mesh%get_dims(CELL) + do iloc = 1, 2 + if (iloc == 1) then + loc_dims = dims + else + loc_dims = cell_dims + call f%set_data_loc(CELL) + end if + + call f%fill(sentinel) + call backend%field_set_face(f, bottom, top, Z_FACE) + call backend%get_field_data(data, f) + expected = sentinel + if (first_z) expected(:, :, 1) = bottom + if (last_z) expected(:, :, loc_dims(3)) = top + write (label, '(a,i0)') 'field_set_face Z_FACE, data_loc case ', iloc + call check_planes(trim(label), data, expected, loc_dims, tolerance, & + all_pass) + + call f%fill(sentinel) + call backend%field_set_face(f, bottom, top, Z_FACE, & + bc_start=BC_NEUMANN, bc_end=BC_DIRICHLET) + call backend%get_field_data(data, f) + expected = sentinel + if (last_z) expected(:, :, loc_dims(3)) = top + write (label, '(a,i0)') 'field_set_face Z_FACE Neumann bottom, case ', & + iloc + call check_planes(trim(label), data, expected, loc_dims, tolerance, & + all_pass) + end do + call f%set_data_loc(VERT) + end block + ! --- field_set_y_plane: one interior plane, nothing else ------------------ ! The wall model writes the first plane above the floor, so this must reach ! an interior plane with the same per-backend group ordering as the faces. diff --git a/tests/unit/test_mesh.f90 b/tests/unit/test_mesh.f90 index 642b64210e..790b0e2540 100644 --- a/tests/unit/test_mesh.f90 +++ b/tests/unit/test_mesh.f90 @@ -73,12 +73,10 @@ subroutine run_test_mesh(use_2decomp, allpass) mesh = mesh_t(dims_global, nproc_dir, L_global, BC_x, BC_y, BC_z, & use_2decomp=use_2decomp) - ! Expected decomposition by 2decomp and generic - if (use_2decomp) then - n_vert_z = [3, 4, 4, 5] - else - n_vert_z = [4, 4, 4, 4] - end if + ! Expected decomposition, the same for 2decomp and generic: the 15 + ! cells of the non-periodic z split 3, 4, 4, 4, and the last rank holds + ! the extra vertex + n_vert_z = [3, 4, 4, 5] allocator = allocator_t(mesh%get_dims(VERT), 8) diff --git a/tests/unit/test_poisson_bc_code.f90 b/tests/unit/test_poisson_bc_code.f90 index 1f45c10d6d..ab2b26213b 100644 --- a/tests/unit/test_poisson_bc_code.f90 +++ b/tests/unit/test_poisson_bc_code.f90 @@ -5,8 +5,9 @@ program test_poisson_bc_code !! poisson_bc_code matches the one x3d2-memcheck gets from !! classify_boundaries. - use m_mesh, only: classify_boundaries, FFT_BC_000, FFT_BC_010, & - FFT_BC_100, FFT_BC_110, FFT_BC_UNSUPPORTED + use m_mesh, only: classify_boundaries, fft_bc_is_11x, FFT_BC_000, & + FFT_BC_010, FFT_BC_100, FFT_BC_110, FFT_BC_111, & + FFT_BC_UNSUPPORTED use m_poisson_fft, only: poisson_bc_code, spectral_dirs, fft_bc_class implicit none @@ -22,12 +23,20 @@ program test_poisson_bc_code call test_bc_code([.true., .true., .false.], '001', FFT_BC_UNSUPPORTED) call test_bc_code([.true., .false., .false.], '011', FFT_BC_UNSUPPORTED) call test_bc_code([.false., .true., .false.], '101', FFT_BC_UNSUPPORTED) - call test_bc_code([.false., .false., .false.], '111', FFT_BC_UNSUPPORTED) + call test_bc_code([.false., .false., .false.], '111', FFT_BC_111) call test_spectral_dirs('000', [1, 2, 3]) call test_spectral_dirs('010', [1, 2, 3]) call test_spectral_dirs('100', [2, 1, 3]) call test_spectral_dirs('110', [3, 1, 2]) + call test_spectral_dirs('111', [3, 1, 2]) + + ! 110 and 111 share the 11x solver, no other class does + if (.not. (fft_bc_is_11x(FFT_BC_110) .and. fft_bc_is_11x(FFT_BC_111))) & + test_pass = .false. + if (fft_bc_is_11x(FFT_BC_000) .or. fft_bc_is_11x(FFT_BC_010) & + .or. fft_bc_is_11x(FFT_BC_100) & + .or. fft_bc_is_11x(FFT_BC_UNSUPPORTED)) test_pass = .false. if (.not. test_pass) then error stop "FAIL" diff --git a/tests/verification/test_poisson_bc.f90 b/tests/verification/test_poisson_bc.f90 index cea885b4b6..999d11a578 100644 --- a/tests/verification/test_poisson_bc.f90 +++ b/tests/verification/test_poisson_bc.f90 @@ -2,14 +2,16 @@ program test_poisson !! Poisson Solver Validation Test (self-contained, no input files) !! !! Validates the Poisson solver across the boundary condition - !! configurations listed in the table cases, 7 on a uniform mesh: + !! configurations listed in the table cases, 9 on a uniform mesh: !! ppp : all periodic (128 x 64 x 32) !! pdp : y-dirichlet (128 x 65 x 128) !! dpp : x-dirichlet (129 x 64 x 128) !! ddp : x,y-dirichlet (129 x 257 x 64) + !! ddd : x,y,z-dirichlet (65 x 65 x 129) !! pdp_odd : y-dirichlet, odd (128 x 82 x 128) !! dpp_odd : x-dirichlet, odd (82 x 64 x 128) !! ddp_odd : x,y-dirichlet, odd (82 x 82 x 64) + !! ddd_odd : x,y,z-dirichlet, odd (82 x 82 x 82) !! The y-stretched rows are described in the table itself. !! The code is one letter per direction in x,y,z order: d = dirichlet, !! n = neumann, p = periodic. The solver works on cells, and a @@ -45,7 +47,9 @@ program test_poisson !! grid indices, and only Check 2 applies (the analytical solution uses the !! exact wavenumber, which a mode this close to the grid scale cannot !! match). That x index is the Nyquist mode only when x is periodic, so a - !! solver that zeroes it as if it were leaves the RHS unsolved. + !! solver that zeroes it as if it were leaves the RHS unsolved. With a + !! non-periodic z (ddd) the alternating z factor is no longer a single + !! mode, but it is still a grid-scale RHS the round trip must recover. !! !! Layout: setup_case builds one poisson_ctx_t for each configuration !! (mesh, backend, allocators, tdsops, vector_calculus, poisson_fft) and @@ -145,6 +149,12 @@ program test_poisson bc_case_t('x,y-dirichlet', [129, 257, 64], & [character(len=9) :: 'dirichlet', 'dirichlet', 'periodic'], & 'uniform', 0.0_dp), & + ! No periodic direction (#449). 64 x 64 x 128 cells: z carries the + ! decomposition, 64 cells per rank at 2 ranks, and on the CUDA backend + ! every cell count divides by 2, as its multi-rank 111 case needs. + bc_case_t('x,y,z-dirichlet', [65, 65, 129], & + [character(len=9) :: 'dirichlet', 'dirichlet', 'dirichlet'], & + 'uniform', 0.0_dp), & ! Stretched in y. Only pdp is solved with the stretched operator, the ! other BC codes end in the abort of the solver. 64 cells in x and z. bc_case_t('y-dirichlet centred', [64, 65, 64], & @@ -181,6 +191,9 @@ program test_poisson 'uniform', 0.0_dp), & bc_case_t('x,y-dirichlet, odd', [82, 82, 64], & [character(len=9) :: 'dirichlet', 'dirichlet', 'periodic'], & + 'uniform', 0.0_dp), & + bc_case_t('x,y,z-dirichlet, odd', [82, 82, 82], & + [character(len=9) :: 'dirichlet', 'dirichlet', 'dirichlet'], & 'uniform', 0.0_dp)] !&> @@ -1086,6 +1099,9 @@ function run_mode_test(ctx) result(res) end do end do end do + ! With odd cell counts in x and in a non-periodic z (ddd_odd) neither + ! factor sums to zero, and the mean is not solvable with Neumann BCs + call remove_weighted_mean(ctx, host_field) f_device => ctx%backend%allocator%get_block(DIR_C, CELL) f_reference => ctx%backend%allocator%get_block(DIR_X) @@ -1116,8 +1132,6 @@ function run_random_test(ctx) result(res) class(field_t), pointer :: u, v, w, f_div, f_device, f_reference class(field_t), pointer :: host_field, temp - integer :: dims(3), j, ierr - real(dp) :: f_mean, f_sums(2) u => ctx%backend%allocator%get_block(DIR_X, VERT) v => ctx%backend%allocator%get_block(DIR_X, VERT) @@ -1138,24 +1152,10 @@ function run_random_test(ctx) result(res) call ctx%backend%allocator%release_block(w) ! Remove the weighted mean on the host, then hand f to the solver in DIR_C - dims = ctx%mesh%get_dims(CELL) host_field => ctx%host_allocator%get_block(DIR_C) call ctx%backend%get_field_data(host_field%data, f_div) call ctx%backend%allocator%release_block(f_div) - ! The weight of the y cell j is 1/midp_ds(j, 2). This weight is the - ! cell width divided by d(2), and it is 1 on a uniform grid. - f_sums = 0._dp - do j = 1, dims(2) - f_sums(1) = f_sums(1) & - + sum(host_field%data(1:dims(1), j, 1:dims(3))) & - /ctx%mesh%geo%midp_ds(j, 2) - f_sums(2) = f_sums(2) & - + real(dims(1)*dims(3), dp)/ctx%mesh%geo%midp_ds(j, 2) - end do - call MPI_Allreduce(MPI_IN_PLACE, f_sums, 2, MPI_X3D2_DP, MPI_SUM, & - MPI_COMM_WORLD, ierr) - f_mean = f_sums(1)/f_sums(2) - host_field%data = host_field%data - f_mean + call remove_weighted_mean(ctx, host_field) f_device => ctx%backend%allocator%get_block(DIR_C, CELL) f_reference => ctx%backend%allocator%get_block(DIR_X) @@ -1178,6 +1178,32 @@ function run_random_test(ctx) result(res) merge('PASS', 'FAIL', res%passed) end function run_random_test + subroutine remove_weighted_mean(ctx, host_field) + !! Subtract the cell-width weighted global mean from a host cell field, + !! the mode the Neumann problem cannot solve for (see the header) + type(poisson_ctx_t), target, intent(in) :: ctx + class(field_t), intent(inout) :: host_field + + integer :: dims(3), j, ierr + real(dp) :: f_mean, f_sums(2) + + dims = ctx%mesh%get_dims(CELL) + ! The weight of the y cell j is 1/midp_ds(j, 2). This weight is the + ! cell width divided by d(2), and it is 1 on a uniform grid. + f_sums = 0._dp + do j = 1, dims(2) + f_sums(1) = f_sums(1) & + + sum(host_field%data(1:dims(1), j, 1:dims(3))) & + /ctx%mesh%geo%midp_ds(j, 2) + f_sums(2) = f_sums(2) & + + real(dims(1)*dims(3), dp)/ctx%mesh%geo%midp_ds(j, 2) + end do + call MPI_Allreduce(MPI_IN_PLACE, f_sums, 2, MPI_X3D2_DP, MPI_SUM, & + MPI_COMM_WORLD, ierr) + f_mean = f_sums(1)/f_sums(2) + host_field%data = host_field%data - f_mean + end subroutine remove_weighted_mean + subroutine upload_random(ctx, f) !! Fill a vertex field with uniform random values in [-1, 1) type(poisson_ctx_t), target, intent(in) :: ctx