[petsc-users] KSP and VecDestroy

Barry Smith bsmith at mcs.anl.gov
Fri Oct 7 21:10:12 CDT 2011


  Did you call KSPDestroy()? References to two of the vectors are retained by the KSP object, only when the KSP object is destroyed are those references released and hence those two vectors destroyed.

  You can use -malloc -malloc_dump to see all the memory that was allocated by PETSc and not freed.


On Oct 7, 2011, at 8:01 PM, Shiyuan wrote:

> 
> 
> On Fri, Oct 7, 2011 at 7:49 PM, Jed Brown <jedbrown at mcs.anl.gov> wrote:
> On Fri, Oct 7, 2011 at 19:48, Shiyuan <gshy2014 at gmail.com> wrote:
> Hi, 
>    I want to track down an memory problem and have something I don't understand. 
> In the following part, I create four vectors and destroy them. If I don't call KSPSolve, log tells me four Vec are created and four Vec are destroyed which I expect. But if I call KSPSolve, log tells me four Vec are created but only two are destroyed in that Stage? Does KSP refer the two vectors somewhere inside which make VecDestroy cannot destroy them?
> 
> Yes
>  
> Does it create memory leak becase of that?
> 
> No, they are destroyed when you call KSPDestroy().
>  
> Thanks. 
> 
>     ierr=PetscLogStagePush(memoryWatch);CHKERRQ(ierr);
>     MatGetVecs(*(pCSolverNeu->pK),&x0,&rhs); VecDuplicate(x0,&(workVec[0]));VecDuplicate(x0,&(workVec[1]));
>     VecSetRandom(x0,randomctx);   
>     VecCopy(x0,workVec[0]);
>     MatMult(*(pMGDataVec[0]->pA),x0,rhs);
>    
>     ierr=KSPSolve(ksp,rhs, workVec[0]);CHKERRQ(ierr);
>     
>     ierr=VecDestroy(&x0);CHKERRQ(ierr);
>     ierr=VecDestroy(&rhs);CHKERRQ(ierr);
>     ierr=VecDestroy(&workVec[0]);CHKERRQ(ierr);
>     ierr=VecDestroy(&workVec[1]);CHKERRQ(ierr);
>     ierr=PetscLogStagePop();CHKERRQ(ierr);
> 
> But in the -log_summary, these two Vecs seems not be counted as destroyed, which makes the number of created Vec is two more than the number of destroyed Vec. This makes me think that I forget to free memory. Is it supposed to work that way? Is the memory part  in the log meant to be a way to detect the memory leak problem? Thanks.  



More information about the petsc-users mailing list