[petsc-users] How to set KSP Solver Options at Runtime?
Matthew Knepley
knepley at gmail.com
Wed Jul 9 18:53:21 CDT 2014
On Thu, Jul 10, 2014 at 1:37 AM, Jin, Shuangshuang <
Shuangshuang.Jin at pnnl.gov> wrote:
> 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.
>
The preconditioner always depends on the particular problem being solved.
You will have to tell us more about it.
Thanks,
Matt
> Thanks,
> Shuangshuang
>
>
--
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20140710/6c37fb89/attachment.html>
More information about the petsc-users
mailing list