You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
AFAICT, the @inbounds macro is not propagated into lambdas, generators and broadcasts.
using Base:@propagate_boundsusing LinearAlgebra
@propagate_inboundsf(a, i, x) = a[i+1] - x
functionfoo(a)
ret =zero(eltype(a))
@inboundsfor i inaxes(a, 1)
ret +=f(a, i, 0)
end
ret
endfunctionfooG(a)
@inboundssum(f(a, i, 0) for i inaxes(a, 1))
endfunctionfooλ(a)
@inboundssum(i ->f(a, i, 0), a)
endfunctionfooB(a)
la =length(a)
@inboundsdot([1,2,3], f.(Ref(a), la, [1,2,3]))
end
julia> a =1:51:5
julia>foo(a)
20
julia>fooG(a)
ERROR: BoundsError: attempt to access 5-element UnitRange{Int64} at index [6]
Stacktrace:
[1] getindex at ./range.jl:646 [inlined]
[2] f at ./REPL[28]:1 [inlined]
[3] #9 at ./none:0 [inlined]
[4] MappingRF at ./reduce.jl:93 [inlined]
[5] _foldl_impl at ./reduce.jl:62 [inlined]
[6] foldl_impl at ./reduce.jl:48 [inlined]
[7] mapfoldl_impl at ./reduce.jl:44 [inlined]
[8] #mapfoldl#204 at ./reduce.jl:160 [inlined]
[9] mapfoldl at ./reduce.jl:160 [inlined]
[10] #mapreduce#208 at ./reduce.jl:287 [inlined]
[11] mapreduce at ./reduce.jl:287 [inlined]
[12] sum at ./reduce.jl:494 [inlined]
[13] sum at ./reduce.jl:511 [inlined]
[14] fooG(::UnitRange{Int64}) at ./REPL[16]:2
[15] top-level scope at REPL[35]:1
[16] run_repl(::REPL.AbstractREPL, ::Any) at /tmp/portage/dev-lang/julia-1.5.3/work/julia-1.5.3/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288
julia>fooλ(a)
ERROR: BoundsError: attempt to access 5-element UnitRange{Int64} at index [6]
Stacktrace:
[1] getindex at ./range.jl:646 [inlined]
[2] f at ./REPL[28]:1 [inlined]
[3] #11 at ./REPL[17]:2 [inlined]
[4] _mapreduce(::var"#11#12"{UnitRange{Int64}}, ::typeof(Base.add_sum), ::IndexLinear, ::UnitRange{Int64}) at ./reduce.jl:411
[5] _mapreduce_dim at ./reducedim.jl:318 [inlined]
[6] #mapreduce#620 at ./reducedim.jl:310 [inlined]
[7] mapreduce at ./reducedim.jl:310 [inlined]
[8] _sum at ./reducedim.jl:727 [inlined]
[9] #sum#628 at ./reducedim.jl:723 [inlined]
[10] sum at ./reducedim.jl:723 [inlined]
[11] fooλ(::UnitRange{Int64}) at ./REPL[17]:2
[12] top-level scope at REPL[36]:1
[13] run_repl(::REPL.AbstractREPL, ::Any) at /tmp/portage/dev-lang/julia-1.5.3/work/julia-1.5.3/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288
julia>fooB(a)
ERROR: BoundsError: attempt to access 5-element UnitRange{Int64} at index [6]
Stacktrace:
[1] getindex at ./range.jl:646 [inlined]
[2] f at ./REPL[28]:1 [inlined]
[3] _broadcast_getindex_evalf at ./broadcast.jl:648 [inlined]
[4] _broadcast_getindex at ./broadcast.jl:621 [inlined]
[5] getindex at ./broadcast.jl:575 [inlined]
[6] macro expansion at ./broadcast.jl:932 [inlined]
[7] macro expansion at ./simdloop.jl:77 [inlined]
[8] copyto! at ./broadcast.jl:931 [inlined]
[9] copyto! at ./broadcast.jl:886 [inlined]
[10] copy at ./broadcast.jl:862 [inlined]
[11] materialize at ./broadcast.jl:837 [inlined]
[12] fooB(::UnitRange{Int64}) at ./REPL[18]:3
[13] top-level scope at REPL[37]:1
[14] run_repl(::REPL.AbstractREPL, ::Any) at /tmp/portage/dev-lang/julia-1.5.3/work/julia-1.5.3/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288
Although this can be circumvented for Generators and Lambdas by moving the @inbounds macro into the Generator/Lambda, this is rather unpractical in functions which are @propagate_inbounds themselves. The developer does not know, if he needs to add a @inbounds macro statically.
The only workaround I know for the broadcasting part is to add an explicit @inbounds inside the broadcasted function. This is hard if the function shouldn't/can't change and the same problem in propagated contexts applies.
How can I propagate disabled bounds checks into generators, lambdas and broadcasts. And how can I do this, when the foo functions are not explicitely marked @inbounds, but are @propagate_inbounds marked themselves?
The text was updated successfully, but these errors were encountered:
AFAICT, the
@inbounds
macro is not propagated into lambdas, generators and broadcasts.Although this can be circumvented for Generators and Lambdas by moving the
@inbounds
macro into the Generator/Lambda, this is rather unpractical in functions which are@propagate_inbounds
themselves. The developer does not know, if he needs to add a@inbounds
macro statically.The only workaround I know for the broadcasting part is to add an explicit
@inbounds
inside the broadcasted function. This is hard if the function shouldn't/can't change and the same problem in propagated contexts applies.How can I propagate disabled bounds checks into generators, lambdas and broadcasts. And how can I do this, when the
foo
functions are not explicitely marked@inbounds
, but are@propagate_inbounds
marked themselves?The text was updated successfully, but these errors were encountered: