[pypy-dev] libgmp

Stefan Behnel stefan_ml at behnel.de
Fri Feb 24 09:17:13 CET 2012


Maciej Fijalkowski, 24.02.2012 03:18:
> On Thu, Feb 23, 2012 at 6:56 PM, Timothy Baldridge wrote:
>> For a project I'm working on, I'd like to have support for gmp in
>> pypy. I have a ctypes pypy module, but from what I understand, pypy's
>> ctypes are a bit slow compared to CPython. What's the best way to get
>> access to get access to libgmp from python? Would you be against a
>> pull request that added libgmp as an RPython module?
> 
> PyPy's ctypes story is a kind of sad one. There was an attempt to make
> it fast, but it's definitely unfinished. If you hit the correct sweet
> spot of ctypes, it's super-fast, much faster than cpython. If you
> however don't, it's slow. Maybe you can show a concrete usecase and I
> can try and speed it up?

Yep, benchmarking the obvious solution instead of stepping right into the
optimisation is *always* a good idea.


> I suppose ctypes is the advertised way of accessing C from PyPy for now.

Sorry for bringing up this topic again, but I know that loads of people in
the Cython community are rather happy users of gmp. Sage contains quite a
bit of gmp code, for example, and random people keep mentioning their own
usage of it on the cython-users mailing list quite frequently. At least,
there should be some code around to build on.

So, as a reply to the OP: seeing the current advances in getting Cython
code to run in PyPy (interfacing at the C-API level), another option would
be to do exactly as you would in CPython and implement the performance
critical parts of the code that heavily rely on gmp interaction in Cython
(to get plain C code out of it), and then import and use that from PyPy,
but at a much higher level than with direct individual calls to the gmp
functions. However much faster ctypes can be in PyPy than in CPython, you
just can't beat the performance of a straight block of C code when the goal
is to talk to C code. Cython will also allow you to run code in parallel
with OpenMP, in case you need that.

Depending on your needs, that approach could reduce the amount of
interfacing to a minimum, thus equally reducing the chance of running into
performance problems with ctypes.

PyPy support for Cython is not released yet and still hasn't left the alpha
quality stage. However, if it works, it works.

You can find it here:

https://github.com/scoder/cython

And you will most likely also need a nightly build of PyPy.

Stefan



More information about the pypy-dev mailing list