Skip to content

Unwrap triangular matrices in broadcast #1332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 4, 2025
Merged

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented May 4, 2025

In broadcasting over triangular matrices, we loop only over the stored indices. For these indices, indexing into a triangular matrix is equivalent to indexing into its parent. We may therefore replace an UpperOrLowerTriangular matrix by its parent, which removes the branch in getindex. This improves performance:

julia> L = LowerTriangular(zeros(600,600));

julia> L2 = copy(L);

julia> @btime broadcast!(+, $L2, $L, $L);
  161.176 μs (0 allocations: 0 bytes) # master
  80.894 μs (0 allocations: 0 bytes) # this PR

This replacement is performed recursively on a Broadcasted object by looping over its args, and non-triangular elements are left untouched. Only UpperOrLowerTriangular matrices will be replaced by their parents.

Copy link
Member

@dkarrasch dkarrasch left a comment

Choose a reason for hiding this comment

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

Nice!

Copy link

codecov bot commented May 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.74%. Comparing base (c9b6456) to head (b50674e).
Report is 18 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1332   +/-   ##
=======================================
  Coverage   93.74%   93.74%           
=======================================
  Files          34       34           
  Lines       15752    15759    +7     
=======================================
+ Hits        14766    14773    +7     
  Misses        986      986           

☔ View full report in Codecov by Sentry.
📢 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.

@jishnub jishnub added the performance Must go faster label May 4, 2025
@jishnub jishnub merged commit 5165fd3 into master May 4, 2025
4 checks passed
@jishnub jishnub deleted the jishnub/preprocess_bcast branch May 4, 2025 15:55
@ViralBShah ViralBShah added the backport 1.12 Change should be backported to release-1.12 label May 11, 2025
jishnub added a commit that referenced this pull request May 12, 2025
In broadcasting over triangular matrices, we loop only over the stored
indices. For these indices, indexing into a triangular matrix is
equivalent to indexing into its parent. We may therefore replace an
`UpperOrLowerTriangular` matrix by its parent, which removes the branch
in `getindex`. This improves performance:
```julia
julia> L = LowerTriangular(zeros(600,600));

julia> L2 = copy(L);

julia> @Btime broadcast!(+, $L2, $L, $L);
  161.176 μs (0 allocations: 0 bytes) # master
  80.894 μs (0 allocations: 0 bytes) # this PR
```
This replacement is performed recursively on a `Broadcasted` object by
looping over its `args`, and non-triangular elements are left untouched.
Only `UpperOrLowerTriangular` matrices will be replaced by their
`parent`s.

(cherry picked from commit 5165fd3)
@jishnub jishnub mentioned this pull request May 12, 2025
27 tasks
jishnub added a commit that referenced this pull request May 26, 2025
Backported PRs:
- [x] #1209 <!-- Remove `LinearAlgebra` qualifications in `cholesky.jl`
-->
- [x] #1230 <!-- Avoid materializing `diag` in `Diagonal` `kron` -->
- [x] #1240 <!-- Reduce `stable_muladdmul` branches in `generic
matvecmul!` -->
- [x] #1247 <!-- fix dispatch to herk -->
- [x] #1255 <!-- use smaller matrix size in `peakflops` on 32-bit -->
- [x] #1310 <!-- Only `@noinline` error path in `matmul_size_check` -->
- [x] #1267 <!-- Refine column ranges in `_isbanded_impl` -->
- [x] #1320 <!-- Copy matrices in `triu`/`tril` if no zero exists for
the `eltype` -->
- [x] #1324 <!-- Fix empty `Tridiagonal` broadcast -->
- [x] #1327 <!-- `iszero` check in hessenberg setindex -->
- [x] #1326 <!-- Fix multiplication with empty `HessenbergQ` -->
- [x] #1332 <!-- Unwrap triangular matrices in broadcast -->
- [x] #1337 <!-- Change `1:size` to `axes` in bidiag mul -->
- [x] #1342 <!-- `Char` uplo in `Bidiagonal` constructor -->
- [x] #1344 <!-- Update the docstring of ldiv! -->
- [x] #1335 <!-- Test: prune old LA based on ENV variable -->
- [x] #1346 <!-- Fix scaling unit triangular matrices -->
- [x] #1355 <!-- Add compat notice for `diagview` -->
- [x] #1349 <!-- Prune `LinearAlgebra` module in ambiguity test -->

Contains multiple commits, manual intervention needed:
- [x] #1238 <!-- Ensure positive-definite matrix in lapack posv test -->
- [x] #1298 <!-- Add `diagm` example -->
- [x] #1312 <!-- WIP: Try use method deletion instead of custom sysimage
-->
- [x] #1333 <!-- Make `fillstored!` public -->
- [x] #1331 <!-- Document SingularException throw for
inv(::AbstractMatrix) -->
- [x] #1350 <!-- Fix copy for partly initialized unit triangular -->

Non-merged PRs with backport label:
- [x] #1352 <!-- log for dense diagonal matrix with negative elements
-->
- [ ] #1305 <!-- Bounds-checking in triangular indexing branches -->

---------

Co-authored-by: Mateus Araújo <[email protected]>
Co-authored-by: Jeff Bezanson <[email protected]>
Co-authored-by: Steven G. Johnson <[email protected]>
Co-authored-by: WalterMadelim <[email protected]>
Co-authored-by: Kristoffer Carlsson <[email protected]>
Co-authored-by: Daniel Karrasch <[email protected]>
Co-authored-by: Michael Abbott <[email protected]>
@jishnub jishnub removed the backport 1.12 Change should be backported to release-1.12 label Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants