<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Jan 12, 2018 at 9:38 AM, Jaroslaw Piwonski <span dir="ltr"><<a href="mailto:jpi@informatik.uni-kiel.de" target="_blank">jpi@informatik.uni-kiel.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div dir="auto" style="word-wrap:break-word">Dear PETSc users,<div><br></div><div>I would like to use petsc4py to create (say) PETSc vectors and</div><div>then pass them to a routine from a module written in Cython,</div><div>which in turn passes them to a routine written in C:</div><div><br></div><div>main.py ==============================<wbr>==========================</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#0433ff">from</span> petsc4py <span style="color:#0433ff">import</span> PETSc</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#0433ff">import</span> m3d4py</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#0433ff">if</span> __name__ == <span style="color:#b4261a">"__main__"</span>:</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    v = PETSc.Vec()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    v.create()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    v.setType(<span style="color:#b4261a">"standard"</span>)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    v.setSizes(3)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    v.set(0.3)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    v.assemble()</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    m3d4py.sub_pyx(v)</div></div><div><br></div><div>m3d4py.pyx ==============================<wbr>==========================</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">cimport petsc4py.PETSc as PETSc</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">cdef extern from "sub.c":</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    int sub_c(Vec)</div></div></div></div></blockquote><div><br></div><div>I think the declaration above is wrong. 'Vec' looks like it is seeing the Python definition, and</div><div>probably you want 'PetscVec'.</div><div><br></div><div>  Thanks,</div><div><br></div><div>    Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div dir="auto" style="word-wrap:break-word"><div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">def sub_pyx(PETSc.Vec vec):</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    vec.view()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    sub_c(vec.vec)</div></div><div><br></div><div><div>sub.c ==============================<wbr>==========================</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(4,51,255);background-color:rgb(255,255,255)"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(4,51,255);background-color:rgb(255,255,255)">#include <span style="color:#b4261a">"petsc.h"</span></div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#0433ff">extern</span> PetscErrorCode sub_c(Vec);</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">PetscErrorCode sub_c(Vec vec) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    PetscFunctionBegin;</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    VecView(vec, PETSC_VIEWER_STDOUT_WORLD);</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    PetscFunctionReturn(0);</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div></div><div>==============================<wbr>==========================</div><div><br></div><div>I used (ana/mini)conda to install petsc4py and cython. When I try to</div><div>compile the cython file I get the following error:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">$> cython -f m3d4py.pyx -I /Users/jpicau/miniconda3/lib/<wbr>python3.6/site-packages/<wbr>petsc4py/include/</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">Error compiling Cython file:</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">------------------------------<wbr>------------------------------</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">...</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">cdef extern from "sub.c":</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">    int sub_c(Vec)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">def sub_pyx(PETSc.Vec vec):</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">    vec.view()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">    sub_c(vec.vec)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">            ^</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">------------------------------<wbr>------------------------------</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">m3d4py.pyx:8:13: Cannot convert 'PetscVec' to Python object</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">make: *** [all] Error 1</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>==============================<wbr>==========================</div><div><br></div><div>I somehow realize that this is not correct. But comparing this to the petsc4py implementation ...</div><div><br></div><div>petscvec.pxi:</div><div><span style="background-color:rgb(255,255,255)"><span class="m_3803948213351856062Apple-tab-span" style="white-space:pre-wrap">       </span>int VecView(PetscVec,PetscViewer)</span></div><div><br></div><div>Vec.pyx:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    def view(self, Viewer viewer=None):</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">        cdef PetscViewer vwr = NULL</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">        if viewer is not None: vwr = viewer.vwr</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">        CHKERR( VecView(self.vec, vwr) )</div></div><div><br></div><div>… I do not understand why.</div><div><br></div><div>Can some explain this to me or just give me a hint where I can dig further, please?</div><div><br></div><div>And just out of pure curiosity, why isn’t the return value of VecView() in petscvec.pxi defined as <span style="font-family:Menlo;font-size:11px;background-color:rgb(255,255,255)">PetscErrorCode?</span></div><div><span style="font-family:Menlo;font-size:11px;background-color:rgb(255,255,255)">The type seems to be defined:</span></div><div><span style="font-family:Menlo;font-size:11px;background-color:rgb(255,255,255)"><br></span></div><div>libpetsc4py.pyx:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    ctypedef int PetscErrorCode</div></div><div><br></div><div>Thanks in advance,</div><div>Jaroslaw</div><div><br></div><div><div>
<div style="word-wrap:break-word"><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">—</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Dr. Jaroslaw Piwonski<br><br>Christian-Albrechts-<wbr>Universität zu Kiel<br>Institut für Informatik<br>Algorithmische Optimale Steuerung — CO2-Aufnahme des Meeres<br><a href="https://maps.google.com/?q=Christian-Albrechts-Platz+4+24118+Kiel&entry=gmail&source=g">Christian-Albrechts-Platz 4</a><br><a href="https://maps.google.com/?q=Christian-Albrechts-Platz+4+24118+Kiel&entry=gmail&source=g">24118 Kiel</a><br><br>Telefon: <a href="tel:+49%20431%208807452" value="+494318807452" target="_blank">+49 431 880-7452</a><br>Telefax: <a href="tel:+49%20431%208807618" value="+494318807618" target="_blank">+49 431 880-7618</a></div></div>
</div>
<br></div></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="http://www.caam.rice.edu/~mk51/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div>
</div></div>