[petsc-users] Is it possible to free extra memory after matassemble?

Barry Smith bsmith at mcs.anl.gov
Fri Feb 18 08:26:23 CST 2011


On Feb 18, 2011, at 8:20 AM, Gong Ding wrote:

> Hi,
> I had added following code to aij.c in the function MatAssemblyEnd_SeqAIJ after pack the matrix elements.
> Just allocate three new array and copy data to them.
> This skill is heavily used in macro MatSeqXAIJReallocateAIJ
> if( a->maxnz != a->nz ) {
>  ierr = PetscMalloc3(a->nz,MatScalar,&new_a,a->nz,PetscInt,&new_j,A->rmap->n+1,PetscInt,&new_i);CHKERRQ(ierr);
>  ierr = PetscMemcpy(new_a,a->a,a->nz*sizeof(MatScalar));CHKERRQ(ierr);
>  ierr = PetscMemcpy(new_i,a->i,(A->rmap->n+1)*sizeof(PetscInt));CHKERRQ(ierr);
>  ierr = PetscMemcpy(new_j,a->j,a->nz*sizeof(PetscInt));CHKERRQ(ierr);
>  ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
>  a->a = new_a;
>  a->i = new_i;
>  a->j = new_j;
>  a->maxnz = a->nz;
> }
> 
> It seems work well. However, Barry, do you think it has some problem?

  Yes

1) you still have the HUGE grabbing of memory during the initial allocation of the matrix (sometimes with virtual memory this may not hurt you but sometimes it will).

2) processes rarely actually return memory they've malloced back to the underlying OS so the program is still sitting on all that memory (sometimes because of virtual memory this may not hurt you).

Admittedly this is much easier than doing the preallocation correctly so if it works for you then great.

   Barry

> 
> 
> On Feb 18, 2011, at 2:26 AM, Gong Ding wrote:
> 
>> Hi,
>> After update my FVM code to support higher order, I have to preallocate more memory when creating the matrix. However, only a few cells (determined at run time) needed to be high order, thus preallocated memory is overkill too much.
> 
>   You should preallocate ONLY the space you need so that only the space you need is ever allocated. In Unix/Windows there is no good way to recover memory. Why is the preallocated memory too much?
> 
>   The way to do this is each time the matrix nonzero structure will change. (1) loop over you cells determining what needs to be "high order" and determine the number of nonzeros for each row, the (2) preallocate the matrix, then (3) loop over cells again building the actual sparse matrix entries and putting them in. If the matrix nonzero structure changes each time step then just delete the old matrix and generate a new one each time,  creating a new matrix with the proper preallocation is not an expensive operation.
> 
>   Yes it may seem inefficient to loop over the cells twice but you will find that it actually works very well, saves tons of memory and is much much faster than not preallocating.
> 
>   Barry
> 
> 
>> 
>> Is it possible to add a function to reassemble the AIJ matrix to free the extra memory?
>> Or it has already done when MatAssembly is called?
>> 
>> 
> 



More information about the petsc-users mailing list