Trying to serialize runtime generated function and MTK ODESystem

I have a system with around 1500 equations. It takes a long time to run the following function, so I want to serialize and deserialize f_ip, dvs, psym and io_sys so I can use them between sessions.

(_, f_ip), dvs, psym, io_sys = ModelingToolkit.generate_control_function(sys, inputs)

However, using deserialize("sys.bin") takes 20 minutes. f_ip is a runtime generated function and io_sys is a modelingtoolkit ODESystem. dvs and psym are small objects. Most of the time of deserialization is used on f_ip and io_sys.

How can I speed up the deserialization significantly? Can I reduce the file size of the serialization, in order to speed up deserialization? Somehow, β€œsys.bin” ends up at almost 3GB in storage.

I tried using write and include as discussed here: Using Serialization to store then load a lambdified function? - #2 by SteffenPL
But this gave me a syntax error in the β€œsys.jl” file:
#β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ── Expected ")"

The easiest way to do this would be to use the JuliaSimCompiler with the LLVM backend, since that would create a binary and the serialization is then exactly that binary. You could then just load the DLL into a new session and the function would come right up pretty instantly. @baggepinnen I know this (JSC control functions) just merged, is there any docs on it though?

1 Like

Thanks. Some instructions on how to do this and/or some docs would be amazing!

There are no JSC specific docs, it mostly follows the same interface but for IRSystems instead of ODESystem. The need to serialize the function will likely go away with JSCompiler since JSC is just much faster than MTK for this kind of thing, <2000 equations is rather small.

    @time f_ip, dvs, psym, io_sys = ModelingToolkit.generate_control_function(IRSystem(model), inputs)
    10.705200 seconds (10.94 M allocations: 710.725 MiB, 2.29% gc time, 94.28% compilation time: 27% of which was recompilation)

This solved the speed problem, thanks.