how do you access the nonzero elements in SeqAIJ matrices

Ahmed El Zein ahmed at azein.com
Mon Aug 4 10:52:07 CDT 2008


On Mon, 2008-08-04 at 10:12 -0500, Barry Smith wrote:
> >>>
> >>>
> >>>
> >>   Make sure this is a MPI_SeqAIJ matrix.
> > I thought that this would work with any seqaij matrix. does it have to
> > be MPISEQAIJ or can it just be MATSEQAIJ?
> >>
> 
>    Sorry, my mistake. Yes a MATSEQAIJ matrix.
> 
>     Barry
> 
> >>    It is only valid to access this data after you have put the values
> >> into
> >> the matrix and called MatAssemblyBegin() and MatAssemblyEnd().
> > my code looks like:
> >    ierr = MatCreate(PETSC_COMM_WORLD,&A); CHKERRQ(ierr);
> >    ierr = MatSetSizes(A,M,N,M,N); CHKERRQ(ierr);
> >    ierr = MatSetType(A, MATSEQAIJ); CHKERRQ(ierr);
> >    ierr =  MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
> >    [...lots of MatSetValue() calls...]
> >    ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
> >
> >> If you are creating a matrix from  i, j, and a look at
> >> MatCreateSeqAIJWithArrays()
> > I would like to extract the data from already assembled matrix (to  
> > copy
> > to a GPU actually).
> 
>     Ok, what is the code where you access the pointers?
> 
    int nz =21;
    Mat_SeqAIJ  *a = (Mat_SeqAIJ*)A->data;
    MatScalar *val = a->a;
    PetscInt  *ptr = a->i;
    PetscInt  *ind = a->j;

    for(i=0;i<M;i++)
        printf("ptr[%d] = %d\n",i,ptr[i]);

    for(i=0;i<nz;i++)
         printf("ind[%d] = %d\n",i,ind[i]);

    for(i=0;i<nz;i++)
        printf("val[%d]] = %lf\n",i,val[i]);


Ahmed

>     Barry
> 
> >
> >
> > Ahmed
> >>
> >>    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