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

Broadcast shouldn't use inbounds when calling f #19188

Merged
merged 2 commits into from
Nov 4, 2016

Conversation

mbauman
Copy link
Member

@mbauman mbauman commented Nov 2, 2016

Previously, broadcast(getindex, A, I...) would call getindex within an
at-inbounds context. This patch simply moves the function call so that it
will throw a BoundsError appropriately.

Fixes #19203.

@@ -337,3 +337,8 @@ end
@test broadcast(+, 1.0, (0, -2.0)) == (1.0,-1.0)
@test broadcast(+, 1.0, (0, -2.0), [1]) == [2.0, 0.0]
@test broadcast(*, ["Hello"], ", ", ["World"], "!") == ["Hello, World!"]

# Ensure that broadcast doesn't use @inbounds when calling the function
Copy link
Member Author

@mbauman mbauman Nov 2, 2016

Choose a reason for hiding this comment

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

The tests are run with --check-bounds=yes, no? What's the best way to make sure that this catches regressions?

Copy link
Contributor

Choose a reason for hiding this comment

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

test/boundscheck.jl

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, thanks!

@mbauman mbauman force-pushed the mb/broadcast-outbounds branch from 409650a to 940c111 Compare November 2, 2016 04:23
@kshyatt kshyatt added the broadcast Applying a function over a collection label Nov 2, 2016
Previously, `broadcast(getindex, A, I...)` would call `getindex` within an
at-inbounds context. This patch simply moves the function call so that it
will throw a BoundsError appropriately.
@stevengj
Copy link
Member

stevengj commented Nov 3, 2016

@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

@stevengj
Copy link
Member

stevengj commented Nov 3, 2016

There seem to be some relevant slowdowns. It seems like we should still be able to use @inbounds somewhere, maybe in _broadcast_getindex?

@mbauman
Copy link
Member Author

mbauman commented Nov 3, 2016

This is a bug-fix. The loss in performance is entirely due to restoring bounds checks in places where they had been erroneously removed before. In order to re-gain that performance, we'd have to add @propagate_inbounds annotations to the entire call chain, and then annotate the broadcast calls — if they're safe — with @inbounds f.(...).

Unfortunately, this uses @noinline (I assume) to ensure type stability of the inner loop, which precludes the use of @propagate_inbounds.

@stevengj
Copy link
Member

stevengj commented Nov 3, 2016

But wouldn't adding

@inline _broadcast_getindex(::Any, A::AbstractArray, I) = @inbounds A[I]

be safe? Here, the indices I are constructed by broadcast itself, and should always be inside the array bounds.

@mbauman
Copy link
Member Author

mbauman commented Nov 3, 2016

Sure, but that's unrelated to this change. Edit: Oh, I see, you want to pair it with a perf enhancement. Makes sense and is simple. I'll do it tonight.

Try to restore some performance by allowing inbounds propagation into _broadcast_getindex.
@mbauman
Copy link
Member Author

mbauman commented Nov 3, 2016

@nanosoldier runbenchmarks(ALL, vs=":master")

@nanosoldier
Copy link
Collaborator

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

@stevengj stevengj merged commit 89d9f16 into master Nov 4, 2016
@stevengj stevengj deleted the mb/broadcast-outbounds branch November 4, 2016 01:33
@tkelman
Copy link
Contributor

tkelman commented Feb 22, 2017

I don't think this is relevant on release-0.5 without #16986

fcard pushed a commit to fcard/julia that referenced this pull request Feb 28, 2017
* Broadcast shouldn't use inbounds when calling f

Previously, `broadcast(getindex, A, I...)` would call `getindex` within an
at-inbounds context. This patch simply moves the function call so that it
will throw a BoundsError appropriately.

* Add inbounds elsewhere in broadcast

Try to restore some performance by allowing inbounds propagation into _broadcast_getindex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
broadcast Applying a function over a collection
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants