Skip to content

Commit 32679eb

Browse files
efaulhabersvchb
andauthored
Improve GPU performance with shifting (trixi-framework#974)
* Improve GPU performance with shifting * Improve update callback timers * Fix TVF * Add timer * Fix --------- Co-authored-by: Sven Berger <berger.sven@gmail.com>
1 parent 4725885 commit 32679eb

File tree

5 files changed

+69
-72
lines changed

5 files changed

+69
-72
lines changed

src/callbacks/update.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,20 @@ function (update_callback!::UpdateCallback)(integrator)
8888
end
8989

9090
# Update open boundaries first, since particles might be activated or deactivated
91-
@trixi_timeit timer() "update open boundary" foreach_system(semi) do system
91+
foreach_system(semi) do system
9292
update_open_boundary_eachstep!(system, v_ode, u_ode, semi, t, integrator)
9393
end
9494

95-
@trixi_timeit timer() "update particle packing" foreach_system(semi) do system
95+
foreach_system(semi) do system
9696
update_particle_packing(system, v_ode, u_ode, semi, integrator)
9797
end
9898

9999
# This is only used by the particle packing system and should be removed in the future
100-
@trixi_timeit timer() "update TVF" foreach_system(semi) do system
100+
foreach_system(semi) do system
101101
update_transport_velocity!(system, v_ode, semi, integrator)
102102
end
103103

104-
@trixi_timeit timer() "particle shifting" foreach_system(semi) do system
104+
foreach_system(semi) do system
105105
particle_shifting_from_callback!(u_ode, shifting_technique(system), system,
106106
v_ode, semi, integrator)
107107
end

src/preprocessing/particle_packing/system.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ function update_particle_packing(system::ParticlePackingSystem, v_ode, u_ode,
269269
semi, integrator)
270270
u = wrap_u(u_ode, system, semi)
271271

272-
update_position!(u, system, semi)
272+
@trixi_timeit timer() "update particle packing" update_position!(u, system, semi)
273273

274274
# Tell OrdinaryDiffEq that `integrator.u` has been modified
275275
u_modified!(integrator, true)
@@ -365,14 +365,16 @@ end
365365
# Update from `UpdateCallback` between time steps (skip for fixed systems)
366366
@inline function update_transport_velocity!(system::ParticlePackingSystem{<:Any, false},
367367
v_ode, semi, integrator)
368-
v = wrap_v(v_ode, system, semi)
369-
@threaded semi for particle in each_integrated_particle(system)
370-
for i in 1:ndims(system)
371-
system.advection_velocity[i, particle] = v[i, particle]
372-
373-
# The particle velocity is set to zero at the beginning of each time step to
374-
# achieve a fully stationary state.
375-
v[i, particle] = zero(eltype(system))
368+
@trixi_timeit timer() "update packing TVF" begin
369+
v = wrap_v(v_ode, system, semi)
370+
@threaded semi for particle in each_integrated_particle(system)
371+
for i in 1:ndims(system)
372+
system.advection_velocity[i, particle] = v[i, particle]
373+
374+
# The particle velocity is set to zero at the beginning of each time step to
375+
# achieve a fully stationary state.
376+
v[i, particle] = zero(eltype(system))
377+
end
376378
end
377379
end
378380

src/schemes/boundary/open_boundary/system.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,16 +301,18 @@ function update_open_boundary_eachstep!(system::OpenBoundarySystem, v_ode, u_ode
301301
semi, t, integrator)
302302
(; boundary_model) = system
303303

304-
u = wrap_u(u_ode, system, semi)
305-
v = wrap_v(v_ode, system, semi)
304+
@trixi_timeit timer() "update open boundary" begin
305+
u = wrap_u(u_ode, system, semi)
306+
v = wrap_v(v_ode, system, semi)
306307

307-
@trixi_timeit timer() "check domain" check_domain!(system, v, u, v_ode, u_ode, semi)
308+
@trixi_timeit timer() "check domain" check_domain!(system, v, u, v_ode, u_ode, semi)
308309

309-
update_pressure_model!(system, v, u, semi, integrator.dt)
310+
update_pressure_model!(system, v, u, semi, integrator.dt)
310311

311-
# Update density, pressure and velocity based on the specific boundary model
312-
@trixi_timeit timer() "update boundary quantities" begin
313-
update_boundary_quantities!(system, boundary_model, v, u, v_ode, u_ode, semi, t)
312+
# Update density, pressure and velocity based on the specific boundary model
313+
@trixi_timeit timer() "update boundary quantities" begin
314+
update_boundary_quantities!(system, boundary_model, v, u, v_ode, u_ode, semi, t)
315+
end
314316
end
315317

316318
# Tell OrdinaryDiffEq that `integrator.u` has been modified

src/schemes/fluid/fluid.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ end
146146
vdiff = current_velocity(v_particle_system, particle_system, particle) -
147147
current_velocity(v_neighbor_system, neighbor_system, neighbor)
148148

149+
vdiff += continuity_equation_shifting_term(shifting_technique(particle_system),
150+
particle_system, neighbor_system,
151+
particle, neighbor, rho_a, rho_b)
152+
149153
dv[end, particle] += rho_a / rho_b * m_b * dot(vdiff, grad_kernel)
150154

151155
# Artificial density diffusion should only be applied to systems representing a fluid
@@ -157,10 +161,6 @@ end
157161
pos_diff, distance, m_b, rho_a, rho_b, particle_system,
158162
grad_kernel)
159163
end
160-
161-
continuity_equation_shifting!(dv, shifting_technique(particle_system),
162-
particle_system, neighbor_system,
163-
particle, neighbor, grad_kernel, rho_a, rho_b, m_b)
164164
end
165165

166166
function calculate_dt(v_ode, u_ode, cfl_number, system::AbstractFluidSystem, semi)

src/schemes/fluid/shifting_techniques.jl

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ end
4949
end
5050

5151
# Additional term(s) in the continuity equation due to the shifting technique
52-
function continuity_equation_shifting!(dv, shifting,
53-
particle_system, neighbor_system,
54-
particle, neighbor, grad_kernel, rho_a, rho_b, m_b)
55-
return dv
52+
@inline function continuity_equation_shifting_term(shifting, particle_system,
53+
neighbor_system,
54+
particle, neighbor, rho_a, rho_b)
55+
return zero(SVector{ndims(particle_system), eltype(particle_system)})
5656
end
5757

5858
@doc raw"""
@@ -324,10 +324,11 @@ See [`ParticleShiftingTechnique`](@ref).
324324
struct MomentumEquationTermSun2019 end
325325

326326
# Additional term in the momentum equation due to the shifting technique
327-
@inline function dv_shifting(shifting::ParticleShiftingTechnique, system, neighbor_system,
328-
v_system, v_neighbor_system, particle, neighbor,
329-
m_a, m_b, rho_a, rho_b, pos_diff, distance,
330-
grad_kernel, correction)
327+
@propagate_inbounds function dv_shifting(shifting::ParticleShiftingTechnique, system,
328+
neighbor_system,
329+
v_system, v_neighbor_system, particle, neighbor,
330+
m_a, m_b, rho_a, rho_b, pos_diff, distance,
331+
grad_kernel, correction)
331332
return dv_shifting(shifting.momentum_equation_term, system, neighbor_system,
332333
v_system, v_neighbor_system, particle, neighbor,
333334
m_a, m_b, rho_a, rho_b, pos_diff, distance,
@@ -351,21 +352,19 @@ end
351352
end
352353

353354
# `ParticleShiftingTechnique{<:Any, <:Any, true}` means `modify_continuity_equation=true`
354-
function continuity_equation_shifting!(dv,
355-
shifting::ParticleShiftingTechnique{<:Any, <:Any,
356-
true},
357-
system, neighbor_system,
358-
particle, neighbor, grad_kernel, rho_a, rho_b, m_b)
359-
delta_v_diff = delta_v(system, particle) -
360-
delta_v(neighbor_system, neighbor)
361-
362-
dv[end, particle] += rho_a / rho_b * m_b * dot(delta_v_diff, grad_kernel)
363-
364-
second_continuity_equation_term!(dv, shifting.second_continuity_equation_term,
365-
system, neighbor_system,
366-
particle, neighbor, grad_kernel, rho_a, rho_b, m_b)
355+
@propagate_inbounds function continuity_equation_shifting_term(shifting::ParticleShiftingTechnique{<:Any,
356+
<:Any,
357+
true},
358+
system, neighbor_system,
359+
particle, neighbor,
360+
rho_a, rho_b)
361+
delta_v_a = delta_v(system, particle)
362+
delta_v_b = delta_v(neighbor_system, neighbor)
363+
delta_v_diff = delta_v_a - delta_v_b
367364

368-
return dv
365+
second_term = second_continuity_equation_term(shifting.second_continuity_equation_term,
366+
delta_v_a, delta_v_b, rho_a, rho_b)
367+
return delta_v_diff + second_term
369368
end
370369

371370
"""
@@ -378,29 +377,23 @@ See [`ParticleShiftingTechnique`](@ref).
378377
"""
379378
struct ContinuityEquationTermSun2019 end
380379

381-
@inline function second_continuity_equation_term!(dv,
382-
::ContinuityEquationTermSun2019,
383-
system, neighbor_system,
384-
particle, neighbor, grad_kernel,
385-
rho_a, rho_b, m_b)
386-
rho_v = rho_a * delta_v(system, particle) + rho_b * delta_v(neighbor_system, neighbor)
387-
388-
dv[end, particle] += m_b / rho_b * dot(rho_v, grad_kernel)
389-
390-
return dv
380+
@propagate_inbounds function second_continuity_equation_term(::ContinuityEquationTermSun2019,
381+
delta_v_a, delta_v_b,
382+
rho_a, rho_b)
383+
return delta_v_a + rho_b / rho_a * delta_v_b
391384
end
392385

393-
@inline function second_continuity_equation_term!(dv, second_continuity_equation_term,
394-
system, neighbor_system,
395-
particle, neighbor, grad_kernel,
396-
rho_a, rho_b, m_b)
397-
return dv
386+
@inline function second_continuity_equation_term(second_continuity_equation_term,
387+
delta_v_a, delta_v_b, rho_a, rho_b)
388+
return zero(delta_v_a)
398389
end
399390

400391
# `ParticleShiftingTechnique{<:Any, true}` means `update_everystage=true`
401392
function update_shifting!(system, shifting::ParticleShiftingTechnique{<:Any, true},
402393
v, u, v_ode, u_ode, semi)
403-
update_shifting_inner!(system, shifting, v, u, v_ode, u_ode, semi)
394+
@trixi_timeit timer() "update shifting" begin
395+
update_shifting_inner!(system, shifting, v, u, v_ode, u_ode, semi)
396+
end
404397
end
405398

406399
# `ParticleShiftingTechnique{<:Any, false}` means `update_everystage=false`
@@ -506,10 +499,10 @@ end
506499
function particle_shifting_from_callback!(u_ode,
507500
shifting::ParticleShiftingTechnique{<:Any, false},
508501
system, v_ode, semi, integrator)
509-
@trixi_timeit timer() "particle shifting" begin
510-
# Update the shifting velocity
511-
update_shifting_from_callback!(system, shifting, v_ode, u_ode, semi)
502+
# Update the shifting velocity
503+
update_shifting_from_callback!(system, shifting, v_ode, u_ode, semi)
512504

505+
@trixi_timeit timer() "apply particle shifting" begin
513506
# Update the particle positions with the shifting velocity
514507
apply_particle_shifting!(u_ode, shifting, system, semi, integrator.dt)
515508
end
@@ -604,18 +597,18 @@ end
604597
# The function above misuses the pressure acceleration function by passing a Matrix as `p_a`.
605598
# This doesn't work with `tensile_instability_control`, so we disable TIC in this case.
606599
@inline function tensile_instability_control(m_a, m_b, rho_a, rho_b, p_a::SMatrix, p_b, W_a)
607-
return pressure_acceleration_continuity_density(m_a, m_b, rho_a, rho_b, p_a, A_b, W_a)
600+
return pressure_acceleration_continuity_density(m_a, m_b, rho_a, rho_b, p_a, p_b, W_a)
608601
end
609602

610-
function continuity_equation_shifting!(dv, shifting::TransportVelocityAdami{true},
611-
particle_system, neighbor_system,
612-
particle, neighbor, grad_kernel, rho_a, rho_b, m_b)
603+
@propagate_inbounds function continuity_equation_shifting_term(::TransportVelocityAdami{true},
604+
particle_system,
605+
neighbor_system,
606+
particle, neighbor,
607+
rho_a, rho_b)
613608
delta_v_diff = delta_v(particle_system, particle) -
614609
delta_v(neighbor_system, neighbor)
615610

616-
dv[end, particle] += rho_a / rho_b * m_b * dot(delta_v_diff, grad_kernel)
617-
618-
return dv
611+
return delta_v_diff
619612
end
620613

621614
function update_shifting!(system, shifting::TransportVelocityAdami, v, u, v_ode,

0 commit comments

Comments
 (0)