Skip to content

Commit 43af516

Browse files
committed
Accelerate reduction over dim1
1 parent 79c1ba4 commit 43af516

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

base/reducedim.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,25 @@ function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArrayOrBroadcasted)
267267
if reducedim1(R, A)
268268
# keep the accumulator as a local variable when reducing along the first dimension
269269
i1 = first(axes1(R))
270+
ax1 = axes1(A)
270271
@inbounds for IA in CartesianIndices(indsAt)
271272
IR = Broadcast.newindex(IA, keep, Idefault)
272-
r = R[i1,IR]
273-
@simd for i in axes(A, 1)
274-
r = op(r, f(A[i, IA]))
273+
if op === min || op === max #|| op === _extrema_op
274+
elf(i) = @inbounds f(A[i, IA])
275+
r = mapreduce_impl(elf, op, ax1, firstindex(ax1), lastindex(ax1))
276+
R[i1,IR] = op(R[i1,IR], r)
277+
else
278+
r = R[i1,IR]
279+
@simd for i in ax1
280+
r = op(r, f(A[i, IA]))
281+
end
282+
R[i1,IR] = r
275283
end
276-
R[i1,IR] = r
277284
end
278285
else
279286
@inbounds for IA in CartesianIndices(indsAt)
280287
IR = Broadcast.newindex(IA, keep, Idefault)
281-
@simd for i in axes(A, 1)
288+
@simd for i in axes1(A)
282289
R[i,IR] = op(R[i,IR], f(A[i,IA]))
283290
end
284291
end

0 commit comments

Comments
 (0)