Skip to content

Commit

Permalink
GPU-compatible neighborhood search (trixi-framework#563)
Browse files Browse the repository at this point in the history
* Use PointNeighbors.jl#45

* Reformat

* Implement suggestions

---------

Co-authored-by: Niklas Neher <[email protected]>
  • Loading branch information
efaulhaber and LasNikas authored Jul 15, 2024
1 parent b82162f commit b3219cb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/general/neighborhood_search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function PointNeighbors.foreach_point_neighbor(f, system::GPUSystem, neighbor_sy
neighborhood_search;
points=each_moving_particle(system),
parallel=true)
@threaded system for point in points
PointNeighbors.foreach_neighbor(f, system_coords, neighbor_coords,
neighborhood_search, point)
end
# For `GPUSystem`s, explicitly pass the backend, so a `GPUSystem` with a CPU
# backend will actually launch the KernelAbstractions.jl kernels on the CPU.
foreach_point_neighbor(f, system_coords, neighbor_coords, neighborhood_search;
points, parallel=KernelAbstractions.get_backend(system_coords))
end
92 changes: 52 additions & 40 deletions src/general/semidiscretization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -644,20 +644,20 @@ function update_nhs!(neighborhood_search,
neighbor::Union{FluidSystem, TotalLagrangianSPHSystem},
u_system, u_neighbor)
# The current coordinates of fluids and solids change over time
PointNeighbors.update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, true))
update!(neighborhood_search, system,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, true))
end

function update_nhs!(neighborhood_search,
system::FluidSystem, neighbor::BoundarySPHSystem,
u_system, u_neighbor)
# Boundary coordinates only change over time when `neighbor.ismoving[]`
PointNeighbors.update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, neighbor.ismoving[]))
update!(neighborhood_search, system,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, neighbor.ismoving[]))
end

function update_nhs!(neighborhood_search,
Expand All @@ -667,10 +667,10 @@ function update_nhs!(neighborhood_search,

# TODO: Update only `active_coordinates` of open boundaries.
# Problem: Removing inactive particles from neighboring lists is necessary.
PointNeighbors.update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, true))
update!(neighborhood_search, system,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, true))
end

function update_nhs!(neighborhood_search,
Expand All @@ -680,20 +680,20 @@ function update_nhs!(neighborhood_search,

# TODO: Update only `active_coordinates` of open boundaries.
# Problem: Removing inactive particles from neighboring lists is necessary.
PointNeighbors.update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, true))
update!(neighborhood_search, system,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, true))
end

function update_nhs!(neighborhood_search,
system::TotalLagrangianSPHSystem, neighbor::FluidSystem,
u_system, u_neighbor)
# The current coordinates of fluids and solids change over time
PointNeighbors.update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, true))
update!(neighborhood_search, system,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, true))
end

function update_nhs!(neighborhood_search,
Expand All @@ -708,10 +708,10 @@ function update_nhs!(neighborhood_search,
u_system, u_neighbor)
# The current coordinates of solids change over time.
# Boundary coordinates only change over time when `neighbor.ismoving[]`.
PointNeighbors.update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, neighbor.ismoving[]))
update!(neighborhood_search, system,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, neighbor.ismoving[]))
end

function update_nhs!(neighborhood_search,
Expand All @@ -734,10 +734,10 @@ function update_nhs!(neighborhood_search,
#
# Boundary coordinates only change over time when `neighbor.ismoving[]`.
# The current coordinates of fluids and solids change over time.
PointNeighbors.update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(system.ismoving[], true))
update!(neighborhood_search, system,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(system.ismoving[], true))
end

function update_nhs!(neighborhood_search,
Expand All @@ -746,30 +746,30 @@ function update_nhs!(neighborhood_search,
u_system, u_neighbor)
# `system` coordinates only change over time when `system.ismoving[]`.
# `neighbor` coordinates only change over time when `neighbor.ismoving[]`.
PointNeighbors.update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(system.ismoving[], neighbor.ismoving[]))
update!(neighborhood_search, system,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(system.ismoving[], neighbor.ismoving[]))
end

function update_nhs!(neighborhood_search,
system::DEMSystem, neighbor::DEMSystem,
u_system, u_neighbor)
# Both coordinates change over time
PointNeighbors.update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, true))
update!(neighborhood_search, system,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, true))
end

function update_nhs!(neighborhood_search,
system::DEMSystem, neighbor::BoundaryDEMSystem,
u_system, u_neighbor)
# DEM coordinates change over time, the boundary coordinates don't
PointNeighbors.update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, false))
update!(neighborhood_search, system,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
points_moving=(true, false))
end

function update_nhs!(neighborhood_search,
Expand All @@ -788,6 +788,18 @@ function update_nhs!(neighborhood_search,
return neighborhood_search
end

# Forward to PointNeighbors.jl
function update!(neighborhood_search, system, x, y; points_moving=(true, false))
PointNeighbors.update!(neighborhood_search, x, y; points_moving)
end

# For `GPUSystem`s, explicitly pass the backend, so that a `GPUSystem` with a CPU
# backend will actually launch the KernelAbstractions.jl kernels on the CPU.
function update!(neighborhood_search, system::GPUSystem, x, y; points_moving=(true, false))
PointNeighbors.update!(neighborhood_search, x, y; points_moving,
parallelization_backend=KernelAbstractions.get_backend(system))
end

function check_configuration(systems)
foreach_system(systems) do system
check_configuration(system, systems)
Expand Down

0 comments on commit b3219cb

Please sign in to comment.