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

Matthew Knepley knepley at gmail.com
Tue Aug 23 07:41:47 CDT 2022


On Mon, Aug 22, 2022 at 12:12 PM Patrick Alken <patrick.alken at geomag.info>
wrote:

> Thank you, I have read that document. I have changed my criteria to:
>
>
> if (j >= first && (j - first) < m) {
>
As Barry says, this should be

if ((j >= first) && (j < last)) {


>   /*diagonal*/
>
> else
>
>   /*off-diagonal*/
>
>
> It seems to be working better. I don't know how to get the c1 and c2
> columns referred to by the documentation.
>
Thanks

    Matt


> Patrick
>
>
>
> On 8/22/22 10:03, Zhang, Hong wrote:
>
> 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).
> petsc.org
> if you use mpiaij matrix.
> Hong
> ------------------------------
> *From:* petsc-users <petsc-users-bounces at mcs.anl.gov>
> <petsc-users-bounces at mcs.anl.gov> on behalf of Patrick Alken
> <patrick.alken at geomag.info> <patrick.alken at geomag.info>
> *Sent:* Monday, August 22, 2022 10:56 AM
> *To:* petsc-users <petsc-users at mcs.anl.gov> <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];
>
>          else
>
>              ++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?
>
>

-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ <http://www.cse.buffalo.edu/~knepley/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20220823/65f9f714/attachment.html>


More information about the petsc-users mailing list