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

Barry Smith bsmith at petsc.dev
Mon Aug 22 11:39:15 CDT 2022


 MatGetOwnershipRangeColumn() tells you the start and end+1 of the columns that are in the diagonal block. For square matrices they are the same as rstart and rend.

  Barry


> On 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) {
>   /*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.
> 
> Patrick
> 
> 
> On 8/22/22 10:03, Zhang, Hong wrote:
>> See https://petsc.org/release/docs/manualpages/Mat/MatMPIAIJSetPreallocation.html#MatMPIAIJSetPreallocation <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 <http://petsc.org/>
>> if you use mpiaij matrix.
>> Hong
>> From: petsc-users <petsc-users-bounces at mcs.anl.gov> <mailto:petsc-users-bounces at mcs.anl.gov> on behalf of Patrick Alken <patrick.alken at geomag.info> <mailto:patrick.alken at geomag.info>
>> Sent: Monday, August 22, 2022 10:56 AM
>> To: petsc-users <petsc-users at mcs.anl.gov> <mailto: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?

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


More information about the petsc-users mailing list