diff --git a/src/common/m_constants.fpp b/src/common/m_constants.fpp index 7cc8f7b7bd..308b668973 100644 --- a/src/common/m_constants.fpp +++ b/src/common/m_constants.fpp @@ -31,7 +31,7 @@ module m_constants !> Reals per IB in restart_data/ib_state: time, force(3), torque(3), vel(3), angular_vel(3), angles(3), centroid(3), radius, !! Twall, ib_mass_relax bank. Twall carries a thermal_bc = 3 body's evolving temperature across a restart; the bank is the - !! body's unreturned gas mass (ib_mass_res(1), identical on every rank). + !! body's unreturned gas mass (patch_ib%mass_bank, held by the owner). integer, parameter :: ib_state_nfields = 22 !> Radius cutoff to avoid division by zero for 3D spherical harmonic patch (geometry 14) real(wp), parameter :: small_radius = 1.e-32_wp diff --git a/src/common/m_derived_types.fpp b/src/common/m_derived_types.fpp index 3d965a8654..a620897e2a 100644 --- a/src/common/m_derived_types.fpp +++ b/src/common/m_derived_types.fpp @@ -422,8 +422,9 @@ module m_derived_types real(wp) :: mass, moment !< mass and moment of inertia of object used to compute forces in 2-way coupling real(wp), dimension(1:3) :: force, torque !< vectors for the computed force and torque values applied to an IB !> Last step's gasification rate and face mass out [kg/s], and fluid heat in [W] (ib_heat_wrt; per unit depth in 2D) - real(wp) :: mdot, mdot_face, qdot - real(wp) :: mass_bank !< ib_mass_relax bank read from a restart; only s_read_ib_restart_data sets it + real(wp) :: mdot, mdot_face, qdot + !> ib_mass_relax bank: set from a restart, advanced by the owner rank, moved with the body at a handoff + real(wp) :: mass_bank real(wp), dimension(1:3) :: vel real(wp), dimension(1:3) :: step_vel !< velocity array used to store intermediate steps in the time_stepper module real(wp), dimension(1:3) :: angular_vel diff --git a/src/simulation/m_data_output.fpp b/src/simulation/m_data_output.fpp index bdabd03acb..a93f483947 100644 --- a/src/simulation/m_data_output.fpp +++ b/src/simulation/m_data_output.fpp @@ -1191,7 +1191,7 @@ contains write (file_unit) num_local_ibs do i = 1, num_local_ibs ib_idx = local_ib_patch_ids(i) - call s_pack_ib_state(patch_ib(ib_idx), mytime, ib_buf, f_ib_mass_bank(patch_ib(ib_idx)%gbl_patch_id)) + call s_pack_ib_state(patch_ib(ib_idx), mytime, ib_buf, patch_ib(ib_idx)%mass_bank) write (file_unit) patch_ib(ib_idx)%gbl_patch_id write (file_unit) ib_buf @@ -1218,7 +1218,7 @@ contains do i = 1, num_local_ibs ib_idx = local_ib_patch_ids(i) - call s_pack_ib_state(patch_ib(ib_idx), mytime, ib_buf, f_ib_mass_bank(patch_ib(ib_idx)%gbl_patch_id)) + call s_pack_ib_state(patch_ib(ib_idx), mytime, ib_buf, patch_ib(ib_idx)%mass_bank) ! Global IB index determines position in file disp = int(patch_ib(ib_idx)%gbl_patch_id - 1, MPI_OFFSET_KIND)*int(ib_state_nfields, MPI_OFFSET_KIND)*WP_MOK @@ -1249,7 +1249,7 @@ contains if (ios /= 0) call s_mpi_abort('Cannot open IB state output file: ' // trim(file_loc)) do i = 1, num_ibs - call s_pack_ib_state(patch_ib(i), mytime, ib_buf, f_ib_mass_bank(patch_ib(i)%gbl_patch_id)) + call s_pack_ib_state(patch_ib(i), mytime, ib_buf, patch_ib(i)%mass_bank) write (file_unit) ib_buf end do @@ -1447,9 +1447,6 @@ contains integer, intent(in) :: time_step $:GPU_UPDATE(host='[patch_ib(1:num_ibs)]') - if (ib_mass_relax > 0) then - $:GPU_UPDATE(host='[ib_mass_res]') - end if if (parallel_io) then call s_write_parallel_ib_state(time_step) diff --git a/src/simulation/m_ibm.fpp b/src/simulation/m_ibm.fpp index 1e2fc566c7..06d0855081 100644 --- a/src/simulation/m_ibm.fpp +++ b/src/simulation/m_ibm.fpp @@ -83,8 +83,8 @@ module m_ibm & s_write_ib_surface, s_ib_flux_wall_prepare, s_update_ib_temperatures, s_ibm_alloc_fine, s_ibm_setup_fine, & & s_ibm_swap_to_fine, s_ibm_restore_from_fine, s_ibm_load_fine_markers, s_ibm_reset_fine_park, s_ibm_note_fine_memory, & & num_gps, f_ibm_fine_gps, s_ibm_tally_gps_by_body, s_ibm_check_bodies_on_one_rank, s_conserve_ib_mass, & - & s_add_swept_cells, s_fill_uncovered_cells, f_ib_mass_bank, ib_mass_res, s_ibm_set_conduction_temperature, & - & s_ibm_restore_conduction_temperature, s_write_ib_conduction_temperature, s_read_ib_conduction_temperature + & s_add_swept_cells, s_fill_uncovered_cells, s_ibm_set_conduction_temperature, s_ibm_restore_conduction_temperature, & + & s_write_ib_conduction_temperature, s_read_ib_conduction_temperature !> Ghost points at which the reacting-surface Newton solve did not reach its tolerance, so the point fell back to a chemically !! inert wall (keeping a prescribed Twall). Counted because that fallback is otherwise indistinguishable from a surface @@ -165,29 +165,31 @@ module m_ibm logical :: ib_mass_only = .false. !< only the mass is accumulated, from fixed bodies without AMR (s_accumulate_ib_mass) $:GPU_DECLARE(create='[record_surface]') - !> Per global IB, for the thermal_bc = 3 update and ib_heat_wrt: (1) surface area and (4) gasification rate, from gp_surf; (2) - !! energy into the solid and (3) mass out of it, both summed over the faces between the body's cells and fluid cells with the RK - !! stage weights, so over a step they are exactly the energy and mass the fluid exchanged with the body; (5) the gas mass of the - !! body's ib_mass_relax shell. One allreduce takes all five. + !> Per patch_ib slot (the bodies this rank holds, not global ids), for the thermal_bc = 3 update, ib_heat_wrt and ib_mass_relax: + !! (1) surface area and (4) gasification rate, from gp_surf; (2) energy into the solid and (3) mass out of it, both summed over + !! the faces between the body's cells and fluid cells with the RK stage weights, so over a step they are exactly the energy and + !! mass the fluid exchanged with the body; (5) the gas mass of the body's ib_mass_relax shell; (6) a copy of the body's bank + !! (patch_ib%mass_bank) and (7) the share of it released this step, for the multi-rank exchange. Rows 1:5 are this rank's + !! partial sums until s_ib_exchange_heat adds them over the ranks that hold the body, through its owner. + integer, parameter :: ib_nheat = 7 real(wp), allocatable, dimension(:,:) :: ib_heat $:GPU_DECLARE(create='[ib_heat]') - !> Per global IB and shard: (1) energy and (2) mass from the face fluxes and (3) shell mass of ib_mass_relax, before they are - !! summed into ib_heat(2:3) and ib_heat(5). Every cell of a body's surface adds to the same few sums, and on AMD a device atomic - !! on a double is a compare-and-swap loop that serializes by address, so the adds are spread over ib_nshard sums. + !> Per patch_ib slot and shard: (1) energy and (2) mass from the face fluxes and (3) shell mass of ib_mass_relax, before they + !! are summed into ib_heat(2:3) and ib_heat(5). Every cell of a body's surface adds to the same few sums, and on AMD a device + !! atomic on a double is a compare-and-swap loop that serializes by address, so the adds are spread over ib_nshard sums. real(wp), allocatable, dimension(:,:,:) :: ib_part integer, parameter :: ib_nshard = 32 !< a power of two (f_ib_shard) $:GPU_DECLARE(create='[ib_part]') - !> ib_mass_relax, per global IB: (1) gas mass gained from the body beyond its gasification and not yet returned, (2) the share - !! released this step. ib_shell: global IB id of each fluid cell within ib_shell_width cells of a body, else 0. ib_markers_prev: - !! markers at the last placement of a moving body (s_add_swept_cells). - real(wp), allocatable, dimension(:,:) :: ib_mass_res - type(integer_field) :: ib_shell, ib_markers_prev - integer, parameter :: ib_shell_width = 2 - $:GPU_DECLARE(create='[ib_mass_res, ib_shell, ib_markers_prev]') - - integer :: num_gps !< Number of ghost points + !> ib_mass_relax: the bank of a body (the gas mass it gained beyond its gasification and not yet returned) is + !! patch_ib%mass_bank, held by the owner. ib_shell: patch_ib slot of each fluid cell within ib_shell_width cells of a body, else + !! 0 (valid until the step's handoff). ib_markers_prev: markers at the last placement of a moving body (s_add_swept_cells). + type(integer_field) :: ib_shell, ib_markers_prev + integer, parameter :: ib_shell_width = 2 + $:GPU_DECLARE(create='[ib_shell, ib_markers_prev]') + integer :: ib_slot_cap = 0 !< slots of ib_heat and ib_part: a bound on the bodies one rank holds + integer :: num_gps !< Number of ghost points #if defined(MFC_OpenACC) $:GPU_DECLARE(create='[gp_layers, num_gps]') #elif defined(MFC_OpenMP) @@ -202,11 +204,13 @@ module m_ibm logical :: centroid_offsets_active = .false. !< some patch is a moving airfoil or STL, which carries a centroid_offset ! IB force reduction (s_communicate_ib_forces): distinct neighborhood ranks in ascending order, and buffers allocated once - integer, parameter :: ib_rec_len = 7 !< gbl_patch_id, force(1:3), torque(1:3) + integer, parameter :: ib_rec_len = 7 !< gbl_patch_id, force(1:3), torque(1:3) integer :: n_ib_nbrs = 0 integer, allocatable :: ib_nbrs(:), ib_send_counts(:), ib_reqs(:), ib_stats(:,:) + integer, allocatable :: ib_reply_slots(:,:), ib_reply_counts(:) !< s_ib_exchange_heat: bodies each neighbor sent partials of real(wp), allocatable :: ib_send_bufs(:,:), ib_recv_bufs(:,:) - private :: ib_rec_len, n_ib_nbrs, ib_nbrs, ib_send_counts, ib_reqs, ib_stats, ib_send_bufs, ib_recv_bufs + private :: ib_rec_len, n_ib_nbrs, ib_nbrs, ib_send_counts, ib_reqs, ib_stats, ib_send_bufs, ib_recv_bufs, ib_reply_slots, & + & ib_reply_counts !> Force (1:3) and torque (4:6) on each local IB from the fluid-body face fluxes of the last RHS evaluation real(wp), allocatable, dimension(:,:) :: ib_face_ft @@ -364,20 +368,15 @@ contains $:GPU_UPDATE(device='[gp_surf]') end if if (ib_exchange) then - @:ALLOCATE(ib_heat(5, num_gbl_ibs), ib_part(3, ib_nshard, num_gbl_ibs)) + ! One slot per body this rank holds (patch_ib slots), with room for the count to change as bodies move between ranks + call s_mpi_allreduce_max(real(num_ibs, wp), max_num_gps_rank) + ib_slot_cap = int(max(1_8, min(int(num_gbl_ibs, 8), 2_8*int(max_num_gps_rank, 8) + 64_8))) + @:ALLOCATE(ib_heat(ib_nheat, ib_slot_cap), ib_part(3, ib_nshard, ib_slot_cap)) ib_heat = 0._wp ib_part = 0._wp $:GPU_UPDATE(device='[ib_heat, ib_part]') end if if (ib_mass_relax > 0) then - @:ALLOCATE(ib_mass_res(2, num_gbl_ibs)) - ! The bank of each body, from the restart record of the rank that owns it (zeros elsewhere), shared by all ranks - ib_mass_res = 0._wp - do i = 1, num_local_ibs - ib_mass_res(1, patch_ib(i)%gbl_patch_id) = patch_ib(i)%mass_bank - end do - call s_mpi_allreduce_vectors_sum(ib_mass_res, ib_mass_res, 2, num_gbl_ibs) - $:GPU_UPDATE(device='[ib_mass_res]') @:ALLOCATE(ib_shell%sf(0:m, 0:n, 0:p)) @:ACC_SETUP_SFs(ib_shell) end if @@ -1544,22 +1543,22 @@ contains type(scalar_field), dimension(sys_size), intent(in) :: q_cons_vf real(wp), parameter :: sigma_sb = 5.670374419e-8_wp !< Stefan-Boltzmann constant [W/m^2/K^4] real(wp), parameter :: T_ref = 298.15_wp - real(wp) :: q_in - integer :: i, g + real(wp) :: q_in, bank, rel + integer :: i, g, n_relax ! Area and gasification rate from the surface records of the last stage - $:GPU_PARALLEL_LOOP(private='[g]') - do g = 1, num_gbl_ibs - ib_heat(1, g) = 0._wp - ib_heat(4, g) = 0._wp + $:GPU_PARALLEL_LOOP(private='[i]') + do i = 1, num_ibs + ib_heat(1, i) = 0._wp + ib_heat(4, i) = 0._wp end do $:END_GPU_PARALLEL_LOOP() if (num_gps > 0) then $:GPU_PARALLEL_LOOP(private='[i, g]') do i = 1, num_gps if (gp_surf(1, i) > 0._wp) then - g = patch_ib(ghost_points(i)%ib_patch_id)%gbl_patch_id + g = ghost_points(i)%ib_patch_id $:GPU_ATOMIC(atomic='update') ib_heat(1, g) = ib_heat(1, g) + gp_surf(1, i) $:GPU_ATOMIC(atomic='update') @@ -1570,7 +1569,7 @@ contains end if if (ib_mass_relax > 0) call s_ib_shell_mass(q_cons_vf) $:GPU_PARALLEL_LOOP(private='[g, i]') - do g = 1, num_gbl_ibs + do g = 1, num_ibs ib_heat(5, g) = 0._wp do i = 1, ib_nshard ib_heat(2, g) = ib_heat(2, g) + ib_part(1, i, g) @@ -1580,28 +1579,35 @@ contains ib_part(2, i, g) = 0._wp ib_part(3, i, g) = 0._wp end do + ib_heat(6, g) = patch_ib(g)%mass_bank end do $:END_GPU_PARALLEL_LOOP() - ! A host round trip is a device sync, so one rank, whose allreduce is the identity, skips it + ! A host round trip is a device sync, so one rank, which holds every body, skips it if (num_procs > 1) then - $:GPU_UPDATE(host='[ib_heat]') - call s_mpi_allreduce_vectors_sum(ib_heat, ib_heat, 5, num_gbl_ibs) - $:GPU_UPDATE(device='[ib_heat]') + $:GPU_UPDATE(host='[ib_heat(1:ib_nheat, 1:num_ibs)]') + call s_ib_exchange_heat(dt_step) + $:GPU_UPDATE(device='[ib_heat(1:ib_nheat, 1:num_ibs)]') + if (ib_mass_relax > 0) then + $:GPU_PARALLEL_LOOP(private='[i]') + do i = 1, num_ibs + patch_ib(i)%mass_bank = ib_heat(6, i) + end do + $:END_GPU_PARALLEL_LOOP() + end if end if ! On the device, where patch_ib is current (moving IBs update it there). Twall advances only for the bodies this rank holds, ! which stays consistent because thermal_bc = 3 requires moving_ibm = 0 (case_validator) - $:GPU_PARALLEL_LOOP(private='[i, g, q_in]', copyin='[dt_step]') + $:GPU_PARALLEL_LOOP(private='[i, q_in]', copyin='[dt_step]') do i = 1, num_ibs - g = patch_ib(i)%gbl_patch_id - q_in = ib_heat(2, g) - if (patch_ib(i)%thermal_bc == 3) q_in = q_in + patch_ib(i)%cp_solid*(patch_ib(i)%Twall - T_ref)*ib_heat(4, g) - patch_ib(i)%mdot = ib_heat(4, g) - patch_ib(i)%mdot_face = ib_heat(3, g) + q_in = ib_heat(2, i) + if (patch_ib(i)%thermal_bc == 3) q_in = q_in + patch_ib(i)%cp_solid*(patch_ib(i)%Twall - T_ref)*ib_heat(4, i) + patch_ib(i)%mdot = ib_heat(4, i) + patch_ib(i)%mdot_face = ib_heat(3, i) patch_ib(i)%qdot = q_in if (patch_ib(i)%thermal_bc == 3) then q_in = q_in + patch_ib(i)%heat_power - patch_ib(i)%emissivity*sigma_sb*ib_heat(1, & - & g)*(patch_ib(i)%Twall**4 - patch_ib(i)%T_rad**4) + & i)*(patch_ib(i)%Twall**4 - patch_ib(i)%T_rad**4) patch_ib(i)%Twall = min(max(patch_ib(i)%Twall & & + dt_step*q_in/(patch_ib(i)%rho_solid*patch_ib(i)%cp_solid*f_ib_volume(patch_ib(i))), T_surface_min), & & T_surface_max) @@ -1609,37 +1615,45 @@ contains end do $:END_GPU_PARALLEL_LOOP() - ! ib_mass_relax: the gas should gain exactly the gasified mass; bank the rest and release 1/ib_mass_relax of the bank - if (ib_mass_relax > 0) then - $:GPU_PARALLEL_LOOP(private='[g]', copyin='[dt_step]') - do g = 1, num_gbl_ibs - ib_mass_res(1, g) = ib_mass_res(1, g) + (ib_heat(3, g) - ib_heat(4, g))*dt_step - ib_mass_res(2, g) = ib_mass_res(1, g)/real(ib_mass_relax, wp) - ib_mass_res(1, g) = ib_mass_res(1, g) - ib_mass_res(2, g) + ! ib_mass_relax with one rank (several: s_ib_exchange_heat): the gas should gain exactly the gasified mass; bank the rest + ! and release 1/ib_mass_relax of the bank + if (ib_mass_relax > 0 .and. num_procs == 1) then + n_relax = ib_mass_relax + $:GPU_PARALLEL_LOOP(private='[i, bank, rel]', copyin='[dt_step, n_relax]') + do i = 1, num_ibs + bank = patch_ib(i)%mass_bank + call s_ib_bank_step(bank, rel, (ib_heat(3, i) - ib_heat(4, i))*dt_step, n_relax) + patch_ib(i)%mass_bank = bank + ib_heat(7, i) = rel end do $:END_GPU_PARALLEL_LOOP() end if ! The next step accumulates afresh - $:GPU_PARALLEL_LOOP(private='[g]') - do g = 1, num_gbl_ibs - ib_heat(2, g) = 0._wp - ib_heat(3, g) = 0._wp + $:GPU_PARALLEL_LOOP(private='[i]') + do i = 1, num_ibs + ib_heat(2, i) = 0._wp + ib_heat(3, i) = 0._wp end do $:END_GPU_PARALLEL_LOOP() end subroutine s_update_ib_temperatures - !> The ib_mass_relax bank of global IB g (0 without ib_mass_relax), for the restart record. Reads the host copy. - impure function f_ib_mass_bank(g) result(bank) + !> One step of an ib_mass_relax bank: add the mass gained beyond gasification, dm, and release 1/ib_mass_relax of the sum. + pure subroutine s_ib_bank_step(bank, rel, dm, n_relax) + + $:GPU_ROUTINE(parallelism='[seq]') - integer, intent(in) :: g - real(wp) :: bank + real(wp), intent(inout) :: bank + real(wp), intent(out) :: rel + real(wp), intent(in) :: dm + integer, intent(in) :: n_relax !< ib_mass_relax, passed because a device routine does not see the global - bank = 0._wp - if (ib_mass_relax > 0) bank = ib_mass_res(1, g) + bank = bank + dm + rel = bank/real(n_relax, wp) + bank = bank - rel - end function f_ib_mass_bank + end subroutine s_ib_bank_step !> ib_mass_relax: tag the fluid cells within ib_shell_width cells of each body (ib_shell) and add their gas mass to ib_part(3) !! (summed into ib_heat(5) by s_update_ib_temperatures). A cell within reach of two bodies is assigned to the first one found. @@ -1670,7 +1684,7 @@ contains if (mk /= 0) then call s_decode_patch_periodicity(mk, ib_tmp, xp, yp, zp) call s_get_neighborhood_idx(ib_tmp, ib_id) - if (ib_id > 0) g = patch_ib(ib_id)%gbl_patch_id + g = max(ib_id, 0) ! always assigned: a conditional store left g at 0 on NVHPC GPU end if end if ib_shell%sf(j, k, l) = g @@ -1710,7 +1724,7 @@ contains g = ib_shell%sf(j, k, l) if (g > 0) then if (ib_heat(5, g) > 0._wp) then - f = 1._wp - ib_mass_res(2, g)/ib_heat(5, g) + f = 1._wp - ib_heat(7, g)/ib_heat(5, g) do i = eqn_idx%cont%beg, eqn_idx%E q_cons_vf(i)%sf(j, k, l) = f*q_cons_vf(i)%sf(j, k, l) end do @@ -1780,7 +1794,7 @@ contains end do vol = dx(j)*dy(k) if (num_dims == 3) vol = vol*dz(l) - g = patch_ib(ib_id)%gbl_patch_id + g = ib_id $:GPU_ATOMIC(atomic='update') ib_heat(2, g) = ib_heat(2, g) + real(sgn, wp)*e_b*vol/dt_step $:GPU_ATOMIC(atomic='update') @@ -3028,7 +3042,7 @@ contains if (ib_id > 0) then area = f_ib_face_area(idir, j, k, l) f_m = f_ib_face_mass(hatR, j, k, l) - g = patch_ib(ib_id)%gbl_patch_id + g = ib_id sh = f_ib_shard(j, k, l) $:GPU_ATOMIC(atomic='update') ib_part(2, sh, g) = ib_part(2, sh, g) - real(side, wp)*w*area*f_m @@ -3208,7 +3222,7 @@ contains end do f_E = f_E + 0.5_wp*(vb(1)**2 + vb(2)**2 + vb(3)**2)*f_m end if - g = patch_ib(ib_id)%gbl_patch_id + g = ib_id sh = f_ib_shard(j, k, l) $:GPU_ATOMIC(atomic='update') ib_part(1, sh, g) = ib_part(1, sh, g) + real(side, wp)*w*area*f_E @@ -3529,6 +3543,7 @@ contains ! an owner never receives more records than it owns, and a broadcast never carries more than its sender owns max_recs = min(size(patch_ib), num_local_ibs_max) allocate (ib_send_counts(n_ib_nbrs), ib_reqs(2*n_ib_nbrs), ib_stats(MPI_STATUS_SIZE, 2*n_ib_nbrs)) + allocate (ib_reply_slots(max_recs, max(n_ib_nbrs, 1)), ib_reply_counts(max(n_ib_nbrs, 1))) allocate (ib_send_bufs(ib_rec_len*max_recs, max(n_ib_nbrs, 1)), ib_recv_bufs(ib_rec_len*max_recs, max(n_ib_nbrs, 1))) #endif @@ -3547,6 +3562,140 @@ contains end function f_ib_nbr_slot + !> Adds ib_heat(1:5) (the host copy: this rank's partial sums per slot) over the ranks that hold each body, through its owner + !! (patch_ib%owner_rank), and, with ib_mass_relax, steps the owners' banks. Phase 1: each rank sends its nonzero partials of + !! bodies it does not own to the owner, which adds them in ascending source-rank order. Phase 2: each owner returns the totals + !! and the bank's release (row 7) to the ranks that sent, the only ranks with cells of the body. Both are point-to-point with + !! neighborhood ranks and carry only bodies a rank shares, so the volume follows the bodies on rank boundaries, not the body + !! count. + impure subroutine s_ib_exchange_heat(dt_step) + + real(wp), intent(in) :: dt_step + +#ifdef MFC_MPI + integer, parameter :: nf = 6 !< gbl_patch_id, ib_heat(1:5) + integer :: i, j, s, r, slot, pos, nvals, ierr + + if (n_ib_nbrs > 0) then + do s = 1, n_ib_nbrs + call MPI_IRECV(ib_recv_bufs(:,s), size(ib_recv_bufs, 1), mpi_p, ib_nbrs(s), 612, MPI_COMM_WORLD, ib_reqs(s), ierr) + end do + ib_send_counts = 0 + do i = 1, num_ibs + if (patch_ib(i)%owner_rank == proc_rank) cycle + if (all(ib_heat(1:5,i) == 0._wp)) cycle + slot = f_ib_nbr_slot(patch_ib(i)%owner_rank) + pos = nf*ib_send_counts(slot) + ib_send_bufs(pos + 1, slot) = real(patch_ib(i)%gbl_patch_id, wp) + ib_send_bufs(pos + 2:pos + nf,slot) = ib_heat(1:5,i) + ib_send_counts(slot) = ib_send_counts(slot) + 1 + end do + do s = 1, n_ib_nbrs + call MPI_ISEND(ib_send_bufs(:,s), nf*ib_send_counts(s), mpi_p, ib_nbrs(s), 612, MPI_COMM_WORLD, & + & ib_reqs(n_ib_nbrs + s), ierr) + end do + call MPI_WAITALL(2*n_ib_nbrs, ib_reqs, ib_stats, ierr) + + ! own partial first, then each neighbor in ascending rank order, whatever order the messages arrived in + ib_reply_counts = 0 + do s = 1, n_ib_nbrs + call MPI_GET_COUNT(ib_stats(:,s), mpi_p, nvals, ierr) + do r = 0, nvals/nf - 1 + pos = nf*r + call s_get_neighborhood_idx(nint(ib_recv_bufs(pos + 1, s)), j) + @:ASSERT(j > 0, 'IB partial for an IB this rank does not hold') + @:ASSERT(patch_ib(j)%owner_rank == proc_rank, 'IB partial for an IB this rank does not own') + ib_heat(1:5,j) = ib_heat(1:5,j) + ib_recv_bufs(pos + 2:pos + nf,s) + ib_reply_counts(s) = ib_reply_counts(s) + 1 + ib_reply_slots(ib_reply_counts(s), s) = j + end do + end do + end if + + ib_heat(7,1:num_ibs) = 0._wp + if (ib_mass_relax > 0) then + do i = 1, num_local_ibs + j = local_ib_patch_ids(i) + call s_ib_bank_step(ib_heat(6, j), ib_heat(7, j), (ib_heat(3, j) - ib_heat(4, j))*dt_step, ib_mass_relax) + end do + end if + if (n_ib_nbrs == 0) return + + do s = 1, n_ib_nbrs + ib_reqs(s) = MPI_REQUEST_NULL + ib_reqs(n_ib_nbrs + s) = MPI_REQUEST_NULL + if (ib_send_counts(s) > 0) then + call MPI_IRECV(ib_recv_bufs(:,s), size(ib_recv_bufs, 1), mpi_p, ib_nbrs(s), 613, MPI_COMM_WORLD, ib_reqs(s), ierr) + end if + if (ib_reply_counts(s) == 0) cycle + do r = 0, ib_reply_counts(s) - 1 + j = ib_reply_slots(r + 1, s) + pos = ib_rec_len*r + ib_send_bufs(pos + 1, s) = real(patch_ib(j)%gbl_patch_id, wp) + ib_send_bufs(pos + 2:pos + 6,s) = ib_heat(1:5,j) + ib_send_bufs(pos + 7, s) = ib_heat(7, j) + end do + call MPI_ISEND(ib_send_bufs(:,s), ib_rec_len*ib_reply_counts(s), mpi_p, ib_nbrs(s), 613, MPI_COMM_WORLD, & + & ib_reqs(n_ib_nbrs + s), ierr) + end do + call MPI_WAITALL(2*n_ib_nbrs, ib_reqs, ib_stats, ierr) + + do s = 1, n_ib_nbrs + if (ib_send_counts(s) == 0) cycle + call MPI_GET_COUNT(ib_stats(:,s), mpi_p, nvals, ierr) + do r = 0, nvals/ib_rec_len - 1 + pos = ib_rec_len*r + call s_get_neighborhood_idx(nint(ib_recv_bufs(pos + 1, s)), j) + @:ASSERT(j > 0, 'IB total for an IB this rank does not hold') + ib_heat(1:5,j) = ib_recv_bufs(pos + 2:pos + 6,s) + ib_heat(7, j) = ib_recv_bufs(pos + 7, s) + end do + end do +#endif + + end subroutine s_ib_exchange_heat + + !> At a handoff, move the ib_mass_relax banks of the bodies that changed owner from the old owner to the new one. The new owner + !! knows which neighbors to expect from (gain_from); the old owner queued its records in s_handoff_ib_ownership. + impure subroutine s_ib_migrate_banks(n_gain, gain_gid, gain_from, num_entries) + + integer, intent(in) :: n_gain, gain_gid(:), gain_from(:), num_entries + +#ifdef MFC_MPI + integer :: s, r, j, nvals, ierr, n_exp(max(n_ib_nbrs, 1)) + + if (n_ib_nbrs == 0) return + n_exp = 0 + do r = 1, n_gain + s = f_ib_nbr_slot(gain_from(r)) + n_exp(s) = n_exp(s) + 1 + end do + do s = 1, n_ib_nbrs + ib_reqs(s) = MPI_REQUEST_NULL + ib_reqs(n_ib_nbrs + s) = MPI_REQUEST_NULL + if (n_exp(s) > 0) then + call MPI_IRECV(ib_recv_bufs(:,s), 2*n_exp(s), mpi_p, ib_nbrs(s), 620, MPI_COMM_WORLD, ib_reqs(s), ierr) + end if + if (ib_send_counts(s) > 0) then + call MPI_ISEND(ib_send_bufs(:,s), 2*ib_send_counts(s), mpi_p, ib_nbrs(s), 620, MPI_COMM_WORLD, & + & ib_reqs(n_ib_nbrs + s), ierr) + end if + end do + call MPI_WAITALL(2*n_ib_nbrs, ib_reqs, ib_stats, ierr) + do s = 1, n_ib_nbrs + if (n_exp(s) == 0) cycle + call MPI_GET_COUNT(ib_stats(:,s), mpi_p, nvals, ierr) + @:ASSERT(nvals == 2*n_exp(s), 'IB bank handoff: record count mismatch') + do r = 0, n_exp(s) - 1 + call s_get_neighborhood_idx(nint(ib_recv_bufs(2*r + 1, s)), j, num_entries) + @:ASSERT(j > 0, 'IB bank handoff for an IB this rank does not hold') + patch_ib(j)%mass_bank = ib_recv_bufs(2*r + 2, s) + end do + end do +#endif + + end subroutine s_ib_migrate_banks + !> Reduces forces and torques so every rank holding an IB ends with bit-identical values. Each IB has one owner, the rank whose !! subdomain holds its centroid (patch_ib%owner_rank, stamped at handoff). Phase 1: each rank sends its nonzero partials to the !! owner, which adds them to its own partial in ascending source-rank order once all have arrived. Phase 2: each owner sends its @@ -3650,6 +3799,8 @@ contains integer :: max_nbrs character(len=1), allocatable :: send_buf(:), recv_bufs(:,:) integer, allocatable :: requests(:), recv_neighbor_list(:) + integer :: n_gain, slot + integer, allocatable :: gain_gid(:), gain_from(:) #ifdef MFC_MPI call nvtxStartRange("HANDOFF-IB-OWNERSHIP") @@ -3670,6 +3821,8 @@ contains num_ibs_old = num_ibs output_idx = 0 local_output_idx = 0 + n_gain = 0 + allocate (gain_gid(num_local_ibs_max), gain_from(num_local_ibs_max)) do i = 1, num_ibs old_to_new(i) = -1 centroid = [patch_ib(i)%x_centroid, patch_ib(i)%y_centroid, 0._wp] @@ -3689,6 +3842,12 @@ contains @:PROHIBIT(local_output_idx > num_local_ibs_max, & & "Too many IBs on a single processor rank. Modify case file or increase limit of num_local_ibs_max to resolve.") local_ib_patch_ids(local_output_idx) = output_idx + if (patch_ib(output_idx)%owner_rank /= proc_rank) then + ! gained from a neighbor, which sends the ib_mass_relax bank (s_ib_migrate_banks) + n_gain = n_gain + 1 + gain_gid(n_gain) = patch_ib(output_idx)%gbl_patch_id + gain_from(n_gain) = patch_ib(output_idx)%owner_rank + end if patch_ib(output_idx)%owner_rank = proc_rank end if end if @@ -3773,6 +3932,7 @@ contains ! Unpack all received buffers num_ibs_pre = num_ibs + ib_send_counts = 0 do nbr_idx = 1, max_nbrs if (recv_neighbor_list(nbr_idx) == MPI_PROC_NULL) cycle unpack_pos = 0 @@ -3795,7 +3955,14 @@ contains & "Owned plus neighboring IBs on a rank exceed num_local_ibs_max, which sizes ib_box and ib_face_ft. Use more ranks or increase num_local_ibs_max.") patch_ib(num_ibs) = tmp_patch else - ! an IB we already hold changed owner: record the new owner, keep our own copy of its state + ! an IB we already hold changed owner: record the new owner, keep our own copy of its state; the old + ! owner queues its bank for the new one + if (ib_mass_relax > 0 .and. patch_ib(j)%owner_rank == proc_rank) then + slot = f_ib_nbr_slot(tmp_patch%owner_rank) + ib_send_counts(slot) = ib_send_counts(slot) + 1 + ib_send_bufs(2*ib_send_counts(slot) - 1, slot) = real(tmp_patch%gbl_patch_id, wp) + ib_send_bufs(2*ib_send_counts(slot), slot) = patch_ib(j)%mass_bank + end if patch_ib(j)%owner_rank = tmp_patch%owner_rank end if end do @@ -3803,8 +3970,12 @@ contains @:PROHIBIT(num_ibs > num_gbl_ibs, "IB handoff duplicated a patch: num_ibs exceeds num_gbl_ibs.") deallocate (send_buf, recv_bufs, requests, recv_neighbor_list) + if (ib_mass_relax > 0) call s_ib_migrate_banks(n_gain, gain_gid, gain_from, num_ibs_pre) + deallocate (gain_gid, gain_from) $:GPU_UPDATE(device='[patch_ib(1:num_ibs), num_ibs]') call s_merge_ib_lookup(num_ibs_pre) + @:PROHIBIT(ib_slot_cap > 0 .and. num_ibs > ib_slot_cap, & + & "A rank holds more IBs than its ib_heat slots; clustering of IBs exceeds the 2x margin set at start-up") end if call nvtxEndRange() @@ -4217,8 +4388,8 @@ contains if (allocated(ib_heat)) then @:DEALLOCATE(ib_heat, ib_part) end if - if (allocated(ib_mass_res)) then - @:DEALLOCATE(ib_mass_res, ib_shell%sf) + if (associated(ib_shell%sf)) then + @:DEALLOCATE(ib_shell%sf) end if if (associated(ib_markers_prev%sf)) then @:DEALLOCATE(ib_markers_prev%sf) @@ -4226,7 +4397,8 @@ contains call s_finalize_ib_patches() if (collision_model > 0) call s_finalize_collisions_module() #ifdef MFC_MPI - if (allocated(ib_nbrs)) deallocate (ib_nbrs, ib_send_counts, ib_reqs, ib_stats, ib_send_bufs, ib_recv_bufs) + if (allocated(ib_nbrs)) deallocate (ib_nbrs, ib_send_counts, ib_reqs, ib_stats, ib_send_bufs, ib_recv_bufs, & + & ib_reply_slots, ib_reply_counts) #endif end subroutine s_finalize_ibm_module diff --git a/src/simulation/m_time_steppers.fpp b/src/simulation/m_time_steppers.fpp index d871ce56b7..c2bd8bf474 100644 --- a/src/simulation/m_time_steppers.fpp +++ b/src/simulation/m_time_steppers.fpp @@ -663,14 +663,15 @@ contains if (amr) call s_amr_step_fold(q_cons_ts(1)%vf) if (ib) then + ! Before the handoff: ib_heat and ib_shell are indexed by the patch_ib slots the stages used + if (ib_exchange) call s_update_ib_temperatures(dt, q_cons_ts(1)%vf) + if (ib_mass_relax > 0) call s_conserve_ib_mass(q_cons_ts(1)%vf) if (moving_immersed_boundary_flag) then call s_wrap_periodic_ibs() ! wraps the positions of IBs to the local proc call s_handoff_ib_ownership() ! recomputes which ranks own which IBs and communicate to neighbors else if (ib_state_wrt) then call s_compute_ib_forces() end if - if (ib_exchange) call s_update_ib_temperatures(dt, q_cons_ts(1)%vf) - if (ib_mass_relax > 0) call s_conserve_ib_mass(q_cons_ts(1)%vf) end if ! Adaptive dt: final stage