MichielStock / Kronecker.jl

A general-purpose toolbox for efficient Kronecker-based algebra.
MIT License
86 stars 14 forks source link

Optimized addition, subtraction and negation #111

Closed jishnub closed 2 years ago

jishnub commented 2 years ago

This PR reduces some temporary allocations in addition and subtraction, and optimizes unary negation for KroneckerProduct.

Master:

julia> A = rand(30,30) ⊗ rand(40, 40);

julia> B = rand(40,40) ⊗ rand(30,30);

julia> C = rand(40,40) ⊗ Diagonal(rand(30));

julia> @btime $A + $B;
  5.653 ms (6 allocations: 32.96 MiB)

julia> @btime $A - $B;
  5.836 ms (6 allocations: 32.96 MiB)

julia> @btime -$A;
  2.055 ms (2 allocations: 10.99 MiB)

julia> @btime -$C;
  3.896 ms (2 allocations: 10.99 MiB)

This PR:

julia> @btime $A + $B;
  4.846 ms (4 allocations: 21.97 MiB)

julia> @btime $A - $B;
  4.920 ms (4 allocations: 21.97 MiB)

julia> @btime -$A; # lazy kronecker product
  1.086 μs (1 allocation: 7.19 KiB)

julia> @btime -$C; # lazy kronecker product
  84.321 ns (1 allocation: 304 bytes)

Sorry about the whitespace changes, it's an automated VSCode setting that I've not figured out how to disable. The main changes are lines 411-494 in src/base.jl, and an added lastmatrix method in kroneckerpowers.jl on line 44

codecov-commenter commented 2 years ago

Codecov Report

Merging #111 (22756ec) into master (70fc882) will increase coverage by 0.42%. The diff coverage is 91.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #111      +/-   ##
==========================================
+ Coverage   90.43%   90.86%   +0.42%     
==========================================
  Files          10       10              
  Lines         805      810       +5     
==========================================
+ Hits          728      736       +8     
+ Misses         77       74       -3     
Impacted Files Coverage Δ
src/factorization.jl 58.82% <33.33%> (-3.68%) :arrow_down:
src/indexedkroncker.jl 76.47% <66.66%> (-0.81%) :arrow_down:
src/eigen.jl 86.66% <75.00%> (ø)
src/kroneckersum.jl 83.90% <93.33%> (-0.19%) :arrow_down:
src/base.jl 94.36% <97.14%> (+1.77%) :arrow_up:
src/kroneckergraphs.jl 100.00% <100.00%> (ø)
src/kroneckerpowers.jl 89.83% <100.00%> (+0.17%) :arrow_up:
src/multiply.jl 100.00% <100.00%> (ø)
src/names.jl 100.00% <100.00%> (ø)
src/vectrick.jl 94.53% <100.00%> (-0.29%) :arrow_down:
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 70fc882...22756ec. Read the comment docs.

MichielStock commented 2 years ago

Looks good and the speedups are even more impressive on my computer.

I don't think the whitespace changes are too bad. If you want, you might even open the other files of the source code so they are consistent?

jishnub commented 2 years ago

I've updated all files now. If this is merged first, I'll rebase the other branches on master so that they are easier to review