Date: 2021-09-20T09:43:51+00:00

Actual source code: ks-twosided.c
slepc-3.15.2 2021-09-20
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2021, 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: "krylovschur"

     13:    Method: Two-sided Arnoldi with Krylov-Schur restart (for left eigenvectors)

     15:    References:

     17:        [1] I.N. Zwaan and M.E. Hochstenbach, "Krylov-Schur-type restarts
     18:            for the two-sided Arnoldi method", SIAM J. Matrix Anal. Appl.
     19:            38(2):297-321, 2017.

     21: */

     23: #include <slepc/private/epsimpl.h>
     24: #include "krylovschur.h"
     25: #include <slepcblaslapack.h>

     27: static PetscErrorCode EPSTwoSidedRQUpdate1(EPS eps,Mat M,PetscInt nv)
     28: {
     29:   PetscErrorCode    ierr;
     30:   PetscScalar       *T,*S,*A,*w,beta;
     31:   const PetscScalar *pM;
     32:   Vec               u;
     33:   PetscInt          ld,ncv=eps->ncv,i,l,nnv;
     34:   PetscBLASInt      info,n_,ncv_,*p,one=1;

     37:   DSGetLeadingDimension(eps->ds,&ld);
     38:   PetscMalloc3(nv,&p,ncv*ncv,&A,ncv,&w);
     39:   BVGetActiveColumns(eps->V,&l,&nnv);
     40:   BVSetActiveColumns(eps->V,0,nv);
     41:   BVSetActiveColumns(eps->W,0,nv);
     42:   BVGetColumn(eps->V,nv,&u);
     43:   BVDotVec(eps->W,u,w);
     44:   BVRestoreColumn(eps->V,nv,&u);
     45:   MatDenseGetArrayRead(M,&pM);
     46:   PetscArraycpy(A,pM,ncv*ncv);
     47:   MatDenseRestoreArrayRead(M,&pM);
     48:   PetscBLASIntCast(nv,&n_);
     49:   PetscBLASIntCast(ncv,&ncv_);
     50:   PetscFPTrapPush(PETSC_FP_TRAP_OFF);
     51:   PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&n_,&n_,A,&ncv_,p,&info));
     52:   SlepcCheckLapackInfo("getrf",info);
     53:   PetscLogFlops(2.0*n_*n_*n_/3.0);
     54:   PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("N",&n_,&one,A,&ncv_,p,w,&ncv_,&info));
     55:   SlepcCheckLapackInfo("getrs",info);
     56:   PetscLogFlops(2.0*n_*n_-n_);
     57:   BVMultColumn(eps->V,-1.0,1.0,nv,w);
     58:   DSGetArray(eps->ds,DS_MAT_A,&S);
     59:   beta = S[(nv-1)*ld+nv];
     60:   for (i=0;i<nv;i++) S[(nv-1)*ld+i] += beta*w[i];
     61:   DSRestoreArray(eps->ds,DS_MAT_A,&S);
     62:   BVGetColumn(eps->W,nv,&u);
     63:   BVDotVec(eps->V,u,w);
     64:   BVRestoreColumn(eps->W,nv,&u);
     65:   PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("C",&n_,&one,A,&ncv_,p,w,&ncv_,&info));
     66:   PetscFPTrapPop();
     67:   BVMultColumn(eps->W,-1.0,1.0,nv,w);
     68:   DSGetArray(eps->ds,DS_MAT_B,&T);
     69:   beta = T[(nv-1)*ld+nv];
     70:   for (i=0;i<nv;i++) T[(nv-1)*ld+i] += beta*w[i];
     71:   DSRestoreArray(eps->ds,DS_MAT_B,&T);
     72:   PetscFree3(p,A,w);
     73:   BVSetActiveColumns(eps->V,l,nnv);
     74:   BVSetActiveColumns(eps->W,l,nnv);
     75:   return(0);
     76: }

     78: static PetscErrorCode EPSTwoSidedRQUpdate2(EPS eps,Mat M,PetscInt k)
     79: {
     81:   PetscScalar    *Q,*pM,*w,zero=0.0,sone=1.0,*c,*A;
     82:   PetscBLASInt   n_,ncv_,ld_;
     83:   PetscReal      norm;
     84:   PetscInt       l,nv,ncv=eps->ncv,ld,i,j;

     87:   DSGetLeadingDimension(eps->ds,&ld);
     88:   BVGetActiveColumns(eps->V,&l,&nv);
     89:   BVSetActiveColumns(eps->V,0,nv);
     90:   BVSetActiveColumns(eps->W,0,nv);
     91:   PetscMalloc2(ncv*ncv,&w,ncv,&c);
     92:   /* u = u - V*V'*u */
     93:   BVOrthogonalizeColumn(eps->V,k,c,&norm,NULL);
     94:   BVScaleColumn(eps->V,k,1.0/norm);
     95:   DSGetArray(eps->ds,DS_MAT_A,&A);
     96:   /* H = H + V'*u*b' */
     97:   for (j=l;j<k;j++) {
     98:     for (i=0;i<k;i++) A[i+j*ld] += c[i]*A[k+j*ld];
     99:     A[k+j*ld] *= norm;
    100:   }
    101:   DSRestoreArray(eps->ds,DS_MAT_A,&A);
    102:   BVOrthogonalizeColumn(eps->W,k,c,&norm,NULL);
    103:   BVScaleColumn(eps->W,k,1.0/norm);
    104:   DSGetArray(eps->ds,DS_MAT_B,&A);
    105:   /* H = H + V'*u*b' */
    106:   for (j=l;j<k;j++) {
    107:     for (i=0;i<k;i++) A[i+j*ld] += c[i]*A[k+j*ld];
    108:     A[k+j*ld] *= norm;
    109:   }
    110:   DSRestoreArray(eps->ds,DS_MAT_B,&A);

    112:   /* M = Q'*M*Q */
    113:   MatDenseGetArray(M,&pM);
    114:   PetscBLASIntCast(ncv,&ncv_);
    115:   PetscBLASIntCast(nv,&n_);
    116:   PetscBLASIntCast(ld,&ld_);
    117:   DSGetArray(eps->ds,DS_MAT_Q,&Q);
    118:   PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&n_,&n_,&n_,&sone,pM,&ncv_,Q,&ld_,&zero,w,&ncv_));
    119:   DSRestoreArray(eps->ds,DS_MAT_Q,&Q);
    120:   DSGetArray(eps->ds,DS_MAT_Z,&Q);
    121:   PetscStackCallBLAS("BLASgemm",BLASgemm_("C","N",&n_,&n_,&n_,&sone,Q,&ld_,w,&ncv_,&zero,pM,&ncv_));
    122:   DSRestoreArray(eps->ds,DS_MAT_Z,&Q);
    123:   MatDenseRestoreArray(M,&pM);
    124:   PetscFree2(w,c);
    125:   BVSetActiveColumns(eps->V,l,nv);
    126:   BVSetActiveColumns(eps->W,l,nv);
    127:   return(0);
    128: }

    130: PetscErrorCode EPSSolve_KrylovSchur_TwoSided(EPS eps)
    131: {
    132:   PetscErrorCode  ierr;
    133:   EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
    134:   Mat             M,U,Op,OpHT;
    135:   PetscReal       norm,norm2,beta,betat;
    136:   PetscScalar     *S,*T;
    137:   PetscInt        ld,l,nv,nvt,k,nconv,dsn,dsk;
    138:   PetscBool       breakdownt,breakdown,breakdownl;

    141:   DSGetLeadingDimension(eps->ds,&ld);
    142:   EPSGetStartVector(eps,0,NULL);
    143:   EPSGetLeftStartVector(eps,0,NULL);
    144:   l = 0;
    145:   MatCreateSeqDense(PETSC_COMM_SELF,eps->ncv,eps->ncv,NULL,&M);

    147:   STGetOperator(eps->st,&Op);
    148:   MatCreateHermitianTranspose(Op,&OpHT);

    150:   /* Restart loop */
    151:   while (eps->reason == EPS_CONVERGED_ITERATING) {
    152:     eps->its++;

    154:     /* Compute an nv-step Arnoldi factorization for Op */
    155:     nv = PetscMin(eps->nconv+eps->mpd,eps->ncv);
    156:     DSGetArray(eps->ds,DS_MAT_A,&S);
    157:     BVMatArnoldi(eps->V,Op,S,ld,eps->nconv+l,&nv,&beta,&breakdown);
    158:     DSRestoreArray(eps->ds,DS_MAT_A,&S);

    160:     /* Compute an nv-step Arnoldi factorization for Op' */
    161:     nvt = nv;
    162:     DSGetArray(eps->ds,DS_MAT_B,&T);
    163:     BVMatArnoldi(eps->W,OpHT,T,ld,eps->nconv+l,&nvt,&betat,&breakdownt);
    164:     DSRestoreArray(eps->ds,DS_MAT_B,&T);

    166:     /* Make sure both factorizations have the same length */
    167:     nv = PetscMin(nv,nvt);
    168:     DSSetDimensions(eps->ds,nv,0,eps->nconv,eps->nconv+l);
    169:     if (l==0) {
    170:       DSSetState(eps->ds,DS_STATE_INTERMEDIATE);
    171:     } else {
    172:       DSSetState(eps->ds,DS_STATE_RAW);
    173:     }
    174:     breakdown = (breakdown || breakdownt)? PETSC_TRUE: PETSC_FALSE;

    176:     /* Update M, modify Rayleigh quotients S and T */
    177:     BVSetActiveColumns(eps->V,eps->nconv+l,nv);
    178:     BVSetActiveColumns(eps->W,eps->nconv+l,nv);
    179:     BVMatProject(eps->V,NULL,eps->W,M);

    181:     EPSTwoSidedRQUpdate1(eps,M,nv);

    183:     /* Solve projected problem */
    184:     DSSolve(eps->ds,eps->eigr,eps->eigi);
    185:     DSSort(eps->ds,eps->eigr,eps->eigi,NULL,NULL,NULL);
    186:     DSSynchronize(eps->ds,eps->eigr,eps->eigi);
    187:     DSUpdateExtraRow(eps->ds);

    189:     /* Check convergence */
    190:     BVNormColumn(eps->V,nv,NORM_2,&norm);
    191:     BVNormColumn(eps->W,nv,NORM_2,&norm2);
    192:     EPSKrylovConvergence(eps,PETSC_FALSE,eps->nconv,nv-eps->nconv,beta*norm,betat*norm2,1.0,&k);
    193:     (*eps->stopping)(eps,eps->its,eps->max_it,k,eps->nev,&eps->reason,eps->stoppingctx);
    194:     nconv = k;

    196:     /* Update l */
    197:     if (eps->reason != EPS_CONVERGED_ITERATING || breakdown || k==nv) l = 0;
    198:     else {
    199:       l = PetscMax(1,(PetscInt)((nv-k)*ctx->keep));
    200:       DSGetTruncateSize(eps->ds,k,nv,&l);
    201:     }
    202:     if (!ctx->lock && l>0) { l += k; k = 0; } /* non-locking variant: reset no. of converged pairs */
    203:     if (l) { PetscInfo1(eps,"Preparing to restart keeping l=%D vectors\n",l); }

    205:     /* Update the corresponding vectors V(:,idx) = V*Q(:,idx) */
    206:     BVSetActiveColumns(eps->V,eps->nconv,nv);
    207:     BVSetActiveColumns(eps->W,eps->nconv,nv);
    208:     DSGetMat(eps->ds,DS_MAT_Q,&U);
    209:     BVMultInPlace(eps->V,U,eps->nconv,k+l);
    210:     MatDestroy(&U);
    211:     DSGetMat(eps->ds,DS_MAT_Z,&U);
    212:     BVMultInPlace(eps->W,U,eps->nconv,k+l);
    213:     MatDestroy(&U);
    214:     if (eps->reason == EPS_CONVERGED_ITERATING && !breakdown) {
    215:       BVCopyColumn(eps->V,nv,k+l);
    216:       BVCopyColumn(eps->W,nv,k+l);
    217:     }

    219:     if (eps->reason == EPS_CONVERGED_ITERATING) {
    220:       if (breakdown || k==nv) {
    221:         /* Start a new Arnoldi factorization */
    222:         PetscInfo2(eps,"Breakdown in Krylov-Schur method (it=%D norm=%g)\n",eps->its,(double)beta);
    223:         if (k<eps->nev) {
    224:           EPSGetStartVector(eps,k,&breakdown);
    225:           EPSGetLeftStartVector(eps,k,&breakdownl);
    226:           if (breakdown || breakdownl) {
    227:             eps->reason = EPS_DIVERGED_BREAKDOWN;
    228:             PetscInfo(eps,"Unable to generate more start vectors\n");
    229:           }
    230:         }
    231:       } else {
    232:         DSGetDimensions(eps->ds,&dsn,NULL,NULL,&dsk,NULL);
    233:         DSSetDimensions(eps->ds,dsn,0,k,dsk);
    234:         DSTruncate(eps->ds,k+l,PETSC_FALSE);
    235:       }
    236:       EPSTwoSidedRQUpdate2(eps,M,k+l);
    237:     }
    238:     eps->nconv = k;
    239:     EPSMonitor(eps,eps->its,nconv,eps->eigr,eps->eigi,eps->errest,nv);
    240:   }

    242:   STRestoreOperator(eps->st,&Op);
    243:   MatDestroy(&OpHT);

    245:   DSTruncate(eps->ds,eps->nconv,PETSC_TRUE);
    246:   MatDestroy(&M);
    247:   return(0);
    248: }