[petsc-users] free unused allocated nonzeros after overestimating preallocation

Tobias Neckel neckel at in.tum.de
Mon May 3 08:21:34 CDT 2010


Hi,

> I am preallocating a matrix using
>   MatMPIAIJSetPreallocation(HalfM,27,PETSC_NULL,26,PETSC_NULL);
> Since I have no easy way to know whether the entries will end up in
> petsc's 'diagonal' or 'nondiagonal'. After assembling the matrix this
> results in:
> 
> Matrix Object:
>  type=mpiaij, rows=9801, cols=9801
>  total: nonzeros=231601, allocated nonzeros=519453
>    [0] Local rows 9801 nz 231601 nz alloced 519453 mem 6551364, not
> using I-node routines
>    [0] on-diagonal part: nz 231601
>    [0] off-diagonal part: nz 0
> 
> On 1 processor, and some numbers split between on/off on more, as
> expected. So far no problem since I can temporarily spare the memory
> for 1 such matrix.
> Next, I would like to compress the storage somehow, to free the memory
> not used, since I know this nonzero pattern will not change, but I
> can't find a way to do this in petsc.
> Worse, when I use MatConvert/MatDuplicate to initialize my other
> matrices, they all seem to inherit this property of using about twice
> as much memory as needed. This is a problem when taking the number of
> rows to 1 million +.
> 
> What is the easiest way to free the unused memory?

Why not iterating once to count how many entries you really need (and 
storing the counts per row in a vector), creating the matrix with the 
exact number of necessary entries, and then iterating a second time?

Seems easier than squeezing out sth.

Best regards
Tobias


More information about the petsc-users mailing list