Switch from SWIG to pybind11 for Python binding

Issue #897 resolved
Prof Garth Wells created an issue

The SWIG wrappers have become very complicated over time, and few (if any) developers understand them. They are also a barrier for new contributors. Getting the SWIG wrapping to work properly is often the hardest part of any new development. Complexity of the wrapping has also lead to interface inconsistencies because it can be difficult to get SWIG to wrap as one would like.

The SWIG wrapping is also a performance bottleneck for JIT.

A transition to using http://pybind11.readthedocs.io/ has started. pybind11 is C++ (no 3rd language to master), compact and is designed to interface with STL objects, NumPy and Eigen. Unlike SWIG, it requires hand-wrapping of functions. However, number of lines of hand-wrapped pybind11 is will be fewer than the SWIG hand-coded boilerplate.

It will be possible to build and install the Python interface using setuptools. This provides a clear separation between the DOLFIN C++ library and the Python interface, which is a simplification and has packaging advantages.

See also #859.

Comments (5)

  1. Prof Garth Wells reporter

    Here's the plan:

    1. We shortly copy the python/ directory in the pybind11 branch into master.
    2. We add with/without pybind11 decorators to the unit tests, and start running the unit tests with pybind11 on CircleCI (Python 3 only).
    3. We run the demos that work with pybind11 on CircleCI (Python 3 only)
    4. The owners of specialised demos, i.e. multimesh, take care of updates in the pybind11 interface.
    5. After the 2017.2 release, we delete the SWIG code and retire Instant.

    The pybind11 interface will continue to be improved (especially error handling) and polished, but it will be helpful to merge soon into DOLFIN to avoid the tests drifting away from the DOLFIN master branch.

  2. Log in to comment