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

Feature request: overloadable updating operators #3217

Closed
bsxfan opened this issue May 27, 2013 · 5 comments
Closed

Feature request: overloadable updating operators #3217

bsxfan opened this issue May 27, 2013 · 5 comments
Labels
needs decision A decision on this change is needed status:won't change Indicates that work won't continue on an issue or pull request

Comments

@bsxfan
Copy link

bsxfan commented May 27, 2013

As far as I can see, += and the other updating operators are not handled via overloadable methods. For example, A += B results in A = +(A,B). There is considerable scope for performance gains, especially for matrices, if one could tailor the behaviour of +=. As an example, consider the case of A += B, where both are 1000 by 1000 matrices. I get (total elapsed time for 100 such operations) on an 8-core machine:

  1. Standard Julia A+=B: 1s
  2. A hand-coded loop that iterates over all 10^6 elements: 0.5s
  3. Linalg.BLAS.axpy!(1.0,A,B): 0.02s

(The advantage of 2 over 1 is that it does not have to allocate a temporary matrix to accomodate the sum. The advantage of 3 over 2 is multiple threads and whatever other magic BLAS does.)

@bsxfan
Copy link
Author

bsxfan commented May 27, 2013

In the mean time, (similar in spirit to A[diagonal] as discussed in #3164), I have rigged my setindex! so that A[:+] = B calls into BLAS.axpy!. I have a more complicated version also, where A[:+] = B,C,D sums all of A,B,C,D in-place in A.

@IainNZ
Copy link
Member

IainNZ commented May 30, 2013

I've always thought not having this was an oversight, but when I asked someone about it, apparently it didn't play nice with type inference? Not sure. But if it is indeed not possible, would be good to document why.

@JeffBezanson
Copy link
Sponsor Member

Nope, it's totally possible and would have no effect on type inference. What we didn't like about it is that it has to work on both mutable and immutable types (e.g. both arrays and numbers). The expression x += y would become a bit of a trap, as it would work fine for numbers, but if the same code were called on arrays it would mutate x unexpectedly. It also creates the possibility of + and += doing totally different things, or accidentally having slightly different behavior.

@IainNZ
Copy link
Member

IainNZ commented May 30, 2013

Ah I see, that does make sense. Just enough rope to hang oneself...

@JeffBezanson
Copy link
Sponsor Member

I feel this is addressed by issues #249 #1115 #3022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs decision A decision on this change is needed status:won't change Indicates that work won't continue on an issue or pull request
Projects
None yet
Development

No branches or pull requests

3 participants