Date: 2023-11-27T17:18:19+00:00

Actual source code: rqcg.c
slepc-3.20.1 2023-11-27
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: static 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: static PetscErrorCode EPSSetUp_RQCG(EPS eps)
     38: {
     39:   PetscInt       nmat;
     40:   EPS_RQCG       *ctx = (EPS_RQCG*)eps->data;

     42:   PetscFunctionBegin;
     43:   EPSCheckHermitianDefinite(eps);
     44:   PetscCall(EPSSetDimensions_Default(eps,eps->nev,&eps->ncv,&eps->mpd));
     45:   if (eps->max_it==PETSC_DEFAULT) eps->max_it = PetscMax(100,2*eps->n/eps->ncv);
     46:   if (!eps->which) eps->which = EPS_SMALLEST_REAL;
     47:   PetscCheck(eps->which==EPS_SMALLEST_REAL,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver supports only smallest real eigenvalues");
     48:   EPSCheckUnsupported(eps,EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_EXTRACTION);
     49:   EPSCheckIgnored(eps,EPS_FEATURE_BALANCE);

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

     53:   PetscCall(EPSAllocateSolution(eps,0));
     54:   PetscCall(EPS_SetInnerProduct(eps));

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

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

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

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

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

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

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

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

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

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

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

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

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

    235:     PetscCall(EPSMonitor(eps,eps->its,k,eps->eigr,eps->eigi,eps->errest,nv));
    236:     eps->nconv = k;
    237:   }

    239:   PetscCall(PetscFree(gamma));
    240:   PetscFunctionReturn(PETSC_SUCCESS);
    241: }

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

    247:   PetscFunctionBegin;
    248:   if (nrest==PETSC_DEFAULT) {
    249:     ctx->nrest = 0;
    250:     eps->state = EPS_STATE_INITIAL;
    251:   } else {
    252:     PetscCheck(nrest>0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Reset parameter must be >0");
    253:     ctx->nrest = nrest;
    254:   }
    255:   PetscFunctionReturn(PETSC_SUCCESS);
    256: }

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

    262:    Logically Collective

    264:    Input Parameters:
    265: +  eps - the eigenproblem solver context
    266: -  nrest - the number of iterations between resets

    268:    Options Database Key:
    269: .  -eps_rqcg_reset - Sets the reset parameter

    271:    Level: advanced

    273: .seealso: EPSRQCGGetReset()
    274: @*/
    275: PetscErrorCode EPSRQCGSetReset(EPS eps,PetscInt nrest)
    276: {
    277:   PetscFunctionBegin;
    280:   PetscTryMethod(eps,"EPSRQCGSetReset_C",(EPS,PetscInt),(eps,nrest));
    281:   PetscFunctionReturn(PETSC_SUCCESS);
    282: }

    284: static PetscErrorCode EPSRQCGGetReset_RQCG(EPS eps,PetscInt *nrest)
    285: {
    286:   EPS_RQCG *ctx = (EPS_RQCG*)eps->data;

    288:   PetscFunctionBegin;
    289:   *nrest = ctx->nrest;
    290:   PetscFunctionReturn(PETSC_SUCCESS);
    291: }

    293: /*@
    294:    EPSRQCGGetReset - Gets the reset parameter used in the RQCG method.

    296:    Not Collective

    298:    Input Parameter:
    299: .  eps - the eigenproblem solver context

    301:    Output Parameter:
    302: .  nrest - the reset parameter

    304:    Level: advanced

    306: .seealso: EPSRQCGSetReset()
    307: @*/
    308: PetscErrorCode EPSRQCGGetReset(EPS eps,PetscInt *nrest)
    309: {
    310:   PetscFunctionBegin;
    312:   PetscAssertPointer(nrest,2);
    313:   PetscUseMethod(eps,"EPSRQCGGetReset_C",(EPS,PetscInt*),(eps,nrest));
    314:   PetscFunctionReturn(PETSC_SUCCESS);
    315: }

    317: static PetscErrorCode EPSReset_RQCG(EPS eps)
    318: {
    319:   EPS_RQCG       *ctx = (EPS_RQCG*)eps->data;

    321:   PetscFunctionBegin;
    322:   PetscCall(BVDestroy(&ctx->AV));
    323:   PetscCall(BVDestroy(&ctx->W));
    324:   PetscCall(BVDestroy(&ctx->P));
    325:   PetscCall(BVDestroy(&ctx->G));
    326:   ctx->allocsize = 0;
    327:   PetscFunctionReturn(PETSC_SUCCESS);
    328: }

    330: static PetscErrorCode EPSSetFromOptions_RQCG(EPS eps,PetscOptionItems *PetscOptionsObject)
    331: {
    332:   PetscBool      flg;
    333:   PetscInt       nrest;

    335:   PetscFunctionBegin;
    336:   PetscOptionsHeadBegin(PetscOptionsObject,"EPS RQCG Options");

    338:     PetscCall(PetscOptionsInt("-eps_rqcg_reset","Reset parameter","EPSRQCGSetReset",20,&nrest,&flg));
    339:     if (flg) PetscCall(EPSRQCGSetReset(eps,nrest));

    341:   PetscOptionsHeadEnd();
    342:   PetscFunctionReturn(PETSC_SUCCESS);
    343: }

    345: static PetscErrorCode EPSDestroy_RQCG(EPS eps)
    346: {
    347:   PetscFunctionBegin;
    348:   PetscCall(PetscFree(eps->data));
    349:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSRQCGSetReset_C",NULL));
    350:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSRQCGGetReset_C",NULL));
    351:   PetscFunctionReturn(PETSC_SUCCESS);
    352: }

    354: static PetscErrorCode EPSView_RQCG(EPS eps,PetscViewer viewer)
    355: {
    356:   EPS_RQCG       *ctx = (EPS_RQCG*)eps->data;
    357:   PetscBool      isascii;

    359:   PetscFunctionBegin;
    360:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii));
    361:   if (isascii) PetscCall(PetscViewerASCIIPrintf(viewer,"  reset every %" PetscInt_FMT " iterations\n",ctx->nrest));
    362:   PetscFunctionReturn(PETSC_SUCCESS);
    363: }

    365: SLEPC_EXTERN PetscErrorCode EPSCreate_RQCG(EPS eps)
    366: {
    367:   EPS_RQCG       *rqcg;

    369:   PetscFunctionBegin;
    370:   PetscCall(PetscNew(&rqcg));
    371:   eps->data = (void*)rqcg;

    373:   eps->useds = PETSC_TRUE;
    374:   eps->categ = EPS_CATEGORY_PRECOND;

    376:   eps->ops->solve          = EPSSolve_RQCG;
    377:   eps->ops->setup          = EPSSetUp_RQCG;
    378:   eps->ops->setupsort      = EPSSetUpSort_Default;
    379:   eps->ops->setfromoptions = EPSSetFromOptions_RQCG;
    380:   eps->ops->destroy        = EPSDestroy_RQCG;
    381:   eps->ops->reset          = EPSReset_RQCG;
    382:   eps->ops->view           = EPSView_RQCG;
    383:   eps->ops->backtransform  = EPSBackTransform_Default;
    384:   eps->ops->setdefaultst   = EPSSetDefaultST_GMRES;

    386:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSRQCGSetReset_C",EPSRQCGSetReset_RQCG));
    387:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSRQCGGetReset_C",EPSRQCGGetReset_RQCG));
    388:   PetscFunctionReturn(PETSC_SUCCESS);
    389: }