:orphan:
# KSPMonitorSet
Sets an ADDITIONAL function to be called at every iteration to monitor the residual/error etc. 
## Synopsis
```
#include "petscksp.h" 
#include "petscmat.h" 
PetscErrorCode KSPMonitorSet(KSP ksp, PetscErrorCode (*monitor)(KSP, PetscInt, PetscReal, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
```
Logically Collective


## Input Parameters

- ***ksp -*** iterative context obtained from `KSPCreate()`
- ***monitor -*** pointer to function (if this is NULL, it turns off monitoring
- ***mctx    -*** [optional] context for private data for the
monitor routine (use `NULL` if no context is desired)
- ***monitordestroy -*** [optional] routine that frees monitor context
(may be `NULL`)



## Calling Sequence of `monitor`
```none
PetscErrorCode  monitor(KSP ksp, PetscInt it, PetscReal rnorm, void *mctx)
```

- ***ksp -*** iterative context obtained from `KSPCreate()`
- ***it -*** iteration number
- ***rnorm -*** (estimated) 2-norm of (preconditioned) residual
- ***mctx  -*** optional monitoring context, as set by `KSPMonitorSet()`



## Calling Sequence of `monitordestroy`
```none
PetscErrorCode destroy(void *mctx)
```


## Options Database Keys

- ***-ksp_monitor               -*** sets `KSPMonitorResidual()`
- ***-ksp_monitor draw          -*** sets `KSPMonitorResidualDraw()` and plots residual
- ***-ksp_monitor draw::draw_lg -*** sets `KSPMonitorResidualDrawLG()` and plots residual
- ***-ksp_monitor_pause_final   -*** Pauses any graphics when the solve finishes (only works for internal monitors)
- ***-ksp_monitor_true_residual -*** sets `KSPMonitorTrueResidual()`
- ***-ksp_monitor_true_residual draw::draw_lg -*** sets `KSPMonitorTrueResidualDrawLG()` and plots residual
- ***-ksp_monitor_max           -*** sets `KSPMonitorTrueResidualMax()`
- ***-ksp_monitor_singular_value -*** sets `KSPMonitorSingularValue()`
- ***-ksp_monitor_cancel -*** cancels all monitors that have
been hardwired into a code by
calls to `KSPMonitorSet()`, but
does not cancel those set via
the options database.





## Notes
The default is to do nothing.  To print the residual, or preconditioned
residual if `KSPSetNormType`(ksp,`KSP_NORM_PRECONDITIONED`) was called, use
`KSPMonitorResidual()` as the monitoring routine, with a `PETSCVIEWERASCII` as the
context.

Several different monitoring routines may be set by calling
`KSPMonitorSet()` multiple times; all will be called in the
order in which they were set.


## Fortran Notes
Only a single monitor function can be set for each `KSP` object


## See Also
 [](ch_ksp), `KSPMonitorResidual()`, `KSPMonitorCancel()`, `KSP`

## Level
beginner

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/interface/itfunc.c.html#KSPMonitorSet">src/ksp/ksp/interface/itfunc.c</A>

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex2f.F90.html">src/ksp/ksp/tutorials/ex2f.F90</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex42.c.html">src/ksp/ksp/tutorials/ex42.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex9.c.html">src/ksp/ksp/tutorials/ex9.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/snes/tutorials/ex30.c.html">src/snes/tutorials/ex30.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex47.c.html">src/ts/tutorials/ex47.c</A><BR>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/ksp/ksp/interface/itfunc.c)


[Index of all KSP routines](index.md)  
[Table of Contents for all manual pages](/manualpages/index.md)  
[Index of all manual pages](/manualpages/singleindex.md)  
