Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

Optionally using GMP to implement long if available

 

 

First page Previous page 1 2 Next page Last page  View All Python dev RSS feed   Index | Next | Previous | View Threaded


paul.w.miller at wmich

Nov 3, 2008, 9:56 AM

Post #1 of 33 (1565 views)
Permalink
Optionally using GMP to implement long if available

I've read some of the past discussion about including GMP into the
python core and understand the reasons for not doing so. Rather than
that, what about patching Python's long implementation to use GMP if
it's available, and the default implementation if not? Are there any
philosophical or technical objections to this? If not, I would consider
drafting a patch.

Thanks,

Paul

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


musiccomposition at gmail

Nov 3, 2008, 11:19 AM

Post #2 of 33 (1516 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

On Mon, Nov 3, 2008 at 11:56 AM, Paul Miller <paul.w.miller [at] wmich> wrote:
> I've read some of the past discussion about including GMP into the
> python core and understand the reasons for not doing so. Rather than
> that, what about patching Python's long implementation to use GMP if
> it's available, and the default implementation if not? Are there any
> philosophical or technical objections to this? If not, I would consider
> drafting a patch.

The main objection is that GMP is licensed under LGPL which I believe
conflicts with Python's very open license. Also, there would
incompatibilities between versions of Python that had GMP enabled and
those that didn't with regards to floating points.

>
> Thanks,
>
> Paul




--
Cheers,
Benjamin Peterson
"There's nothing quite as beautiful as an oboe... except a chicken
stuck in a vacuum cleaner."
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


victor.stinner at haypocalc

Nov 3, 2008, 12:41 PM

Post #3 of 33 (1527 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

Hi,

Le Monday 03 November 2008 18:56:37 Paul Miller, vous avez écrit :
> I've read some of the past discussion about including GMP into the
> python core and understand the reasons for not doing so.

Please, check this issue: http://bugs.python.org/issue1814

I patched Python3 to use GMP because I thaugh that GMP is much faster than
Python for integer computation. The "problem" is that GMP target are integers
much bigger than 10**100. As I remember, GMP has a special hack for very
small integer: integer which can be stored in one CPU word, but it's not
enough to speed up Python.

With my last patch, with GMP, Python is 2% *slower*.

My patch can be improved, but I expected +20% to +100% to no -2% :-/

And there is also the license issue...

--
Victor Stinner aka haypo
http://www.haypocalc.com/blog/
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


skip at pobox

Nov 3, 2008, 12:48 PM

Post #4 of 33 (1517 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

Benjamin> The main objection is that GMP is licensed under LGPL which I
Benjamin> believe conflicts with Python's very open license.

If GMP itself isn't included with Python how can there be a licensing issue?

Skip
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


martin at v

Nov 3, 2008, 1:56 PM

Post #5 of 33 (1512 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

skip [at] pobox wrote:
> Benjamin> The main objection is that GMP is licensed under LGPL which I
> Benjamin> believe conflicts with Python's very open license.
>
> If GMP itself isn't included with Python how can there be a licensing issue?

On Windows, the GMP binaries would be incorporated into pythonxy.dll.
This would force anybody providing a copy of pythonxy.dll to also
provide the sources of GMP.

Regards,
Martin
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


greg at krypto

Nov 3, 2008, 2:26 PM

Post #6 of 33 (1518 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

On Mon, Nov 3, 2008 at 12:41 PM, Victor Stinner
<victor.stinner [at] haypocalc> wrote:
> Hi,
>
> Le Monday 03 November 2008 18:56:37 Paul Miller, vous avez écrit :
>> I've read some of the past discussion about including GMP into the
>> python core and understand the reasons for not doing so.
>
> Please, check this issue: http://bugs.python.org/issue1814
>
> I patched Python3 to use GMP because I thaugh that GMP is much faster than
> Python for integer computation. The "problem" is that GMP target are integers
> much bigger than 10**100. As I remember, GMP has a special hack for very
> small integer: integer which can be stored in one CPU word, but it's not
> enough to speed up Python.
>
> With my last patch, with GMP, Python is 2% *slower*.
>
> My patch can be improved, but I expected +20% to +100% to no -2% :-/

One optimization that could be done to the existing Python longobject
code is to allow it to use larger digits. Currently it is hardcoded
to use 15bit digits.

The most common desktop+server CPUs in the world (x86) all support
efficient 32bit*32bit -> 64bit multiply so there is no good reason to
limit python itself to 15bit digits when on either x86 or x86_64. 30
or 31bit digits would be more efficient as more numbers would then be
a single digit shortcutting their computations and the number of
digits for others would be half.

The ability to use 15bit digits is good for other cpus but is wasteful
when the hardware supports more (32bit x86 and all 64bit archs).

>
> And there is also the license issue...
>
> --
> Victor Stinner aka haypo
> http://www.haypocalc.com/blog/
> _______________________________________________
> Python-Dev mailing list
> Python-Dev [at] python
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/greg%40krypto.org
>
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


martin at v

Nov 3, 2008, 3:20 PM

Post #7 of 33 (1520 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

> One optimization that could be done to the existing Python longobject
> code is to allow it to use larger digits. Currently it is hardcoded
> to use 15bit digits.
>
> The most common desktop+server CPUs in the world (x86) all support
> efficient 32bit*32bit -> 64bit multiply so there is no good reason to
> limit python itself to 15bit digits when on either x86 or x86_64.

Perhaps Tim Peters should also comment here - but if you can come up
with a patch that does that in a portable way, I would be in favor.
The challenge, ISTM, is to make it still compile on all systems
(regardless of how inefficient it might be on minor platforms).

Regards,
Martin
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


skip at pobox

Nov 3, 2008, 4:37 PM

Post #8 of 33 (1516 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

Benjamin> The main objection is that GMP is licensed under LGPL which I
Benjamin> believe conflicts with Python's very open license.

>> If GMP itself isn't included with Python how can there be a licensing
>> issue?

Martin> On Windows, the GMP binaries would be incorporated into
Martin> pythonxy.dll. This would force anybody providing a copy of
Martin> pythonxy.dll to also provide the sources of GMP.

As I understand it the proposal was to allow people to substitute GMP for
Python's long implementation. Just deliver binaries with the Python long
version if you don't want to distribute the GMP source. OTOH, it should be
no big deal to drop a zip archive of the GMP sources which correspond to the
code bound into the DLL. OTOOH, doesn't Windows support dynamic linking?
Can't pythonxy.dll dynamically link to a gmpMN.dll?

Skip
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


thomas at python

Nov 3, 2008, 5:03 PM

Post #9 of 33 (1517 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

On Tue, Nov 4, 2008 at 01:37, <skip [at] pobox> wrote:

>
> Benjamin> The main objection is that GMP is licensed under LGPL which I
> Benjamin> believe conflicts with Python's very open license.
>
> >> If GMP itself isn't included with Python how can there be a licensing
> >> issue?
>
> Martin> On Windows, the GMP binaries would be incorporated into
> Martin> pythonxy.dll. This would force anybody providing a copy of
> Martin> pythonxy.dll to also provide the sources of GMP.
>
> As I understand it the proposal was to allow people to substitute GMP for
> Python's long implementation. Just deliver binaries with the Python long
> version if you don't want to distribute the GMP source. OTOH, it should be
> no big deal to drop a zip archive of the GMP sources which correspond to
> the
> code bound into the DLL. OTOOH, doesn't Windows support dynamic linking?
> Can't pythonxy.dll dynamically link to a gmpMN.dll?
>

Neither of those (shipping sources or dynamically linking to GMP) would
solve the LGPL issue. People who distribute that build of Python would still
be held by the LGPL -- such as shipping any sources that they embed that
Python into.

--
Thomas Wouters <thomas [at] python>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!


stephen at xemacs

Nov 3, 2008, 6:07 PM

Post #10 of 33 (1520 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

Thomas Wouters writes:

> Neither of those (shipping sources or dynamically linking to GMP) would
> solve the LGPL issue. People who distribute that build of Python would still
> be held by the LGPL -- such as shipping any sources that they embed that
> Python into.

No, that's exactly what the "L" in "LGPL" means. You only have to
ship the sources to any object module (library or program) actually
containing GMP code, including any modifications you make to them. So
if GMP is kept entirely in a separate library and you don't borrow any
code from it, the rest of your code is not affected by the LGPL.

This includes any marshalling code that does nothing but load the GMP
library and connect it up to Python's internal API; that need not be
LGPL unless you add it to the GMP library.

The "problem" with the LGPL is the administrative burden of tracking
which sources need to be distributed under what conditions, not that
it insinuates itself into your own code.

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


tim.peters at gmail

Nov 3, 2008, 6:37 PM

Post #11 of 33 (1518 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

[Gregory P. Smith]
>> One optimization that could be done to the existing Python longobject
>> code is to allow it to use larger digits. Currently it is hardcoded
>> to use 15bit digits.
>>
>> The most common desktop+server CPUs in the world (x86) all support
>> efficient 32bit*32bit -> 64bit multiply so there is no good reason to
>> limit python itself to 15bit digits when on either x86 or x86_64.

[Martin v. Löwis]
> Perhaps Tim Peters should also comment here - but if you can come up
> with a patch that does that in a portable way, I would be in favor.
> The challenge, ISTM, is to make it still compile on all systems
> (regardless of how inefficient it might be on minor platforms).

Eh -- the strong points of Python's long implementation have always
been reliability and portability. Both are greatly aided by sticking
to operations spellable in standard C89, and by avoiding
platform-specific trickery & #ifdef'ery. So, no, I'm not keen on
this. Note that while 32x32->64 multiply is supported by x86 HW, that
doesn't mean there's a uniform way to get at this HW capability across
C compilers. In contrast, (at least) efficient HW 15x15->30 multiply
is universally spelled in C via "i*j" :-)

A similar gripe applies to schemes to replace the long implementation
by GMP (optionally or not): it adds complexity to the code. I like
GMP myself, but am happy using one of the Python GMP wrappers when I
/want/ GMP (as timings in other messages show, using GMP is a speed
loser before ints grow "really big").

Indeed, if I had it to do over again, I would balk even at adding
Karatsuba multiplication to Python (it added extra complexity with no
real payback, given that those who care about the speed of very long
int multiplication are far better off using a GMP wrapper anyway).

grouchily y'rs - tim
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


martin at v

Nov 3, 2008, 9:45 PM

Post #12 of 33 (1513 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

> Martin> On Windows, the GMP binaries would be incorporated into
> Martin> pythonxy.dll. This would force anybody providing a copy of
> Martin> pythonxy.dll to also provide the sources of GMP.
>
> As I understand it the proposal was to allow people to substitute GMP for
> Python's long implementation. Just deliver binaries with the Python long
> version if you don't want to distribute the GMP source.

Ah, as an option, it could work.

> OTOH, it should be
> no big deal to drop a zip archive of the GMP sources which correspond to the
> code bound into the DLL.

How would end users then extract the sources from the DLL? How would
they learn that they even have them in the first place?

Also, people would complain about the increase in size - it's 3.2MB
as a zip file (1,7M as .tar.bz2).

> OTOOH, doesn't Windows support dynamic linking?
> Can't pythonxy.dll dynamically link to a gmpMN.dll?

How would that help in not having to distribute the source? Dynamic
linking still requires that the code is available at run-time.

Regards,
Martin
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


martin at v

Nov 3, 2008, 9:48 PM

Post #13 of 33 (1505 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

Stephen J. Turnbull wrote:
> Thomas Wouters writes:
>
> > Neither of those (shipping sources or dynamically linking to GMP) would
> > solve the LGPL issue. People who distribute that build of Python would still
> > be held by the LGPL -- such as shipping any sources that they embed that
> > Python into.
>
> No, that's exactly what the "L" in "LGPL" means. You only have to
> ship the sources to any object module (library or program) actually
> containing GMP code, including any modifications you make to them. So
> if GMP is kept entirely in a separate library and you don't borrow any
> code from it, the rest of your code is not affected by the LGPL.

Sure. However, when you ship pythonxy.dll to your users, and it links
with gmp.dll, you *will* have to ship gmp.dll to your users, as well -
they just won't accept having to download gmp.dll from elsewhere (they
may not even know what Python is). So then you have to include the
source (of GMP, not Python). The L is really only about "include source
just from the library (gmp), not the application (Python)".

Regards,
Martin
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


stephen at xemacs

Nov 3, 2008, 10:32 PM

Post #14 of 33 (1508 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

"Martin v. Löwis" writes:
> Stephen J. Turnbull wrote:
> > Thomas Wouters writes:
> >
> > > Neither of those (shipping sources or dynamically linking to
> > > GMP) would solve the LGPL issue. People who distribute that
> > > build of Python would still be held by the LGPL -- such as
> > > shipping any sources that they embed that Python into.
> >
> > No, that's exactly what the "L" in "LGPL" means. You only have to
> > ship the sources to any object module (library or program) actually
> > containing GMP code, including any modifications you make to them. So
> > if GMP is kept entirely in a separate library and you don't borrow any
> > code from it, the rest of your code is not affected by the LGPL.
>
> Sure. However, when you ship pythonxy.dll to your users, and it links
> with gmp.dll, you *will* have to ship gmp.dll to your users [and]
> then you have to include the source (of GMP, not Python).

Right. There is definitely an administrative cost here, one that
Python itself doesn't impose. This may not be a big deal for some
downstream projects, but that is for each project to decide. I agree,
it is quite reasonable for Python to prefer to avoid the issue by
allowing downstream to add GMP or not as it likes, and simplify its
own maintenance burden by only providing the simple portable good-
enough-for-government-work implementation.

Thomas's wording strongly suggested to me that he thought that the
presence of gmp.dll would force a project to include not only GMP
source, but Python source *and* even my Killer-App's source. That's
not true, and that's the only issue I was trying to address.
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


greg.ewing at canterbury

Nov 4, 2008, 12:47 AM

Post #15 of 33 (1510 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

Martin v. Löwis wrote:

> On Windows, the GMP binaries would be incorporated into pythonxy.dll.
> This would force anybody providing a copy of pythonxy.dll to also
> provide the sources of GMP.

I thought the suggestion was to provide a way of optionally
compiling Python to use GMP. The standard Python distribution
for Windows wouldn't be compiled that way, so it wouldn't
have any GMP-related code in its dll.

--
Greg
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


thomas at python

Nov 4, 2008, 12:55 AM

Post #16 of 33 (1512 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

On Tue, Nov 4, 2008 at 07:32, Stephen J. Turnbull <stephen [at] xemacs>wrote:

> "Martin v. Löwis" writes:
> > Stephen J. Turnbull wrote:
> > > Thomas Wouters writes:
> > >
> > > > Neither of those (shipping sources or dynamically linking to
> > > > GMP) would solve the LGPL issue. People who distribute that
> > > > build of Python would still be held by the LGPL -- such as
> > > > shipping any sources that they embed that Python into.
> > >
> > > No, that's exactly what the "L" in "LGPL" means. You only have to
> > > ship the sources to any object module (library or program) actually
> > > containing GMP code, including any modifications you make to them. So
> > > if GMP is kept entirely in a separate library and you don't borrow any
> > > code from it, the rest of your code is not affected by the LGPL.
> >
> > Sure. However, when you ship pythonxy.dll to your users, and it links
> > with gmp.dll, you *will* have to ship gmp.dll to your users [and]
> > then you have to include the source (of GMP, not Python).
>
> Right. There is definitely an administrative cost here, one that
> Python itself doesn't impose. This may not be a big deal for some
> downstream projects, but that is for each project to decide. I agree,
> it is quite reasonable for Python to prefer to avoid the issue by
> allowing downstream to add GMP or not as it likes, and simplify its
> own maintenance burden by only providing the simple portable good-
> enough-for-government-work implementation.
>
> Thomas's wording strongly suggested to me that he thought that the
> presence of gmp.dll would force a project to include not only GMP
> source, but Python source *and* even my Killer-App's source. That's
> not true, and that's the only issue I was trying to address.
>

Ah, but not true according to who? If you read the LGPL, you'll note that it
specifically says that 'linking in' makes the LGPL library part of the work
(or rather, makes the whole a derived work.) Considering that, on Windows,
the linking is done at compiletime and only the loading is done at runtime,
it's unclear to me that this is not the case here. I don't know if RMS and
the FSF think of it this way, but I stopped trying to guess their thoughts
and actions a long time ago. It is certainly the case that such a
combination is enough to scare off corporate lawyers who aren't sure either
(most of them, I bet) and would advise against using that build of Python
because of the LGPL components. It's not without cost to Python, even
discounting the administrativia.

--
Thomas Wouters <thomas [at] python>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!


greg.ewing at canterbury

Nov 4, 2008, 1:25 AM

Post #17 of 33 (1515 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

Martin v. Löwis wrote:
> you *will* have to ship gmp.dll to your users, as well ... So then
> you have to include the source (of GMP

Are you sure? I thought the source-provision requirements
of the *GPL licences only apply when you distribute a
*modified* version of something. Here you're just shipping
an unmodified GMP library.

--
Greg
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


dickinsm at gmail

Nov 4, 2008, 3:00 AM

Post #18 of 33 (1511 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

[Martin v. Löwis]
> Perhaps Tim Peters should also comment here - but if you can come up
> with a patch that does that in a portable way, I would be in favor.
> The challenge, ISTM, is to make it still compile on all systems
> (regardless of how inefficient it might be on minor platforms).

I've just uploaded a patch to the issue tracker that allows Python
to use 30-bit digits on platforms having a 64-bit integer type:

http://bugs.python.org/issue4258

[Tim Peters]
> Note that while 32x32->64 multiply is supported by x86 HW, that
> doesn't mean there's a uniform way to get at this HW capability across
> C compilers. In contrast, (at least) efficient HW 15x15->30 multiply
> is universally spelled in C via "i*j" :-)

If we're talking about standards and portability, doesn't "i*j" fail
on those (nonexistent?) platforms where the 'int' type is only 16-bits?
Shouldn't this be something like "(long)i * j" instead?

And for 32x32 -> 64, can't this simply be replaced by "(uint64_t)i * j",
where uint64_t is as in C99? I'd hope that most compilers would
manage to turn this into the appropriate 32x32-bit hardware multiply.

I agree that very-long-integer optimizations probably don't really belong in
Python, but this patch should also provide significant benefits for short
and medium-sized integers. I guess I need to go away and do some
benchmarking...

Mark
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


matthieu.brucher at gmail

Nov 4, 2008, 3:20 AM

Post #19 of 33 (1506 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

2008/11/4 Greg Ewing <greg.ewing [at] canterbury>:
> Martin v. Löwis wrote:
>>
>> you *will* have to ship gmp.dll to your users, as well ... So then
>
>> you have to include the source (of GMP
>
> Are you sure? I thought the source-provision requirements
> of the *GPL licences only apply when you distribute a
> *modified* version of something. Here you're just shipping
> an unmodified GMP library.

In fact you only have to provide a way to get the sources of the
library. You don't have to ship the library with the source.

As for the LGPL on Windows, wxWidgets does this for a long time now
and a program does not have to be open source or LGPL or whatever.

Matthieu
--
Information System Engineer, Ph.D.
Website: http://matthieu-brucher.developpez.com/
Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn: http://www.linkedin.com/in/matthieubrucher
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


stephen at xemacs

Nov 4, 2008, 3:31 AM

Post #20 of 33 (1503 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

Thomas Wouters writes:

> Ah, but not true according to who? [...]

> It is certainly the case that such a combination is enough to scare
> off corporate lawyers who aren't sure either (most of them, I bet)
> and would advise against using that build of Python because of the
> LGPL components.

I don't think this is a matter of "according to whom", except where
you have a timid client and a FUD-ful lawyer, which is a plausible
case, I admit. Copyright law is very literal about this kind of
thing, and does not distinguish between bytes that represent a
symbolic name for a function, and bytes that represent the address of
the function. If the LGPL licenses symbolic references as long as the
function object code is not copied into your executable, then
numerical addresses are also OK as long as the function object code is
not copied into the executable you distribute.

I agree the language used in the LGPL is unfortunately imprecise. But
the FSF's intent and understanding is quite clear about the issues you
raise. See their License FAQ and David Turner's article about Java
vs. the LGPL:

http://www.fsf.org/licensing/licenses/gpl-faq.html#Prelinking
http://www.fsf.org/licensing/licenses/gpl-faq.html#LGPLv3ContributorVersion
http://www.fsf.org/licensing/licenses/lgpl-java.html

I see nothing anywhere to indicate that the FSF thinks that the LGPL
should work differently in a practical sense on Windows than it does
on GNU/Linux, and while Turner's article refers to language
environments rather than OSes, it's pretty clear that the assertion
that "the LGPL just works as intended and expected" applies to OS
environments too.
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


skip at pobox

Nov 4, 2008, 4:00 AM

Post #21 of 33 (1500 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

>> OTOH, it should be no big deal to drop a zip archive of the GMP
>> sources which correspond to the code bound into the DLL.

Martin> How would end users then extract the sources from the DLL? How
Martin> would they learn that they even have them in the first place?

I think you missed an implied comma in my statement. I meant:

1. distribute pythonxy.dll which binds to GMP.
2. distribute a gmpmn.zip which contains the source for the version used
in #1.

Skip
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


victor.stinner at haypocalc

Nov 4, 2008, 9:38 AM

Post #22 of 33 (1502 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

Le Monday 03 November 2008 18:56:37 Paul Miller, vous avez écrit :
> Rather than that, what about patching Python's long implementation
> to use GMP if it's available, and the default implementation if not?

Yes, I like this suggestion of two flavors. Python with GMP and Python without
GMP (builtin integer library). It's "easy" to change the long type
implementation since only few modules use PyLong internals:
- mashal: import/export (.pyc)
=> core function might be moved to longobject.c
- math: use some special features of the PyLong type
=> macro can be written for that
or simplify a #ifdef.

I first wrote a patch in January 2008 to test Python3 + GMP. After your email
I updated my patch and fix it to pass most regression tests. Depending of the
patch version, the overhead (of using GMP instead a builtin library) is
between 14% and 3% (Python is slower with GMP). I used pystone which is not a
math application.

More details at:
http://bugs.python.org/issue1814

--
Victor Stinner aka haypo
http://www.haypocalc.com/blog/
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


tim.peters at gmail

Nov 4, 2008, 10:33 AM

Post #23 of 33 (1511 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

Hey, Mark -- let's establish some background here first. It's a fact
that the product of two N-bit integers can require 2N bits, and also a
fact that lots of HW supports NxN->2N bit integer multiplication
directly.

However, it's unfortunately also a fact that standard C has no
corresponding concept: "*" in C always means NxN->N multiplication
(single-width product, with possibility of overflow). I don't know
whether C99 improved this situation -- I know it was proposed to add
some "double-width integer product" /functions/, but I don't know
whether that was adopted. I do know that "*" remained "single-width".

[Tim Peters]
>> Note that while 32x32->64 multiply is supported by x86 HW, that
>> doesn't mean there's a uniform way to get at this HW capability across
>> C compilers. In contrast, (at least) efficient HW 15x15->30 multiply
>> is universally spelled in C via "i*j" :-)

['Mark Dickinson]
> If we're talking about standards and portability, doesn't "i*j" fail
> on those (nonexistent?) platforms where the 'int' type is only 16-bits?
> Shouldn't this be something like "(long)i * j" instead?

Sorry, I should have made type declarations explicit. In Python, the
basic long building block is "digit", which is typedef'ed to C
unsigned short. C89 guarantees this holds at least 16 bits. Whenever
two digits are multiplied, the code intends to cast (at least) one of
them to "twodigits" first (if you ever see a spot where this doesn't
happen, it's a bug). "twodigits" is typedef'ed to C long. C89
guarantees that a long holds at least 32 bits.

So C guarantees that we're doing (at least) 32x32->32 multiplication
whenever you see code like

digit i, j;
twodigits k;

k = (twodigits)i * j;

In particular, the (at least) 32x32->32 C89 guarantees for that is at
least 15x15->30, and the latter is all that longobject.c intends to
rely on. Along with the cast to twodigits, this is achieved across
all conforming C compilers simply by using infix "*". The code from
1990 still works fine, on everything from cell phones to archaic Cray
boxes.


> And for 32x32 -> 64, can't this simply be replaced by "(uint64_t)i * j",
> where uint64_t is as in C99? I'd hope that most compilers would
> manage to turn this into the appropriate 32x32-bit hardware multiply.

1. That's C99, not C89, and therefore less portable.

2. On platforms that support it, this is at least 64x64->64 multiplication,
potentially much more expensive than the 32x32->64 (or 31x31->62?)
flavor you /intend/ to move to.

3. There's no way to know exactly what compilers will do with this short of
staring at generated code.

FYI, in a previous life working in speech recognition, under
Microsoft's Visual Studio 6 the only way we found to get at the
Pentium's 32x32->64 HW ability efficiently was via using inline
assembler. For example, using various MSVC spellings of "64-bit int"
instead for the inputs usually generated external calls to a
long-winded C library "piece by piece" multiplication routine (which,
at the time, emulated 64x64->128 multiplication, then threw away the
high 64 bits).

Again, the fundamental problem here is the disconnect between what
some HW is capable of and what C allows to express (at least through
C89). That's why it's impossible here to write portable code in C
that's also efficient. Even what Python does now is vulnerable on the
efficiency count: on some weird platforms, "long" is 64 bits, and so
multiplying a pair of twodigits incurs the expense of (usually
non-native) 64x64->64 multiplication.


> I agree that very-long-integer optimizations probably don't really belong in
> Python,

Depends in part on whether Python can attract as many obsessed
maintainers and porters for such gonzo algorithms as GMP attracts ;-)


> but this patch should also provide significant benefits for short
> and medium-sized integers. I guess I need to go away and do some
> benchmarking...

If you can /get at/ HW 32x32->64 int multiply, of course that would be faster.
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


mal at egenix

Nov 4, 2008, 10:58 AM

Post #24 of 33 (1509 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

On 2008-11-04 18:38, Victor Stinner wrote:
> Le Monday 03 November 2008 18:56:37 Paul Miller, vous avez écrit :
>> Rather than that, what about patching Python's long implementation
>> to use GMP if it's available, and the default implementation if not?
>
> Yes, I like this suggestion of two flavors. Python with GMP and Python without
> GMP (builtin integer library). It's "easy" to change the long type
> implementation since only few modules use PyLong internals:
> - mashal: import/export (.pyc)
> => core function might be moved to longobject.c
> - math: use some special features of the PyLong type
> => macro can be written for that
> or simplify a #ifdef.

Ahem, what about the gazillion extension modules out there ?

You cannot simply change the long implementation and expect
everything to just work. Besides, like Tim already said: adding
lots and lots of #ifdefs is just going to make it harder
to maintain the implementation without gaining anything much.

I don't really see much of a need for this anyway. If people
want to use GMP they can download gmpy or PyGMP or our
experimental mxNumber extension:

http://www.egenix.com/products/python/mxExperimental/mxNumber/

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Nov 04 2008)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


victor.stinner at haypocalc

Nov 4, 2008, 11:01 AM

Post #25 of 33 (1508 views)
Permalink
Re: Optionally using GMP to implement long if available [In reply to]

About 31, 32, 63 or 64 bits: I guess that you want to avoid integer overflow.
Intel has an "overflow" flag, changed for all instructions. For other CPUs,
you can use emulate this flag. Example with the type int that I used in my
GMP patch:

Add:
int a, b, sum;
sum = a + b;
exact = ((a < 0) ^ (b < 0)) || ((a < 0) == (sum < 0));

Substract:
int a, b, diff;
diff = a + b;
exact = ((a < 0) == (b < 0)) || ((a < 0) == (diff < 0));

Multiply:
int a, b, product;
if (a == 0 || b == 0) {
product = 0; /* exact */
} else if (a != INT_MIN || (b == 1)) {
product = a * b;
exact = (product / b) == a);
} else {
/* INT_MIN * -1 = -INT_MIN: overflow */
}

Divide:
int a, b, q;
if (a != INT_MIN || b != -1) {
q = a / b; /* exact */
} else {
/* INT_MIN / -1 = -INT_MIN: overflow */
}

Checking overflow may costs more than using a smaller base. Only a benchmark
can answer ;-)

--
Victor Stinner aka haypo
http://www.haypocalc.com/blog/
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

First page Previous page 1 2 Next page Last page  View All Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.