[petsc-users] On MatDestroy() in Fortran
Barry Smith
bsmith at petsc.dev
Sun Jul 31 11:19:32 CDT 2022
PetscObjectGetReference() but when a destroy routine is called, the pointer value is nulled so you cannot do
MatDestroy(A);
PetscObjectGetReference(A);
You could something like
B = A
MatDestroy(A);
PetscObjectGetReference(B);
Note if the object is actually destroyed (memory freed because the reference count got to zero) then calling PetscObjectGetReference(); will crash on it.
The KSP has all kinds of stuff in it that depend a great deal on the KSPType and PCType selected, these can use a large amount of memory.
You could also try KSPReset() instead of the KSPDestroy()/KSPCreate() pair. The reset attempts to clear out most of the data in the KSP so this might be as good as the Destroy/Create pair.
Sounds like your best choice is either KSPReset() or KSPDestroy()/KSPCreate()
> On Jul 31, 2022, at 5:22 AM, Edoardo alinovi <edoardo.alinovi at gmail.com> wrote:
>
> Hello Barry,
>
> I tested what we discussed yesterday and I would say the KSPDestroy works good, while the trick of setting the matrix to NULL in KSPSetOperators does not.
>
> I attach here the memory traces of my code:
> - baseline (each equation keep the matrix and does not destroy KSP)
> - With KSPSetOperators(ksp, PETSC_NULL_MAT,PETSC_NULL_MAT)
> - With KSPDestroy() before MatDestroy()
>
> Clearly, the last one is a winner, while the first two look pretty similar! Any possible explanation? I can happily destroy KSP, but the other one is cleaner to implement :)
>
> Is there any way to get the reference count of an object just to double check it is not referenced anymore?
>
> Thank you
>
> Il Sab 30 Lug 2022, 21:59 Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>> ha scritto:
>
> Yes, as you describe below.
>
>> On Jul 30, 2022, at 4:31 PM, Edoardo alinovi <edoardo.alinovi at gmail.com <mailto:edoardo.alinovi at gmail.com>> wrote:
>>
>> Hello Barry,
>>
>> I assume you are calling KSPSetOperators() before each new KSPSolve() so that it knows to solve a new system?
>>
>> Yes, I do.
>>
>> This looks like a good trick: KSPSetOperators(ksp,PETSC_NULL_MAT,PETSC_NULL_MAT)
>>
>> So you would do:
>>
>> 1) KSPSetOperators(ksp,A,A)
>>
>> 2) KSPSolve(myksp, myrhs, mysol)
>>
>> 3) KSPSetOperators(ksp,PETSC_NULL_MAT,PETSC_NULL_MAT)
>>
>> 4) MatDestroy(A)
>>
>> Am I right?
>>
>> I'll have a try and I'll try to destroy the ksp of each equation as well. The second is more tedious because I'll have to reconstruct it every time. I'll do some tests to see if that helps.
>>
>> Thanks a lot!
>>
>>
>>
>
> <baseline.PNG><KSPDestroy.PNG><setOperatorsNULL.PNG>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20220731/478657f9/attachment.html>
More information about the petsc-users
mailing list