# Autodifferencing and type stability in DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/autodifferencing-and-type-stability-in-differentialequations-jl/74773>\
**Category:** New to Julia\
**Tags:** diffeq\
**Created:** [January 17, 2022, 9:29pm UTC](https://discourse.julialang.org/t/autodifferencing-and-type-stability-in-differentialequations-jl/74773 "2022-01-17T21:29:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brynn](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@brynn](https://discourse.julialang.org/u/brynn)\
**Post date:** [January 17, 2022, 9:29pm UTC](https://discourse.julialang.org/t/autodifferencing-and-type-stability-in-differentialequations-jl/74773/1 "2022-01-17T21:29:46Z")

</div>

I’m working to optimize a solver for a large, stiff, system of differential equations using DifferentialEquations.jl. I’ve been following through the guide on Code Optimization in the documentation, and have been trying to minimize my allocations. I’m using QNDF() as my solver and I’ve found that I can’t just preallocate all of my arrays because u and du change type from Vector{Float64} to something of type Float64(::ForwardDiff.Dual{ForwardDiff.Tag{OrdinaryDiffEq.OrdinaryDiffEqTag, Float64}, Float64, 12}}

Currently, in my ODEFunction, I test for the type of du, and allocate a new array if it is not of type Vector{Float64}. I was wondering if there is a more efficient approach to this. I have tried turning autodifferencing off, by setting QNDF(autodiff=false), but this doesn’t improve my overall solver speed.

My ODEFunction roughly looks like this - I can post a better example if needed. I’m mostly just wondering if there’s any documentation I’m missing that helps handle this change of type in a memory efficient way.

```julia
function odefunc!(du, u, p, t)
    #load parameters 
    L,B,Lu,Bu= p

    #check type
    if typeof(du) != Vector{Float64}
        Lu = similar(du)
        Bu = similar(Lu)
    end

    mul!(Lu, L, u)
    mul!(Bu, B, u)
    @. du = Lu + Bu
    nothing
end

```

Any thoughts appreciated!

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 17, 2022, 9:39pm UTC](https://discourse.julialang.org/t/autodifferencing-and-type-stability-in-differentialequations-jl/74773/2 "2022-01-17T21:39:26Z")

</div>

Hi @brynn, welcome!

I don’t believe I can check your [MWE](https://www.google.com/search?client=firefox-b-d&q=julia+discourse+mwe), but at least I’m missing how `BF` is used?

Edit: ah, OK! But there are still some parts missing?

---

<div class="post-metadata">

**Author:** ![brynn](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@brynn](https://discourse.julialang.org/u/brynn)\
**Post date:** [January 17, 2022, 9:56pm UTC](https://discourse.julialang.org/t/autodifferencing-and-type-stability-in-differentialequations-jl/74773/3 "2022-01-17T21:56:57Z")

</div>

Sorry - I removed most of the code to just try and show what my real question is. I can come up with code that actually can be run to show what my problem is (it will just take me a little while to figure that out!)  
Not sure if this clarification helps at all but: in my main script, Lu and Bu are instantiated as type Vector{Float64} and then passed in as a parameter, so that each time the solver calls my ODEFunction, it should reuse those variables and not allocate anything new. However, for some timesteps, u and du are not simple vectors and when multiplied by u, the result can’t be allocated into Lu and Bu because it’s the wrong type. I wasn’t sure if this was a common problem and if there is a cleaner solution than what I’m doing.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 17, 2022, 10:01pm UTC](https://discourse.julialang.org/t/autodifferencing-and-type-stability-in-differentialequations-jl/74773/4 "2022-01-17T22:01:26Z")

</div>

Thank you!

If you try to come up with a MWE you either will find a problem yourself or help us to reproduce and (hopefully;) improve. As far as I know type instability should not be a problem in your case.

> [@brynn](#):
>
> However, for some timesteps, u and du are not simple vectors

This sounds unusual however. Maybe convert them before in this 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:** [January 17, 2022, 10:55pm UTC](https://discourse.julialang.org/t/autodifferencing-and-type-stability-in-differentialequations-jl/74773/5 "2022-01-17T22:55:43Z")

</div>

> [@brynn](#):
>
> I’ve found that I can’t just preallocate all of my arrays because u and du change type from Vector{Float64} to something of type Float64(::ForwardDiff.Dual{ForwardDiff.Tag{OrdinaryDiffEq.OrdinaryDiffEqTag, Float64}, Float64, 12}}

Check out PreallocationTools.jl. See the FAQ on this.

[https://diffeq.sciml.ai/stable/basics/faq/#I-get-Dual-number-errors-when-I-solve-my-ODE-with-Rosenbrock-or-SDIRK-methods](https://diffeq.sciml.ai/stable/basics/faq/#I-get-Dual-number-errors-when-I-solve-my-ODE-with-Rosenbrock-or-SDIRK-methods)

> **[GitHub - SciML/PreallocationTools.jl: Tools for building non-allocating...](https://github.com/SciML/PreallocationTools.jl)**
>
> Tools for building non-allocating pre-cached functions in Julia, allowing for GC-free usage of automatic differentiation in complex codes - GitHub - SciML/PreallocationTools.jl: Tools for building ...

I should probably add an example on this to that tutorial.

---

<div class="post-metadata">

**Author:** ![brynn](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@brynn](https://discourse.julialang.org/u/brynn)\
**Post date:** [January 18, 2022, 2:03am UTC](https://discourse.julialang.org/t/autodifferencing-and-type-stability-in-differentialequations-jl/74773/6 "2022-01-18T02:03:40Z")

</div>

Thanks for pointing that out, it worked really well!
