# [petsc-users] zeros in sparse aij

Barry Smith bsmith at mcs.anl.gov
Tue Dec 6 23:11:13 CST 2011

```On Dec 6, 2011, at 10:26 PM, Xiangdong Liang wrote:

> Hello everyone,
>
> Can anyone help on the usage of MAT_IGNORE_ZERO_ENTRIES? Suppose I
> want to have a 10-by-10 sparse matrix, where each row has maximum two
> nonzeros. I would like to have petsc ignore the 0.0  values by not
> allocating space for 0.0 values. In my problem, the nonzero pattern of
> sparse matrix will never change. I hope the ignore of zero entries
> will save lots of operations when I am doing matrix-vector product or
> spare-direct decomposition .
>
> Below is my script. When I check the mat_view_info, I have
>
> Matrix Object:
> type=mpiaij, rows=10, cols=10
> total: nonzeros=20, allocated nonzeros=40
>   not using I-node (on process 0) routines
>
> Is there a way to have nonzeros=10, and allocated nonzeros=10, since
> the number of real nonzeros=10? Thanks.

Those "extra" nonzeros are not actually in the sparse matrix so they do not cause extra multiples. You can safely ignore them.

Barry

>
> Xiangdong
>
> -----------------------------------------------------------
>  Mat A;  int N=10, i, ns,ne;
>   ierr=MatCreateMPIAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,N,N,2,NULL,2,NULL,&A);CHKERRQ(ierr);
>   ierr = MatGetOwnershipRange(A, &ns, &ne); CHKERRQ(ierr);
>   for (i=ns;i<ne;i++)    {      ierr=MatSetValue(A,i, i, 1.0,
> ADD_VALUES); CHKERRQ(ierr);      ierr=MatSetValue(A,i, (i+1)%N, 0.0,
>     MatSetOption(A,MAT_IGNORE_ZERO_ENTRIES,PETSC_TRUE);     ierr =
> MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);     ierr =
> MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
>
> --------------------------------------------------------
>
>
> On Thu, Dec 1, 2011 at 10:01 PM, Barry Smith <bsmith at mcs.anl.gov> wrote:
>>
>>  The checks in the code are lines like
>>
>>      if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
>>
>> hence changing the source code  to ignore small values is not difficult.  But I think this is a terribly cheesy hack and would hope people would not do this.
>>
>>    Barry
>>
>>
>>
>> On Dec 1, 2011, at 8:42 PM, Mohamad M. Nasr-Azadani wrote:
>>
>>> I was always wondering how difficult it would be to define a constant say, ZERO_DROP_TOLERANCE so that when the matrix is setup, MatAssemblyBegin()/End(), it automatically ignores the values below this threshold?
>>> I understand the for the matrices that might change in successive iterations, that might not be a good idea since the memory reallocation could be very slow, but for a lot of cases which a constant matrix, that could be simply helpful.
>>>
>>> Best,
>>>
>>>
>>>
>>>
>>>
>>> On Thu, Dec 1, 2011 at 5:00 PM, Barry Smith <bsmith at mcs.anl.gov> wrote:
>>>
>>> On Dec 1, 2011, at 6:44 PM, Xiangdong Liang wrote:
>>>
>>>> Hello everyone,
>>>>
>>>> I have a question about whether zeros will change the sparse pattern.
>>>> Suppose I am generating two sparse matrices A and B. A is generated
>>>> with exactly number of nonzeros, say 10 nnz per row. When I generate
>>>> matrix B, I specify 12 nonzeros, but two of them are given zero values
>>>> by MatSetValue. Will A and B have same sparsity patterns and
>>>> performance ? In other words, will PETSc simply ignore these zeros
>>>> entries?  I output these A and B in matlab, and it seems that these
>>>> zeros entries are ignored. I just want to double check on this.
>>>>
>>>> Is the option MAT_IGNORE_ZERO_ENTRIES on by default?
>>>
>>>   No, by default PETSc will insert the zero values thus increasing the nonzero pattern. If you set that option to the matrix then inserting values it will not introduce an increase in the pattern.
>>>
>>>
>>>    BTW: if you then load the matrix via a binary file to MATLAB it may be that MATLAB silently removes those locations so you don't see them.
>>>   Barry
>>>
>>>> Thanks.
>>>>
>>>> Best,
>>>> Xiangdong
>>>
>>>
>>

```