[petsc-users] How to set KSP Solver Options at Runtime?

Jin, Shuangshuang Shuangshuang.Jin at pnnl.gov
Wed Jul 9 18:37:19 CDT 2014


Hi, I'm using the KSP solver as following to solve a linear system Ax=b, where A is a 16767x16767 square matrix, b is a 16767 length vector.

EXTERN_C_BEGIN
extern PetscErrorCode PCCreate_Jacobi(PC);
EXTERN_C_END

static PetscErrorCode solvingAxb(Mat A, Vec b, PetscInt nbus, Vec &x, const int me)
{
  PetscErrorCode ierr;

  KSP ksp; // linear solver context
  Vec u; // exact solution
  PetscInt its;
  PetscReal norm; // norm of solution error
  PetscLogDouble t1, t2;
  PetscViewer viewer;
  PC pc; // preconditioner context
  PetscInt Istart, Iend;

  ierr = VecDuplicate(b, &u); CHKERRQ(ierr);
  ierr = VecDuplicate(b, &x); CHKERRQ(ierr);

  ierr = KSPCreate(PETSC_COMM_WORLD, &ksp); CHKERRQ(ierr);

  ierr = KSPSetOperators(ksp, A, A, DIFFERENT_NONZERO_PATTERN); CHKERRQ(ierr);

  PCRegister("ourjacobi", PCCreate_Jacobi);

  KSPGetPC(ksp, &pc);
  PCSetType(pc, "ourjacobi");

  ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr);

  ierr = KSPSolve(ksp, b, x); CHKERRQ(ierr);

  ierr = MatMult(A,x,u); CHKERRQ(ierr);
  ierr = VecAXPY(u, -1.0, b); CHKERRQ(ierr);
  ierr = VecNorm(u, NORM_2, &norm); CHKERRQ(ierr);
  ierr = KSPGetIterationNumber(ksp, &its); CHKERRQ(ierr);

  ierr = VecDestroy(&b); CHKERRQ(ierr);
  ierr = VecDestroy(&u); CHKERRQ(ierr);

  ierr = KSPDestroy(&ksp);CHKERRQ(ierr);

  PetscFunctionReturn(0);
}

I would like to know how to set the solver options at runtime to make it run faster, such as ksp_type, pc_type, and etc? It takes very long time to solve the system if I use no options.

Thanks,
Shuangshuang

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20140709/c8d479b9/attachment.html>


More information about the petsc-users mailing list