You can use PETSc functions to allocate and free memory and then run with -malloc_debug and you will get a printout of memory used and any unfreed memory.
Mark

On Thu, Sep 30, 2021 at 4:14 PM Alfredo J Duarte Gomez <aduarteg@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