[petsc-users] preallocation for FE matrix

Barry Smith bsmith at mcs.anl.gov
Mon Oct 4 16:50:31 CDT 2010


  Then perhaps your calls to MatSetValues() are wrong? Everything cannot be write and yet still get the wrong preallocation.

   Here is a trick you can do after providing the preallocation info to the matrix but before calling MatSetValues() call MatSetOption(mat,MAT_NEW_NONZERO_LOCATION_ERR) now it will automatically stop when it finds it has "run out of preallocated spaces" during a set values so in the debugger you can see what has happened when the "impossible" actually happens.

   Barry


On Oct 4, 2010, at 4:43 PM, Tabrez Ali wrote:

> Barry Smith wrote:
>> 
>> On Oct 4, 2010, at 4:21 PM, Tabrez Ali wrote:
>> 
>>   
>>> I am trying to assemble a FE matrix (adding one element at a time from the local stiffness matrix) and cant seem to get the preallocation right. I am correctly calculating the number of non zeros per row and storing the value in the array [nnzpr].
>>>     
>> 
>>   (For sequential case) you cannot be calculating the number of nonzeros per row correctly and yet not getting the preallocation correct.
>> 
>>   For your simple problem. Print the nonzeros per row you are computing then print the sparse matrix you computing. That will show which rows are not computed or set properly.
>>   
> 
> I am calculating the nonzeros per row correctly and have checked this by explicitly forming the dense matrix.
> 
> Here is the stiffness matrix (1 corresponds to non zero location) pattern for the same problem.
> 
>   1  1  1  1  1  1  1  1  0  0  0  0
>   1  1  1  1  1  1  1  1  0  0  0  0
>   1  1  1  1  1  1  1  1  1  1  1  1
>   1  1  1  1  1  1  1  1  1  1  1  1
>   1  1  1  1  1  1  1  1  1  1  1  1
>   1  1  1  1  1  1  1  1  1  1  1  1
>   1  1  1  1  1  1  1  1  0  0  0  0
>   1  1  1  1  1  1  1  1  0  0  0  0
>   0  0  1  1  1  1  0  0  1  1  1  1
>   0  0  1  1  1  1  0  0  1  1  1  1
>   0  0  1  1  1  1  0  0  1  1  1  1
>   0  0  1  1  1  1  0  0  1  1  1  1
> 
> and here is the [nnzpr] array that I passed to MatCreateSeqBAIJ
>   
>    8
>    8
>   12
>   12
>   12
>   12
>    8
>    8
>    8
>    8
>    8
>    8
> 
> The sum of values in [nnzpr] adds up to 112.
> 
> Thanks again
>>> I dont understand why -mat_view_info shows allocated nonzeros to be 154 when sum(nnzpr) is 112.
>>>     
>> 
>>    If you mess up the count on a row it allocates a few extra for that row (incase you end up needing them) so the number allocated can be more then the number actually needed.
>> 
>>    Barry
>> 
>> 
>>   
>>> Here is part of the relevant code
>>> 
>>> ...
>>> call MatCreateSeqBAIJ(Petsc_Comm_Self, 1, m, n, petsc_null_integer, nnzpr, Mat_A, ierr)
>>> ...
>>> ! Assume a bilinear quad (2 dof per node)
>>> do j1=1,8
>>>   do j2=1,8
>>>     call MatSetValues(Mat_A, 1, indx(j1)-1, 1, indx(j2)-1, k(j1,j2), Add_Values, ierr)
>>>   end do
>>> end do
>>> ...
>>> call MatAssemblyBegin(Mat_A,Mat_Final_Assembly,ierr)
>>> call MatAssemblyEnd(Mat_A,Mat_Final_Assembly,ierr)
>>> ...
>>> 
>>> On running it I get
>>> 
>>> -bash-3.00$ ./a.out <inp -info -mat_view_info
>>> ...
>>>  Total non-zero elements estimated i.e., sum(nnzpr) =                  112
>>> [0] PetscCommDuplicate(): Duplicating a communicator 1140850689 -2080374784 max tags = 2147483647
>>> [0] PetscCommDuplicate():   returning tag 2147483647
>>> [0] MatAssemblyEnd_SeqBAIJ(): Matrix size: 12 X 12, block size 1; storage space: 42 unneeded, 112 used
>>> [0] MatAssemblyEnd_SeqBAIJ(): Number of mallocs during MatSetValues is 9
>>> [0] MatAssemblyEnd_SeqBAIJ(): Most nonzeros blocks in any row is 12
>>> ...
>>> Matrix Object:
>>>   type=seqbaij, rows=12, cols=12
>>>   total: nonzeros=112, allocated nonzeros=154
>>>       block size is 1
>>> 
>>> I dont understand why -mat_view_info shows allocated nonzeros to be 154 when sum(nnzpr) is 112.
>>> 
>>> Thanks in advance.
>>>     
>> 
>>   
> 



More information about the petsc-users mailing list