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

Specialize Diagonal * Matrix * Diagonal #46400

Merged
merged 5 commits into from
Aug 24, 2022

Conversation

chengchingwen
Copy link
Member

This is a fast path for a 3 argument matrix multiplication where the first and third matrix is a Diagonal. This 3 arg matmul is commonly used in graph theory (e.g. computing graph laplacian, cc @yuehhua). With this fast path we get a performance boost that is about 2 times faster.

using LinearAlgebra, BenchmarkTools

A = randn(2000, 2000);
D = Diagonal(randn(2000));
D2 = Diagonal(randn(2000));

# on master
julia> @btime $D * $A * $D2;
  8.501 ms (4 allocations: 61.04 MiB)

# with this PR
julia> @btime $D * $A * $D2;
  4.151 ms (6 allocations: 30.52 MiB)

@oscardssmith oscardssmith added performance Must go faster linear algebra Linear algebra labels Aug 18, 2022
@N5N3 N5N3 added the needs tests Unit tests are required for this change label Aug 18, 2022
@chengchingwen
Copy link
Member Author

@N5N3 what kinds of test do we need? Is @test Da * A * Db ≈ (Da * A) * Db enough?

@N5N3
Copy link
Member

N5N3 commented Aug 18, 2022

That's enough. The correctness is intuitive here. The tag was added only for coverage.

@N5N3 N5N3 removed the needs tests Unit tests are required for this change label Aug 18, 2022
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.

I'd like to suggest two more tests to specifically target the size checks. Otherwise this LGTM. I don't think Number-transpose-like optimizations are needed.

stdlib/LinearAlgebra/test/diagonal.jl Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants