[petsc-users] On MatDestroy() in Fortran
Barry Smith
bsmith at petsc.dev
Sat Jul 30 14:13:33 CDT 2022
I assume you are calling KSPSetOperators() before each new KSPSolve() so that it knows to solve a new system?
If the above is true with your current code it should thus have two matrices in memory at the most since every new call to KSPSetOperators() will trigger freeing the previous.
In C you could do
KSPSetOperators(ksp,NULL,NULL)
immediately after the KSPSolve() (at the same time that you destroy the matrix) this will cause KSP to decrease the reference count on the matrix so it will be freed and you will only ever have one matrix in memory at a time.
In fortran
KSPSetOperators(ksp,PETSC_NULL_MAT,PETSC_NULL_MAT)
might work.
Depending on your individual systems and the preconditioners you are using there might be a good amount of reuse of preconditioner information from each solve that could decrease the run time. This would not happen if you reuse the KSP for multiple different linear systems, then each solve requires a complete rebuild of the preconditioner. So you may be trading speed for memory usage and might consider using a computing system with more memory.
Barry
> On Jul 30, 2022, at 2:42 PM, Edoardo alinovi <edoardo.alinovi at gmail.com> wrote:
>
> Barry, Jacob,
>
> Thank you very much for your clarification, I think the reference counting is what is preventing matDestroy() from releasing the matrix memory.
>
> Just to give you a bit of context, in my CFD code I am solving momentum, pressure, plus two more equations for the turbulence model (k and omega). I have seen that allocating a matrix for each equation and keep it in memory it's a bit optimistic for larger problems, so what I am trying to do is the following:
>
> do itime=1,ntimes
>
> momentum equation:
>
> allocate the matrix
> fill the matrix
> ksp-solve()
> matDestroy()
>
> pressure equation
> allocate the matrix
> fill the matrix
> ksp-solve()
> matDestroy()
>
> k equation
> allocate the matrix
> fill the matrix
> ksp-solve()
> matDestroy()
>
> omega equation
> allocate the matrix
> fill the matrix
> ksp-solve()
> matDestroy()
>
> enddo
>
> Matrices for momentum, pressure, k and omega have the same size but belong to different objects so that's why I have 4 of them :)
>
> The idea I have in mind is to destroy A after its use for each equation ,so that the next equation matrix to be solved can pick up the memory freed from the previous one. At the end of the story I'll just allocate space for 1 matrix and not 4, or at least this is what I am trying to achieve.
>
> The thing is I do not destroy the ksp, so maybe that detail is preventing my code from working as intended!
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20220730/3aeb4463/attachment.html>
More information about the petsc-users
mailing list