[petsc-users] how to use MatSetValues?

Barry Smith bsmith at mcs.anl.gov
Tue Mar 8 07:40:52 CST 2011


On Mar 8, 2011, at 5:58 AM, Tomasz Jankowski wrote:

>>> 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.
> 
> yes, yes. of course you are absolutely right.
> I did a mistake here. But I was only my supposedly thinking about 'linebyline' scheme. In fact each entry of my matrix 'appear' (when the data file is processed)  in random order. so, it seems that with MatSetValues I could only use 'onebyone' scheme :-(
> 

   If you are reading in the data from an ASCII file then (1) you should sweep through the file twice, the first time counting the number of nonzeros per row, then do proper preallocation of the matrix with MatCreateSeqAIJ() then (2) sweep through again calling the MatSetValues().  You should then call MatView() with a binary viewer and save the matrix in binary format. Now your "real" PETSc code would use MatLoad() to quickly load the binary file (either in parallel or sequential) to do whatever calculation you want to do.

  Barry

The "extra" cost of two sweeps through the file and using the binary file will save you HUGE amounts of time, you have to do it this way. This is discussed in numerious previous petsc-users  email at the archive.



> i hope I will find how to access private structure of matrix.
> 
> tom
> 



More information about the petsc-users mailing list