Kevin

   A couple of different things are at play here producing the unexpected 
results.

   I have created a merge request 
https://gitlab.com/petsc/petsc/-/merge_requests/7179 clarifying why the results 
obtained from the KSPGetResidualHistory() and KSPGetIterationNumber() can be 
different in the docs. 

   I also fixed a couple of locations of KSPLogResidual() (in gmres and fgmres) 
that resulted in extra incorrect logging of the history.

   In summary, with the "standard" textbook Krylov methods, one expects numIts 
= nEntries - 1, but this need not be the case for advanced Krylov methods (like 
those with inner iterations or pipelining) or under exceptional circumstances 
like the use of CG in trust region methods.

   Barry



> On Jan 11, 2024, at 1:20 PM, Kevin G. Wang <[email protected]> wrote:
> 
> Hi Barry,
> 
> Thanks for your help!!
> 
> I have checked that in KSPSetResidualHistory, "reset" is set to PETSC_TRUE. I 
> did a few quick tests after reading your message. There seems to be some 
> patterns between "numIts" (given by KSPGetIterationNumber) and "nEntries" 
> (given by KSPGetResidualHistory):
> 
> 1. With gmres or fgmres as the solver:
>   - If the number of iterations (until error tolerance is met) is small, like 
> 20 - 30, indeed as you said, numIts = nEntries - 1.
>   - if the number of iterations is large, this is no longer true. I have a 
> case where nEntries = 372, numIts = 360.
> 2. With bcgsl, it looks like numIts = 2*(nEntries - 1).
> 3. With ibcgs, nEntries = 0, while numIts is nonzero.
> 
> In all these tests, I have set the preconditioner to "none".
> 
> My code (where the KSP functions are called) is here: 
> https://github.com/kevinwgy/m2c/blob/main/LinearSystemSolver.cpp
> 
> I am using PETSc 3.12.4.
> 
> Thanks!
> Kevin
> 
> 
> On Thu, Jan 11, 2024 at 12:26 PM Barry Smith <[email protected] 
> <mailto:[email protected]>> wrote:
>> 
>>    Trying again.
>> 
>>     Normally, numIts would be one less than nEntries since the initial 
>> residual is computed (and stored in the history) before any iterations.
>> 
>>     Is this what you are seeing or are you seeing other values for the two?
>> 
>>     I've started a run of the PETSc test suite that compares the two values 
>> for inconsistencies for all tests to see if I can find any problems.
>> 
>>     Barry
>> 
>>     Also note the importance of the reset value in KSPSetResidualHistory() 
>> which means the values will not match when reset is PETSC_FALSE.
>> 
>>> On Jan 10, 2024, at 7:09 PM, Kevin G. Wang <[email protected] 
>>> <mailto:[email protected]>> wrote:
>>> 
>>> Hello everyone!
>>> 
>>> I am writing a code that uses PETSc/KSP to solve linear systems. I just 
>>> realized that after running "KSPSolve(...)", the number of iterations given 
>>> by 
>>> 
>>> KSPGetIterationNumber(ksp, &numIts)
>>> 
>>> is *different* from the size of the residual history given by
>>> 
>>> KSPGetResidualHistory(ksp, NULL, &nEntries);
>>> 
>>> That is, "numIts" is not equal to "nEntries". Is this expected, or a bug in 
>>> my code? (I thought they should be the same...)
>>> 
>>> I have tried several pairs of solvers and preconditioners (e.g., fgmres & 
>>> bjacobi, ibcgs & bjacobi). This issue happens to all of them.
>>> 
>>> Thanks!
>>> Kevin
>>> 
>>> --
>>> Kevin G. Wang, Ph.D.
>>> Associate Professor
>>> Kevin T. Crofton Department of Aerospace and Ocean Engineering
>>> Virginia Tech
>>> 1600 Innovation Dr., VTSS Rm 224H, Blacksburg, VA 24061
>>> Office: (540) 231-7547  |  Mobile: (650) 862-2663 
>>> URL: https://www.aoe.vt.edu/people/faculty/wang.html 
>>> Codes: https://github.com/kevinwgy
>> 
> 
> 
> --
> Kevin G. Wang, Ph.D.
> Associate Professor
> Kevin T. Crofton Department of Aerospace and Ocean Engineering
> Virginia Tech
> 1600 Innovation Dr., VTSS Rm 224H, Blacksburg, VA 24061
> Office: (540) 231-7547  |  Mobile: (650) 862-2663 
> URL: https://www.aoe.vt.edu/people/faculty/wang.html 
> Codes: https://github.com/kevinwgy

Reply via email to