Sunday 6 May 2012

First look at Riverbank SIP (Python bindings for C and C++ libraries)

This is where I take a step back and start looking at improving my rapid prototyping Python, numpy and C pipeline. I'm looking into replacing SWIG with SIP.

I've posted two tutorials about generating Python bindings using SWIG and numpy.i:
These describe a lot of the programming I do. Using SWIG, I can write code which is completely free from any Python/C constructs and easily generate usable bindings. The second tutorial shows automatic memory deallocation when the Python arrays are deleted. This makes my code truly "pythonic".

On the Python side of things, a wealth of libraries (Matplotlib, PIL, VTK, etc...) allows me to load various datasets (from databases, various microscopy image formats, synthetic data...), test the code and display useful data. Also if needed, I can use my code in a native application.

Some simple GCC optimisations have really made my code fly:
  • native CPU optimisation enabled by -march=native
  • auto-vectorisation, enabled at -O3 (and detailed by -ftree-vectorizer-verbose=2)
  • OpenMP multithreading enabled by -fopenmp requires a bit more work but is well worth the trouble (more about this here).
So back to bindings, and let me reiterate that both SWIG and numpy.i have been good and loyal friends over the years. I have never had to worry about my bindings until some recent work which is mostly Python but repeatedly calls a few scattered functions written in C. In this case, it's worth trying to optimise the bindings as well as the code, so it's time to look at some alternatives to SWIG. I found some anecdotal evidence that I should be looking into SIP as a replacement for SWIG.

My first baby step was to adapt SWIG's example.fact() to SIP. Result code is here.

EDIT: As a second baby step, I wrote a C++ example. Result code is here.

Next step will be to convert numpy.i to SIP. Maybe start with IN_ARRAY1, try and stress the binding code and compare to SWIG.

Stay tuned! And of course, if you want to share your own experience with SWIG / SIP / Cython, I'd love to hear about it.

No comments:

Post a Comment