Python<->C cross-language interoperability

Matthew Knepley knepley at
Fri Nov 23 13:41:45 CST 2007

On Nov 23, 2007 2:34 PM, Lisandro Dalcin <dalcinl at> wrote:
> I've achieved considerable progress in making petsc4py provide
> cross-language interoperable in the context of PETSc applications.
> I've implemented some new types for Mat, KSP, PC, SNES and TS. Those
> new type implementations enables dynamically use a Python class
> implementing some specific interface, and then this
> pure-Python-written class can be used in any PETSc application written
> in C/C++(/Fortran?)
> All this is very much like to having 'shell' types, but the language
> for implementing the specific operations is Python.
> Additionally, this is so unobtrusive to core PETSc code, than I want
> to ask if it there is any interest in provide a bit more of support in
> PETSc for this. All what would be needed is to make a call to
> Py_Initialize() inside PetscInitialize(), and the a call to
> Py_Finalize() inside PetscFinalize(); and of couse, get PETSc
> libraries linked against Python library.

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. I
> really need this to correctly manage Python stuff 'composed' inside
> PETSc objects (I currently use PetscContainer's, but I would love to
> have more specific support)

Yes, this is fine too. We should try and do it before the next release
since it will be a major release.

> If any of you want to take a look, I have a trivial C SNES example,
> were I solve a trivial nonlinear problem, but the nonlinear loop is
> implemented by calling many methods of a Python class (you pass at
> command line an option 'pymodule,PyClass' ). Those methods provide
> pre/post-solve/step actions, monitors, convergence test, custom linear
> solve operation, and custom linesearch (by default none, if you want
> one you can to implement it from scratch, as I've not yet taken the
> step of making current PETSc line search's reusable).

Cool, where is it?


> --
> 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

What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which
their experiments lead.
-- Norbert Wiener

More information about the petsc-dev mailing list