[petsc-users] Better way to pre-allocate memory for matrix being read in ???
Jed Brown
jed at 59A2.org
Sun Feb 13 16:47:46 CST 2011
On Sun, Feb 13, 2011 at 23:37, Gaurish Telang <gaurish108 at gmail.com> wrote:
> I have a text file containing the non-zero entries of a sparse matrix of
> dimension 2683x1274, stored in the form (row, column, element) i.e. [i, j,
> element] format.
>
This is a horrible format and can not scale. If it becomes a performance
issue, change the format.
>
> That is ALL the information I have regarding the matrix.
>
> However when pre-allocating memory with MatSeqAIJSetPreallocation(Mat
> B,PetscInt nz,const PetscInt nnz[]), the parameters nz and nnz need to be
> known,
>
> nz=number of nonzeros per row (same for all rows)
> nnz=array containing the number of nonzeros in the various rows (possibly
> different for each row) or PETSC_NULL<http://www.mcs.anl.gov/petsc/petsc-2/snapshots/petsc-dev/docs/manualpages/Sys/PETSC_NULL.html#PETSC_NULL>
>
> which i do not know for my matrix, ( unless I resort to using MATLAB. ).
>
> Does that mean I have to set nz= 1274 (the length of a row) and
> nnz=PETSC_NULL ?
>
No, read the file twice. The first time through, just count the number of
nonzeros (per row), then set preallocation with the correct size, and
finally read the file a second time calling MatSetValue() for each entry.
For a matrix this small, you could just read it in without preallocating,
but that will get too expensive quickly if you increase the matrix size.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20110213/3e8b77eb/attachment.htm>
More information about the petsc-users
mailing list