zero out columns

Matthew Knepley knepley at
Fri Jan 18 08:34:43 CST 2008

I am guessing that you want to do this in order to

  1) enforce a Dirichlet condition (or other strong condition)

  2) maintain symmetry in the system

If not, maybe you are stuck with the solution below. However, if
you are setting boundary conditions, I would strongly advise you
to eliminate those variables up front. I have solved systems of
this kind for more than a decade and have tried every approach.
I believe the best one (except for active set optimization problems)
is to eliminate the constrained variables.

PETSc provides a nice mechanism for doing so. You must generate
some sort of map from degrees of freedom to Vec/Mat indices. For
constrained variables, map them to a negative index. Then when
you call MatSetValues/VecSetValues() they will be ignored automatically.
This is the bottom layer of my BC handling.



On Jan 18, 2008 4:32 AM, Toby D. Young <tyoung at> wrote:
> Hello users,
> The procedure I wish to program with petsc requires setting a series of
> matrix elements to zero, and for a given number of rows and columns.
> The operation is intended to run for large parallel sparse matrices.
> Using MatZeroRowsIS() I can easily achieve zeroing out the rows I want
> and now I am wondering how to zero out the columns. I imagine there are
> a few other ways of doing this, I've come up with two:
> Method 1
> MatGetColumnIJ()     // get a specified column
> VecSetValues()       // set wanted indices to zero
> MatRestoreColumnIJ() // restore vector to the matrix
> The Petsc manual warns "Since PETSc matrices are usually stored in
> compressed row format, this routine will generally be
> slow." (of MatGetColumnIJ).
> Method 2
> MatTranspose()       // Take the transpose of the matrix
> MatZeroRowsIS()      // Zeroi out the row which was the target column
> This requires creating a new matrix, i.e. the transpose and then
> releasing memory from the old matrix. The advantage is that it is
> relatively straightforward to code up.
> The rather obvious third way is to simply force the column elements to
> be zero with MatSetValues() which, I am guessing, is not likely to be
> efficient and I am not really considering this.
> I am wondering if anyone has a clue as to which of the methods above is
> likely to be more efficient for large parallel sparse matrices. Perhaps
> someone may have a suggesttion for an alternative approach.
> Thanks in advance.
> Best,
>         Toby
> --
> Toby D. Young - Adiunkt (Assistant Professor)
> Department of Computational Science
> Institute of Fundamental Technological Research
> Polish Academy of Sciences
> Room 206, Swietokrzyska 21
> 00-049 Warsaw, POLAND

What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which
their experiments lead.
-- Norbert Wiener

More information about the petsc-users mailing list