From 0f777fc69848aa5cefb8607d33aa95ea5340f2bf Mon Sep 17 00:00:00 2001 From: kaanolgu Date: Fri, 20 Nov 2026 14:25:30 +0000 Subject: [PATCH 1/6] Add the OpenMP spectral kernels for the stretched 010 Poisson solve --- .../omp/kernels/spectral_processing.f90 | 343 ++++++++++++++++++ 1 file changed, 343 insertions(+) diff --git a/src/backend/omp/kernels/spectral_processing.f90 b/src/backend/omp/kernels/spectral_processing.f90 index 4915b8928a..f03913dde6 100644 --- a/src/backend/omp/kernels/spectral_processing.f90 +++ b/src/backend/omp/kernels/spectral_processing.f90 @@ -290,6 +290,349 @@ subroutine process_spectral_010( & end subroutine process_spectral_010 + subroutine process_spectral_010_fw( & + div_u, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, & + nx, ny, nz, ax, bx, ay, by, az, bz & + ) + !! Forward third of the spectral post-process for the stretched 010 case: + !! normalise, post-process in z and x, then pair the y modes. + !! + !! Ref. JCP 228 (2009), 5989–6015, Sec 4 + implicit none + + !> Divergence of velocity in spectral space + complex(dp), intent(inout), dimension(:, :, :) :: div_u + !> Spectral equivalence constants + real(dp), intent(in), dimension(:) :: ax, bx, ay, by, az, bz + !> Grid size in spectral space + integer, intent(in) :: nx_spec, ny_spec, nz_spec + !> Offsets in the permuted pencils in spectral space + integer, intent(in) :: x_sp_st, y_sp_st, z_sp_st + !> Global cell size + integer, intent(in) :: nx, ny, nz + + integer :: i, j, k, ix, iy, iz, iy_r + real(dp) :: tmp_r, tmp_c, div_r, div_c, l_r, l_c, r_r, r_c + + !$omp parallel do private(div_r, div_c, ix, iz, tmp_r, tmp_c) collapse(3) + do k = 1, nz_spec + do j = 1, ny_spec + do i = 1, nx_spec + ix = i + x_sp_st + iz = k + z_sp_st + + ! normalisation + div_r = real(div_u(i, j, k), kind=dp)/nx/ny/nz + div_c = aimag(div_u(i, j, k))/nx/ny/nz + + ! postprocess in z + tmp_r = div_r + tmp_c = div_c + div_r = tmp_r*bz(iz) + tmp_c*az(iz) + div_c = tmp_c*bz(iz) - tmp_r*az(iz) + if (iz > nz/2 + 1) div_r = -div_r + if (iz > nz/2 + 1) div_c = -div_c + + ! postprocess in x + tmp_r = div_r + tmp_c = div_c + div_r = tmp_r*bx(ix) + tmp_c*ax(ix) + div_c = tmp_c*bx(ix) - tmp_r*ax(ix) + if (ix > nx/2 + 1) div_r = -div_r + if (ix > nx/2 + 1) div_c = -div_c + + ! update the entry + div_u(i, j, k) = cmplx(div_r, div_c, kind=dp) + end do + end do + end do + !$omp end parallel do + + !$omp parallel do private(iy, iy_r, l_r, l_c, r_r, r_c) collapse(3) + do k = 1, nz_spec + do j = 2, ny_spec/2 + 1 + do i = 1, nx_spec + iy = j + y_sp_st + iy_r = ny_spec - j + 2 + y_sp_st + + l_r = real(div_u(i, j, k), kind=dp) + l_c = aimag(div_u(i, j, k)) + r_r = real(div_u(i, ny_spec - j + 2, k), kind=dp) + r_c = aimag(div_u(i, ny_spec - j + 2, k)) + + ! update the entry + div_u(i, j, k) = 0.5_dp*cmplx( & !& + l_r*by(iy) + l_c*ay(iy) + r_r*by(iy) - r_c*ay(iy), & + -l_r*ay(iy) + l_c*by(iy) + r_r*ay(iy) + r_c*by(iy), kind=dp & + ) + div_u(i, ny_spec - j + 2, k) = 0.5_dp*cmplx( & !& + r_r*by(iy_r) + r_c*ay(iy_r) + l_r*by(iy_r) - l_c*ay(iy_r), & + -r_r*ay(iy_r) + r_c*by(iy_r) + l_r*ay(iy_r) + l_c*by(iy_r), & + kind=dp & + ) + end do + end do + end do + !$omp end parallel do + + end subroutine process_spectral_010_fw + + subroutine process_spectral_010_poisson( & + div_u, a_re, a_im, off, inc, nx_spec, n, nz_spec, x_sp_st, z_sp_st, & + nx, ny, nz & + ) + !! Solve the Poisson equation with the pentadiagonal matrices of the + !! stretched 010 case, along y. + !! + !! The elimination overwrites a_re and a_im, so the caller passes + !! working copies of the matrices. + !! + !! Ref. JCP 228 (2009), 5989–6015, Sec 4 + implicit none + + !> Divergence of velocity in spectral space + complex(dp), intent(inout), dimension(:, :, :) :: div_u + !> Pentadiagonal matrices, overwritten by the elimination + real(dp), intent(inout), dimension(:, :, :, :) :: a_re, a_im + !> Offset and increment. The increment is 2 for only the odd or even rows + integer, intent(in) :: off, inc + !> Grid size in spectral space. n is the number of rows of the matrix + integer, intent(in) :: nx_spec, n, nz_spec + !> Offsets in the permuted pencils in spectral space + integer, intent(in) :: x_sp_st, z_sp_st + !> Global cell size + integer, intent(in) :: nx, ny, nz + + integer :: i, j, k, jm, nm + real(dp) :: tmp_r, tmp_c, div_r, div_c + + !$omp parallel do private(j, jm, nm, tmp_r, tmp_c, div_r, div_c) collapse(2) + do k = 1, nz_spec + do i = 1, nx_spec + ! Forward pass for the pentadiagonal matrix + do j = 1, n - 2 + ! j mapping based on odd/even + ! inc=2, off=0 ---> j => 2j - 1 + ! inc=2, off=1 ---> j => 2j + ! inc=1, off=0 ---> j => j + jm = inc*j + off - inc/2 + ! eliminate diag-1 + tmp_r = 0._dp + if (abs(a_re(i, j, k, 3)) > eps_wave) then + tmp_r = a_re(i, j + 1, k, 2)/a_re(i, j, k, 3) + end if + tmp_c = 0._dp + if (abs(a_im(i, j, k, 3)) > eps_wave) then + tmp_c = a_im(i, j + 1, k, 2)/a_im(i, j, k, 3) + end if + div_r = real(div_u(i, jm + inc, k) - tmp_r*div_u(i, jm, k), kind=dp) + div_c = aimag(div_u(i, jm + inc, k) - tmp_c*div_u(i, jm, k)) + div_u(i, jm + inc, k) = cmplx(div_r, div_c, kind=dp) + ! modify pentadiagonal coefficients in-place + a_re(i, j + 1, k, 3) = a_re(i, j + 1, k, 3) - tmp_r*a_re(i, j, k, 4) + a_im(i, j + 1, k, 3) = a_im(i, j + 1, k, 3) - tmp_c*a_im(i, j, k, 4) + a_re(i, j + 1, k, 4) = a_re(i, j + 1, k, 4) - tmp_r*a_re(i, j, k, 5) + a_im(i, j + 1, k, 4) = a_im(i, j + 1, k, 4) - tmp_c*a_im(i, j, k, 5) + + ! eliminate diag-2 + tmp_r = 0._dp + if (abs(a_re(i, j, k, 3)) > eps_wave) then + tmp_r = a_re(i, j + 2, k, 1)/a_re(i, j, k, 3) + end if + tmp_c = 0._dp + if (abs(a_im(i, j, k, 3)) > eps_wave) then + tmp_c = a_im(i, j + 2, k, 1)/a_im(i, j, k, 3) + end if + div_r = real(div_u(i, jm + 2*inc, k) - tmp_r*div_u(i, jm, k), & + kind=dp) + div_c = aimag(div_u(i, jm + 2*inc, k) - tmp_c*div_u(i, jm, k)) + div_u(i, jm + 2*inc, k) = cmplx(div_r, div_c, kind=dp) + ! modify pentadiagonal coefficients in-place + a_re(i, j + 2, k, 2) = a_re(i, j + 2, k, 2) - tmp_r*a_re(i, j, k, 4) + a_im(i, j + 2, k, 2) = a_im(i, j + 2, k, 2) - tmp_c*a_im(i, j, k, 4) + a_re(i, j + 2, k, 3) = a_re(i, j + 2, k, 3) - tmp_r*a_re(i, j, k, 5) + a_im(i, j + 2, k, 3) = a_im(i, j + 2, k, 3) - tmp_c*a_im(i, j, k, 5) + end do + + ! handle the last row + if (abs(a_re(i, n - 1, k, 3)) > eps_wave) then + tmp_r = a_re(i, n, k, 2)/a_re(i, n - 1, k, 3) + else + tmp_r = 0._dp + end if + if (abs(a_im(i, n - 1, k, 3)) > eps_wave) then + tmp_c = a_im(i, n, k, 2)/a_im(i, n - 1, k, 3) + else + tmp_c = 0._dp + end if + div_r = a_re(i, n, k, 3) - tmp_r*a_re(i, n - 1, k, 4) + div_c = a_im(i, n, k, 3) - tmp_c*a_im(i, n - 1, k, 4) + + ! j mapping based on odd/even for last point j=n + nm = inc*n + off - inc/2 + if (abs(div_r) > eps_wave) then + tmp_r = tmp_r/div_r + div_r = real(div_u(i, nm, k), kind=dp)/div_r & + - tmp_r*real(div_u(i, nm - inc, k), kind=dp) + else + tmp_r = 0._dp + div_r = 0._dp + end if + if (abs(div_c) > eps_wave) then + tmp_c = tmp_c/div_c + div_c = aimag(div_u(i, nm, k))/div_c & + - tmp_c*aimag(div_u(i, nm - inc, k)) + else + tmp_c = 0._dp + div_c = 0._dp + end if + div_u(i, nm, k) = cmplx(div_r, div_c, kind=dp) + + if (abs(a_re(i, n - 1, k, 3)) > eps_wave) then + tmp_r = 1._dp/a_re(i, n - 1, k, 3) + else + tmp_r = 0._dp + end if + if (abs(a_im(i, n - 1, k, 3)) > eps_wave) then + tmp_c = 1._dp/a_im(i, n - 1, k, 3) + else + tmp_c = 0._dp + end if + div_r = a_re(i, n - 1, k, 4)*tmp_r + div_c = a_im(i, n - 1, k, 4)*tmp_c + div_u(i, nm - inc, k) = cmplx( & !& + real(div_u(i, nm - inc, k), kind=dp)*tmp_r & + - real(div_u(i, nm, k), kind=dp)*div_r, & + aimag(div_u(i, nm - inc, k))*tmp_c & + - aimag(div_u(i, nm, k))*div_c, & + kind=dp & + ) + + ! zero the mean mode, tested on the global spectral indices + if (i + x_sp_st == nx/2 + 1 .and. k + z_sp_st == nz/2 + 1) then + div_u(i, nm, k) = 0._dp + div_u(i, nm - inc, k) = 0._dp + end if + + ! backward pass + do j = n - 2, 1, -1 + ! j mapping based on odd/even + jm = inc*j + off - inc/2 + if (abs(a_re(i, j, k, 3)) > eps_wave) then + tmp_r = 1._dp/a_re(i, j, k, 3) + else + tmp_r = 0._dp + end if + if (abs(a_im(i, j, k, 3)) > eps_wave) then + tmp_c = 1._dp/a_im(i, j, k, 3) + else + tmp_c = 0._dp + end if + div_u(i, jm, k) = cmplx( & !& + tmp_r*(real(div_u(i, jm, k), kind=dp) & + - a_re(i, j, k, 4)*real(div_u(i, jm + inc, k), kind=dp) & + - a_re(i, j, k, 5) & + *real(div_u(i, jm + 2*inc, k), kind=dp)), & + tmp_c*(aimag(div_u(i, jm, k)) & + - a_im(i, j, k, 4)*aimag(div_u(i, jm + inc, k)) & + - a_im(i, j, k, 5)*aimag(div_u(i, jm + 2*inc, k))), & + kind=dp & + ) + if (i + x_sp_st == nx/2 + 1 .and. k + z_sp_st == nz/2 + 1) & + div_u(i, jm, k) = 0._dp + end do + end do + end do + !$omp end parallel do + + end subroutine process_spectral_010_poisson + + subroutine process_spectral_010_bw( & + div_u, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, & + nx, ny, nz, ax, bx, ay, by, az, bz & + ) + !! Backward third of the spectral post-process for the stretched 010 case: + !! unpair the y modes, then post-process in x and z. + !! + !! Ref. JCP 228 (2009), 5989–6015, Sec 4 + implicit none + + !> Divergence of velocity in spectral space + complex(dp), intent(inout), dimension(:, :, :) :: div_u + !> Spectral equivalence constants + real(dp), intent(in), dimension(:) :: ax, bx, ay, by, az, bz + !> Grid size in spectral space + integer, intent(in) :: nx_spec, ny_spec, nz_spec + !> Offsets in the permuted pencils in spectral space + integer, intent(in) :: x_sp_st, y_sp_st, z_sp_st + !> Global cell size + integer, intent(in) :: nx, ny, nz + + integer :: i, j, k, ix, iy, iz, iy_r + real(dp) :: tmp_r, tmp_c, div_r, div_c, l_r, l_c, r_r, r_c + + !$omp parallel do private(iy, iy_r, l_r, l_c, r_r, r_c) collapse(3) + do k = 1, nz_spec + do j = 2, ny_spec/2 + 1 + do i = 1, nx_spec + iy = j + y_sp_st + iy_r = ny_spec - j + 2 + y_sp_st + + l_r = real(div_u(i, j, k), kind=dp) + l_c = aimag(div_u(i, j, k)) + r_r = real(div_u(i, ny_spec - j + 2, k), kind=dp) + r_c = aimag(div_u(i, ny_spec - j + 2, k)) + + ! update the entry + div_u(i, j, k) = cmplx( & !& + l_r*by(iy) - l_c*ay(iy) + r_r*ay(iy) + r_c*by(iy), & + l_r*ay(iy) + l_c*by(iy) - r_r*by(iy) + r_c*ay(iy), kind=dp & + ) + div_u(i, ny_spec - j + 2, k) = cmplx( & !& + r_r*by(iy_r) - r_c*ay(iy_r) + l_r*ay(iy_r) + l_c*by(iy_r), & + r_r*ay(iy_r) + r_c*by(iy_r) - l_r*by(iy_r) + l_c*ay(iy_r), & + kind=dp & + ) + end do + end do + end do + !$omp end parallel do + + !$omp parallel do private(div_r, div_c, ix, iz, tmp_r, tmp_c) collapse(3) + do k = 1, nz_spec + do j = 1, ny_spec + do i = 1, nx_spec + ix = i + x_sp_st + iz = k + z_sp_st + + div_r = real(div_u(i, j, k), kind=dp) + div_c = aimag(div_u(i, j, k)) + + ! post-process in z + tmp_r = div_r + tmp_c = div_c + div_r = tmp_r*bz(iz) - tmp_c*az(iz) + div_c = tmp_c*bz(iz) + tmp_r*az(iz) + if (iz > nz/2 + 1) div_r = -div_r + if (iz > nz/2 + 1) div_c = -div_c + + ! post-process in x + tmp_r = div_r + tmp_c = div_c + div_r = tmp_r*bx(ix) - tmp_c*ax(ix) + div_c = tmp_c*bx(ix) + tmp_r*ax(ix) + if (ix > nx/2 + 1) div_r = -div_r + if (ix > nx/2 + 1) div_c = -div_c + + ! update the entry + div_u(i, j, k) = cmplx(div_r, div_c, kind=dp) + end do + end do + end do + !$omp end parallel do + + end subroutine process_spectral_010_bw + subroutine process_spectral_100( & div_u, waves, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, & nx, ny, nz, ax, bx, ay, by, az, bz & From 2753cd3126442cc2e8c345c8215ef7f2714a41cb Mon Sep 17 00:00:00 2001 From: kaanolgu Date: Fri, 20 Nov 2026 14:25:30 +0000 Subject: [PATCH 2/6] Solve the top-bottom stretched 010 case in the OpenMP Poisson solver on one rank --- src/backend/omp/poisson_fft.f90 | 63 ++++++++++++++++++++++++-- tests/CMakeLists.txt | 11 +++-- tests/verification/test_poisson_bc.f90 | 13 +++--- 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/src/backend/omp/poisson_fft.f90 b/src/backend/omp/poisson_fft.f90 index 4c9c745932..723d4885ae 100644 --- a/src/backend/omp/poisson_fft.f90 +++ b/src/backend/omp/poisson_fft.f90 @@ -14,6 +14,9 @@ module m_omp_poisson_fft use m_tdsops, only: dirps_t use m_omp_spectral, only: process_spectral_000, process_spectral_010, & + process_spectral_010_fw, & + process_spectral_010_poisson, & + process_spectral_010_bw, & process_spectral_100, process_spectral_110, & process_spectral_110_fw, & process_spectral_110_solve, & @@ -62,6 +65,10 @@ module m_omp_poisson_fft type, extends(omp_poisson_fft_t) :: omp_poisson_010_t !! Periodic in x and z, non-periodic in y + !> Working copies of the stretching matrices. The pentadiagonal + !! elimination overwrites its matrices, so each solve starts from a copy + !! of the pristine matrices of the base class. + real(dp), allocatable, dimension(:, :, :, :) :: a_work_re, a_work_im contains procedure :: init_buffers => init_buffers_010 procedure :: fft_postprocess => fft_postprocess_010_omp @@ -199,9 +206,12 @@ subroutine init_omp(self, mesh, xdirps, ydirps, zdirps, lowmem) call self%base_init(mesh, xdirps, ydirps, zdirps, & self%sp%zsz, self%sp%zst - 1) - if (mesh%geo%stretched(2)) then - call abort_run('OpenMP backends FFT based Poisson solver does not & - &support stretching in y-direction yet!') + ! Only the top-bottom stretching on one rank is solved + if (mesh%geo%stretched(2) .and. & + (trim(mesh%geo%stretching(2)) /= 'top-bottom' & + .or. self%p_row*self%p_col > 1)) then + call abort_run('OpenMP backends FFT based Poisson solver supports only & + &top-bottom y stretching on one rank!') end if allocate (self%c_x(self%nx_spec, self%ny_spec, self%nz_spec)) @@ -254,6 +264,12 @@ subroutine init_buffers_010(self, mesh, dims) ! dimension whole, which the y-pencil of sp provides. if (self%p_col > 1) call self%init_pair_buffers(with_waves=.true.) + ! The top-bottom stretching matrices are symmetric, odd and even alike + if (self%stretched_y) then + allocate (self%a_work_re, mold=self%a_odd_re) + allocate (self%a_work_im, mold=self%a_odd_im) + end if + end subroutine init_buffers_010 subroutine init_buffers_100(self, mesh, dims) @@ -632,6 +648,47 @@ subroutine fft_postprocess_010_omp(self) az=self%az, bz=self%bz & ) call transpose_y_to_z(self%c_pair, self%c_x, self%sp) + else if (self%stretched_y) then + call process_spectral_010_fw( & + div_u=self%c_x, & + nx_spec=self%nx_spec, ny_spec=self%ny_spec, nz_spec=self%nz_spec, & + x_sp_st=self%sp_st(1), y_sp_st=self%sp_st(2), & + z_sp_st=self%sp_st(3), & + nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob, & + ax=self%ax, bx=self%bx, ay=self%ay, by=self%by, & + az=self%az, bz=self%bz & + ) + + ! The odd rows, then the even rows, each with its own matrices + self%a_work_re = self%a_odd_re + self%a_work_im = self%a_odd_im + call process_spectral_010_poisson( & + div_u=self%c_x, a_re=self%a_work_re, a_im=self%a_work_im, & + off=0, inc=2, nx_spec=self%nx_spec, n=self%ny_spec/2, & + nz_spec=self%nz_spec, x_sp_st=self%sp_st(1), & + z_sp_st=self%sp_st(3), & + nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob & + ) + + self%a_work_re = self%a_even_re + self%a_work_im = self%a_even_im + call process_spectral_010_poisson( & + div_u=self%c_x, a_re=self%a_work_re, a_im=self%a_work_im, & + off=1, inc=2, nx_spec=self%nx_spec, n=self%ny_spec/2, & + nz_spec=self%nz_spec, x_sp_st=self%sp_st(1), & + z_sp_st=self%sp_st(3), & + nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob & + ) + + call process_spectral_010_bw( & + div_u=self%c_x, & + nx_spec=self%nx_spec, ny_spec=self%ny_spec, nz_spec=self%nz_spec, & + x_sp_st=self%sp_st(1), y_sp_st=self%sp_st(2), & + z_sp_st=self%sp_st(3), & + nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob, & + ax=self%ax, bx=self%bx, ay=self%ay, by=self%by, & + az=self%az, bz=self%bz & + ) else call process_spectral_010( & div_u=self%c_x, waves=self%waves, & diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2fbeaa95b3..a1a16edacb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -477,10 +477,10 @@ function(define_backend_tests backend) set(X3D2_TEST_NAME_SUFFIX _${bc}) define_verification_test(verification/test_poisson_bc.f90 1 ${backend} --bc ${bc}) endforeach() - # Stretching in y is specific to the pdp (010) solver and needs the CUDA - # backend, so these cases are registered for CUDA only. On 2 ranks only - # top-bottom is supported, the other stretchings stop at the abort of the - # solver. + # Stretching in y is specific to the pdp (010) solver. The CUDA backend + # registers every stretching, the OpenMP backend only top-bottom on one + # rank. On 2 ranks only top-bottom is supported on CUDA, the other + # stretchings stop at the abort of the solver. if(${backend} STREQUAL cuda) foreach(stretch bottom centred top-bottom) set(X3D2_TEST_NAME_SUFFIX _pdp_${stretch}) @@ -509,6 +509,9 @@ function(define_backend_tests backend) FAIL_REGULAR_EXPRESSION "must never be reached" TIMEOUT 60) endforeach() + elseif(is_omp) + set(X3D2_TEST_NAME_SUFFIX _pdp_top-bottom) + define_verification_test(verification/test_poisson_bc.f90 1 ${backend} --bc pdp --stretch top-bottom) endif() # Validated 2 rank runs of the same test: the suffix keeps their names apart set(X3D2_TEST_NAME_SUFFIX _dpp) diff --git a/tests/verification/test_poisson_bc.f90 b/tests/verification/test_poisson_bc.f90 index cea885b4b6..989ada94f9 100644 --- a/tests/verification/test_poisson_bc.f90 +++ b/tests/verification/test_poisson_bc.f90 @@ -73,12 +73,13 @@ program test_poisson !! --dump-matrix rank 0 writes the stretching matrices of !! each stretched case to x3d2_.dump !! (the format of the Incompact3d dumps) - !! The stretched cases need the CUDA backend for BC code pdp. Top-bottom - !! stretching runs on 1 or 2 ranks, centred and bottom need 1 rank. The - !! other BC codes with stretching in y, and every stretched case on the - !! OpenMP backend, end in the abort of the solver. On a stretched case Check - !! 1 is printed and not judged, because cos(n*pi*y) is not an eigenfunction - !! of the stretched discrete operator. + !! The stretched cases need the CUDA backend for BC code pdp, except + !! top-bottom stretching, which also runs on one rank with the OpenMP + !! backend. Top-bottom stretching runs on 1 or 2 ranks on CUDA, centred and + !! bottom need 1 rank. The other BC codes with stretching in y, and the + !! centred and bottom cases on the OpenMP backend, end in the abort of the + !! solver. On a stretched case Check 1 is printed and not judged, because + !! cos(n*pi*y) is not an eigenfunction of the stretched discrete operator. !! A run with one case per process is what a multi rank ctest entry needs, !! because with the OpenMP backend 2decomp can be initialised only once per !! process and some cases still stop on more than one rank. What the solver From 2600fccb266c1afbf514290d464f1ae4385d0064 Mon Sep 17 00:00:00 2001 From: kaanolgu Date: Fri, 20 Nov 2026 14:25:30 +0000 Subject: [PATCH 3/6] Build the stretching matrices on the y-pencil for the OpenMP 010 case on many ranks --- src/backend/omp/poisson_fft.f90 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/backend/omp/poisson_fft.f90 b/src/backend/omp/poisson_fft.f90 index 723d4885ae..538cbbdc6b 100644 --- a/src/backend/omp/poisson_fft.f90 +++ b/src/backend/omp/poisson_fft.f90 @@ -203,8 +203,17 @@ subroutine init_omp(self, mesh, xdirps, ydirps, zdirps, lowmem) ! decomp_2d_fft_get_size hands back. Read sp directly, subtracting one ! to convert the start position into an offset. self%sp => decomp_2d_fft_get_sp() - call self%base_init(mesh, xdirps, ydirps, zdirps, & - self%sp%zsz, self%sp%zst - 1) + if (mesh%geo%stretched(2) .and. self%bc_code == '010' & + .and. self%p_col > 1) then + ! The stretched solve runs on the y-pencil, where y is whole, so the + ! stretching matrices must cover that block. + call self%base_init(mesh, xdirps, ydirps, zdirps, & + self%sp%zsz, self%sp%zst - 1, & + n_blk=self%sp%ysz, blk_st=self%sp%yst - 1) + else + call self%base_init(mesh, xdirps, ydirps, zdirps, & + self%sp%zsz, self%sp%zst - 1) + end if ! Only the top-bottom stretching on one rank is solved if (mesh%geo%stretched(2) .and. & From 6485e47f8af43f3202b309cf093df00b5e3b9d9c Mon Sep 17 00:00:00 2001 From: kaanolgu Date: Fri, 20 Nov 2026 14:25:30 +0000 Subject: [PATCH 4/6] Solve the top-bottom stretched 010 case in the OpenMP Poisson solver on many ranks --- src/backend/omp/poisson_fft.f90 | 128 ++++++++++++++----------- tests/CMakeLists.txt | 8 +- tests/verification/test_poisson_bc.f90 | 12 +-- 3 files changed, 85 insertions(+), 63 deletions(-) diff --git a/src/backend/omp/poisson_fft.f90 b/src/backend/omp/poisson_fft.f90 index 538cbbdc6b..a06971885c 100644 --- a/src/backend/omp/poisson_fft.f90 +++ b/src/backend/omp/poisson_fft.f90 @@ -215,12 +215,12 @@ subroutine init_omp(self, mesh, xdirps, ydirps, zdirps, lowmem) self%sp%zsz, self%sp%zst - 1) end if - ! Only the top-bottom stretching on one rank is solved + ! Only the top-bottom stretching is solved. A y decomposition stops in + ! init_buffers_010. if (mesh%geo%stretched(2) .and. & - (trim(mesh%geo%stretching(2)) /= 'top-bottom' & - .or. self%p_row*self%p_col > 1)) then + trim(mesh%geo%stretching(2)) /= 'top-bottom') then call abort_run('OpenMP backends FFT based Poisson solver supports only & - &top-bottom y stretching on one rank!') + &top-bottom y stretching!') end if allocate (self%c_x(self%nx_spec, self%ny_spec, self%nz_spec)) @@ -646,58 +646,23 @@ subroutine fft_postprocess_010_omp(self) ! The FFT leaves y split across p_col in the z-pencil. Hop to the ! y-pencil, where y is whole, pair there, and hop back. call transpose_z_to_y(self%c_x, self%c_pair, self%sp) - call process_spectral_010( & - div_u=self%c_pair, waves=self%waves_pair, & - nx_spec=self%sp%ysz(1), ny_spec=self%sp%ysz(2), & - nz_spec=self%sp%ysz(3), & - x_sp_st=self%sp%yst(1) - 1, y_sp_st=self%sp%yst(2) - 1, & - z_sp_st=self%sp%yst(3) - 1, & - nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob, & - ax=self%ax, bx=self%bx, ay=self%ay, by=self%by, & - az=self%az, bz=self%bz & - ) + if (self%stretched_y) then + call solve_stretched_010(self, self%c_pair, self%sp%yst - 1) + else + call process_spectral_010( & + div_u=self%c_pair, waves=self%waves_pair, & + nx_spec=self%sp%ysz(1), ny_spec=self%sp%ysz(2), & + nz_spec=self%sp%ysz(3), & + x_sp_st=self%sp%yst(1) - 1, y_sp_st=self%sp%yst(2) - 1, & + z_sp_st=self%sp%yst(3) - 1, & + nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob, & + ax=self%ax, bx=self%bx, ay=self%ay, by=self%by, & + az=self%az, bz=self%bz & + ) + end if call transpose_y_to_z(self%c_pair, self%c_x, self%sp) else if (self%stretched_y) then - call process_spectral_010_fw( & - div_u=self%c_x, & - nx_spec=self%nx_spec, ny_spec=self%ny_spec, nz_spec=self%nz_spec, & - x_sp_st=self%sp_st(1), y_sp_st=self%sp_st(2), & - z_sp_st=self%sp_st(3), & - nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob, & - ax=self%ax, bx=self%bx, ay=self%ay, by=self%by, & - az=self%az, bz=self%bz & - ) - - ! The odd rows, then the even rows, each with its own matrices - self%a_work_re = self%a_odd_re - self%a_work_im = self%a_odd_im - call process_spectral_010_poisson( & - div_u=self%c_x, a_re=self%a_work_re, a_im=self%a_work_im, & - off=0, inc=2, nx_spec=self%nx_spec, n=self%ny_spec/2, & - nz_spec=self%nz_spec, x_sp_st=self%sp_st(1), & - z_sp_st=self%sp_st(3), & - nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob & - ) - - self%a_work_re = self%a_even_re - self%a_work_im = self%a_even_im - call process_spectral_010_poisson( & - div_u=self%c_x, a_re=self%a_work_re, a_im=self%a_work_im, & - off=1, inc=2, nx_spec=self%nx_spec, n=self%ny_spec/2, & - nz_spec=self%nz_spec, x_sp_st=self%sp_st(1), & - z_sp_st=self%sp_st(3), & - nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob & - ) - - call process_spectral_010_bw( & - div_u=self%c_x, & - nx_spec=self%nx_spec, ny_spec=self%ny_spec, nz_spec=self%nz_spec, & - x_sp_st=self%sp_st(1), y_sp_st=self%sp_st(2), & - z_sp_st=self%sp_st(3), & - nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob, & - ax=self%ax, bx=self%bx, ay=self%ay, by=self%by, & - az=self%az, bz=self%bz & - ) + call solve_stretched_010(self, self%c_x, self%sp_st) else call process_spectral_010( & div_u=self%c_x, waves=self%waves, & @@ -712,6 +677,61 @@ subroutine fft_postprocess_010_omp(self) end subroutine fft_postprocess_010_omp + subroutine solve_stretched_010(self, c, st) + !! Top-bottom stretched 010 solve of a spectral block c, whose y lines + !! are whole. st holds the offset of the block in the global spectral + !! grid, and the stretching matrices cover the same block. + implicit none + + class(omp_poisson_010_t) :: self + complex(dp), intent(inout), dimension(:, :, :) :: c + integer, intent(in) :: st(3) + + integer :: nx_b, ny_b, nz_b + + nx_b = size(c, dim=1) + ny_b = size(c, dim=2) + nz_b = size(c, dim=3) + + call process_spectral_010_fw( & + div_u=c, & + nx_spec=nx_b, ny_spec=ny_b, nz_spec=nz_b, & + x_sp_st=st(1), y_sp_st=st(2), z_sp_st=st(3), & + nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob, & + ax=self%ax, bx=self%bx, ay=self%ay, by=self%by, & + az=self%az, bz=self%bz & + ) + + ! The odd rows, then the even rows, each with its own matrices + self%a_work_re = self%a_odd_re + self%a_work_im = self%a_odd_im + call process_spectral_010_poisson( & + div_u=c, a_re=self%a_work_re, a_im=self%a_work_im, & + off=0, inc=2, nx_spec=nx_b, n=ny_b/2, nz_spec=nz_b, & + x_sp_st=st(1), z_sp_st=st(3), & + nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob & + ) + + self%a_work_re = self%a_even_re + self%a_work_im = self%a_even_im + call process_spectral_010_poisson( & + div_u=c, a_re=self%a_work_re, a_im=self%a_work_im, & + off=1, inc=2, nx_spec=nx_b, n=ny_b/2, nz_spec=nz_b, & + x_sp_st=st(1), z_sp_st=st(3), & + nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob & + ) + + call process_spectral_010_bw( & + div_u=c, & + nx_spec=nx_b, ny_spec=ny_b, nz_spec=nz_b, & + x_sp_st=st(1), y_sp_st=st(2), z_sp_st=st(3), & + nx=self%nx_glob, ny=self%ny_glob, nz=self%nz_glob, & + ax=self%ax, bx=self%bx, ay=self%ay, by=self%by, & + az=self%az, bz=self%bz & + ) + + end subroutine solve_stretched_010 + subroutine fft_postprocess_100_omp(self) !! Post-process div U* in spectral space for the non-periodic x case. !! diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a1a16edacb..35a162d5d8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -478,9 +478,9 @@ function(define_backend_tests backend) define_verification_test(verification/test_poisson_bc.f90 1 ${backend} --bc ${bc}) endforeach() # Stretching in y is specific to the pdp (010) solver. The CUDA backend - # registers every stretching, the OpenMP backend only top-bottom on one - # rank. On 2 ranks only top-bottom is supported on CUDA, the other - # stretchings stop at the abort of the solver. + # registers every stretching, the OpenMP backend only top-bottom. On 2 + # ranks only top-bottom is supported, the other stretchings stop at the + # abort of the solver. if(${backend} STREQUAL cuda) foreach(stretch bottom centred top-bottom) set(X3D2_TEST_NAME_SUFFIX _pdp_${stretch}) @@ -512,6 +512,8 @@ function(define_backend_tests backend) elseif(is_omp) set(X3D2_TEST_NAME_SUFFIX _pdp_top-bottom) define_verification_test(verification/test_poisson_bc.f90 1 ${backend} --bc pdp --stretch top-bottom) + set(X3D2_TEST_NAME_SUFFIX _pdp_top-bottom) + define_verification_test(verification/test_poisson_bc.f90 2 ${backend} --bc pdp --stretch top-bottom) endif() # Validated 2 rank runs of the same test: the suffix keeps their names apart set(X3D2_TEST_NAME_SUFFIX _dpp) diff --git a/tests/verification/test_poisson_bc.f90 b/tests/verification/test_poisson_bc.f90 index 989ada94f9..3cba2a07fa 100644 --- a/tests/verification/test_poisson_bc.f90 +++ b/tests/verification/test_poisson_bc.f90 @@ -74,12 +74,12 @@ program test_poisson !! each stretched case to x3d2_.dump !! (the format of the Incompact3d dumps) !! The stretched cases need the CUDA backend for BC code pdp, except - !! top-bottom stretching, which also runs on one rank with the OpenMP - !! backend. Top-bottom stretching runs on 1 or 2 ranks on CUDA, centred and - !! bottom need 1 rank. The other BC codes with stretching in y, and the - !! centred and bottom cases on the OpenMP backend, end in the abort of the - !! solver. On a stretched case Check 1 is printed and not judged, because - !! cos(n*pi*y) is not an eigenfunction of the stretched discrete operator. + !! top-bottom stretching, which also runs with the OpenMP backend. Top-bottom + !! stretching runs on 1 or 2 ranks, centred and bottom need 1 rank on CUDA. + !! The other BC codes with stretching in y, and the centred and bottom cases + !! on the OpenMP backend, end in the abort of the solver. On a stretched case + !! Check 1 is printed and not judged, because cos(n*pi*y) is not an + !! eigenfunction of the stretched discrete operator. !! A run with one case per process is what a multi rank ctest entry needs, !! because with the OpenMP backend 2decomp can be initialised only once per !! process and some cases still stop on more than one rank. What the solver From 11305dc715a651974399e6982899855f643feb99 Mon Sep 17 00:00:00 2001 From: kaanolgu Date: Fri, 20 Nov 2026 14:25:30 +0000 Subject: [PATCH 5/6] Share the forward and backward steps between the uniform and stretched OpenMP 010 kernels --- .../omp/kernels/spectral_processing.f90 | 129 ++---------------- 1 file changed, 8 insertions(+), 121 deletions(-) diff --git a/src/backend/omp/kernels/spectral_processing.f90 b/src/backend/omp/kernels/spectral_processing.f90 index f03913dde6..0d6829238a 100644 --- a/src/backend/omp/kernels/spectral_processing.f90 +++ b/src/backend/omp/kernels/spectral_processing.f90 @@ -133,69 +133,12 @@ subroutine process_spectral_010( & !> Global cell size integer, intent(in) :: nx, ny, nz - integer :: i, j, k, ix, iy, iz, iy_r - real(dp) :: tmp_r, tmp_c, div_r, div_c, l_r, l_c, r_r, r_c - - !$omp parallel do private(div_r, div_c, ix, iz, tmp_r, tmp_c) collapse(3) - do k = 1, nz_spec - do j = 1, ny_spec - do i = 1, nx_spec - ix = i + x_sp_st - iz = k + z_sp_st - - ! normalisation - div_r = real(div_u(i, j, k), kind=dp)/nx/ny/nz - div_c = aimag(div_u(i, j, k))/nx/ny/nz - - ! postprocess in z - tmp_r = div_r - tmp_c = div_c - div_r = tmp_r*bz(iz) + tmp_c*az(iz) - div_c = tmp_c*bz(iz) - tmp_r*az(iz) - if (iz > nz/2 + 1) div_r = -div_r - if (iz > nz/2 + 1) div_c = -div_c - - ! postprocess in x - tmp_r = div_r - tmp_c = div_c - div_r = tmp_r*bx(ix) + tmp_c*ax(ix) - div_c = tmp_c*bx(ix) - tmp_r*ax(ix) - if (ix > nx/2 + 1) div_r = -div_r - if (ix > nx/2 + 1) div_c = -div_c - - ! update the entry - div_u(i, j, k) = cmplx(div_r, div_c, kind=dp) - end do - end do - end do - !$omp end parallel do - - !$omp parallel do private(div_r, div_c, iy, iy_r, l_r, l_c, r_r, r_c) collapse(3) - do k = 1, nz_spec - do j = 2, ny_spec/2 + 1 - do i = 1, nx_spec - iy = j + y_sp_st - iy_r = ny_spec - j + 2 + y_sp_st - - l_r = real(div_u(i, j, k), kind=dp) - l_c = aimag(div_u(i, j, k)) - r_r = real(div_u(i, ny_spec - j + 2, k), kind=dp) - r_c = aimag(div_u(i, ny_spec - j + 2, k)) + integer :: i, j, k + real(dp) :: tmp_r, tmp_c, div_r, div_c - ! update the entry - div_u(i, j, k) = 0.5_dp*cmplx( & !& - l_r*by(iy) + l_c*ay(iy) + r_r*by(iy) - r_c*ay(iy), & - -l_r*ay(iy) + l_c*by(iy) + r_r*ay(iy) + r_c*by(iy), kind=dp & - ) - div_u(i, ny_spec - j + 2, k) = 0.5_dp*cmplx( & !& - r_r*by(iy_r) + r_c*ay(iy_r) + l_r*by(iy_r) - l_c*ay(iy_r), & - -r_r*ay(iy_r) + r_c*by(iy_r) + l_r*ay(iy_r) + l_c*by(iy_r), & - kind=dp & - ) - end do - end do - end do - !$omp end parallel do + call process_spectral_010_fw( & + div_u, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, & + nx, ny, nz, ax, bx, ay, by, az, bz) ! Solve Poisson !$omp parallel do private(div_r, div_c, tmp_r, tmp_c) collapse(3) @@ -228,65 +171,9 @@ subroutine process_spectral_010( & !$omp end parallel do ! post-process backward - !$omp parallel do private(div_r, div_c, iy, iy_r, l_r, l_c, r_r, r_c) collapse(3) - do k = 1, nz_spec - do j = 2, ny_spec/2 + 1 - do i = 1, nx_spec - iy = j + y_sp_st - iy_r = ny_spec - j + 2 + y_sp_st - - l_r = real(div_u(i, j, k), kind=dp) - l_c = aimag(div_u(i, j, k)) - r_r = real(div_u(i, ny_spec - j + 2, k), kind=dp) - r_c = aimag(div_u(i, ny_spec - j + 2, k)) - - ! update the entry - div_u(i, j, k) = cmplx( & !& - l_r*by(iy) - l_c*ay(iy) + r_r*ay(iy) + r_c*by(iy), & - l_r*ay(iy) + l_c*by(iy) - r_r*by(iy) + r_c*ay(iy), kind=dp & - ) - div_u(i, ny_spec - j + 2, k) = cmplx( & !& - r_r*by(iy_r) - r_c*ay(iy_r) + l_r*ay(iy_r) + l_c*by(iy_r), & - r_r*ay(iy_r) + r_c*by(iy_r) - l_r*by(iy_r) + l_c*ay(iy_r), & - kind=dp & - ) - end do - end do - end do - !$omp end parallel do - - !$omp parallel do private(div_r, div_c, ix, iz, tmp_r, tmp_c) collapse(3) - do k = 1, nz_spec - do j = 1, ny_spec - do i = 1, nx_spec - ix = i + x_sp_st - iz = k + z_sp_st - - div_r = real(div_u(i, j, k), kind=dp) - div_c = aimag(div_u(i, j, k)) - - ! post-process in z - tmp_r = div_r - tmp_c = div_c - div_r = tmp_r*bz(iz) - tmp_c*az(iz) - div_c = tmp_c*bz(iz) + tmp_r*az(iz) - if (iz > nz/2 + 1) div_r = -div_r - if (iz > nz/2 + 1) div_c = -div_c - - ! post-process in x - tmp_r = div_r - tmp_c = div_c - div_r = tmp_r*bx(ix) - tmp_c*ax(ix) - div_c = tmp_c*bx(ix) + tmp_r*ax(ix) - if (ix > nx/2 + 1) div_r = -div_r - if (ix > nx/2 + 1) div_c = -div_c - - ! update the entry - div_u(i, j, k) = cmplx(div_r, div_c, kind=dp) - end do - end do - end do - !$omp end parallel do + call process_spectral_010_bw( & + div_u, nx_spec, ny_spec, nz_spec, x_sp_st, y_sp_st, z_sp_st, & + nx, ny, nz, ax, bx, ay, by, az, bz) end subroutine process_spectral_010 From cc1708d0f6e7171abcfea61cd5497172e37c026e Mon Sep 17 00:00:00 2001 From: kaanolgu Date: Fri, 20 Nov 2026 14:25:30 +0000 Subject: [PATCH 6/6] Register the top-bottom stretched channel smoke case for the OpenMP backend --- tests/CMakeLists.txt | 9 +++++++-- tests/regression/channel_smoke_golden_omp.csv | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/regression/channel_smoke_golden_omp.csv diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 35a162d5d8..72b6a50dd8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -892,11 +892,16 @@ endif() if(WITH_2DECOMPFFT AND _cpu_omp_backend) define_regression_case_test(tgv_smoke tgv_smoke.x3d tgv_smoke_golden_omp.csv 2 omp ${_smoke_cmp_args}) + # The OpenMP backend runs only top-bottom y stretching in the FFT solver, so + # only the top-bottom channel case is registered for it. Its golden is blessed + # from a double precision gfortran build. + define_regression_case_test(channel_smoke channel_smoke.x3d + channel_smoke_golden_omp.csv 1 omp ${_channel_cmp_args}) endif() # CUDA regression cases. The channel cases run the 010 FFT Poisson solver with -# a stretched y grid, one case per stretching operator. Only the CUDA backend -# supports y stretching in the FFT solver, and on more than one rank only +# a stretched y grid, one case per stretching operator. The CUDA backend +# supports every y stretching in the FFT solver, and on more than one rank only # top-bottom stretching. The channel cases run on one rank. Their goldens # are blessed from a double precision NVHPC build, and a single precision # build checks against them too. diff --git a/tests/regression/channel_smoke_golden_omp.csv b/tests/regression/channel_smoke_golden_omp.csv new file mode 100644 index 0000000000..b0bbe91efb --- /dev/null +++ b/tests/regression/channel_smoke_golden_omp.csv @@ -0,0 +1,12 @@ +# time, kinetic_energy, enstrophy, div_u_max, div_u_mean + 0.000000000000E+00, 1.434099531354E-01, 1.125117171042E+00, 1.270508443971E-15, 1.885379076064E-16 + 2.500000000000E-02, 1.434179543730E-01, 1.125325259152E+00, 1.102476481864E-16, 1.102476481809E-16 + 5.000000000000E-02, 1.434179574820E-01, 1.125316100652E+00, 1.126857053985E-16, 1.126857053955E-16 + 7.500000000000E-02, 1.434179605162E-01, 1.125308034781E+00, 2.536787400961E-16, 2.536787400880E-16 + 1.000000000000E-01, 1.434179634725E-01, 1.125300909082E+00, 5.919657621542E-17, 5.919657621057E-17 + 1.250000000000E-01, 1.434179663495E-01, 1.125294595000E+00, 7.497284555003E-17, 7.497284554811E-17 + 1.500000000000E-01, 1.434179691468E-01, 1.125288983746E+00, 2.893319067445E-17, 2.893319066583E-17 + 1.750000000000E-01, 1.434179718651E-01, 1.125283982950E+00, 8.594926620483E-17, 8.594926620222E-17 + 2.000000000000E-01, 1.434179745055E-01, 1.125279513919E+00, 3.551219643961E-18, 3.551219639801E-18 + 2.250000000000E-01, 1.434179770699E-01, 1.125275509397E+00, 1.981397450081E-17, 1.981397449527E-17 + 2.500000000000E-01, 1.434179795604E-01, 1.125271911727E+00, 9.236348607947E-17, 9.236348607577E-17