In multibody models, many parameters are sparse, e.g., arrays like [1, 0, 0] or diagonal inertia matrices. Allowing JSCompiler to make use of this information when simplifying the system and generating code can lead to a 2x performance boost for multibody simulations. The downside is of course that those parameters cannot be changed without recompiling. It's thus only worthwhile for simulations where runtime is longer than compile time.
To see why this makes such a big difference, consider the expression R'*I*R where the matrices are both 3x3. Without any structure, this expands to the computation
The PR #123 adds an explicit option to handle structural zeros in inertia matrices only, but this PR adds more of a nuclear option using JuliaSimCompiler.freeze_parameters, replacing all parameters that have the value 0 or 1 with their value. For a simple model of a car, this improved both simulation and RHS time by 2x.
Code for the above
@parameters R[1:3, 1:3] I[1:3, 1:3];
R,I = collect.((R,I));
collect(R'I*R)
# # With known zeros
@parameters r[1:3];
r = collect(r);
Rr = Diagonal(collect(r));
collect(Rr'I*Rr)
@parameters i[1:3];
i = collect(i);
Ii = Diagonal(collect(i));
R'Ii*R
Status: Waiting for JSCompiler release
In multibody models, many parameters are sparse, e.g., arrays like
[1, 0, 0]
or diagonal inertia matrices. Allowing JSCompiler to make use of this information when simplifying the system and generating code can lead to a 2x performance boost for multibody simulations. The downside is of course that those parameters cannot be changed without recompiling. It's thus only worthwhile for simulations where runtime is longer than compile time.To see why this makes such a big difference, consider the expression
R'*I*R
where the matrices are both 3x3. Without any structure, this expands to the computationIf we know that
R
is diagonal (very common case), we getIf we know that
I
is diagonal we getand if both are diagonal, we get
The PR #123 adds an explicit option to handle structural zeros in inertia matrices only, but this PR adds more of a nuclear option using
JuliaSimCompiler.freeze_parameters
, replacing all parameters that have the value 0 or 1 with their value. For a simple model of a car, this improved both simulation and RHS time by 2x.Code for the above