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

Make SwapMatrixColumns and SwapMatrixRows faster for classic matrices (lists-of-lists) #4951

Merged

Conversation

fingolfin
Copy link
Member

Yet another commit from an unfinished branch/PR that might be considered useful on its own: faster "early methods" for SwapRows/SwapCols using kernel methods. It is not tested thoroughly and I am not even sure we want to go in this direction, but we could e.g. use this as basis for discussions during the GAP Days


This speeds up swap operations for plain list matrices a lot, and
even for "real" matrix objs, as the overhead of a GAP early method
is much higher than the overhead of one written in the kernel.

Now whether this gain is worth the extra complexity is another question;
though of course when doing heavy linear algebra, these row and column
can add up.

Before:

gap> mat:=IdentityMat(10,Integers);;
gap> for i in [1..1000000] do SwapMatrixColumns(mat,1,2); od; time;
326
gap> for i in [1..1000000] do SwapMatrixRows(mat,1,2); od; time;
90
gap> for i in [1..1000000] do tmp:=mat[1];mat[1]:=mat[2];mat[2]:=tmp; od; time;
37

gap> mat:=IdentityMat(10,GF(2));; ConvertToMatrixRep(mat);; mat;
<a 10x10 matrix over GF2>
gap> for i in [1..1000000] do SwapMatrixRows(mat,1,2); od; time;
146
gap> for i in [1..1000000] do tmp:=mat[1];mat[1]:=mat[2];mat[2]:=tmp; od; time;
84

After:

gap> mat:=IdentityMat(10,Integers);;
gap> for i in [1..1000000] do SwapMatrixColumns(mat,1,2); od; time;
153
gap> for i in [1..1000000] do SwapMatrixRows(mat,1,2); od; time;
19
gap> for i in [1..1000000] do tmp:=mat[1];mat[1]:=mat[2];mat[2]:=tmp; od; time;
45

gap> mat:=IdentityMat(10,GF(2));; ConvertToMatrixRep(mat);; mat;
<a 10x10 matrix over GF2>
gap> for i in [1..1000000] do SwapMatrixRows(mat,1,2); od; time;
116
gap> for i in [1..1000000] do tmp:=mat[1];mat[1]:=mat[2];mat[2]:=tmp; od; time;
83

@fingolfin fingolfin force-pushed the mh/matrix-manipulation-kernel branch from 390517c to 0715dd9 Compare November 8, 2024 01:51
@fingolfin fingolfin marked this pull request as ready for review November 9, 2024 10:42
@fingolfin
Copy link
Member Author

The work is done and I see no harm. The idea of course is that ideally code switching to SwapMatrixRows should be penalized as little as possible for it, or even better, should benefit from it. This is clearly the case for the plist matrix. For the GF2 rep it still is a little bit slower but that could be mitigated further in the future if desired.

So I see no harm in merging this. Agreed @ThomasBreuer ?

@fingolfin fingolfin added kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements topic: performance bugs or enhancements related to performance (improvements or regressions) labels Nov 9, 2024
@fingolfin fingolfin changed the title kernel: add some kernel dispatchers for matrix manipulation Make SwapMatrixColumns and SwapMatrixRows faster for classic matrices (lists-of-lists) Nov 9, 2024
@fingolfin fingolfin added the release notes: use title For PRs: the title of this PR is suitable for direct use in the release notes label Nov 9, 2024
Copy link
Contributor

@ThomasBreuer ThomasBreuer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem with the idea and with the use of the kernel operation for matrices without holes.
I do not understand the explicit error cases in FuncSWAP_MAT_ROWS. Apparently the code deals also with holes inside the lists:

gap> l:= [ 1, 2,, 4 ];; SWAP_MAT_ROWS( l, 2, 3 ); l;
[ 1,, 2, 4 ]

(If the hole is beyond the length of the list, the result is different.)

src/lists.c Outdated Show resolved Hide resolved
src/lists.c Show resolved Hide resolved
src/lists.c Show resolved Hide resolved
This speeds up swap operations for plain list matrices a lot, and
even for "real" matrix objs, as the overhead of a GAP early method
is much higher than the overhead of one written in the kernel.

Now whether this gain is worth the extra complexity is another question;
though of course when doing heavy linear algebra, these row and column
can add up.

Before:

    gap> mat:=IdentityMat(10,Integers);;
    gap> for i in [1..1000000] do SwapMatrixColumns(mat,1,2); od; time;
    326
    gap> for i in [1..1000000] do SwapMatrixRows(mat,1,2); od; time;
    90
    gap> for i in [1..1000000] do tmp:=mat[1];mat[1]:=mat[2];mat[2]:=tmp; od; time;
    37

    gap> mat:=IdentityMat(10,GF(2));; ConvertToMatrixRep(mat);; mat;
    <a 10x10 matrix over GF2>
    gap> for i in [1..1000000] do SwapMatrixRows(mat,1,2); od; time;
    146
    gap> for i in [1..1000000] do tmp:=mat[1];mat[1]:=mat[2];mat[2]:=tmp; od; time;
    84

After:

    gap> mat:=IdentityMat(10,Integers);;
    gap> for i in [1..1000000] do SwapMatrixColumns(mat,1,2); od; time;
    153
    gap> for i in [1..1000000] do SwapMatrixRows(mat,1,2); od; time;
    19
    gap> for i in [1..1000000] do tmp:=mat[1];mat[1]:=mat[2];mat[2]:=tmp; od; time;
    45

    gap> mat:=IdentityMat(10,GF(2));; ConvertToMatrixRep(mat);; mat;
    <a 10x10 matrix over GF2>
    gap> for i in [1..1000000] do SwapMatrixRows(mat,1,2); od; time;
    116
    gap> for i in [1..1000000] do tmp:=mat[1];mat[1]:=mat[2];mat[2]:=tmp; od; time;
    83
@fingolfin
Copy link
Member Author

Revised. Now we get

gap> l:= [ 1, 2,, 4 ];; SWAP_MAT_ROWS( l, 2, 3 ); l;
Error, Matrix Element: <mat>[3] must have an assigned value
not in any function at *stdin*:1
type 'quit;' to quit to outer loop
brk>

and the error handling should be consistent overall.

@fingolfin fingolfin merged commit e234def into gap-system:master Nov 11, 2024
33 checks passed
@fingolfin fingolfin deleted the mh/matrix-manipulation-kernel branch November 11, 2024 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements release notes: use title For PRs: the title of this PR is suitable for direct use in the release notes topic: kernel topic: performance bugs or enhancements related to performance (improvements or regressions)
Projects
No open projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants