-
-
Notifications
You must be signed in to change notification settings - Fork 224
Big slowdown of ODE with matrix equations #3708
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
Comments
Profiling (after running the first code block above): using ProfileCanvas
@profview sol = solve(prob, Tsit5(); dt = 1e-5, adaptive = false, save_everystep = false) The allocations come from prob.f.f.f_iip # see var"##cse#5" = ...
|
So this is known, but a bit necessary as an intermediate step. Think of just a matrix multiplication. If you scalarize the equations, then: du[1] = A[1,1]*x[1] + A[2,1]*x[2]
du[2] = A[1,2]*x[1] + A[2,2]*x[2] That's not allocating tmp = A*x
du .= tmp That is allocating. But, it will also be O(1) in compile time. So we got array functions working and we're keeping in that direction. Though we probably need to start setting up the functions with a bump allocator or something. That said, the "workaround" for now is just to manually scalarize. The "regression" is now that isn't done automatically, so you can just force it if that's the behavior you need. But down the line, we'll be improving Symbolics.jl's build_function for this kind of behavior, using a bump allocator and Reactant.jl |
Thank you, I see and think that is an excellent direction to go. By scalarizing manually, do you mean to only define scalar variables and write out all the equations for every index? Or is there a way to "scalarize-transform" the equations/system after creating it with array variables? My equations are created by looping over the indices with a user-configurable maximum index, so I really wish for some automatic handling. I do not have success with e.g. sys = ModelingToolkit.scalarize(sys) # this would be very convenient or @named sys = System(ModelingToolkit.scalarize(eqs), t, vcat(ModelingToolkit.scalarize(x)...), []) |
We can probably make an overload that does that. It wouldn't be so hard to do. |
Rather than manually scalarizing, doesn't the |
I think that would work if my equations were of that form, with |
|
Uh oh!
There was an error while loading. Please reload this page.
This "matrix ODE" is much slower and allocates much more than it used to:
For comparison, here is the equivalent "scalar ODE":
This was for a
2x1
matrix. The problem is much worse for larger sizes.The text was updated successfully, but these errors were encountered: