how do you access the nonzero elements in SeqAIJ matrices

Alex Peyser a.peyser at umiami.edu
Mon Aug 4 11:52:10 CDT 2008


It's to let you interleave calls. You can call MatAssemblyBegin as soon as 
you've finished assembling the matrix, but you need not call MatAssemblyEnd 
until you require more operations on A. 

So you can build A (call Begin), build B (call Begin), build a vector x and b, 
and only call MatAssemblyEnd when you use the solver on A. I've found that 
proper placement of MatAssemblyBegin and End make huge performance 
differences. If you can isolate all your write operations, just call 
MatAssemblyBegin once, and then call MatAssemblyEnd just before you require 
the matrix, you take a huge load off of interprocess communications. On the 
other hand, if you mix your read and writes so that you have many 
MatAssemblyBegin/End pairs, you'll get several orders of magnitude worse 
performance.

Like most programming -- think about what's going on at a low level. If you 
think about the involved MPI calls, there must be some sends or broadcasts 
going on, with a barrier at the end to synchronize the processes. The fewer 
sends and particularly the fewer barrier calls, the better performance you'll 
get.

If you get an error, very often it's that you've gotten your processes out of 
sync --- the constraints on which calls require a MatAssembly pair aren't in 
the documentation.

Regards,
Alex Peyser

On Monday 04 August 2008 12:09:16 pm Ahmed El Zein wrote:
> On Mon, 2008-08-04 at 23:13 +0800, Zi-Hao Wei wrote:
> > On Mon, Aug 4, 2008 at 10:53 PM, Ahmed El Zein <ahmed at azein.com> wrote:
> > > 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);
> >
> > I think that the code should be
> >   ierr = MatCreate(PETSC_COMM_WORLD,&A); CHKERRQ(ierr);
> >   ierr = MatSetSizes(A,M,N,M,N); CHKERRQ(ierr);
> >   ierr = MatSetType(A, MATSEQAIJ); CHKERRQ(ierr);
> >   [...lots of MatSetValue() calls...]
> >   ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
> >   ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
> >
> > The routines MatAssemblyBegin and MatAssemblyEnd should be called
> > after completing all calls to MatSetValues().
>
> You are right of course. But if these 2 calls are meant to be right
> after each other, why isn't there just one MatAssembly() call versus a
> Begin and End call?
>
> btw fixing this did not affect the problem I had.
>
> Ahmed


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20080804/b4305472/attachment.sig>


More information about the petsc-dev mailing list