Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread-parallelize src term addition Euler Gravity #2102

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions src/semidiscretization/semidiscretization_euler_gravity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ function rhs!(du_ode, u_ode, semi::SemidiscretizationEulerGravity, t)
u_euler = wrap_array(u_ode, semi_euler)
du_euler = wrap_array(du_ode, semi_euler)
u_gravity = wrap_array(cache.u_ode, semi_gravity)
n_elements = size(u_euler)[end]

time_start = time_ns()

Expand All @@ -238,20 +239,38 @@ function rhs!(du_ode, u_ode, semi::SemidiscretizationEulerGravity, t)

# add gravitational source source_terms to the Euler part
if ndims(semi_euler) == 1
@views @. du_euler[2, .., :] -= u_euler[1, .., :] * u_gravity[2, .., :]
@views @. du_euler[3, .., :] -= u_euler[2, .., :] * u_gravity[2, .., :]
@threaded for element in 1:n_elements
@views @. du_euler[2, .., element] -= u_euler[1, .., element] *
u_gravity[2, .., element]
@views @. du_euler[3, .., element] -= u_euler[2, .., element] *
u_gravity[2, .., element]
end
elseif ndims(semi_euler) == 2
@views @. du_euler[2, .., :] -= u_euler[1, .., :] * u_gravity[2, .., :]
@views @. du_euler[3, .., :] -= u_euler[1, .., :] * u_gravity[3, .., :]
@views @. du_euler[4, .., :] -= (u_euler[2, .., :] * u_gravity[2, .., :] +
u_euler[3, .., :] * u_gravity[3, .., :])
@threaded for element in 1:n_elements
@views @. du_euler[2, .., element] -= u_euler[1, .., element] *
u_gravity[2, .., element]
@views @. du_euler[3, .., element] -= u_euler[1, .., element] *
u_gravity[3, .., element]
@views @. du_euler[4, .., element] -= (u_euler[2, .., element] *
u_gravity[2, .., element] +
u_euler[3, .., element] *
u_gravity[3, .., element])
end
elseif ndims(semi_euler) == 3
@views @. du_euler[2, .., :] -= u_euler[1, .., :] * u_gravity[2, .., :]
@views @. du_euler[3, .., :] -= u_euler[1, .., :] * u_gravity[3, .., :]
@views @. du_euler[4, .., :] -= u_euler[1, .., :] * u_gravity[4, .., :]
@views @. du_euler[5, .., :] -= (u_euler[2, .., :] * u_gravity[2, .., :] +
u_euler[3, .., :] * u_gravity[3, .., :] +
u_euler[4, .., :] * u_gravity[4, .., :])
@threaded for element in 1:n_elements
@views @. du_euler[2, .., element] -= u_euler[1, .., element] *
u_gravity[2, .., element]
@views @. du_euler[3, .., element] -= u_euler[1, .., element] *
u_gravity[3, .., element]
@views @. du_euler[4, .., element] -= u_euler[1, .., element] *
u_gravity[4, .., element]
@views @. du_euler[5, .., element] -= (u_euler[2, .., element] *
u_gravity[2, .., element] +
u_euler[3, .., element] *
u_gravity[3, .., element] +
u_euler[4, .., element] *
u_gravity[4, .., element])
end
else
error("Number of dimensions $(ndims(semi_euler)) not supported.")
end
Expand Down Expand Up @@ -328,8 +347,11 @@ function timestep_gravity_2N!(cache, u_euler, tau, dtau, gravity_parameters,

# Note that `u_ode` is `u_gravity` in `rhs!` above
@unpack u_ode, du_ode, u_tmp1_ode = cache
n_elements = size(u_euler)[end]

u_tmp1_ode .= zero(eltype(u_tmp1_ode))
du_gravity = wrap_array(du_ode, semi_gravity)

for stage in eachindex(c)
tau_stage = tau + dtau * c[stage]

Expand All @@ -342,7 +364,10 @@ function timestep_gravity_2N!(cache, u_euler, tau, dtau, gravity_parameters,
# put in gravity source term proportional to Euler density
# OBS! subtract off the background density ρ_0 (spatial mean value)
# Note: Adding to `du_gravity` is essentially adding to `du_ode`!
@views @. du_gravity[1, .., :] += grav_scale * (u_euler[1, .., :] - rho0)
@threaded for element in 1:n_elements
@views @. du_gravity[1, .., element] += grav_scale *
(u_euler[1, .., element] - rho0)
end

a_stage = a[stage]
b_stage_dtau = b[stage] * dtau
Expand Down Expand Up @@ -384,9 +409,12 @@ function timestep_gravity_3Sstar!(cache, u_euler, tau, dtau, gravity_parameters,

# Note that `u_ode` is `u_gravity` in `rhs!` above
@unpack u_ode, du_ode, u_tmp1_ode, u_tmp2_ode = cache
n_elements = size(u_euler)[end]

u_tmp1_ode .= zero(eltype(u_tmp1_ode))
u_tmp2_ode .= u_ode
du_gravity = wrap_array(du_ode, semi_gravity)

for stage in eachindex(c)
tau_stage = tau + dtau * c[stage]

Expand All @@ -399,7 +427,10 @@ function timestep_gravity_3Sstar!(cache, u_euler, tau, dtau, gravity_parameters,
# put in gravity source term proportional to Euler density
# OBS! subtract off the background density ρ_0 around which the Jeans instability is perturbed
# Note: Adding to `du_gravity` is essentially adding to `du_ode`!
@views @. du_gravity[1, .., :] += grav_scale * (u_euler[1, .., :] - rho0)
@threaded for element in 1:n_elements
@views @. du_gravity[1, .., element] += grav_scale *
(u_euler[1, .., element] - rho0)
end

delta_stage = delta[stage]
gamma1_stage = gamma1[stage]
Expand Down
Loading