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

time consuming function "addToResult!" #154

Open
johhell opened this issue Aug 29, 2022 · 0 comments
Open

time consuming function "addToResult!" #154

johhell opened this issue Aug 29, 2022 · 0 comments

Comments

@johhell
Copy link
Contributor

johhell commented Aug 29, 2022

test case

instModel.result.n_w_invariant = 149
instModel.nsegments = 1
length(instModel.x_start) = 17
number of result points = 5509

timing

... Timings for simulation of SMtest (without initialization):
 ─────────────────────────────────────────────────────────────────────────────────────────
                                                               Time          Allocations  
                                                         ───────────────   ───────────────
                     Total measured:                          2.50s            1.03GiB    

 Section                                         ncalls     time    %tot     alloc    %tot
 ─────────────────────────────────────────────────────────────────────────────────────────
 Modia.simulate!                                      1    2.40s   96.2%   0.98GiB   94.8%
 DifferentialEquations.solve                          1    2.40s   96.2%   0.98GiB   94.8%
 Modia getDerivatives!                            17.6k    2.38s   95.2%   1.03GiB   99.5%*
*Modia addToResult!                               5.51k    1.55s   62.0%   0.97GiB   93.9%   <<====
 Modia LinearEquationsIteration!                  87.8k    167ms    6.7%   4.02MiB    0.4%
 Modia.init!                                          1   95.6ms    3.8%   55.1MiB    5.2%
 Modia LinearEquationsIteration! (solve A*x=b)    52.7k   78.1ms    3.1%   4.02MiB    0.4%
 Modia eventIteration!                                1   91.5μs    0.0%   2.56KiB    0.0%
 DifferentialEquations.ODEProblem                     1   1.23μs    0.0%     0.00B    0.0%
 ─────────────────────────────────────────────────────────────────────────────────────────
  2.498988 seconds (2.94 M allocations: 1.034 GiB, 6.49% gc time)

modified addToResult!

function addToResult!(m::SimulationModel{FloatType,TimeType}, x, time, w_invariant...)::Nothing where {FloatType,TimeType}
the result of getDerivatives is called w_invariant and has the type Tuple, which is immutable. So it can be used directly (without deepcopy) in:

#     push!(result.w_invariant[end], deepcopy(w_invariant))
    push!(result.w_invariant[end], w_invariant)

timing with modified addToResult!

... Timings for simulation of SMtest (without initialization):
 ─────────────────────────────────────────────────────────────────────────────────────────
                                                               Time          Allocations  
                                                         ───────────────   ───────────────
                     Total measured:                          943ms            88.5MiB    

 Section                                         ncalls     time    %tot     alloc    %tot
 ─────────────────────────────────────────────────────────────────────────────────────────
 Modia getDerivatives!                            17.6k    859ms   91.2%   83.7MiB   94.6%
 Modia.simulate!                                      1    853ms   90.6%   33.4MiB   37.8%
 DifferentialEquations.solve                          1    853ms   90.5%   33.3MiB   37.6%
 Modia LinearEquationsIteration!                  87.8k    160ms   17.0%   4.02MiB    4.5%
 Modia.init!                                          1   88.8ms    9.4%   55.1MiB   62.2%
 Modia LinearEquationsIteration! (solve A*x=b)    52.7k   74.4ms    7.9%   4.02MiB    4.5%
*Modia addToResult!                               5.51k   65.5ms    6.9%   23.8MiB   26.9%   <<====
 Modia eventIteration!                                1   85.6μs    0.0%   2.56KiB    0.0%
 DifferentialEquations.ODEProblem                     1   1.30μs    0.0%     0.00B    0.0%
 ─────────────────────────────────────────────────────────────────────────────────────────
  0.944182 seconds (1.02 M allocations: 88.559 MiB)

time consumption and storage allocation was reduced dramatically

postscript

in previous Modia versions (<=0.8.4) the same approach was used.

function addToResult!(m::SimulationModel, variableValues...)::Nothing
    push!(m.result_vars , variableValues)
    push!(m.result_der_x, deepcopy(m.der_x))
    return nothing
end

used versions

Modia version = "0.9.4-dev"
Julia = 1.7.1

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

1 participant