It is not possible because KSPConvergedReasonViewFromOptions() gets its 
format directly from the command line each time it is called and 
KSPConvergedReasonViewFromOptions() is called (wildly incorrectly; no function 
that processes the options database should be called from compute code) 
directly in KSPSolve_Private.

  The correct fix would be to rework that reason view from options to use the 
style of 

  if (ksp->viewSol) PetscCall(ObjectView((PetscObject)ksp->vec_sol, 
ksp->viewerSol, ksp->formatSol));
  if (ksp->view) PetscCall(ObjectView((PetscObject)ksp, ksp->viewer, 
ksp->format));

where the check for the viewer and format from the options database is done in 
the KSPSetFromOptions() and then the check in KSPSolve_Private() is a fast 
check. 

Once this is done one could provide an API for changing the format in the code 
at any time. 

Note that PetscObjectViewFromOptions() is used extensively in PETSc but should 
be completely refactored (removed in its current form) because it does both the 
check of the options data and the viewing in one operation, instead of getting 
the information and allowing use later.

  Barry




> On Feb 8, 2023, at 2:39 AM, Pierre Jolivet <pie...@joliv.et> wrote:
> 
> Hello,
> There is not much code around using PETSC_VIEWER_FAILED, and I don’t 
> understand the following behavior.
> diff --git a/src/ksp/ksp/tutorials/ex1.c b/src/ksp/ksp/tutorials/ex1.c
> index 702467ebd51..708a2ff8c9a 100644
> --- a/src/ksp/ksp/tutorials/ex1.c
> +++ b/src/ksp/ksp/tutorials/ex1.c
> @@ -147,3 +147,5 @@ int main(int argc, char **args)
>   PetscCall(MatShift(A, 2.0));
> +  
> PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp)),
>  PETSC_VIEWER_FAILED));
>   PetscCall(KSPSolve(ksp, b, x));
> +  
> PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp))));
> 
> $ ./ex1 -ksp_converged_reason
> Linear solve converged due to CONVERGED_RTOL iterations 5
> […]
> Linear solve converged due to CONVERGED_RTOL iterations 5
> 
> How do I get the second solve to remain silent _unless_ there is a failure?
> Why is the code not doing what the documentation is stating, i.e., “use 
> PETSC_VIEWER_FAILED to only display a reason if it fails” (maybe because the 
> KSP has its own viewer but how do I retrieve it in a user-code)?
> 
> Thanks,
> Piere

Reply via email to