Python<->C cross-language interoperability

Lisandro Dalcin dalcinl at gmail.com
Sat Nov 24 14:28:45 CST 2007


On 11/23/07, Barry Smith <bsmith at mcs.anl.gov> wrote:
>    BTW: run with -snes_monitor the nonlinear convergence is terribly
> slow.

Expected! This example solves 'x^2=0'. If my mind is not failing, for
roots with multiplicity greather than 1, convergence is linear.

Note that you could try almos any SNES example in PETSc souces,
provided that you link the executable with the Python library, and you
add the lines initizlizing and finalizing Python.

> BTW: I have added a --download-numpy and --download-petsc4py to
> petsc-dev. Preliminary version
> uses the SVN versions. Eventually I'd like it to auto-select the
> correct version.

Fine. Anyway, please note that for using the SVN version, SWIG is
required for building the wrappers. For a release version, this is not
required. Additionally, downloading and building a petsc4py release is
almost trivial if Python have 'setuptools' package installed. All what
is needed is to do

$ export PETSC_DIR=/path/to/petsc
$ export PETSC_ARCH=arch1:arch2:arch3
$ export PYTHONPATH=$HOME/lib/python
$ easy_install -d $HOME/lib/python petsc4py

This will download (easy_install looks at python package index to find
were the tarball is)and install a 'multi-petsc-arch' petsc4py at
$HOME/lib/python (the normal place were you should install python
packages on your home dir). The first 'petsc-arch' is used by default,
but you can select other at runtime via environ vars or
programatically.


>
>     Barry
>
> On Nov 23, 2007, at 2:38 PM, Lisandro Dalcin wrote:
>
> > On 11/23/07, Matthew Knepley <knepley at gmail.com> 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:
> >
> > http://petsc4py.googlecode.com/svn/trunk/petsc/lib/ext/petsc/src/snes/impls/python/
> >
> > The C example is 'ex1.c' (note the line calling Py_Initialize() ), the
> > equivalent Python example is 'ex1.py'. Both examples calls the methods
> > of class 'MyNewton' (implemented in file 'mysolver.py').
> >
> > 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
> > mysolver.py at MyNewton.setFromOptions() methods) a lot of ouput
> > showing the methods and aguments passed during the complete nonlinear
> > solve.
> >
> > 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
> >
>
>


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