Hello, I’m trying to build hydrodynamic codes in 1D, 2D and 3D in Julia. So far I have the codes working with first order solvers, and I’ve managed to make the 1D and 2D faster than Python and C++, now I’m trying to implement a new solver called HLL (and HLLC) and I’ve been using @turbo
to improve the performance of the loops I have in the code, but it seems that I can’t use @turbo
if I have an if statement inside the for. This is the code I need to optimize:
function p2f(F,UPr)
F[1] = UPr[1]*UPr[2]
F[2] = UPr[1]*UPr[2]*UPr[2] + UPr[4]
F[3] = UPr[1]*UPr[2]*UPr[3]
F[4] = UPr[2]*(0.5*UPr[1]*(UPr[2]^2 + UPr[3]^2) +
gamma/(gamma-1)*UPr[4])
end
function p2g(F,UPr)
F[1] = UPr[1]*UPr[3]
F[2] = UPr[1]*UPr[2]*UPr[3]
F[3] = UPr[1]*UPr[3]*UPr[3] + UPr[4]
F[4] = UPr[3]*(0.5*UPr[1]*(UPr[2]^2 + UPr[3]^2) +
gamma/(gamma-1)*UPr[4])
end
function HLLFluxes(U,F,G,UPrim)
FL = zeros(Float64, neq)
FR = zeros(Float64, neq)
GL = zeros(Float64, neq)
GR = zeros(Float64, neq)
wavespeed(UPrim)
for i in 1:nx+1
for j in 1:ny+1
if sl[i,j] >= 0
@views p2f(F[:,i,j],UPrim[:,i,j])
elseif sr[i,j] <= 0
@views p2f(F[:,i,j],UPrim[:,i+1,j])
else
@views p2f(FL,UPrim[:,i,j])
@views p2f(FR,UPrim[:,i+1,j])
@views F[:,i,j] = (sr[i,j]*FL[:] .- sl[i,j]*FR[:] .+ sl[i,j]*sr[i,j]*(U[:,i+1,j] .- U[:,i,j]))./(
sr[i,j]-sl[i,j])
end
if sd[i,j] >= 0
@views p2g(G[:,i,j],UPrim[:,i,j])
elseif su[i,j] <= 0
@views p2g(G[:,i,j],UPrim[:,i,j+1])
else
@views p2g(GL,UPrim[:,i,j])
@views p2g(GR,UPrim[:,i,j+1])
@views G[:,i,j] = (su[i,j]*GL[:] .- sd[i,j]*GR[:] .+ sd[i,j]*su[i,j]*(U[:,i,j+1] .- U[:,i,j]))./(
su[i,j]-sd[i,j])
end
end
end
end
The first two functions only make the code more readable, the third function has the conditions of when the code takes the fluxes from the “left” or the “right” but since i need this condition, I can’t use the turbo macro to accelerate the code. I also tried doing A = sl[i,j] .>= 0
to get a BitMatrix and do the operations from p2f
and p2g
but didn’t improve the performance, so is there a way to use turbo with an if statement or there’s another way to ptimize the function HLLFluxes? Thanks in advance.