[petsc-users] MatSetValues for nonzeros of a sparse matrix

Jed Brown jed at 59A2.org
Wed Jan 13 10:01:03 CST 2010


On Wed, 13 Jan 2010 16:53:20 +0100, Umut Tabak <u.tabak at tudelft.nl> wrote:
> Jed Brown wrote:
> >   MatSetValues(A, 1, &idxm[r], 1, &idxn[r], &valuesMat[r*4+r], INSERT_VALUES);
> >
> > or
> >
> >   MatSetValue(A, idxm[r], idxn[r], valuesMat[r*4+r], INSERT_VALUES);
> >
> >   
> Thanks for the quick replies, the above, I guess, still means to keep 
> the zeros but below explanation is more practical from an efficiency 
> point of view.

No, the index r*4+r is selecting only the diagonal (nonzero entries)
from the example array that you provided.

> >> Also I keep the row and column indices of the non-zero values and as
> >> well the non-zeros values in arrays of proper type.
> >>     
> >
> > you should loop over rows and insert all entries of that row at once.
> >   
> This was where I was confused :), I should keep the non-zeros of the row 
> and set them with the loop.

You should call MatSetValues once per row.  For example, suppose we have

  row = 8;
  cols[] = {2,7,8,9,14};
  vals[] = {-1.0,-2.0,6.0,-1.5,-1.2};

then we would call

  MatSetValues(A,1,&row,5,cols,vals,INSERT_VALUES);

to set these 5 values on row 8.  I hope this helps.

Jed


More information about the petsc-users mailing list