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

Use lazy attribute for _Karatsuba_threshold #24945

Closed
jdemeyer opened this issue Mar 11, 2018 · 21 comments
Closed

Use lazy attribute for _Karatsuba_threshold #24945

jdemeyer opened this issue Mar 11, 2018 · 21 comments

Comments

@jdemeyer
Copy link

This avoids a nasty import at startup of matrix.matrix_space in polynomial rings.

I'm adding a dependency on #24947 which is technically not needed, but it makes it easier to merge things in #24742.

Depends on #24947

Component: misc

Author: Jeroen Demeyer

Branch/Commit: f14395c

Reviewer: Marc Mezzarobba

Issue created by migration from https://trac.sagemath.org/ticket/24945

@jdemeyer jdemeyer added this to the sage-8.2 milestone Mar 11, 2018
@jdemeyer
Copy link
Author

@jdemeyer
Copy link
Author

New commits:

0b4bdd8Avoid importing matrices in polynomial rings

@jdemeyer
Copy link
Author

Commit: 0b4bdd8

@mezzarobba
Copy link
Member

comment:3

LGTM, but what about making the (default, since there is a method to override it...) threshold an attribute of the base ring instead?

@videlec
Copy link
Contributor

videlec commented Mar 11, 2018

comment:4

Why is there a need for a try/except here!?

@jdemeyer
Copy link
Author

comment:5

Replying to @videlec:

Why is there a need for a try/except here!?

Because this is happening at Sage startup time. So sage.all.MatrixSpace may not exist yet.

@videlec
Copy link
Contributor

videlec commented Mar 11, 2018

comment:6

Replying to @jdemeyer:

Replying to @videlec:

Why is there a need for a try/except here!?

Because this is happening at Sage startup time. So sage.all.MatrixSpace may not exist yet.

This is very bad. I am very much in favor of something like Marc suggested

try:
   threshold = base_ring._karatsuba_threshold
except AttributeError:
    threshold = 0

that would not depend on some startup effect.

@jdemeyer
Copy link
Author

comment:7

Hmm, adding that to all possible base rings seems strange. I would much rather do it depending on the element class. For example, generic polynomials would not use Karatsuba, but polynomials over ZZ would.

@jdemeyer
Copy link
Author

comment:8

I'm thinking of something like

        # element class determines default Karatsuba threshold
        self._Karatsuba_threshold = self.Element._default_karatsuba_threshold(base_ring)

@mezzarobba
Copy link
Member

comment:9

Replying to @jdemeyer:

Hmm, adding that to all possible base rings seems strange. I would much rather do it depending on the element class. For example, generic polynomials would not use Karatsuba, but polynomials over ZZ would.

As far as I understand, the goal is not to decide whether to use Karatsuba, but when the generic multiplication code should switch from the schoolbook algorithm to Karatsuba. This depends on the base ring (or its element class): basically, the more expensive multiplication is compared to addition, the lower the threshold should be. And yes, I guess most base rings wouldn't need to override the default value.

@jdemeyer
Copy link
Author

comment:10

But we have really a lot of possible base rings in Sage. Should they all get a _default_karatsuba_threshold() method?

@jdemeyer
Copy link
Author

comment:11

A different solution is to use a lazy_attribute instead for _Karatsuba_threshold. That might solve the startup problem.

@mezzarobba
Copy link
Member

comment:12

Replying to @jdemeyer:

But we have really a lot of possible base rings in Sage. Should they all get a _default_karatsuba_threshold() method?

I don't know. Would it be a problem to have such a method (or a simple attribute) either in the category of rings or even in Parent?

@jdemeyer

This comment has been minimized.

@jdemeyer
Copy link
Author

Dependencies: #24947

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 15, 2018

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

447b4e5Properly fix signature of Matrix_gfpn_dense.__init__
f14395cUse a lazy_attribute for _Karatsuba_threshold

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 15, 2018

Changed commit from 0b4bdd8 to f14395c

@jdemeyer
Copy link
Author

comment:15

I went with the simple solution of using a lazy_attribute.

@jdemeyer

This comment has been minimized.

@jdemeyer jdemeyer changed the title Avoid importing matrices in polynomial rings Use lazy attribute for _Karatsuba_threshold Mar 15, 2018
@mezzarobba
Copy link
Member

Reviewer: Marc Mezzarobba

@vbraun
Copy link
Member

vbraun commented Mar 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants