SciML / DiffEqCallbacks.jl

A library of useful callbacks for hybrid scientific machine learning (SciML) with augmented differential equation solvers
https://docs.sciml.ai/DiffEqCallbacks/stable/
Other
85 stars 44 forks source link

Memory allocations of `PresetTimeCallback` after v3.2.0 #211

Closed albertomercurio closed 4 months ago

albertomercurio commented 4 months ago

Describe the example

I get a lot of memory allocations, never obtained before v3.2.0

Minimal Reproducible Example 👇

# Define a simple ODE system
function simple_ode!(du, u, p, t)
    mul!(du, p.A, u, p.α[1], 0)
end

# Initial condition
u0 = rand(ComplexF64, 10)
A = rand(ComplexF64, 10, 10)

# Time span for the solution
tlist = range(0, 10, 1000)
tspan = (tlist[1], tlist[end])

p = (A=A,α=rand(2))

# Define the ODE problem
prob = ODEProblem(simple_ode!, u0, tspan, p)

# Callback function to modify the parameter
function change_param!(integrator)
    integrator.p.α[1] = 0.5 # Change the parameter value
end

callback = PresetTimeCallback(tlist, change_param!, save_positions=(false,false))

# Solve the ODE with the callback
sol = solve(prob, Tsit5(), callback=callback);

@time solve(prob, Tsit5(), callback=callback);
0.001125 seconds (26.55 k allocations: 2.361 MiB)

@time solve(prob, Tsit5());
0.000074 seconds (980 allocations: 101.891 KiB)
ChrisRackauckas commented 4 months ago

@oscardssmith could I get help with this? I think it's from the views I added to fix the adjoint floating point issues, but the views should be... fine?

oscardssmith commented 4 months ago

I think the problem here is the fun closure capture performance bug.

oscardssmith commented 4 months ago

This is now fixed On DiffEqCallbacks version 3.1, this gives (9.06 k allocations: 1.926 MiB) and with https://github.com/SciML/DiffEqCallbacks.jl/pull/212, we're at (10.07 k allocations: 1.946 MiB).

ChrisRackauckas commented 4 months ago

That's a typo right?

oscardssmith commented 4 months ago

no 3.1 had 9k allocations, 3.2 had 26k allocations, and main has 10k.