# Creating a matrix as Symmetric causes excessive allocations in multithreaded loop

**URL:** <https://discourse.julialang.org/t/creating-a-matrix-as-symmetric-causes-excessive-allocations-in-multithreaded-loop/54059>\
**Category:** Performance\
**Created:** [January 27, 2021, 2:51pm UTC](https://discourse.julialang.org/t/creating-a-matrix-as-symmetric-causes-excessive-allocations-in-multithreaded-loop/54059 "2021-01-27T14:51:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedrohnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pedrohnv/32/212503_2.png) [@pedrohnv](https://discourse.julialang.org/u/pedrohnv)\
**Post date:** [January 27, 2021, 2:51pm UTC](https://discourse.julialang.org/t/creating-a-matrix-as-symmetric-causes-excessive-allocations-in-multithreaded-loop/54059/1 "2021-01-27T14:51:25Z")

</div>

In the following code snippet, I use a symmetric matrix to calculate another. If I assign the matrix as Symmetric, it leads to excessive allocations if the loop is multi-threaded.

```julia
using BenchmarkTools
function foo()
    ns = 1500
    ne = 200
    rbar = rand(Float64, (ne,ne)) * 30
    #rbar = Symmetric(rbar, :L) # comment this line to see the difference
    potzl = rand(ComplexF64, (ne,ne))
    zls = [Array{ComplexF64}(undef, (ne,ne)) for t = 1:Threads.nthreads()]
    Threads.@threads for f = 1:ns
        t = Threads.threadid()
        zl = zls[t]
        g = rand(ComplexF64)
        for k = 1:ne
            for i = k:ne
                exp_gr = exp(-g * rbar[i,k]) # FIXME exp very slow
                zl[i,k] = exp_gr * potzl[i,k]
            end
        end
    end
end
precompile(foo, ())
@btime foo()

```

The output of `@btime` without commenting `rbar = Symmetric(rbar, :L)` is `3.168 s (180900039 allocations: 4.95 GiB`. If I comment that line, the output of `@btime` is `287.963 ms (36 allocations: 3.67 MiB)`. Why does that happen?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [January 27, 2021, 2:58pm UTC](https://discourse.julialang.org/t/creating-a-matrix-as-symmetric-causes-excessive-allocations-in-multithreaded-loop/54059/2 "2021-01-27T14:58:20Z")

</div>

> [@pedrohnv](#):
>
> ```julia
> rbar = rand(Float64, (ne,ne)) * 30
> rbar = Symmetric(rbar, :L) # comment this line to see the difference
> 
> ```

Here `@code_warntype foo()` shows a problem. The issue is that you’re using the same name for two objects of differing type, which sometimes the compiler is smart enough to understand, but not always. If you give the first one a different name, the problem goes away.

---

<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:** [January 27, 2021, 3:36pm UTC](https://discourse.julialang.org/t/creating-a-matrix-as-symmetric-causes-excessive-allocations-in-multithreaded-loop/54059/3 "2021-01-27T15:36:37Z")

</div>

See also the [performance tips: avoid changing the type of a variable](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-changing-the-type-of-a-variable).
