A little demo:
The thing I am trying to wrap my head around is that I observed that a broadcast to combine three vectors would allocate in a loop:
@time for step in 1:nsteps
@. U1 = U0 + dt*V0 + ((dt^2)/2)*A0;
end
yields
3.080494 seconds (130.00 k allocations: 4.883 MiB)
The number of allocations grows linearly with the upper bound of the range (nsteps
).
The code is not global, the loop exists inside of function.
The problem is that this doesn’t show in a trivial code, only in my original code.
I made these observations: Recall that the loop refers to the variable dt
. The allocation trigger is this: If I compute omega_max
from the solution of an eigenvalue problem, the loop would allocate.
evals, evecs, nconv = eigs(K, M; nev=1, which=:LM, explicittransform=:none)
@show omega_max = sqrt(evals[1])
# omega_max = 2.76450e+06
@show dt = Float64(0.99* 2/omega_max)
@show typeof(dt)
On the other hand, if I set omega_max
to a fixed value, the loop does not allocate.
evals, evecs, nconv = eigs(K, M; nev=1, which=:LM, explicittransform=:none)
# @show omega_max = sqrt(evals[1])
omega_max = 2.76450e+06
@show dt = Float64(0.99* 2/omega_max)
@show typeof(dt)
Did I mention that it was weird?