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

Offset matrix multiplication via generic_matmatmul! #270

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

Conversation

mcabbott
Copy link

@mcabbott mcabbott commented Dec 26, 2021

This is meant to work with JuliaLang/julia#43552, although I think mul! might work without that.

Alternative to #146. Since this does not overload *, I think it should not encounter the endless method ambiguities that tends to cause, against Adjoint matrices and other types. In fact I think other packages could overload generic_matmul! too in the same way, and each unwrap nicely, so long as they all only dispatch on the output C, which is created by similar.

This seems to cause one extra allocation and hence is slower than without offsets. I'm not so sure why, I thought MulAddMul(α,β) (whose type depends on the values of α,β) was the culprit, but in fact get similar times after avoiding that:

julia> for N in [3, 10, 100, 1000]
           @show N
           A, B, x = rand(N,N), rand(N,N), rand(N)
           @btime $A * $B
           A, B, x = OffsetArray(A,5,5), OffsetArray(B,5,5), OffsetArray(x,5)
           @btime $A * $B
       end
N = 3
  min 31.407 ns, mean 33.633 ns (1 allocation, 128 bytes)
  min 52.950 ns, mean 56.294 ns (2 allocations, 144 bytes)
N = 10
  min 180.901 ns, mean 200.923 ns (1 allocation, 896 bytes)
  min 209.057 ns, mean 225.422 ns (2 allocations, 912 bytes)
N = 100
  min 9.625 μs, mean 15.880 μs (2 allocations, 78.17 KiB)
  min 9.583 μs, mean 15.396 μs (3 allocations, 78.19 KiB)
N = 1000
  min 7.685 ms, mean 8.350 ms (2 allocations, 7.63 MiB)
  min 7.756 ms, mean 8.336 ms (3 allocations, 7.63 MiB)

Discussed briefly in this long discourse thread.

Comment on lines 73 to 79
if tA == 'N'
if tB == 'N'
mul!(C1, A1, B1, alpha, beta)
elseif tB == 'T'
mul!(C1, A1, transpose(B1), alpha, beta)
elseif tB == 'C'
mul!(C1, A1, adjoint(B1), alpha, beta)
Copy link
Author

Choose a reason for hiding this comment

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

A bigger change to LinearAlgebra.generic_matmatmul! would be to make it keep adjoint longer, before introducing 'C', etc. Then this nest of conditions could be removed.

It seems an odd design that MulAddMul pushes α,β into the type domain (partly) at the same time that it moves transpose/adjoint to values from types. Perhaps JuliaLang/julia#43552 could fix both at the same time.

Copy link
Author

Choose a reason for hiding this comment

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

816b928 (and JuliaLang/julia@aad0522) changes this. It removes the extra allocation above.

The downside is that these methods will not be called for mul! with a version of Julia older than JuliaLang/julia#43552 .

@codecov
Copy link

codecov bot commented Dec 28, 2021

Codecov Report

Merging #270 (816b928) into master (54ade62) will decrease coverage by 7.58%.
The diff coverage is 3.57%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #270      +/-   ##
==========================================
- Coverage   96.52%   88.93%   -7.59%     
==========================================
  Files           5        6       +1     
  Lines         460      488      +28     
==========================================
- Hits          444      434      -10     
- Misses         16       54      +38     
Impacted Files Coverage Δ
src/OffsetArrays.jl 95.54% <0.00%> (-2.87%) ⬇️
src/linearalgebra.jl 3.70% <3.70%> (ø)
src/axes.jl 97.46% <0.00%> (-2.54%) ⬇️
src/utils.jl 96.00% <0.00%> (-2.00%) ⬇️

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 54ade62...816b928. Read the comment docs.

@mcabbott mcabbott changed the title Allow some linear algebra Offset matrix multiplication via generic_matmatmul! Dec 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant