[petsc-users] how to use MatSetValues?

Gianluca Meneghello gianmail at gmail.com
Tue Mar 8 04:02:43 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.

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.

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.

I hope this helps

Gianluca





On 8 March 2011 10:38, Tomasz Jankowski <tomjan at jay.au.poznan.pl> wrote:
>> You should do a loop over the values
>>
>> for (int i = 0; i < 5; i++)
>> MatSetValues(xx,1,row[i],1,col[i],val[i],INSERT_VALUES);
>
> here is the point Gianluca,
> I don't want to store one by one values because It will take the ages with
> hundreds of milions of values(even with preallocation). I would like to set
> size of row,col and val arrays to e.g. one milion than fill them with
> data and such prepared large chunk of data pass to MatSetValues. What is
> suggested by petsc's developers. But it doesn't work properly for me - I
> dont know what i'm doing wrong...
>
> tom
>
>>
>>
>> On 8 March 2011 10:16, Tomasz Jankowski <tomjan at jay.au.poznan.pl> wrote:
>>>>
>>>> I think m and n are the number of rows and columns you are inserting
>>>> in the matrix. If you insert one value at a time, that would be
>>>>
>>>> MatSetValues(xx,1,row,1,col,val,INSERT_VALUES);
>>>>
>>>> I hope it helps
>>>>
>>>> Gianluca
>>>
>>> hello Gianluca, thanks for re.
>>>
>>> your sugestion doesn't work.
>>>
>>> my code
>>>
>>>  Mat xx;
>>>  PetscInitialize(&argc,&argv,(char *)0,help);
>>>
>>>  MatCreateSeqAIJ(PETSC_COMM_SELF,5,5,PETSC_DECIDE,PETSC_NULL,&xx);
>>>
>>>  PetscInt row [] ={0, 1, 2, 3, 4};
>>>  PetscInt col [] = {1, 0, 4, 2, 3};
>>>  PetscScalar val[] = {1.1,2.2,3.3,4.4,5.5};
>>>
>>>  MatSetValues(xx,5,row,5,col,val,INSERT_VALUES);
>>>
>>>  MatAssemblyBegin(xx,MAT_FINAL_ASSEMBLY);
>>>  MatAssemblyEnd(xx,MAT_FINAL_ASSEMBLY);
>>>
>>>  MatView(xx,PETSC_VIEWER_STDOUT_WORLD);
>>>  PetscFinalize();
>>>
>>> and here few examples of results
>>>
>>>  MatSetValues(xx,1,row,1,col,val,INSERT_VALUES);
>>>
>>> row 0: (1, 1.1)
>>> row 1:
>>> row 2:
>>> row 3:
>>> row 4:
>>>
>>>
>>>  MatSetValues(xx,5,row,1,col,val,INSERT_VALUES);
>>>
>>> row 0: (1, 1.1)
>>> row 1: (1, 2.2)
>>> row 2: (1, 3.3)
>>> row 3: (1, 4.4)
>>> row 4: (1, 5.5)
>>>
>>>  MatSetValues(xx,5,row,5,col,val,INSERT_VALUES);
>>>
>>> row 0: (0, 2.2)  (1, 1.1)  (2, 4.4)  (3, 5.5)  (4, 3.3)
>>> row 1: (0, 4.94066e-324)  (1, 2.07411e-317)  (2, 1.4822e-323)  (3,
>>> 2.07356e-317)  (4, 4.24399e-314)
>>> row 2: (0, 6.36599e-314)  (1, 2.122e-314)  (2, 2.07402e-317)  (3,
>>> 1.5957e-316)  (4, 1.18832e-312)
>>> row 3: (0, 6.95332e-310)  (1, 2.07367e-317)  (2, 0)  (3, 1.20407e-312)
>>>  (4,
>>> 0)
>>> row 4: (0, 6.95332e-310)  (1, 0)  (2, 2.07378e-317)  (3, 0)  (4,
>>> 2.122e-314)
>>>
>>> tom
>>>
>>> ########################################################
>>> #               tomjan at jay.au.poznan.pl                #
>>> #              jay.au.poznan.pl/~tomjan/               #
>>> ########################################################
>>>
>>> On Tue, 8 Mar 2011, Gianluca Meneghello wrote:
>>>
>>>> I think m and n are the number of rows and columns you are inserting
>>>> in the matrix. If you insert one value at a time, that would be
>>>>
>>>> MatSetValues(xx,1,row,1,col,val,INSERT_VALUES);
>>>>
>>>> I hope it helps
>>>>
>>>> Gianluca
>>>>
>>>> On 8 March 2011 09:39, Tomasz Jankowski <tomjan at jay.au.poznan.pl> wrote:
>>>>>
>>>>> hello,
>>>>>
>>>>> Could someone give me a hand with MatSetValues function?
>>>>>
>>>>> suppose such example sparse matrix
>>>>>
>>>>> 0   1.1 0   0   0
>>>>> 2.2 0   0   0   0
>>>>> 0   0   0   0   3.3
>>>>> 0   0   4.4 0   0
>>>>> 0   0   0   5.5 0
>>>>>
>>>>> after storing it in row, col and val arrays we have
>>>>>
>>>>> row[0] = 0;col[0]=1;val[0]=1.1;
>>>>> row[1] = 1;col[1]=0;val[1]=2.2;
>>>>> row[2] = 2;col[2]=4;val[2]=3.3;
>>>>> row[3] = 3;col[3]=2;val[3]=4.4;
>>>>> row[3] = 4;col[3]=3;val[4]=5.5;
>>>>>
>>>>> question is: how to use MatSetValues with it?
>>>>>
>>>>> I thought that 'MatSetValues(xx,5,row,1,col,val,INSERT_VALUES);' would
>>>>> be
>>>>> good but it doesn't .(I suppose I don't fully anderstand how it
>>>>> works...)
>>>>>
>>>>> thanks,
>>>>>
>>>>> tom
>>>>>
>>>>> ########################################################
>>>>> #               tomjan at jay.au.poznan.pl                #
>>>>> #              jay.au.poznan.pl/~tomjan/               #
>>>>> ########################################################
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> "[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
>>>
>>
>>
>>
>> --
>> "[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
>



-- 
"[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