[petsc-users] reuse an assembled matrix when a linear problem is solved with KSP and DM

Barry Smith bsmith at mcs.anl.gov
Tue Sep 17 22:06:41 CDT 2013


On Sep 17, 2013, at 10:01 PM, Roc Wang <pengxwang at hotmail.com> wrote:

> 
> 
> > Subject: Re: [petsc-users] reuse an assembled matrix when a linear problem is solved with KSP and DM
> > From: bsmith at mcs.anl.gov
> > Date: Tue, 17 Sep 2013 20:45:03 -0500
> > CC: petsc-users at mcs.anl.gov
> > To: pengxwang at hotmail.com
> > 
> > 
> > On Sep 17, 2013, at 8:35 PM, Roc Wang <pengxwang at hotmail.com> wrote:
> > 
> > > Hello,
> > > 
> > > I am trying to solve a linear problem by using KSPSolve and DM. In the example /src/ksp/ksp/examples/tutorials/ex45.c, the Matrix and the RHS vector are computed by calling KSPSetComputeRHS and KSPSetComputeOperators in which functions ComputeRHS() and ComputeMatrix() are called and the matrix is assembled in ComputeMatrix(). 
> > > 
> > > Since the linear system is solved in an iteration, it will save time to reuse the matrix assembled in ComputeMatrix(). That is the ComputeMatrix() is called only once, but ComputeRHS() is called in each iteration.
> > 
> > What do you need mean by an "iteration" here? Do you mean one iteration of a KSP solvers, of which several are needed to produce the approximate solution or something else? 
> >
>   sorry for the misunderstanding. By iteration, I mean the linear system needs to be solved many times with different RHS, the RHS is changed in each time the linear sytem is solved. It's not the iteration that the KSP takes. 
> > The ComputeRHS() function is only called ONCE for each KSPSolve (even though KSP may take several iterations to solve the linear system).
> > 
>    So, I need to call KSPSetComputeRHS() each time I want to change the RHS, right?
> > The ComputeMatrix() function is actually only called ONCE period, unless you call KSPSetComputeOperators() again, even if you call KSPSolve() several times it uses the same matrix until you call KSPSetComputerOperators() again.
> > 
>    Thus, I just need to call KSPSetComputerOperators() once when the matrix is assembled the first time to solve the linear system, after that only KSPSetComputeRHS(), when I want to change RHS vector.

   No. Each new KSPSolve() will automatically call the ComputeRHS function again. You do NOT need to call the KSPSetComputeRHS() function more then once.

   Barry

Yes the KSPSetComputeRHS() and KSPSetComputeOperators() functions behave slightly differently.


> > > Can anyone tell me what should I do to keep the assembled matrix?
> > > 
> > > I am thinking not to use KSPSetComputeOperators for assembling the matrix, but use DMCreateMatrix() alternatively. Am I right?
> > 
> > That is a completely orthogonal issue; DMCreateMatrix() only gives you an empty sparse matrix with the right nonzero structure for the mesh associated with DM. You still need to compute its entries yourself, possibly in a ComputeMatrix function you write.
>   Yes, I plan to compute the entries after calling  DMCreateMatrix(),  just like calling MatSetValuesStencil() in for loop and then MatAssemblyBegin(), MatAssemblyBegin().
>   
> 
> > 
> > Barry
> > 
> > > 
> > > Any hits would be appreciated very much. Thanks alot.
> > > 
> > > 
> >



More information about the petsc-users mailing list