<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Helvetica, sans-serif; ">
<div>The DM keeps track of the local vectors created by DMGetLocalVector(). So, if your routine gets called a million times with exactly one DMGetLocalVector() followed by one call to DMRestoreLocalVector(), then only one vector is created.</div>
<div><br>
</div>
<div>Shri</div>
<div><br>
</div>
<span id="OLK_SRC_BODY_SECTION">
<div style="font-family:Calibri; font-size:11pt; text-align:left; color:black; BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt">
<span style="font-weight:bold">From: </span>Miguel Angel Salazar de Troya <<a href="mailto:salazardetroya@gmail.com">salazardetroya@gmail.com</a>><br>
<span style="font-weight:bold">Date: </span>Wed, 19 Nov 2014 14:02:10 -0600<br>
<span style="font-weight:bold">To: </span>barry smith <<a href="mailto:bsmith@mcs.anl.gov">bsmith@mcs.anl.gov</a>><br>
<span style="font-weight:bold">Cc: </span>"<a href="mailto:petsc-users@mcs.anl.gov">petsc-users@mcs.anl.gov</a>" <<a href="mailto:petsc-users@mcs.anl.gov">petsc-users@mcs.anl.gov</a>><br>
<span style="font-weight:bold">Subject: </span>Re: [petsc-users] PetscFunctionBegin, -malloc_dump and C++ classes with PETSc objects<br>
</div>
<div><br>
</div>
<blockquote id="MAC_OUTLOOK_ATTRIBUTION_BLOCKQUOTE" style="BORDER-LEFT: #b5c4df 5 solid; PADDING:0 0 0 5; MARGIN:0 0 0 5;">
<div dir="ltr">You were right. I was forgetting a DMRestoreLocalVector(). Thanks. If we are creating local vectors every time we call functions such as the RHSFunction of a TS implementation, which is called many times in the TS integration, will this be a
problem in terms of performance? I've noticed that it might be. This is the way it is implemented in some TS. I wanted to double check with you guys before I figure out a way to create just one local vector that is re used in functions such as RHSFunction.
<div><br>
</div>
<div>Miguel</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Tue, Nov 18, 2014 at 12:54 PM, Barry Smith <span dir="ltr">
<<a href="mailto:bsmith@mcs.anl.gov" target="_blank">bsmith@mcs.anl.gov</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class=""><br>
> On Nov 18, 2014, at 11:54 AM, Miguel Angel Salazar de Troya <<a href="mailto:salazardetroya@gmail.com">salazardetroya@gmail.com</a>> wrote:<br>
><br>
> PetscFinalize() is inside of the class destructor (the last line of the destructor), so when the object goes out of scope, the class destructor is called and PetscFinalize() as well. Is it better to have PetscFinalize() outside of the destructor and call
the destructor explicitly before?<br>
<br>
</span> It shouldn't really matter.<br>
<br>
My guess is you must be missing calling a destroy or restore on one of the PETSc objects.<br>
<span class="HOEnZb"><font color="#888888"><br>
Barry<br>
</font></span>
<div class="HOEnZb">
<div class="h5"><br>
><br>
> Miguel<br>
><br>
> On Tue, Nov 18, 2014 at 11:32 AM, Barry Smith <<a href="mailto:bsmith@mcs.anl.gov">bsmith@mcs.anl.gov</a>> wrote:<br>
><br>
> > On Nov 18, 2014, at 11:19 AM, Miguel Angel Salazar de Troya <<a href="mailto:salazardetroya@gmail.com">salazardetroya@gmail.com</a>> wrote:<br>
> ><br>
> > Hi<br>
> ><br>
> > I'm implementing a problem using the TS. Most of my functions are methods inside of a class, except for the callbacks (to form the RHS and the TS monitor), which are outside of the class, although in the same .C file where the class methods are implemented.
For these callbacks I followed the network example:<br>
> ><br>
> > <a href="https://bitbucket.org/petsc/petsc/src/a614f7369d93d476173b8fc6bf2463276dcbdb3a/src/snes/examples/tutorials/network/pflow/pf.c?at=master" target="_blank">
https://bitbucket.org/petsc/petsc/src/a614f7369d93d476173b8fc6bf2463276dcbdb3a/src/snes/examples/tutorials/network/pflow/pf.c?at=master</a><br>
> ><br>
> > Therefore, the callbacks have the PetscFunctionBegin at the beginning and PetscFunctionReturn(0) at the end. My problems come when I run the program with -malloc_dump and I get a lot of unfreed memory. Inspecting the output I see that the line of my code
where the memory is allocated corresponds with the line when PetscFunctionBegin is called.<br>
><br>
> This is normal. We cannot register the exact line the memory allocated, only the location of the PETScFunctionBegin;<br>
><br>
><br>
> > Later in the file, I see that the function DMGetLocalVector() is called within a petsc internal routine (at the file dmget.c). I also call this routine in my callback methods few lines after PetscFunctionBegin. The procedure that I follow to use the local
vectors is as the one in the network example. For vectors that I want to modify this is:<br>
> ><br>
> > ierr = DMGetLocalVector(networkdm,&localX);CHKERRQ(ierr);<br>
> > ierr = DMGlobalToLocalBegin(networkdm,X,INSERT_VALUES,localX);CHKERRQ(ierr);<br>
> > ierr = DMGlobalToLocalEnd(networkdm,X,INSERT_VALUES,localX);CHKERRQ(ierr);<br>
> > ierr = VecGetArray(localX,&xarr);CHKERRQ(ierr);<br>
> ><br>
> > Modify values in xarr<br>
> ><br>
> > ierr = VecRestoreArray(localX,&xarr);CHKERRQ(ierr);<br>
> > ierr = DMLocalToGlobalBegin(networkdm,localX,INSERT_VALUES,X);CHKERRQ(ierr);<br>
> > ierr = DMLocalToGlobalEnd(networkdm,localX,INSERT_VALUES,X);CHKERRQ(ierr);<br>
> > ierr = DMRestoreLocalVector(networkdm,&localX);CHKERRQ(ierr);<br>
> ><br>
> > One last thing that I think it might be a issue here is how I destroy the petsc objects. I create the petsc objects within a class. For instance, the class has a petsc vector that later passes to the TS object to get the solution. To destroy the petsc objects,
I use the class destructor, where at the end I call PetscFinalize() Inside the class I pass the callbacks to the TS routines that need them (e.g. TSSetRHSFunction() ) I can compile the code and run it, but many memory allocations are not freed. What can be
the issue here? Do you know of an example using C++ classes to implement PETSc methods? Thanks in advance.<br>
><br>
> Do you call the class destructor yourself explicitly before the PetscFinalize()? You need to, otherwise the class may not be destroyed until after PetscFinalize() and hence the PETSc objects won't be freed when you call PetscFinalize().<br>
><br>
> You also need to make sure that you destroy ALL PETSc objects, if you miss even one PETSc object, since the objects have references to each other it may be that many PETSc objects do not get freed and hence -malloc_dump shows many objects still alive.<br>
><br>
> Barry<br>
><br>
> ><br>
> > Miguel<br>
> ><br>
> > --<br>
> > Miguel Angel Salazar de Troya<br>
> > Graduate Research Assistant<br>
> > Department of Mechanical Science and Engineering<br>
> > University of Illinois at Urbana-Champaign<br>
> > (217) 550-2360<br>
> > <a href="mailto:salaza11@illinois.edu">salaza11@illinois.edu</a><br>
> ><br>
><br>
><br>
><br>
><br>
> --<br>
> Miguel Angel Salazar de Troya<br>
> Graduate Research Assistant<br>
> Department of Mechanical Science and Engineering<br>
> University of Illinois at Urbana-Champaign<br>
> (217) 550-2360<br>
> <a href="mailto:salaza11@illinois.edu">salaza11@illinois.edu</a><br>
><br>
<br>
</div>
</div>
</blockquote>
</div>
<br>
<br clear="all">
<div><br>
</div>
-- <br>
<div class="gmail_signature">
<div dir="ltr"><font face="verdana,sans-serif"><b>Miguel Angel Salazar de Troya</b></font><span><font color="#888888"><br>
<font face="arial,helvetica,sans-serif">Graduate Research Assistant<br>
Department of Mechanical Science and Engineering<br>
</font>University of Illinois at Urbana-Champaign<br>
(217) 550-2360<br>
<a href="mailto:salaza11@illinois.edu" target="_blank">salaza11@illinois.edu</a></font></span>
<div><br>
</div>
</div>
</div>
</div>
</blockquote>
</span>
</body>
</html>