[petsc-users] petsc4py, cython

Matthew Knepley knepley at gmail.com
Fri Jan 12 09:35:00 CST 2018


On Fri, Jan 12, 2018 at 9:38 AM, Jaroslaw Piwonski <
jpi at informatik.uni-kiel.de> wrote:

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

I think the declaration above is wrong. 'Vec' looks like it is seeing the
Python definition, and
probably you want 'PetscVec'.

  Thanks,

    Matt


>
> 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
> <https://maps.google.com/?q=Christian-Albrechts-Platz+4+24118+Kiel&entry=gmail&source=g>
> 24118 Kiel
> <https://maps.google.com/?q=Christian-Albrechts-Platz+4+24118+Kiel&entry=gmail&source=g>
>
> Telefon: +49 431 880-7452 <+49%20431%208807452>
> Telefax: +49 431 880-7618 <+49%20431%208807618>
>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ <http://www.caam.rice.edu/~mk51/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20180112/c61f17f1/attachment-0001.html>


More information about the petsc-users mailing list