[petsc-dev] [petsc-maint] Memory leak in some usage of MatAXPY

Barry Smith bsmith at mcs.anl.gov
Fri Oct 17 14:57:04 CDT 2014


> On Oct 17, 2014, at 2:33 PM, Hong <hzhang at mcs.anl.gov> wrote:
> 
> Barry,
> Current implementation of MatAXPY_MPIAIJ() (and other matrix types)
> for SUBSET_NONZERO_PATTERN creates maps
> xtoy and XtoY and attaches these maps to matrix Y, which cannot be
> reused and is unnecessarily complicated (I wrote it many years ago).
> 
> A simple way for this case is calling MatAXPY_Basic()
> which adds aX to Y via MatSetValues() without changing data structure of Y.

   I’m afraid that it will be horrifically slow since it will be constantly reallocating for new entries.

  What about keeping your custom code but simply freeing the maps at the end of the MatAXPY instead of attaching them?

  Barry

> 
> If you agree, I like make this change:
> 
> $ git diff src/mat/utils/axpy.c
> diff --git a/src/mat/utils/axpy.c b/src/mat/utils/axpy.c
> index 23caac7..c629c9c 100644
> --- a/src/mat/utils/axpy.c
> +++ b/src/mat/utils/axpy.c
> @@ -35,7 +35,7 @@ PetscErrorCode  MatAXPY(Mat Y,PetscScalar a,Mat
> X,MatStructure str)
> 
> -  if (Y->ops->axpy) {
> +  if (Y->ops->axpy && str != SUBSET_NONZERO_PATTERN) {
>     ierr = (*Y->ops->axpy)(Y,a,X,str);CHKERRQ(ierr);
>   } else {
>     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
> 
> 
> and remove xtoy and XtoY from struct Mat_xxx.
> 
> This fixes the memory leak reported by Jose.
> 
> Hong
> 
> ---------- Forwarded message ----------
> From: Hong <hzhang at mcs.anl.gov>
> Date: Thu, Oct 16, 2014 at 12:52 PM
> Subject: Re: [petsc-maint] Memory leak in some usage of MatAXPY
> To: "Jose E. Roman" <jroman at dsic.upv.es>
> Cc: petsc-maint Help <petsc-maint at mcs.anl.gov>, Carmen Campos
> <mccampos at dsic.upv.es>
> 
> 
> Jose:
> I can repeat the leak with your code. I'll investigate and fix it on
> maint. I will let you know once I'm done.
> 
> Hong
> 
>> We are experiencing a memory leak in MatAXPY, appearing only in some specific usages:
>> - with more than one process
>> - with SUBSET_NONZERO_PATTERN
>> - with several calls to MatAXPY
>> 
>> The problem appears in master and maint, and also in petsc-3.4.
>> 
>> The situation is the following: imagine you want to accumulate two matrices onto a third one. The third one is chosen to have the sparse pattern containing the pattern of the other two (so that SUBSET_NONZERO_PATTERN can be used). Then calling MatAXPY twice with subset+subset gives a memory leak, but different+subset does not.
>> 
>> I am attaching a sample code that reproduces the problem. It has the command-line options:
>> -different: use DIFFERENT_NONZERO_PATTERN for the first MatAXPY (instead of subset)
>> -skip: omit the second MatAXPY
>> 
>> $ mpirun -np 2 ./testMatAXPY                    --> memory leak
>> $ mpirun -np 2 ./testMatAXPY -different         --> ok
>> $ mpirun -np 2 ./testMatAXPY -skip              --> ok
>> $ mpirun -np 2 ./testMatAXPY -different -skip   --> ok
>> 
>> Jose
>> 




More information about the petsc-dev mailing list