# [petsc-users] combine two matrices with different non-zero structure

Smith, Barry F. bsmith at mcs.anl.gov
Tue Jan 30 22:17:15 CST 2018

```MaTAXPY with same nonzero pattern is much faster.

> On Jan 30, 2018, at 10:10 PM, Marius Buerkle <mbuerkle at web.de> wrote:
>
> Thanks. While B'*C*B may not have the same nonzero pattern as A it does not matter as this is done by MaTAXPY with different nonzero pattern it should not matter when using  MatPtAPNumeric_MPIAIJ_MPIAIJ_scalable()  with a restricted reduction. I will give it a try.
>
>
>
>
> To do this most efficiently you would compute exactly the part of F = B'*C*B you need (that matches the nonzero pattern of A) do a MatAXPY() with same numerical pattern and then just stick the values of D into the original matrix (without worrying about excluding the new nonzeros since there won't be any).
>
> MatPtAPNumeric_MPIAIJ_MPIAIJ_scalable() does the numerical triple product so the only special code you need is to rewrite MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable() so that that it takes another matrix (your A) that determines the nonzero pattern you want and from this constructs the needed symbolic information needed to do the (special, reduced) matrix triple product.
>
> If you can this great, if not then I suspect there is not much you can do to improve the efficiency of what you have already done.
>
> Barry
>
>
> > On Jan 30, 2018, at 9:15 PM, Marius Buerkle <mbuerkle at web.de> wrote:
> >
> > The matrix A is the extracted submatrix and I am basicallly doing something like this D=A+B'*C*B. Where A, B, and C are sparse but B'*C*C will clearly generate some addional nonzero entries in D. And D should go back into the bigger matrix but only the entries which are already nonzero in A.
> >
> >
> > Could you tell us exactly what matrix matrix products you are doing?
> >
> > Barry
> >
> >
> > > On Jan 30, 2018, at 6:53 PM, Marius Buerkle <mbuerkle at web.de> wrote:
> > >
> > > Barry,
> > >
> > > Thanks for you reply. The pulled matrix is symmetric but that's it. At the moment I am doing a copy of the matrix right after MatCreateSubMatrix to keep it's nonzero structure. To insert the matrix which I obtained after the multiplications back into the bigger matrix I use the initially copied Matrix to get the nonzero elements with MatGetRow and as I know the offset of the SubMatrix relative to the elements of the big-one I can use MatSetValues to insert the elements row by row. That kinda works but it is rather inefficient. Is there anyway to avoid copying the whole submatrix in the beginning, as I don't need the actual values but only the positions of the nonzero elements in one way or the other.
> > >
> > > Marius
> > >
> > >
> > >
> > >> Marius,
> > >>
> > >> We don't provide a way to insert a "generic" sparse matrix into a bigger matrices (dense matrices coming from element stiffness matrices yes) so I don't see any simple solution. >Does the submatrix you pull out have any particular structure, what does it represent?
> > >>
> > >> Barry
> > >
> > >
> > >>>
> > >>> Hi !
> > >>>
> > >>> I have the followng problem. I create a Submatrix containing a subset of row/columns of the original matrix. After some matrix multiplications the non-zero strucutre of the resulting matrix changed. Now I want to insert this submatix back into the original one keeping only the non-zero entries which are present in the original matrix discarding the others which accumulated due to the matrix multiplications. Is there an easy or also not so easy way to do this ?
> > >>>
> > >>> Best,
> > >>> Marius
> > >
> >
>

```