[petsc-users] setting block matrices with preallocation

Umut Tabak u.tabak at tudelft.nl
Mon Jan 18 09:41:51 CST 2010

Dear all,

Some time ago, I asked some questions about setting entries of a sparse 
matrix efficiently. I got some response from Jed Brown and Barry Smith. 
I can set the matrix entries of one sparse matrix very efficiently for 
the moment. I use MatSetValues and preallocate the array as explained in 
the manual for performance and set the rows in once in a loop. My actual 
goal was to set the entries of a matrix in blocks. Let me rephrase, say 
I would like to set the blocks of a matrix like
       1       1
1 [  Kaa  Kas ]               or     [  Kaa    0    ]              does 
not matter.
2 [    0     Kss ]                       [  Ksa   Kss  ]
Since I can read the matrices Kaa, Ksa, Kss, and their non-zeros, row 
and column indices of these matrices alone.

I thought that to get the same efficient setting of values, I can do 
some kind of block selection on a 'switch' where I can allocate the nnz, 
and values for the rows of this big matrix and set the row and column 
indices with some offsets. I mean sth like if blocks are on the same 
global row, say 1, change nnz for those rows in that block and allocate 
the necessary space and set the values of this array. But I thought this 
should be done before me by someone else, so just wanted some advice 
before coding. All blocks are pretty sparse if that helps from the point 
of view of ideas. If I do not do this preallocation, the performance 
decreases, as explained in the manual, drastically(not for this case but 
for stand alone matrices for instance.)

I appreciate any brighter ideas.


More information about the petsc-users mailing list