Skip to content

wrapping with an OffsetArray results in slow scalar indexing :( #291

Description

@bjarthur

with just a DiskArray the index count is 1 (which is good):

julia> using DiskArrays

julia> using DiskArrays.TestTypes

julia> a = AccessCountDiskArray(reshape(1:24, 2, 3, 4), chunksize=(2, 3, 2))
2×3×4 AccessCountDiskArray{Int64, 3, Base.ReshapedArray{Int64, 3, UnitRange{Int64}, Tuple{}}, DiskArrays.ChunkRead{DiskArrays.NoStepRange}}

Chunked: (
    [2]
    [3]
    [2, 2]
)

julia> getindex_count(a)
0

julia> a[:,:,1]
2×3 Matrix{Int64}:
 1  3  5
 2  4  6

julia> getindex_count(a)
1

but wrap it in an OffsetArray and it is 6 (very bad!):

julia> using OffsetArrays

julia> o = OffsetArray(a, 1,2,3)
2×3×4 OffsetArray(::AccessCountDiskArray{Int64, 3, Base.ReshapedArray{Int64, 3, UnitRange{Int64}, Tuple{}}, DiskArrays.ChunkRead{DiskArrays.NoStepRange}}, 2:3, 3:5, 4:7) with eltype Int64 with indices 2:3×3:5×4:7:
[:, :, 4] =
 1  3  5
 2  4  6

[:, :, 5] =
 7   9  11
 8  10  12

[:, :, 6] =
 13  15  17
 14  16  18

[:, :, 7] =
 19  21  23
 20  22  24

julia> getindex_count(a)
97

julia> o[:,:,4]
2×3 OffsetArray(::Matrix{Int64}, 2:3, 3:5) with eltype Int64 with indices 2:3×3:5:
 1  3  5
 2  4  6

julia> getindex_count(a)    ### 6!  when ideally should be 1
103

because somehow it doesn't chunk the access:

julia> allow_scalar(false)
false

julia> a[:,:,1]
2×3 Matrix{Int64}:
 1  3  5
 2  4  6

julia> o[:,:,4]
ERROR: Scalar indexing with `Int` is very slow, and currently is disallowed. Run DiskArrays.allowscalar(true) to allow
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:44
  [2] _scalar_error()
    @ DiskArrays ~/.julia/packages/DiskArrays/uMiMX/src/scalar.jl:34
  [3] checkscalar(A::AccessCountDiskArray{…}, I::Tuple{…})
    @ DiskArrays ~/.julia/packages/DiskArrays/uMiMX/src/scalar.jl:30
  [4] getindex_disk(::AccessCountDiskArray{…}, ::Int64, ::Int64, ::Vararg{…})
    @ DiskArrays ~/.julia/packages/DiskArrays/uMiMX/src/indexing.jl:32
  [5] getindex
    @ ~/.julia/packages/DiskArrays/uMiMX/src/indexing.jl:312 [inlined]
  [6] getindex
    @ ~/.julia/packages/OffsetArrays/b7qpm/src/OffsetArrays.jl:421 [inlined]
  [7] macro expansion
    @ ./multidimensional.jl:981 [inlined]
  [8] macro expansion
    @ ./cartesian.jl:64 [inlined]
  [9] macro expansion
    @ ./multidimensional.jl:979 [inlined]
 [10] _unsafe_getindex!
    @ ./multidimensional.jl:989 [inlined]
 [11] _unsafe_getindex(::IndexCartesian, ::OffsetArray{…}, ::Base.Slice{…}, ::Base.Slice{…}, ::Int64)
    @ Base ./multidimensional.jl:970
 [12] _getindex
    @ ./multidimensional.jl:956 [inlined]
 [13] getindex(::OffsetArray{Int64, 3, AccessCountDiskArray{…}}, ::Function, ::Function, ::Int64)
    @ Base ./abstractarray.jl:1342
 [14] top-level scope
    @ REPL[14]:1
Some type information was truncated. Use `show(err)` to see complete types.

happy to help fix this issue given a little guidance!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions