# KSPBuildSolution
Builds the approximate solution in a vector provided. 
## Synopsis
```
#include "petscksp.h" 
#include "petscmat.h" 
PetscErrorCode KSPBuildSolution(KSP ksp, Vec v, Vec *V)
```
Collective


## Input Parameter

- ***ctx -*** iterative context obtained from `KSPCreate()`



## Output Parameter
Provide exactly one of

- ***v -*** location to stash solution.
- ***V -*** the solution is returned in this location. This vector is created
internally. This vector should NOT be destroyed by the user with
`VecDestroy()`.



## Notes
This routine can be used in one of two ways
```none
      KSPBuildSolution(ksp,NULL,&V);
   or
      KSPBuildSolution(ksp,v,NULL); or KSPBuildSolution(ksp,v,&v);
```

In the first case an internal vector is allocated to store the solution
(the user cannot destroy this vector). In the second case the solution
is generated in the vector that the user provides. Note that for certain
methods, such as `KSPCG`, the second case requires a copy of the solution,
while in the first case the call is essentially free since it simply
returns the vector where the solution already is stored. For some methods
like `KSPGMRES` this is a reasonably expensive operation and should only be
used in truly needed.




## See Also
 [](chapter_ksp), `KSPGetSolution()`, `KSPBuildResidual()`, `KSP`

## Level
developer

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/interface/itfunc.c.html#KSPBuildSolution">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.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex57f.F90.html">src/ksp/ksp/tutorials/ex57f.F90.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex9.c.html">src/ksp/ksp/tutorials/ex9.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ts/tutorials/ex47.c.html">src/ts/tutorials/ex47.c.html</A><BR>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/bcgs/bcgs.c.html#KSPBuildSolution_BCGS">KSPBuildSolution_BCGS in src/ksp/ksp/impls/bcgs/bcgs.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/fetidp/fetidp.c.html#KSPBuildSolution_FETIDP">KSPBuildSolution_FETIDP in src/ksp/ksp/impls/fetidp/fetidp.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/gcr/gcr.c.html#KSPBuildSolution_GCR">KSPBuildSolution_GCR in src/ksp/ksp/impls/gcr/gcr.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/gmres/agmres/agmres.c.html#KSPBuildSolution_AGMRES">KSPBuildSolution_AGMRES in src/ksp/ksp/impls/gmres/agmres/agmres.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/gmres/dgmres/dgmres.c.html#KSPBuildSolution_DGMRES">KSPBuildSolution_DGMRES in src/ksp/ksp/impls/gmres/dgmres/dgmres.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/gmres/fgmres/fgmres.c.html#KSPBuildSolution_FGMRES">KSPBuildSolution_FGMRES in src/ksp/ksp/impls/gmres/fgmres/fgmres.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/gmres/gmres.c.html#KSPBuildSolution_GMRES">KSPBuildSolution_GMRES in src/ksp/ksp/impls/gmres/gmres.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/gmres/lgmres/lgmres.c.html#KSPBuildSolution_LGMRES">KSPBuildSolution_LGMRES in src/ksp/ksp/impls/gmres/lgmres/lgmres.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/gmres/pgmres/pgmres.c.html#KSPBuildSolution_PGMRES">KSPBuildSolution_PGMRES in src/ksp/ksp/impls/gmres/pgmres/pgmres.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/impls/gmres/pipefgmres/pipefgmres.c.html#KSPBuildSolution_PIPEFGMRES">KSPBuildSolution_PIPEFGMRES in src/ksp/ksp/impls/gmres/pipefgmres/pipefgmres.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](/docs/manualpages/index.md)  
[Index of all manual pages](/docs/manualpages/singleindex.md)  
