Skip to content
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

LinearAlgebra: use band index in structured broadcast #54075

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jishnub
Copy link
Contributor

@jishnub jishnub commented Apr 14, 2024

This adds a new BandIndex type internal to LinearAlgebra that parallels a CartesianIndex, and stores a band index and a linear index of an element along that band. If the index of the band is a compile-time constant (which is often the case with Diagonal/Bidiagonal/Tridiagonal matrices), constant-propagation may eliminate branches in indexing into the matrix, and directly forward the indexing to the corresponding diagonal. This is particularly important in broadcasting for these matrices, which acts band-wise.

An example of an improvement in performance with this PR:

julia> using LinearAlgebra

julia> T = Tridiagonal(rand(893999), rand(894000), rand(893999)); # a large matrix

julia> @btime $T .+ $T;
  5.387 ms (10 allocations: 20.46 MiB) # v"1.12.0-DEV.337"
  2.872 ms (10 allocations: 20.46 MiB) # This PR

julia> @btime $T + $T; # reference
  2.885 ms (10 allocations: 20.46 MiB)

This makes the broadcast operation as fast as the sum, where the latter adds the diagonals directly.

I'm not 100% certain why this works as well as it does, as the constant band index may get lost in the newindex computation. I suspect branch prediction somehow works around this and preserves the constant.

@jishnub jishnub added domain:linear algebra Linear algebra domain:arrays [a, r, r, a, y, s] labels Apr 14, 2024
@jishnub jishnub changed the title LinearAlgebra: use BandIndex in structured broadcast LinearAlgebra: use band index in structured broadcast Apr 14, 2024
@jishnub
Copy link
Contributor Author

jishnub commented May 7, 2024

Gentle bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays [a, r, r, a, y, s] domain:linear algebra Linear algebra
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant