<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""></div><div>PetscObjectGetReference() but when a destroy routine is called, the pointer value is nulled so you cannot do </div><div><br class=""></div><div>MatDestroy(A);</div><div>PetscObjectGetReference(A);</div><div><br class=""></div><div>You could something like</div><div><br class=""></div><div>B = A</div><div><br class=""></div><div><div>MatDestroy(A);</div><div>PetscObjectGetReference(B);</div><div class=""><br class=""></div><div class="">Note if the object is actually destroyed (memory freed because the reference count got to zero) then calling PetscObjectGetReference(); will crash on it. </div><div class=""><br class=""></div><div class="">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. </div><div class=""><br class=""></div><div class="">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. </div><div class=""><br class=""></div><div class="">Sounds like your best choice is either KSPReset() or KSPDestroy()/KSPCreate()</div><div class=""><br class=""></div><div class=""><br class=""></div></div><div><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class="">On Jul 31, 2022, at 5:22 AM, Edoardo alinovi <<a href="mailto:edoardo.alinovi@gmail.com" class="">edoardo.alinovi@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Hello Barry,</div><div class=""><br class=""></div><div class="">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.</div><div dir="auto" class=""><br class=""></div><div class="">I attach here the memory traces of my code:</div><div class="">- baseline (each equation keep the matrix and does not destroy KSP)</div><div class="">- With KSPSetOperators(ksp, PETSC_NULL_MAT,PETSC_NULL_MAT)</div><div class="">- With KSPDestroy() before MatDestroy()</div><div class=""><br class=""></div><div class="">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 :)</div><div class=""><br class=""></div><div dir="auto" class="">Is there any way to get the reference count of an object just to double check it is not referenced anymore?</div><div dir="auto" class=""><br class=""></div><div class="">Thank you</div></div><br class=""><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il Sab 30 Lug 2022, 21:59 Barry Smith <<a href="mailto:bsmith@petsc.dev" target="_blank" class="">bsmith@petsc.dev</a>> ha scritto:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class=""><div class=""><br class=""></div> Yes, as you describe below. <br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jul 30, 2022, at 4:31 PM, Edoardo alinovi <<a href="mailto:edoardo.alinovi@gmail.com" rel="noreferrer" target="_blank" class="">edoardo.alinovi@gmail.com</a>> wrote:</div><br class=""><div class=""><div dir="ltr" class="">Hello Barry,<div class=""><br class=""></div><div class=""><i class=""><b class=""> I assume you are calling KSPSetOperators() before each new KSPSolve() so that it knows to solve a new system?</b></i><br class=""></div><div class=""><i class=""><b class=""><br class=""></b></i></div><div class="">Yes, I do.</div><div class=""><br class=""></div><div class="">This looks like a good trick: KSPSetOperators(ksp,PETSC_NULL_MAT,PETSC_NULL_MAT)</div><div class=""><br class=""></div><div class="">So you would do: </div><div class=""><br class=""></div><div class="">1) KSPSetOperators(ksp,A,A)<br class=""></div><div class=""><br class=""></div><div class="">2) KSPSolve(myksp, myrhs, mysol)<br class=""></div><div class=""><br class=""></div><div class="">3) KSPSetOperators(ksp,PETSC_NULL_MAT,PETSC_NULL_MAT)<br class=""></div><div class=""><br class=""></div><div class="">4) MatDestroy(A)</div><div class=""><br class=""></div><div class="">Am I right?</div><div class=""><br class=""></div><div class="">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. </div><div class=""><br class=""></div><div class="">Thanks a lot! </div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div>
</div></blockquote></div><br class=""></div></blockquote></div>
<span id="cid:f_l6942nd80"><baseline.PNG></span><span id="cid:f_l6942nds1"><KSPDestroy.PNG></span><span id="cid:f_l6942ndt2"><setOperatorsNULL.PNG></span></div></blockquote></div><br class=""></body></html>