From 84f5244168629e4261b678bce71df0345a6be66d Mon Sep 17 00:00:00 2001 From: Daniel Vickers Date: Tue, 10 Nov 2026 11:50:48 -0800 Subject: [PATCH 1/4] Optimizied dense-particle suspension collision detection by offloading de-duplication step to GPU --- src/simulation/m_collisions.fpp | 157 +++++++++++++++++++++++++++++++- 1 file changed, 156 insertions(+), 1 deletion(-) diff --git a/src/simulation/m_collisions.fpp b/src/simulation/m_collisions.fpp index f5e41fee51..9c376c48c6 100644 --- a/src/simulation/m_collisions.fpp +++ b/src/simulation/m_collisions.fpp @@ -32,6 +32,18 @@ module m_collisions !> true when any IB-IB or IB-wall contact was detected on this rank since the last adaptive-dt computation logical :: collisions_active + !> One bit per unordered pair (a < b) of this rank's IBs (local + neighborhood indices, a triangular index over + !! num_local_ibs_max): the ghost-point candidate pass claims each pair with an atomic OR, so only the first thread to find a + !! pair records it and duplicates are dropped on the GPU. Bits are cleared after the forces are applied, so the set is empty + !! between calls. num_local_ibs_max = 30000 gives 4.5e8 bits (56 MB). + integer, allocatable, dimension(:) :: pair_bits + $:GPU_DECLARE(create='[pair_bits]') + !> MFC_IB_PAIR_DEDUP=host selects the original serial host deduplication (kept for comparison); anything else uses the GPU + logical :: host_dedup + !> wall-clock seconds spent in IB pair detection on this rank, and the number of calls, reported at finalize + real(kind(0d0)) :: detect_seconds + integer :: detect_calls + contains subroutine s_initialize_collisions_module() @@ -46,6 +58,20 @@ contains @:ALLOCATE(collision_lookup(num_local_ibs_max * 27 * 8, 4)) @:ALLOCATE(wall_overlap_distances(num_local_ibs_max*27, 6)) + block + character(len=16) :: mode + integer :: length, status + call get_environment_variable("MFC_IB_PAIR_DEDUP", mode, length, status) + host_dedup = status == 0 .and. trim(mode) == "host" + end block + if (.not. host_dedup) then + @:ALLOCATE(pair_bits(f_pair_bit_index(num_local_ibs_max - 1, num_local_ibs_max)/32 + 1)) + pair_bits = 0 + $:GPU_UPDATE(device='[pair_bits]') + end if + detect_seconds = 0d0 + detect_calls = 0 + wall_overlap_distances = 0 collisions_active = .false. $:GPU_UPDATE(device='[wall_overlap_distances]') @@ -68,7 +94,18 @@ contains ! get is distance used in the force calculation with each IB and each wall call s_detect_wall_collisions(any_wall_collision) - call s_detect_ib_collisions(ghost_points, ib_markers, num_gps, num_considered_collisions) + block + integer(kind=8) :: c0, c1, rate + call system_clock(c0, rate) + if (host_dedup) then + call s_detect_ib_collisions(ghost_points, ib_markers, num_gps, num_considered_collisions) + else + call s_detect_ib_collisions_gpu(ghost_points, ib_markers, num_gps, num_considered_collisions) + end if + call system_clock(c1) + detect_seconds = detect_seconds + real(c1 - c0, kind(0d0))/real(rate, kind(0d0)) + detect_calls = detect_calls + 1 + end block ! accumulate across RK stages; consumed (and reset) by s_compute_dt once per time step collisions_active = collisions_active .or. any_wall_collision .or. (num_considered_collisions > 0) @@ -79,6 +116,8 @@ contains call s_apply_ib_collision_forces_soft_sphere(num_considered_collisions, forces, torques) end select + if (.not. host_dedup) call s_clear_pair_bits(num_considered_collisions) + end subroutine s_apply_collision_forces !> @brief applies collision forces to IBs assuming a soft-sphere collision model (all IBs are circles or spheres) @@ -240,6 +279,116 @@ contains !> uses ghost-point/image-point information to determine if it is possible if two IBs are colliding, effectively an optimized !! nearest neighbor search + !> Triangular index (0-based) of the unordered pair a < b, both in 1 ... num_local_ibs_max + pure function f_pair_bit_index(a, b) result(idx) + + $:GPU_ROUTINE(parallelism='[seq]') + integer, intent(in) :: a, b + integer(kind=8) :: idx + + idx = int(a - 1, 8)*int(2*num_local_ibs_max - a, 8)/2 + int(b - a - 1, 8) + + end function f_pair_bit_index + + !> Same candidates as s_detect_ib_collisions (every ghost point next to another patch's marker, both patches recording the + !! pair), deduplicated on the GPU: each candidate is mapped to its sorted neighborhood pair (a, b) and claims that pair's bit in + !! pair_bits with an atomic OR; only the thread that flips the bit appends the pair. Bits are cleared by s_clear_pair_bits after + !! the forces. A pair's periodic image is taken from the claiming candidate; two different images of one pair would need a + !! domain about two particles across. + subroutine s_detect_ib_collisions_gpu(gps, ib_markers, num_gps, num_considered_collisions) + + type(ghost_point), dimension(num_gps), intent(in) :: gps + type(integer_field), intent(in) :: ib_markers + integer, intent(in) :: num_gps + integer, intent(out) :: num_considered_collisions + integer :: i, j, k, ii, jj, kk, z_bound, gp_idx, gp_patch_id, neighbor_patch_id + integer :: a, b, gbl_a, gbl_b, enc_a, enc_b, xp, yp, zp, word, bit, mask, old_word, slot + integer :: num_found, max_pairs + integer(kind=8) :: idx + + num_found = 0 + max_pairs = size(collision_lookup, 1) + z_bound = 0; if (num_dims == 3) z_bound = 2 + + $:GPU_PARALLEL_LOOP(private='[gp_idx, i, j, k, ii, jj, kk, gp_patch_id, neighbor_patch_id, a, b, gbl_a, gbl_b, enc_a, & + & enc_b, xp, yp, zp, idx, word, bit, mask, old_word, slot]', copy='[num_found]', copyin='[z_bound, max_pairs]') + do gp_idx = 1, num_gps + i = gps(gp_idx)%loc(1) + j = gps(gp_idx)%loc(2) + k = 0; if (num_dims == 3) k = gps(gp_idx)%loc(3) + gp_patch_id = ib_markers%sf(i, j, k) + + ! first marker of another patch in the cube around the ghost point, as in s_detect_ib_collisions + neighbor_patch_id = 0 + neighbor_search: do ii = i - 2, i + 2 + do jj = j - 2, j + 2 + do kk = k - z_bound, k + z_bound + if (ib_markers%sf(ii, jj, kk) /= 0 .and. ib_markers%sf(ii, jj, kk) /= gp_patch_id) then + neighbor_patch_id = ib_markers%sf(ii, jj, kk) + exit neighbor_search + end if + end do + end do + end do neighbor_search + if (neighbor_patch_id == 0) cycle + + ! sorted neighborhood indices, the encoded ids (which carry the periodic image) following their patch + call s_decode_patch_periodicity(gp_patch_id, gbl_a, xp, yp, zp) + call s_get_neighborhood_idx(gbl_a, a) + call s_decode_patch_periodicity(neighbor_patch_id, gbl_b, xp, yp, zp) + call s_get_neighborhood_idx(gbl_b, b) + if (a <= 0 .or. b <= 0 .or. a == b) cycle + enc_a = gp_patch_id + enc_b = neighbor_patch_id + if (b < a) then + slot = a; a = b; b = slot + slot = enc_a; enc_a = enc_b; enc_b = slot + end if + + ! claim the pair: the thread whose OR flips the bit is the first to find it + idx = f_pair_bit_index(a, b) + word = int(idx/32) + 1 + bit = int(mod(idx, 32_8)) + mask = ishft(1, bit) + $:GPU_ATOMIC(atomic='capture') + old_word = pair_bits(word) + pair_bits(word) = ior(pair_bits(word), mask) + $:END_GPU_ATOMIC_CAPTURE() + if (iand(old_word, mask) /= 0) cycle + + $:GPU_ATOMIC(atomic='capture') + num_found = num_found + 1 + slot = num_found + $:END_GPU_ATOMIC_CAPTURE() + if (slot > max_pairs) cycle + collision_lookup(slot, 1) = a + collision_lookup(slot, 2) = b + collision_lookup(slot, 3) = enc_a + collision_lookup(slot, 4) = enc_b + end do + $:END_GPU_PARALLEL_LOOP() + + @:PROHIBIT(num_found > max_pairs, & + & "More collisions detected than memory to hold them. Consider increasing the size of the collision_lookup array") + num_considered_collisions = num_found + + end subroutine s_detect_ib_collisions_gpu + + !> Clear the bits the last s_detect_ib_collisions_gpu set (every set bit belongs to a recorded pair, so whole words can be + !! zeroed) + subroutine s_clear_pair_bits(num_considered_collisions) + + integer, intent(in) :: num_considered_collisions + integer :: n + + $:GPU_PARALLEL_LOOP(private='[n]') + do n = 1, num_considered_collisions + pair_bits(int(f_pair_bit_index(collision_lookup(n, 1), collision_lookup(n, 2))/32) + 1) = 0 + end do + $:END_GPU_PARALLEL_LOOP() + + end subroutine s_clear_pair_bits + subroutine s_detect_ib_collisions(gps, ib_markers, num_gps, num_considered_collisions) type(ghost_point), dimension(num_gps), intent(in) :: gps @@ -428,6 +577,12 @@ contains subroutine s_finalize_collisions_module() + ! one line per rank: pair-detection wall time, for comparing the host and GPU deduplication + print '(A,A,A,I0,A,F12.3,A,I0,A,I0)', ' IB_PAIR_DETECT mode=', merge('host', 'gpu ', host_dedup), ' rank ', proc_rank, & + & ' seconds ', detect_seconds, ' calls ', detect_calls, ' num_local_ibs ', num_local_ibs + if (.not. host_dedup) then + @:DEALLOCATE(pair_bits) + end if @:DEALLOCATE(collision_lookup) @:DEALLOCATE(wall_overlap_distances) From 161b7b53455c0e7fe71180727c839c9f4eb8417a Mon Sep 17 00:00:00 2001 From: Daniel Vickers Date: Tue, 10 Nov 2026 11:55:43 -0800 Subject: [PATCH 2/4] Remove old collision code --- src/simulation/m_collisions.fpp | 171 ++++---------------------------- 1 file changed, 17 insertions(+), 154 deletions(-) diff --git a/src/simulation/m_collisions.fpp b/src/simulation/m_collisions.fpp index 9c376c48c6..ab42031571 100644 --- a/src/simulation/m_collisions.fpp +++ b/src/simulation/m_collisions.fpp @@ -38,11 +38,6 @@ module m_collisions !! between calls. num_local_ibs_max = 30000 gives 4.5e8 bits (56 MB). integer, allocatable, dimension(:) :: pair_bits $:GPU_DECLARE(create='[pair_bits]') - !> MFC_IB_PAIR_DEDUP=host selects the original serial host deduplication (kept for comparison); anything else uses the GPU - logical :: host_dedup - !> wall-clock seconds spent in IB pair detection on this rank, and the number of calls, reported at finalize - real(kind(0d0)) :: detect_seconds - integer :: detect_calls contains @@ -58,19 +53,9 @@ contains @:ALLOCATE(collision_lookup(num_local_ibs_max * 27 * 8, 4)) @:ALLOCATE(wall_overlap_distances(num_local_ibs_max*27, 6)) - block - character(len=16) :: mode - integer :: length, status - call get_environment_variable("MFC_IB_PAIR_DEDUP", mode, length, status) - host_dedup = status == 0 .and. trim(mode) == "host" - end block - if (.not. host_dedup) then - @:ALLOCATE(pair_bits(f_pair_bit_index(num_local_ibs_max - 1, num_local_ibs_max)/32 + 1)) - pair_bits = 0 - $:GPU_UPDATE(device='[pair_bits]') - end if - detect_seconds = 0d0 - detect_calls = 0 + @:ALLOCATE(pair_bits(f_pair_bit_index(num_local_ibs_max - 1, num_local_ibs_max)/32 + 1)) + pair_bits = 0 + $:GPU_UPDATE(device='[pair_bits]') wall_overlap_distances = 0 collisions_active = .false. @@ -94,18 +79,7 @@ contains ! get is distance used in the force calculation with each IB and each wall call s_detect_wall_collisions(any_wall_collision) - block - integer(kind=8) :: c0, c1, rate - call system_clock(c0, rate) - if (host_dedup) then - call s_detect_ib_collisions(ghost_points, ib_markers, num_gps, num_considered_collisions) - else - call s_detect_ib_collisions_gpu(ghost_points, ib_markers, num_gps, num_considered_collisions) - end if - call system_clock(c1) - detect_seconds = detect_seconds + real(c1 - c0, kind(0d0))/real(rate, kind(0d0)) - detect_calls = detect_calls + 1 - end block + call s_detect_ib_collisions(ghost_points, ib_markers, num_gps, num_considered_collisions) ! accumulate across RK stages; consumed (and reset) by s_compute_dt once per time step collisions_active = collisions_active .or. any_wall_collision .or. (num_considered_collisions > 0) @@ -116,7 +90,7 @@ contains call s_apply_ib_collision_forces_soft_sphere(num_considered_collisions, forces, torques) end select - if (.not. host_dedup) call s_clear_pair_bits(num_considered_collisions) + call s_clear_pair_bits(num_considered_collisions) end subroutine s_apply_collision_forces @@ -277,8 +251,6 @@ contains end subroutine s_apply_wall_collision_forces_soft_sphere - !> uses ghost-point/image-point information to determine if it is possible if two IBs are colliding, effectively an optimized - !! nearest neighbor search !> Triangular index (0-based) of the unordered pair a < b, both in 1 ... num_local_ibs_max pure function f_pair_bit_index(a, b) result(idx) @@ -290,12 +262,14 @@ contains end function f_pair_bit_index - !> Same candidates as s_detect_ib_collisions (every ghost point next to another patch's marker, both patches recording the - !! pair), deduplicated on the GPU: each candidate is mapped to its sorted neighborhood pair (a, b) and claims that pair's bit in - !! pair_bits with an atomic OR; only the thread that flips the bit appends the pair. Bits are cleared by s_clear_pair_bits after - !! the forces. A pair's periodic image is taken from the claiming candidate; two different images of one pair would need a - !! domain about two particles across. - subroutine s_detect_ib_collisions_gpu(gps, ib_markers, num_gps, num_considered_collisions) + !> Uses ghost-point information to find IB pairs that may be colliding, effectively an optimized nearest-neighbor search: every + !! ghost point with another patch's marker within 2 cells gives a candidate pair. Both patches record it: the rank that owns the + !! contact point may hold interior ghost points of only one of them, so one-sided detection can leave that rank blind to a + !! contact within a cell of its boundary. The many duplicate candidates are dropped on the GPU: each candidate is mapped to its + !! sorted neighborhood pair (a, b) and claims that pair's bit in pair_bits with an atomic OR, and only the thread that flips the + !! bit appends the pair. Bits are cleared by s_clear_pair_bits after the forces. A pair's periodic image is taken from the + !! claiming candidate; two different images of one pair would need a domain about two particles across. + subroutine s_detect_ib_collisions(gps, ib_markers, num_gps, num_considered_collisions) type(ghost_point), dimension(num_gps), intent(in) :: gps type(integer_field), intent(in) :: ib_markers @@ -318,7 +292,7 @@ contains k = 0; if (num_dims == 3) k = gps(gp_idx)%loc(3) gp_patch_id = ib_markers%sf(i, j, k) - ! first marker of another patch in the cube around the ghost point, as in s_detect_ib_collisions + ! first marker of another patch in the cube around the ghost point neighbor_patch_id = 0 neighbor_search: do ii = i - 2, i + 2 do jj = j - 2, j + 2 @@ -372,10 +346,9 @@ contains & "More collisions detected than memory to hold them. Consider increasing the size of the collision_lookup array") num_considered_collisions = num_found - end subroutine s_detect_ib_collisions_gpu + end subroutine s_detect_ib_collisions - !> Clear the bits the last s_detect_ib_collisions_gpu set (every set bit belongs to a recorded pair, so whole words can be - !! zeroed) + !> Clear the bits the last s_detect_ib_collisions set (every set bit belongs to a recorded pair, so whole words can be zeroed) subroutine s_clear_pair_bits(num_considered_collisions) integer, intent(in) :: num_considered_collisions @@ -389,111 +362,6 @@ contains end subroutine s_clear_pair_bits - subroutine s_detect_ib_collisions(gps, ib_markers, num_gps, num_considered_collisions) - - type(ghost_point), dimension(num_gps), intent(in) :: gps - type(integer_field), intent(in) :: ib_markers - integer, intent(in) :: num_gps - integer, intent(out) :: num_considered_collisions - integer :: i, j, k, z_bound, ii, jj, kk - integer, dimension(2) :: decoded_pairs - integer :: gp_idx, gp_patch_id, neighbor_patch_id, local_idx - integer :: pair_idx, out_idx - logical :: already_found - - ! Temporary array to hold all detected pairs (with potential duplicates) - integer, dimension(num_gps, 2) :: raw_pairs - integer :: num_raw, local_num_raw - - num_raw = 0 - z_bound = 0; if (num_dims == 3) z_bound = 2 - - $:GPU_PARALLEL_LOOP(private='[gp_idx, gp_patch_id, neighbor_patch_id, local_num_raw, i, j, k, ii, jj, kk]', & - & copy='[raw_pairs, num_raw]', copyin='[z_bound]') - do gp_idx = 1, num_gps - i = gps(gp_idx)%loc(1) - j = gps(gp_idx)%loc(2) - k = 0; if (num_dims == 3) k = gps(gp_idx)%loc(3) - gp_patch_id = ib_markers%sf(i, j, k) - - ! search in a cube around the BG for Ib markers belonging to another patch - neighbor_search: do ii = i - 2, i + 2 - do jj = j - 2, j + 2 - do kk = k - z_bound, k + z_bound - neighbor_patch_id = ib_markers%sf(ii, jj, kk) - - ! Any neighbor of a different patch is a candidate pair. Both patches record it: the rank that owns the - ! contact point may hold interior ghost points of only one of them, so one-sided detection can leave - ! that rank blind to a contact that sits within a cell of its boundary. The host pass below sorts the - ! pair and drops duplicates. - if (neighbor_patch_id /= 0 .and. neighbor_patch_id /= gp_patch_id) then - $:GPU_ATOMIC(atomic='capture') - num_raw = num_raw + 1 - local_num_raw = num_raw - $:END_GPU_ATOMIC_CAPTURE() - - raw_pairs(local_num_raw, 1) = gp_patch_id - raw_pairs(local_num_raw, 2) = neighbor_patch_id - exit neighbor_search - end if - end do - end do - end do neighbor_search - end do - $:END_GPU_PARALLEL_LOOP() - - ! Coalesce collisions unique pairs - num_considered_collisions = 0 - collision_lookup = 0 - ! for each pair found in the raw collection - do pair_idx = 1, num_raw - already_found = .false. - - ! get the decoded pairs for checking if they exist, using ii,jj,kk as dummy indices - call s_decode_patch_periodicity(raw_pairs(pair_idx, 1), decoded_pairs(1), ii, jj, kk) - call s_decode_patch_periodicity(raw_pairs(pair_idx, 2), decoded_pairs(2), ii, jj, kk) - call s_get_neighborhood_idx(decoded_pairs(1), local_idx) - decoded_pairs(1) = local_idx - call s_get_neighborhood_idx(decoded_pairs(2), local_idx) - decoded_pairs(2) = local_idx - - ! skip self-collisions (an IB cannot collide with its own periodic image) - if (decoded_pairs(1) == decoded_pairs(2)) cycle - - ! need to swap to guarantee the smaller decoded marker value is in index 1 and prevent double-counting - if (decoded_pairs(2) < decoded_pairs(1)) then - decoded_pairs(1) = decoded_pairs(1) + decoded_pairs(2) - decoded_pairs(2) = decoded_pairs(1) - decoded_pairs(2) - decoded_pairs(1) = decoded_pairs(1) - decoded_pairs(2) - raw_pairs(pair_idx, 1) = raw_pairs(pair_idx, 1) + raw_pairs(pair_idx, 2) - raw_pairs(pair_idx, 2) = raw_pairs(pair_idx, 1) - raw_pairs(pair_idx, 2) - raw_pairs(pair_idx, 1) = raw_pairs(pair_idx, 1) - raw_pairs(pair_idx, 2) - end if - - ! check if it is already in the list - do out_idx = 1, num_considered_collisions - if (collision_lookup(out_idx, 1) == decoded_pairs(1) .and. collision_lookup(out_idx, 2) == decoded_pairs(2)) then - already_found = .true. - exit - end if - end do - - ! and if it is not, append it to the list of pairs - if (.not. already_found) then - num_considered_collisions = num_considered_collisions + 1 - @:PROHIBIT(num_considered_collisions > size(collision_lookup, 1) , & - & "More collisions detected than memory to hold them. Consider increasing the size of the collision_lookup array") - - collision_lookup(num_considered_collisions, 1) = decoded_pairs(1) - collision_lookup(num_considered_collisions, 2) = decoded_pairs(2) - collision_lookup(num_considered_collisions, 3) = raw_pairs(pair_idx, 1) - collision_lookup(num_considered_collisions, 4) = raw_pairs(pair_idx, 2) - end if - end do - $:GPU_UPDATE(device='[collision_lookup]') - - end subroutine s_detect_ib_collisions - !> @brief uses boundary conditions and particle locations to check for wall conditions subroutine s_detect_wall_collisions(any_wall_collision) @@ -577,12 +445,7 @@ contains subroutine s_finalize_collisions_module() - ! one line per rank: pair-detection wall time, for comparing the host and GPU deduplication - print '(A,A,A,I0,A,F12.3,A,I0,A,I0)', ' IB_PAIR_DETECT mode=', merge('host', 'gpu ', host_dedup), ' rank ', proc_rank, & - & ' seconds ', detect_seconds, ' calls ', detect_calls, ' num_local_ibs ', num_local_ibs - if (.not. host_dedup) then - @:DEALLOCATE(pair_bits) - end if + @:DEALLOCATE(pair_bits) @:DEALLOCATE(collision_lookup) @:DEALLOCATE(wall_overlap_distances) From 7aa71cad0c770e44d4e97c359e67effc34a07863 Mon Sep 17 00:00:00 2001 From: "Daniel J. Vickers" Date: Wed, 18 Nov 2026 16:19:49 -0600 Subject: [PATCH 3/4] Lint --- src/simulation/m_collisions.fpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/simulation/m_collisions.fpp b/src/simulation/m_collisions.fpp index ab42031571..e488c3e366 100644 --- a/src/simulation/m_collisions.fpp +++ b/src/simulation/m_collisions.fpp @@ -251,7 +251,7 @@ contains end subroutine s_apply_wall_collision_forces_soft_sphere - !> Triangular index (0-based) of the unordered pair a < b, both in 1 ... num_local_ibs_max + !> Triangular index (0-based) of the unordered pair a < b, both in 1 to num_local_ibs_max pure function f_pair_bit_index(a, b) result(idx) $:GPU_ROUTINE(parallelism='[seq]') From 7daede841bdf9ee42cf327c466598c675878cf22 Mon Sep 17 00:00:00 2001 From: "Daniel J. Vickers" Date: Fri, 20 Nov 2026 11:00:22 -0600 Subject: [PATCH 4/4] Added pull request recommended fix --- src/simulation/m_collisions.fpp | 65 ++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/src/simulation/m_collisions.fpp b/src/simulation/m_collisions.fpp index e488c3e366..e3493430fe 100644 --- a/src/simulation/m_collisions.fpp +++ b/src/simulation/m_collisions.fpp @@ -33,10 +33,11 @@ module m_collisions logical :: collisions_active !> One bit per unordered pair (a < b) of this rank's IBs (local + neighborhood indices, a triangular index over - !! num_local_ibs_max): the ghost-point candidate pass claims each pair with an atomic OR, so only the first thread to find a + !! pair_bits_capacity): the ghost-point candidate pass claims each pair with an atomic OR, so only the first thread to find a !! pair records it and duplicates are dropped on the GPU. Bits are cleared after the forces are applied, so the set is empty - !! between calls. num_local_ibs_max = 30000 gives 4.5e8 bits (56 MB). + !! between calls. Sized from num_ibs and regrown with it; memory is quadratic in the body count (1e4 bodies take 6 MB). integer, allocatable, dimension(:) :: pair_bits + integer :: pair_bits_capacity !< Number of IBs the bitset is indexed for $:GPU_DECLARE(create='[pair_bits]') contains @@ -53,9 +54,7 @@ contains @:ALLOCATE(collision_lookup(num_local_ibs_max * 27 * 8, 4)) @:ALLOCATE(wall_overlap_distances(num_local_ibs_max*27, 6)) - @:ALLOCATE(pair_bits(f_pair_bit_index(num_local_ibs_max - 1, num_local_ibs_max)/32 + 1)) - pair_bits = 0 - $:GPU_UPDATE(device='[pair_bits]') + call s_resize_pair_bits(num_ibs) wall_overlap_distances = 0 collisions_active = .false. @@ -64,6 +63,22 @@ contains end subroutine s_initialize_collisions_module + !> Size the pair bitset for num_bodies IBs, doubled so a growing body count does not reallocate every step (empty on exit) + subroutine s_resize_pair_bits(num_bodies) + + integer, intent(in) :: num_bodies + + if (allocated(pair_bits)) then + @:DEALLOCATE(pair_bits) + end if + + pair_bits_capacity = min(max(2*num_bodies, 2), num_local_ibs_max) + @:ALLOCATE(pair_bits(f_pair_bit_index(pair_bits_capacity - 1, pair_bits_capacity, pair_bits_capacity)/32 + 1)) + pair_bits = 0 + $:GPU_UPDATE(device='[pair_bits]') + + end subroutine s_resize_pair_bits + subroutine s_apply_collision_forces(ghost_points, num_gps, ib_markers, forces, torques) type(ghost_point), dimension(:), intent(in) :: ghost_points @@ -77,6 +92,9 @@ contains if (collision_model == 0) return + ! IBs migrating onto this rank can outgrow the bitset + if (num_ibs > pair_bits_capacity) call s_resize_pair_bits(num_ibs) + ! get is distance used in the force calculation with each IB and each wall call s_detect_wall_collisions(any_wall_collision) call s_detect_ib_collisions(ghost_points, ib_markers, num_gps, num_considered_collisions) @@ -251,14 +269,14 @@ contains end subroutine s_apply_wall_collision_forces_soft_sphere - !> Triangular index (0-based) of the unordered pair a < b, both in 1 to num_local_ibs_max - pure function f_pair_bit_index(a, b) result(idx) + !> Triangular index (0-based) of the unordered pair a < b, both in 1 to num_bodies + pure function f_pair_bit_index(a, b, num_bodies) result(idx) $:GPU_ROUTINE(parallelism='[seq]') - integer, intent(in) :: a, b + integer, intent(in) :: a, b, num_bodies integer(kind=8) :: idx - idx = int(a - 1, 8)*int(2*num_local_ibs_max - a, 8)/2 + int(b - a - 1, 8) + idx = int(a - 1, 8)*int(2*num_bodies - a, 8)/2 + int(b - a - 1, 8) end function f_pair_bit_index @@ -272,20 +290,22 @@ contains subroutine s_detect_ib_collisions(gps, ib_markers, num_gps, num_considered_collisions) type(ghost_point), dimension(num_gps), intent(in) :: gps - type(integer_field), intent(in) :: ib_markers - integer, intent(in) :: num_gps - integer, intent(out) :: num_considered_collisions - integer :: i, j, k, ii, jj, kk, z_bound, gp_idx, gp_patch_id, neighbor_patch_id - integer :: a, b, gbl_a, gbl_b, enc_a, enc_b, xp, yp, zp, word, bit, mask, old_word, slot - integer :: num_found, max_pairs - integer(kind=8) :: idx + type(integer_field), intent(in) :: ib_markers + integer, intent(in) :: num_gps + integer, intent(out) :: num_considered_collisions + integer :: i, j, k, ii, jj, kk, z_bound, gp_idx, gp_patch_id, neighbor_patch_id + integer :: a, b, gbl_a, gbl_b, enc_a, enc_b, xp, yp, zp, bit, mask, old_word, slot + integer :: num_found, max_pairs, num_bodies + integer(kind=8) :: idx, word num_found = 0 max_pairs = size(collision_lookup, 1) + num_bodies = pair_bits_capacity z_bound = 0; if (num_dims == 3) z_bound = 2 $:GPU_PARALLEL_LOOP(private='[gp_idx, i, j, k, ii, jj, kk, gp_patch_id, neighbor_patch_id, a, b, gbl_a, gbl_b, enc_a, & - & enc_b, xp, yp, zp, idx, word, bit, mask, old_word, slot]', copy='[num_found]', copyin='[z_bound, max_pairs]') + & enc_b, xp, yp, zp, idx, word, bit, mask, old_word, slot]', copy='[num_found]', copyin='[z_bound, & + & max_pairs, num_bodies]') do gp_idx = 1, num_gps i = gps(gp_idx)%loc(1) j = gps(gp_idx)%loc(2) @@ -320,8 +340,8 @@ contains end if ! claim the pair: the thread whose OR flips the bit is the first to find it - idx = f_pair_bit_index(a, b) - word = int(idx/32) + 1 + idx = f_pair_bit_index(a, b, num_bodies) + word = idx/32 + 1 bit = int(mod(idx, 32_8)) mask = ishft(1, bit) $:GPU_ATOMIC(atomic='capture') @@ -352,11 +372,12 @@ contains subroutine s_clear_pair_bits(num_considered_collisions) integer, intent(in) :: num_considered_collisions - integer :: n + integer :: n, num_bodies - $:GPU_PARALLEL_LOOP(private='[n]') + num_bodies = pair_bits_capacity + $:GPU_PARALLEL_LOOP(private='[n]', copyin='[num_bodies]') do n = 1, num_considered_collisions - pair_bits(int(f_pair_bit_index(collision_lookup(n, 1), collision_lookup(n, 2))/32) + 1) = 0 + pair_bits(f_pair_bit_index(collision_lookup(n, 1), collision_lookup(n, 2), num_bodies)/32 + 1) = 0 end do $:END_GPU_PARALLEL_LOOP()