Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allocation with v.="x"on SparseArrays #404

Open
dpinol opened this issue Jul 7, 2023 · 4 comments
Open

allocation with v.="x"on SparseArrays #404

dpinol opened this issue Jul 7, 2023 · 4 comments

Comments

@dpinol
Copy link
Contributor

dpinol commented Jul 7, 2023

The following code allocates 128 bytes in julia 1.9 & 1.10 (also in previous julia versions). However, when using dense vectors, it doesn't allocate.

using SparseArrays

const v=spzeros(10)
v[1]=4
f(v) = v.*=4
@allocated f(v)

Currently the workaround is using map!(f, v,v)

@SobhanMP
Copy link
Member

SobhanMP commented Jul 18, 2023

Do note that another solution is nonzeros(v) .*= 4.

The culprit is that broadcast is only for unaliased vector (unalised refers to the left and right-hand side sharing memory, here both matrices are the same, so they naturally share vector) because it may or may not change the structure.
map! on the other hand only works on the nonzero elements so it is safe to apply to aliased vector

using SparseArrays
function f(x, y)
    for i in 1:10000
        y .= x .* 1.1
        x .= y .* (1 / 1.1)
    end
    x
end
x = spzeros(10)
x[1] = 3
y = copy(x)
@time f(x, y)
@time f(x, y)

gives

  0.094615 seconds (271.80 k allocations: 18.823 MiB, 99.75% compilation time)
  0.000205 seconds

@ViralBShah we could optimize this for fixed sparse matrices, what do you think?

@ViralBShah
Copy link
Member

I suppose that makes sense. @Wimmerer ?

@rayegun
Copy link
Member

rayegun commented Jul 19, 2023

I'm not convinced we can't optimize this within the semantics of sparse broadcast.

@SobhanMP
Copy link
Member

So we only trigger a copy if the structure changes or just do it inplace?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants