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

Multiplication and Addition Broadcast into Dense Array #7

Open
Anemometer opened this issue Nov 9, 2020 · 2 comments
Open

Multiplication and Addition Broadcast into Dense Array #7

Anemometer opened this issue Nov 9, 2020 · 2 comments

Comments

@Anemometer
Copy link

Anemometer commented Nov 9, 2020

extendable.jl provides several mul! methods and a Base.:+ override returning a sparse result when adding e.g. a sparse matrix to an ExtendableSparseMatrix.
However, if an ExtendableSparseMatrix is multiplied by a scalar or added to another ExtendableSparseMatrix, i.e. running

using SparseArrays, LinearAlgebra, ExtendableSparse
A = ExtendableSparseMatrix(sprand(100,100,.5));
K = ExtendableSparseMatrix(sprand(100,100,.5));
A + 0.4 * K

the default Broadcasting machinery reverts to the Broadcast.DefaultArrayStyle type.
This means that a dense destination container of type Array{Float64,2} is allocated and the existing SparseArrays machinery for sparse array maths is skipped completely leading to a significant performance and memory hit.

If there are no plans to implement custom broadcasting behaviour for the ExtendableSparseMatrix type, couldn't we provide new methods such as

function Base.:*(s::Tv, ext::ExtendableSparseMatrix{Tv,Ti}) where {Tv<:Number,Ti<:Integer}
       @inbounds flush!(ext)
       return s * ext.cscmatrix
end

function Base.:+(ext1::ExtendableSparseMatrix{Tv,Ti}, ext2::ExtendableSparseMatrix{Tv,Ti}) where {Tv<:Number,Ti<:Integer}
       @inbounds flush!(ext1)
       @inbounds flush!(ext2)
       return ext1.cscmatrix + ext2.cscmatrix
end

to guard against such unpleasant surprises when handling ExtendableSparseMatrix?

@j-fu
Copy link
Owner

j-fu commented Dec 4, 2020

Thanks for figuring this out!
Best option is to make a pull request out of this.

@krishvishal
Copy link

Does this performance hit also effect assignment with broadcasting?

For example

A .= 1.0 # A is ExtendableSparseMatrix

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

3 participants