On Fri, 14 Aug 2009 23:17:04 +0200, Jon Olav Vik  
<[email protected]> wrote:

> Jon Olav Vik <jono...@...> writes:
>
> Answering a few of my own questions, in case anyone else is interested:
>
>> Kurt Smith <kwmsm...@...> writes:
>> > Hi, Jon.  I'm the 'maintaner' of fwrap, which is the new name of
>> > 'f2cy' (I clearly need to put an update post on the blog!).  It's
>> > still in development, and will have basic functionality ready to go in
>> > a few weeks.  I don't know if we'll have C (i.e. cdef) callbacks
>> > implemented by then, but it will be high on the priority list.  It
>> > will certainly help you wrap a Fortran library in Cython, as you
>> > desire, although the api is similar to f2py where the wrapping is
>> > automated,
>>
>> Looking at http://www.scipy.org/F2py, it certainly looks promising.
[...]
> Chapters 5 and 9 of this book have detailed examples:
> http://www.amazon.com/Python-Scripting-Computational-Science-Engineering/dp/3540739157/

To wrap this up (pardon the pun), f2py turned out to be a perfect fit in  
this case (expensive function evaluations). I spent a day on the examples  
in Langtangen's book, studying the f2py user guide, and some manual  
preprocessing of the source files to be wrapped. I wrote a dummy .f file  
to provide signatures for the functions to be differentiated, then  
autogenerated a .pyf header file with f2py. With a moderate amount of  
careful editing of the .pyf file, the wrapper ended up being very flexible  
and pythonic.

The wrapper simplifies the interface by:
* Allowing lists etc for vectors "x"; these are autoconverted to Numpy  
arrays.
* Using Python callables for callback functions "f(x)".
* Possibility of passing extra parameters to f(x, ...).
* Hiding parameters for array dimensions (these are obtained from the  
Numpy arrays).
* Providing defaults for technical parameters such as the order of  
accuracy.
* Returning results as function values instead of using output arguments.

The first four are done automatically by f2py; the remaining ones are done  
by tweaking individual lines of the .pyf header file. These convenience  
features save me a lot of effort compared to having a more low-level,  
true-to-Fortran wrapper. I lose a little speed because callbacks go  
through Python and because of argument checking/conversion, but that  
doesn't matter so much when the function evaluations themselves are  
expensive.

(The functions in question are the differential equation simulations I  
posted about previously. Using Cython for the right-hand-side function  
gave a 10x speedup for the whole integration; 100x for the  
right-hand-side, but keeping the Pysundials wrapper which passes callbacks  
through Python. I gave up wrapping CVODE directly at this time; it ended  
up requiring too much manual work for my current level of (in)expertise.)

Thanks to everyone who helped!

Best regards,
Jon Olav

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to