@inbounds does nothing when converting Cartesian to linear Index

Hi,

I do not understand the profiling of the following MWE.

using BenchmarkTools
using Random

function weirdsum(a,lis,ci)
    li = @inbounds lis[ci]
    ac = @inbounds a[ci]
    al = @inbounds a[li]
    return ac+al
end

function testli()
    Random.seed!(123)
    a=rand(4,5,6)
    cis = CartesianIndices(a)
    lis = LinearIndices(cis)
    s =0.0
    @inbounds for i = 1:1000_000_000
        ci = rand(cis)
        s+=weirdsum(a,lis,ci)
    end
    @show s
end

testli()

It seems that a significant time proportion is spent in bound checking (line 5 : li = @inbounds lis[ci]) despite the @inbounds macro.

Any hint ?

Laurent

I don’t see that behavior. Can you reproduce it in a fresh Julia session?

1 Like

I have just created a fresh project and use a restarted VS Code session (for @profview)
and I get the same result.

Just in case, here is the result of versioninfo()

julia> include("toto.jl")
s = 9.716345882702819e8
9.716345882702819e8

julia> @profview testli()
s = 9.716345882702819e8

julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 Γ— Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
  Threads: 11 on 8 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 12

julia> 

Is there anything in your startup? Or does --check-bounds=yes appear in Base.julia_cmd()?

I only see checkbounds in my profiles if I explicitly start Julia with --check-bounds=yes.

My terminal session
β–Ά julia +1.10 --startup-file=no -e 'using Random

function weirdsum(a,lis,ci)
    li = @inbounds lis[ci]
    ac = @inbounds a[ci]
    al = @inbounds a[li]
    return ac+al
end

function testli()
    Random.seed!(123)
    a=rand(4,5,6)
    cis = CartesianIndices(a)
    lis = LinearIndices(cis)
    s =0.0
    @inbounds for i = 1:1000_000_000
        ci = rand(cis)
        s+=weirdsum(a,lis,ci)
    end
    @show s
end

testli()
using Profile
@profile testli()
Profile.print()' | grep 'check'

 β–Ά julia +1.10 --startup-file=no --check-bounds=yes -e 'using Random

function weirdsum(a,lis,ci)
    li = @inbounds lis[ci]
    ac = @inbounds a[ci]
    al = @inbounds a[li]
    return ac+al
end

function testli()
    Random.seed!(123)
    a=rand(4,5,6)
    cis = CartesianIndices(a)
    lis = LinearIndices(cis)
    s =0.0
    @inbounds for i = 1:1000_000_000
        ci = rand(cis)
        s+=weirdsum(a,lis,ci)
    end
    @show s
end

testli()
using Profile
@profile testli()
Profile.print()' | grep 'check'
  1β•Ž    β•Ž    1    ...abstractarray.jl:697; checkbounds
   β•Ž    β•Ž    251  ...abstractarray.jl:699; checkbounds
   β•Ž    β•Ž     251  ...abstractarray.jl:684; checkbounds
246β•Ž    β•Ž     246  ...abstractarray.jl:697; checkbounds
251β•Ž    β•Ž     279  ...abstractarray.jl:699; checkbounds
   β•Ž    β•Ž    β•Ž 28   ...bstractarray.jl:678; checkbounds
   β•Ž    β•Ž  386  .../abstractarray.jl:699; checkbounds
   β•Ž    β•Ž   386  .../abstractarray.jl:678; checkbounds
   β•Ž    β•Ž    386  ...abstractarray.jl:725; checkbounds_indices
   β•Ž    β•Ž     385  ...abstractarray.jl:725; checkbounds_indices
   β•Ž    β•Ž    β•Ž 385  ...bstractarray.jl:725; checkbounds_indices
   β•Ž    β•Ž    β•Ž  385  ...bstractarray.jl:760; checkindex
   β•Ž    β•Ž     1    ...abstractarray.jl:760; checkindex
 52β•Ž    β•Ž   57   .../abstractarray.jl:699; checkbounds
   β•Ž    β•Ž    5    ...abstractarray.jl:684; checkbounds
   β•Ž    β•Ž     5    ...abstractarray.jl:760; checkindex
2 Likes

You are right ! Thank you very much for your help !

julia> Base.julia_cmd()
`/Users/laurentplagne/.julia/juliaup/julia-1.10.0+0.aarch64.apple.darwin14/bin/julia -C native -J/Users/laurentplagne/.julia/juliaup/julia-1.10.0+0.aarch64.apple.darwin14/lib/julia/sys.dylib --check-bounds=yes -g1`