[petsc-users] Preallocating a matrix with the d_nnz and o_nnz arrays

Zhang, Hong hzhang at mcs.anl.gov
Mon Aug 22 11:03:31 CDT 2022

See https://petsc.org/release/docs/manualpages/Mat/MatMPIAIJSetPreallocation.html#MatMPIAIJSetPreallocation
MatMPIAIJSetPreallocation - PETSc<https://petsc.org/release/docs/manualpages/Mat/MatMPIAIJSetPreallocation.html#MatMPIAIJSetPreallocation>
MatMPIAIJSetPreallocation Preallocates memory for a sparse parallel matrix in AIJ format (the default parallel PETSc format). For good matrix assembly performance the user should preallocate the matrix storage by setting the parameters d_nz (or d_nnz) and o_nz (or o_nnz).
if you use mpiaij matrix.
From: petsc-users <petsc-users-bounces at mcs.anl.gov> on behalf of Patrick Alken <patrick.alken at geomag.info>
Sent: Monday, August 22, 2022 10:56 AM
To: petsc-users <petsc-users at mcs.anl.gov>
Subject: [petsc-users] Preallocating a matrix with the d_nnz and o_nnz arrays

I am looping through all the non-zero elements of my matrix to count the
diagonal and off-diagonal terms in order to pre-allocate the matrix.
However, what is the criteria for knowing if a given element is in the
diagonal portion or off-diagonal portion? It seems that it will depend
on row, column, and also processor. I was doing something like this:

MatGetLocalSize(A, &m, &n);

MatGetOwnershipRange(A, &first, &last);

for (i = first; i < last; ++i) {

     for (j = 0; j < N; ++j) {

         if (abs(i - j) < m)

             ++d_nnz[i - first];


             ++o_nnz[i - first];



However, the criteria: abs(i - j) < m is not correct. I think the
correct criteria will include the processor number also. Does anyone
know the correct criteria to use?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20220822/b356a284/attachment.html>

More information about the petsc-users mailing list