-
-
Notifications
You must be signed in to change notification settings - Fork 489
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
Prime slicing for matrix multiplication #12177
Comments
This comment has been minimized.
This comment has been minimized.
comment:2
Hi Simon, great that you're getting the ball rolling. Two comments: (a) I think the this code shouldn't be in Sage but on a lower level, preferably LinBox. But perhaps we can write proof of concept in Sage first and then port to C++? (b)I figure we should get an idea about what performance to expect, so: p = 17
K.<a> = GF(p^2)
A = [random_matrix(GF(p),2000,2000) for _ in range(2)]
B = [random_matrix(GF(p),2000,2000) for _ in range(2)]
C = [matrix(GF(p),2000,2000) for _ in range(2)]
def mul2(K, C,A,B):
poly = K.polynomial()
T0 = A[0]*B[0]
C[0] += T0
C[1] -= T0
C[1] += (A[0] + A[1])*(B[0]+B[1])
T0 = A[1]*B[1]
C[1] -= T0
for i,c in enumerate(list(poly)):
if i == 2:
break
C[i] -= c*T0
return C mul2 (I didn't check for bugs) with these inputs takes about 3 seconds on my computer, which was to be expected since a product over GF(p) takes about 1 second on my computer. For comparison Magma is a bit better:
I'm not sure we can do much about it, since the performance essentially depends on the speed of mod-p matrices. |
comment:3
I think we have to do two things: #. Have some code that deals with lists of matrices. I understand that Burcin has such code. I think the second point should actually done not by Karatsuba, but by a modification of Level-n Toom multiplication (where n is the degree of our field extension). See Wikipedia for a method to compute the level-n formula. Ideally, we would also merge the defining polynomial of our field extension into the Toom formula. |
comment:4
Here are some thoughts - I am not sure if this is all correct, but I think it is a starting point: Elements of Assumption: Each polynomial of degree I am not sure, though, whether we can choose any n-tuple of elements of k. First step: Choose n points from k. Evalutation of a generic polynomial of degree n-1 gives rise to n linear combinations of the polynomial's coefficients, described by some invertible square matrix. Let A be the inverse of that matrix. Multiplying two polynomials of degree n modulo the defining polynomial now means: Evaluate the two factors at the given n points. For each point, multiply the two evaluations. Multiply this with A, and read off the coefficients of the product. So, this can be used for our polynomials of matrices. If my arguments work, then we would be down to n multiplications over GF(p) for one multiplication over |
comment:5
Sorry, in my previous post, I totally forgot to include "reduction modulo defining polynomial". Anyway, I am now trying to produce some code. |
Attachment: toom.py.gz proof of concept |
comment:6
I attached a proof of concept that the Toom thingy works. |
comment:7
I attached a patch with template classes implementing this polynomial with matrix coefficients representation. There is also an implementation of the naive multiplication algorithm for GF(pk) to demonstrate FFLAS calls. Timings for the naive multiplication from Martin's example above (comment:2) for p=17 are:
That's n2 coefficient matrix multiplications with some overhead to handle the rollover with the minimal polynomial. We should look at a better algorithm. :) |
comment:8
Replying to @burcin:
... which probably is Toom. Martin, do you have code for Toom multiplication? |
comment:9
I've attached it, but it's going to be slow if you try it because a*A for a in the field there is no special code in Sage yet. |
Attachment: toom_matrix.py.gz Another proof of concept |
comment:10
Martin, in your Toom proof of concept, aren't you evaluating at too few points? We start with polynomials of degree less than the degree of the field extension. But the product will have (before reduction) twice that degree. Hence, instead of
The following lines are to be changed accordingly. We could of course include the reduction to degree l-1 into the matrix |
comment:11
Simon, |
comment:12
Replying to @malb:
Sorry! I was somehow misreading it as |
Attachment: trac_12177-coeff_matrices_template.patch.gz |
comment:13
I refreshed my patch with a version that implements the multi point evaluation approach using FFLAS. It can be reached via the
|
This comment has been minimized.
This comment has been minimized.
comment:15
See #9888 for a related discussion. |
In Sage, matrix arithmetic over finite fields is fast in the following cases:
M4RI
overGF(2)
GF(2^e)
, usingM4RIE
(Add M4RIE to Sage #9562)In all other cases, it sucks. There is the suggestion to use a wrapper of a fork of
C-MeatAxe
(#12103), but this would only work up to field size 255 and might have further disadvantages.Martin Albrecht suggested to use "prime slicing" instead:
GF(p^n)
by a list of n matrices overGF(p)
(with Linbox as backend)GF(p^n)
is implemented by a series of multiplications overGF(p)
On sage-devel, Martin gave a couple of references:
On another occasion, Martin also pointed out how to compute echelon forms in that setting.
The purpose of this ticket is to make that idea real.
TODO
CC: @malb @burcin @robertwb @boothby
Component: linear algebra
Keywords: prime slicing, Karatsuba
Issue created by migration from https://trac.sagemath.org/ticket/12177
The text was updated successfully, but these errors were encountered: