# Types and Memory allocation in the operation of Vector{Matrix{ComplexF64}}

**URL:** <https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672>\
**Category:** New to Julia\
**Tags:** question, type-stability\
**Created:** [February 15, 2023, 8:52am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672 "2023-02-15T08:52:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_i/32/46880_2.png) [@J\_I](https://discourse.julialang.org/u/J_I)\
**Post date:** [February 15, 2023, 8:52am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672/1 "2023-02-15T08:52:59Z")

</div>

Hi, now, I’m calculating the time evolution of Vector{Matrix{ComplexF64}} by Runge-Kutta method, and I have problems regarding the calculation below::

```julia
ρ += (h/6)*(k1 + 2*k2 + 2*k3 + k4)

```

here, `k1~k4`, are `Vector{Matrix{ComplexF64}}`, and `h` is `Float64.`  
I thought it would be natural to think that `ρ` will be `Vector{Matrix{ComplexF64}}`, but the `code_warntype` teaches me it is `Any`.

How can I understand this behaviour?

Also, there is memory allocation in this calculation, even though there is no creation of new arrays. How can I get rid of them?

---

<div class="post-metadata">

**Author:** ![J\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_i/32/46880_2.png) [@J\_I](https://discourse.julialang.org/u/J_I)\
**Post date:** [February 15, 2023, 9:10am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672/2 "2023-02-15T09:10:42Z")

</div>

I should have given you more information.  
I tried

```julia
@. ρ += (T_param.h/6)*(k1 + 2*k2 + 2*k3 + k4)

```

And it indeed fixed the type instability of `ρ`, but it increased the memory allocation, investigated by `Profile`.  
How can I solve the type instability problem and the memory allocation problem at the same time?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [February 15, 2023, 9:22am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672/3 "2023-02-15T09:22:27Z")

</div>

MWE

```julia
julia> k1 = [rand(2,2)];

julia> k2 = copy(k1);

julia> using Test

julia> @inferred k1 + k2
ERROR: return type Vector{Matrix{Float64}} does not match inferred return type Any
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] top-level scope
   @ REPL[17]:1

```

I wonder why this isn’t being inferred (it seems the `eltype` is not being inferred). The following seems to be inferred, though:

```julia
julia> bc = Broadcast.instantiate(Broadcast.broadcasted(+, k1, k2));

julia> @inferred copy(bc)
1-element Vector{Matrix{Float64}}:
 [0.8596129565473165 1.8419784632408078; 0.4550608871647064 0.4035134314769242]

```

Wonder if @N5N3 might have some ideas

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [February 15, 2023, 9:22am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672/4 "2023-02-15T09:22:55Z")

</div>

Hi @J_I, could you maybe post a Minimum Working Example, so that we can experiment with it and show you what’s wrong? I have a few ideas but it’s hard to tell without being able to run the code

---

<div class="post-metadata">

**Author:** ![N5N3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/n5n3/32/17663_2.png) [@N5N3](https://discourse.julialang.org/u/N5N3)\
**Post date:** [February 15, 2023, 9:33am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672/5 "2023-02-15T09:33:33Z")

</div>

Looks like a nested `broadcast` which our compiler refuse to infer now.

---

<div class="post-metadata">

**Author:** ![J\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_i/32/46880_2.png) [@J\_I](https://discourse.julialang.org/u/J_I)\
**Post date:** [February 15, 2023, 9:40am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672/6 "2023-02-15T09:40:40Z")

</div>

Here is MWE.

```julia
function MWE_1()
    h = 0.01
    k1 = [rand(ComplexF64,4,4) for _ in 1:500]
    k2 = [rand(ComplexF64,4,4) for _ in 1:500]
    k3 = [rand(ComplexF64,4,4) for _ in 1:500]
    k4 = [rand(ComplexF64,4,4) for _ in 1:500]
    ρ = [rand(ComplexF64,4,4) for _ in 1:500]
    ρ = h/6 * (k1 + 2*k2 + 2*k3 + k4)
end

@code_warntype MWE_1()

```

the result is

```julia
Arguments
  #self#::Core.Const(MWE_1)
Locals
  #9::var"#9#14"
  #8::var"#8#13"
  #7::var"#7#12"
  #6::var"#6#11"
  #5::var"#5#10"
  ρ::Any
  k4::Vector{Matrix{ComplexF64}}
  k3::Vector{Matrix{ComplexF64}}
  k2::Vector{Matrix{ComplexF64}}
  k1::Vector{Matrix{ComplexF64}}
  h::Float64
Body::Any

```

this type instability problem is solved by adding @.

```julia
function MWE_2()
    h = 0.01
    k1 = [rand(ComplexF64,4,4) for _ in 1:500]
    k2 = [rand(ComplexF64,4,4) for _ in 1:500]
    k3 = [rand(ComplexF64,4,4) for _ in 1:500]
    k4 = [rand(ComplexF64,4,4) for _ in 1:500]
    ρ = [rand(ComplexF64,4,4) for _ in 1:500]
    @. ρ = h/6 * (k1 + 2*k2 + 2*k3 + k4)
end

```

like below

```julia
Arguments
  #self#::Core.Const(MWE_2)
Locals
  #19::var"#19#24"
  #18::var"#18#23"
  #17::var"#17#22"
  #16::var"#16#21"
  #15::var"#15#20"
  ρ::Vector{Matrix{ComplexF64}}
  k4::Vector{Matrix{ComplexF64}}
  k3::Vector{Matrix{ComplexF64}}
  k2::Vector{Matrix{ComplexF64}}
  k1::Vector{Matrix{ComplexF64}}
  h::Float64
Body::Vector{Matrix{ComplexF64}}

```

but this makes another problem of unknown memory allocation.

```julia
        - function MWE_2()
        - h = 0.01
        0 k1 = [rand(ComplexF64,4,4) for _ in 1:500]
        0 k2 = [rand(ComplexF64,4,4) for _ in 1:500]
        0 k3 = [rand(ComplexF64,4,4) for _ in 1:500]
        0 k4 = [rand(ComplexF64,4,4) for _ in 1:500]
        0 ρ = [rand(ComplexF64,4,4) for _ in 1:500]
   576000 @. ρ = h/6 * (k1 + 2*k2 + 2*k3 + k4)
        - end
        - 
        - # @code_warntype MWE_1()
        - # @code_warntype MWE_2()
        - 
        - using Profile 
        - Profile.clear_malloc_data()
        - MWE_2()

```

---

<div class="post-metadata">

**Author:** ![N5N3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/n5n3/32/17663_2.png) [@N5N3](https://discourse.julialang.org/u/N5N3)\
**Post date:** [February 15, 2023, 9:50am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672/7 "2023-02-15T09:50:35Z")

</div>

If your real usage is still limited to 4x4.  
The simplest fix here is use `Vector` of `SMatrix` instead of `Vector` of `Matrix`.

`StaticArrays.jl` has its own broadcast (+, /, …) implementation thus hopefully the rescusion detection would ignore it.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [February 15, 2023, 10:08am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672/8 "2023-02-15T10:08:36Z")

</div>

> [@J\_I](#):
>
> How can I get rid of them?

Probably simplest to loop over the arrays instead of using broadcast. This may be faster as well.

---

<div class="post-metadata">

**Author:** ![J\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_i/32/46880_2.png) [@J\_I](https://discourse.julialang.org/u/J_I)\
**Post date:** [February 16, 2023, 12:13am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672/9 "2023-02-16T00:13:55Z")

</div>

I confirmed your suggestion, and it completely solved the problem. Thank you very much!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 16, 2023, 12:34am UTC](https://discourse.julialang.org/t/types-and-memory-allocation-in-the-operation-of-vector-matrix-complexf64/94672/10 "2023-02-16T00:34:52Z")

</div>

PS. Please do _not_ post screenshots. Post `quoted code`. See [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)
