<html><body><div style="font-family: times new roman, new york, times, serif; font-size: 12pt; color: #000000"><div>Thank you!<br></div><div><br></div><div><br></div><hr id="zwchr"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>De: </b>"Matthew Knepley" <knepley@gmail.com><br><b>À: </b>"Nicolas Pozin" <nicolas.pozin@inria.fr><br><b>Cc: </b>"PETSc" <petsc-users@mcs.anl.gov><br><b>Envoyé: </b>Mercredi 5 Août 2015 13:38:20<br><b>Objet: </b>Re: [petsc-users] problem with MatShellGetContext<br><div><br></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Aug 5, 2015 at 4:15 AM, Nicolas Pozin <span dir="ltr"><<a href="mailto:nicolas.pozin@inria.fr" target="_blank">nicolas.pozin@inria.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div style="font-family:times new roman,new york,times,serif;font-size:12pt;color:#000000"><div>Hello,<br></div><div><br></div><div>I'm trying to solve a system with a matrix free operator and through conjugate gradient method. <br></div><div>To make ideas clear, I set up the following simple example (I am using petsc-3.6) and I get this error message :</div></div></div></blockquote><div><br></div><div>Yes, you are passing a C++ function userMult, so the compiler sticks "this" in as the first argument. We do not</div><div>recommend this kind of wrapping.</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><div style="font-family:times new roman,new york,times,serif;font-size:12pt;color:#000000"><div><div>"</div><div>[0]PETSC ERROR: --------------------- Error Message ------------------------------------<br>[0]PETSC ERROR: Invalid argument!<br>[0]PETSC ERROR: Wrong type of object: Parameter # 1!<br>[0]PETSC ERROR: ------------------------------------------------------------------------<br>[0]PETSC ERROR: Petsc Release Version 3.4.3, Oct, 15, 2013 <br>[0]PETSC ERROR: See docs/changes/index.html for recent updates.<br>[0]PETSC ERROR: See docs/faq.html for hints about trouble shooting.<br>[0]PETSC ERROR: See docs/index.html for manual pages.<br>[0]PETSC ERROR: ------------------------------------------------------------------------<br>[0]PETSC ERROR: ./test on a ubuntu_release named pl-59080 by npozin Wed Aug 5 10:55:26 2015<br>[0]PETSC ERROR: Libraries linked from /home/npozin/Felisce_libraries/petsc_3.4.3/ubuntu_release/lib<br>[0]PETSC ERROR: Configure run at Wed Jul 22 16:18:36 2015<br>[0]PETSC ERROR: Configure options PETSC_ARCH=ubuntu_release --with-cxx=g++ --with-fc=gfortran --with-cc=gcc --with-x=0 --download-openmpi --download-f-blas-lapack --download-superlu --download-superlu_dist --with-superlu_dist=1 --download-metis --download-mumps --download-parmetis --with-superlu_dist=1 --download-boost --with-boost=1 --download-scalapack with-external-packages-dir=/home/npozin/Felisce_libraries/petsc_3.4.3/packages<br>[0]PETSC ERROR: ------------------------------------------------------------------------<br>[0]PETSC ERROR: MatShellGetContext() line 202 in /home/npozin/Felisce_libraries/petsc_3.4.3/src/mat/impls/shell/shell.c<br>End userMult<br>[0]PETSC ERROR: MatMult() line 2179 in /home/npozin/Felisce_libraries/petsc_3.4.3/src/mat/interface/matrix.c<br>[0]PETSC ERROR: KSP_MatMult() line 204 in /home/npozin/Felisce_libraries/petsc_3.4.3/include/petsc-private/kspimpl.h<br>[0]PETSC ERROR: KSPSolve_CG() line 219 in /home/npozin/Felisce_libraries/petsc_3.4.3/src/ksp/ksp/impls/cg/cg.c<br>[0]PETSC ERROR: KSPSolve() line 441 in /home/npozin/Felisce_libraries/petsc_3.4.3/src/ksp/ksp/interface/itfunc.c<br>"</div><div><br></div><div>I don't understand where the problem comes from with the matrix argument of MatShellGetContext.</div><div>Any idea on what I do wrong?</div><div><br></div><div>Thanks a lot,</div>Nicolas</div><div><br></div><div><br></div><div><br></div><div>#include <iostream><br>#include <petscksp.h><br><div><br></div>using namespace std;<br><div><br></div><br>typedef struct {<br> int val;<br>} MyCtx;<br><div><br></div><br>class ShellClass {<br> Mat matShell;<br> KSP ksp;<br> PC pc;<br> Vec x;<br> Vec b;<br> <br>public:<br> void userMult(Mat Amat, Vec x, Vec y) {<br> cout << "Inside userMult" << endl;<br> <br> MyCtx *ctx;<br> MatShellGetContext(Amat, (void *) ctx);<br> <br> cout << "End userMult" << endl;<br> }<br><div><br></div> void solveShell() {<br> // context<br> MyCtx *ctx = new MyCtx;<br> ctx->val = 42;<br><div><br></div> // pc<br> PCCreate(PETSC_COMM_WORLD, &pc);<br> PCSetType(pc, PCNONE);<br><div><br></div> // ksp<br> KSPCreate(PETSC_COMM_WORLD, &ksp);<br> KSPSetType(ksp, KSPCG);<br> KSPSetPC(ksp, pc);<br> KSPSetFromOptions(ksp);<br><div><br></div> // matshell<br> int m = 10;<br> int n = 10;<br> MatCreateShell(PETSC_COMM_WORLD, m, n, PETSC_DETERMINE, PETSC_DETERMINE, ctx, &matShell);<br> MatShellSetOperation(matShell, MATOP_MULT, (void(*)(void))&ShellClass::userMult);<br><div><br></div><br> // create vectors<br> MatCreateVecs(matShell, &x, 0);<br> VecDuplicate(x, &b);<br> VecSet(b, 1.);<br><div><br></div> // set operators<br> KSPSetOperators(ksp, matShell, matShell);<br><div><br></div> // solve (call to userMult)<br> KSPSolve(ksp, b, x);<br> }<br>};<br><div><br></div><br><div><br></div>int main(int argc, char** argv) {<br> PetscInitialize(&argc, &argv, NULL, NULL);<br> <br> ShellClass foo;<br> foo.solveShell();<br> <br> PetscFinalize();<br> return 0;<br>}<br><div><br></div></div></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">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></div>
</blockquote><div><br></div></div></body></html>