I’m attempting to speed up a residual computation function with FLoops
but am getting some type instability. The resid_simple
function is the version that uses LinearAlgebra
the allocations make it slow. The Floops
version is faster, but I would like to eliminate the type instability.
using FLoops
using LinearAlgebra
function resid_simple(stage1::AbstractArray{T}, stage2::AbstractArray{T}) where{T <: AbstractFloat}
stage1_denom = norm(stage1 , 2)
if isinf(stage1_denom) || iszero(stage1_denom)
resid = NaN
else
resid = norm(stage2 .- stage1, 2) / stage1_denom
end
resid
end
function resid(stage1::AbstractArray{T}, stage2::AbstractArray{T}) where{T <: AbstractFloat}
numerator = zero(T)
stage1_denom = zero(T)
@floop for I in CartesianIndices(stage1)
a = stage1[I]^2
@reduce(stage1_denom += a)
end
stage1_denom = sqrt(stage1_denom)
if isinf(stage1_denom) || iszero(stage1_denom)
resid = 0.0
else
@floop for I in CartesianIndices(stage1)
a = (stage2[I] - stage1[I])^2
@reduce(numerator += a)
end
resid = sqrt(numerator) / stage1_denom
end
resid
end
U = rand(4, 10_000, 10_000);
Unp1 = rand(4, 10_000, 10_000);
ρ = @view U[1,:,:];
ρnp1 = @view Unp1[1,:,:];
@code_warntype resid(ρ,ρnp1)
This gives shows some type instability on the resid
, numerator
and stage1_denom
variables.
Variables
#self#::Core.Const(resid)
stage1::SubArray{Float64, 2, Array{Float64, 3}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, true}
stage2::SubArray{Float64, 2, Array{Float64, 3}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, true}
@_4::Union{}
@_5::Union{}
resid::Any
numerator::Any
grouped_accs#270::Any
result#269::Any
__##combine_function#268::var"#__##combine_function#268#6"
__##reducing_function#267::var"#__##reducing_function#267#5"{SubArray{Float64, 2, Array{Float64, 3}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, true}, SubArray{Float64, 2, Array{Float64, 3}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, true}}
__##oninit_function#266::var"#__##oninit_function#266#4"
stage1_denom::Any
grouped_accs#263::Any
result#262::Any
__##combine_function#261::var"#__##combine_function#261#3"
__##reducing_function#260::var"#__##reducing_function#260#2"{SubArray{Float64, 2, Array{Float64, 3}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, true}}
__##oninit_function#259::var"#__##oninit_function#259#1"
@_19::Any
Body::Any
How do I get rid of this? I’m running Julia 1.6 with FLoops v0.1.10.