-
-
Notifications
You must be signed in to change notification settings - Fork 528
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
Speed up square matrix times vector over GF(2) #37375
Speed up square matrix times vector over GF(2) #37375
Conversation
…rix_times_vector_over_F2' into speed_up_square_matrix_times_vector_over_F2
Documentation preview for this PR (built with commit 86a75ae; changes) is ready! 🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
When doing a matrix-times-vector multiplication over F_2, there is some inefficiency caused by calling `VectorSpace` to create a parent for the result. This becomes noticeable when repeatedly multiplying small matrices. When the dimensions are all the same, this can be improved by using the parent of the vector also as the parent for the result. Before: ``` sage: A = random_matrix(GF(2),10^2,10^2) sage: v0 = vector(random_matrix(GF(2),10^2,1)) sage: %timeit A*v0 2.78 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) ``` After: ``` sage: A = random_matrix(GF(2),10,10) sage: v0 = vector(random_matrix(GF(2),10,1)) sage: %timeit A*v0 995 ns ± 22.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) ``` URL: sagemath#37375 Reported by: kedlaya Reviewer(s): Lorenz Panny
When doing a matrix-times-vector multiplication over F_2, there is some inefficiency caused by calling `VectorSpace` to create a parent for the result. This becomes noticeable when repeatedly multiplying small matrices. When the dimensions are all the same, this can be improved by using the parent of the vector also as the parent for the result. Before: ``` sage: A = random_matrix(GF(2),10^2,10^2) sage: v0 = vector(random_matrix(GF(2),10^2,1)) sage: %timeit A*v0 2.78 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) ``` After: ``` sage: A = random_matrix(GF(2),10,10) sage: v0 = vector(random_matrix(GF(2),10,1)) sage: %timeit A*v0 995 ns ± 22.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) ``` URL: sagemath#37375 Reported by: kedlaya Reviewer(s): Lorenz Panny
Just a note, your timing doesn't show anything, since the dimensions are different (one's with |
Oh! Good catch. But there is still a significant speedup: The reduction in runtime is about |
I fixed the examples to both use 10 by 10 matrices, as indeed this problem is more acute with small matrices. There seems to be a fixed amount of overhead with matrix/vector creation which is rampant throughout Sage; a broader fix for that would obviate the need for such trickery. |
When doing a matrix-times-vector multiplication over F_2, there is some inefficiency caused by calling
VectorSpace
to create a parent for the result. This becomes noticeable when repeatedly multiplying small matrices.When the dimensions are all the same, this can be improved by using the parent of the vector also as the parent for the result. Before:
After: