Okay I realized that it is because that Julia is not able to at runtime determine the eltype(array), when it gets mixed input. It works fine when all inputs are of the same type, then no error is detected.
Any smart way to fix it or I just have to split into two function calls?
EDIT: My current solution:
Enforce the type of variable arguments through Vararg.
function ResetArrays!(arrays::Vararg{Vector{T}}) where T
@inbounds for array in arrays
fill!(array,zero(eltype(array)))
end
end
Split call into two function calls:
# Clean up arrays, Vector{T} and Vector{SVector{3,T}} must be cleansed individually,
# to avoid run time dispatch errors
ResetArrays!(Kernel, dρdtI,dρdtIₙ⁺)
ResetArrays!(KernelGradient, dvdtI, Acceleration)
I am unsure why, but your solution reports no errors and seems to be able to dispatch on type correctly
a -> fill!(a, zero(eltype(a))) is an anonymous function, that acts as a function barrier.
EDIT: This was true, but definitely not the explanation (see @matthias314 comment below). The following still triggers a “runtime dispatch detected” by JET.
In a less compact way, one could create a named function
function reset_array!(a)
fill!(a, zero(eltype(a)))
end
You are right, it looks like the function barrier is not enough;
thanks a lot for the correction.
julia> using JET
julia> function reset_array!(a)
fill!(a, zero(eltype(a)))
end
julia> function reset_all!(arrays)
for a in arrays
reset_array!(a)
end
end
julia> @report_opt reset_all!(Any[[1, 2], [1.0, 2.0]])
═════ 1 possible error found ═════
┌ reset_all!(arrays::Vector{Any}) @ Main ./REPL[14]:3
│ runtime dispatch detected: reset_array!(%19::Any)::Any
└────────────────────
But the foreach does not seem to work for JET either ?
julia> function reset_all_2!(arrays)
foreach(reset_array!, arrays)
end
julia> @report_opt reset_all_2!(Any[[1, 2], [1.0, 2.0]])
┌ reset_all_2!(arrays::Vector{Any}) @ Main ./REPL[27]:2
│┌ foreach(f::typeof(reset_array!), itr::Vector{Any}) @ Base ./abstractarray.jl:3094
││ runtime dispatch detected: f::typeof(reset_array!)(%19::Any)::Any
│└────────────────────