# Efficient memory allocation for large system of ODEs

**URL:** <https://discourse.julialang.org/t/efficient-memory-allocation-for-large-system-of-odes/11226>\
**Category:** Performance\
**Created:** [May 29, 2018, 10:36am UTC](https://discourse.julialang.org/t/efficient-memory-allocation-for-large-system-of-odes/11226 "2018-05-29T10:36:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![swainarpit](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@swainarpit](https://discourse.julialang.org/u/swainarpit)\
**Post date:** [May 29, 2018, 10:36am UTC](https://discourse.julialang.org/t/efficient-memory-allocation-for-large-system-of-odes/11226/1 "2018-05-29T10:36:28Z")

</div>

Hi,

I have a system of differential equations that mimic the clonal population in an immune system. For whoever is curious, the problem definition can be found in the post:

> [@PositiveDomain callback and isoutofdomain implementation](https://discourse.julialang.org/t/positivedomain-callback-and-isoutofdomain-implementation/11097):
>
> Hi, I have been trying to solve a system of differential equations in Julia. I have run into a few problems and am not clear on what I am doing wrong. Problem definition: The system I am working on is a model of the immune system. This is represented by 9 differential equations. One of the parameters is time-dependent and is a step function representing the lifetime of an infection in the immune system. This makes the system discontinuous at the time of entry and removal of the infection. To …

At present, I am simulating with a small number of clones and thus, am able to deal with the memory required. But, for higher number of clones, the performance of my present code is pretty detrimental. Say, for 2000 clones (variable n), the code runs for about 15 hours and stops prematurely because of the shortage of memory. As advised by @ChrisRackauckas in his tutorials, I tried saving only the last point of the simulation, but again faced the same situation.

The code is clearly memory inefficient. I tried using minimizing global variables by defining the function main. But, the memory and the time required increases for these cases. What else can be done for managing the memory?

Thanks

Code: [https://www.pastiebin.com/5b0d2ad13e206](https://www.pastiebin.com/5b0d2ad13e206)

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 29, 2018, 11:48am UTC](https://discourse.julialang.org/t/efficient-memory-allocation-for-large-system-of-odes/11226/2 "2018-05-29T11:48:28Z")

</div>

Have you read (and maybe re-read) the “Performance tips” section of the manual? Have you profiled your code: [https://docs.julialang.org/en/stable/manual/profile/#Profiling-1](https://docs.julialang.org/en/stable/manual/profile/#Profiling-1)?

---

<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:** [May 29, 2018, 12:54pm UTC](https://discourse.julialang.org/t/efficient-memory-allocation-for-large-system-of-odes/11226/3 "2018-05-29T12:54:16Z")

</div>

> [@swainarpit](#):
>
> But, the memory and the time required increases for these cases.

```julia
p = [n, g, p2, p3, p4, p5, p6, h1, h2, z];

```

`n` is an integer which makes this array not of the same types, which makes it `typeof(p) = Vector{Any}`, which makes every parameter in your inner loop not inferred and thus your whole code not type-stable. I would instead recommend using a tuple or something like Parameters.jl. Just pull that inner function aside to see:

```julia
n = 4; # number of clones
h1 = [841.0, 292.0, 860.0, 882.0];
h2 = h1 .+ [564.0, 374.0, 727.0, 813.0];
g = [0.791508, 0.116203, 0.702549, 0.832428];
p2 = [2.0, 1.0, 3.0, 7.0];
p3 = 0.01+(1-0.01)*[0.739588, 0.546912, 0.725891, 0.0332758];
p4 = 0.001+(1-0.001)*[0.040059, 0.804641, 0.639816, 0.513092];
p5 = 0.001+(1-0.001)*[0.9515, 0.2969, 0.624892, 0.34171];
p6 = 0.01+(1-0.01)*[0.455292, 0.339854, 0.0306784, 0.437118];
z = zeros(n);

y0 = [1.0; vec(hcat([65.0, 14.0, 56.0, 65.0],[0.492182, 0.431219, 0.538526, 0.517137])')];
f = similar(y0)
y = y0
t = 20.0

p = [n, g, p2, p3, p4, p5, p6, h1, h2, z]
@code_warntype Scaled_Null(f,y,p,t)

p = (n, g, p2, p3, p4, p5, p6, h1, h2, z)
@code_warntype Scaled_Null(f,y,p,t)

```

So if you make that a tuple it should be much better.
