Skip to content

Commit cecd5f2

Browse files
jishnubmbauman
authored and
KristofferC
committed
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]> (cherry picked from commit 3d20a92)
1 parent 4d46082 commit cecd5f2

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

base/reducedim.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,9 @@ function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArrayOrBroadcasted)
291291
# use mapreduce_impl, which is probably better tuned to achieve higher performance
292292
nslices = div(length(A), lsiz)
293293
ibase = first(LinearIndices(A))-1
294-
for i = 1:nslices
295-
@inbounds R[i] = op(R[i], mapreduce_impl(f, op, A, ibase+1, ibase+lsiz))
294+
for i in eachindex(R)
295+
r = op(@inbounds(R[i]), mapreduce_impl(f, op, A, ibase+1, ibase+lsiz))
296+
@inbounds R[i] = r
296297
ibase += lsiz
297298
end
298299
return R

test/offsetarray.jl

+7
Original file line numberDiff line numberDiff line change
@@ -872,3 +872,10 @@ end
872872
show(b, (A, A))
873873
@test String(take!(b)) == "([1], [1])"
874874
end
875+
876+
@testset "mapreduce with OffsetRanges" begin
877+
r = 5:100
878+
a = OffsetArray(r, 2)
879+
b = sum(a, dims=1)
880+
@test b[begin] == sum(r)
881+
end

0 commit comments

Comments
 (0)