# Use multithreaded Jacobian function generated with ModelingToolKit.jl for application in DifferentialEquations.jl

**URL:** https://discourse.julialang.org/t/use-multithreaded-jacobian-function-generated-with-modelingtoolkit-jl-for-application-in-differentialequations-jl/42317
**Category:** General Usage
**Tags:** diffeq
**Created:** [June 30, 2020, 6:24pm UTC](https://discourse.julialang.org/t/use-multithreaded-jacobian-function-generated-with-modelingtoolkit-jl-for-application-in-differentialequations-jl/42317 "2020-06-30T18:24:34Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![luis\_munoz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_munoz/32/8146_2.png) [@luis\_munoz](https://discourse.julialang.org/u/luis_munoz)
#### Post date: [June 30, 2020, 6:24pm UTC](https://discourse.julialang.org/t/use-multithreaded-jacobian-function-generated-with-modelingtoolkit-jl-for-application-in-differentialequations-jl/42317/1 "2020-06-30T18:24:35Z")

</div>

Hi,

I would like to complete the example PDE in the [SciML News](https://sciml.ai/2020/06/01/ModelingToolkit.html) (also in the [ModelingToolKit.jl documentation](https://mtk.sciml.ai/stable/highlevel/#)), section “ModelingToolkit Automatic Parallelism”, on automatically computed multithreaded Jacobian functions via `ModelingToolkit.build_function` with the objective to speed-up ODE simulations in DifferentialEquations.jl. I follow the steps and obtain the function `multithreadedjac`. I then go on with

```julia
#=
previous code, see provided links
...
multithreadedjac = eval(ModelingToolkit.build_function(vec(jac),u,multithread=true)[2])
=#
MyA = zeros(N,N)
AMx = zeros(N,N)
DA = zeros(N,N)
# Define the initial condition as normal arrays
u0 = rand(N,N,3)
#solve 
using DifferentialEquations
prob_jac = ODEProblem(ODEFunction(f!,jac = multithreadedjac),u0,(0.0,10.0))
solve(prob_jac)

```

leading to a  
`ERROR: MethodError: no method matching (::var"#31#32")(::Array{Float64,2}, ::Array{Float64,3}, ::DiffEqBase.NullParameters, ::Float64)...`

Any assistance on how to successfully translate back the multithreaded Jacobian for applications as DiffEq ODE is much appreciated.

I am using Julia v1.4, DifferentialEquations.jl v6.14.0 and ModelingToolKit.jl v3.10.2

---

<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: [July 4, 2020, 7:13am UTC](https://discourse.julialang.org/t/use-multithreaded-jacobian-function-generated-with-modelingtoolkit-jl-for-application-in-differentialequations-jl/42317/2 "2020-07-04T07:13:47Z")

</div>

Hey,  
Sorry it took a bit to respond. I was redoing the documentation, and in the new version the way to do this is much more explicit:

[https://mtk.sciml.ai/dev/tutorials/auto\_parallel/](https://mtk.sciml.ai/dev/tutorials/auto_parallel/)

Cheers!

---

<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: [July 4, 2020, 7:36am UTC](https://discourse.julialang.org/t/use-multithreaded-jacobian-function-generated-with-modelingtoolkit-jl-for-application-in-differentialequations-jl/42317/3 "2020-07-04T07:36:56Z")

</div>

P.S. I found a sync bug when writing that so [https://github.com/SciML/ModelingToolkit.jl/pull/488](https://github.com/SciML/ModelingToolkit.jl/pull/488) is needed for the tutorial to work if you solve multiple times in rapid succession… I’ll get this patched in a few hours.

---

<div class="post-metadata">

### Author: ![luis\_munoz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_munoz/32/8146_2.png) [@luis\_munoz](https://discourse.julialang.org/u/luis_munoz)
#### Post date: [July 7, 2020, 1:29pm UTC](https://discourse.julialang.org/t/use-multithreaded-jacobian-function-generated-with-modelingtoolkit-jl-for-application-in-differentialequations-jl/42317/4 "2020-07-07T13:29:20Z")

</div>

Thanks @ChrisRackauckas for the quick update of the documentation. It makes the application of efficient multithreading (or parallel computing) straightforward with DifferentialEquations.jl!  
Regarding the actual computation of the jacobian using the function `ModelingToolkit.jacobian()`, is there any chance to release a version which directly considers ODE sparsity rather than applying `sparse()` to the dense jacobian generated (as it has been done with other ModelingToolkit.jl functions using “sparse” as a keyword argument)? There should be benefits for larger systems where jacobian calculations can take a while (my case).

---

<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: [July 7, 2020, 1:36pm UTC](https://discourse.julialang.org/t/use-multithreaded-jacobian-function-generated-with-modelingtoolkit-jl-for-application-in-differentialequations-jl/42317/5 "2020-07-07T13:36:53Z")

</div>

@luis_munoz yeah… @shashi was working on that today 🙂

[https://github.com/JuliaSymbolics/SymbolicUtils.jl/issues/61#issuecomment-654826458](https://github.com/JuliaSymbolics/SymbolicUtils.jl/issues/61#issuecomment-654826458)

---

<div class="post-metadata">

### Author: ![luis\_munoz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_munoz/32/8146_2.png) [@luis\_munoz](https://discourse.julialang.org/u/luis_munoz)
#### Post date: [July 7, 2020, 1:45pm UTC](https://discourse.julialang.org/t/use-multithreaded-jacobian-function-generated-with-modelingtoolkit-jl-for-application-in-differentialequations-jl/42317/6 "2020-07-07T13:45:30Z")

</div>

Great! I’ll keep an eye on the progress then 👍
