Skip to content

Fold init into mapreduce with | or & along the rows - #1069

Merged
ViralBShah merged 2 commits into
mainfrom
vs/any-all-dims-init
Oct 14, 2026
Merged

ViralBShah merged 2 commits into
mainfrom
vs/any-all-dims-init

Conversation

@ViralBShah

Copy link
Copy Markdown
Member

mapreduce(pred, |, A; dims=1, init) and mapreduce(pred, &, A; dims=1, init) on a sparse matrix ignore init: the result is the reduction of each column alone, where dense folds it into the initial value.

Before

julia> A = sparse([1.0 2.0; 0.0 3.0]);

julia> mapreduce(x -> x > 5, |, A; dims=1, init=true)
1×2 Matrix{Bool}:
 0  0

julia> mapreduce(!iszero, &, A; dims=1, init=false)
1×2 Matrix{Bool}:
 0  1

After

julia> mapreduce(x -> x > 5, |, A; dims=1, init=true)
1×2 Matrix{Bool}:
 1  1

julia> mapreduce(!iszero, &, A; dims=1, init=false)
1×2 Matrix{Bool}:
 0  0

The short-circuiting _mapreducerows! kernel for | and & assigned each column's reduction to R[1, ii], which on entry holds the initial value. It now reads that value first, skips a column the initial value already decides, and stores the column's reduction otherwise. The & method reuses the | kernel with the negated predicate as before, but passes a negate::Bool flag in place of the post function, so that the kernel can also interpret the initial value.

mapreduce(>(2.0), |, A; dims=1) on a 10^4 × 10^3 matrix with 10^6 stored entries, none of which satisfies the predicate, takes 273 μs and 1280 bytes before and 274 μs and 1280 bytes after.

Split from #1022.

This is a wrong result on released versions too (Julia 1.13 gives the same output), so it is a backport candidate.

Written by Claude Code.

🤖 Generated with Claude Code

ViralBShah and others added 2 commits October 13, 2026 03:06
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.78%. Comparing base (c0f75f0) to head (23e1ae6).
⚠️ Report is 35 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1069      +/-   ##
==========================================
+ Coverage   94.75%   94.78%   +0.02%     
==========================================
  Files          16       16              
  Lines        9885     9880       -5     
==========================================
- Hits         9367     9365       -2     
+ Misses        518      515       -3     

☔ 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.

@ViralBShah ViralBShah left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed by Codex at 23e1ae6.

No actionable findings. The initial-value short circuit and the negated-predicate path handle | and & consistently, including columns with implicit zeros and fully stored columns.

@ViralBShah ViralBShah added the backport 1.13 Change should be backported to release-1.13 label Oct 14, 2026
@ViralBShah
ViralBShah merged commit 18a8e4a into main Oct 14, 2026
10 checks passed
@ViralBShah
ViralBShah deleted the vs/any-all-dims-init branch October 14, 2026 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 1.13 Change should be backported to release-1.13

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant