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

Avoid aliasing in in UniformScaling*AbstractMatrix #18286

Merged
merged 3 commits into from
Sep 1, 2016
Merged

Conversation

andreasnoack
Copy link
Member

...and remove unnecessary UniformScaling*SparseMatrixCSC methods

@cstjean
Copy link
Contributor

cstjean commented Aug 30, 2016

There was a long discussion in #15546 (comment) about whether return values should be dealiased or not. It seems important to settle that issue sooner than later, especially if the conclusion is in favor of aliasing.

@andreasnoack
Copy link
Member Author

Thanks for the reference. I'd forgotten that discussion. However, as I read the discussion it covers several cases and the value dependent aliasing behavior was pretty close to be considered undesirable. It's also how power_by_squaring works right now so for the time being that might be the default (though it is probably not consistently applied). Finally, I'm often using * for allocation in contrast to using A_mul_B! when the memory has already been allocated so this could actually become a real problem.

@cstjean
Copy link
Contributor

cstjean commented Aug 31, 2016

Fair enough.

*(A::AbstractMatrix, J::UniformScaling) = J.λ == 1 ? A : J.λ*A
*(J::UniformScaling, A::AbstractVecOrMat) = J.λ == 1 ? A : J.λ*A
*(A::AbstractMatrix, J::UniformScaling) = J.λ == 1 ? copy(A) : J.λ*A
*(J::UniformScaling, A::AbstractVecOrMat) = J.λ == 1 ? copy(A) : J.λ*A
Copy link
Contributor

Choose a reason for hiding this comment

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

Neither the new definition nor the previous one is type-stable (eg. UniformScaling(1.0) * eye(Int, 3) returns a Matrix{Int}). Should it be fixed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. Thanks.


function *(A::AbstractMatrix, J::UniformScaling)
if J.λ == 1
T = promote_op(*, eltype(A), eltype(J))
Copy link
Contributor

Choose a reason for hiding this comment

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

why have the special case at all?

Copy link
Member Author

Choose a reason for hiding this comment

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

Doh.

...and remove unnecessary UniformScaling*SparseMatrixCSC methods
@cstjean
Copy link
Contributor

cstjean commented Aug 31, 2016

The special casing of 1 does seem out-of-place. Shouldn't it be done in *(::AbstractArray, ::Number) if it's done at all? FWIW, a copy is about 35% faster than a scalar multiplication on a 100K element vector, on 0.4.

I know we're trying to avoid having too many types, but if IdentityMatrix were its own type, then *(m::AbstractMatrix, ::IdentityMatrix) = m would obey the alias-stability criterion.

@andreasnoack
Copy link
Member Author

andreasnoack commented Aug 31, 2016

@cstjean I've removed the special casing. It might make sense to special-case it for performance, but then I think it should happen in the code for .* and/or scale! and not here.

I don't think it is necessary to avoid the copy for performance by introducing a new type. Where performance is critical, the user will probably use A_mul_B! to do the multiplication in-place so we could then just add an A_mul_B! method for UniformScaling and here aliasing shouldn't be an issue.

@andreasnoack
Copy link
Member Author

@tkelman I've updated the tests to make sure that the non-commutative case is tested. I realized that Number*Array doesn't dispatch to scale!.

@andreasnoack andreasnoack merged commit cd94c99 into master Sep 1, 2016
@andreasnoack andreasnoack deleted the anj/uniform branch September 1, 2016 13:15
mfasi pushed a commit to mfasi/julia that referenced this pull request Sep 5, 2016
* Avoid aliasing in in UniformScaling*AbstractMatrix

...and remove unnecessary UniformScaling*SparseMatrixCSC methods

* Broaden the tests for non-commutative multiplication

* Add Quaternion test case for q*[q] and clean up the Quaternion test type
tkelman pushed a commit that referenced this pull request Sep 6, 2016
* Avoid aliasing in in UniformScaling*AbstractMatrix

...and remove unnecessary UniformScaling*SparseMatrixCSC methods

* Broaden the tests for non-commutative multiplication

* Add Quaternion test case for q*[q] and clean up the Quaternion test type

(cherry picked from commit cd94c99)
tkelman pushed a commit that referenced this pull request Sep 13, 2016
* Avoid aliasing in in UniformScaling*AbstractMatrix

...and remove unnecessary UniformScaling*SparseMatrixCSC methods

* Broaden the tests for non-commutative multiplication

* Add Quaternion test case for q*[q] and clean up the Quaternion test type

(cherry picked from commit cd94c99)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants