using OrdinaryDiffEq
n = 2^18;
u0 = rand(ComplexF64, n);
function f(du, u, p, t)
for i in 1:length(u)
du[i] = -1.0im * u[i]
end
return
end
mutable struct A
u0::Vector{ComplexF64}
integrator
function A(u0)
a = new()
a.u0 = u0
prob = ODEProblem(f, u0, (0, 1), nothing)
a.integrator = init(prob, Tsit5(), save_on=false, save_start=false, save_end=false, calck=false, alias_u0=true, abstol=1e-8, reltol=1e-8);
return a
end
end
function g(u1, u2)
x = 0.0im
for i in 1:length(u1)
x += u1[i] * conj(u2[i])
end
return x
end
function g2(a::A)
u1 = a.u0
u2 = a.integrator.u
x = 0.0im
for i in 1:length(u1)
x += u1[i] * conj(u2[i])
end
return x
end
a = A(u0);
step!(a.integrator, 1, true)
Basically, g() and g2() calculate the dot product of the initial and final states. (Yes, I could have used dot(), but this is a prototype.) Naively, I expect they behave similarly. It turns out that g2() allocates extra memory in each call. Can someone please give me some insight? Thanks!
Thanks for the suggestions @ChrisRackauckas. I misremembered. I actually passed a as the last parameter to ODEProblem() (replacing nothing) in the real code. Because Julia doesn’t support mutual reference, I defined a.integrator not in the constructor but in a separate function.