# Creating functions with ModelingToolkit for parallel usage

**URL:** <https://discourse.julialang.org/t/creating-functions-with-modelingtoolkit-for-parallel-usage/27632>\
**Category:** Modelling & Simulations\
**Created:** [August 16, 2019, 10:08pm UTC](https://discourse.julialang.org/t/creating-functions-with-modelingtoolkit-for-parallel-usage/27632 "2019-08-16T22:08:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [August 16, 2019, 10:08pm UTC](https://discourse.julialang.org/t/creating-functions-with-modelingtoolkit-for-parallel-usage/27632/1 "2019-08-16T22:08:38Z")

</div>

Hi,  
after figuring out how to use ModelingToolkit to create functions/derivates etc. I would like to run code generated by ModelingToolkit in parallel. E.g. solving the Lorenz system for multiple initial states, see the following example:

```julia
using Distributed 
using ModelingToolkit
using DifferentialEquations

# create the Lorenz system
eqs_mt = []
push!(eqs_mt, dt(x) ~ lorenz_mt(x,y,z)[1])
push!(eqs_mt, dt(y) ~ lorenz_mt(x,y,z)[2])
push!(eqs_mt, dt(z) ~ lorenz_mt(x,y,z)[3])
de_mt = ODESystem(eqs_mt)
ode_func_mt = ODEFunction(de_mt, [x,y,z], [sigma,rho,beta])

u0 = [19.,20.,50.]
params = [16.,45.92,4]

ode_prob_mt = ODEProblem(ode_func_mt, u0, (0., 10.),params)

# add processes to workspace
addprocs(2)

@everywhere begin
    
    using DifferentialEquations
    using ModelingToolkit
    
    function solve_lorenz(ode_problem)
        print(solve(ode_problem))
    end
end

```

This code errors with

> On worker 2:  
> UndefVarError: ###375 not defined  
> deserialize\_datatype at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:1115  
> handle\_deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:771  
> deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:731  
> deserialize\_datatype at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:1139  
> handle\_deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:771  
> deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:731  
> deserialize\_datatype at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:1139  
> handle\_deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:771  
> deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:731  
> deserialize\_datatype at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:1139  
> handle\_deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:771  
> deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:731  
> handle\_deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:778  
> deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:731  
> #3 at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:869  
> ntuple at ./tuple.jl:136  
> deserialize\_tuple at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:869  
> handle\_deserialize at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:761  
> deserialize\_msg at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Serialization/src/Serialization.jl:731  
> #invokelatest#1 at ./essentials.jl:742 [inlined]  
> invokelatest at ./essentials.jl:741 [inlined]  
> message\_handler\_loop at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Distributed/src/process\_messages.jl:160  
> process\_tcp\_streams at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.1/Distributed/src/process\_messages.jl:117  
> #105 at ./task.jl:259

I assume that the generated function, with name `###375`, is not available on the worker process as it was created on the main process. Is there a possibility to make this function available to worker processes without calling `ODEFunction`? I would like to avoid costly generation of Jacobians, etc. on each worker process, when they are already precalculated on the main process.

---

<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:** [August 17, 2019, 12:51am UTC](https://discourse.julialang.org/t/creating-functions-with-modelingtoolkit-for-parallel-usage/27632/2 "2019-08-17T00:51:58Z")

</div>

I need a better answer two for how to message pass a generic function. That’s really all it is. MWE: I already created `f(x) = x` locally, how to send it after?

---

<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 3, 2019, 12:42pm UTC](https://discourse.julialang.org/t/creating-functions-with-modelingtoolkit-for-parallel-usage/27632/3 "2019-09-03T12:42:27Z")

</div>

Can you open an issue with a reproducible example? I think I have a way to fix this but I’m not sure if it’s the same as your issue.

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [September 4, 2019, 9:09pm UTC](https://discourse.julialang.org/t/creating-functions-with-modelingtoolkit-for-parallel-usage/27632/4 "2019-09-04T21:09:55Z")

</div>

I filed an issue on [https://github.com/JuliaDiffEq/ModelingToolkit.jl/issues/174](https://github.com/JuliaDiffEq/ModelingToolkit.jl/issues/174)

---

<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 5, 2019, 4:22am UTC](https://discourse.julialang.org/t/creating-functions-with-modelingtoolkit-for-parallel-usage/27632/5 "2019-09-05T04:22:08Z")

</div>

Fixed by [https://github.com/JuliaDiffEq/ModelingToolkit.jl/pull/173](https://github.com/JuliaDiffEq/ModelingToolkit.jl/pull/173) . Requires the new GG.jl to get registered!
