# Allocation with in-place sum of a transposed matrix

**URL:** https://discourse.julialang.org/t/allocation-with-in-place-sum-of-a-transposed-matrix/44066
**Category:** General Usage
**Created:** [August 1, 2020, 12:21am UTC](https://discourse.julialang.org/t/allocation-with-in-place-sum-of-a-transposed-matrix/44066 "2020-08-01T00:21:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [August 1, 2020, 12:21am UTC](https://discourse.julialang.org/t/allocation-with-in-place-sum-of-a-transposed-matrix/44066/1 "2020-08-01T00:21:00Z")

</div>

I need to do in-place summations of a transposed matrix and I get memory allocation in the process. For example, running function `test` of the following code

```julia
function test_sum_transpose(b, a)
    sum!(b, a')
end

function test_sum(b, a)
    sum!(b, a)
end

function test()
    a = [1 2 3; 3 4 5]
    b = [0.0, 0.0, 0.0]; @time test_sum_transpose(b, a)
    b = [0.0, 0.0]; @time test_sum(b, a)
    return nothing
end

```

I get

```julia
  0.000001 seconds (1 allocation: 16 bytes)
  0.000001 seconds

```

Is there any way to achieve this summation (sum the columns of `a`) and assign the result to `b` without allocations?

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 1, 2020, 5:12am UTC](https://discourse.julialang.org/t/allocation-with-in-place-sum-of-a-transposed-matrix/44066/2 "2020-08-01T05:12:14Z")

</div>

The `@time` macro isn’t suitable for microbenchmarks. The allocation numbers are not reliable. Use the BenchmarkTools package instead.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [August 1, 2020, 7:51am UTC](https://discourse.julialang.org/t/allocation-with-in-place-sum-of-a-transposed-matrix/44066/3 "2020-08-01T07:51:12Z")

</div>

Also, you don’t have to create all these functions to test simple things like this. In this case it seems that there is an allocation from forming the adjoint:

```julia
julia> using BenchmarkTools

julia> a = [1 2 3; 3 4 5]; b = [0.0, 0.0];

julia> @btime sum!($b, $a);
  24.024 ns (0 allocations: 0 bytes)

julia> b = [0.0, 0.0, 0.0];

julia> @btime sum!($b, $a');
  31.165 ns (1 allocation: 16 bytes)

julia> @btime $a';
  6.489 ns (1 allocation: 16 bytes)

```

---

<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: [August 1, 2020, 8:09am UTC](https://discourse.julialang.org/t/allocation-with-in-place-sum-of-a-transposed-matrix/44066/4 "2020-08-01T08:09:51Z")

</div>

And, like the allocations for views, this goes away in Julia 1.5:

```julia
julia> @btime sum!($b, $a);
  19.287 ns (0 allocations: 0 bytes)

julia> @btime sum!($b, $a');
  22.419 ns (0 allocations: 0 bytes)

julia> VERSION
v"1.5.0-rc1.0"

```

---

<div class="post-metadata">

### Author: ![aaraujo71](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaraujo71/32/10635_2.png) [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)
#### Post date: [August 1, 2020, 10:05am UTC](https://discourse.julialang.org/t/allocation-with-in-place-sum-of-a-transposed-matrix/44066/5 "2020-08-01T10:05:08Z")

</div>

“And, like the allocations for views, this goes away in Julia 1.5”

After watching a JuliaCon20 video on the new v1.5 features, It came to my mind that this problem could probably go away with the new version.

Thank you all for the replies.

Another thing I’ve noticed is that `@. c = (b - a)'` also allocates memory. Is this going to change in v1.5 as well?

```julia
julia> @btime @. $c = ($b - $a)'
  69.017 ns (2 allocations: 144 bytes)
2×3 Array{Int64,2}:
 9 9 11
 9 9 11

```
