# High allocations count for TwoPointBVProblem in DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/high-allocations-count-for-twopointbvproblem-in-differentialequations-jl/96296>\
**Category:** Modelling & Simulations\
**Tags:** performance, ode, allocations\
**Created:** [March 19, 2023, 8:33am UTC](https://discourse.julialang.org/t/high-allocations-count-for-twopointbvproblem-in-differentialequations-jl/96296 "2023-03-19T08:33:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hawkes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hawkes/32/22365_2.png) [@Hawkes](https://discourse.julialang.org/u/Hawkes)\
**Post date:** [March 19, 2023, 8:33am UTC](https://discourse.julialang.org/t/high-allocations-count-for-twopointbvproblem-in-differentialequations-jl/96296/1 "2023-03-19T08:33:59Z")

</div>

Hi,

I’m currently trying to port some simulation code from Python to Julia. The Python version uses scipy’s [solve\_bvp](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_bvp.html) function to solve a two point boundary value problem.

I am using DifferentialEquations.jl to accomplish this task in Julia. It works and the result matches the solve\_bvp output just fine. However, the Julia version is much, much slower compared to solve\_bvp. I have benchmarked my rhs function and boundary condition to make them as fast as possible, but it seems DifferentialEquations.jl allocates a lot.

Additionally, it seems that TwoPointBVProblems are not correctly handled, as in [BoundaryValueDiffEq.jl/solve.jl at master · SciML/BoundaryValueDiffEq.jl · GitHub](https://github.com/SciML/BoundaryValueDiffEq.jl/blob/master/src/solve.jl#L50) the `isa` condition will always result to false, because the constructor of TwoPointBVProblem always sets the field `problem_type` to `StandardBVProblem`.

I have boiled down my simulation to a short MWE, which showcases the problem (the MWE is not a real two point BV problem mathematically, but this should not be relevant here):

```julia
using DifferentialEquations, BenchmarkTools

a = [1, 2, 3]
b = [1, 1, 1]

function rhs!(du, u, p, t)
    a, _ = p
    for i in axes(u, 1)
        du[i] = u[i] * a[i]
    end
end

function boundary_condition!(residual, u, p, t)
    _, b = p
    @. residual = u[1] - b
end

tspan = (0, 1)

u0 = zeros(3, 11)
u0 = [u0[:,i] for i in axes(u0, 2)]
p = [a, b]
bvp = TwoPointBVProblem(rhs!, boundary_condition!, u0, tspan, p)

@benchmark solve($bvp, MIRK4(), dt=0.1, save_everystep=false)

```

Output:

```julia
BenchmarkTools.Trial: 898 samples with 1 evaluation.
 Range (min … max): 4.562 ms … 8.521 ms ┊ GC (min … max): 0.00% … 37.20%
 Time (median): 4.968 ms ┊ GC (median): 0.00%
 Time (mean ± σ): 5.567 ms ± 1.299 ms ┊ GC (mean ± σ): 11.16% ± 14.98%

  ▁ █▆                                                    
  █▄▅▆▆███▅▃▃▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▃▂▃▂▃▃▃▄▅▄▃ ▃
  4.56 ms Histogram: frequency by time 8.34 ms <

 Memory estimate: 9.16 MiB, allocs estimate: 115818.

```

Is there anything I can improve in terms of speed for this kind of problem?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [March 19, 2023, 9:26am UTC](https://discourse.julialang.org/t/high-allocations-count-for-twopointbvproblem-in-differentialequations-jl/96296/2 "2023-03-19T09:26:33Z")

</div>

just out of curoisity, have you tried Shooting?

---

<div class="post-metadata">

**Author:** ![Hawkes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hawkes/32/22365_2.png) [@Hawkes](https://discourse.julialang.org/u/Hawkes)\
**Post date:** [March 19, 2023, 9:41am UTC](https://discourse.julialang.org/t/high-allocations-count-for-twopointbvproblem-in-differentialequations-jl/96296/3 "2023-03-19T09:41:18Z")

</div>

Not really, as it is not the ideal algorithm type for the real problem (aside from the MWE).

---

<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:** [March 19, 2023, 10:55am UTC](https://discourse.julialang.org/t/high-allocations-count-for-twopointbvproblem-in-differentialequations-jl/96296/4 "2023-03-19T10:55:08Z")

</div>

The BVP solvers will be going through a complete overhaul this spring and summer, so I’m just going to hold off here and say it’s going to completely change and be fixed by that.

---

<div class="post-metadata">

**Author:** ![Hawkes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hawkes/32/22365_2.png) [@Hawkes](https://discourse.julialang.org/u/Hawkes)\
**Post date:** [March 19, 2023, 11:22am UTC](https://discourse.julialang.org/t/high-allocations-count-for-twopointbvproblem-in-differentialequations-jl/96296/5 "2023-03-19T11:22:52Z")

</div>

Thanks @ChrisRackauckas ! I guess then I will use SciPy via Py(thon)Call for now and wait for the overhaul.
