Python<->C cross-language interoperability

Lisandro Dalcin dalcinl at
Fri Nov 23 14:38:03 CST 2007

On 11/23/07, Matthew Knepley <knepley at> wrote:
> Yes, we should definitely do this. I think I already have the support for
> linking to the Python library somewhere.
> > Perhaps I will also ask in the near future for adding two 'slots' in
> > the base PetscObject structure for better supporting petsc4py.
> Yes, this is fine too. We should try and do it before the next release
> since it will be a major release.

Nice, then I'll try to add the needed changes in petsc-dev, and I'll
will surely need your help for adding support in configure system.

> > If any of you want to take a look, I have a trivial C SNES example,
> Cool, where is it?

Well, all this is in petsc4py SVN repo, the specific example for SNES
is at this location:

The C example is 'ex1.c' (note the line calling Py_Initialize() ), the
equivalent Python example is ''. Both examples calls the methods
of class 'MyNewton' (implemented in file '').

The actual SNESPYTHON type is implemented in file 'python.c', but that
type is actually available through petsc4py, and automatically
registered at import time.

To run the example, you have to build petsc4py SVN using petsc-2.3.3
or petsc-dev (use shared libs), and install petsc4py somewere (and put
that location on PYTHONPATH). After that, build 'ex1.c' (the
'makefile' is using the old bmake paths, fix for the petsc-dev case)
and run it like the following;

$ make ex1
$ ./ex1 -snes_type python -trace

Your will see in your terminal (because of -trace option, look at at MyNewton.setFromOptions() methods) a lot of ouput
showing the methods and aguments passed during the complete nonlinear

Ask me if you have trouble with something.

I'll wait for your comments. Please note that all this is very
preliminar and was develeped in a couple of days, there is room for a
lot of improvement.

Lisandro Dalcín
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594

More information about the petsc-dev mailing list