[petsc-users] PC shell destroy

Alfredo J Duarte Gomez aduarteg at utexas.edu
Fri Nov 19 11:39:29 CST 2021


Good morning,

Sorry for picking this back up such a long time after the initial response,
but I am now in a good place to implement this change after thoroughly
testing the preconditioner shell alone.

I have been looking for resources but it is still not clear to me how to
use the MATSHELL for this purpose. Do I have to create my own MATSHELL that
is basically just the solution and the shift itself, or at least operations
that do this? Or is the matshell simply a dummy object that will allow me
to interface the solution and ashift?

In either of this cases I am still not sure how to begin this process, are
there any helpful examples that I could model  the MATSHELL from? KSP
example ex15.c was incredibly helpful for the creation of my PCSHELL so it
would be very helpful if there is something similar for the MATSHELL, and
passing the ashift.

Thank you,

-Alfredo

On Thu, Sep 30, 2021 at 5:09 PM Barry Smith <bsmith at petsc.dev> wrote:

>
>   Alfredo,
>
>   I think the best approach for you to use is to have your own MATSHELL
> and your own PCSHELL. You will use your MATSHELL as the second matrix
> argument to TSSetIJacobian(). It should record the current x and the
> current shift.
>
>   Your PCSHELL will then, in PCSetUp(), get access to the current x and
> the current shift from your MATSHELL and build itself. In other words most
> of your
>
> / Get necessary objects from TS context
>   TSGetTime(ts,&time);
>   TSGetApplicationContext(ts,&user);
>   TSGetSolution(ts,&X);
>   TSGetTimeStep(ts,&dt);
>   TSGetStepNumber(ts, &stepi);
>   TSGetDM(ts,&da);
>
>   tdt = time+dt;
>   // Update preconditioner context with current values
>   ierr = ShellPCSetUp(pc,da,tdt,dt,X,user);CHKERRQ(ierr);
>
>
>  code will disappear and you won't need to mess with the internals of the
> TS (getting current dt etc) at all. What you need is handed off to your
> TSSetIJacobian() function which will stick it into your MATSHELL. So nice
> and clean code.
>
>   Regarding the PCDestroy() for your PCSHELL. It only gets called when the
> PC is finally destroyed which is when the TS is destroy. So if building
> your PC in PCSetUp() requires creating new objects you should destroy any
> previous ones when you create the new ones, hence "lost" objects won't
> persist in the code.
>
>   Barry
>
>
>
>
>
> On Sep 30, 2021, at 4:14 PM, Alfredo J Duarte Gomez <aduarteg at utexas.edu>
> wrote:
>
> Good afternoon PETSC team,
>
> I am currently developing an application for PETSC in which I use my own
> preconditioner with a PCSHELL.
>
> I have successfully set all the functions and the performance of the
> preconditioner is good. I am using this PCSHELL within a TS object, and it
> is imperative that the objects in the PCSHELL context are freed every time
> since the memory requirements of those are large.
>
> I have set up the Preconditioner before the TS starts with the following
> block of code:
>
> ----------------------------------------------------------------------------------------------------
>
> ierr = PCSetType(pc,PCSHELL);CHKERRQ(ierr);
>   ierr = ShellPCCreate(&shell);CHKERRQ(ierr);
>   ierr = PCShellSetApply(pc,MatrixFreePreconditioner);CHKERRQ(ierr);
>   ierr = PCShellSetContext(pc,shell);CHKERRQ(ierr);
>   ierr = PCShellSetDestroy(pc,ShellPCDestroy);CHKERRQ(ierr);
>   ierr = PCShellSetName(pc,"MyPreconditioner");CHKERRQ(ierr);
>   ierr = ShellPCSetUp(pc,da,0.0,dt,u,user);CHKERRQ(ierr);
>   ierr = TSSetPreStep(ts,PreStep);CHKERRQ(ierr);
>
>
> ------------------------------------------------------------------------------------------------
>
> The shell context is then updated by using the following code within the
> TSPreStep function:
>
> ---------------------------------------------------------------------------
>
>  ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
>   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
>   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
>
>   // Get necessary objects from TS context
>   TSGetTime(ts,&time);
>   TSGetApplicationContext(ts,&user);
>   TSGetSolution(ts,&X);
>   TSGetTimeStep(ts,&dt);
>   TSGetStepNumber(ts, &stepi);
>   TSGetDM(ts,&da);
>
>   tdt = time+dt;
>   // Update preconditioner context with current values
>   ierr = ShellPCSetUp(pc,da,tdt,dt,X,user);CHKERRQ(ierr);
> ---------------------------------------------------------------------------
>
> I have set up the necessary code in the function ShellPCDestroy to free
> the objects within this context, however I am unsure when/if this function
> is called automatically. Do I have to free the context myself after every
> step? How would I call the function myself?
>
> I am running out of memory after a few steps, and I think this shell
> context is the culprit.
>
> In addition to that, is it possible to get what is called the "ashift" dF/dU
> + a*dF/dU_t in this function from the TS object?
>
> https://petsc.org/release/docs/manualpages/TS/TSSetIJacobian.html
>
> I need it as an input for my preconditioner (currrently hardcoded for
> TSBEULER where ashift is always 1/dt).
>
> Thank you,
>
> -Alfredo
>
> --
> Alfredo Duarte
> Graduate Research Assistant
> The University of Texas at Austin
>
>
>

-- 
Alfredo Duarte
Graduate Research Assistant
The University of Texas at Austin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20211119/514d8a5b/attachment-0001.html>


More information about the petsc-users mailing list