[petsc-users] how to use MatSetValues?

Gianluca Meneghello gianmail at gmail.com
Tue Mar 8 05:21:31 CST 2011


> 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...

You don't have to allocate a full array for each line, just an array
with the nonzero elements and a corresponding array with the row
indices.

That is, to store a line like
[ 0 0 0 1 0 0 2 0 0 0 3 0 0]

the two array would be

icols[] = { 3 , 6, 10 };
ivals[] = { 1 , 2, 3};

and the call for the value would be

MatSetValues(xx,1,irow,3,icols,*ivals,INSERT_VALUES);

If there are multiple lines with the same nonzero structure they can
be stored together.

Gianluca



On 8 March 2011 12:13, Tomasz Jankowski <tomjan at jay.au.poznan.pl> wrote:
>> 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
>
>



-- 
"[Je pense que] l'homme est un monde qui vaut des fois les mondes et
que les plus ardentes ambitions sont celles qui ont eu l'orgueil de
l'Anonymat" -- Non omnibus, sed mihi et tibi
Amedeo Modigliani


More information about the petsc-users mailing list