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

fixed regression in sparse of adjoint of sparse (#28948) #28954

Merged
merged 4 commits into from
Sep 4, 2018

Conversation

ExpandingMan
Copy link
Contributor

@ExpandingMan ExpandingMan commented Aug 29, 2018

This is a fix to #28948. What was happening is that sparse was causing a call to be made to SparseMatrixCSC with explicit type parameters which was a fallback to an inefficient method, as without the methods I added only SparseMatrixCSC without explicit type parameters knew how to do this operation efficiently.

Note that there is probably room for improvement here: performance of SparseMatrixCSC{T,Ti}(M::Adjoint{U,SparseMatrixCSC{U,Ti}}) where T \ne U will still be terrible, it's just not immediately obvious to me what the best solution to that is.

@ExpandingMan ExpandingMan changed the title fixed regression in sparse of adjoint (#28948) fixed regression in sparse of adjoint of sparse (#28948) Aug 29, 2018
@ExpandingMan
Copy link
Contributor Author

ExpandingMan commented Aug 30, 2018

Ok, what's the deal with these "xcode8" tests, do we expect these to pass? Looks like there are lots of failures that seem to have absolutely nothing to do with this PR, though looking around I see other PR's passing it.

@vchuravy
Copy link
Member

vchuravy commented Aug 30, 2018

Ok, what's the deal with these "xcode8" tests,

You mean the Mac OSX tests? Yes we expect those to pass. There was one network failure so I restarted CI.

@ExpandingMan
Copy link
Contributor Author

ExpandingMan commented Aug 30, 2018

For give my ignorance as to how the process works, but as this is essentially just a fix of a performance regression, I'm wondering if this can get a backport tag?

On second thought I just realized that the tag would probably be added after approval 🤦‍♂️ . Nvm, I'm sure you guys will give the approprate tag.

end
function SparseMatrixCSC{Tv,Ti}(M::Transpose{Tv,SparseMatrixCSC{Tv,Ti}}) where {Tv,Ti}
copy(M)
end
SparseMatrixCSC(M::Adjoint{<:Any,<:SparseMatrixCSC}) = copy(M)
SparseMatrixCSC(M::Transpose{<:Any,<:SparseMatrixCSC}) = copy(M)
Copy link
Member

Choose a reason for hiding this comment

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

Thanks @ExpandingMan! :)

Perhaps completing the method set would be worthwhile, e.g. the sketch

SparseMatrixCSC(M::Adjoint{<:Any,<:SparseMatrixCSC}) = copy(M)
SparseMatrixCSC(M::Transpose{<:Any,<:SparseMatrixCSC}) = copy(M)
SparseMatrixCSC{Tv}(M::Adjoint{Tv,<:SparseMatrixCSC{Tv}}) where {Tv} = copy(M)
SparseMatrixCSC{Tv}(M::Transpose{Tv,<:SparseMatrixCSC{Tv}}) where {Tv} = copy(M)
SparseMatrixCSC{Tv,Ti}(M::Adjoint{Tv,SparseMatrixCSC{Tv,Ti}}) where {Tv,Ti} = copy(M)
SparseMatrixCSC{Tv,Ti}(M::Transpose{Tv,SparseMatrixCSC{Tv,Ti}}) where {Tv,Ti} = copy(M)

Regarding

Note that there is probably room for improvement here: performance of SparseMatrixCSC{T,Ti}(M::Adjoint{U,SparseMatrixCSC{U,Ti}}) where T \ne U will still be terrible, it's just not immediately obvious to me what the best solution to that is.

in combination with the preceding, the following sketch provides simple if suboptimal, but decent, implementations

SparseMatrixCSC{Tv}(M::Adjoint{<:Any,<:SparseMatrixCSC}) where {Tv} = SparseMatrixCSC{Tv}(copy(M))
SparseMatrixCSC{Tv}(M::Transpose{<:Any,<:SparseMatrixCSC}) where {Tv} = SparseMatrixCSC{Tv}(copy(M))
SparseMatrixCSC{Tv,Ti}(M::Adjoint{<:Any,<:SparseMatrixCSC}) where {Tv,Ti} = SparseMatrixCSC{Tv,Ti}(copy(M))
SparseMatrixCSC{Tv,Ti}(M::Transpose{<:Any,<:SparseMatrixCSC}) where {Tv,Ti} = SparseMatrixCSC{Tv,Ti}(copy(M))

The net number of method definitions could probably be reduced via LinearAlgebra.AdjOrTrans, e.g.

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

collapses to

SparseMatrixCSC(M::AdjOrTrans{<:Any,<:SparseMatrixCSC}) = copy(M)

Best!

@ExpandingMan
Copy link
Contributor Author

Ok, added the recommended methods. I did not use LinearAlgebra.AdjOrTrans since nothing else in SparseArrays seems to use that and I thought it would be confusing unless I changed everything.

end
function SparseMatrixCSC{Tv,Ti}(M::Transpose{Tv,SparseMatrixCSC{Tv,Ti}}) where {Tv,Ti}
copy(M)
end
Copy link
Member

Choose a reason for hiding this comment

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

Why use long-form definitions for these methods?

end
function SparseMatrixCSC{Tv,Ti}(M::Transpose{<:Any,SparseMatrixCSC}) where {Tv,Ti}
SparseMatrixCSC{Tv,Ti}(copy(M))
end
Copy link
Member

Choose a reason for hiding this comment

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

Likewise these long-form definitions? :)

@ExpandingMan
Copy link
Contributor Author

I had left the other methods as blocks because they definitely go past my screen-split in vim 😉 . I checked the file though, and there are already other lines which are longer, so I collapsed them as well.

Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks @ExpandingMan! :)

Are these constructors already tested, or might they benefit from a little exercise?

@ExpandingMan
Copy link
Contributor Author

I have tested these, but not at all thoroughly. If you want to do a quick sanity check it may be worthwhile. The "really scary" regression definitely seems to be fixed.

(I think freebsd might need to be re-triggered.)

@KristofferC KristofferC mentioned this pull request Sep 4, 2018
@KristofferC KristofferC added the potential benchmark Could make a good benchmark in BaseBenchmarks label Sep 4, 2018
@KristofferC
Copy link
Member

It feels a bit "silly" that we need all these definitions but if that is what it takes let's just do it. Will merge when FreeBSD finishes CI. Butting a benchmark label so if someone feels inclined can put this case into BaseBenchmarks.jl

@ExpandingMan
Copy link
Contributor Author

One could probably do a thorough pass through SparseArrays and consolidate many things, but this was a very severe regression that needed to be fixed quickly and as I'm only somewhat familiar with the SparseArrays code I didn't want to be the one to start refactoring things. Besides, this may be the sort of situation in which it pays to be explicit to the point of pedantry just to make sure issues like the one that prompted this PR don't crop up.

Is there some action required to re-trigger freebsd?

@KristofferC
Copy link
Member

Hm, I tried to do it manually but it didn't seem to take. Anyway, sparse tests succeeded on FreeBSD anyway so merging.

@KristofferC KristofferC merged commit c43793c into JuliaLang:master Sep 4, 2018
KristofferC pushed a commit that referenced this pull request Sep 4, 2018
* fixed regression in sparse of adjoint

(cherry picked from commit c43793c)
KristofferC pushed a commit that referenced this pull request Sep 8, 2018
* fixed regression in sparse of adjoint

(cherry picked from commit c43793c)
KristofferC pushed a commit that referenced this pull request Sep 8, 2018
* fixed regression in sparse of adjoint

(cherry picked from commit c43793c)
KristofferC pushed a commit that referenced this pull request Feb 11, 2019
* fixed regression in sparse of adjoint

(cherry picked from commit c43793c)
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants