[petsc-users] how to use MatSetValues?

Tomasz Jankowski tomjan at jay.au.poznan.pl
Tue Mar 8 05:13:48 CST 2011


> I'm not an expert, but I think you have to provide two vector for row
> and cols and a matrix for values corresponding to all combinations of
> rows and cols. The entry v is required to be a "logically two
> dimensional array of values", so this is my understanding.
>
> If this is true, in your case it would require to pass the whole full
> matrix in v, zeros included, given that you have 1 value per row and 1
> value per col. That said, I have no idea on whether the zeros are then
> taken away or not once the matrix is assembled. You may insert "block"
> of values, but I still think each block is required to be a full
> rectangular matrix.
>
> There can alway be the possibility that I'm completely wrong about
> what I'm saying... this is how I used it so far.

It seems that you are right Gianluca,
   int m =3;
   int n = 3;
   PetscInt indxm [] ={0, 1, 2};
   PetscInt indxn [] = {0, 1, 2};
   PetscScalar vals[][3] = {{1.1,0,0},{0,1.2,0},{0,0,1.3}};
   MatSetValues(xx,m,indxm,n,indxn,*vals,INSERT_VALUES);

only such code gives expected result
row 0: (0, 1.1)  (1, 0)  (2, 0)
row 1: (0, 0)  (1, 1.2)  (2, 0)
row 2: (0, 0)  (1, 0)  (2, 1.3)


> Something I'm doing is to set the values line by line by using
> MatSetValues(xx,1,row,ncols,col,val,INSERT_VALUES);
> where row has 1 elements, cols and val ncols elements.

I'm building 100mln x 100mln matrices and bigger. what means that with 
linebyline scheme i have to 100mln times allocate array with size 
equal to 100mln with only few nonzero entries.
not efficient too much i think...


> Actually, I asked a similar question some time ago, and I was
> redirected to the possibility of assembling the matrix accessing
> directly the private structure of the matrix.
> See ~petsc/src/mat/examples/tutorials/ex12.c for seqaij format.

maybe it will be the solution... 'll check it

again, thanks Gianluca for posting.

tom



More information about the petsc-users mailing list