SparseArrays.jl icon indicating copy to clipboard operation
SparseArrays.jl copied to clipboard

Bug in map! for sparse matrices with aliasing

Open pablosanjose opened this issue 5 years ago • 0 comments

We've had aliasing safeguards for broadcast and generic map! for a while (see JuliaLang/julia#21693, JuliaLang/julia#25890 in particular). However, I believe this machinery was never ported to SparseArrays's map!:

julia> using SparseArrays

julia> s0 = sprand(10, 10, 0.1); s1 = sprand(10, 10, 0.1); sc = copy(s0);

julia> map!(+, s0, s0, s1);

julia> s0 == sc + s1
false

The last should be true, as in the dense case:

julia> s0 = Matrix(sprand(10, 10, 0.1)); s1 = Matrix(sprand(10, 10, 0.1)); sc = copy(s0);

julia> map!(+, s0, s0, s1);

julia> s0 == sc + s1
true

This behaviour is in 1.5.1 and master

Broadcast seems to work ok, although I think it doesn't use sparse-specific codepaths, so it is much slower than map!

julia> s0 = sprand(10,10,0.1); s1 = sprand(10,10,0.1); sc = copy(s0);

julia> s0 .= s0 .+ s1;

julia> s0 == sc + s1
true

pablosanjose avatar Sep 03 '20 13:09 pablosanjose