MGSolver_Status _GetSolveStatus( MGSolver_PETScData* mgData ) { PC pc; const KSPType kspType; const PCType pcType; KSPConvergedReason reason; PetscErrorCode ec; ec = KSPGetType( mgData->ksp, &kspType ); CheckPETScError( ec ); ec = KSPGetPC( mgData->ksp, &pc ); CheckPETScError( ec ); ec = PCGetType( pc, &pcType ); CheckPETScError( ec ); if( !strcmp( kspType, KSPRICHARDSON ) && !strcmp( pcType, PCSOR ) ) { double rnorm; PetscInt curIt; //rnorm = PETScMatrixSolver_GetResidualNorm( self ); //curIt = PETScMatrixSolver_GetIterations( self ); rnorm = _GetResidualNorm( mgData ); KSPGetIterationNumber( mgData->ksp, &curIt ); //PETScMatrixSolver_SetNormType( self, MultigridSolver_NormType_Preconditioned ); KSPSetNormType( mgData->ksp, MultigridSolver_NormType_Preconditioned ); ec = KSPDefaultConverged( mgData->ksp, curIt, (PetscScalar)rnorm, &reason, PETSC_NULL ); CheckPETScError( ec ); } else { ec = KSPGetConvergedReason( mgData->ksp, &reason ); CheckPETScError( ec ); } return reason; }
double _GetResidualNorm( MGSolver_PETScData* mgData ) { PC pc; const KSPType kspType; const PCType pcType; PetscScalar rnorm; PetscErrorCode ec; ec = KSPGetType( mgData->ksp, &kspType ); CheckPETScError( ec ); ec = KSPGetPC( mgData->ksp, &pc ); CheckPETScError( ec ); ec = PCGetType( pc, &pcType ); CheckPETScError( ec ); if( !strcmp( kspType, KSPRICHARDSON ) && !strcmp( pcType, PCSOR ) ) { Vec residual; //residual = MatrixSolver_GetResidual( mgData ); //rnorm = (PetscScalar)Vector_L2Norm( residual ); residual = _GetResidual( mgData ); VecNorm( residual, NORM_2, &rnorm ); } else { ec = KSPGetResidualNorm( mgData->ksp, &rnorm ); CheckPETScError( ec ); } return (double)rnorm; }
static PetscErrorCode TaoSetFromOptions_BQNLS(PetscOptionItems *PetscOptionsObject,Tao tao) { TAO_BNK *bnk = (TAO_BNK *)tao->data; TAO_BQNK *bqnk = (TAO_BQNK*)bnk->ctx; PetscErrorCode ierr; KSPType ksp_type; PetscBool is_spd; PetscFunctionBegin; ierr = PetscOptionsHead(PetscOptionsObject,"Quasi-Newton-Krylov method for bound constrained optimization");CHKERRQ(ierr); ierr = PetscOptionsEList("-tao_bqnls_as_type", "active set estimation method", "", BNK_AS, BNK_AS_TYPES, BNK_AS[bnk->as_type], &bnk->as_type, 0);CHKERRQ(ierr); ierr = PetscOptionsReal("-tao_bqnls_epsilon", "(developer) tolerance used when computing actual and predicted reduction", "", bnk->epsilon, &bnk->epsilon,NULL);CHKERRQ(ierr); ierr = PetscOptionsReal("-tao_bqnls_as_tol", "(developer) initial tolerance used when estimating actively bounded variables", "", bnk->as_tol, &bnk->as_tol,NULL);CHKERRQ(ierr); ierr = PetscOptionsReal("-tao_bqnls_as_step", "(developer) step length used when estimating actively bounded variables", "", bnk->as_step, &bnk->as_step,NULL);CHKERRQ(ierr); ierr = PetscOptionsInt("-tao_bqnls_max_cg_its", "number of BNCG iterations to take for each Newton step", "", bnk->max_cg_its, &bnk->max_cg_its,NULL);CHKERRQ(ierr); ierr = PetscOptionsTail();CHKERRQ(ierr); ierr = TaoSetFromOptions(bnk->bncg);CHKERRQ(ierr); ierr = TaoLineSearchSetFromOptions(tao->linesearch);CHKERRQ(ierr); ierr = KSPSetFromOptions(tao->ksp);CHKERRQ(ierr); ierr = KSPGetType(tao->ksp,&ksp_type);CHKERRQ(ierr); bnk->is_nash = bnk->is_gltr = bnk->is_stcg = PETSC_FALSE; ierr = MatSetFromOptions(bqnk->B);CHKERRQ(ierr); ierr = MatGetOption(bqnk->B, MAT_SPD, &is_spd);CHKERRQ(ierr); if (!is_spd) SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_INCOMP, "LMVM matrix must be symmetric positive-definite"); PetscFunctionReturn(0); }
int TaoLinearSolverPetsc::GetObjFcn(double *o_fcn) { const KSPType ktype; int info; PetscTruth flg; PetscFunctionBegin; info = KSPGetType(ksp, &ktype); CHKERRQ(info); info = PetscStrcmp((char *)ktype, KSPNASH, &flg); CHKERRQ(info); if (flg == PETSC_TRUE) { info = KSPNASHGetObjFcn(ksp, o_fcn); CHKERRQ(info); } info = PetscStrcmp((char *)ktype, KSPSTCG, &flg); CHKERRQ(info); if (flg == PETSC_TRUE) { info = KSPSTCGGetObjFcn(ksp, o_fcn); CHKERRQ(info); } info = PetscStrcmp((char *)ktype, KSPGLTR, &flg); CHKERRQ(info); if (flg == PETSC_TRUE) { info = KSPGLTRGetObjFcn(ksp, o_fcn); CHKERRQ(info); } PetscFunctionReturn(0); }
int TaoLinearSolverPetsc::SetTrustRadius(double rad) { const KSPType ktype; int info; PetscTruth flg; PetscFunctionBegin; info = KSPGetType(ksp, &ktype); CHKERRQ(info); info = PetscStrcmp((char *)ktype, KSPNASH, &flg); CHKERRQ(info); if (flg == PETSC_TRUE) { info = KSPNASHSetRadius(ksp, rad); CHKERRQ(info); } info = PetscStrcmp((char *)ktype, KSPSTCG, &flg); CHKERRQ(info); if (flg == PETSC_TRUE) { info = KSPSTCGSetRadius(ksp, rad); CHKERRQ(info); } info = PetscStrcmp((char *)ktype, KSPGLTR, &flg); CHKERRQ(info); if (flg == PETSC_TRUE) { info = KSPGLTRSetRadius(ksp, rad); CHKERRQ(info); } PetscFunctionReturn(0); }
PetscErrorCode STSetFromOptions_Shell(ST st) { PetscErrorCode ierr; PC pc; PCType pctype; KSPType ksptype; PetscFunctionBegin; if (!st->ksp) { ierr = STGetKSP(st,&st->ksp);CHKERRQ(ierr); } ierr = KSPGetPC(st->ksp,&pc);CHKERRQ(ierr); ierr = KSPGetType(st->ksp,&ksptype);CHKERRQ(ierr); ierr = PCGetType(pc,&pctype);CHKERRQ(ierr); if (!pctype && !ksptype) { if (st->shift_matrix == ST_MATMODE_SHELL) { /* in shell mode use GMRES with Jacobi as the default */ ierr = KSPSetType(st->ksp,KSPGMRES);CHKERRQ(ierr); ierr = PCSetType(pc,PCJACOBI);CHKERRQ(ierr); } else { /* use direct solver as default */ ierr = KSPSetType(st->ksp,KSPPREONLY);CHKERRQ(ierr); ierr = PCSetType(pc,PCREDUNDANT);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
/*@ PCMGGetSmootherUp - Gets the KSP context to be used as smoother after coarse grid correction (post-smoother). Not Collective, KSP returned is parallel if PC is Input Parameters: + pc - the multigrid context - l - the level (0 is coarsest) to supply Ouput Parameters: . ksp - the smoother Level: advanced Notes: calling this will result in a different pre and post smoother so you may need to set options on the pre smoother also .keywords: MG, multigrid, get, smoother, up, post-smoother, level .seealso: PCMGGetSmootherUp(), PCMGGetSmootherDown() @*/ PetscErrorCode PCMGGetSmootherUp(PC pc,PetscInt l,KSP *ksp) { PC_MG *mg = (PC_MG*)pc->data; PC_MG_Levels **mglevels = mg->levels; PetscErrorCode ierr; const char *prefix; MPI_Comm comm; PetscFunctionBegin; PetscValidHeaderSpecific(pc,PC_CLASSID,1); /* This is called only if user wants a different pre-smoother from post. Thus we check if a different one has already been allocated, if not we allocate it. */ if (!l) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"There is no such thing as a up smoother on the coarse grid"); if (mglevels[l]->smoothu == mglevels[l]->smoothd) { KSPType ksptype; PCType pctype; PC ipc; PetscReal rtol,abstol,dtol; PetscInt maxits; KSPNormType normtype; ierr = PetscObjectGetComm((PetscObject)mglevels[l]->smoothd,&comm);CHKERRQ(ierr); ierr = KSPGetOptionsPrefix(mglevels[l]->smoothd,&prefix);CHKERRQ(ierr); ierr = KSPGetTolerances(mglevels[l]->smoothd,&rtol,&abstol,&dtol,&maxits);CHKERRQ(ierr); ierr = KSPGetType(mglevels[l]->smoothd,&ksptype);CHKERRQ(ierr); ierr = KSPGetNormType(mglevels[l]->smoothd,&normtype);CHKERRQ(ierr); ierr = KSPGetPC(mglevels[l]->smoothd,&ipc);CHKERRQ(ierr); ierr = PCGetType(ipc,&pctype);CHKERRQ(ierr); ierr = KSPCreate(comm,&mglevels[l]->smoothu);CHKERRQ(ierr); ierr = KSPSetErrorIfNotConverged(mglevels[l]->smoothu,pc->erroriffailure);CHKERRQ(ierr); ierr = PetscObjectIncrementTabLevel((PetscObject)mglevels[l]->smoothu,(PetscObject)pc,mglevels[0]->levels-l);CHKERRQ(ierr); ierr = KSPSetOptionsPrefix(mglevels[l]->smoothu,prefix);CHKERRQ(ierr); ierr = KSPSetTolerances(mglevels[l]->smoothu,rtol,abstol,dtol,maxits);CHKERRQ(ierr); ierr = KSPSetType(mglevels[l]->smoothu,ksptype);CHKERRQ(ierr); ierr = KSPSetNormType(mglevels[l]->smoothu,normtype);CHKERRQ(ierr); ierr = KSPSetConvergenceTest(mglevels[l]->smoothu,KSPConvergedSkip,NULL,NULL);CHKERRQ(ierr); ierr = KSPGetPC(mglevels[l]->smoothu,&ipc);CHKERRQ(ierr); ierr = PCSetType(ipc,pctype);CHKERRQ(ierr); ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)mglevels[l]->smoothu);CHKERRQ(ierr); ierr = PetscObjectComposedDataSetInt((PetscObject) mglevels[l]->smoothu, PetscMGLevelId, mglevels[l]->level);CHKERRQ(ierr); } if (ksp) *ksp = mglevels[l]->smoothu; PetscFunctionReturn(0); }
int TaoLinearSolverPetsc::GetLambda(double *lambda) { const KSPType ktype; int info; PetscTruth flg; PetscFunctionBegin; *lambda = 0.0; info = KSPGetType(ksp, &ktype); CHKERRQ(info); info = PetscStrcmp((char *)ktype, KSPGLTR, &flg); CHKERRQ(info); if (flg == PETSC_TRUE) { info = KSPGLTRGetLambda(ksp, lambda); CHKERRQ(info); } PetscFunctionReturn(0); }
PetscErrorCode PCBDDCSubSchursSetUp(PCBDDCSubSchurs sub_schurs, Mat S, IS is_A_I, IS is_A_B, PetscInt ncc, IS is_cc[], PetscInt xadj[], PetscInt adjncy[], PetscInt nlayers) { Mat A_II,A_IB,A_BI,A_BB; ISLocalToGlobalMapping BtoNmap,ItoNmap; PetscBT touched; PetscInt i,n_I,n_B,n_local,*local_numbering; PetscBool is_sorted; PetscErrorCode ierr; PetscFunctionBegin; ierr = ISSorted(is_A_I,&is_sorted);CHKERRQ(ierr); if (!is_sorted) { SETERRQ(PetscObjectComm((PetscObject)is_A_I),PETSC_ERR_PLIB,"IS for I dofs should be shorted"); } ierr = ISSorted(is_A_B,&is_sorted);CHKERRQ(ierr); if (!is_sorted) { SETERRQ(PetscObjectComm((PetscObject)is_A_B),PETSC_ERR_PLIB,"IS for B dofs should be shorted"); } /* get sizes */ ierr = ISGetLocalSize(is_A_I,&n_I);CHKERRQ(ierr); ierr = ISGetLocalSize(is_A_B,&n_B);CHKERRQ(ierr); n_local = n_I+n_B; /* maps */ ierr = ISLocalToGlobalMappingCreateIS(is_A_B,&BtoNmap);CHKERRQ(ierr); if (nlayers >= 0 && xadj != NULL && adjncy != NULL) { /* I problems have a different size of the original ones */ ierr = ISLocalToGlobalMappingCreateIS(is_A_I,&ItoNmap);CHKERRQ(ierr); /* allocate some auxiliary space */ ierr = PetscMalloc1(n_local,&local_numbering);CHKERRQ(ierr); ierr = PetscBTCreate(n_local,&touched);CHKERRQ(ierr); } else { ItoNmap = 0; local_numbering = 0; touched = 0; } /* get Schur complement matrices */ ierr = MatSchurComplementGetSubMatrices(S,&A_II,NULL,&A_IB,&A_BI,&A_BB);CHKERRQ(ierr); /* allocate space for schur complements */ ierr = PetscMalloc5(ncc,&sub_schurs->is_AEj_I,ncc,&sub_schurs->is_AEj_B,ncc,&sub_schurs->S_Ej,ncc,&sub_schurs->work1,ncc,&sub_schurs->work2);CHKERRQ(ierr); sub_schurs->n_subs = ncc; /* cycle on subsets and extract schur complements */ for (i=0;i<sub_schurs->n_subs;i++) { Mat AE_II,AE_IE,AE_EI,AE_EE; IS is_I,is_subset_B; /* get IS for subsets in B numbering */ ierr = ISDuplicate(is_cc[i],&sub_schurs->is_AEj_B[i]);CHKERRQ(ierr); ierr = ISSort(sub_schurs->is_AEj_B[i]);CHKERRQ(ierr); ierr = ISGlobalToLocalMappingApplyIS(BtoNmap,IS_GTOLM_DROP,sub_schurs->is_AEj_B[i],&is_subset_B);CHKERRQ(ierr); /* BB block on subset */ ierr = MatGetSubMatrix(A_BB,is_subset_B,is_subset_B,MAT_INITIAL_MATRIX,&AE_EE);CHKERRQ(ierr); if (ItoNmap) { /* is ItoNmap has been computed, extracts only a part of I dofs */ const PetscInt* idx_B; PetscInt n_local_dofs,n_prev_added,j,layer,subset_size; /* all boundary dofs must be skipped when adding layers */ ierr = PetscBTMemzero(n_local,touched);CHKERRQ(ierr); ierr = ISGetIndices(is_A_B,&idx_B);CHKERRQ(ierr); for (j=0;j<n_B;j++) { ierr = PetscBTSet(touched,idx_B[j]);CHKERRQ(ierr); } ierr = ISRestoreIndices(is_A_B,&idx_B);CHKERRQ(ierr); /* add next layers of dofs */ ierr = ISGetLocalSize(is_cc[i],&subset_size);CHKERRQ(ierr); ierr = ISGetIndices(is_cc[i],&idx_B);CHKERRQ(ierr); ierr = PetscMemcpy(local_numbering,idx_B,subset_size*sizeof(PetscInt));CHKERRQ(ierr); ierr = ISRestoreIndices(is_cc[i],&idx_B);CHKERRQ(ierr); n_local_dofs = subset_size; n_prev_added = subset_size; for (layer=0;layer<nlayers;layer++) { PetscInt n_added; if (n_local_dofs == n_I+subset_size) break; if (n_local_dofs > n_I+subset_size) { SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Error querying layer %d. Out of bound access (%d > %d)",layer,n_local_dofs,n_I+subset_size); } ierr = PCBDDCAdjGetNextLayer_Private(local_numbering+n_local_dofs,n_prev_added,touched,xadj,adjncy,&n_added);CHKERRQ(ierr); n_prev_added = n_added; n_local_dofs += n_added; if (!n_added) break; } /* IS for I dofs in original numbering and in I numbering */ ierr = ISCreateGeneral(PetscObjectComm((PetscObject)ItoNmap),n_local_dofs-subset_size,local_numbering+subset_size,PETSC_COPY_VALUES,&sub_schurs->is_AEj_I[i]);CHKERRQ(ierr); ierr = ISSort(sub_schurs->is_AEj_I[i]);CHKERRQ(ierr); ierr = ISGlobalToLocalMappingApplyIS(ItoNmap,IS_GTOLM_DROP,sub_schurs->is_AEj_I[i],&is_I);CHKERRQ(ierr); /* II block */ ierr = MatGetSubMatrix(A_II,is_I,is_I,MAT_INITIAL_MATRIX,&AE_II);CHKERRQ(ierr); } else { /* in this case we can take references of already existing IS and matrices for I dofs */ /* IS for I dofs in original numbering */ ierr = PetscObjectReference((PetscObject)is_A_I);CHKERRQ(ierr); sub_schurs->is_AEj_I[i] = is_A_I; /* IS for I dofs in I numbering TODO: "first" argument of ISCreateStride is not general */ ierr = ISCreateStride(PetscObjectComm((PetscObject)is_A_I),n_I,0,1,&is_I);CHKERRQ(ierr); /* II block is the same */ ierr = PetscObjectReference((PetscObject)A_II);CHKERRQ(ierr); AE_II = A_II; } /* IE block */ ierr = MatGetSubMatrix(A_IB,is_I,is_subset_B,MAT_INITIAL_MATRIX,&AE_IE);CHKERRQ(ierr); /* EI block */ ierr = MatGetSubMatrix(A_BI,is_subset_B,is_I,MAT_INITIAL_MATRIX,&AE_EI);CHKERRQ(ierr); /* setup Schur complements on subset */ ierr = MatCreateSchurComplement(AE_II,AE_II,AE_IE,AE_EI,AE_EE,&sub_schurs->S_Ej[i]);CHKERRQ(ierr); ierr = MatGetVecs(sub_schurs->S_Ej[i],&sub_schurs->work1[i],&sub_schurs->work2[i]);CHKERRQ(ierr); if (AE_II == A_II) { /* we can reuse the same ksp */ KSP ksp; ierr = MatSchurComplementGetKSP(S,&ksp);CHKERRQ(ierr); ierr = MatSchurComplementSetKSP(sub_schurs->S_Ej[i],ksp);CHKERRQ(ierr); } else { /* build new ksp object which inherits ksp and pc types from the original one */ KSP origksp,schurksp; PC origpc,schurpc; KSPType ksp_type; PCType pc_type; PetscInt n_internal; ierr = MatSchurComplementGetKSP(S,&origksp);CHKERRQ(ierr); ierr = MatSchurComplementGetKSP(sub_schurs->S_Ej[i],&schurksp);CHKERRQ(ierr); ierr = KSPGetType(origksp,&ksp_type);CHKERRQ(ierr); ierr = KSPSetType(schurksp,ksp_type);CHKERRQ(ierr); ierr = KSPGetPC(schurksp,&schurpc);CHKERRQ(ierr); ierr = KSPGetPC(origksp,&origpc);CHKERRQ(ierr); ierr = PCGetType(origpc,&pc_type);CHKERRQ(ierr); ierr = PCSetType(schurpc,pc_type);CHKERRQ(ierr); ierr = ISGetSize(is_I,&n_internal);CHKERRQ(ierr); if (n_internal) { /* UMFPACK gives error with 0 sized problems */ MatSolverPackage solver=NULL; ierr = PCFactorGetMatSolverPackage(origpc,(const MatSolverPackage*)&solver);CHKERRQ(ierr); if (solver) { ierr = PCFactorSetMatSolverPackage(schurpc,solver);CHKERRQ(ierr); } } ierr = KSPSetUp(schurksp);CHKERRQ(ierr); } /* free */ ierr = MatDestroy(&AE_II);CHKERRQ(ierr); ierr = MatDestroy(&AE_EE);CHKERRQ(ierr); ierr = MatDestroy(&AE_IE);CHKERRQ(ierr); ierr = MatDestroy(&AE_EI);CHKERRQ(ierr); ierr = ISDestroy(&is_I);CHKERRQ(ierr); ierr = ISDestroy(&is_subset_B);CHKERRQ(ierr); } /* free */ ierr = ISLocalToGlobalMappingDestroy(&ItoNmap);CHKERRQ(ierr); ierr = ISLocalToGlobalMappingDestroy(&BtoNmap);CHKERRQ(ierr); ierr = PetscFree(local_numbering);CHKERRQ(ierr); ierr = PetscBTDestroy(&touched);CHKERRQ(ierr); PetscFunctionReturn(0); }
#if defined(PETSC_HAVE_FORTRAN_CAPS) #define kspgettype_ KSPGETTYPE #define kspsettype_ KSPSETTYPE #define kspview_ KSPVIEW #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) #define kspgettype_ kspgettype #define kspsettype_ kspsettype #define kspview_ kspview #endif PETSC_EXTERN void PETSC_STDCALL kspgettype_(KSP *ksp,char* name PETSC_MIXED_LEN(len),PetscErrorCode *ierr PETSC_END_LEN(len)) { const char *tname; *ierr = KSPGetType(*ksp,&tname);if (*ierr) return; *ierr = PetscStrncpy(name,tname,len); FIXRETURNCHAR(PETSC_TRUE,name,len); } PETSC_EXTERN void PETSC_STDCALL kspsettype_(KSP *ksp,char* type PETSC_MIXED_LEN(len),PetscErrorCode *ierr PETSC_END_LEN(len)) { char *t; FIXCHAR(type,len,t); *ierr = KSPSetType(*ksp,t);if (*ierr) return; FREECHAR(type,t); } PETSC_EXTERN void PETSC_STDCALL kspview_(KSP *ksp,PetscViewer *viewer, PetscErrorCode *ierr)
void PETScKrylovLinearSolver::initializeSolverState(const SAMRAIVectorReal<NDIM, double>& x, const SAMRAIVectorReal<NDIM, double>& b) { IBTK_TIMER_START(t_initialize_solver_state); int ierr; // Rudimentary error checking. #if !defined(NDEBUG) if (x.getNumberOfComponents() != b.getNumberOfComponents()) { TBOX_ERROR(d_object_name << "::initializeSolverState()\n" << " vectors must have the same number of components" << std::endl); } const Pointer<PatchHierarchy<NDIM> >& patch_hierarchy = x.getPatchHierarchy(); if (patch_hierarchy != b.getPatchHierarchy()) { TBOX_ERROR(d_object_name << "::initializeSolverState()\n" << " vectors must have the same hierarchy" << std::endl); } const int coarsest_ln = x.getCoarsestLevelNumber(); if (coarsest_ln < 0) { TBOX_ERROR(d_object_name << "::initializeSolverState()\n" << " coarsest level number must not be negative" << std::endl); } if (coarsest_ln != b.getCoarsestLevelNumber()) { TBOX_ERROR(d_object_name << "::initializeSolverState()\n" << " vectors must have same coarsest level number" << std::endl); } const int finest_ln = x.getFinestLevelNumber(); if (finest_ln < coarsest_ln) { TBOX_ERROR(d_object_name << "::initializeSolverState()\n" << " finest level number must be >= coarsest level number" << std::endl); } if (finest_ln != b.getFinestLevelNumber()) { TBOX_ERROR(d_object_name << "::initializeSolverState()\n" << " vectors must have same finest level number" << std::endl); } for (int ln = coarsest_ln; ln <= finest_ln; ++ln) { if (!patch_hierarchy->getPatchLevel(ln)) { TBOX_ERROR(d_object_name << "::initializeSolverState()\n" << " hierarchy level " << ln << " does not exist" << std::endl); } } #endif // Deallocate the solver state if the solver is already initialized. if (d_is_initialized) { d_reinitializing_solver = true; deallocateSolverState(); } // Create the KSP solver. if (d_managing_petsc_ksp) { ierr = KSPCreate(d_petsc_comm, &d_petsc_ksp); IBTK_CHKERRQ(ierr); resetKSPOptions(); } else if (!d_petsc_ksp) { TBOX_ERROR(d_object_name << "::initializeSolverState()\n" << " cannot initialize solver state for wrapped PETSc KSP object " "if the wrapped object is NULL" << std::endl); } // Setup solution and rhs vectors. d_x = x.cloneVector(x.getName()); d_petsc_x = PETScSAMRAIVectorReal::createPETScVector(d_x, d_petsc_comm); d_b = b.cloneVector(b.getName()); d_petsc_b = PETScSAMRAIVectorReal::createPETScVector(d_b, d_petsc_comm); // Initialize the linear operator and preconditioner objects. if (d_A) d_A->initializeOperatorState(*d_x, *d_b); if (d_managing_petsc_ksp || d_user_provided_mat) resetKSPOperators(); if (d_pc_solver) d_pc_solver->initializeSolverState(*d_x, *d_b); if (d_managing_petsc_ksp || d_user_provided_pc) resetKSPPC(); // Set the KSP options from the PETSc options database. if (d_options_prefix != "") { ierr = KSPSetOptionsPrefix(d_petsc_ksp, d_options_prefix.c_str()); IBTK_CHKERRQ(ierr); } ierr = KSPSetFromOptions(d_petsc_ksp); IBTK_CHKERRQ(ierr); // Reset the member state variables to correspond to the values used by the // KSP object. (Command-line options always take precedence.) const char* ksp_type; ierr = KSPGetType(d_petsc_ksp, &ksp_type); IBTK_CHKERRQ(ierr); d_ksp_type = ksp_type; PetscBool initial_guess_nonzero; ierr = KSPGetInitialGuessNonzero(d_petsc_ksp, &initial_guess_nonzero); IBTK_CHKERRQ(ierr); d_initial_guess_nonzero = (initial_guess_nonzero == PETSC_TRUE); ierr = KSPGetTolerances( d_petsc_ksp, &d_rel_residual_tol, &d_abs_residual_tol, NULL, &d_max_iterations); IBTK_CHKERRQ(ierr); // Configure the nullspace object. resetKSPNullspace(); // Indicate that the solver is initialized. d_reinitializing_solver = false; d_is_initialized = true; IBTK_TIMER_STOP(t_initialize_solver_state); return; } // initializeSolverState
int main(int argc,char **args) { Mat mat; /* matrix */ Vec b,ustar,u; /* vectors (RHS, exact solution, approx solution) */ PC pc; /* PC context */ KSP ksp; /* KSP context */ PetscErrorCode ierr; PetscInt n = 10,i,its,col[3]; PetscScalar value[3]; PCType pcname; KSPType kspname; PetscReal norm,tol=1000.*PETSC_MACHINE_EPSILON; PetscInitialize(&argc,&args,(char*)0,help); /* Create and initialize vectors */ ierr = VecCreateSeq(PETSC_COMM_SELF,n,&b);CHKERRQ(ierr); ierr = VecCreateSeq(PETSC_COMM_SELF,n,&ustar);CHKERRQ(ierr); ierr = VecCreateSeq(PETSC_COMM_SELF,n,&u);CHKERRQ(ierr); ierr = VecSet(ustar,1.0);CHKERRQ(ierr); ierr = VecSet(u,0.0);CHKERRQ(ierr); /* Create and assemble matrix */ ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,n,n,3,NULL,&mat);CHKERRQ(ierr); value[0] = -1.0; value[1] = 2.0; value[2] = -1.0; for (i=1; i<n-1; i++) { col[0] = i-1; col[1] = i; col[2] = i+1; ierr = MatSetValues(mat,1,&i,3,col,value,INSERT_VALUES);CHKERRQ(ierr); } i = n - 1; col[0] = n - 2; col[1] = n - 1; ierr = MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr); i = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0; ierr = MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr); ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); /* Compute right-hand-side vector */ ierr = MatMult(mat,ustar,b);CHKERRQ(ierr); /* Create PC context and set up data structures */ ierr = PCCreate(PETSC_COMM_WORLD,&pc);CHKERRQ(ierr); ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); ierr = PCSetFromOptions(pc);CHKERRQ(ierr); ierr = PCSetOperators(pc,mat,mat);CHKERRQ(ierr); ierr = PCSetUp(pc);CHKERRQ(ierr); /* Create KSP context and set up data structures */ ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr); ierr = KSPSetType(ksp,KSPRICHARDSON);CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); ierr = PCSetOperators(pc,mat,mat);CHKERRQ(ierr); ierr = KSPSetPC(ksp,pc);CHKERRQ(ierr); ierr = KSPSetUp(ksp);CHKERRQ(ierr); /* Solve the problem */ ierr = KSPGetType(ksp,&kspname);CHKERRQ(ierr); ierr = PCGetType(pc,&pcname);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_SELF,"Running %s with %s preconditioning\n",kspname,pcname);CHKERRQ(ierr); ierr = KSPSolve(ksp,b,u);CHKERRQ(ierr); ierr = VecAXPY(u,-1.0,ustar);CHKERRQ(ierr); ierr = VecNorm(u,NORM_2,&norm); ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr); if (norm > tol) { ierr = PetscPrintf(PETSC_COMM_SELF,"2 norm of error %g Number of iterations %D\n",(double)norm,its);CHKERRQ(ierr); } /* Free data structures */ ierr = KSPDestroy(&ksp);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = VecDestroy(&ustar);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = MatDestroy(&mat);CHKERRQ(ierr); ierr = PCDestroy(&pc);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
/* TaoSolve_NTR - Implements Newton's Method with a trust region approach for solving unconstrained minimization problems. The basic algorithm is taken from MINPACK-2 (dstrn). TaoSolve_NTR computes a local minimizer of a twice differentiable function f by applying a trust region variant of Newton's method. At each stage of the algorithm, we use the prconditioned conjugate gradient method to determine an approximate minimizer of the quadratic equation q(s) = <s, Hs + g> subject to the trust region constraint || s ||_M <= radius, where radius is the trust region radius and M is a symmetric positive definite matrix (the preconditioner). Here g is the gradient and H is the Hessian matrix. Note: TaoSolve_NTR MUST use the iterative solver KSPCGNASH, KSPCGSTCG, or KSPCGGLTR. Thus, we set KSPCGNASH, KSPCGSTCG, or KSPCGGLTR in this routine regardless of what the user may have previously specified. */ static PetscErrorCode TaoSolve_NTR(Tao tao) { TAO_NTR *tr = (TAO_NTR *)tao->data; KSPType ksp_type; PetscBool is_nash,is_stcg,is_gltr; KSPConvergedReason ksp_reason; PC pc; TaoConvergedReason reason; PetscReal fmin, ftrial, prered, actred, kappa, sigma, beta; PetscReal tau, tau_1, tau_2, tau_max, tau_min, max_radius; PetscReal f, gnorm; PetscReal delta; PetscReal norm_d; PetscErrorCode ierr; PetscInt bfgsUpdates = 0; PetscInt needH; PetscInt i_max = 5; PetscInt j_max = 1; PetscInt i, j, N, n, its; PetscFunctionBegin; if (tao->XL || tao->XU || tao->ops->computebounds) { ierr = PetscPrintf(((PetscObject)tao)->comm,"WARNING: Variable bounds have been set but will be ignored by ntr algorithm\n");CHKERRQ(ierr); } ierr = KSPGetType(tao->ksp,&ksp_type);CHKERRQ(ierr); ierr = PetscStrcmp(ksp_type,KSPCGNASH,&is_nash);CHKERRQ(ierr); ierr = PetscStrcmp(ksp_type,KSPCGSTCG,&is_stcg);CHKERRQ(ierr); ierr = PetscStrcmp(ksp_type,KSPCGGLTR,&is_gltr);CHKERRQ(ierr); if (!is_nash && !is_stcg && !is_gltr) { SETERRQ(PETSC_COMM_SELF,1,"TAO_NTR requires nash, stcg, or gltr for the KSP"); } /* Initialize the radius and modify if it is too large or small */ tao->trust = tao->trust0; tao->trust = PetscMax(tao->trust, tr->min_radius); tao->trust = PetscMin(tao->trust, tr->max_radius); if (NTR_PC_BFGS == tr->pc_type && !tr->M) { ierr = VecGetLocalSize(tao->solution,&n);CHKERRQ(ierr); ierr = VecGetSize(tao->solution,&N);CHKERRQ(ierr); ierr = MatCreateLMVM(((PetscObject)tao)->comm,n,N,&tr->M);CHKERRQ(ierr); ierr = MatLMVMAllocateVectors(tr->M,tao->solution);CHKERRQ(ierr); } /* Check convergence criteria */ ierr = TaoComputeObjectiveAndGradient(tao, tao->solution, &f, tao->gradient);CHKERRQ(ierr); ierr = TaoGradientNorm(tao, tao->gradient,NORM_2,&gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1,"User provided compute function generated Inf or NaN"); needH = 1; ierr = TaoMonitor(tao, tao->niter, f, gnorm, 0.0, 1.0, &reason);CHKERRQ(ierr); if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0); /* Create vectors for the limited memory preconditioner */ if ((NTR_PC_BFGS == tr->pc_type) && (BFGS_SCALE_BFGS != tr->bfgs_scale_type)) { if (!tr->Diag) { ierr = VecDuplicate(tao->solution, &tr->Diag);CHKERRQ(ierr); } } /* Modify the preconditioner to use the bfgs approximation */ ierr = KSPGetPC(tao->ksp, &pc);CHKERRQ(ierr); switch(tr->pc_type) { case NTR_PC_NONE: ierr = PCSetType(pc, PCNONE);CHKERRQ(ierr); ierr = PCSetFromOptions(pc);CHKERRQ(ierr); break; case NTR_PC_AHESS: ierr = PCSetType(pc, PCJACOBI);CHKERRQ(ierr); ierr = PCSetFromOptions(pc);CHKERRQ(ierr); ierr = PCJacobiSetUseAbs(pc,PETSC_TRUE);CHKERRQ(ierr); break; case NTR_PC_BFGS: ierr = PCSetType(pc, PCSHELL);CHKERRQ(ierr); ierr = PCSetFromOptions(pc);CHKERRQ(ierr); ierr = PCShellSetName(pc, "bfgs");CHKERRQ(ierr); ierr = PCShellSetContext(pc, tr->M);CHKERRQ(ierr); ierr = PCShellSetApply(pc, MatLMVMSolveShell);CHKERRQ(ierr); break; default: /* Use the pc method set by pc_type */ break; } /* Initialize trust-region radius */ switch(tr->init_type) { case NTR_INIT_CONSTANT: /* Use the initial radius specified */ break; case NTR_INIT_INTERPOLATION: /* Use the initial radius specified */ max_radius = 0.0; for (j = 0; j < j_max; ++j) { fmin = f; sigma = 0.0; if (needH) { ierr = TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);CHKERRQ(ierr); needH = 0; } for (i = 0; i < i_max; ++i) { ierr = VecCopy(tao->solution, tr->W);CHKERRQ(ierr); ierr = VecAXPY(tr->W, -tao->trust/gnorm, tao->gradient);CHKERRQ(ierr); ierr = TaoComputeObjective(tao, tr->W, &ftrial);CHKERRQ(ierr); if (PetscIsInfOrNanReal(ftrial)) { tau = tr->gamma1_i; } else { if (ftrial < fmin) { fmin = ftrial; sigma = -tao->trust / gnorm; } ierr = MatMult(tao->hessian, tao->gradient, tao->stepdirection);CHKERRQ(ierr); ierr = VecDot(tao->gradient, tao->stepdirection, &prered);CHKERRQ(ierr); prered = tao->trust * (gnorm - 0.5 * tao->trust * prered / (gnorm * gnorm)); actred = f - ftrial; if ((PetscAbsScalar(actred) <= tr->epsilon) && (PetscAbsScalar(prered) <= tr->epsilon)) { kappa = 1.0; } else { kappa = actred / prered; } tau_1 = tr->theta_i * gnorm * tao->trust / (tr->theta_i * gnorm * tao->trust + (1.0 - tr->theta_i) * prered - actred); tau_2 = tr->theta_i * gnorm * tao->trust / (tr->theta_i * gnorm * tao->trust - (1.0 + tr->theta_i) * prered + actred); tau_min = PetscMin(tau_1, tau_2); tau_max = PetscMax(tau_1, tau_2); if (PetscAbsScalar(kappa - 1.0) <= tr->mu1_i) { /* Great agreement */ max_radius = PetscMax(max_radius, tao->trust); if (tau_max < 1.0) { tau = tr->gamma3_i; } else if (tau_max > tr->gamma4_i) { tau = tr->gamma4_i; } else { tau = tau_max; } } else if (PetscAbsScalar(kappa - 1.0) <= tr->mu2_i) { /* Good agreement */ max_radius = PetscMax(max_radius, tao->trust); if (tau_max < tr->gamma2_i) { tau = tr->gamma2_i; } else if (tau_max > tr->gamma3_i) { tau = tr->gamma3_i; } else { tau = tau_max; } } else { /* Not good agreement */ if (tau_min > 1.0) { tau = tr->gamma2_i; } else if (tau_max < tr->gamma1_i) { tau = tr->gamma1_i; } else if ((tau_min < tr->gamma1_i) && (tau_max >= 1.0)) { tau = tr->gamma1_i; } else if ((tau_1 >= tr->gamma1_i) && (tau_1 < 1.0) && ((tau_2 < tr->gamma1_i) || (tau_2 >= 1.0))) { tau = tau_1; } else if ((tau_2 >= tr->gamma1_i) && (tau_2 < 1.0) && ((tau_1 < tr->gamma1_i) || (tau_2 >= 1.0))) { tau = tau_2; } else { tau = tau_max; } } } tao->trust = tau * tao->trust; } if (fmin < f) { f = fmin; ierr = VecAXPY(tao->solution, sigma, tao->gradient);CHKERRQ(ierr); ierr = TaoComputeGradient(tao,tao->solution, tao->gradient);CHKERRQ(ierr); ierr = TaoGradientNorm(tao, tao->gradient,NORM_2,&gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); needH = 1; ierr = TaoMonitor(tao, tao->niter, f, gnorm, 0.0, 1.0, &reason);CHKERRQ(ierr); if (reason != TAO_CONTINUE_ITERATING) { PetscFunctionReturn(0); } } } tao->trust = PetscMax(tao->trust, max_radius); /* Modify the radius if it is too large or small */ tao->trust = PetscMax(tao->trust, tr->min_radius); tao->trust = PetscMin(tao->trust, tr->max_radius); break; default: /* Norm of the first direction will initialize radius */ tao->trust = 0.0; break; } /* Set initial scaling for the BFGS preconditioner This step is done after computing the initial trust-region radius since the function value may have decreased */ if (NTR_PC_BFGS == tr->pc_type) { if (f != 0.0) { delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(tr->M,delta);CHKERRQ(ierr); } /* Have not converged; continue with Newton method */ while (reason == TAO_CONTINUE_ITERATING) { ++tao->niter; tao->ksp_its=0; /* Compute the Hessian */ if (needH) { ierr = TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);CHKERRQ(ierr); needH = 0; } if (NTR_PC_BFGS == tr->pc_type) { if (BFGS_SCALE_AHESS == tr->bfgs_scale_type) { /* Obtain diagonal for the bfgs preconditioner */ ierr = MatGetDiagonal(tao->hessian, tr->Diag);CHKERRQ(ierr); ierr = VecAbs(tr->Diag);CHKERRQ(ierr); ierr = VecReciprocal(tr->Diag);CHKERRQ(ierr); ierr = MatLMVMSetScale(tr->M,tr->Diag);CHKERRQ(ierr); } /* Update the limited memory preconditioner */ ierr = MatLMVMUpdate(tr->M, tao->solution, tao->gradient);CHKERRQ(ierr); ++bfgsUpdates; } while (reason == TAO_CONTINUE_ITERATING) { ierr = KSPSetOperators(tao->ksp, tao->hessian, tao->hessian_pre);CHKERRQ(ierr); /* Solve the trust region subproblem */ ierr = KSPCGSetRadius(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 = KSPCGGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr); if (0.0 == tao->trust) { /* Radius was uninitialized; use the norm of the direction */ if (norm_d > 0.0) { tao->trust = norm_d; /* Modify the radius if it is too large or small */ tao->trust = PetscMax(tao->trust, tr->min_radius); tao->trust = PetscMin(tao->trust, tr->max_radius); } else { /* The direction was bad; set radius to default value and re-solve the trust-region subproblem to get a direction */ tao->trust = tao->trust0; /* Modify the radius if it is too large or small */ tao->trust = PetscMax(tao->trust, tr->min_radius); tao->trust = PetscMin(tao->trust, tr->max_radius); ierr = KSPCGSetRadius(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 = KSPCGGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr); if (norm_d == 0.0) SETERRQ(PETSC_COMM_SELF,1, "Initial direction zero"); } } ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr); ierr = KSPGetConvergedReason(tao->ksp, &ksp_reason);CHKERRQ(ierr); if ((KSP_DIVERGED_INDEFINITE_PC == ksp_reason) && (NTR_PC_BFGS == tr->pc_type) && (bfgsUpdates > 1)) { /* Preconditioner is numerically indefinite; reset the approximate if using BFGS preconditioning. */ if (f != 0.0) { delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(tr->M, delta);CHKERRQ(ierr); ierr = MatLMVMReset(tr->M);CHKERRQ(ierr); ierr = MatLMVMUpdate(tr->M, tao->solution, tao->gradient);CHKERRQ(ierr); bfgsUpdates = 1; } if (NTR_UPDATE_REDUCTION == tr->update_type) { /* Get predicted reduction */ ierr = KSPCGGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr); if (prered >= 0.0) { /* The predicted reduction has the wrong sign. This cannot happen in infinite precision arithmetic. Step should be rejected! */ tao->trust = tr->alpha1 * PetscMin(tao->trust, norm_d); } else { /* Compute trial step and function value */ ierr = VecCopy(tao->solution,tr->W);CHKERRQ(ierr); ierr = VecAXPY(tr->W, 1.0, tao->stepdirection);CHKERRQ(ierr); ierr = TaoComputeObjective(tao, tr->W, &ftrial);CHKERRQ(ierr); if (PetscIsInfOrNanReal(ftrial)) { tao->trust = tr->alpha1 * PetscMin(tao->trust, norm_d); } else { /* Compute and actual reduction */ actred = f - ftrial; prered = -prered; if ((PetscAbsScalar(actred) <= tr->epsilon) && (PetscAbsScalar(prered) <= tr->epsilon)) { kappa = 1.0; } else { kappa = actred / prered; } /* Accept or reject the step and update radius */ if (kappa < tr->eta1) { /* Reject the step */ tao->trust = tr->alpha1 * PetscMin(tao->trust, norm_d); } else { /* Accept the step */ if (kappa < tr->eta2) { /* Marginal bad step */ tao->trust = tr->alpha2 * PetscMin(tao->trust, norm_d); } else if (kappa < tr->eta3) { /* Reasonable step */ tao->trust = tr->alpha3 * tao->trust; } else if (kappa < tr->eta4) { /* Good step */ tao->trust = PetscMax(tr->alpha4 * norm_d, tao->trust); } else { /* Very good step */ tao->trust = PetscMax(tr->alpha5 * norm_d, tao->trust); } break; } } } } else { /* Get predicted reduction */ ierr = KSPCGGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr); if (prered >= 0.0) { /* The predicted reduction has the wrong sign. This cannot happen in infinite precision arithmetic. Step should be rejected! */ tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d); } else { ierr = VecCopy(tao->solution, tr->W);CHKERRQ(ierr); ierr = VecAXPY(tr->W, 1.0, tao->stepdirection);CHKERRQ(ierr); ierr = TaoComputeObjective(tao, tr->W, &ftrial);CHKERRQ(ierr); if (PetscIsInfOrNanReal(ftrial)) { tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d); } else { ierr = VecDot(tao->gradient, tao->stepdirection, &beta);CHKERRQ(ierr); actred = f - ftrial; prered = -prered; if ((PetscAbsScalar(actred) <= tr->epsilon) && (PetscAbsScalar(prered) <= tr->epsilon)) { kappa = 1.0; } else { kappa = actred / prered; } tau_1 = tr->theta * beta / (tr->theta * beta - (1.0 - tr->theta) * prered + actred); tau_2 = tr->theta * beta / (tr->theta * beta + (1.0 + tr->theta) * prered - actred); tau_min = PetscMin(tau_1, tau_2); tau_max = PetscMax(tau_1, tau_2); if (kappa >= 1.0 - tr->mu1) { /* Great agreement; accept step and update radius */ if (tau_max < 1.0) { tao->trust = PetscMax(tao->trust, tr->gamma3 * norm_d); } else if (tau_max > tr->gamma4) { tao->trust = PetscMax(tao->trust, tr->gamma4 * norm_d); } else { tao->trust = PetscMax(tao->trust, tau_max * norm_d); } break; } else if (kappa >= 1.0 - tr->mu2) { /* Good agreement */ if (tau_max < tr->gamma2) { tao->trust = tr->gamma2 * PetscMin(tao->trust, norm_d); } else if (tau_max > tr->gamma3) { tao->trust = PetscMax(tao->trust, tr->gamma3 * norm_d); } else if (tau_max < 1.0) { tao->trust = tau_max * PetscMin(tao->trust, norm_d); } else { tao->trust = PetscMax(tao->trust, tau_max * norm_d); } break; } else { /* Not good agreement */ if (tau_min > 1.0) { tao->trust = tr->gamma2 * PetscMin(tao->trust, norm_d); } else if (tau_max < tr->gamma1) { tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d); } else if ((tau_min < tr->gamma1) && (tau_max >= 1.0)) { tao->trust = tr->gamma1 * PetscMin(tao->trust, norm_d); } else if ((tau_1 >= tr->gamma1) && (tau_1 < 1.0) && ((tau_2 < tr->gamma1) || (tau_2 >= 1.0))) { tao->trust = tau_1 * PetscMin(tao->trust, norm_d); } else if ((tau_2 >= tr->gamma1) && (tau_2 < 1.0) && ((tau_1 < tr->gamma1) || (tau_2 >= 1.0))) { tao->trust = tau_2 * PetscMin(tao->trust, norm_d); } else { tao->trust = tau_max * PetscMin(tao->trust, norm_d); } } } } } /* The step computed was not good and the radius was decreased. Monitor the radius to terminate. */ ierr = TaoMonitor(tao, tao->niter, f, gnorm, 0.0, tao->trust, &reason);CHKERRQ(ierr); } /* The radius may have been increased; modify if it is too large */ tao->trust = PetscMin(tao->trust, tr->max_radius); if (reason == TAO_CONTINUE_ITERATING) { ierr = VecCopy(tr->W, tao->solution);CHKERRQ(ierr); f = ftrial; ierr = TaoComputeGradient(tao, tao->solution, tao->gradient);CHKERRQ(ierr); ierr = TaoGradientNorm(tao, tao->gradient,NORM_2,&gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); needH = 1; ierr = TaoMonitor(tao, tao->niter, f, gnorm, 0.0, tao->trust, &reason);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
void PETScLinearSolver::Solver() { //TEST #ifdef TEST_MEM_PETSC PetscLogDouble mem1, mem2; PetscMemoryGetCurrentUsage(&mem1); #endif /* //TEST PetscViewer viewer; PetscViewerASCIIOpen(PETSC_COMM_WORLD, "x.txt", &viewer); PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB); PetscObjectSetName((PetscObject)x,"Solution"); VecView(x, viewer); */ int its; PetscLogDouble v1,v2; KSPConvergedReason reason; // #define PETSC34 //kg44 quick fix to compile PETSC with version PETSCV3.4 #ifdef USEPETSC34 PetscTime(&v1); #else PetscGetTime(&v1); #endif #if (PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR > 4) KSPSetOperators(lsolver, A, A); #else KSPSetOperators(lsolver, A, A, DIFFERENT_NONZERO_PATTERN); #endif KSPSolve(lsolver, b, x); KSPGetConvergedReason(lsolver,&reason); //CHKERRQ(ierr); if (reason==KSP_DIVERGED_INDEFINITE_PC) { PetscPrintf(PETSC_COMM_WORLD,"\nDivergence because of indefinite preconditioner;\n"); PetscPrintf(PETSC_COMM_WORLD,"Run the executable again but with -pc_factor_shift_positive_definite option.\n"); } else if (reason<0) { PetscPrintf(PETSC_COMM_WORLD,"\nOther kind of divergence: this should not happen.\n"); } else { const char *slv_type; const char *prc_type; KSPGetType(lsolver, &slv_type); PCGetType(prec, &prc_type); PetscPrintf(PETSC_COMM_WORLD,"\n================================================"); PetscPrintf(PETSC_COMM_WORLD, "\nLinear solver %s with %s preconditioner", slv_type, prc_type); KSPGetIterationNumber(lsolver,&its); //CHKERRQ(ierr); PetscPrintf(PETSC_COMM_WORLD,"\nConvergence in %d iterations.\n",(int)its); PetscPrintf(PETSC_COMM_WORLD,"\n================================================"); } PetscPrintf(PETSC_COMM_WORLD,"\n"); //VecAssemblyBegin(x); //VecAssemblyEnd(x); //kg44 quick fix to compile PETSC with version PETSCV3.4 #ifdef USEPETSC34 PetscTime(&v2); #else PetscGetTime(&v2); #endif time_elapsed += v2-v1; #define aTEST_OUT #ifdef TEST_OUT //TEST PetscViewer viewer; PetscViewerASCIIOpen(PETSC_COMM_WORLD, "x2.txt", &viewer); PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB); PetscObjectSetName((PetscObject)A,"Matrix"); MatView(A, viewer); PetscObjectSetName((PetscObject)x,"Solution"); VecView(x, viewer); PetscObjectSetName((PetscObject)b,"RHS"); VecView(b, viewer); VecDestroy(&b); VecDestroy(&x); MatDestroy(&A); if(lsolver) KSPDestroy(&lsolver); // if(prec) PCDestroy(&prec); if(global_x0) delete [] global_x0; if(global_x1) delete [] global_x1; PetscFinalize(); exit(0); #endif #ifdef TEST_MEM_PETSC //TEST PetscMemoryGetCurrentUsage(&mem2); PetscPrintf(PETSC_COMM_WORLD, "###Memory usage by solver. Before :%f After:%f Increase:%d\n", mem1, mem2, (int)(mem2 - mem1)); #endif }
std::pair<std::string,std::string> RBConstructionBase<Base>::set_alternative_solver (AutoPtr<LinearSolver<Number> >& #ifdef LIBMESH_HAVE_PETSC ls #endif ) { // It seems that setting it this generic way has no effect... // PreconditionerType orig_pc = this->linear_solver->preconditioner_type(); // this->linear_solver->set_preconditioner_type(AMG_PRECOND); // so we do it the "hard" way. std::string orig_petsc_pc_type_string, orig_petsc_ksp_type_string; #ifdef LIBMESH_HAVE_PETSC // ... but we can set it the "hard" way PetscLinearSolver<Number>* petsc_linear_solver = libmesh_cast_ptr<PetscLinearSolver<Number>*>(ls.get()); // Note: #define PCType char*, and PCGetType just sets a pointer. We'll use // the string below to make a real copy, and set the PC back to its original // type at the end of the function. #if PETSC_VERSION_LESS_THAN(3,0,0) || !PETSC_VERSION_LESS_THAN(3,4,0) // Pre-3.0 and petsc-dev (as of October 2012) use non-const versions PCType orig_petsc_pc_type; KSPType orig_petsc_ksp_type; #else const PCType orig_petsc_pc_type; const KSPType orig_petsc_ksp_type; #endif if (petsc_linear_solver) { PC pc = petsc_linear_solver->pc(); int ierr = PCGetType(pc, &orig_petsc_pc_type); LIBMESH_CHKERRABORT(ierr); KSP ksp = petsc_linear_solver->ksp(); ierr = KSPGetType(ksp, &orig_petsc_ksp_type); LIBMESH_CHKERRABORT(ierr); // libMesh::out << "orig_petsc_pc_type (before)=" << orig_petsc_pc_type << std::endl; // Make actual copies of the original PC and KSP types orig_petsc_pc_type_string = orig_petsc_pc_type; orig_petsc_ksp_type_string = orig_petsc_ksp_type; #ifdef LIBMESH_HAVE_PETSC_HYPRE // Set solver/PC combo specified in input file... if (this->alternative_solver == "amg") { // Set HYPRE and boomeramg PC types ierr = PCSetType(pc, PCHYPRE); LIBMESH_CHKERRABORT(ierr); ierr = PCHYPRESetType(pc, "boomeramg"); LIBMESH_CHKERRABORT(ierr); } #endif // LIBMESH_HAVE_PETSC_HYPRE if (this->alternative_solver == "mumps") { // We'll use MUMPS... TODO: configure libmesh to detect // when MUMPS is available via PETSc. // No initial guesses can be specified with KSPPREONLY. We // can leave the solver as gmres or whatever and it should // converge in 1 iteration. Otherwise, to use KSPPREONLY, // you may need to do: // KSPSetInitialGuessNonzero(ksp, PETSC_FALSE); // ierr = KSPSetType(ksp, KSPPREONLY); // LIBMESH_CHKERRABORT(ierr); // Need to call the equivalent for the command line options: // -ksp_type preonly -pc_type lu -pc_factor_mat_solver_package mumps ierr = PCSetType(pc, PCLU); LIBMESH_CHKERRABORT(ierr); #if !(PETSC_VERSION_LESS_THAN(3,0,0)) ierr = PCFactorSetMatSolverPackage(pc,"mumps"); LIBMESH_CHKERRABORT(ierr); #endif } } else { // Otherwise, the cast failed and we are not using PETSc... libMesh::out << "You are not using PETSc, so don't know how to set AMG PC." << std::endl; libMesh::out << "Returning empty string!" << std::endl; } #endif // LIBMESH_HAVE_PETSC return std::make_pair(orig_petsc_pc_type_string, orig_petsc_ksp_type_string); }
int main(int argc,char **args) { Mat A; /* linear system matrix */ PetscErrorCode ierr; PetscMPIInt rank=0; PetscBool flg; PetscViewer fd; /* viewer */ PetscViewer log; char file[PETSC_MAX_PATH_LEN]; char logfile[PETSC_MAX_PATH_LEN]; char lockfile[PETSC_MAX_PATH_LEN], tmpstr[PETSC_MAX_PATH_LEN], dirname[PETSC_MAX_PATH_LEN], matrix[PETSC_MAX_PATH_LEN]; char hash[20]; PetscLogDouble solveTime,endTime,startTime; PetscInt its; PetscReal norm; KSP ksp; // Linear solver context Vec b,x,u; // RHS, solution, vector for norm calculation PetscScalar one = 1.0; PetscInt m, n, i; FILE *lock; /* if (rank == 0) { printf("Command line arguments:\n"); for (i=0; i < argc; i++) printf("%d: %s\n", i, args[i]); } // Save args int argcount = argc; char **argv = (char**) malloc (argc*sizeof(char*)); for (i=0; i < argc; i++) { argv[i] = (char*) malloc(strlen(args[i]) + 1); strcpy(argv[i],args[i]); } MPI_Comm_rank(MPI_COMM_WORLD,&rank); */ PetscInitialize(&argc,&args,(char *)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-hash",hash,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) { strcpy(hash,"nohash"); } ierr = PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) { PetscPrintf(PETSC_COMM_WORLD,"Must indicate matrix file with the -f option"); } /* Create lock file */ if (rank == 0) { for (i = strlen(file); i> 0; i--) if (file[i] == '.') break; strncpy(tmpstr, file, i-1); for (i = strlen(tmpstr); i> 0; i--) if (file[i] == '/') break; strncpy(dirname, tmpstr, i); dirname[i] = '\0'; sprintf(lockfile,"%s/../timing/.%s.%s", dirname, basename(tmpstr), hash); sprintf(logfile,"%s/../timing/%s.%s.log", dirname, basename(tmpstr), hash); lock = fopen(lockfile, "w"); fprintf(lock, "%s\n", file); fclose(lock); } /* Read file */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); // Create matrix ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetType(A,MATMPIAIJ); CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); // Load matrix from file ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatGetSize(A, &m, &n); CHKERRQ(ierr); // Assemble matrix //ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); //ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); // Create RHS vector ierr = VecCreate(PETSC_COMM_WORLD,&b);CHKERRQ(ierr); ierr = VecSetSizes(b,PETSC_DECIDE,n); CHKERRQ(ierr); ierr = VecSetFromOptions(b);CHKERRQ(ierr); ierr = VecSet(b,one); CHKERRQ(ierr); //ierr = VecLoad(b,fd);CHKERRQ(ierr); // Create vectors x and u ierr = VecDuplicate(b,&x);CHKERRQ(ierr); ierr = VecDuplicate(b,&u);CHKERRQ(ierr); // Create KSP ierr = KSPCreate(PETSC_COMM_WORLD,&ksp); CHKERRQ(ierr); ierr = KSPSetInitialGuessNonzero(ksp,PETSC_FALSE);CHKERRQ(ierr); ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr); // Setup KSP ierr = KSPSetUp(ksp);CHKERRQ(ierr); ierr = KSPSetUpOnBlocks(ksp);CHKERRQ(ierr); // Get start time ierr = PetscTime(&startTime);CHKERRQ(ierr); // Get KSP and PC type KSPType kt; ierr = KSPGetType(ksp,&kt); PC pc; ierr = KSPGetPC(ksp,&pc); PCType pt; ierr = PCGetType(pc,&pt); // Print method info ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD, logfile, &log); CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(log, "Hash: %s\n", hash); ierr = PetscViewerASCIIPrintf(log, "%s | %s",kt,pt);CHKERRQ(ierr); // Make sure the program doesn't crash // while trying to solve the system PetscPushErrorHandler(PetscIgnoreErrorHandler,NULL); ierr = KSPSolve(ksp,b,x); PetscPopErrorHandler(); // Check if anything went wrong if(ierr == 0 || ierr == -1){ // If no error occurred or stopped by MyKSPMonitor, // compute normal and stuff ierr = KSPGetIterationNumber(ksp,&its);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 = PetscTime(&endTime);CHKERRQ(ierr); // Compute solve time solveTime = endTime - startTime; // Check if KSP converged KSPConvergedReason reason; KSPGetConvergedReason(ksp,&reason); // Print convergence code, solve time, preconditioned norm, iterations ierr = PetscViewerASCIIPrintf(log, " | %D | %e | %g | %D\n",reason,solveTime,norm,its);CHKERRQ(ierr); ierr = KSPView(ksp,log); ierr = PCView(pc,log); ierr = PetscLogView(log); } else{ // Disaster happened, bail out if (rank == 0) remove(lockfile); PetscFinalize(); return 0; } // Again, destroy KSP and vector ierr = KSPDestroy(&ksp);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr); if (rank == 0) remove(lockfile); PetscFinalize(); return 0; }
void SolverLinearPetsc<T>::init () { // Initialize the data structures if not done so already. if ( !this->initialized() ) { this->setInitialized( true ); int ierr=0; // 2.1.x & earlier style #if (PETSC_VERSION_MAJOR == 2) && (PETSC_VERSION_MINOR <= 1) // Create the linear solver context ierr = SLESCreate ( this->worldComm().globalComm(), &M_sles ); CHKERRABORT( this->worldComm().globalComm(),ierr ); // Create the Krylov subspace & preconditioner contexts ierr = SLESGetKSP ( M_sles, &M_ksp ); CHKERRABORT( this->worldComm().globalComm(),ierr ); ierr = SLESGetPC ( M_sles, &M_pc ); CHKERRABORT( this->worldComm().globalComm(),ierr ); // Have the Krylov subspace method use our good initial guess rather than 0 ierr = KSPSetInitialGuessNonzero ( M_ksp, PETSC_TRUE ); CHKERRABORT( this->worldComm().globalComm(),ierr ); // Set user-specified solver and preconditioner types this->setPetscSolverType(); this->setPetscPreconditionerType(); this->setPetscConstantNullSpace(); // Set the options from user-input // Set runtime options, e.g., // -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol> // These options will override those specified above as long as // SLESSetFromOptions() is called _after_ any other customization // routines. ierr = SLESSetFromOptions ( M_sles ); CHKERRABORT( this->worldComm().globalComm(),ierr ); // 2.2.0 & newer style #else // Create the linear solver context ierr = KSPCreate ( this->worldComm().globalComm(), &M_ksp ); CHKERRABORT( this->worldComm().globalComm(),ierr ); // Create the preconditioner context ierr = KSPGetPC ( M_ksp, &M_pc ); CHKERRABORT( this->worldComm().globalComm(),ierr ); // Have the Krylov subspace method use our good initial guess rather than 0 ierr = KSPSetInitialGuessNonzero ( M_ksp, PETSC_TRUE ); CHKERRABORT( this->worldComm().globalComm(),ierr ); // Set user-specified solver and preconditioner types this->setPetscSolverType(); this->setPetscConstantNullSpace(); // Set the options from user-input // Set runtime options, e.g., // -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol> // These options will override those specified above as long as // KSPSetFromOptions() is called _after_ any other customization // routines. //ierr = PCSetFromOptions ( M_pc ); //CHKERRABORT( this->worldComm().globalComm(),ierr ); ierr = KSPSetFromOptions ( M_ksp ); CHKERRABORT( this->worldComm().globalComm(),ierr ); #endif // Have the Krylov subspace method use our good initial guess // rather than 0, unless the user requested a KSPType of // preonly, which complains if asked to use initial guesses. #if PETSC_VERSION_LESS_THAN(3,0,0) KSPType ksp_type; #else #if PETSC_VERSION_LESS_THAN(3,4,0) const KSPType ksp_type; #else KSPType ksp_type; #endif #endif ierr = KSPGetType ( M_ksp, &ksp_type ); CHKERRABORT( this->worldComm().globalComm(),ierr ); if ( std::string((char*)ksp_type) == std::string( ( char* )KSPPREONLY ) ) { ierr = KSPSetInitialGuessNonzero ( M_ksp, PETSC_FALSE ); CHKERRABORT( this->worldComm().globalComm(),ierr ); } if ( std::string((char*)ksp_type) == std::string( ( char* )KSPGMRES ) ) { int nRestartGMRES = option(_name="gmres-restart", _prefix=this->prefix() ).template as<int>(); ierr = KSPGMRESSetRestart( M_ksp, nRestartGMRES ); CHKERRABORT( this->worldComm().globalComm(),ierr ); } // Notify PETSc of location to store residual history. // This needs to be called before any solves, since // it sets the residual history length to zero. The default // behavior is for PETSc to allocate (internally) an array // of size 1000 to hold the residual norm history. ierr = KSPSetResidualHistory( M_ksp, PETSC_NULL, // pointer to the array which holds the history PETSC_DECIDE, // size of the array holding the history PETSC_TRUE ); // Whether or not to reset the history for each solve. CHKERRABORT( this->worldComm().globalComm(),ierr ); //If there is a preconditioner object we need to set the internal setup and apply routines if ( this->M_preconditioner ) { VLOG(2) << "preconditioner: " << this->M_preconditioner << "\n"; PCSetType(M_pc, PCSHELL); PCShellSetName( M_pc, this->M_preconditioner->name().c_str() ); PCShellSetContext( M_pc,( void* )this->M_preconditioner.get() ); PCShellSetSetUp( M_pc,__feel_petsc_preconditioner_setup ); PCShellSetApply( M_pc,__feel_petsc_preconditioner_apply ); PCShellSetView( M_pc,__feel_petsc_preconditioner_view ); #if PETSC_VERSION_LESS_THAN(3,4,0) const PCType pc_type; #else PCType pc_type; #endif ierr = PCGetType ( M_pc, &pc_type ); CHKERRABORT( this->worldComm().globalComm(),ierr ); VLOG(2) << "preconditioner set as " << pc_type << "\n"; } else { this->setPetscPreconditionerType(); // sets the software that is used to perform the factorization PetscPCFactorSetMatSolverPackage( M_pc,this->matSolverPackageType() ); } if ( Environment::vm(_name="ksp-monitor",_prefix=this->prefix()).template as<bool>() ) { KSPMonitorSet( M_ksp,KSPMonitorDefault,PETSC_NULL,PETSC_NULL ); } } }
bool PETScLinearSolver::solve(PETScMatrix& A, PETScVector& b, PETScVector& x) { BaseLib::RunTime wtimer; wtimer.start(); // define TEST_MEM_PETSC #ifdef TEST_MEM_PETSC PetscLogDouble mem1, mem2; PetscMemoryGetCurrentUsage(&mem1); #endif #if (PETSC_VERSION_NUMBER > 3040) KSPSetOperators(_solver, A.getRawMatrix(), A.getRawMatrix()); #else KSPSetOperators(_solver, A.getRawMatrix(), A.getRawMatrix(), DIFFERENT_NONZERO_PATTERN); #endif KSPSolve(_solver, b.getRawVector(), x.getRawVector()); KSPConvergedReason reason; KSPGetConvergedReason(_solver, &reason); bool converged = true; if (reason > 0) { const char* ksp_type; const char* pc_type; KSPGetType(_solver, &ksp_type); PCGetType(_pc, &pc_type); PetscPrintf(PETSC_COMM_WORLD, "\n================================================"); PetscPrintf(PETSC_COMM_WORLD, "\nLinear solver %s with %s preconditioner", ksp_type, pc_type); PetscInt its; KSPGetIterationNumber(_solver, &its); PetscPrintf(PETSC_COMM_WORLD, "\nconverged in %d iterations", its); switch (reason) { case KSP_CONVERGED_RTOL: PetscPrintf(PETSC_COMM_WORLD, " (relative convergence criterion fulfilled)."); break; case KSP_CONVERGED_ATOL: PetscPrintf(PETSC_COMM_WORLD, " (absolute convergence criterion fulfilled)."); break; default: PetscPrintf(PETSC_COMM_WORLD, "."); } PetscPrintf(PETSC_COMM_WORLD, "\n================================================\n"); } else if (reason == KSP_DIVERGED_ITS) { const char* ksp_type; const char* pc_type; KSPGetType(_solver, &ksp_type); PCGetType(_pc, &pc_type); PetscPrintf(PETSC_COMM_WORLD, "\nLinear solver %s with %s preconditioner", ksp_type, pc_type); PetscPrintf(PETSC_COMM_WORLD, "\nWarning: maximum number of iterations reached.\n"); } else { converged = false; if (reason == KSP_DIVERGED_INDEFINITE_PC) { PetscPrintf(PETSC_COMM_WORLD, "\nDivergence because of indefinite preconditioner,"); PetscPrintf(PETSC_COMM_WORLD, "\nTry to run again with " "-pc_factor_shift_positive_definite option.\n"); } else if (reason == KSP_DIVERGED_BREAKDOWN_BICG) { PetscPrintf(PETSC_COMM_WORLD, "\nKSPBICG method was detected so the method could not " "continue to enlarge the Krylov space."); PetscPrintf(PETSC_COMM_WORLD, "\nTry to run again with another solver.\n"); } else if (reason == KSP_DIVERGED_NONSYMMETRIC) { PetscPrintf(PETSC_COMM_WORLD, "\nMatrix or preconditioner is unsymmetric but KSP " "requires symmetric.\n"); } else { PetscPrintf(PETSC_COMM_WORLD, "\nDivergence detected, use command option " "-ksp_monitor or -log_summary to check the details.\n"); } } #ifdef TEST_MEM_PETSC PetscMemoryGetCurrentUsage(&mem2); PetscPrintf( PETSC_COMM_WORLD, "###Memory usage by solver. Before: %f After: %f Increase: %d\n", mem1, mem2, (int)(mem2 - mem1)); #endif _elapsed_ctime += wtimer.elapsed(); return converged; }