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

Mutable addition and substraction of sparse arrays #281

Merged
merged 4 commits into from
Apr 24, 2024
Merged

Conversation

blegat
Copy link
Member

@blegat blegat commented Apr 23, 2024

Turns out broadcast! is well implemented for SparseArrays:

julia> v = sparsevec([1, 3], [π, 2π])
3-element SparseVector{Float64, Int64} with 2 stored entries:
  [1]  =  3.14159
  [3]  =  6.28319

julia> a = copy(v)
3-element SparseVector{Float64, Int64} with 2 stored entries:
  [1]  =  3.14159
  [3]  =  6.28319

julia> b = copy(v)
3-element SparseVector{Float64, Int64} with 2 stored entries:
  [1]  =  3.14159
  [3]  =  6.28319

julia> @btime Broadcast.broadcast!(+, $v, 3, $a, 2, $b)
  38.791 ns (0 allocations: 0 bytes)
3-element SparseVector{Float64, Int64} with 3 stored entries:
  [1]  =  11.2832
  [2]  =  5.0
  [3]  =  17.5664

julia> @btime Broadcast.broadcast!(-, $v, 3, $a)
  27.560 ns (0 allocations: 0 bytes)
3-element SparseVector{Float64, Int64} with 3 stored entries:
  [1]  =  -0.141593
  [2]  =  3.0
  [3]  =  -3.28319

julia> @btime Broadcast.broadcast!(-, $v, $a, $b)
  31.149 ns (0 allocations: 0 bytes)
3-element SparseVector{Float64, Int64} with 0 stored entries

Since what we need exactly matches the definition of broadcast!, we might as well call broadcast! by default for any AbstractArray which is what this PR does.

Copy link

codecov bot commented Apr 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.80%. Comparing base (e367b37) to head (0166303).
Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #281   +/-   ##
=======================================
  Coverage   91.80%   91.80%           
=======================================
  Files          23       23           
  Lines        2197     2197           
=======================================
  Hits         2017     2017           
  Misses        180      180           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kalmarek
Copy link

kalmarek commented Apr 23, 2024

@blegat for some reason the tests in StarAlgebras still allocate. Part of this is de-aliasing (I'm fine with this), but there is one additional allocation that happens only while testing, I cannot trace it in REPL:

julia> @test @allocations(MA.operate_to!(YY, +, Y, YY)) == 2
Test Passed

but

FixedBasis caching && allocations: Test Failed at /home/kalmar/.julia/dev/StarAlgebras/test/caching_allocations.jl:54
  Expression: #= /home/kalmar/.julia/dev/StarAlgebras/test/caching_allocations.jl:54 =# @allocations(MA.operate_to!(YY, +, Y, YY)) == 2
   Evaluated: 3 == 2

@blegat
Copy link
Member Author

blegat commented Apr 24, 2024

Testing allocations within the environment of a @testset is usually not easy, wrapping it in a function helps. To solve this issue in MA, we use

function alloc_test(f, n)
f() # compile
@test n == @allocated f()
end
function alloc_test_le(f, n)
f() # compile
@test n >= @allocated f()
end

This should be resolved by JuliaAlgebra/StarAlgebras.jl#29

@blegat blegat merged commit cbf8d63 into master Apr 24, 2024
11 checks passed
@odow odow deleted the bl/add_sub_sparse branch April 24, 2024 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants