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

ierr = KSPCreate(PETSC_COMM_WORLD,&ksp); CHKERRQ(ierr);
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?

  • $\begingroup$ What's the variable ksp in your code? What's PETSC, GMRES, ILU(0), SLE, BiCGStab and SSOR? $\endgroup$ – Juho Aug 20 '13 at 10:33
  • $\begingroup$ @Juho, These are terms of PETSC. $\endgroup$ – Nurlan Aug 20 '13 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 ... */ 

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

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:


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



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 = KSPSetTolerances(ksp,1.e-2/((m+1)*(n+1)),1.e-50,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:


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.

  • $\begingroup$ what means 10 in -pc_sor_its 10? $\endgroup$ – Nurlan Aug 23 '13 at 5:30
  • 1
    $\begingroup$ @NurlanKenzhebekov That is the number of SOR iterations to be used in each application of the preconditioner. $\endgroup$ – Jed Brown Aug 23 '13 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.