[petsc-users] adding mat rows

Barry Smith bsmith at mcs.anl.gov
Thu Nov 19 14:05:25 CST 2015


  So long as you arrange things so that "partner rows" are always on the same process I think you should be able to do the following.

   Call MatGetRow() on the first row, pull out the values of interest, call MatRestoreRow() then call MatSetValues() on the second row with ADD_VALUES.   Call MatSetValues() on the first row with the values you want to set with INSERT_VALUES. Not that each process can call MatGetRow (and hence the MatSetValues) a different number of times.

   Since you cannot mix calls of ADD and INSERT. Have all the processes do their ADDING to all the second partner rows then call MatAssemblyBegin/End once then have all the processes do their setting of values into the first partner rows and call MatAssemblyBegin/End again. There is no need to call the MatAssemblyBegin/End once for each row in this case.

  Barry

If the partner rows are on different processes it would require much more work.

This seems like an odd thing to do, by changing some rows you are still losing information even if you have changed other rows by summing in the partner row.

> On Nov 19, 2015, at 1:48 PM, Carles Bona <carlesbona at gmail.com> wrote:
> 
> I have a system of equations:
> 
> a11*x1 + a12*x2 + ... + a1n*xn = b1
> a21*x1 + a22*x2 + ... + a2n*xn = b2
> .
> .
> .
> an1*x1 + an2*x2 + ... + ann*xn = bn
> 
> Let's say I want to modify the first equation, but I don't want to lose the information there, so I will add first the first and second equations, store the result in the second equation and then modify the first equation. Like this:
> 
> x1 - x2 = 0
> (a11+a21)*x1 + (a12+a22)*x2 + ... + (a1n+a2n)*xn = b1+b2
> .
> .
> .
> an1*x1 + an2*x2 + ... + ann*xn = bn
> 
> And I want to do this for a few rows of my matrix. I have already built the nonzero structure so that these additions can be done without hitting a non preallocated location (for example a case where a21 was never allocated because it was meant to be zero always and now, with a11 present, it's different than zero).
> 
> Any hints?
> 
> Thanks!
> 
> Carles
> 
> El dia 19/11/2015 19:55, "Barry Smith" <bsmith at mcs.anl.gov> va escriure:
> 
> > On Nov 19, 2015, at 11:25 AM, Carles Bona <carlesbona at gmail.com> wrote:
> >
> > Dear all,
> >
> > I would like to add some of my equations before I modify them.
> 
>    Please explain what you mean by this. Algebraically exactly what do you want to do?
> 
> 
> > I haven't found any high level function that would allow me to add rows of a matrix (I am working with a parallel BAIJ). Is there any nice way of doing this?
> >
> > I have tried with MatGetRow/MatRestoreRow, but I am struggling a bit to retain the cols and vals, as only one processor can call MatGetRow but if only that processor tries to allocate memory then one gets a segmentation fault. I guess I should allocate enough memory on all processors...
> >
> > If I refrain from storing the cols and vals I need to call MatSetValues before returning the pointer, with a subsequent call to assemblybegin/end for each row, which slows down the code.
> >
> > The other option would be to forget about these row additions after the matrix has been filled and try to fill it while taking into account these row additions at the same time. I guess I need to be constantly checking for indices then.
> >
> > So, which option (not necessarily mentioned here) would you reccomend?
> >
> > Thanks a lot,
> >
> > Carles
> 



More information about the petsc-users mailing list