Discussion:
[petsc-users] Compute initial guess multiple times
Filippo Leonardi
2015-10-09 10:04:53 UTC
Permalink
So, I want to solve a system Ax = b multiple times, reuse A with different bs.
It is natural to set differents initial guesses each time for iterative
solvers.

And
KSPSetComputeInitialGuess(ksp,ComputeGuess,&user);
is what PETSc provide for setting the Guess.

I am unsure if I am making something wrong, but it seems to me that this
function is called only once the matrix is first setted up. I want to be
called each time.

See my modified ex32.c for comparison.

./ex32 -pc_type mg -pc_mg_type full -ksp_type fgmres -ksp_monitor_short -
pc_mg_levels 3 -mg_coarse_pc_factor_shift_type nonzero -
ksp_initial_guess_nonzero

Thanks.

Best,
Filippo
Barry Smith
2015-10-09 18:49:25 UTC
Permalink
The current model is a bit wonky.


At the first call to KSPSolve after a call to KSPSetComputeOperators the computematrix, computerhs, and computeinitialguess are called.

In subsequent calls to KSPSolve ONLY the computerhs is called.

So for example

KSPSetComputeOperators()
KSPSolve -- computematrix, rhs, initial guess called
KSPSolve --- computerhs called
KSPSetComputerOperators()
KSPSolve -- computematrix, rhs, initial guess called


Note that the KSPSetCompute...() functions are just one particular way of using KSP. You can also use the "standard" interface

Set values into matrix and rhs and initial guess (call KSPSetInitialGuessNonzero)
KSPSolve(ksp,b,x)
change values in b, this would use the current values in x as the initial guess for the next solve
KSPSolve(ksp,b,x)
change values in b and x
KSPSolve()
Change values in matrix and b and x (for initial guess)
KSPSolve()

This gives you exact detailed control over when and what you set in initial guesses.

Is there a particular reason for your problem that it is better to use the KSPSetCompute...() interface?

Barry

Note I wrote the KSPSetCompute...() interface so that PCMG could "build its own" coarse grid matrix values without the user needing to set values for all the levels before calling KSPSolve.



> On Oct 9, 2015, at 5:04 AM, Filippo Leonardi <***@sam.math.ethz.ch> wrote:
>
> So, I want to solve a system Ax = b multiple times, reuse A with different bs.
> It is natural to set differents initial guesses each time for iterative
> solvers.
>
> And
> KSPSetComputeInitialGuess(ksp,ComputeGuess,&user);
> is what PETSc provide for setting the Guess.
>
> I am unsure if I am making something wrong, but it seems to me that this
> function is called only once the matrix is first setted up. I want to be
> called each time.
>
> See my modified ex32.c for comparison.
>
> ./ex32 -pc_type mg -pc_mg_type full -ksp_type fgmres -ksp_monitor_short -
> pc_mg_levels 3 -mg_coarse_pc_factor_shift_type nonzero -
> ksp_initial_guess_nonzero
>
> Thanks.
>
> Best,
> Filippo<ex32.c>
Leonardi Filippo
2015-10-09 18:58:57 UTC
Permalink
Actually I used to use KSPSolve directly, then I've started using KSPSetCompute... because (if I remember correctly) (as you mention) I wanted KSPComputeMatrix to compute all the coarse matrices for me.

Now I switched to completely use algebraic multigrid or galerkin computed matrices, so I can revert back to use the KSPSolve directly, but, for testing purposes, it may be good to keep being able to use PCMG.

My question now is: can I use SetComputeMatrix() and then call KSPSolve(ksp,b,x)? (I forgot if i need to use b,x = NULL or not when using the compute matrix routine.)
________________________________________
Da: Barry Smith [***@mcs.anl.gov]
Inviato: venerdì 9 ottobre 2015 20.49
A: Leonardi Filippo
Cc: petsc-***@mcs.anl.gov
Oggetto: Re: [petsc-users] Compute initial guess multiple times

The current model is a bit wonky.


At the first call to KSPSolve after a call to KSPSetComputeOperators the computematrix, computerhs, and computeinitialguess are called.

In subsequent calls to KSPSolve ONLY the computerhs is called.

So for example

KSPSetComputeOperators()
KSPSolve -- computematrix, rhs, initial guess called
KSPSolve --- computerhs called
KSPSetComputerOperators()
KSPSolve -- computematrix, rhs, initial guess called


Note that the KSPSetCompute...() functions are just one particular way of using KSP. You can also use the "standard" interface

Set values into matrix and rhs and initial guess (call KSPSetInitialGuessNonzero)
KSPSolve(ksp,b,x)
change values in b, this would use the current values in x as the initial guess for the next solve
KSPSolve(ksp,b,x)
change values in b and x
KSPSolve()
Change values in matrix and b and x (for initial guess)
KSPSolve()

This gives you exact detailed control over when and what you set in initial guesses.

Is there a particular reason for your problem that it is better to use the KSPSetCompute...() interface?

Barry

Note I wrote the KSPSetCompute...() interface so that PCMG could "build its own" coarse grid matrix values without the user needing to set values for all the levels before calling KSPSolve.



> On Oct 9, 2015, at 5:04 AM, Filippo Leonardi <***@sam.math.ethz.ch> wrote:
>
> So, I want to solve a system Ax = b multiple times, reuse A with different bs.
> It is natural to set differents initial guesses each time for iterative
> solvers.
>
> And
> KSPSetComputeInitialGuess(ksp,ComputeGuess,&user);
> is what PETSc provide for setting the Guess.
>
> I am unsure if I am making something wrong, but it seems to me that this
> function is called only once the matrix is first setted up. I want to be
> called each time.
>
> See my modified ex32.c for comparison.
>
> ./ex32 -pc_type mg -pc_mg_type full -ksp_type fgmres -ksp_monitor_short -
> pc_mg_levels 3 -mg_coarse_pc_factor_shift_type nonzero -
> ksp_initial_guess_nonzero
>
> Thanks.
>
> Best,
> Filippo<ex32.c>
Barry Smith
2015-10-09 19:06:52 UTC
Permalink
> On Oct 9, 2015, at 1:58 PM, Leonardi Filippo <***@sam.math.ethz.ch> wrote:
>
> Actually I used to use KSPSolve directly, then I've started using KSPSetCompute... because (if I remember correctly) (as you mention) I wanted KSPComputeMatrix to compute all the coarse matrices for me.
>
> Now I switched to completely use algebraic multigrid or galerkin computed matrices, so I can revert back to use the KSPSolve directly, but, for testing purposes, it may be good to keep being able to use PCMG.
>
> My question now is: can I use SetComputeMatrix() and then call KSPSolve(ksp,b,x)? (I forgot if i need to use b,x = NULL or not when using the compute matrix routine.)

What about providing the ComputeMatrix() but NOT the computerrhs or computeinitialguess? Make sure to call KSPSetInitialGuessNonzero() so KSP doesn't zero out the x you pass in).

Barry

> ________________________________________
> Da: Barry Smith [***@mcs.anl.gov]
> Inviato: venerdì 9 ottobre 2015 20.49
> A: Leonardi Filippo
> Cc: petsc-***@mcs.anl.gov
> Oggetto: Re: [petsc-users] Compute initial guess multiple times
>
> The current model is a bit wonky.
>
>
> At the first call to KSPSolve after a call to KSPSetComputeOperators the computematrix, computerhs, and computeinitialguess are called.
>
> In subsequent calls to KSPSolve ONLY the computerhs is called.
>
> So for example
>
> KSPSetComputeOperators()
> KSPSolve -- computematrix, rhs, initial guess called
> KSPSolve --- computerhs called
> KSPSetComputerOperators()
> KSPSolve -- computematrix, rhs, initial guess called
>
>
> Note that the KSPSetCompute...() functions are just one particular way of using KSP. You can also use the "standard" interface
>
> Set values into matrix and rhs and initial guess (call KSPSetInitialGuessNonzero)
> KSPSolve(ksp,b,x)
> change values in b, this would use the current values in x as the initial guess for the next solve
> KSPSolve(ksp,b,x)
> change values in b and x
> KSPSolve()
> Change values in matrix and b and x (for initial guess)
> KSPSolve()
>
> This gives you exact detailed control over when and what you set in initial guesses.
>
> Is there a particular reason for your problem that it is better to use the KSPSetCompute...() interface?
>
> Barry
>
> Note I wrote the KSPSetCompute...() interface so that PCMG could "build its own" coarse grid matrix values without the user needing to set values for all the levels before calling KSPSolve.
>
>
>
>> On Oct 9, 2015, at 5:04 AM, Filippo Leonardi <***@sam.math.ethz.ch> wrote:
>>
>> So, I want to solve a system Ax = b multiple times, reuse A with different bs.
>> It is natural to set differents initial guesses each time for iterative
>> solvers.
>>
>> And
>> KSPSetComputeInitialGuess(ksp,ComputeGuess,&user);
>> is what PETSc provide for setting the Guess.
>>
>> I am unsure if I am making something wrong, but it seems to me that this
>> function is called only once the matrix is first setted up. I want to be
>> called each time.
>>
>> See my modified ex32.c for comparison.
>>
>> ./ex32 -pc_type mg -pc_mg_type full -ksp_type fgmres -ksp_monitor_short -
>> pc_mg_levels 3 -mg_coarse_pc_factor_shift_type nonzero -
>> ksp_initial_guess_nonzero
>>
>> Thanks.
>>
>> Best,
>> Filippo<ex32.c>
>
Loading...