[petsc-users] petsc4py, cython
Jaroslaw Piwonski (CAU)
jpi at informatik.uni-kiel.de
Fri Jan 12 10:13:47 CST 2018
> Am 12.01.2018 um 16:35 schrieb Matthew Knepley <knepley at gmail.com <mailto:knepley at gmail.com>>:
>
> On Fri, Jan 12, 2018 at 9:38 AM, Jaroslaw Piwonski <jpi at informatik.uni-kiel.de <mailto: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‘.
I tried a lot in this regard. Unfortunately nothing worked:
$> 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(PetscVec)
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
This seems not to be the point … the compiler does not complain about this definition:
$> 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(i_can_define_here_what_i_want_it_wont_matter)
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
Thanks,
Jaroslaw
@Matt:
Sorry, the first answer went to the wrong address.
>
> 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 <tel:+49%20431%208807452>
> Telefax: +49 431 880-7618 <tel:+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/>
—
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/98946573/attachment-0001.html>
More information about the petsc-users
mailing list