Skip to content

Commit 9cbdd68

Browse files
committed
Make grid buffer handling stage-independent
1 parent b6bf034 commit 9cbdd68

4 files changed

Lines changed: 159 additions & 58 deletions

File tree

src/common/m_boundary_common.fpp

Lines changed: 86 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -342,70 +342,68 @@ contains
342342

343343
!> Populate the buffers of the grid variables, which are constituted of the cell-boundary locations and cell-width
344344
!! distributions, based on the boundary conditions.
345-
subroutine s_populate_grid_variables_buffers
346-
347-
#ifdef MFC_SIMULATION
348-
! Simulation allocates the cell-boundary arrays with buff_size ghost layers, so the
349-
! ghost extrapolation extends over the full buffer. In post-process the module-level
350-
! offset_x/y/z (which the x_cb/y_cb/z_cb allocations are sized to) are used instead.
351-
type(int_bounds_info) :: offset_x, offset_y, offset_z
352-
353-
offset_x%beg = buff_size; offset_x%end = buff_size
354-
offset_y%beg = buff_size; offset_y%end = buff_size
355-
offset_z%beg = buff_size; offset_z%end = buff_size
356-
357-
! Global domain bounds
345+
subroutine s_populate_grid_variables_buffers(x_cb_in, x_cc_in, dx_in, x_offset, y_offset, z_offset, y_cb_in, y_cc_in, dy_in, &
346+
& z_cb_in, z_cc_in, dz_in, global_bounds)
347+
348+
type(int_bounds_info), intent(in) :: x_offset, y_offset, z_offset
349+
real(wp), intent(inout) :: x_cb_in(-1 - x_offset%beg:)
350+
real(wp), intent(inout) :: x_cc_in(-buff_size:), dx_in(-buff_size:)
351+
real(wp), optional, intent(inout) :: y_cb_in(-1 - y_offset%beg:), z_cb_in(-1 - z_offset%beg:)
352+
real(wp), optional, intent(inout) :: y_cc_in(-buff_size:), dy_in(-buff_size:)
353+
real(wp), optional, intent(inout) :: z_cc_in(-buff_size:), dz_in(-buff_size:)
354+
type(bounds_info), optional, dimension(3), intent(out) :: global_bounds
355+
356+
if (present(global_bounds)) then
358357
#ifdef MFC_MPI
359-
call s_mpi_allreduce_min(x_cb(-1), glb_bounds(1)%beg)
360-
call s_mpi_allreduce_max(x_cb(m), glb_bounds(1)%end)
361-
if (n > 0) then
362-
call s_mpi_allreduce_min(y_cb(-1), glb_bounds(2)%beg)
363-
call s_mpi_allreduce_max(y_cb(n), glb_bounds(2)%end)
364-
if (p > 0) then
365-
call s_mpi_allreduce_min(z_cb(-1), glb_bounds(3)%beg)
366-
call s_mpi_allreduce_max(z_cb(p), glb_bounds(3)%end)
358+
call s_mpi_allreduce_min(x_cb_in(-1), global_bounds(1)%beg)
359+
call s_mpi_allreduce_max(x_cb_in(m), global_bounds(1)%end)
360+
if (n > 0) then
361+
call s_mpi_allreduce_min(y_cb_in(-1), global_bounds(2)%beg)
362+
call s_mpi_allreduce_max(y_cb_in(n), global_bounds(2)%end)
363+
if (p > 0) then
364+
call s_mpi_allreduce_min(z_cb_in(-1), global_bounds(3)%beg)
365+
call s_mpi_allreduce_max(z_cb_in(p), global_bounds(3)%end)
366+
end if
367367
end if
368-
end if
369368
#else
370-
glb_bounds(1)%beg = x_cb(-1); glb_bounds(1)%end = x_cb(m)
371-
if (n > 0) then
372-
glb_bounds(2)%beg = y_cb(-1); glb_bounds(2)%end = y_cb(n)
373-
if (p > 0) then
374-
glb_bounds(3)%beg = z_cb(-1); glb_bounds(3)%end = z_cb(p)
369+
global_bounds(1)%beg = x_cb_in(-1); global_bounds(1)%end = x_cb_in(m)
370+
if (n > 0) then
371+
global_bounds(2)%beg = y_cb_in(-1); global_bounds(2)%end = y_cb_in(n)
372+
if (p > 0) then
373+
global_bounds(3)%beg = z_cb_in(-1); global_bounds(3)%end = z_cb_in(p)
374+
end if
375375
end if
376-
end if
377-
#endif
378-
$:GPU_UPDATE(device='[glb_bounds]')
379376
#endif
377+
end if
380378

381-
#ifndef MFC_PRE_PROCESS
382-
call s_populate_grid_bc_direction(1, -1, bc_x, offset_x)
383-
call s_populate_grid_bc_direction(1, 1, bc_x, offset_x)
379+
call s_populate_grid_bc_direction(x_cb_in, x_cc_in, dx_in, m, 1, -1, bc_x, x_offset)
380+
call s_populate_grid_bc_direction(x_cb_in, x_cc_in, dx_in, m, 1, 1, bc_x, x_offset)
384381

385382
if (n == 0) return
386383

387384
#:if not MFC_CASE_OPTIMIZATION or num_dims > 1
388-
call s_populate_grid_bc_direction(2, -1, bc_y, offset_y)
389-
call s_populate_grid_bc_direction(2, 1, bc_y, offset_y)
385+
call s_populate_grid_bc_direction(y_cb_in, y_cc_in, dy_in, n, 2, -1, bc_y, y_offset)
386+
call s_populate_grid_bc_direction(y_cb_in, y_cc_in, dy_in, n, 2, 1, bc_y, y_offset)
390387
#:endif
391388

392389
if (p == 0) return
393390

394391
#:if not MFC_CASE_OPTIMIZATION or num_dims > 2
395-
call s_populate_grid_bc_direction(3, -1, bc_z, offset_z)
396-
call s_populate_grid_bc_direction(3, 1, bc_z, offset_z)
392+
call s_populate_grid_bc_direction(z_cb_in, z_cc_in, dz_in, p, 3, -1, bc_z, z_offset)
393+
call s_populate_grid_bc_direction(z_cb_in, z_cc_in, dz_in, p, 3, 1, bc_z, z_offset)
397394
#:endif
398-
#endif
399395

400396
end subroutine s_populate_grid_variables_buffers
401397

402-
#ifndef MFC_PRE_PROCESS
403-
!> Populate grid variable buffers (cell widths and centers) for one direction and location.
404-
subroutine s_populate_grid_bc_direction(bc_dir, bc_loc, bc_bounds, offset_dir)
398+
!> Populate cell-boundary, cell-center, and cell-width buffers for one coordinate direction.
399+
subroutine s_populate_grid_bc_direction(cell_boundaries, cell_centers, cell_widths, num_cells, bc_dir, bc_loc, bc_bounds, &
400+
& offset)
405401

406-
integer, intent(in) :: bc_dir, bc_loc
407-
type(int_bounds_info), intent(in) :: bc_bounds, offset_dir
408-
integer :: bc_edge
402+
integer, intent(in) :: num_cells, bc_dir, bc_loc
403+
type(int_bounds_info), intent(in) :: bc_bounds, offset
404+
real(wp), intent(inout) :: cell_boundaries(-1 - offset%beg:)
405+
real(wp), intent(inout) :: cell_centers(-buff_size:), cell_widths(-buff_size:)
406+
integer :: bc_edge, i, source_index
409407

410408
if (bc_loc == -1) then
411409
bc_edge = bc_bounds%beg
@@ -414,23 +412,55 @@ contains
414412
end if
415413

416414
if (bc_edge >= 0) then
417-
call s_mpi_sendrecv_grid_variables_buffers(bc_dir, bc_loc, offset_dir)
415+
call s_mpi_sendrecv_grid_variable_buffer(cell_boundaries, cell_centers, cell_widths, num_cells, bc_bounds, bc_loc, &
416+
& offset)
418417
return
419418
end if
420419

421-
select case (bc_edge)
422-
case (BC_PERIODIC)
423-
call s_grid_periodic_bc(bc_dir, bc_loc, offset_dir)
424-
case (BC_REFLECTIVE)
425-
call s_grid_reflective_bc(bc_dir, bc_loc, offset_dir)
426-
case (BC_AXIS)
427-
if (bc_dir == 2) call s_grid_axis_bc(bc_loc, offset_dir)
428-
case default
429-
call s_grid_ghost_cell_extrapolation_bc(bc_dir, bc_loc, offset_dir)
430-
end select
420+
if (bc_edge == BC_AXIS .and. (bc_dir /= 2 .or. bc_loc == 1)) return
421+
422+
do i = 1, buff_size
423+
if (bc_loc == -1) then
424+
select case (bc_edge)
425+
case (BC_PERIODIC)
426+
source_index = num_cells - i + 1
427+
case (BC_REFLECTIVE, BC_AXIS)
428+
source_index = i - 1
429+
case default
430+
source_index = 0
431+
end select
432+
cell_widths(-i) = cell_widths(source_index)
433+
else
434+
select case (bc_edge)
435+
case (BC_PERIODIC)
436+
source_index = i - 1
437+
case (BC_REFLECTIVE)
438+
source_index = num_cells - i + 1
439+
case default
440+
source_index = num_cells
441+
end select
442+
cell_widths(num_cells + i) = cell_widths(source_index)
443+
end if
444+
end do
445+
446+
if (bc_loc == -1) then
447+
do i = 1, offset%beg
448+
cell_boundaries(-1 - i) = cell_boundaries(-i) - cell_widths(-i)
449+
end do
450+
do i = 1, buff_size
451+
cell_centers(-i) = cell_centers(1 - i) - (cell_widths(1 - i) + cell_widths(-i))/2._wp
452+
end do
453+
else
454+
do i = 1, offset%end
455+
cell_boundaries(num_cells + i) = cell_boundaries(num_cells + i - 1) + cell_widths(num_cells + i)
456+
end do
457+
do i = 1, buff_size
458+
cell_centers(num_cells + i) = cell_centers(num_cells + i - 1) + (cell_widths(num_cells + i - 1) &
459+
& + cell_widths(num_cells + i))/2._wp
460+
end do
461+
end if
431462

432463
end subroutine s_populate_grid_bc_direction
433-
#endif
434464

435465
!> Deallocate boundary condition buffer arrays allocated during module initialization.
436466
subroutine s_finalize_boundary_common_module()

src/common/m_mpi_common.fpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,55 @@ contains
17691769
!> The goal of this procedure is to populate the buffers of the grid variables by communicating with the neighboring processors.
17701770
!! Note that only the buffers of the cell-width distributions are handled in such a way. This is because the buffers of
17711771
!! cell-boundary locations may be calculated directly from those of the cell-width distributions.
1772+
subroutine s_mpi_sendrecv_grid_variable_buffer(cell_boundaries, cell_centers, cell_widths, num_cells, bc_bounds, pbc_loc, &
1773+
& offset)
1774+
1775+
integer, intent(in) :: num_cells, pbc_loc
1776+
type(int_bounds_info), intent(in) :: bc_bounds, offset
1777+
real(wp), intent(inout) :: cell_boundaries(-1 - offset%beg:)
1778+
real(wp), intent(inout) :: cell_centers(-buff_size:)
1779+
real(wp), intent(inout) :: cell_widths(-buff_size:)
1780+
1781+
#ifdef MFC_MPI
1782+
integer :: ierr
1783+
integer :: i
1784+
1785+
if (pbc_loc == -1) then
1786+
if (bc_bounds%end >= 0) then
1787+
call MPI_SENDRECV(cell_widths(num_cells - buff_size + 1), buff_size, mpi_p, bc_bounds%end, 0, &
1788+
& cell_widths(-buff_size), buff_size, mpi_p, bc_bounds%beg, 0, MPI_COMM_WORLD, &
1789+
& MPI_STATUS_IGNORE, ierr)
1790+
else
1791+
call MPI_SENDRECV(cell_widths(0), buff_size, mpi_p, bc_bounds%beg, 1, cell_widths(-buff_size), buff_size, mpi_p, &
1792+
& bc_bounds%beg, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE, ierr)
1793+
end if
1794+
do i = 1, offset%beg
1795+
cell_boundaries(-1 - i) = cell_boundaries(-i) - cell_widths(-i)
1796+
end do
1797+
do i = 1, buff_size
1798+
cell_centers(-i) = cell_centers(1 - i) - (cell_widths(1 - i) + cell_widths(-i))/2._wp
1799+
end do
1800+
else
1801+
if (bc_bounds%beg >= 0) then
1802+
call MPI_SENDRECV(cell_widths(0), buff_size, mpi_p, bc_bounds%beg, 1, cell_widths(num_cells + 1), buff_size, &
1803+
& mpi_p, bc_bounds%end, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE, ierr)
1804+
else
1805+
call MPI_SENDRECV(cell_widths(num_cells - buff_size + 1), buff_size, mpi_p, bc_bounds%end, 0, &
1806+
& cell_widths(num_cells + 1), buff_size, mpi_p, bc_bounds%end, 1, MPI_COMM_WORLD, &
1807+
& MPI_STATUS_IGNORE, ierr)
1808+
end if
1809+
do i = 1, offset%end
1810+
cell_boundaries(num_cells + i) = cell_boundaries(num_cells + i - 1) + cell_widths(num_cells + i)
1811+
end do
1812+
do i = 1, buff_size
1813+
cell_centers(num_cells + i) = cell_centers(num_cells + i - 1) + (cell_widths(num_cells + i - 1) &
1814+
& + cell_widths(num_cells + i))/2._wp
1815+
end do
1816+
end if
1817+
#endif
1818+
1819+
end subroutine s_mpi_sendrecv_grid_variable_buffer
1820+
17721821
#ifndef MFC_PRE_PROCESS
17731822
subroutine s_mpi_sendrecv_grid_variables_buffers(mpi_dir, pbc_loc, offset)
17741823

src/post_process/m_start_up.fpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,13 @@ contains
162162
if (chemistry) call s_compute_q_T_sf(q_T_sf, q_cons_vf, idwint)
163163
164164
if (buff_size > 0) then
165-
call s_populate_grid_variables_buffers()
165+
if (n == 0) then
166+
call s_populate_grid_variables_buffers(x_cb, x_cc, dx, offset_x, offset_y, offset_z)
167+
else if (p == 0) then
168+
call s_populate_grid_variables_buffers(x_cb, x_cc, dx, offset_x, offset_y, offset_z, y_cb, y_cc, dy)
169+
else
170+
call s_populate_grid_variables_buffers(x_cb, x_cc, dx, offset_x, offset_y, offset_z, y_cb, y_cc, dy, z_cb, z_cc, dz)
171+
end if
166172
call s_populate_variables_buffers(bc_type, q_cons_vf, q_T_sf=q_T_sf)
167173
end if
168174

src/simulation/m_start_up.fpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,23 @@ contains
870870
call s_read_data_files(q_cons_ts(1)%vf)
871871
end if
872872

873-
call s_populate_grid_variables_buffers()
873+
block
874+
type(int_bounds_info), dimension(3) :: grid_offsets
875+
876+
grid_offsets(:)%beg = buff_size
877+
grid_offsets(:)%end = buff_size
878+
if (n == 0) then
879+
call s_populate_grid_variables_buffers(x_cb, x_cc, dx, grid_offsets(1), grid_offsets(2), grid_offsets(3), &
880+
& global_bounds=glb_bounds)
881+
else if (p == 0) then
882+
call s_populate_grid_variables_buffers(x_cb, x_cc, dx, grid_offsets(1), grid_offsets(2), grid_offsets(3), y_cb, &
883+
& y_cc, dy, global_bounds=glb_bounds)
884+
else
885+
call s_populate_grid_variables_buffers(x_cb, x_cc, dx, grid_offsets(1), grid_offsets(2), grid_offsets(3), y_cb, &
886+
& y_cc, dy, z_cb, z_cc, dz, glb_bounds)
887+
end if
888+
end block
889+
$:GPU_UPDATE(device='[glb_bounds]')
874890

875891
if (model_eqns == model_eqns_6eq) call s_initialize_internal_energy_equations(q_cons_ts(1)%vf)
876892
if (ib) then

0 commit comments

Comments
 (0)