[petsc-users] Check if a matrix has been created

Mark Adams mfadams at lbl.gov
Fri Mar 22 16:46:21 CDT 2019


On Fri, Mar 22, 2019 at 5:17 PM Shashwat Sharma via petsc-users <
petsc-users at mcs.anl.gov> wrote:

> Hello,
>
> I'd like to be able to check if a Mat object has been created and
> allocated; if it hasn't been created yet, I want to create and allocate it,
> otherwise I want to reuse the existing allocation.
>

Have your constructor initialize it to PETSC_NULL_MAT. And the test with if
(m_mat==PETSC_NULL_MAT) MatCreate ....


> The context is that I have a Mat variable declared (but not defined) in a
> header file, so that it is available to the entire class. It is created
> (MatCreate) and allocated at a later point in the implementation, and may
> need to be reused with different values several times. But I don't want to
> allocate it unless it's needed, because it would be quite large.
>
> Also, I want my destructor to only call MatDestroy on the matrix if it has
> been created (otherwise it gives seg faults).
>

Same, but I think you can call MatDestroy with a NULL matrix.


>
> How can I achieve this?
>
> Thanks.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20190322/3e3c803b/attachment.html>


More information about the petsc-users mailing list