<div dir="ltr">Hello all, <div><br></div><div>I'm a relatively new PETSc user and am currently attempting to apply matrix-free methods to a program I wrote that uses the KSP and DMDA libraries. </div><div><br></div><div>However, I've come across an issue. When I use KSPSetDM, it requires that I set operators with KSPSetComputeOperators rather than simply setting an operator. However, the primary issue here is that when I use KSPSetComputeOperators, I attempt to compute my operators with the following function:</div><div><br></div><div><div>PetscErrorCode ComputeMatrix(KSP ksp, Mat A, Mat jac, void *user){</div><div>  PetscErrorCode ierr;</div><div>  AppCtx         *ctx = (AppCtx*)user;</div><div><br></div><div>  PetscFunctionBegin;</div><div>  ierr = MatSetType(A, MATSHELL);CHKERRQ(ierr);</div><div>  ierr = MatCreateShell(PETSC_COMM_WORLD,ctx->high-ctx->low,ctx->high-ctx->low,\</div><div>    PETSC_DETERMINE, PETSC_DETERMINE, (void*)ctx, &A);CHKERRQ(ierr);</div><div>  ierr = MatShellSetOperation(A,MATOP_MULT,(void(*)(void))MyMatMult);</div><div>    CHKERRQ(ierr);</div><div>  ierr = MatSetUp(A);CHKERRQ(ierr);</div><div><br></div><div>  ierr = MatSetType(jac, MATSHELL);CHKERRQ(ierr);</div><div>  ierr = MatCreateShell(PETSC_COMM_WORLD,ctx->high-ctx->low,ctx->high-ctx->low,\</div><div>    PETSC_DETERMINE, PETSC_DETERMINE, (void*)ctx, &jac);CHKERRQ(ierr);</div><div>  ierr = MatShellSetOperation(jac,MATOP_MULT,(void(*)(void))MyMatMult);</div><div>    CHKERRQ(ierr);</div><div>  ierr = MatSetUp(jac);CHKERRQ(ierr);</div><div><br></div><div>  PetscFunctionReturn(ierr);</div><div>}</div></div><div><br></div><div>When I try to run KSPSolve, I get the following error message:</div><div><br></div><div><div>joseph@Otto ~/petsc-3.9.3/Diffusion $ mpirun -n 1 ./diffu2matfree -x 3 -y 3</div><div>shell</div><div>[0]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------</div><div>[0]PETSC ERROR: Object is in wrong state</div><div>[0]PETSC ERROR: Must call MatXXXSetPreallocation() or MatSetUp() on argument 1 "mat" before MatMult()</div><div>[0]PETSC ERROR: See <a href="http://www.mcs.anl.gov/petsc/documentation/faq.html">http://www.mcs.anl.gov/petsc/documentation/faq.html</a> for trouble shooting.</div><div>[0]PETSC ERROR: Petsc Release Version 3.9.3, Jul, 02, 2018 </div><div>[0]PETSC ERROR: ./diffu2matfree on a arch-linux2-c-debug named Otto by joseph Thu Jul 19 17:53:00 2018</div><div>[0]PETSC ERROR: Configure options --with-cc=gcc --with-cxx=g++ --with-fc=gfortran --download-mpich --download-fblaslapack</div><div>[0]PETSC ERROR: #1 MatMult() line 2306 in /home/joseph/petsc-3.9.3/src/mat/interface/matrix.c</div><div>[0]PETSC ERROR: #2 PCApplyBAorAB() line 682 in /home/joseph/petsc-3.9.3/src/ksp/pc/interface/precon.c</div><div>[0]PETSC ERROR: #3 KSP_PCApplyBAorAB() line 304 in /home/joseph/petsc-3.9.3/include/petsc/private/kspimpl.h</div><div>[0]PETSC ERROR: #4 KSPGMRESCycle() line 152 in /home/joseph/petsc-3.9.3/src/ksp/ksp/impls/gmres/gmres.c</div><div>[0]PETSC ERROR: #5 KSPSolve_GMRES() line 234 in /home/joseph/petsc-3.9.3/src/ksp/ksp/impls/gmres/gmres.c</div><div>[0]PETSC ERROR: #6 KSPSolve() line 669 in /home/joseph/petsc-3.9.3/src/ksp/ksp/interface/itfunc.c</div><div>[0]PETSC ERROR: #7 main() line 110 in /home/joseph/petsc-3.9.3/Diffusion/diffu2matfree.cpp</div><div>[0]PETSC ERROR: PETSc Option Table entries:</div><div>[0]PETSC ERROR: -x 3</div><div>[0]PETSC ERROR: -y 3</div><div>[0]PETSC ERROR: ----------------End of Error Message -------send entire error message to petsc-maint@mcs.anl.gov----------</div><div>application called MPI_Abort(MPI_COMM_WORLD, 73) - process 0</div></div><div><br></div><div><br></div><div>Does anybody know what's going on here? I've also tried to make the algorithm work without KSPSetDM and KSPSetComputeOperators, but my MatMult step is dependent on DMDA, which is no longer used for the vectors in the KSP if I don't use KSPSetDM. I've attached my code in full if there are any questions as to the structure of my code. </div><div><br></div><div>Thanks, </div><div><br></div><div>Joseph Essman</div></div>