Here is MWE.
function MWE_1()
h = 0.01
k1 = [rand(ComplexF64,4,4) for _ in 1:500]
k2 = [rand(ComplexF64,4,4) for _ in 1:500]
k3 = [rand(ComplexF64,4,4) for _ in 1:500]
k4 = [rand(ComplexF64,4,4) for _ in 1:500]
ρ = [rand(ComplexF64,4,4) for _ in 1:500]
ρ = h/6 * (k1 + 2*k2 + 2*k3 + k4)
end
@code_warntype MWE_1()
the result is
Arguments
#self#::Core.Const(MWE_1)
Locals
#9::var"#9#14"
#8::var"#8#13"
#7::var"#7#12"
#6::var"#6#11"
#5::var"#5#10"
ρ::Any
k4::Vector{Matrix{ComplexF64}}
k3::Vector{Matrix{ComplexF64}}
k2::Vector{Matrix{ComplexF64}}
k1::Vector{Matrix{ComplexF64}}
h::Float64
Body::Any
this type instability problem is solved by adding @.
function MWE_2()
h = 0.01
k1 = [rand(ComplexF64,4,4) for _ in 1:500]
k2 = [rand(ComplexF64,4,4) for _ in 1:500]
k3 = [rand(ComplexF64,4,4) for _ in 1:500]
k4 = [rand(ComplexF64,4,4) for _ in 1:500]
ρ = [rand(ComplexF64,4,4) for _ in 1:500]
@. ρ = h/6 * (k1 + 2*k2 + 2*k3 + k4)
end
like below
Arguments
#self#::Core.Const(MWE_2)
Locals
#19::var"#19#24"
#18::var"#18#23"
#17::var"#17#22"
#16::var"#16#21"
#15::var"#15#20"
ρ::Vector{Matrix{ComplexF64}}
k4::Vector{Matrix{ComplexF64}}
k3::Vector{Matrix{ComplexF64}}
k2::Vector{Matrix{ComplexF64}}
k1::Vector{Matrix{ComplexF64}}
h::Float64
Body::Vector{Matrix{ComplexF64}}
but this makes another problem of unknown memory allocation.
- function MWE_2()
- h = 0.01
0 k1 = [rand(ComplexF64,4,4) for _ in 1:500]
0 k2 = [rand(ComplexF64,4,4) for _ in 1:500]
0 k3 = [rand(ComplexF64,4,4) for _ in 1:500]
0 k4 = [rand(ComplexF64,4,4) for _ in 1:500]
0 ρ = [rand(ComplexF64,4,4) for _ in 1:500]
576000 @. ρ = h/6 * (k1 + 2*k2 + 2*k3 + k4)
- end
-
- # @code_warntype MWE_1()
- # @code_warntype MWE_2()
-
- using Profile
- Profile.clear_malloc_data()
- MWE_2()