ModelingToolkit.jl icon indicating copy to clipboard operation
ModelingToolkit.jl copied to clipboard

Homogenize parameters by default

Open YingboMa opened this issue 2 years ago • 6 comments

By default, we have something like Vector{Union{Int, Float64, Vector{Float64}, Vector{Int}}} which results in excessive compilation time. We should homogenize it to Tuple{Vector{Int}, Vector{Float64}, Vector{Vector{Float64}}, Vector{Vector{Int}}}}.

I wrote a script here as a prototype https://gist.github.com/YingboMa/72198a5cbb5db17b7e681ffad244b72a

@shashi do you have time to handle this?

CC: @bradcarman

YingboMa avatar Aug 12 '22 16:08 YingboMa

Is this related to another issue?

shashi avatar Aug 21 '22 17:08 shashi

Yes, the problem this solves is Julia can fail to compile a jacobian function which is built with a mixed Union type parameter vector. Julia needs to go thru every element of the parameter vector, determine it's type and compile the expression. With a Tuple that is split into N variables, julia then only needs to compile the N variable types in the jacobian expression. The following folder contains examples, jac3.jl will cause Julia to stall in precompilation, whereas jac3_mod.jl with a single type parameter vector compiles and executes very quickly.

examples.zip

bradcarman avatar Aug 22 '22 14:08 bradcarman

Would this change mean that parameter tuples mixing floats and parameters get promoted to all Float64 internally? That would be undesirable, and I think potentially break, some Catalyst models.

isaacsas avatar Aug 22 '22 15:08 isaacsas

Or is the idea to convert mixed-type parameter vector/tuples into a tuple with the mixed types separated into vector components?

isaacsas avatar Aug 22 '22 15:08 isaacsas

Yes, split into vector components of like types

bradcarman avatar Aug 22 '22 15:08 bradcarman

Would this just be applied to parameter vectors, or to mixed-type parameter tuples too?

isaacsas avatar Aug 22 '22 15:08 isaacsas