[petsc-users] MatZeroEntries for retaining allocated space

Junchao Zhang junchao.zhang at gmail.com
Tue Apr 12 15:46:26 CDT 2022

On Tue, Apr 12, 2022 at 12:23 PM Samuel Estes <samuelestes91 at gmail.com>

> Maybe I should create a separate question for this but I did some testing
> and now I'm a bit confused on how PETSc handles preallocated space which is
> not assigned a value. I created a test case where I allocated a certain
> amount of space for a matrix. I then only assigned a value to the (0,0)
> entry and assembled the matrix. When I look at the MatInfo it says that
> there were 0 unneeded entries. Furthermore, when I use MatView, I see that
> there are 0 values in the matrix so there actually were values which were
> put there.
> I have a guess at what's happening and if I'm correct then it would solve
> my original problem but I would like to confirm here. I am using the
> MatPreallocator class along with the MatPreallocatorPreallocate() routine
> to allocate space for the matrix. My guess is that this routine
> automatically assigns a value of zero to the entries which I specified in
> the preallocator. So if I had done the original MatXXXAIJSetPreallocation()
> routines which just allocate a certain number of non-zeros to the rows,
> then I would have to explicitly assign them zero values or else I would
> lose unassigned space in the assembly routines. Since the MatPreallocator
> specifies exactly which entries will be non-zero, PETSc also actually
> assigns a zero value under the hood so that I don't have to explicitly
> assign values myself. Is this correct?
I think your understanding is correct.
I think the rule is: if you have not specified the exact sparsity pattern
in preallocation (for example, you only said this row had this number of
nonzeros, but did not give the column indices), then in MatAssebmlyEnd(),
unused slots will be compressed out.
You can call MatZeroEntries() on assembled or unassembled (but
preallocated) matrices. The routine just zeros the internal value array.
It does not help you to preserve nonzero entries.
With MatPreallocatorPreallocate(), the sparsity is specified, so that petsc
will keep the entries.

> Sorry for the convoluted question. I hope it's clear.
> On Tue, Apr 12, 2022 at 11:54 AM Samuel Estes <samuelestes91 at gmail.com>
> wrote:
>> Hi,
>> I have a problem where I want to allocate some nonzero structure to a
>> sparse matrix which will be used repeatedly to solve some equations. The
>> assembly routines will compress out allocated space which has not been
>> assigned a value so I know that I need to assign zero values to any unused
>> entries in order to keep that space. Will MatZeroEntries do this? If not, I
>> could always just loop over and explicitly assign zero values but
>> MatZeroEntries would be more convenient. Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20220412/88f6eff6/attachment.html>

More information about the petsc-users mailing list