[petsc-dev] SNESSetKSP and SNESDestroy

Pierre Jolivet pierre.jolivet at enseeiht.fr
Thu May 30 08:22:20 CDT 2019

> On 30 May 2019, at 2:42 PM, Matthew Knepley <knepley at gmail.com> wrote:
> On Thu, May 30, 2019 at 6:08 AM Pierre Jolivet via petsc-dev <petsc-dev at mcs.anl.gov <mailto:petsc-dev at mcs.anl.gov>> wrote:
> Hello,
> I’m doing a continuation loop with something like:
> KSP ksp;
> KSPSetUp(ksp);
> while(cond) {
>         SNES snes;
>         SNESCreate(&snes);
>         SNESSetKSP(snes, &ksp);
>         SNESSolve(snes);
>         SNESDestroy(&snes);
> }
> For the first iteration, everything is OK.
> After that, it looks like SNESDestroy is messing up with my outer-defined KSP(*), i.e., for the code to work I need to comment out the SNESDestroy.
> Is this the expected behavior? (if not, I’ll provide a MWE)
> How to fix this properly, without having leaks by not destroying the inner-defined SNESes?
> Thanks in advance,
> Pierre
> (*) to the outer-defined KSP is attached a PCFIELDSPLIT, first SNESSolve, everything OK:
> PC Object: 1 MPI processes
>   type: fieldsplit
>     FieldSplit with MULTIPLICATIVE composition: total splits = 2
> Second SNESSolve:
> [0]PETSC ERROR: PCFieldSplitSetDefaults() Unhandled case, must have at least two fields, not 1
> I believe SNESDestroy() calls SNESReset(), which calls KSPReset(), which is wiping out your information.

OK, I thought there was a reference count in XXXReset, but it looks like it is just in the XXXDestroy. That explains why.

> This pattern is in a lot of places. Destroy calls Reset because it is natural to reuse the code which tears down
> the internal structures. Reset calls reset on subobjects because we also want to tear down their internal structures (usually).
> Reset wouldn't do what we want on resizing if we left subobjects unreset. However, this chain is causing subobjects to
> be wiped clean on Destroy, so that we cannot reuse subobjects.
> It would not even work if you change the Destroy to Reset in the continuation loop. However, I think you can hoist all
> the SNES setup outside the loop. If you are changing sizes, then the KSP needs to be wiped out anyway.

Indeed, when I turn on mesh adaptation, I wipe out the KSP myself and set everything again, so no problem.

> If not, then
> you do not need to wipe out the SNES.

Now, this is more of a limitation of our DSL. I don’t expose PETSc SNES type to users, we just do SNESSolve(my Mat+KSP) in the loop, which Create, Solve, and Destroy.
But I guess I’ll have to split those steps.


>   Thanks,
>      Matt
> -- 
> What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.
> -- Norbert Wiener
> https://www.cse.buffalo.edu/~knepley/ <http://www.cse.buffalo.edu/~knepley/>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20190530/e33d9a8b/attachment.html>

More information about the petsc-dev mailing list