help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

## Re: [Help-gsl] Matrix memory deallocation problem

 From: James Bergstra Subject: Re: [Help-gsl] Matrix memory deallocation problem Date: Thu, 6 Jul 2006 16:44:13 +0200 User-agent: Mutt/1.5.11+cvs20060403

```On Thu, Jul 06, 2006 at 03:05:23PM +0100, Wei Cheng wrote:
> I have a matrix-matrix multiplication function based on BLAS matrix-matrix
> mutiplication function,just for the ease of my own use as follow:
>
> //******************************************************************************
>    gsl_matrix* matrix_matrix_mul(CBLAS_TRANSPOSE_t TransA,CBLAS_TRANSPOSE_t
> TransB,const gsl_matrix* X1,const gsl_matrix* X2)
>   //matrix matrix multiplication, return the result
>   {
>      int N1=(*X1).size1;
>      int N2=(*X2).size1;
>      int D1=(*X1).size2;
>      int D2=(*X2).size2;
>      gsl_matrix* Y;// to store the product in Y
>
> //determine the dimension of product matrix Y
>      if(TransB==CblasTrans)
>      //X2 is transposed
>         Y=gsl_matrix_alloc (N1,N2);
>      else if(TransA==CblasTrans)
>      //X1 is transposed
>         Y=gsl_matrix_alloc (D1,D2);
>      else
>      //No transpose
>         Y=gsl_matrix_alloc (N1,D2);
>
> //calling BLAS general matrix multiplication function , product is stored in
> Y
>      gsl_blas_dgemm (TransA, TransB, 1.0, X1, X2, 0.0, Y);
>
>      return Y;
>
>   }
> //*****************************************************************************
> The function returns the matrix product  of X1 and X2. I notice that
> whenever we create matrix we have to free the memory with gsl_matrix_free.
> However in  the above function I can't free Y before I return it and i am
> not able to free Y after return statement.
> Memory leak problem occurs when I call the above function 5000000 times in a
> loop like this:
>
> //************************************************************************************
> void testing()
> {
> gsl_matrix* A=rand(50,100);//create random 50 by 100 matrix
> gsl_matrix* B=rand(100,50);//create random 100 by 50 matrix
>
> for(int i=0;i<5000000;i++)
> matrix_matrix_mul(CblasNoTrans ,CblasNoTrans ,A ,B );//calculate the project
> of matrix A and B}
> //*************************************************************************************
>
> The problem I am having is that during the loop, my RAM usage shoot up and
> eventually run out. I am certain the problem is with the the deallocation of
> Y in mutiplication function.
>
> Can anybody help me with this matrix deallocation problem. This is just a
> test, in my application the matrices are thousands by thousands.
If you want good performance, then you should allocate the output matrix
just once, outside this loop.  It's faster and there's no deallocation
problem.

Eg.

void testing()
{
gsl_matrix* A=rand(50,100);//create random 50 by 100 matrix
gsl_matrix* B=rand(100,50);//create random 100 by 50 matrix
gsl_matrix* C=gsl_matrix_alloc(50,50);

for(int i=0;i<5000000;i++)
{
gsl_blas_dgemm (CblasNoTrans ,CblasNoTrans, 1.0, A, B, 0.0, C);
}
}

--
http://www-etud.iro.umontreal.ca/~bergstrj

```

reply via email to

 [Prev in Thread] Current Thread [Next in Thread]