Open siddharthabishnu opened 1 month ago
Will replacing an @inbounds with @propagate_inbounds fix the problem? Does this happen with --check-bounds=yes
?
Indeed, I think the fact you do not have an out-of-bounds access error is connected to the @inbounds flag.
With regards to the second problem, I think it might be more consistent to have the correct "displaced" indices in interior
refer to the valid data chunk instead of the index starting from 1. We need a PR to address this.
The user should always be working with "grid" indices. So yeah that should be fixed. I'm surprised that view
doesn't give us something with grid indices though already using OffsetArray
.
Also just to note, this is our design, so its not really about being "consistent" or not. We just have to implement the design we choose, for the moment we choose that the user works with grid indices.
@glwagner and @simone-silvestri, thank you for the clarification. Indeed, this does not happen with --check-bounds=yes
, as shown below:
julia> my_field[-2, :, :]
ERROR: BoundsError: attempt to access 6×7×8 OffsetArray(::Array{Float64, 3}, -1:4, -1:5, -1:6) with eltype Float64 with indices -1:4×-1:5×-1:6 at index [-2, OffsetArrays.IdOffsetRange(values=-1:5, indices=-1:5), OffsetArrays.IdOffsetRange(values=-1:6, indices=-1:6)]
Stacktrace:
[1] throw_boundserror(A::OffsetArrays.OffsetArray{Float64, 3, Array{Float64, 3}}, I::Tuple{Int64, Base.Slice{OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}}}, Base.Slice{OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}}}})
@ Base ./abstractarray.jl:737
[2] checkbounds
@ ./abstractarray.jl:702 [inlined]
[3] _getindex
@ ./multidimensional.jl:888 [inlined]
[4] getindex
@ ./abstractarray.jl:1291 [inlined]
[5] getindex(::Field{Center, Center, Center, Nothing, RectilinearGrid{…}, Tuple{…}, OffsetArrays.OffsetArray{…}, Float64, FieldBoundaryConditions{…}, Nothing, Oceananigans.Fields.FieldBoundaryBuffers{…}}, ::Int64, ::Function, ::Function)
@ Oceananigans.Fields /Users/Sid/Library/CloudStorage/Dropbox/StudyFolder/PostDocMITDesktop/Codes/Oceananigans/main/src/Fields/field.jl:408
[6] top-level scope
@ REPL[23]:1
Some type information was truncated. Use `show(err)` to see complete types.
julia> my_field[:, -2, :]
ERROR: BoundsError: attempt to access 6×7×8 OffsetArray(::Array{Float64, 3}, -1:4, -1:5, -1:6) with eltype Float64 with indices -1:4×-1:5×-1:6 at index [OffsetArrays.IdOffsetRange(values=-1:4, indices=-1:4), -2, OffsetArrays.IdOffsetRange(values=-1:6, indices=-1:6)]
Stacktrace:
[1] throw_boundserror(A::OffsetArrays.OffsetArray{Float64, 3, Array{Float64, 3}}, I::Tuple{Base.Slice{OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}}}, Int64, Base.Slice{OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}}}})
@ Base ./abstractarray.jl:737
[2] checkbounds
@ ./abstractarray.jl:702 [inlined]
[3] _getindex
@ ./multidimensional.jl:888 [inlined]
[4] getindex
@ ./abstractarray.jl:1291 [inlined]
[5] getindex(::Field{Center, Center, Center, Nothing, RectilinearGrid{…}, Tuple{…}, OffsetArrays.OffsetArray{…}, Float64, FieldBoundaryConditions{…}, Nothing, Oceananigans.Fields.FieldBoundaryBuffers{…}}, ::Function, ::Int64, ::Function)
@ Oceananigans.Fields /Users/Sid/Library/CloudStorage/Dropbox/StudyFolder/PostDocMITDesktop/Codes/Oceananigans/main/src/Fields/field.jl:408
[6] top-level scope
@ REPL[24]:1
Some type information was truncated. Use `show(err)` to see complete types.
julia> my_field[:, :, -2]
ERROR: BoundsError: attempt to access 6×7×8 OffsetArray(::Array{Float64, 3}, -1:4, -1:5, -1:6) with eltype Float64 with indices -1:4×-1:5×-1:6 at index [OffsetArrays.IdOffsetRange(values=-1:4, indices=-1:4), OffsetArrays.IdOffsetRange(values=-1:5, indices=-1:5), -2]
Stacktrace:
[1] throw_boundserror(A::OffsetArrays.OffsetArray{Float64, 3, Array{Float64, 3}}, I::Tuple{Base.Slice{OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}}}, Base.Slice{OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}}}, Int64})
@ Base ./abstractarray.jl:737
[2] checkbounds
@ ./abstractarray.jl:702 [inlined]
[3] _getindex
@ ./multidimensional.jl:888 [inlined]
[4] getindex
@ ./abstractarray.jl:1291 [inlined]
[5] getindex(::Field{Center, Center, Center, Nothing, RectilinearGrid{…}, Tuple{…}, OffsetArrays.OffsetArray{…}, Float64, FieldBoundaryConditions{…}, Nothing, Oceananigans.Fields.FieldBoundaryBuffers{…}}, ::Function, ::Function, ::Int64)
@ Oceananigans.Fields /Users/Sid/Library/CloudStorage/Dropbox/StudyFolder/PostDocMITDesktop/Codes/Oceananigans/main/src/Fields/field.jl:408
[6] top-level scope
@ REPL[25]:1
Some type information was truncated. Use `show(err)` to see complete types.
julia> interior(my_windowed_field, :, :, 5:6)
ERROR: BoundsError: attempt to access 2×3×2 view(::Array{Float64, 3}, 3:4, 3:5, :) with eltype Float64 at index [1:2, 1:3, 5:6]
Stacktrace:
[1] throw_boundserror(A::SubArray{Float64, 3, Array{Float64, 3}, Tuple{UnitRange{Int64}, UnitRange{Int64}, Base.Slice{Base.OneTo{Int64}}}, false}, I::Tuple{Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}})
@ Base ./abstractarray.jl:737
[2] checkbounds
@ ./abstractarray.jl:702 [inlined]
[3] view(::SubArray{Float64, 3, Array{Float64, 3}, Tuple{UnitRange{Int64}, UnitRange{Int64}, Base.Slice{Base.OneTo{Int64}}}, false}, ::Function, ::Function, ::UnitRange{Int64})
@ Base ./subarray.jl:184
[4] interior(::Field{…}, ::Function, ::Function, ::UnitRange{…})
@ Oceananigans.Fields /Users/Sid/Library/CloudStorage/Dropbox/StudyFolder/PostDocMITDesktop/Codes/Oceananigans/main/src/Fields/field.jl:395
[5] top-level scope
@ REPL[10]:1
Some type information was truncated. Use `show(err)` to see complete types.
I will create a PR to address the second issue, as suggested by @simone-silvestri.
Consider the following MWE:
Even by accessing array elements indices outside the interior and halo ranges, we don’t get out-of-bounds errors. Instead, we obtain junk values for these indices. This behavior applies to windowed fields as well. But why is that the case? What is the reasoning behind this design choice, since I am assuming it’s on purpose?
Secondly, when accessing the interior elements of a windowed field, the indices in the windowed dimension must start from 1 instead of their actual values, which also results in junk values for these elements. Is this also intentional, or should I create a pull request to address it?
cc @glwagner @simone-silvestri @navidcy