# Trying to serialize runtime generated function and MTK ODESystem

**URL:** https://discourse.julialang.org/t/trying-to-serialize-runtime-generated-function-and-mtk-odesystem/119961
**Category:** Modelling & Simulations
**Tags:** serialization
**Created:** [September 27, 2024, 1:20pm UTC](https://discourse.julialang.org/t/trying-to-serialize-runtime-generated-function-and-mtk-odesystem/119961 "2024-09-27T13:20:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)
#### Post date: [September 27, 2024, 1:20pm UTC](https://discourse.julialang.org/t/trying-to-serialize-runtime-generated-function-and-mtk-odesystem/119961/1 "2024-09-27T13:20:41Z")

</div>

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.

```julia
(_, 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](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884/2)  
But this gave me a syntax error in the “sys.jl” file:  
`#─────────────┘ ── Expected ")"`

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 27, 2024, 3:05pm UTC](https://discourse.julialang.org/t/trying-to-serialize-runtime-generated-function-and-mtk-odesystem/119961/2 "2024-09-27T15:05:48Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)
#### Post date: [September 27, 2024, 3:07pm UTC](https://discourse.julialang.org/t/trying-to-serialize-runtime-generated-function-and-mtk-odesystem/119961/3 "2024-09-27T15:07:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [September 27, 2024, 3:15pm UTC](https://discourse.julialang.org/t/trying-to-serialize-runtime-generated-function-and-mtk-odesystem/119961/4 "2024-09-27T15:15:00Z")

</div>

> [@ChrisRackauckas](#):
>
> is there any docs on it though?

> **[Input output · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/dev/basics/InputOutput/#Generating-a-dynamics-function-with-inputs,-f)**
>
> Documentation for ModelingToolkit.jl.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [September 27, 2024, 3:18pm UTC](https://discourse.julialang.org/t/trying-to-serialize-runtime-generated-function-and-mtk-odesystem/119961/5 "2024-09-27T15:18:06Z")

</div>

There are no JSC specific docs, it mostly follows the same interface but for `IRSystem`s 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.

---

<div class="post-metadata">

### Author: ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)
#### Post date: [September 28, 2024, 11:52am UTC](https://discourse.julialang.org/t/trying-to-serialize-runtime-generated-function-and-mtk-odesystem/119961/6 "2024-09-28T11:52:37Z")

</div>

```julia
    @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.
