[SciPy-dev] Cython, f2py and GSoC

Dag Sverre Seljebotn dagss@student.matnat.uio...
Thu Mar 26 09:45:13 CDT 2009


David Huard wrote:
>
>
> On Wed, Mar 25, 2009 at 6:23 PM, Dag Sverre Seljebotn 
> <dagss@student.matnat.uio.no <mailto:dagss@student.matnat.uio.no>> wrote:
>
>     This is in preparation for a GSoC project application; Kurt Smith has
>     approached me about doing a project on Fortran integration in Cython
>     with myself as mentor.
>
>     Although we started out with a Fortran/Cython perspective, we think
>     that this potentially affects the SciPy community and f2py as well.
>
>     The main issues:
>      1) f2py doesn't work that well for Cython, as it requires Python
>     packing/unpacking of arguments. A more direct call approach is needed.
>      2) f2py and Cython has a certain overlap in their implementation
>     (both
>     generate Python extension modules), and need to tackle many of the
>     same
>     issues both now and especially in the future
>
>     Could we solve this so that in getting Fortran/Cython integration, we
>     also set up a development path for further development of f2py with
>     Cython as a backend?
>
>     Below is a scetch of our current plan to give you an idea. More full
>     specifications etc. will come later and we can have any
>     discussions then.
>
>      1) Add a Cython syntax and API for passing acquired PEP-3118
>     buffers/NumPy arrays to external C functions (i.e. as a struct
>     with the
>     necesarry information (pointer, shape, strides)). This simply means
>     defining a syntax for passing information that Cython already has
>     to an
>     external C function.
>
>      2) Create a new tool which uses the parser part of f2py (with any
>     necesarry improvements) but adds a different backend which
>     generates a C
>     interface to the given Fortran module, along with a Cython pxd
>     file for
>     it. (Adding a C .h file target, to get "f2c" functionality, would be
>     trivial.)
>
>     This will be done using the Fortran 2003 C bindings. So a .f90 file is
>     generated which compiles to a C interface to the library. Array
>     parameters will be passed as the PEP-3118-like structs we define
>     in 1),
>     and so the functions will be callable directly with e.g. NumPy arrays
>     from Cython. Copy-in/out might be necesarry for Fortran to be able to
>     work with the arrays, if so this will happen in the Fortran wrapper
>     generated by this new tool.
>
>
> Do you plan to write a modified copy of the entire subroutine or just 
> a wrapper subroutine accepting C types, which then calls the original 
> function ?
The latter.

Though we could always skip wrapping of functions which already are 
declared BIND(C), that's actually a nice idea. This would make it 
possible to write a wrapper manually and skip this overhead in extreme 
situations (where the Fortran compiler also cannot inline the call), 
while still having Python bindings generated.

Dag Sverre


More information about the Scipy-dev mailing list