Skip to content

Make rational matrix rref default to flint_multimodular, add suboptions for flint algorithm #39733

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

Merged
merged 16 commits into from
Apr 18, 2025

Conversation

user202729
Copy link
Contributor

@user202729 user202729 commented Mar 18, 2025

Because one of the algorithms used by flint is multimodular, it ought to be faster than the implementation in Python.

At least after we upgrade to a version after flintlib/flint#2129 .

(p/s: if someone uses the old version, the current choice of flint might be slower in some cases, see the linked issue. An alternative which is likely always faster is to explicitly use the multimodular algorithm in flint. Do you think the current implementation is fine, or should we provide an explicit flint_multimodular option instead?)

Fixes #39197

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

#39204

Copy link

github-actions bot commented Mar 18, 2025

Documentation preview for this PR (built with commit 5fa99ec; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

Copy link
Collaborator

@tscrim tscrim left a comment

Choose a reason for hiding this comment

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

How much benchmarking did you (or someone else) do for this?

@user202729
Copy link
Contributor Author

The benchmark I did is just in https://github.com/sagemath/sage/pull/39204/files#diff-6d6c677113f407891fb7d10502e75720ae2a392be8e2b1104502ce99b1b6a342R208 .

The author of flint benchmarked between the two algorithms available in flint to make sure the faster one is chosen in flintlib/flint#2144

I haven't go figure out how to install flint from source (required to test flint with the extra pull request).

A safer way (if you think it's safer?) would be to provide an extra option algorithm="flint_multimodular" in sage (which always use multimodular), then we can benchmark it against sage's multimodular, it should be always faster.

@tscrim
Copy link
Collaborator

tscrim commented Apr 3, 2025

Actually, that's probably a good idea to add that option just to provide it to the user (even without the utility for benchmarking). Could you add that (and the other flint option)?

@user202729
Copy link
Contributor Author

user202729 commented Apr 3, 2025

Alright, done. Since flint_multimodular is reliably faster than multimodular even before the linked pull request, I set the default to that.

(also conveniently hide that implementation detail from the user so that we can change it later without breaking stuff.)

#39204 added as a dependency — not strictly necessary, but convenient because it adds a benchmark which I reuse here to demonstrate benchmark of different algorithms against each other.

flint_multimodular beats multimodular by an order of magnitude though. Even with #39204 .

sage:         sage: benchmark(20, 20, 10000)  # long time
[(0.0008, 'flint_multimodular'),
 (0.0014, 'multimodular'),
 (0.0025, 'padic'),
 (1.3512, 'flint_fflu')]
sage:         sage: benchmark(39, 40, 200)  # long time
[(0.0279, 'flint_multimodular'),
 (0.0681, 'padic'),
 (0.0766, 'flint_fflu'),
 (0.9469, 'multimodular')]

Minor nitpickable points:

  • use flint:multimodular instead of flint_multimodular similar to MPolynomialIdeal.groebner_basis's algorithm parameter?

  • change flint_multimodular and flint_fflu to flint_ff_multimodular and flint_ff_lu?

  • if we do the above, also add flint_ff (automatically choose between the two)?

  • change the links to be the following instead?

    automatically chooses between classical algorithm (Gaussian elimination), fraction-free multimodular, and fraction-free LU decomposition,

  • change multimodular and classical to sage_multimodular and sage_classical? (breaking change!)

@user202729 user202729 changed the title Make rational matrix rref default to flint Make rational matrix rref default to flint_multimodular, add suboptions for flint algorithm Apr 3, 2025
@tscrim
Copy link
Collaborator

tscrim commented Apr 4, 2025

Alright, done. Since flint_multimodular is reliably faster than multimodular even before the linked pull request, I set the default to that.

Thank you.

use flint:multimodular instead of flint_multimodular similar to MPolynomialIdeal.groebner_basis's algorithm parameter?

There is very little precedence for such naming, but considering the ideal code uses the color separator, we probably should follow that unless you have an objection.

change flint_multimodular and flint_fflu to flint_ff_multimodular and flint_ff_lu?

if we do the above, also add flint_ff (automatically choose between the two)?

That's probably overkill on options I think.

change the links to be the following instead?

I have no objections to changing the links, but I am not an expert.

change multimodular and classical to sage_multimodular and sage_classical? (breaking change!)

I probably would leave it alone to avoid the backwards incompatible change. Since we are clearly saying what comes from, e.g., PARI, the lack of the label should indicate it is Sage's own (which will also be in the doc).

@user202729 user202729 mentioned this pull request Apr 4, 2025
5 tasks
vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 7, 2025
sagemathgh-39867: Run long test in test-new
    
sagemath#39641 introduced an issue that
long new tests are not ran in test-new.

This is an issue because then the author is only notified by the failure
when test-long finishes. (e.g. happened to me in
sagemath#39733 .)

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39867
Reported by: user202729
Reviewer(s): Tobias Diez
vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 10, 2025
sagemathgh-39867: Run long test in test-new
    
sagemath#39641 introduced an issue that
long new tests are not ran in test-new.

This is an issue because then the author is only notified by the failure
when test-long finishes. (e.g. happened to me in
sagemath#39733 .)

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39867
Reported by: user202729
Reviewer(s): Tobias Diez
Copy link
Collaborator

@tscrim tscrim left a comment

Choose a reason for hiding this comment

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

Thank you. LGTM.

@tscrim tscrim added s: positive review sd125 sage days 125 and removed s: needs review sd125 sage days 125 labels Apr 11, 2025
vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 13, 2025
sagemathgh-39733: Make rational matrix rref default to flint_multimodular, add suboptions for flint algorithm
    
Because one of the algorithms used by flint is multimodular, it ought to
be faster than the implementation in Python.

At least after we upgrade to a version after
flintlib/flint#2129 .

(p/s: if someone uses the old version, the current choice of flint might
be slower in some cases, see the linked issue. An alternative which is
likely always faster is to explicitly use the multimodular algorithm in
flint. Do you think the current implementation is fine, or should we
provide an explicit `flint_multimodular` option instead?)

Fixes sagemath#39197

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->


sagemath#39204
    
URL: sagemath#39733
Reported by: user202729
Reviewer(s): Travis Scrimshaw
vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 13, 2025
sagemathgh-39867: Run long test in test-new
    
sagemath#39641 introduced an issue that
long new tests are not ran in test-new.

This is an issue because then the author is only notified by the failure
when test-long finishes. (e.g. happened to me in
sagemath#39733 .)

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39867
Reported by: user202729
Reviewer(s): Tobias Diez
vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 18, 2025
sagemathgh-39867: Run long test in test-new
    
sagemath#39641 introduced an issue that
long new tests are not ran in test-new.

This is an issue because then the author is only notified by the failure
when test-long finishes. (e.g. happened to me in
sagemath#39733 .)

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39867
Reported by: user202729
Reviewer(s): Tobias Diez
@vbraun vbraun merged commit 3531a87 into sagemath:develop Apr 18, 2025
26 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suboptimal choice of algorithm by Matrix(QQ).solve_right()
3 participants