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

Barry Smith bsmith at mcs.anl.gov
Fri Feb 18 08:11:38 CST 2011


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