[petsc-users] is there a function to append matrix

Jed Brown jed at jedbrown.org
Mon Aug 31 22:58:31 CDT 2020

Karl Lin <karl.linkui at gmail.com> writes:

> Thanks for the quick reply. The reason why I want to do this is because I
> would like to build A and B separately first. Then do something with B by
> itself. Then scale B by a constant. Then append B to A to make C and
> continue some other matrix operations. I took a look at
> MatGetLocalSubMatrix() and there is this line:
> Depending on the format of mat, the returned submat may not implement
> MatMult
> <https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatMult.html#MatMult>().
> Its communicator may be the same as mat, it may be PETSC_COMM_SELF
> <https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/PETSC_COMM_SELF.html#PETSC_COMM_SELF>,
> or some other subcomm of mat's.
> what is the format that will make submat not being able to do MatMult()?
> Thank you very much.

When called on matrices like AIJ, it only returns a Mat capable of doing assembly-related operations (like MatSetValuesLocal).

If you use MatNest, it returns the matching submatrix (which is typically fully-functional), but MatNest does not support monolithic preconditioners like a sparse direct solver.  (It's usually used with PCFieldSplit.)

If you don't mind the extra time and space, you can MatConvert, otherwise assembly into the data structure you want (via MatGetLocalSubMatrix).

> On Mon, Aug 31, 2020 at 10:29 PM Jed Brown <jed at jedbrown.org> wrote:
>> Karl Lin <karl.linkui at gmail.com> writes:
>> > If I have two matrix A and B with the same number of columns, same
>> > distribution pattern (column ownership pattern) among processes but
>> > different number of rows, is there a function to append B to A to make a
>> > new matrix C = [A; B]? Thanks.
>> Sort of; you can create a MatNest with the two matrices and (optionally)
>> convert to AIJ format.
>> Better, you can take the code that builds A and B, but call it on "local"
>> submatrices; see MatGetLocalSubMatrix() or an example like
>> src/snes/tutorials/ex28.c.

More information about the petsc-users mailing list