Date: 2026-01-20T11:35:42+00:00

Actual source code: dvdupdatev.c

      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: "davidson"

     13:    Step: test for restarting, updateV, restartV
     14: */

     16: #include "davidson.h"

     18: typedef struct {
     19:   PetscInt          min_size_V;        /* restart with this number of eigenvectors */
     20:   PetscInt          plusk;             /* at restart, save plusk vectors from last iteration */
     21:   PetscInt          mpd;               /* max size of the searching subspace */
     22:   void              *old_updateV_data; /* old updateV data */
     23:   PetscErrorCode    (*old_isRestarting)(dvdDashboard*,PetscBool*);  /* old isRestarting */
     24:   Mat               oldU;              /* previous projected right igenvectors */
     25:   Mat               oldV;              /* previous projected left eigenvectors */
     26:   PetscInt          size_oldU;         /* size of oldU */
     27:   PetscBool         allResiduals;      /* if computing all the residuals */
     28: } dvdManagV_basic;

     30: static PetscErrorCode dvd_updateV_start(dvdDashboard *d)
     31: {
     32:   dvdManagV_basic *data = (dvdManagV_basic*)d->updateV_data;
     33:   PetscInt        i;

     35:   PetscFunctionBegin;
     36:   for (i=0;i<d->eps->ncv;i++) d->eigi[i] = 0.0;
     37:   d->nR = d->real_nR;
     38:   for (i=0;i<d->eps->ncv;i++) d->nR[i] = 1.0;
     39:   d->nX = d->real_nX;
     40:   for (i=0;i<d->eps->ncv;i++) d->errest[i] = 1.0;
     41:   data->size_oldU = 0;
     42:   d->nconv = 0;
     43:   d->npreconv = 0;
     44:   d->V_tra_s = d->V_tra_e = d->V_new_s = d->V_new_e = 0;
     45:   d->size_D = 0;
     46:   PetscFunctionReturn(PETSC_SUCCESS);
     47: }

     49: static PetscErrorCode dvd_isrestarting_fullV(dvdDashboard *d,PetscBool *r)
     50: {
     51:   PetscInt        l,k;
     52:   PetscBool       restart;
     53:   dvdManagV_basic *data = (dvdManagV_basic*)d->updateV_data;

     55:   PetscFunctionBegin;
     56:   PetscCall(BVGetActiveColumns(d->eps->V,&l,&k));
     57:   restart = (k+2 > d->eps->ncv)? PETSC_TRUE: PETSC_FALSE;

     59:   /* Check old isRestarting function */
     60:   if (PetscUnlikely(!restart && data->old_isRestarting)) PetscCall(data->old_isRestarting(d,&restart));
     61:   *r = restart;
     62:   PetscFunctionReturn(PETSC_SUCCESS);
     63: }

     65: static PetscErrorCode dvd_managementV_basic_d(dvdDashboard *d)
     66: {
     67:   dvdManagV_basic *data = (dvdManagV_basic*)d->updateV_data;

     69:   PetscFunctionBegin;
     70:   /* Restore changes in dvdDashboard */
     71:   d->updateV_data = data->old_updateV_data;

     73:   /* Free local data */
     74:   PetscCall(MatDestroy(&data->oldU));
     75:   PetscCall(MatDestroy(&data->oldV));
     76:   PetscCall(PetscFree(d->real_nR));
     77:   PetscCall(PetscFree(d->real_nX));
     78:   PetscCall(PetscFree(data));
     79:   PetscFunctionReturn(PETSC_SUCCESS);
     80: }

     82: static PetscErrorCode dvd_updateV_conv_gen(dvdDashboard *d)
     83: {
     84:   dvdManagV_basic *data = (dvdManagV_basic*)d->updateV_data;
     85:   PetscInt        npreconv,cMT,cMTX,lV,kV,nV;
     86:   Mat             Z,Z0,Q,Q0;
     87:   PetscBool       t;
     88: #if !defined(PETSC_USE_COMPLEX)
     89:   PetscInt        i;
     90: #endif

     92:   PetscFunctionBegin;
     93:   npreconv = d->npreconv;
     94:   /* Constrains the converged pairs to nev */
     95: #if !defined(PETSC_USE_COMPLEX)
     96:   /* Tries to maintain together conjugate eigenpairs */
     97:   for (i=0; (i + (d->eigi[i]!=0.0?1:0) < npreconv) && (d->nconv + i < d->nev); i+= (d->eigi[i]!=0.0?2:1));
     98:   npreconv = i;
     99: #else
    100:   npreconv = PetscMax(PetscMin(d->nev-d->nconv,npreconv),0);
    101: #endif
    102:   /* For GHEP without B-ortho, converge all of the requested pairs at once */
    103:   PetscCall(PetscObjectTypeCompare((PetscObject)d->eps->ds,DSGHEP,&t));
    104:   if (t && d->nconv+npreconv<d->nev) npreconv = 0;
    105:   /* Quick exit */
    106:   if (npreconv == 0) PetscFunctionReturn(PETSC_SUCCESS);

    108:   PetscCall(BVGetActiveColumns(d->eps->V,&lV,&kV));
    109:   nV  = kV - lV;
    110:   cMT = nV - npreconv;
    111:   /* Harmonics restarts with right eigenvectors, and other with the left ones.
    112:      If the problem is standard or hermitian, left and right vectors are the same */
    113:   if (!(d->W||DVD_IS(d->sEP,DVD_EP_STD)||DVD_IS(d->sEP,DVD_EP_HERMITIAN))) {
    114:     /* ps.Q <- [ps.Q(0:npreconv-1) ps.Z(npreconv:size_H-1)] */
    115:     PetscCall(DSGetMat(d->eps->ds,DS_MAT_Q,&Q));
    116:     PetscCall(DSGetMat(d->eps->ds,DS_MAT_Z,&Z));
    117:     PetscCall(MatDenseGetSubMatrix(Q,0,npreconv,nV,npreconv+cMT,&Q0));
    118:     PetscCall(MatDenseGetSubMatrix(Z,0,npreconv,nV,npreconv+cMT,&Z0));
    119:     PetscCall(MatCopy(Z0,Q0,SAME_NONZERO_PATTERN));
    120:     PetscCall(MatDenseRestoreSubMatrix(Q,&Q0));
    121:     PetscCall(MatDenseRestoreSubMatrix(Z,&Z0));
    122:     PetscCall(DSRestoreMat(d->eps->ds,DS_MAT_Q,&Q));
    123:     PetscCall(DSRestoreMat(d->eps->ds,DS_MAT_Z,&Z));
    124:   }
    125:   if (DVD_IS(d->sEP,DVD_EP_INDEFINITE)) PetscCall(DSPseudoOrthogonalize(d->eps->ds,DS_MAT_Q,nV,d->nBds,&cMTX,d->nBds));
    126:   else PetscCall(DSOrthogonalize(d->eps->ds,DS_MAT_Q,nV,&cMTX));
    127:   cMT = cMTX - npreconv;

    129:   if (d->W) {
    130:     PetscCall(DSOrthogonalize(d->eps->ds,DS_MAT_Z,nV,&cMTX));
    131:     cMT = PetscMin(cMT,cMTX - npreconv);
    132:   }

    134:   /* Lock the converged pairs */
    135:   d->eigr+= npreconv;
    136: #if !defined(PETSC_USE_COMPLEX)
    137:   if (d->eigi) d->eigi+= npreconv;
    138: #endif
    139:   d->nconv+= npreconv;
    140:   d->errest+= npreconv;
    141:   /* Notify the changes in V and update the other subspaces */
    142:   d->V_tra_s = npreconv;          d->V_tra_e = nV;
    143:   d->V_new_s = cMT;               d->V_new_e = d->V_new_s;
    144:   /* Remove oldU */
    145:   data->size_oldU = 0;

    147:   d->npreconv-= npreconv;
    148:   PetscFunctionReturn(PETSC_SUCCESS);
    149: }

    151: static PetscErrorCode dvd_updateV_restart_gen(dvdDashboard *d)
    152: {
    153:   dvdManagV_basic *data = (dvdManagV_basic*)d->updateV_data;
    154:   PetscInt        lV,kV,nV,size_plusk,size_X,cMTX,cMTY,max_restart_size;
    155:   Mat             Q,Q0,Z,Z0,U,V;

    157:   PetscFunctionBegin;
    158:   /* Select size_X desired pairs from V */
    159:   /* The restarted basis should:
    160:      - have at least one spot to add a new direction;
    161:      - keep converged vectors, npreconv;
    162:      - keep at least 1 oldU direction if possible.
    163:   */
    164:   PetscCall(BVGetActiveColumns(d->eps->V,&lV,&kV));
    165:   nV = kV - lV;
    166:   max_restart_size = PetscMax(0,PetscMin(d->eps->mpd - 1,d->eps->ncv - lV - 2));
    167:   size_X = PetscMin(PetscMin(data->min_size_V+d->npreconv,max_restart_size - (max_restart_size - d->npreconv > 1 && data->plusk > 0 && data->size_oldU > 0 ? 1 : 0)), nV);

    169:   /* Add plusk eigenvectors from the previous iteration */
    170:   size_plusk = PetscMax(0,PetscMin(PetscMin(PetscMin(data->plusk,data->size_oldU),max_restart_size - size_X),nV - size_X));

    172:   d->size_MT = nV;
    173:   /* ps.Q <- orth([pX(0:size_X-1) [oldU(0:size_plusk-1); 0] ]) */
    174:   /* Harmonics restarts with right eigenvectors, and other with the left ones.
    175:      If the problem is standard or hermitian, left and right vectors are the same */
    176:   if (!(d->W||DVD_IS(d->sEP,DVD_EP_STD)||DVD_IS(d->sEP,DVD_EP_HERMITIAN))) {
    177:     PetscCall(DSGetMat(d->eps->ds,DS_MAT_Q,&Q));
    178:     PetscCall(DSGetMat(d->eps->ds,DS_MAT_Z,&Z));
    179:     PetscCall(MatDenseGetSubMatrix(Q,0,nV,0,size_X,&Q0));
    180:     PetscCall(MatDenseGetSubMatrix(Z,0,nV,0,size_X,&Z0));
    181:     PetscCall(MatCopy(Z0,Q0,SAME_NONZERO_PATTERN));
    182:     PetscCall(MatDenseRestoreSubMatrix(Q,&Q0));
    183:     PetscCall(MatDenseRestoreSubMatrix(Z,&Z0));
    184:     PetscCall(DSRestoreMat(d->eps->ds,DS_MAT_Q,&Q));
    185:     PetscCall(DSRestoreMat(d->eps->ds,DS_MAT_Z,&Z));
    186:   }
    187:   PetscCheck(size_plusk<=0 || !DVD_IS(d->sEP,DVD_EP_INDEFINITE),PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported plusk>0 in indefinite eigenvalue problems");
    188:   if (size_plusk > 0) {
    189:     PetscCall(DSGetMat(d->eps->ds,DS_MAT_Q,&Q));
    190:     PetscCall(MatDenseGetSubMatrix(Q,0,nV,size_X,size_X+size_plusk,&Q0));
    191:     PetscCall(MatDenseGetSubMatrix(data->oldU,0,nV,0,size_plusk,&U));
    192:     PetscCall(MatCopy(U,Q0,SAME_NONZERO_PATTERN));
    193:     PetscCall(MatDenseRestoreSubMatrix(Q,&Q0));
    194:     PetscCall(MatDenseRestoreSubMatrix(data->oldU,&U));
    195:     PetscCall(DSRestoreMat(d->eps->ds,DS_MAT_Q,&Q));
    196:   }
    197:   if (DVD_IS(d->sEP,DVD_EP_INDEFINITE)) PetscCall(DSPseudoOrthogonalize(d->eps->ds,DS_MAT_Q,size_X,d->nBds,&cMTX,d->nBds));
    198:   else PetscCall(DSOrthogonalize(d->eps->ds,DS_MAT_Q,size_X+size_plusk,&cMTX));

    200:   if (d->W && size_plusk > 0) {
    201:     /* ps.Z <- orth([ps.Z(0:size_X-1) [oldV(0:size_plusk-1); 0] ]) */
    202:     PetscCall(DSGetMat(d->eps->ds,DS_MAT_Z,&Z));
    203:     PetscCall(MatDenseGetSubMatrix(Z,0,nV,size_X,size_X+size_plusk,&Z0));
    204:     PetscCall(MatDenseGetSubMatrix(data->oldV,0,nV,0,size_plusk,&V));
    205:     PetscCall(MatCopy(V,Z0,SAME_NONZERO_PATTERN));
    206:     PetscCall(MatDenseRestoreSubMatrix(Z,&Z0));
    207:     PetscCall(MatDenseRestoreSubMatrix(data->oldV,&V));
    208:     PetscCall(DSRestoreMat(d->eps->ds,DS_MAT_Z,&Z));
    209:     PetscCall(DSOrthogonalize(d->eps->ds,DS_MAT_Z,size_X+size_plusk,&cMTY));
    210:     cMTX = PetscMin(cMTX, cMTY);
    211:   }
    212:   PetscAssert(cMTX<=size_X+size_plusk,PETSC_COMM_SELF,PETSC_ERR_SUP,"Invalid number of columns to restart");

    214:   /* Notify the changes in V and update the other subspaces */
    215:   d->V_tra_s = 0;                     d->V_tra_e = cMTX;
    216:   d->V_new_s = d->V_tra_e;            d->V_new_e = d->V_new_s;

    218:   /* Remove oldU */
    219:   data->size_oldU = 0;

    221:   /* Remove npreconv */
    222:   d->npreconv = 0;
    223:   PetscFunctionReturn(PETSC_SUCCESS);
    224: }

    226: static PetscErrorCode dvd_updateV_testConv(dvdDashboard *d,PetscInt s,PetscInt pre,PetscInt e,PetscInt *nConv)
    227: {
    228:   PetscInt        i,j,b;
    229:   PetscReal       norm;
    230:   PetscBool       conv, c;
    231:   dvdManagV_basic *data = (dvdManagV_basic*)d->updateV_data;

    233:   PetscFunctionBegin;
    234:   if (nConv) *nConv = s;
    235:   for (i=s,conv=PETSC_TRUE;(conv || data->allResiduals) && (i < e);i+=b) {
    236: #if !defined(PETSC_USE_COMPLEX)
    237:     b = d->eigi[i]!=0.0?2:1;
    238: #else
    239:     b = 1;
    240: #endif
    241:     if (i+b-1 >= pre) PetscCall(d->calcpairs_residual(d,i,i+b));
    242:     /* Test the Schur vector */
    243:     for (j=0,c=PETSC_TRUE;j<b && c;j++) {
    244:       norm = d->nR[i+j]/d->nX[i+j];
    245:       c = d->testConv(d,d->eigr[i+j],d->eigi[i+j],norm,&d->errest[i+j]);
    246:     }
    247:     if (conv && c) {
    248:       if (nConv) *nConv = i+b;
    249:     } else conv = PETSC_FALSE;
    250:   }
    251:   pre = PetscMax(pre,i);

    253: #if !defined(PETSC_USE_COMPLEX)
    254:   /* Enforce converged conjugate complex eigenpairs */
    255:   if (nConv) {
    256:     for (j=0;j<*nConv;j++) if (d->eigi[j] != 0.0) j++;
    257:     if (j>*nConv) (*nConv)--;
    258:   }
    259: #endif
    260:   for (i=pre;i<e;i++) d->errest[i] = d->nR[i] = 1.0;
    261:   PetscFunctionReturn(PETSC_SUCCESS);
    262: }

    264: static PetscErrorCode dvd_updateV_update_gen(dvdDashboard *d)
    265: {
    266:   dvdManagV_basic *data = (dvdManagV_basic*)d->updateV_data;
    267:   PetscInt        size_D,s,lV,kV,nV;
    268:   Mat             Q,Q0,Z,Z0,U,V;

    270:   PetscFunctionBegin;
    271:   /* Select the desired pairs */
    272:   PetscCall(BVGetActiveColumns(d->eps->V,&lV,&kV));
    273:   nV = kV - lV;
    274:   size_D = PetscMin(PetscMin(PetscMin(d->bs,nV),d->eps->ncv-nV),nV);
    275:   if (size_D == 0) PetscFunctionReturn(PETSC_SUCCESS);

    277:   /* Fill V with D */
    278:   PetscCall(d->improveX(d,d->npreconv,d->npreconv+size_D,&size_D));

    280:   /* If D is empty, exit */
    281:   d->size_D = size_D;
    282:   if (size_D == 0) PetscFunctionReturn(PETSC_SUCCESS);

    284:   /* Get the residual of all pairs */
    285: #if !defined(PETSC_USE_COMPLEX)
    286:   s = (d->eigi[0]!=0.0)? 2: 1;
    287: #else
    288:   s = 1;
    289: #endif
    290:   PetscCall(BVGetActiveColumns(d->eps->V,&lV,&kV));
    291:   nV = kV - lV;
    292:   PetscCall(dvd_updateV_testConv(d,s,s,data->allResiduals?nV:size_D,NULL));

    294:   /* Notify the changes in V */
    295:   d->V_tra_s = 0;                 d->V_tra_e = 0;
    296:   d->V_new_s = nV;                d->V_new_e = nV+size_D;

    298:   /* Save the projected eigenvectors */
    299:   if (data->plusk > 0) {
    300:     PetscCall(MatZeroEntries(data->oldU));
    301:     data->size_oldU = nV;
    302:     PetscCall(DSGetMat(d->eps->ds,DS_MAT_Q,&Q));
    303:     PetscCall(MatDenseGetSubMatrix(Q,0,nV,0,nV,&Q0));
    304:     PetscCall(MatDenseGetSubMatrix(data->oldU,0,nV,0,nV,&U));
    305:     PetscCall(MatCopy(Q0,U,SAME_NONZERO_PATTERN));
    306:     PetscCall(MatDenseRestoreSubMatrix(Q,&Q0));
    307:     PetscCall(MatDenseRestoreSubMatrix(data->oldU,&U));
    308:     PetscCall(DSRestoreMat(d->eps->ds,DS_MAT_Q,&Q));
    309:     if (d->W) {
    310:       PetscCall(MatZeroEntries(data->oldV));
    311:       PetscCall(DSGetMat(d->eps->ds,DS_MAT_Z,&Z));
    312:       PetscCall(MatDenseGetSubMatrix(Z,0,nV,0,nV,&Z0));
    313:       PetscCall(MatDenseGetSubMatrix(data->oldV,0,nV,0,nV,&V));
    314:       PetscCall(MatCopy(Z0,V,SAME_NONZERO_PATTERN));
    315:       PetscCall(MatDenseRestoreSubMatrix(Z,&Z0));
    316:       PetscCall(MatDenseRestoreSubMatrix(data->oldV,&V));
    317:       PetscCall(DSRestoreMat(d->eps->ds,DS_MAT_Z,&Z));
    318:     }
    319:   }
    320:   PetscFunctionReturn(PETSC_SUCCESS);
    321: }

    323: static PetscErrorCode dvd_updateV_extrapol(dvdDashboard *d)
    324: {
    325:   dvdManagV_basic *data = (dvdManagV_basic*)d->updateV_data;
    326:   PetscInt        i;
    327:   PetscBool       restart,t;

    329:   PetscFunctionBegin;
    330:   /* TODO: restrict select pairs to each case */
    331:   PetscCall(d->calcpairs_selectPairs(d, data->min_size_V+d->npreconv));

    333:   /* If the subspaces doesn't need restart, add new vector */
    334:   PetscCall(d->isRestarting(d,&restart));
    335:   if (!restart) {
    336:     d->size_D = 0;
    337:     PetscCall(dvd_updateV_update_gen(d));

    339:     /* If no vector were converged, exit */
    340:     /* For GHEP without B-ortho, converge all of the requested pairs at once */
    341:     PetscCall(PetscObjectTypeCompare((PetscObject)d->eps->ds,DSGHEP,&t));
    342:     if (d->nconv+d->npreconv < d->nev && (t || d->npreconv == 0)) PetscFunctionReturn(PETSC_SUCCESS);
    343:   }

    345:   /* If some eigenpairs were converged, lock them  */
    346:   if (d->npreconv > 0) {
    347:     i = d->npreconv;
    348:     PetscCall(dvd_updateV_conv_gen(d));

    350:     /* If some eigenpair was locked, exit */
    351:     if (i > d->npreconv) PetscFunctionReturn(PETSC_SUCCESS);
    352:   }

    354:   /* Else, a restarting is performed */
    355:   PetscCall(dvd_updateV_restart_gen(d));
    356:   PetscFunctionReturn(PETSC_SUCCESS);
    357: }

    359: PetscErrorCode dvd_managementV_basic(dvdDashboard *d,dvdBlackboard *b,PetscInt bs,PetscInt mpd,PetscInt min_size_V,PetscInt plusk,PetscBool harm,PetscBool allResiduals)
    360: {
    361:   dvdManagV_basic *data;
    362: #if !defined(PETSC_USE_COMPLEX)
    363:   PetscBool       her_probl,std_probl;
    364: #endif

    366:   PetscFunctionBegin;
    367:   /* Setting configuration constrains */
    368: #if !defined(PETSC_USE_COMPLEX)
    369:   /* if the last converged eigenvalue is complex its conjugate pair is also
    370:      converged */
    371:   her_probl = DVD_IS(d->sEP,DVD_EP_HERMITIAN)? PETSC_TRUE: PETSC_FALSE;
    372:   std_probl = DVD_IS(d->sEP,DVD_EP_STD)? PETSC_TRUE: PETSC_FALSE;
    373:   b->max_size_X = PetscMax(b->max_size_X,bs+((her_probl && std_probl)?0:1));
    374: #else
    375:   b->max_size_X = PetscMax(b->max_size_X,bs);
    376: #endif

    378:   b->max_size_V = PetscMax(b->max_size_V,mpd);
    379:   min_size_V = PetscMin(min_size_V,mpd-bs);
    380:   b->size_V = PetscMax(b->size_V,b->max_size_V+b->max_size_P+b->max_nev);
    381:   b->max_size_oldX = plusk;

    383:   /* Setup the step */
    384:   if (b->state >= DVD_STATE_CONF) {
    385:     PetscCall(PetscNew(&data));
    386:     data->mpd = b->max_size_V;
    387:     data->min_size_V = min_size_V;
    388:     d->bs = bs;
    389:     data->plusk = plusk;
    390:     data->allResiduals = allResiduals;

    392:     d->eigr = d->eps->eigr;
    393:     d->eigi = d->eps->eigi;
    394:     d->errest = d->eps->errest;
    395:     PetscCall(PetscMalloc1(d->eps->ncv,&d->real_nR));
    396:     PetscCall(PetscMalloc1(d->eps->ncv,&d->real_nX));
    397:     if (plusk > 0) PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,d->eps->ncv,d->eps->ncv,NULL,&data->oldU));
    398:     else data->oldU = NULL;
    399:     if (harm && plusk>0) PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,d->eps->ncv,d->eps->ncv,NULL,&data->oldV));
    400:     else data->oldV = NULL;

    402:     data->old_updateV_data = d->updateV_data;
    403:     d->updateV_data = data;
    404:     data->old_isRestarting = d->isRestarting;
    405:     d->isRestarting = dvd_isrestarting_fullV;
    406:     d->updateV = dvd_updateV_extrapol;
    407:     d->preTestConv = dvd_updateV_testConv;
    408:     PetscCall(EPSDavidsonFLAdd(&d->startList,dvd_updateV_start));
    409:     PetscCall(EPSDavidsonFLAdd(&d->destroyList,dvd_managementV_basic_d));
    410:   }
    411:   PetscFunctionReturn(PETSC_SUCCESS);
    412: }