[petsc-users] petsc4py, cython

Jaroslaw Piwonski (CAU) jpi at informatik.uni-kiel.de
Fri Jan 12 08:38:43 CST 2018


Dear PETSc users,

I would like to use petsc4py to create (say) PETSc vectors and
then pass them to a routine from a module written in Cython,
which in turn passes them to a routine written in C:

main.py ========================================================

from petsc4py import PETSc
import m3d4py

if __name__ == "__main__":

    v = PETSc.Vec()
    v.create()
    v.setType("standard")
    v.setSizes(3)
    v.set(0.3)
    v.assemble()

    m3d4py.sub_pyx(v)

m3d4py.pyx ========================================================

cimport petsc4py.PETSc as PETSc

cdef extern from "sub.c":
    int sub_c(Vec)

def sub_pyx(PETSc.Vec vec):
    vec.view()
    sub_c(vec.vec)

sub.c ========================================================

#include "petsc.h"

extern PetscErrorCode sub_c(Vec);

PetscErrorCode sub_c(Vec vec) {
    PetscFunctionBegin;
    VecView(vec, PETSC_VIEWER_STDOUT_WORLD);
    PetscFunctionReturn(0);
}

========================================================

I used (ana/mini)conda to install petsc4py and cython. When I try to
compile the cython file I get the following error:

$> cython -f m3d4py.pyx -I /Users/jpicau/miniconda3/lib/python3.6/site-packages/petsc4py/include/

Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "sub.c":
    int sub_c(Vec)

def sub_pyx(PETSc.Vec vec):
    vec.view()
    sub_c(vec.vec)
            ^
------------------------------------------------------------

m3d4py.pyx:8:13: Cannot convert 'PetscVec' to Python object
make: *** [all] Error 1

========================================================

I somehow realize that this is not correct. But comparing this to the petsc4py implementation ...

petscvec.pxi:
	int VecView(PetscVec,PetscViewer)

Vec.pyx:
    def view(self, Viewer viewer=None):
        cdef PetscViewer vwr = NULL
        if viewer is not None: vwr = viewer.vwr
        CHKERR( VecView(self.vec, vwr) )

… I do not understand why.

Can some explain this to me or just give me a hint where I can dig further, please?

And just out of pure curiosity, why isn’t the return value of VecView() in petscvec.pxi defined as PetscErrorCode?
The type seems to be defined:

libpetsc4py.pyx:
    ctypedef int PetscErrorCode

Thanks in advance,
Jaroslaw

—

Dr. Jaroslaw Piwonski

Christian-Albrechts-Universität zu Kiel
Institut für Informatik
Algorithmische Optimale Steuerung — CO2-Aufnahme des Meeres
Christian-Albrechts-Platz 4
24118 Kiel

Telefon: +49 431 880-7452
Telefax: +49 431 880-7618

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20180112/0596490a/attachment.html>


More information about the petsc-users mailing list