Skip to content

Commit 3d20a92

Browse files
jishnubmbauman
andauthored
Fix indexing in _mapreducedim for OffsetArrays (#55506)
The destination array was being indexed incorrectly if it had offset indices. This led to the following on nightly: ```julia julia> using OffsetArrays julia> r = 5:100; julia> a = OffsetVector(r, 2); julia> sum(a, dims=1) 1-element OffsetArray(::Vector{Int64}, 3:3) with eltype Int64 with indices 3:3: 0 julia> sum(a) 5040 ``` The indexing was marked `@inbounds`, so this was not throwing an error. This PR also follows #55329 and only marks the indexing operations as `@inbounds`, omitting the function calls. --------- Co-authored-by: Matt Bauman <[email protected]>
1 parent 0c64283 commit 3d20a92

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

base/reducedim.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,9 @@ function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArrayOrBroadcasted)
258258
# use mapreduce_impl, which is probably better tuned to achieve higher performance
259259
nslices = div(length(A), lsiz)
260260
ibase = first(LinearIndices(A))-1
261-
for i = 1:nslices
262-
@inbounds R[i] = op(R[i], mapreduce_impl(f, op, A, ibase+1, ibase+lsiz))
261+
for i in eachindex(R)
262+
r = op(@inbounds(R[i]), mapreduce_impl(f, op, A, ibase+1, ibase+lsiz))
263+
@inbounds R[i] = r
263264
ibase += lsiz
264265
end
265266
return R

test/offsetarray.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,3 +907,10 @@ end
907907
v = view([1,2,3,4], :)
908908
@test v[Base.IdentityUnitRange(2:3)] == OffsetArray(2:3, 2:3)
909909
end
910+
911+
@testset "mapreduce with OffsetRanges" begin
912+
r = 5:100
913+
a = OffsetArray(r, 2)
914+
b = sum(a, dims=1)
915+
@test b[begin] == sum(r)
916+
end

0 commit comments

Comments
 (0)