A complete Petsc interface in Python?

Asked by Arvind Krishna

Hi,

I am new to Python but not new to Petsc and C++. I am trying to develop a code in Python using Fenics with Petsc as the linear algebra backend. It appears to me(maybe I am wrong) that Fenics has a rudimentary interface to Petsc. I'd like to be able to use several of the features of Petsc.

I know there exists the down_cast option....I know this returns the C++ pointer of the Petsc object but I am not sure how I could use that in Python. One alternative is to use the Petsc4py interface http://code.google.com/p/petsc4py/ . I spoke to Lisandro, one of the developers of petsc4py amd he suggested using a SWIG interface and that you might be able to support petsc4py without a compile time dependecy . I am not too familiar with SWIG but do you think this is possible.

I'd appreciate suggestions to resolve this issue . Or you could tell me I'm wrong and all petsc options are accessible via fenics :) Otherwise I might reluctantly have to program in C++. Thanks in advance,

Question information

Language:
English Edit question
Status:
Answered
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Johan Hake (johan-hake) said :
#1

Arvind!

> I am new to Python but not new to Petsc and C++. I am trying to develop a
> code in Python using Fenics with Petsc as the linear algebra backend. It
> appears to me(maybe I am wrong) that Fenics has a rudimentary interface to
> Petsc. I'd like to be able to use several of the features of Petsc.
>
> I know there exists the down_cast option....I know this returns the C++
> pointer of the Petsc object but I am not sure how I could use that in
> Python. One alternative is to use the Petsc4py interface
> http://code.google.com/p/petsc4py/ . I spoke to Lisandro, one of the
> developers of petsc4py amd he suggested using a SWIG interface and that
> you might be able to support petsc4py without a compile time dependecy . I
> am not too familiar with SWIG but do you think this is possible.

This has come up as a request previously, but is unfortunately not supported
yet. The reason for this is that the PETSc Python objects in DOLFIN are than
tyhe equivalent in petsy4py.

However if you could help me, either directly or by putting me in contact with
Lisandro, I should be able to write a small interface making it possible to
use what are returned by:

  down_cast(A).mat()
  down_cast(b).vec()

in petsc4py.

What I need to know is the Python C-api code of how I can generate the Python
wrapped PETSc object given the C++ equivalent. I know petsc4py uses cython, so
either we could include a minimal cython based converter, or we could build
the Python object in C code, based on how the struct is comoposed. The latter
is to prefer as we then do not need any complicated cython configuration.

I have looked into the source code of petsc4py. But I have not used cython so
that was all greek for me :P

> I'd appreciate suggestions to resolve this issue . Or you could tell me I'm
> wrong and all petsc options are accessible via fenics :) Otherwise I might
> reluctantly have to program in C++.

Yes, but it would be nice to have this feature in there.

Johan

Revision history for this message
Arvind Krishna (arvindks) said :
#2

Hi Johan,

Thanks for your quick response. I don't know much about SWIG/cython etc, so I won't be able to contribute much to this discussion. Here is the snippet of code that Lisandro suggested I use

%module dolfin2petsc4py
%include petsc4py/petsc4py.i
%{#include "dolfin/la/PETScBaseMatrix.h"%}
%{void dolfin2petsc4py(dolfin::PETScBaseMatrix& M, Mat &A){ Mat *p =
M.mat().get(); A = *p;}%}
namespace dolfin { class PETScBaseMatrix;}%apply Mat *OUTPUT { Mat
&out }void dolfin2petsc4py(dolfin::PETScBaseMatrix&, Mat &out);

I am still trying to make sense of it. I can write an email to Lisandro introducing him to you, I am not sure how busy he is though but he is usually very quick to respond.

Thanks for your help, if we can get this python interface working that would be pretty awesome.

Revision history for this message
Johan Hake (johan-hake) said :
#3

Arvind!

Well, it looks like that is pretty much all that I need :) I did not know that
petsc4py comes with a pretty well developed SWIG interface.

I will add a blueprint for this feature, and add it to trunk when I get time.
Do not count on it for Christmas though ;)

Johan

On Friday December 16 2011 00:20:51 Arvind Krishna wrote:
> Question #181985 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/181985
>
> Status: Answered => Open
>
> Arvind Krishna is still having a problem:
> Hi Johan,
>
> Thanks for your quick response. I don't know much about SWIG/cython etc,
> so I won't be able to contribute much to this discussion. Here is the
> snippet of code that Lisandro suggested I use
>
>
> %module dolfin2petsc4py
> %include petsc4py/petsc4py.i
> %{#include "dolfin/la/PETScBaseMatrix.h"%}
> %{void dolfin2petsc4py(dolfin::PETScBaseMatrix& M, Mat &A){ Mat *p =
> M.mat().get(); A = *p;}%}
> namespace dolfin { class PETScBaseMatrix;}%apply Mat *OUTPUT { Mat
> &out }void dolfin2petsc4py(dolfin::PETScBaseMatrix&, Mat &out);
>
> I am still trying to make sense of it. I can write an email to Lisandro
> introducing him to you, I am not sure how busy he is though but he is
> usually very quick to respond.
>
> Thanks for your help, if we can get this python interface working that
> would be pretty awesome.

Can you help with this problem?

Provide an answer of your own, or ask Arvind Krishna for more information if necessary.

To post a message you must log in.