Date: 2022-11-02T11:46:46+00:00

Actual source code: rqcg.c
slepc-3.18.1 2022-11-02
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain

      6:    This file is part of SLEPc.
      7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
      8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      9: */
     10: /*
     11:    SLEPc eigensolver: "rqcg"

     13:    Method: Rayleigh Quotient Conjugate Gradient

     15:    Algorithm:

     17:        Conjugate Gradient minimization of the Rayleigh quotient with
     18:        periodic Rayleigh-Ritz acceleration.

     20:    References:

     22:        [1] L. Bergamaschi et al., "Parallel preconditioned conjugate gradient
     23:            optimization of the Rayleigh quotient for the solution of sparse
     24:            eigenproblems", Appl. Math. Comput. 175(2):1694-1715, 2006.
     25: */

     27: #include <slepc/private/epsimpl.h>

     29: PetscErrorCode EPSSolve_RQCG(EPS);

     31: typedef struct {
     32:   PetscInt nrest;         /* user-provided reset parameter */
     33:   PetscInt allocsize;     /* number of columns of work BV's allocated at setup */
     34:   BV       AV,W,P,G;
     35: } EPS_RQCG;

     37: PetscErrorCode EPSSetUp_RQCG(EPS eps)
     38: {
     39:   PetscInt       nmat;
     40:   EPS_RQCG       *ctx = (EPS_RQCG*)eps->data;

     42:   EPSCheckHermitianDefinite(eps);
     43:   EPSSetDimensions_Default(eps,eps->nev,&eps->ncv,&eps->mpd);
     44:   if (eps->max_it==PETSC_DEFAULT) eps->max_it = PetscMax(100,2*eps->n/eps->ncv);
     45:   if (!eps->which) eps->which = EPS_SMALLEST_REAL;
     47:   EPSCheckUnsupported(eps,EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_EXTRACTION);
     48:   EPSCheckIgnored(eps,EPS_FEATURE_BALANCE);

     50:   if (!ctx->nrest) ctx->nrest = 20;

     52:   EPSAllocateSolution(eps,0);
     53:   EPS_SetInnerProduct(eps);

     55:   STGetNumMatrices(eps->st,&nmat);
     56:   if (!ctx->allocsize) {
     57:     ctx->allocsize = eps->mpd;
     58:     BVDuplicateResize(eps->V,eps->mpd,&ctx->AV);
     59:     if (nmat>1) BVDuplicate(ctx->AV,&ctx->W);
     60:     BVDuplicate(ctx->AV,&ctx->P);
     61:     BVDuplicate(ctx->AV,&ctx->G);
     62:   } else if (ctx->allocsize!=eps->mpd) {
     63:     ctx->allocsize = eps->mpd;
     64:     BVResize(ctx->AV,eps->mpd,PETSC_FALSE);
     65:     if (nmat>1) BVResize(ctx->W,eps->mpd,PETSC_FALSE);
     66:     BVResize(ctx->P,eps->mpd,PETSC_FALSE);
     67:     BVResize(ctx->G,eps->mpd,PETSC_FALSE);
     68:   }
     69:   DSSetType(eps->ds,DSHEP);
     70:   DSAllocate(eps->ds,eps->ncv);
     71:   EPSSetWorkVecs(eps,1);
     72:   return 0;
     73: }

     75: PetscErrorCode EPSSolve_RQCG(EPS eps)
     76: {
     77:   EPS_RQCG       *ctx = (EPS_RQCG*)eps->data;
     78:   PetscInt       i,j,k,ld,nv,ncv = eps->ncv,kini,nmat;
     79:   PetscScalar    *C,*gamma,g,pap,pbp,pbx,pax,nu,mu,alpha,beta;
     80:   PetscReal      resnorm,a,b,c,d,disc,t;
     81:   PetscBool      reset;
     82:   Mat            A,B,Q,Q1;
     83:   Vec            v,av,bv,p,w=eps->work[0];

     85:   DSGetLeadingDimension(eps->ds,&ld);
     86:   STGetNumMatrices(eps->st,&nmat);
     87:   STGetMatrix(eps->st,0,&A);
     88:   if (nmat>1) STGetMatrix(eps->st,1,&B);
     89:   else B = NULL;
     90:   PetscMalloc1(eps->mpd,&gamma);

     92:   kini = eps->nini;
     93:   while (eps->reason == EPS_CONVERGED_ITERATING) {
     94:     eps->its++;
     95:     nv = PetscMin(eps->nconv+eps->mpd,ncv);
     96:     DSSetDimensions(eps->ds,nv,eps->nconv,0);
     97:     for (;kini<nv;kini++) { /* Generate more initial vectors if necessary */
     98:       BVSetRandomColumn(eps->V,kini);
     99:       BVOrthonormalizeColumn(eps->V,kini,PETSC_TRUE,NULL,NULL);
    100:     }
    101:     reset = (eps->its>1 && (eps->its-1)%ctx->nrest==0)? PETSC_TRUE: PETSC_FALSE;

    103:     if (reset) {
    104:       /* Prevent BVDotVec below to use B-product, restored at the end */
    105:       BVSetMatrix(eps->V,NULL,PETSC_FALSE);

    107:       /* Compute Rayleigh quotient */
    108:       BVSetActiveColumns(eps->V,eps->nconv,nv);
    109:       BVSetActiveColumns(ctx->AV,0,nv-eps->nconv);
    110:       BVMatMult(eps->V,A,ctx->AV);
    111:       DSGetArray(eps->ds,DS_MAT_A,&C);
    112:       for (i=eps->nconv;i<nv;i++) {
    113:         BVSetActiveColumns(eps->V,eps->nconv,i+1);
    114:         BVGetColumn(ctx->AV,i-eps->nconv,&av);
    115:         BVDotVec(eps->V,av,C+eps->nconv+i*ld);
    116:         BVRestoreColumn(ctx->AV,i-eps->nconv,&av);
    117:         for (j=eps->nconv;j<i-1;j++) C[i+j*ld] = PetscConj(C[j+i*ld]);
    118:       }
    119:       DSRestoreArray(eps->ds,DS_MAT_A,&C);
    120:       DSSetState(eps->ds,DS_STATE_RAW);

    122:       /* Solve projected problem */
    123:       DSSolve(eps->ds,eps->eigr,eps->eigi);
    124:       DSSort(eps->ds,eps->eigr,eps->eigi,NULL,NULL,NULL);
    125:       DSSynchronize(eps->ds,eps->eigr,eps->eigi);

    127:       /* Update vectors V(:,idx) = V * Y(:,idx) */
    128:       DSGetMat(eps->ds,DS_MAT_Q,&Q);
    129:       BVMultInPlace(eps->V,Q,eps->nconv,nv);
    130:       MatDenseGetSubMatrix(Q,eps->nconv,PETSC_DECIDE,eps->nconv,PETSC_DECIDE,&Q1);
    131:       BVMultInPlace(ctx->AV,Q1,0,nv-eps->nconv);
    132:       MatDenseRestoreSubMatrix(Q,&Q1);
    133:       DSRestoreMat(eps->ds,DS_MAT_Q,&Q);
    134:       if (B) BVSetMatrix(eps->V,B,PETSC_FALSE);
    135:     } else {
    136:       /* No need to do Rayleigh-Ritz, just take diag(V'*A*V) */
    137:       for (i=eps->nconv;i<nv;i++) {
    138:         BVGetColumn(eps->V,i,&v);
    139:         BVGetColumn(ctx->AV,i-eps->nconv,&av);
    140:         MatMult(A,v,av);
    141:         VecDot(av,v,eps->eigr+i);
    142:         BVRestoreColumn(eps->V,i,&v);
    143:         BVRestoreColumn(ctx->AV,i-eps->nconv,&av);
    144:       }
    145:     }

    147:     /* Compute gradient and check convergence */
    148:     k = -1;
    149:     for (i=eps->nconv;i<nv;i++) {
    150:       BVGetColumn(eps->V,i,&v);
    151:       BVGetColumn(ctx->AV,i-eps->nconv,&av);
    152:       BVGetColumn(ctx->G,i-eps->nconv,&p);
    153:       if (B) {
    154:         BVGetColumn(ctx->W,i-eps->nconv,&bv);
    155:         MatMult(B,v,bv);
    156:         VecWAXPY(p,-eps->eigr[i],bv,av);
    157:         BVRestoreColumn(ctx->W,i-eps->nconv,&bv);
    158:       } else VecWAXPY(p,-eps->eigr[i],v,av);
    159:       BVRestoreColumn(eps->V,i,&v);
    160:       BVRestoreColumn(ctx->AV,i-eps->nconv,&av);
    161:       VecNorm(p,NORM_2,&resnorm);
    162:       BVRestoreColumn(ctx->G,i-eps->nconv,&p);
    163:       (*eps->converged)(eps,eps->eigr[i],0.0,resnorm,&eps->errest[i],eps->convergedctx);
    164:       if (k==-1 && eps->errest[i] >= eps->tol) k = i;
    165:     }
    166:     if (k==-1) k = nv;
    167:     (*eps->stopping)(eps,eps->its,eps->max_it,k,eps->nev,&eps->reason,eps->stoppingctx);

    169:     /* The next lines are necessary to avoid DS zeroing eigr */
    170:     DSGetArray(eps->ds,DS_MAT_A,&C);
    171:     for (i=eps->nconv;i<k;i++) C[i+i*ld] = eps->eigr[i];
    172:     DSRestoreArray(eps->ds,DS_MAT_A,&C);

    174:     if (eps->reason == EPS_CONVERGED_ITERATING) {

    176:       /* Search direction */
    177:       for (i=0;i<nv-eps->nconv;i++) {
    178:         BVGetColumn(ctx->G,i,&v);
    179:         STApply(eps->st,v,w);
    180:         VecDot(w,v,&g);
    181:         BVRestoreColumn(ctx->G,i,&v);
    182:         beta = (!reset && eps->its>1)? g/gamma[i]: 0.0;
    183:         gamma[i] = g;
    184:         BVGetColumn(ctx->P,i,&v);
    185:         VecAXPBY(v,1.0,beta,w);
    186:         if (i+eps->nconv>0) {
    187:           BVSetActiveColumns(eps->V,0,i+eps->nconv);
    188:           BVOrthogonalizeVec(eps->V,v,NULL,NULL,NULL);
    189:         }
    190:         BVRestoreColumn(ctx->P,i,&v);
    191:       }

    193:       /* Minimization problem */
    194:       for (i=eps->nconv;i<nv;i++) {
    195:         BVGetColumn(eps->V,i,&v);
    196:         BVGetColumn(ctx->AV,i-eps->nconv,&av);
    197:         BVGetColumn(ctx->P,i-eps->nconv,&p);
    198:         VecDot(av,v,&nu);
    199:         VecDot(av,p,&pax);
    200:         MatMult(A,p,w);
    201:         VecDot(w,p,&pap);
    202:         if (B) {
    203:           BVGetColumn(ctx->W,i-eps->nconv,&bv);
    204:           VecDot(bv,v,&mu);
    205:           VecDot(bv,p,&pbx);
    206:           BVRestoreColumn(ctx->W,i-eps->nconv,&bv);
    207:           MatMult(B,p,w);
    208:           VecDot(w,p,&pbp);
    209:         } else {
    210:           VecDot(v,v,&mu);
    211:           VecDot(v,p,&pbx);
    212:           VecDot(p,p,&pbp);
    213:         }
    214:         BVRestoreColumn(ctx->AV,i-eps->nconv,&av);
    215:         a = PetscRealPart(pap*pbx-pax*pbp);
    216:         b = PetscRealPart(nu*pbp-mu*pap);
    217:         c = PetscRealPart(mu*pax-nu*pbx);
    218:         t = PetscMax(PetscMax(PetscAbsReal(a),PetscAbsReal(b)),PetscAbsReal(c));
    219:         if (t!=0.0) { a /= t; b /= t; c /= t; }
    220:         disc = b*b-4.0*a*c;
    221:         d = PetscSqrtReal(PetscAbsReal(disc));
    222:         if (b>=0.0 && a!=0.0) alpha = (b+d)/(2.0*a);
    223:         else if (b!=d) alpha = 2.0*c/(b-d);
    224:         else alpha = 0;
    225:         /* Next iterate */
    226:         if (alpha!=0.0) VecAXPY(v,alpha,p);
    227:         BVRestoreColumn(eps->V,i,&v);
    228:         BVRestoreColumn(ctx->P,i-eps->nconv,&p);
    229:         BVOrthonormalizeColumn(eps->V,i,PETSC_TRUE,NULL,NULL);
    230:       }
    231:     }

    233:     EPSMonitor(eps,eps->its,k,eps->eigr,eps->eigi,eps->errest,nv);
    234:     eps->nconv = k;
    235:   }

    237:   PetscFree(gamma);
    238:   return 0;
    239: }

    241: static PetscErrorCode EPSRQCGSetReset_RQCG(EPS eps,PetscInt nrest)
    242: {
    243:   EPS_RQCG *ctx = (EPS_RQCG*)eps->data;

    245:   if (nrest==PETSC_DEFAULT) {
    246:     ctx->nrest = 0;
    247:     eps->state = EPS_STATE_INITIAL;
    248:   } else {
    250:     ctx->nrest = nrest;
    251:   }
    252:   return 0;
    253: }

    255: /*@
    256:    EPSRQCGSetReset - Sets the reset parameter of the RQCG iteration. Every
    257:    nrest iterations, the solver performs a Rayleigh-Ritz projection step.

    259:    Logically Collective on eps

    261:    Input Parameters:
    262: +  eps - the eigenproblem solver context
    263: -  nrest - the number of iterations between resets

    265:    Options Database Key:
    266: .  -eps_rqcg_reset - Sets the reset parameter

    268:    Level: advanced

    270: .seealso: EPSRQCGGetReset()
    271: @*/
    272: PetscErrorCode EPSRQCGSetReset(EPS eps,PetscInt nrest)
    273: {
    276:   PetscTryMethod(eps,"EPSRQCGSetReset_C",(EPS,PetscInt),(eps,nrest));
    277:   return 0;
    278: }

    280: static PetscErrorCode EPSRQCGGetReset_RQCG(EPS eps,PetscInt *nrest)
    281: {
    282:   EPS_RQCG *ctx = (EPS_RQCG*)eps->data;

    284:   *nrest = ctx->nrest;
    285:   return 0;
    286: }

    288: /*@
    289:    EPSRQCGGetReset - Gets the reset parameter used in the RQCG method.

    291:    Not Collective

    293:    Input Parameter:
    294: .  eps - the eigenproblem solver context

    296:    Output Parameter:
    297: .  nrest - the reset parameter

    299:    Level: advanced

    301: .seealso: EPSRQCGSetReset()
    302: @*/
    303: PetscErrorCode EPSRQCGGetReset(EPS eps,PetscInt *nrest)
    304: {
    307:   PetscUseMethod(eps,"EPSRQCGGetReset_C",(EPS,PetscInt*),(eps,nrest));
    308:   return 0;
    309: }

    311: PetscErrorCode EPSReset_RQCG(EPS eps)
    312: {
    313:   EPS_RQCG       *ctx = (EPS_RQCG*)eps->data;

    315:   BVDestroy(&ctx->AV);
    316:   BVDestroy(&ctx->W);
    317:   BVDestroy(&ctx->P);
    318:   BVDestroy(&ctx->G);
    319:   ctx->allocsize = 0;
    320:   return 0;
    321: }

    323: PetscErrorCode EPSSetFromOptions_RQCG(EPS eps,PetscOptionItems *PetscOptionsObject)
    324: {
    325:   PetscBool      flg;
    326:   PetscInt       nrest;

    328:   PetscOptionsHeadBegin(PetscOptionsObject,"EPS RQCG Options");

    330:     PetscOptionsInt("-eps_rqcg_reset","Reset parameter","EPSRQCGSetReset",20,&nrest,&flg);
    331:     if (flg) EPSRQCGSetReset(eps,nrest);

    333:   PetscOptionsHeadEnd();
    334:   return 0;
    335: }

    337: PetscErrorCode EPSDestroy_RQCG(EPS eps)
    338: {
    339:   PetscFree(eps->data);
    340:   PetscObjectComposeFunction((PetscObject)eps,"EPSRQCGSetReset_C",NULL);
    341:   PetscObjectComposeFunction((PetscObject)eps,"EPSRQCGGetReset_C",NULL);
    342:   return 0;
    343: }

    345: PetscErrorCode EPSView_RQCG(EPS eps,PetscViewer viewer)
    346: {
    347:   EPS_RQCG       *ctx = (EPS_RQCG*)eps->data;
    348:   PetscBool      isascii;

    350:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
    351:   if (isascii) PetscViewerASCIIPrintf(viewer,"  reset every %" PetscInt_FMT " iterations\n",ctx->nrest);
    352:   return 0;
    353: }

    355: SLEPC_EXTERN PetscErrorCode EPSCreate_RQCG(EPS eps)
    356: {
    357:   EPS_RQCG       *rqcg;

    359:   PetscNew(&rqcg);
    360:   eps->data = (void*)rqcg;

    362:   eps->useds = PETSC_TRUE;
    363:   eps->categ = EPS_CATEGORY_PRECOND;

    365:   eps->ops->solve          = EPSSolve_RQCG;
    366:   eps->ops->setup          = EPSSetUp_RQCG;
    367:   eps->ops->setupsort      = EPSSetUpSort_Default;
    368:   eps->ops->setfromoptions = EPSSetFromOptions_RQCG;
    369:   eps->ops->destroy        = EPSDestroy_RQCG;
    370:   eps->ops->reset          = EPSReset_RQCG;
    371:   eps->ops->view           = EPSView_RQCG;
    372:   eps->ops->backtransform  = EPSBackTransform_Default;
    373:   eps->ops->setdefaultst   = EPSSetDefaultST_GMRES;

    375:   PetscObjectComposeFunction((PetscObject)eps,"EPSRQCGSetReset_C",EPSRQCGSetReset_RQCG);
    376:   PetscObjectComposeFunction((PetscObject)eps,"EPSRQCGGetReset_C",EPSRQCGGetReset_RQCG);
    377:   return 0;
    378: }