# LU in PETSc

Barry Smith bsmith at mcs.anl.gov
Wed Mar 18 12:27:35 CDT 2009

```    The shift nonzero causes a c*I to be added to the matrix, so
rather than solving A x = b it solves (A + c*I) z = b so, of course, z
is not generally going to be the same as x.
If you use a shift you do not want to use preonly as the KSP method
you should use something like GMRES. Also to get an accurate answer
use something like -ksp_rtol 1.e-12

Matlab and Lapack are doing column pivoting in the factorization
to avoid the zero pivot. PETSc's sparse factorization does not have
this functionality which is why it generated a zero pivot.

Barry

On Mar 18, 2009, at 11:32 AM, Waad Subber wrote:

> Hi,
> I am trying to do LU factorization to an ill-conditioned system.
> PETSc gives me "Detected zero pivot in LU factorization". The
> problem is solved when I use PCFactorSetShiftNonzero.
>
> Just to be sure, I used Matlab and Lapack(DGETRF) to do the
> factorization. They did it without any complain and they gave me the
> same answer. However, the answer I got from PETSc is completely
> different.
>
> In PETSc I did the following:
>
>       call KSPCreate(PETSC_COMM_SELF,ksp1,ierr)
>       call
> KSPSetOperators(ksp1,PSASTF,PSASTF,SAME_NONZERO_PATTERN,ierr)
>       call KSPSetType(ksp1,KSPPREONLY,ierr)
>       call KSPGetPC(ksp1,prec1,ierr)
>       call PCSetType(prec1,PCLU,ierr)
>       call PCFactorSetShiftNonzero(prec1,PETSC_DECIDE,IERR)
>       call KSPSetFromOptions(ksp1,ierr)
>       call KSPSetUp(ksp1,ierr)
>
> Any idea why PETSc gives different answer than Matlab and Lapack
> routine (DGETRF).
>
> Thanks
>