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

5-arg mul gives wrong result #33214

Closed
lkapelevich opened this issue Sep 10, 2019 · 9 comments · Fixed by #33218
Closed

5-arg mul gives wrong result #33214

lkapelevich opened this issue Sep 10, 2019 · 9 comments · Fixed by #33218
Labels
domain:linear algebra Linear algebra kind:bug Indicates an unexpected problem or unintended behavior

Comments

@lkapelevich
Copy link
Contributor

lkapelevich commented Sep 10, 2019

MWE:

using LinearAlgebra, Random

Random.seed!(1)
n = 100
A = rand(n, n)
B = rand(n, n)
C = zeros(n, n)
mul!(C, B, A, -1, 0)
D = -B * A
@show norm(D - C) # 2534.227986442133

This also happens for sparse arrays.

@JeffBezanson JeffBezanson added kind:bug Indicates an unexpected problem or unintended behavior domain:linear algebra Linear algebra labels Sep 10, 2019
@chriscoey
Copy link

@tkf this occurs on Julia 1.4 master, for a variety of matrix types. Any idea why?

@tkf
Copy link
Member

tkf commented Sep 11, 2019

Thanks for the heads up. It turned out to be a stupid mistake (alpha and beta swapped). #33218 should fix it.

@lkapelevich lkapelevich changed the title Passing integer scalars in 5-arg mul gives wrong result 5-arg mul gives wrong result Sep 11, 2019
@lkapelevich
Copy link
Contributor Author

lkapelevich commented Sep 11, 2019

I only just hit another problem where alpha and beta were both floats and the output was incorrect- makes sense that it's not an issue with integers :-). In case it's useful to test,

using Random, SparseArrays, LinearAlgebra
Random.seed!(1)
n = 100;
A = Symmetric(rand(n, n))
B = sparse(rand(n, n))
C = zeros(n, n)
mul!(C, B, A, -1.0, 0.0)
D = -B * A
@show norm(D - C) # 2543.524362254463

The error is not triggered without the Symmetric or without the sparse.

@tkf
Copy link
Member

tkf commented Sep 11, 2019

This also happens for sparse arrays.

I forgot to address this part. @lkapelevich Can you give me an example? Is the snippet you just posted the same as what you mentioned in the OP?

@tkf
Copy link
Member

tkf commented Sep 11, 2019

#33214 (comment) dispatches to the same method I fixed in #33218

@lkapelevich
Copy link
Contributor Author

lkapelevich commented Sep 11, 2019

What I meant in the description was that I was also getting incorrect results when A and B were sparse, e.g.

Random.seed!(1)
n = 100
A = sparse(rand(n, n))
B = sparse(rand(n, n))
C = spzeros(n, n)
mul!(C, B, A, -1, 0)
D = -B * A
@show norm(D - C) # 2534.227986442133

@lkapelevich
Copy link
Contributor Author

lkapelevich commented Sep 11, 2019

(I didn't notice that alpha and beta need not be integers)

@tkf
Copy link
Member

tkf commented Sep 11, 2019

Thanks. Fortunately, the new snippet (#33214 (comment)) is also handled by #33218, too (which is a bit surprising; I guess dispatch plumbing can be improved a lot...).

@fredrikekre
Copy link
Member

fredrikekre commented Sep 11, 2019

Why did this hit generic_matmul at all? Seems like a huge performance trap if you just forget to use 1.0 and 0.0 instead of 1 and 0?

julia> @btime mul!($C, $B, $A, -1, 0);
  835.482 μs (69 allocations: 3.56 KiB)

julia> @btime mul!($C, $B, $A, -1.0, 0.0);
  52.149 μs (0 allocations: 0 bytes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:linear algebra Linear algebra kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants