Python<->C cross-language interoperability

Lisandro Dalcin dalcinl at
Fri Nov 23 13:34:46 CST 2007

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.

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)

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

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