[petsc-dev] simplification of solvers that utilize KSPNASH STCG or GLTR
Barry Smith
bsmith at mcs.anl.gov
Sun Aug 21 21:40:47 CDT 2016
Todd,
The TAO solvers that utilize KSPNASH STCG or GLTR have all this horribly redundant unneeded code
/* Solve the trust region subproblem */
if (NTR_KSP_NASH == tr->ksp_type) {
ierr = KSPNASHSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr);
ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr);
tao->ksp_its+=its;
tao->ksp_tot_its+=its;
ierr = KSPNASHGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr);
} else if (NTR_KSP_STCG == tr->ksp_type) {
ierr = KSPSTCGSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr);
ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr);
tao->ksp_its+=its;
tao->ksp_tot_its+=its;
ierr = KSPSTCGGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr);
} else { /* NTR_KSP_GLTR */
ierr = KSPGLTRSetRadius(tao->ksp,tao->trust);CHKERRQ(ierr);
ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr);
tao->ksp_its+=its;
tao->ksp_tot_its+=its;
ierr = KSPGLTRGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr);
}
This can be simplified a great deal by making the names
KSPCGSetRadius(), KSPGGGetNormD() etc you only need to change the registration from
ierr = PetscObjectComposeFunction((PetscObject)ksp,"KSPNASHSetRadius_C",KSPNASHSetRadius_NASH);CHKERRQ(ierr);
ierr = PetscObjectComposeFunction((PetscObject)ksp,"KSPNASHGetNormD_C",KSPNASHGetNormD_NASH);CHKERRQ(ierr);
ierr = PetscObjectComposeFunction((PetscObject)ksp,"KSPNASHGetObjFcn_C",KSPNASHGetObjFcn_NASH);CHKERRQ(ierr);
to
ierr = PetscObjectComposeFunction((PetscObject)ksp,"KSPCGSetRadius_C",KSPCGSetRadius_NASH);CHKERRQ(ierr);
ierr = PetscObjectComposeFunction((PetscObject)ksp,"KSPCGGetNormD_C",KSPCGGetNormD_NASH);CHKERRQ(ierr);
ierr = PetscObjectComposeFunction((PetscObject)ksp,"KSPCGGetObjFcn_C",KSPCGGetObjFcn_NASH);CHKERRQ(ierr);
Is there any reason we shouldn't do this? Essentially these three KSPs are a special subclass of KSPCG that supports these additional methods. Anything specific to an individual one like the Lanczo stuff still remains with it.
Barry
More information about the petsc-dev
mailing list