Skip to content

Add mapreduce_layout hook for reductions on LayoutArrays - #281

Merged
dlfivefifty merged 1 commit into
masterfrom
mapreduce-layout
Sep 25, 2026
Merged

dlfivefifty merged 1 commit into
masterfrom
mapreduce-layout

Conversation

@dlfivefifty

Copy link
Copy Markdown
Member

Adds a layout hook for reductions, alongside the existing _norm hook:

mapreduce_layout(::Any, f, op, A, dims; kw...) = invoke(mapreduce, Tuple{Any,Any,AbstractArray}, f, op, A; dims, kw...)
Base.mapreduce(f, op, A::LayoutArray; dims=:, kw...) = mapreduce_layout(MemoryLayout(A), f, op, A, dims; kw...)
Base.mapreduce(f, op, A::SubArray{<:Any,N,<:LayoutArray}; dims=:, kw...) where N = mapreduce_layout(MemoryLayout(A), f, op, A, dims; kw...)

sum, prod, maximum, minimum, extrema and reduce all end in mapreduce, so one overload per layout covers them all. The default falls back to Base, so behaviour doesn't change unless a layout opts in. dims and init are passed through.

Motivation: padded arrays in LazyArrays. For example, sum(zeros(∞,2)) and sum(zeros(2,∞)) currently loop forever, because Base iterates over all the zeros. A LazyArrays PR will overload mapreduce_layout for AbstractPaddedLayout so that it reduces over the padded data only.

any, all and count don't go through mapreduce in Base, so they aren't affected.

Testing:

  • Added tests checking that sum, maximum, extrema, prod and mapreduce (with dims and init) match Base for LayoutArrays and their views.
  • A custom layout checks that the hook is actually dispatched to.
  • The full test suite passes on Julia 1.13 and 1.10, including Aqua.
  • The LazyArrays test suite passes with this branch.

Bumps the version to v1.13.0 since this adds a new overloadable function.

🤖 Generated with Claude Code

`mapreduce(f, op, A)` for a `LayoutArray` (or a view of one) now calls
`mapreduce_layout(MemoryLayout(A), f, op, A, dims; kw...)`. By default
this falls back to Base's implementation. Layouts can overload it to
reduce more efficiently, e.g. to avoid iterating over the zeros of an
infinite padded array. Since `sum`, `prod`, `maximum`, `minimum` and
`extrema` all go through `mapreduce`, a single overload covers them all.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.24%. Comparing base (1e4fcc1) to head (15ed221).
⚠️ Report is 12 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master     #281       +/-   ##
===========================================
+ Coverage    0.00%   90.24%   +90.24%     
===========================================
  Files          11       12        +1     
  Lines        1858     1969      +111     
===========================================
+ Hits            0     1777     +1777     
+ Misses       1858      192     -1666     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dlfivefifty
dlfivefifty merged commit bd4ba9f into master Sep 25, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant