PetscErrorCode KSPView_LSQR(KSP ksp,PetscViewer viewer) { KSP_LSQR *lsqr = (KSP_LSQR*)ksp->data; PetscErrorCode ierr; PetscFunctionBegin; if (lsqr->se) { PetscReal rnorm; ierr = KSPLSQRGetStandardErrorVec(ksp,&lsqr->se);CHKERRQ(ierr); ierr = VecNorm(lsqr->se,NORM_2,&rnorm);CHKERRQ(ierr); PetscPrintf(PETSC_COMM_WORLD," Norm of Standard Error %A, Iterations %D\n",rnorm,ksp->its);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode KSPView_LSQR(KSP ksp,PetscViewer viewer) { KSP_LSQR *lsqr = (KSP_LSQR*)ksp->data; PetscErrorCode ierr; PetscBool iascii; PetscFunctionBegin; ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { if (lsqr->se) { PetscReal rnorm; ierr = KSPLSQRGetStandardErrorVec(ksp,&lsqr->se);CHKERRQ(ierr); ierr = VecNorm(lsqr->se,NORM_2,&rnorm);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," Norm of Standard Error %G, Iterations %D\n",rnorm,ksp->its);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
int main(int argc, char** argv) { Mat Q; Vec v, a, se; KSP QRsolver; PC pc; PetscErrorCode ierr; ierr = PetscInitialize(&argc, &argv, NULL, NULL);if (ierr) return ierr; ierr = VecCreate(PETSC_COMM_WORLD, &v);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD, &Q);CHKERRQ(ierr); ierr = MatSetType(Q, MATDENSE);CHKERRQ(ierr); ierr = fill(Q, v);CHKERRQ(ierr); ierr = MatCreateVecs(Q, &a, NULL);CHKERRQ(ierr); ierr = KSPCreate(PETSC_COMM_WORLD, &QRsolver);CHKERRQ(ierr); ierr = KSPGetPC(QRsolver, &pc);CHKERRQ(ierr); ierr = PCSetType(pc, PCNONE);CHKERRQ(ierr); ierr = KSPSetType(QRsolver, KSPLSQR);CHKERRQ(ierr); ierr = KSPSetFromOptions(QRsolver);CHKERRQ(ierr); ierr = KSPSetOperators(QRsolver, Q, Q);CHKERRQ(ierr); ierr = MatViewFromOptions(Q, NULL, "-sys_view");CHKERRQ(ierr); ierr = VecViewFromOptions(a, NULL, "-rhs_view");CHKERRQ(ierr); ierr = KSPSolve(QRsolver, v, a);CHKERRQ(ierr); ierr = KSPLSQRGetStandardErrorVec(QRsolver, &se);CHKERRQ(ierr); if (se) { ierr = VecViewFromOptions(se, NULL, "-se_view");CHKERRQ(ierr); } ierr = KSPDestroy(&QRsolver);CHKERRQ(ierr); ierr = VecDestroy(&a);CHKERRQ(ierr); ierr = VecDestroy(&v);CHKERRQ(ierr); ierr = MatDestroy(&Q);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }