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

fix fastpaths for materializing transposes / adjoints of sparse matrices #28049

Merged
merged 3 commits into from
Jul 12, 2018

Conversation

KristofferC
Copy link
Member

Setup:

using LinearAlgebra
using SparseArrays
using BenchmarkTools
n = 1000
A = sparse(1.0I,n,n);
B = sparse(1.0I,n,n);

Before PR:

julia> @btime $A + $B';
  5.307 ms (28 allocations: 112.11 KiB)

julia> @btime $A + $(B');
  5.305 ms (27 allocations: 112.09 KiB)

After PR:

julia> @btime $A + $B';
  13.897 μs (10 allocations: 63.52 KiB)

julia> @btime $A + $(B');
  13.852 μs (9 allocations: 63.50 KiB)

Fixes #28012

@KristofferC KristofferC added performance Must go faster sparse Sparse arrays labels Jul 11, 2018
@KristofferC KristofferC added the potential benchmark Could make a good benchmark in BaseBenchmarks label Jul 11, 2018
@@ -397,6 +397,8 @@ function SparseMatrixCSC{Tv,Ti}(M::StridedMatrix) where {Tv,Ti}
end
return SparseMatrixCSC(size(M, 1), size(M, 2), colptr, rowval, nzval)
end
SparseMatrixCSC(M::LinearAlgebra.Adjoint{Tv,SparseMatrixCSC{Tv,Ti}}) where {Tv,Ti} = copy(M)
Copy link
Member

@martinholters martinholters Jul 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably relax the signature to Adjoint{<:Any,<:SparseMatrixCSC} (which is also the one of the copy method this should dispatch to); likewise below.

Copy link
Member Author

@KristofferC KristofferC Jul 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm

julia> B = sprand(1,1,1.0);

julia> SparseMatrixCSC(M::Adjoint{<:Any,SparseMatrixCSC}) = copy(M)
SparseMatrixCSC

julia> @which SparseMatrixCSC(B')
(::Type{SparseMatrixCSC})(M::AbstractArray{Tv,2}) where Tv in SparseArrays at /Users/kristoffer/julia/usr/share/julia/stdlib/v0.7/SparseArrays/src/sparsematrix.jl:371

julia> SparseMatrixCSC(M::LinearAlgebra.Adjoint{Tv,SparseMatrixCSC{Tv,Ti}}) where {Tv,Ti} = copy(M)
SparseMatrixCSC

julia> @which SparseMatrixCSC(B')
(::Type{SparseMatrixCSC})(M::Adjoint{Tv,SparseMatrixCSC{Tv,Ti}}) where {Tv, Ti} in Main at REPL[7]:1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjoint{<:Any,<:SparseMatrixCSC}, not Adjoint{<:Any,SparseMatrixCSC}:

julia> B = sprand(1,1,1.0);

julia> SparseMatrixCSC(M::Adjoint{<:Any,<:SparseMatrixCSC}) = copy(M)
SparseMatrixCSC

julia> @which SparseMatrixCSC(B')
(::Type{SparseMatrixCSC})(M::Adjoint{#s2,#s1} where #s1<:SparseMatrixCSC where #s2) in Main at REPL[19]:1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -2242,4 +2242,10 @@ _length_or_count_or_five(x) = length(x)
end
end

@testset "sparse transpose adjoint" begin
A = sprand(10, 10, 0.75)
@test A' == SparseMatrixCSC(A')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to verify the return type, too, because the new definitions do not obviously return a SparseMatrixCSC.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@test A' == SparseMatrixCSC(A')
@test SparseMatrixCSC(A') isa SparseMatrixCSC
@test transpose(A) == SparseMatrixCSC(transpose(A))
@test SparseMatrixCSC(transpose(A)) isa SparseMatrixCSC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Alternatively @test A' == SparseMatrix(A')::SparseMatrixCSC.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster potential benchmark Could make a good benchmark in BaseBenchmarks sparse Sparse arrays
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants