Dear All,
I have been trying to speed up the following piece of code for a FVM method. I used Threads in the first for-loop but it is slower. Ideally, I would like it to be parallel. So I am about to re-write it using sparse matrix-vector products but it is quite a rewrite…
Before I do this, I would like to be sure there is not something better to do,
Thank you for your suggestions,
Best regards
Romain
function rhs!(divergence,Nv,Nw,invDv,invDw,g,V,W)
# this is type stable!!
@inbounds for i=1:Nv
for j= Nw:-1:1
# Values
gij = g[j,i]
gimj = i > 1 ? g[j,i-1]:zero(gij)
gipj = (i+1) <= Nv ? g[j,i+1]:zero(gij)
gijp = j+1 < Nw ? g[j+1,i]:zero(gij)
gijm = j-1 >= 1 ? g[j-1,i]:zero(gij)
# Upwind fluxes + null flux boundary conditions
Fij = i > 1 ? (V[j,i]>=0. ? V[j,i]*gimj: V[j,i] *gij): zero(gij)
Fipj = i < Nv ? (V[j,i+1]>=0.? V[j,i+1]*gij:V[j,i+1]*gipj):zero(gij)
Gijp = j < Nw ? (W[j+1,i]>0. ? W[j+1,i]*gij:W[j+1,i]*gijp):zero(gij)
Gij = j>1 ? (W[j,i]>0. ? W[j,i]*gijm: W[j,i] *gij): zero(gij)
# Divergence
divergence[j,i] = invDv*(Fipj - Fij) + invDw*(Gijp - Gij)
end
end
end
N = 2000
divergence = rand(N,N)
g = rand(N,N)
V = rand(N,N)
W = rand(N,N)
@time rhs!(divergence,N,N,1.,1.,g,V,W)