# KSPSetType
Builds the `KSP` datastructure for a particular `KSPType` 
## Synopsis
```
#include "petscksp.h" 
PetscErrorCode KSPSetType(KSP ksp, KSPType type)
```
Logically Collective on ksp


## Input Parameters

- ***ksp  -*** the Krylov space context
- ***type -*** a known method



## Options Database Key

- ***-ksp_type  <method> -*** Sets the method; use -help for a list  of available methods (for instance, cg or gmres)



## Notes
See "petsc/include/petscksp.h" for available methods (for instance, `KSPCG` or `KSPGMRES`).

Normally, it is best to use the `KSPSetFromOptions()` command and
then set the `KSP` type from the options database rather than by using
this routine.  Using the options database provides the user with
maximum flexibility in evaluating the many different Krylov methods.
The `KSPSetType()` routine is provided for those situations where it
is necessary to set the iterative solver independently of the command
line or options database.  This might be the case, for example, when
the choice of iterative solver changes during the execution of the
program, and the user's application is taking responsibility for
choosing the appropriate method.  In other words, this routine is
not for beginners.




## Developer Note
`KSPRegister()` is used to add Krylov types to `KSPList` from which they are accessed by `KSPSetType()`.


## See Also
 [](chapter_ksp), `PCSetType()`, `KSPType`, `KSPRegister()`, `KSPCreate()`, `KSP`

## Level
intermediate

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

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/impls/stag/tutorials/ex2.c.html">src/dm/impls/stag/tutorials/ex2.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/impls/stag/tutorials/ex3.c.html">src/dm/impls/stag/tutorials/ex3.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/impls/stag/tutorials/ex4.c.html">src/dm/impls/stag/tutorials/ex4.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/impls/stag/tutorials/ex8.c.html">src/dm/impls/stag/tutorials/ex8.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/pc/tutorials/ex1.c.html">src/ksp/pc/tutorials/ex1.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/pc/tutorials/ex2.c.html">src/ksp/pc/tutorials/ex2.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex100.c.html">src/ksp/ksp/tutorials/ex100.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex100f.F90.html">src/ksp/ksp/tutorials/ex100f.F90.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex27.c.html">src/ksp/ksp/tutorials/ex27.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex52.c.html">src/ksp/ksp/tutorials/ex52.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex52f.F90.html">src/ksp/ksp/tutorials/ex52f.F90.html</A><BR>


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


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