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

refactor: improve getu performance for vectors involving observed quantities #41

Merged
merged 1 commit into from
Jan 30, 2024

Conversation

AayushSabharwal
Copy link
Member

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

@AayushSabharwal AayushSabharwal linked an issue Jan 30, 2024 that may be closed by this pull request
@AayushSabharwal
Copy link
Member Author

julia> using ModelingToolkit, SymbolicIndexingInterface, OrdinaryDiffEq
julia> using BenchmarkTools
julia> iv = only(@variables(t))
julia> sts = @variables s1(t) = 2.0 s1s2(t) = 2.0 s2(t) = 2.0
julia> ps = @parameters k1 = 1.0 c1 = 2.0
julia> eqs = [(Differential(t))(s1) ~ -0.25 * c1 * k1 * s1 * s2
           (Differential(t))(s1s2) ~ 0.25 * c1 * k1 * s1 * s2
           (Differential(t))(s2) ~ -0.25 * c1 * k1 * s1 * s2]
julia> @named sys = ODESystem(eqs)
julia> prob = ODEProblem(sys, [], (0.0, 1.0))
julia> sol = solve(prob, Tsit5())
julia> syms1 = [s1 + s2, 2s1 + s2, s1 + 2s2, 3s1+s2, 3s1+2s2, 4s2, 5s1, s1s2+3, s1s2];
julia> obs = SymbolicIndexingInterface.observed(sol, syms1)
julia> f = getu(sol, syms1)
julia> @btime obs(sol.u[1], sol.prob.p, sol.t[1]);
  577.575 ns (25 allocations: 3.75 KiB)

julia> @btime f(sol, 1);
  325.369 ns (21 allocations: 800 bytes)

julia> @btime obs.(sol.u, sol.prob.p |> tuple, sol.t);
  2.306 μs (118 allocations: 9.45 KiB)

julia> @btime f(sol);
  2.009 μs (113 allocations: 5.77 KiB)

Somehow getu is faster now. I verified results:

julia> obs(sol.u[1], sol.prob.p, sol.t[1]) == f(sol, 1)
true
julia> obs.(sol.u, sol.prob.p |> tuple, sol.t) == f(sol)
true

Copy link

codecov bot commented Jan 30, 2024

Codecov Report

Attention: 37 lines in your changes are missing coverage. Please review.

Comparison is base (dab25be) 28.35% compared to head (b8c9101) 25.33%.

Files Patch % Lines
src/state_indexing.jl 0.00% 37 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #41      +/-   ##
==========================================
- Coverage   28.35%   25.33%   -3.03%     
==========================================
  Files           7        7              
  Lines         201      225      +24     
==========================================
  Hits           57       57              
- Misses        144      168      +24     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ChrisRackauckas ChrisRackauckas merged commit 7fdf807 into master Jan 30, 2024
9 of 11 checks passed
@ChrisRackauckas ChrisRackauckas deleted the as/getu-perf branch January 30, 2024 14:24
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

Successfully merging this pull request may close these issues.

getu is slower than observed for arrays of observed quantities
2 participants