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

Speed up square matrix times vector over GF(2) #37375

Merged

Conversation

kedlaya
Copy link
Contributor

@kedlaya kedlaya commented Feb 16, 2024

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,10)
sage: v0 = vector(random_matrix(GF(2),10,1))
sage: %timeit A*v0
2.26 µs ± 4.57 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
981 ns ± 9.57 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

Copy link

Documentation preview for this PR (built with commit 86a75ae; changes) is ready! 🎉

Copy link
Member

@yyyyx4 yyyyx4 left a 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.

vbraun pushed a commit to vbraun/sage that referenced this pull request Feb 18, 2024
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
vbraun pushed a commit to vbraun/sage that referenced this pull request Feb 19, 2024
    
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
@grhkm21
Copy link
Contributor

grhkm21 commented Feb 21, 2024

Just a note, your timing doesn't show anything, since the dimensions are different (one's with $n = 100$, one is with $n = 10$)

@yyyyx4
Copy link
Member

yyyyx4 commented Feb 21, 2024

Oh! Good catch. But there is still a significant speedup: The reduction in runtime is about $50\,\%$ for $10\times10$ matrices and about $20\,\%$ for $100\times100$ matrices.

@kedlaya
Copy link
Contributor Author

kedlaya commented Feb 21, 2024

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.

@vbraun vbraun merged commit e01a438 into sagemath:develop Feb 25, 2024
16 of 22 checks passed
@mkoeppe mkoeppe added this to the sage-10.3 milestone Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants