# Solve a linear system with BiCGStab instead of GMRES in PETSC

Below a piece of code is shown to solve a linear system.

ierr = KSPCreate(PETSC_COMM_WORLD,&ksp); CHKERRQ(ierr);
ierr = KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN); CHKERRQ(ierr);
KSPSetType(ksp,KSPBCGS);
ierr = KSPSetTolerances(ksp,1.e-2/((m+1)*(n+1)),1.e-50,PETSC_DEFAULT, PETSC_DEFAULT); CHKERRQ(ierr);
ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
ierr = PCSetType(pc,PCSOR);CHKERRQ(ierr);
ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);


According to the PETSC manual, PETSC uses GMRES with an ILU(0) preconditioner to solve SLE. I want to modify code to solve system using the BiCGStab method with a SSOR preconditioner.

Can someone help to do that?

• What's the variable ksp in your code? What's PETSC, GMRES, ILU(0), SLE, BiCGStab and SSOR?
– Juho
Commented Aug 20, 2013 at 10:33
• @Juho, These are terms of PETSC. Commented Aug 20, 2013 at 10:49

You can either do it from the code by doing this:

KSP  my_solver ;  // define the KSP method
PC   my_prec   ;  // define the preconditioner

/* Define the matrices to be used ... */

KSPCreate(comm,&my_solver);
KSPSetType(my_solver,KSPBCGS);  // Sets BiCGStab as the krylov method

KSPGetPC(my_solver,&my_prec);
PCSetType(my_prec,PCSOR);     // Sets SSOR as the preconditioner method

/* then continue with the SetType and Solve and other stuff ... */


Or by initializing the KSP method and Preconditioner as done before, then setting them using the command line by adding:

KSPSetFromOptions(my_solver);
KSPSetUp(my_solver);


The second way is more flexible of course, as you can specify your KSP method and PC regardless of what the code defines. Check this link to know more about how to set your values from the database

http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/KSP/KSPSetFromOptions.html#KSPSetFromOptions

The PETSc team always recommends that their users control solver options from the command line. The whole package is built with the idea of extreme flexibility in composing solvers and preconditioners, and the only way to achieve that is to use the command line scheme. You could get rid of setting the KSP and PC types in your code and use

ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);
ierr = KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
ierr = KSPSetTolerances(ksp,1.e-2/((m+1)*(n+1)),1.e-50,PETSC_DEFAULT,
PETSC_DEFAULT);
ierr = KSPSetFromOptions(ksp);
ierr = KSPSolve(ksp,b,x);


and run with

-ksp_type bcgsl -pc_type sor -pc_sor_its 10


KSPSetFromOptions(ksp) will override the default options with the options you have supplied here. So, instead of GMRES with ILU, it will use BiCGStab(l) with SSOR. Additional options to control SSOR can be found on the PETSc man pages:

http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/PC/PCSOR.html

If you insist on setting the options from code, use KSPSetType(ksp,KSPBCGS). Almost all PETSc run-time options have an equivalent functional interface, but it is only recommended when your application is making active decisions about which method to use.

• what means 10 in -pc_sor_its 10? Commented Aug 23, 2013 at 5:30
• @NurlanKenzhebekov That is the number of SOR iterations to be used in each application of the preconditioner. Commented Aug 23, 2013 at 12:44