<html><body><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>"</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><br>using namespace std;<br><br><br>typedef struct {<br>  int val;<br>} MyCtx;<br><br><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><br>  void solveShell() {<br>    // context<br>    MyCtx *ctx = new MyCtx;<br>    ctx->val = 42;<br><br>    // pc<br>    PCCreate(PETSC_COMM_WORLD, &pc);<br>    PCSetType(pc, PCNONE);<br><br>    // ksp<br>    KSPCreate(PETSC_COMM_WORLD, &ksp);<br>    KSPSetType(ksp, KSPCG);<br>    KSPSetPC(ksp, pc);<br>    KSPSetFromOptions(ksp);<br><br>    // 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><br><br>    // create vectors<br>    MatCreateVecs(matShell, &x, 0);<br>    VecDuplicate(x, &b);<br>    VecSet(b, 1.);<br><br>    // set operators<br>    KSPSetOperators(ksp, matShell, matShell);<br><br>    // solve (call to userMult)<br>    KSPSolve(ksp, b, x);<br>  }<br>};<br><br><br><br>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><br></div></div></body></html>