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

Simplify nz2nz_z2z-class sparse unary broadcast (and fix #18974) #19065

Merged
merged 1 commit into from
Nov 2, 2016

Conversation

Sacha0
Copy link
Member

@Sacha0 Sacha0 commented Oct 22, 2016

Following @stevengj's suggestion in #18975, this pull request makes nz2nz_z2z-class sparse unary broadcast leverage existing broadcast machinery rather than reimplement it. Also fixes #18974.

This implementation is slightly slower than the existing implementation due to the view in Bnzval = broadcast(f, view(A.nzval, 1:nnz(A))); removing the view (Bnzval = broadcast(f, A.nzval)) eliminates the performance gap. Bnzval = broadcast(f, A.nzval); resize!(Bnzval, nnz(A)) might be a reasonable alternative. For discussion of performance (and particularly the apparent need for forcing function-type specialization), see #18975. Best!

@Sacha0 Sacha0 changed the title Simplify nz2nz_z2z-class sparse unary broadcast Simplify nz2nz_z2z-class sparse unary broadcast (and fix #18974) Oct 22, 2016
@kshyatt kshyatt added the sparse Sparse arrays label Oct 22, 2016
@tkelman
Copy link
Contributor

tkelman commented Oct 23, 2016

is the view necessary? nzval is only ever not that length if the input data is invalid, right?

@Sacha0
Copy link
Member Author

Sacha0 commented Oct 23, 2016

is the view necessary?

Not strictly, no. The view preserves the method's existing behavior, but so would (Bnzval = broadcast(f, A.nzval); resize!(Bnzval, nnz(A))). Preserving the method's existing behavior also doesn't seem strictly necessary?

nzval is only ever not that length if the input data is invalid, right?

length(A.nzval) > nnz(A) is valid I believe?

Thanks for reviewing! Best!

@tkelman
Copy link
Contributor

tkelman commented Oct 24, 2016

You can have extra space in the nzval array, but there may be places that use nnz and length(nzval) interchangeably without checking whether they're the same.

@stevengj
Copy link
Member

The resize! approach seems fine to me; in common cases it will be a no-op.

…st machinery rather than reimplement it poorly.
@Sacha0
Copy link
Member Author

Sacha0 commented Oct 26, 2016

Switched to the resize! approach. Thanks!

broadcast{TTv}(::typeof(abs2), A::SparseMatrixCSC{Complex{TTv}}) = _broadcast_unary_nz2nz_z2z_T(abs2, A, TTv)
broadcast{TTv}(::typeof(abs), A::SparseMatrixCSC{Complex{TTv}}) = _broadcast_unary_nz2nz_z2z_T(abs, A, TTv)
broadcast{TTv<:Integer}(::typeof(abs), A::SparseMatrixCSC{Complex{TTv}}) = _broadcast_unary_nz2nz_z2z_T(abs, A, Float64)
broadcast{TTv<:BigInt}(::typeof(abs), A::SparseMatrixCSC{Complex{TTv}}) = _broadcast_unary_nz2nz_z2z_T(abs, A, BigFloat)
Copy link
Member

Choose a reason for hiding this comment

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

Did you mean to delete the specialized broadcast methods for abs and abs2 on sparse matrices?

(Eventually I think these should go away in favor of a more general mechanism, but that seems like a matter for another PR?)

Copy link
Member Author

Choose a reason for hiding this comment

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

The deletion was intentional: The (revised) unspecialized broadcast method appears to provide the behavior of the specializations for abs and abs2 (per a little testing at the REPL). More than happy to restore those specializations and nix them elsewhere though if you prefer? Thanks for reviewing!

Copy link
Member

Choose a reason for hiding this comment

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

I'm confused, was there already a PR that updated the unspecialized broadcast for unary operations on sparse arrays?

Copy link
Member Author

Choose a reason for hiding this comment

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

Apologies for the confusion. Clarification: The macro call

 @_enumerate_childmethods(_broadcast_unary_nz2nz_z2z,
     log1p, expm1, abs, abs2, conj)

defines methods

broadcast(::typeof(abs), A::SparseMatrixCSC) = _broadcast_unary_nz2nz_z2z(abs, A)
broadcast(::typeof(abs2), A::SparseMatrixCSC) = _broadcast_unary_nz2nz_z2z(abs2, A)

which, with _broadcast_unary_nz2nz_z2z using Bnzval = broadcast(f, A.nzval) to determine output type on this branch, seem to provide the behavior of the removed methods,

broadcast{TTv}(::typeof(abs2), A::SparseMatrixCSC{Complex{TTv}}) = _broadcast_unary_nz2nz_z2z_T(abs2, A, TTv)
broadcast{TTv}(::typeof(abs), A::SparseMatrixCSC{Complex{TTv}}) = _broadcast_unary_nz2nz_z2z_T(abs, A, TTv)
broadcast{TTv<:Integer}(::typeof(abs), A::SparseMatrixCSC{Complex{TTv}}) = broadcast_unary_nz2nz_z2z_T(abs, A, Float64)
broadcast{TTv<:BigInt}(::typeof(abs), A::SparseMatrixCSC{Complex{TTv}}) = broadcast_unary_nz2nz_z2z_T(abs, A, BigFloat)

Does that clarify? Best!

Copy link
Member

Choose a reason for hiding this comment

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

Ah good.

@stevengj
Copy link
Member

PS. Can you file a performance issue with a test case illustrating the slowdown if you omit the <:Function signature? That really needs to be fixed...

@Sacha0
Copy link
Member Author

Sacha0 commented Oct 27, 2016

Can you file a performance issue with a test case illustrating the slowdown if you omit the <:Function signature? That really needs to be fixed...

Shall do, likely tomorrow morning Done. Thanks!

@tkelman
Copy link
Contributor

tkelman commented Oct 27, 2016

dunno if we have benchmarks for this, but may as well @nanosoldier runbenchmarks(ALL, vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @jrevels

@Sacha0
Copy link
Member Author

Sacha0 commented Nov 2, 2016

The possible performance regressions look like noise.

@stevengj
Copy link
Member

stevengj commented Nov 2, 2016

Yup, they are not things that could have been affected by this.

@stevengj stevengj merged commit 421f079 into JuliaLang:master Nov 2, 2016
@Sacha0
Copy link
Member Author

Sacha0 commented Nov 2, 2016

Thanks for reviewing / merging!

@Sacha0 Sacha0 deleted the simplify_nz2nz_z2z branch November 2, 2016 20:02
Sacha0 added a commit to Sacha0/julia that referenced this pull request Nov 26, 2016
…ver a sparse matrix) from closed PR JuliaLang#18975 that was missed in PR JuliaLang#19065.
Sacha0 added a commit to Sacha0/julia that referenced this pull request Nov 27, 2016
…ver a sparse matrix) from closed PR JuliaLang#18975 that was missed in PR JuliaLang#19065.
Sacha0 added a commit to Sacha0/julia that referenced this pull request Dec 1, 2016
…ver a sparse matrix) from closed PR JuliaLang#18975 that was missed in PR JuliaLang#19065.
fcard pushed a commit to fcard/julia that referenced this pull request Feb 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sparse Sparse arrays
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect element type from specialized sparse unary broadcast methods
5 participants