how do you access the nonzero elements in SeqAIJ matrices

Barry Smith bsmith at mcs.anl.gov
Mon Aug 4 07:57:12 CDT 2008


On Aug 4, 2008, at 3:14 AM, Ahmed El Zein wrote:

> I am working on a project where I would like to copy a sparse matrix  
> in
> CSR format.
>
> I have tried MatGetRow() which works OK but I would really like to get
> pointers to the 3 arrays directly.
>
> I also tried MatGetRowIJ() which allows me to get the i and j arrays  
> but
> I can't see how to access the nonzero elements.
>
> and finally I attempted to access the arrays directly like this:
> Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
> MatScalar *val = a->a;
> PetscInt  *ptr = a->i;
> PetscInt  *ind = a->j;
>
   Make sure this is a MPI_SeqAIJ matrix.

    It is only valid to access this data after you have put the values  
into
the matrix and called MatAssemblyBegin() and MatAssemblyEnd().
If you are creating a matrix from  i, j, and a look at  
MatCreateSeqAIJWithArrays()

    Barry


> However when accessing directly I get different values for ptr and
> SIGSEGV when accessing val or ind.
>
> also I get a bogus number for a->nz (134630032 instead of 21)
>
> Can someone please explain when I am doing wrong?
>
> Ahmed
>




More information about the petsc-dev mailing list