-
Notifications
You must be signed in to change notification settings - Fork 8
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
Slow Hilbert-Schmidt inner product #234
Comments
It's slow because it doesn't get dispatched to MutableArithmetics.jl/src/dispatch.jl Lines 61 to 66 in 6e6a854
but instead to https://github.com/JuliaLang/julia/blob/master/stdlib/LinearAlgebra/src/symmetric.jl#L458 You can do operate(dot, a, b) to be sure to have the fast method called. Having to redirect the methods is a bit tricky because we need to be more specialized than all methods implemented in LinearAlgebra |
Thanks, that does indeed work. And rather ironically In the meanwhile, I was trying to implement a I assume it's rather outdated, as In any case, since we already have the proper method implemented we need to actually use it. What's the difficulty with the dispatch? Can't we just define something like |
If you get that warning, it means you are not dispatched to the right one in MutableArithmetics.
The issue is that the necessary variants is a long list and it changes at every Julia release. Now that the stdlib will be decoupled, it might be more feasible but still. The list is even longer in SparseArrays than LinearAlgebra last time I checked |
Moving this to MutableArithmetics |
I guess we could add the Symmetric case here: MutableArithmetics.jl/src/dispatch.jl Lines 54 to 73 in 6e6a854
|
So we could make the Instead of |
I see, so it's already dispatching correctly when we call About the warning, the problem is that for i=1:n
x += y
end I would get the warning, try to fix it in the recommended way, and fail. |
In LinearAlgebra they simply used a |
Is this a meaningful bottleneck though?
Sure, we can use codegen to simplify things, but there's a cost (in precompilation size and time) to having a large number of methods in MutableArithmetics. |
With
Is this a meaningful bottleneck though? |
Can you give examples of runtime?
MutableArithmetics has >900 dependent packages: https://juliahub.com/ui/Packages/General/MutableArithmetics The increase in loading time affects everyone. Your example might be only a second or two slower for one particular model. |
With the function function timejump(d)
model = JuMP.Model()
a = random_matrix(d)
b = JuMP.@variable(model, [1:d,1:d] in JuMP.PSDCone())
display(@elapsed operate(dot,a,b))
display(@elapsed tr(a*b))
display(@elapsed dot(a,b))
end I get
In the latter case I couldn't even get a time for |
When computing the Hilbert-Schmidt inner product of two matrices
a,b
it is much faster to usedot(a,b)
than the definitiontr(a*b)
, as this avoids a matrix multiplication. I've benchmarked it numerically to confirm, and the difference is orders of magnitude. However, it doesn't work with JuMP variables, for some reasondot(a,b)
is the slower one!I've tried replacing
dot(a,b)
withdot(vec(a),vec(b))
, and it does makes things better. For small dimensions it is still slower thantr(a*b)
, but as we get to dimensions where the speed matters (like 200) it gets orders of magnitude faster. So there is a workaround already, but such a low-level hack shouldn't be necessary,dot
should just do the right thing.Here is the code I used for benchmarking:
The text was updated successfully, but these errors were encountered: