This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to pass a petsc4py.PETSc.Vec to c++ codes by using compile_extension_module?

+1 vote

Hi,

When searching for how to use compile_extension_module I found the following nice example,

from dolfin import *
from numpy import arange
code = '''
namespace dolfin {

void PETSc_exp(std::shared_ptr<dolfin::PETScVector> vec)
{
  Vec x = vec->vec();
  assert(x);
  VecExp(x);
}
}
'''
ext_module = compile_extension_module(code,
          additional_system_headers=["petscvec.h"])
comm = mpi_comm_world()
vec = PETScVector(comm, 10)
vec[:] = arange(10)
print vec[-1]
ext_module.PETSc_exp(vec)
print vec[-1]

where vec is a PETScVector,

In [15]: vec
Out[15]: <dolfin.cpp.la.PETScVector; proxy of <Swig Object of type 'std::shared_ptr< dolfin::PETScVector > *' at 0x3e462d0> >

and vec.vec() is a petsc4py vector,

In [16]: vec.vec()
Out[16]: <petsc4py.PETSc.Vec at 0x3e214d0>

So my question is, how to modidy the code that a petsc4py vector can be passed to ext_module? i.e., the following usage will work?

ext_module.PETSc_exp(vec.vec())

Thanks in advance!

asked Oct 3, 2014 by wangww2011 FEniCS Novice (130 points)
...