# In-place operations on matrices

**URL:** https://discourse.julialang.org/t/in-place-operations-on-matrices/30908
**Category:** Performance
**Created:** [November 10, 2019, 2:55am UTC](https://discourse.julialang.org/t/in-place-operations-on-matrices/30908 "2019-11-10T02:55:09Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)
#### Post date: [November 10, 2019, 2:55am UTC](https://discourse.julialang.org/t/in-place-operations-on-matrices/30908/1 "2019-11-10T02:55:09Z")

</div>

When working with huge matrices, the limitations of the RAM is a real issue, and one would like to perform matrix operations in place if possible. Given a matrix `A` which occupies a sizable chunk of RAM, what is the best way to perform the following operations

```julia
A.*=2;
A.+= A';

```

so that it is done in-place. I noticed that in spite of using the `.` notation, the RAM usage increases by at least 100%, indicating that Julia is creating a copy of the matrix.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [November 10, 2019, 3:33am UTC](https://discourse.julialang.org/t/in-place-operations-on-matrices/30908/2 "2019-11-10T03:33:02Z")

</div>

It’s not a good idea to benchmark in global scope

```julia

julia> f(x) = x.*=2
f (generic function with 1 method)

julia> a = randn(1000,1000);

julia> @time f(a);
  0.000885 seconds (4 allocations: 160 bytes)

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 10, 2019, 5:12am UTC](https://discourse.julialang.org/t/in-place-operations-on-matrices/30908/3 "2019-11-10T05:12:20Z")

</div>

The first operation already happens in place. As for

> [@iamsuddhasattwa](#):
>
> `A.+= A'`

it cannot be done in-place in a linear way, because you would be overwriting the other half of the transposed matrix you still need.

That said, you can just write a simple loop to implement it, and wrap the result in `Symmetric`, since you won’t need the other half.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [November 10, 2019, 11:06am UTC](https://discourse.julialang.org/t/in-place-operations-on-matrices/30908/4 "2019-11-10T11:06:09Z")

</div>

```julia
symmetrize!(a) = begin
    for c in 1:size(a, 2)
    	for r in c:size(a, 1)
    		a[r, c] += a[c, r]
    		a[c, r] = a[r, c]
    	end
    end
    a
end

using BenchmarkTools
a = rand(2500,2500) ;
@btime symmetrize!($a);
@btime $a .+ $a';

```

gives

```julia
julia> @btime symmetrize!($a);                                            
  11.826 ms (0 allocations: 0 bytes)                                      
                                                                          
julia> @btime $a .+ $a';                                                  
  50.207 ms (2 allocations: 47.68 MiB)                                    
                                                                          
julia> 

```

---

<div class="post-metadata">

### Author: ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)
#### Post date: [November 10, 2019, 3:07pm UTC](https://discourse.julialang.org/t/in-place-operations-on-matrices/30908/5 "2019-11-10T15:07:49Z")

</div>

Thanks. I was actually monitoring through my Systems Monitor. Since the matrix was about 5.8 GB, the duplication was clearly visible.

---

<div class="post-metadata">

### Author: ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)
#### Post date: [November 10, 2019, 3:15pm UTC](https://discourse.julialang.org/t/in-place-operations-on-matrices/30908/6 "2019-11-10T15:15:35Z")

</div>

I am wondering how you achieved such a high speed with `symmetrize!` without using Multithreading `Threads.@threads` .

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [November 10, 2019, 5:05pm UTC](https://discourse.julialang.org/t/in-place-operations-on-matrices/30908/7 "2019-11-10T17:05:11Z")

</div>

Since this is memory bound, you could probably go even faster at the expense of clarity by blocking it to be nicer to cache.
