[petsc-users] KSP and matrix-free matrix (shell)

Dave May dave.mayhem23 at gmail.com
Thu Oct 18 12:35:40 CDT 2018


On Thu, 18 Oct 2018 at 17:57, Florian Lindner <mailinglists at xgm.de> wrote:

> Hello,
>
> I try to use the KSP solver package together with a shell matrix:
>
>
>   MyContext mycontext; // an empty struct, not sure it it's needed?
>   Mat s;
>   ierr = MatCreateShell(PETSC_COMM_WORLD, size, size, PETSC_DECIDE,
> PETSC_DECIDE, &mycontext, &s);
>   ierr = MatShellSetOperation(s, MATOP_MULT, (void(*)(void))usermult);
> CHKERRQ(ierr);
>
> To simulate a meaningfull usermult, I use MatMult on an actual existing
> matrix of same dimensions:
>
> extern PetscErrorCode usermult(Mat m ,Vec x, Vec y)
> {
>   PetscErrorCode ierr = 0;
>   ierr = MatMult(matrix, x, y);
>   printf("Call\n");
>   return ierr;
> }
>
> Btw, what is the significance of the Mat m argument here?


m is your shell matrix. You should be calling

MatShellGetContext(m,(void**)&myctx);

https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatShellGetContext.html

to get your user context inside usermult to retrieve any data you need for
the mat-vec product. Where the hell is the variable "matrix"? Is it a
global variable?? If yes - don't do that.


>
> matrix is created like:
>
>   ierr = MatCreate(PETSC_COMM_WORLD, &matrix); CHKERRQ(ierr);
>   ierr = MatSetSizes(matrix, size, size, PETSC_DECIDE, PETSC_DECIDE);
> CHKERRQ(ierr);
>   ierr = MatSetFromOptions(matrix); CHKERRQ(ierr);
>   ierr = MatSetUp(matrix); CHKERRQ(ierr);
>
>
>   MatMult(s, b, x);
>
> works. The usermult function is called.
>
> But trying to use a KSP gives an error:
>
>   KSP solver;
>   KSPCreate(PETSC_COMM_WORLD, &solver);
>   KSPSetFromOptions(solver);
>   KSPSetOperators(solver, s, s);
>
>
> error:
>
> [0]PETSC ERROR: --------------------- Error Message
> --------------------------------------------------------------
> [0]PETSC ERROR: See
> http://www.mcs.anl.gov/petsc/documentation/linearsolvertable.html for
> possible LU and Cholesky solvers
> [0]PETSC ERROR: Could not locate a solver package. Perhaps you must
> ./configure with --download-<package>
> [0]PETSC ERROR: See http://www.mcs.anl.gov/petsc/documentation/faq.html
> for trouble shooting.
> [0]PETSC ERROR: Petsc Release Version 3.9.3, unknown
> [0]PETSC ERROR: ./a.out on a arch-linux2-c-opt named asaru by lindnefn Thu
> Oct 18 17:39:52 2018
> [0]PETSC ERROR: Configure options --with-debugging=0 COPTFLAGS="-O3
> -march=native -mtune=native" CXXOPTFLAGS="-O3 -march=native -mtune=native"
> FOPTFLAGS="-O3 -march=native -mtune=native" --download-petsc4py
> --download-mpi4py --with-mpi-dir=/opt/mpich
> [0]PETSC ERROR: #1 MatGetFactor() line 4328 in
> /home/lindnefn/software/petsc/src/mat/interface/matrix.c
> [0]PETSC ERROR: #2 PCSetUp_ILU() line 142 in
> /home/lindnefn/software/petsc/src/ksp/pc/impls/factor/ilu/ilu.c
> [0]PETSC ERROR: #3 PCSetUp() line 923 in
> /home/lindnefn/software/petsc/src/ksp/pc/interface/precon.c
> [0]PETSC ERROR: #4 KSPSetUp() line 381 in
> /home/lindnefn/software/petsc/src/ksp/ksp/interface/itfunc.c
> [0]PETSC ERROR: #5 KSPSolve() line 612 in
> /home/lindnefn/software/petsc/src/ksp/ksp/interface/itfunc.c
>
> Do I need to MatShellSetOperations additional operations? Like
> MATOP_ILUFACTOR? How can I know what operations to implement?
>
> Best Thanks,
> Florian
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20181018/ac4733d4/attachment.html>


More information about the petsc-users mailing list