<div dir="ltr"><div dir="ltr">On Wed, Jul 22, 2020 at 9:32 AM Zhang, Hong via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov">petsc-users@mcs.anl.gov</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
> On Jul 22, 2020, at 7:03 AM, Adolfo Rodriguez <<a href="mailto:adantra@gmail.com" target="_blank">adantra@gmail.com</a>> wrote:<br>
> <br>
> I am trying to replace the non-linear solver in a flow simulation problem where the matrix sparsity can change during the iterations. I tried successfully to create the matrix within the FormJacobian function but I have a memory leak which I don't know how to fix.<br>
> <br>
> My FormJacobian function is something like this:<br>
> <br>
> .. FormJacobian( ..){<br>
> matrixconstruction(); // returns csr matrix in c++ regular vectors<br>
> MatSeqAIJSetPreallocationCSR ...;<br>
> //loop over the entries and <br>
> MatSetValues;<br>
> MatAssemblyBegin();<br>
> MatAssemblyEnd();<br>
> return 0;<br>
> }<br>
<br>
Yes, the leak is caused by the new matrix created every time FormJacobian is called. The working matrix for SNES needs to be allocated only once and passed to SNES by using<br>
<br>
SNESSetJacobian(SNES snes,Mat Amat,Mat Pmat,PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx)<br>
<br>
In your FormJacobian(SNES snes,Vec x,Mat A,Mat B,void *ctx), you just need to copy the csr matrix to the matrix A, which has already been allocated.<br>
<br>
The working matrix should be destroyed after SNESSolve().<br></blockquote><div><br></div><div>Hong, what is the best way to wipe out the existing structure, preallocate, and set new values?</div><div><br></div><div> Thanks,</div><div><br></div><div> Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Hong (Mr.)<br>
<br>
<br>
> This approach works for small problems but, when dealing with large ones I observe a memory leak. I am pretty sure that It is related to the fact that I am creating new matrices every time FormJacobian is called but I don't know how to destroy it between iterations.<br>
> <br>
> Any suggestions?<br>
> <br>
> Regards,<br>
> <br>
> Adolfo<br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div>