Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange Memory Allocations with saveat #880

Closed
MicheleCeresoli opened this issue Jul 9, 2022 · 1 comment
Closed

Strange Memory Allocations with saveat #880

MicheleCeresoli opened this issue Jul 9, 2022 · 1 comment

Comments

@MicheleCeresoli
Copy link

Hello, I'm observing very strange and large memory allocations while solving a standard ODE and evaluating it at given times. However, after the first benchmark, if (and only if!) I recompile the integrated function these allocations disappear.

The function I'm integrating is:

@inbounds function dyn!(du::Vector{Float64}, u::Vector{Float64}, 
                                 p, t::Float64)
    du[1] =- u[1]
    du[2] = -u[2]
    du[3] = -u[3]
   
    nothing 
end

while test is the function that evaluates the solution at the given times:

import DifferentialEquations: Vern9, ODEProblem, solve
function test(x::Vector{Float64}, tms::Vector{Float64})

    user_prob = ODEProblem(dyn!, x, [0., 86400])
    user_sol = solve(user_prob, Vern9(), abstol=1e-10, reltol=1e-9, saveat=tms, save_everystep=false).u

    return user_sol 
end

Calling it with:

x =  [3., 5., 10];
tms = collect(LinRange(0., 86400., 86400));

using BenchmarkTools
@benchmark test($x, $tms)

yields:

BenchmarkTools.Trial: 30 samples with 1 evaluation.
 Range (min  max):  129.553 ms  535.830 ms  ┊ GC (min  max): 11.32%  74.42%
 Time  (median):     151.338 ms               ┊ GC (median):    14.82%
 Time  (mean ± σ):   167.691 ms ±  71.469 ms  ┊ GC (mean ± σ):  21.46% ± 11.46%

    ██                                                           
  ▆▇██▆▁▆▁▆▄▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄ ▁
  130 ms           Histogram: frequency by time          536 ms <

 Memory estimate: 304.61 MiB, allocs estimate: 432505.

If i recompile dyn! the new benchmark is:

BenchmarkTools.Trial: 81 samples with 1 evaluation.
 Range (min  max):  49.294 ms  106.972 ms  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     59.441 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   62.334 ms ±  12.016 ms  ┊ GC (mean ± σ):  2.41% ± 4.19%

  █ ▁    ▁▁    ▃                                                
  █▆█▇▄▇▇██▇▆▇▇█▁▇▆▆▆▆▄▆▆▆▄▄▁▁▄▁▁▆▆▁▁▁▄▁▁▁▁▁▁▁▆▁▁▄▁▄▄▁▁▁▁▁▁▁▁▄ ▁
  49.3 ms         Histogram: frequency by time         98.8 ms <

 Memory estimate: 17.22 MiB, allocs estimate: 173311.

Any idea what's happening? I see the Garbage Collector is taking quite a lot of time

@MicheleCeresoli MicheleCeresoli changed the title Strange Memory Allocations with DifferentialEquations.jl Strange Memory Allocations with saveat Jul 9, 2022
@ChrisRackauckas
Copy link
Member

I've seen this in other spots, like SciML/OrdinaryDiffEq.jl#1502 . It seems like it's something to do with type inference or inlining being dependent on the method table, and so redefining functions can change what inlines and what does not. But it seems like a core Julia thing and I'm not sure what we could specifically do to avoid it (see some of the links to JuliaLang/julia issues, i.e. JuliaLang/julia#32552 and JuliaLang/julia#35800).

I'm closing this as a case of a Base Julia issue, and thus it's not actionable in this library, but I'll ask some of the folks working on inference what's the current status of fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants