[petsc-users] Examples on solving linear systems using GMRES and/or matrix-free method

Xiang Hao haoxiang at yahoo.cn
Wed Jun 15 12:05:23 CDT 2011


Hi,

I modify the KSP ex1 to a matrix free version, everything works. Basically,
I define a usermult function and pass it to
MatShellSetOperation<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatShellSetOperation.html#MatShellSetOperation>,
as shown in
http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatShellSetOperation.html

PetscErrorCode <http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Sys/PetscErrorCode.html#PetscErrorCode>
usermult(Mat <http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/Mat.html#Mat>,Vec
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec>,Vec
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec>);

ierr = MatCreateShell
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatCreateShell.html#MatCreateShell>(comm
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Sys/comm.html#comm>,m,n,M,N,ctx,&A);

ierr = MatShellSetOperation
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatShellSetOperation.html#MatShellSetOperation>(A,MATOP_MULT,(void(*)(void))usermult);


Now, I have a problem when I try to change the above to a c++
version(I want to use a class).

Let's see I define the following class and it has a function usermult

Class classA{

PetscErrorCode <http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Sys/PetscErrorCode.html#PetscErrorCode>
usermult(Mat <http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/Mat.html#Mat>,Vec
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec>,Vec
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec>);

....

};

PetscErrorCode <http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Sys/PetscErrorCode.html#PetscErrorCode>
classA::usermult(Mat
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/Mat.html#Mat>,Vec
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec>,Vec
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Vec/Vec.html#Vec>)

{

....

}

If I want to use the matrix free method, in the main function, I tried
several different ways to pass the function usermult to
MatShellSetOperation, but they all fails. Does anyone know how to do
this?

int main(int argc, char ** argv)

{

classA ob;

...


ierr = MatCreateShell
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatCreateShell.html#MatCreateShell>(comm
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Sys/comm.html#comm>,m,n,M,N,ctx,&A);

ierr = MatShellSetOperation
<http://www.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-3.0.0/docs/manualpages/Mat/MatShellSetOperation.html#MatShellSetOperation>(A,MATOP_MULT,(void(*)(void))ob.usermult);
?


}


On Wed, Jun 8, 2011 at 5:38 AM, Matthew Knepley <knepley at gmail.com> wrote:

> On Tue, Jun 7, 2011 at 10:20 PM, Xiang Hao <haoxiang at yahoo.cn> wrote:
>
>>  Hi all,
>>
>> I need to solve a PDE ( basically a Poisson's equation with Neumann
>> boundary condition ), and I solved it using a steepest descent method, which
>> is very slow. Now I want to solve the PDE using GMRES. In addition, I do
>> have the matrix A, but I have a function which computes Ax, so I also need
>> to use the matrix free method.
>>
>> I am very new to PETSc, so I am looking for examples on solving linear
>> systems using GMRES and/or matrix-free method.
>>
>
> You can look at SNES ex5 for the Poisson equation (after setting lambda =
> 0.0). SNES ex19 also shows how to use geometric Multigrid.
> Its not hard to do this for Poisson.
>
>   Thanks,
>
>      Matt
>
>
>> Thanks,
>> Xiang
>>
>>
>>
>
>
> --
> What most experimenters take for granted before they begin their
> experiments is infinitely more interesting than any results to which their
> experiments lead.
> -- Norbert Wiener
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20110615/fd2bfda7/attachment.htm>


More information about the petsc-users mailing list