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

Dispatch even more to BLAS #33743

Merged
merged 7 commits into from
Nov 11, 2019
Merged

Dispatch even more to BLAS #33743

merged 7 commits into from
Nov 11, 2019

Conversation

dkarrasch
Copy link
Member

@dkarrasch dkarrasch commented Nov 1, 2019

This addresses an issue raised in #29634 (comment), by applying the methodology of #33229 to the entire matmul.jl, (edit: and fixes an issue in the syrk_wrapper!). One drawback, just as in #33229, is that also Bool parameters are promoted to the numeric type of the matrices, which is unnecessary for BLAS.gemv! and BLAS.gemm!. We can think about how to work around that, perhaps by working on the types only and introduce some more if branches, but I guess avoiding the generic fallback in some more cases is already a significant improvement.

@dkarrasch dkarrasch added the domain:linear algebra Linear algebra label Nov 1, 2019
@dkarrasch dkarrasch closed this Nov 1, 2019
@dkarrasch dkarrasch reopened this Nov 1, 2019
@dkarrasch
Copy link
Member Author

Earlier commits were failing a specific test:

@test mul!(C, vf, transpose(vf), 2, 3) == 2vf*vf' .+ 3C0

We are passing it on master by pure luck. 2 and 3 are not of the vectors' eltype, and so the multiplication is performed by the generic_matmatmul. Changing them to 2.0 and 3.0, we start to fail the test, because we pass the task to BLAS.syrk!. The reason is that there is no test of symmetry for C when beta is not zero. So we have two options: switch to generic matmul directly when beta is not zero, or test symmetry. I have currently implemented the latter. The symmetry test is most expensive for symmetric matrices, but for those we benefit the most from saving half of the calculations.

@tkf
Copy link
Member

tkf commented Nov 3, 2019

Maybe you can use 2 + 0im to avoid dispatching into BLAS? I used this hack here:

@testset "Issue #33214: tiled generic mul!" begin
n = 100
A = rand(n, n)
B = rand(n, n)
C = zeros(n, n)
mul!(C, A, B, -1 + 0im, 0)
D = -A * B
@test D C
# Just in case dispatching on the surface API `mul!` is changed in the future,
# let's test the function where the tiled multiplication is defined.
fill!(C, 0)
LinearAlgebra._generic_matmatmul!(C, 'N', 'N', A, B, LinearAlgebra.MulAddMul(-1, 0))
@test D C
end

@dkarrasch
Copy link
Member Author

dkarrasch commented Nov 3, 2019

The test I mentioned does not dispatch to syrk!, because the matrix C (that we're doubling and adding the symmetric stuff to) is not symmetric. I have added a symmetry check now, and if beta is not zero and C not symmetric, we fall back to gemm! generic_matmatmul!. But maybe we should give the gemm_wrapper! another chance...

@tkf
Copy link
Member

tkf commented Nov 3, 2019

Ah, sorry, I misread your earlier comment.

@dkarrasch
Copy link
Member Author

I have extended the coefficient promotion to sym(v/m)! and her(m/v)!. Unfortunately, I did not see how to do something analogous to the gem(v/m)_wrapper! due to the many combinations of arguments.

@dkarrasch
Copy link
Member Author

I think this is ready for review.

@dkarrasch
Copy link
Member Author

BTW, should this be a 1.3 milestone? I guess we wouldn't really want to ship 5-arg mul!, knowing that you can't call it on some Float matrix with alpha and beta equal to some simple Int and end up with BLAS.

@dkarrasch dkarrasch added the performance Must go faster label Nov 8, 2019
@dkarrasch
Copy link
Member Author

Gentle bump.

@tkf
Copy link
Member

tkf commented Nov 11, 2019

FWIW, it looks good to me. It's a performance bug fix that does not change the API at all so it would be nice if it can be backported to 1.3 (especially because it's still in RC).

@ViralBShah
Copy link
Member

The failing tests appear not to be relevant. Is this otherwise good to merge? Do we need reviews from anyone else?

@dkarrasch
Copy link
Member Author

Unless there are requests for changes, I think this is good to merge.

@ViralBShah ViralBShah merged commit 1ed96b2 into JuliaLang:master Nov 11, 2019
@tkf
Copy link
Member

tkf commented Nov 11, 2019

How about putting the backport label, so that @KristofferC can check if it's OK for backport?

@dkarrasch dkarrasch deleted the moregemm branch November 11, 2019 16:21
@dkarrasch
Copy link
Member Author

I added the backport label. Let's see...

@ViralBShah
Copy link
Member

Should certainly be 1.3.1 or later.

@Jutho
Copy link
Contributor

Jutho commented Nov 12, 2019

Thanks @dkarrasch !

@tkf
Copy link
Member

tkf commented Nov 13, 2019

This PR probably introduced recent test failures: #32924 (comment)

@dkarrasch
Copy link
Member Author

Wherever this goes (in terms of backport), #33843 must go with it.

KristofferC pushed a commit that referenced this pull request Nov 15, 2019
* get MulAddMul out of the BLAS way, promote alpha/beta coeffs

* fix ambiguity

* simplify promotion, :crossedfingers:

* remove promote_unless_bool, add symmetry check `syrk_wrapper!` (bugfix)

* give BLAS another chance

* extend coefficient promotion to sym(v/m)! and hem(v/m)!

* fix typo

(cherry picked from commit 1ed96b2)
@KristofferC KristofferC mentioned this pull request Nov 15, 2019
19 tasks
KristofferC pushed a commit that referenced this pull request Nov 29, 2019
* get MulAddMul out of the BLAS way, promote alpha/beta coeffs

* fix ambiguity

* simplify promotion, :crossedfingers:

* remove promote_unless_bool, add symmetry check `syrk_wrapper!` (bugfix)

* give BLAS another chance

* extend coefficient promotion to sym(v/m)! and hem(v/m)!

* fix typo

(cherry picked from commit 1ed96b2)
@KristofferC KristofferC mentioned this pull request Nov 29, 2019
18 tasks
KristofferC pushed a commit that referenced this pull request Apr 11, 2020
* get MulAddMul out of the BLAS way, promote alpha/beta coeffs

* fix ambiguity

* simplify promotion, :crossedfingers:

* remove promote_unless_bool, add symmetry check `syrk_wrapper!` (bugfix)

* give BLAS another chance

* extend coefficient promotion to sym(v/m)! and hem(v/m)!

* fix typo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:linear algebra Linear algebra performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants