LCOV - code coverage report
Current view: top level - eps/impls/krylov/krylovschur - ks-twosided.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 157 164 95.7 %
Date: 2024-11-23 00:34:26 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       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"
      12             : 
      13             :    Method: Two-sided Arnoldi with Krylov-Schur restart (for left eigenvectors)
      14             : 
      15             :    References:
      16             : 
      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.
      20             : 
      21             : */
      22             : 
      23             : #include <slepc/private/epsimpl.h>
      24             : #include "krylovschur.h"
      25             : #include <slepcblaslapack.h>
      26             : 
      27          77 : static PetscErrorCode EPSTwoSidedRQUpdate1(EPS eps,Mat M,PetscInt nv,PetscReal beta,PetscReal betat)
      28             : {
      29          77 :   PetscScalar       *T,*S,*A,*w;
      30          77 :   const PetscScalar *pM;
      31          77 :   Vec               u;
      32          77 :   PetscInt          ld,ncv=eps->ncv,i,l,nnv;
      33          77 :   PetscBLASInt      info,n_,ncv_,*p,one=1;
      34             : 
      35          77 :   PetscFunctionBegin;
      36          77 :   PetscCall(DSGetLeadingDimension(eps->ds,&ld));
      37          77 :   PetscCall(PetscMalloc3(nv,&p,ncv*ncv,&A,ncv,&w));
      38          77 :   PetscCall(BVGetActiveColumns(eps->V,&l,&nnv));
      39          77 :   PetscCall(BVSetActiveColumns(eps->V,0,nv));
      40          77 :   PetscCall(BVSetActiveColumns(eps->W,0,nv));
      41          77 :   PetscCall(BVGetColumn(eps->V,nv,&u));
      42          77 :   PetscCall(BVDotVec(eps->W,u,w));
      43          77 :   PetscCall(BVRestoreColumn(eps->V,nv,&u));
      44          77 :   PetscCall(MatDenseGetArrayRead(M,&pM));
      45          77 :   PetscCall(PetscArraycpy(A,pM,ncv*ncv));
      46          77 :   PetscCall(MatDenseRestoreArrayRead(M,&pM));
      47          77 :   PetscCall(PetscBLASIntCast(nv,&n_));
      48          77 :   PetscCall(PetscBLASIntCast(ncv,&ncv_));
      49          77 :   PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
      50          77 :   PetscCallBLAS("LAPACKgetrf",LAPACKgetrf_(&n_,&n_,A,&ncv_,p,&info));
      51          77 :   SlepcCheckLapackInfo("getrf",info);
      52          77 :   PetscCall(PetscLogFlops(2.0*n_*n_*n_/3.0));
      53          77 :   PetscCallBLAS("LAPACKgetrs",LAPACKgetrs_("N",&n_,&one,A,&ncv_,p,w,&ncv_,&info));
      54          77 :   SlepcCheckLapackInfo("getrs",info);
      55          77 :   PetscCall(PetscLogFlops(2.0*n_*n_-n_));
      56          77 :   PetscCall(BVMultColumn(eps->V,-1.0,1.0,nv,w));
      57          77 :   PetscCall(DSGetArray(eps->ds,DS_MAT_A,&S));
      58        1542 :   for (i=0;i<nv;i++) S[(nv-1)*ld+i] += beta*w[i];
      59          77 :   PetscCall(DSRestoreArray(eps->ds,DS_MAT_A,&S));
      60          77 :   PetscCall(BVGetColumn(eps->W,nv,&u));
      61          77 :   PetscCall(BVDotVec(eps->V,u,w));
      62          77 :   PetscCall(BVRestoreColumn(eps->W,nv,&u));
      63          77 :   PetscCallBLAS("LAPACKgetrs",LAPACKgetrs_("C",&n_,&one,A,&ncv_,p,w,&ncv_,&info));
      64          77 :   PetscCall(PetscFPTrapPop());
      65          77 :   PetscCall(BVMultColumn(eps->W,-1.0,1.0,nv,w));
      66          77 :   PetscCall(DSGetArray(eps->ds,DS_MAT_B,&T));
      67        1542 :   for (i=0;i<nv;i++) T[(nv-1)*ld+i] += betat*w[i];
      68          77 :   PetscCall(DSRestoreArray(eps->ds,DS_MAT_B,&T));
      69          77 :   PetscCall(PetscFree3(p,A,w));
      70          77 :   PetscCall(BVSetActiveColumns(eps->V,l,nnv));
      71          77 :   PetscCall(BVSetActiveColumns(eps->W,l,nnv));
      72          77 :   PetscFunctionReturn(PETSC_SUCCESS);
      73             : }
      74             : 
      75          64 : static PetscErrorCode EPSTwoSidedRQUpdate2(EPS eps,Mat M,PetscInt k)
      76             : {
      77          64 :   PetscScalar    *Q,*pM,*w,zero=0.0,sone=1.0,*c,*A;
      78          64 :   PetscBLASInt   n_,ncv_,ld_;
      79          64 :   PetscReal      norm;
      80          64 :   PetscInt       l,nv,ncv=eps->ncv,ld,i,j;
      81             : 
      82          64 :   PetscFunctionBegin;
      83          64 :   PetscCall(DSGetLeadingDimension(eps->ds,&ld));
      84          64 :   PetscCall(BVGetActiveColumns(eps->V,&l,&nv));
      85          64 :   PetscCall(BVSetActiveColumns(eps->V,0,nv));
      86          64 :   PetscCall(BVSetActiveColumns(eps->W,0,nv));
      87          64 :   PetscCall(PetscMalloc2(ncv*ncv,&w,ncv,&c));
      88             :   /* u = u - V*V'*u */
      89          64 :   PetscCall(BVOrthogonalizeColumn(eps->V,k,c,&norm,NULL));
      90          64 :   PetscCall(BVScaleColumn(eps->V,k,1.0/norm));
      91          64 :   PetscCall(DSGetArray(eps->ds,DS_MAT_A,&A));
      92             :   /* H = H + V'*u*b' */
      93         673 :   for (j=l;j<k;j++) {
      94        6593 :     for (i=0;i<k;i++) A[i+j*ld] += c[i]*A[k+j*ld];
      95         609 :     A[k+j*ld] *= norm;
      96             :   }
      97          64 :   PetscCall(DSRestoreArray(eps->ds,DS_MAT_A,&A));
      98          64 :   PetscCall(BVOrthogonalizeColumn(eps->W,k,c,&norm,NULL));
      99          64 :   PetscCall(BVScaleColumn(eps->W,k,1.0/norm));
     100          64 :   PetscCall(DSGetArray(eps->ds,DS_MAT_B,&A));
     101             :   /* H = H + V'*u*b' */
     102         673 :   for (j=l;j<k;j++) {
     103        6593 :     for (i=0;i<k;i++) A[i+j*ld] += c[i]*A[k+j*ld];
     104         609 :     A[k+j*ld] *= norm;
     105             :   }
     106          64 :   PetscCall(DSRestoreArray(eps->ds,DS_MAT_B,&A));
     107             : 
     108             :   /* M = Q'*M*Q */
     109          64 :   PetscCall(MatDenseGetArray(M,&pM));
     110          64 :   PetscCall(PetscBLASIntCast(ncv,&ncv_));
     111          64 :   PetscCall(PetscBLASIntCast(nv,&n_));
     112          64 :   PetscCall(PetscBLASIntCast(ld,&ld_));
     113          64 :   PetscCall(DSGetArray(eps->ds,DS_MAT_Q,&Q));
     114          64 :   PetscCallBLAS("BLASgemm",BLASgemm_("N","N",&n_,&n_,&n_,&sone,pM,&ncv_,Q,&ld_,&zero,w,&ncv_));
     115          64 :   PetscCall(DSRestoreArray(eps->ds,DS_MAT_Q,&Q));
     116          64 :   PetscCall(DSGetArray(eps->ds,DS_MAT_Z,&Q));
     117          64 :   PetscCallBLAS("BLASgemm",BLASgemm_("C","N",&n_,&n_,&n_,&sone,Q,&ld_,w,&ncv_,&zero,pM,&ncv_));
     118          64 :   PetscCall(DSRestoreArray(eps->ds,DS_MAT_Z,&Q));
     119          64 :   PetscCall(MatDenseRestoreArray(M,&pM));
     120          64 :   PetscCall(PetscFree2(w,c));
     121          64 :   PetscCall(BVSetActiveColumns(eps->V,l,nv));
     122          64 :   PetscCall(BVSetActiveColumns(eps->W,l,nv));
     123          64 :   PetscFunctionReturn(PETSC_SUCCESS);
     124             : }
     125             : 
     126          13 : PetscErrorCode EPSSolve_KrylovSchur_TwoSided(EPS eps)
     127             : {
     128          13 :   EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
     129          13 :   Mat             M,U,Op,OpHT,S,T;
     130          13 :   PetscReal       norm,norm2,beta,betat;
     131          13 :   PetscInt        ld,l,nv,nvt,k,nconv,dsn,dsk;
     132          13 :   PetscBool       breakdownt,breakdown,breakdownl;
     133             : 
     134          13 :   PetscFunctionBegin;
     135          13 :   PetscCall(DSGetLeadingDimension(eps->ds,&ld));
     136          13 :   PetscCall(EPSGetStartVector(eps,0,NULL));
     137          13 :   PetscCall(EPSGetLeftStartVector(eps,0,NULL));
     138          13 :   l = 0;
     139          13 :   PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,eps->ncv,eps->ncv,NULL,&M));
     140             : 
     141          13 :   PetscCall(STGetOperator(eps->st,&Op));
     142          13 :   PetscCall(MatCreateHermitianTranspose(Op,&OpHT));
     143             : 
     144             :   /* Restart loop */
     145          90 :   while (eps->reason == EPS_CONVERGED_ITERATING) {
     146          77 :     eps->its++;
     147             : 
     148             :     /* Compute an nv-step Arnoldi factorization for Op */
     149          77 :     nv = PetscMin(eps->nconv+eps->mpd,eps->ncv);
     150          77 :     PetscCall(DSSetDimensions(eps->ds,nv,eps->nconv,eps->nconv+l));
     151          77 :     PetscCall(DSGetMat(eps->ds,DS_MAT_A,&S));
     152          77 :     PetscCall(BVMatArnoldi(eps->V,Op,S,eps->nconv+l,&nv,&beta,&breakdown));
     153          77 :     PetscCall(DSRestoreMat(eps->ds,DS_MAT_A,&S));
     154             : 
     155             :     /* Compute an nv-step Arnoldi factorization for Op' */
     156          77 :     nvt = nv;
     157          77 :     PetscCall(DSSetDimensions(eps->ds,nv,eps->nconv,eps->nconv+l));
     158          77 :     PetscCall(DSGetMat(eps->ds,DS_MAT_B,&T));
     159          77 :     PetscCall(BVMatArnoldi(eps->W,OpHT,T,eps->nconv+l,&nvt,&betat,&breakdownt));
     160          77 :     PetscCall(DSRestoreMat(eps->ds,DS_MAT_B,&T));
     161             : 
     162             :     /* Make sure both factorizations have the same length */
     163          77 :     nv = PetscMin(nv,nvt);
     164          77 :     PetscCall(DSSetDimensions(eps->ds,nv,eps->nconv,eps->nconv+l));
     165          77 :     if (l==0) PetscCall(DSSetState(eps->ds,DS_STATE_INTERMEDIATE));
     166          64 :     else PetscCall(DSSetState(eps->ds,DS_STATE_RAW));
     167          77 :     breakdown = (breakdown || breakdownt)? PETSC_TRUE: PETSC_FALSE;
     168             : 
     169             :     /* Update M, modify Rayleigh quotients S and T */
     170          77 :     PetscCall(BVSetActiveColumns(eps->V,eps->nconv+l,nv));
     171          77 :     PetscCall(BVSetActiveColumns(eps->W,eps->nconv+l,nv));
     172          77 :     PetscCall(BVMatProject(eps->V,NULL,eps->W,M));
     173             : 
     174          77 :     PetscCall(EPSTwoSidedRQUpdate1(eps,M,nv,beta,betat));
     175             : 
     176             :     /* Solve projected problem */
     177          77 :     PetscCall(DSSolve(eps->ds,eps->eigr,eps->eigi));
     178          77 :     PetscCall(DSSort(eps->ds,eps->eigr,eps->eigi,NULL,NULL,NULL));
     179          77 :     PetscCall(DSSynchronize(eps->ds,eps->eigr,eps->eigi));
     180          77 :     PetscCall(DSUpdateExtraRow(eps->ds));
     181             : 
     182             :     /* Check convergence */
     183          77 :     PetscCall(BVNormColumn(eps->V,nv,NORM_2,&norm));
     184          77 :     PetscCall(BVNormColumn(eps->W,nv,NORM_2,&norm2));
     185          77 :     PetscCall(EPSKrylovConvergence(eps,PETSC_FALSE,eps->nconv,nv-eps->nconv,beta*norm,betat*norm2,1.0,&k));
     186          77 :     PetscCall((*eps->stopping)(eps,eps->its,eps->max_it,k,eps->nev,&eps->reason,eps->stoppingctx));
     187          77 :     nconv = k;
     188             : 
     189             :     /* Update l */
     190          77 :     if (eps->reason != EPS_CONVERGED_ITERATING || breakdown || k==nv) l = 0;
     191             :     else {
     192          64 :       l = PetscMax(1,(PetscInt)((nv-k)*ctx->keep));
     193          64 :       PetscCall(DSGetTruncateSize(eps->ds,k,nv,&l));
     194             :     }
     195          77 :     if (!ctx->lock && l>0) { l += k; k = 0; } /* non-locking variant: reset no. of converged pairs */
     196          77 :     if (l) PetscCall(PetscInfo(eps,"Preparing to restart keeping l=%" PetscInt_FMT " vectors\n",l));
     197             : 
     198             :     /* Update the corresponding vectors V(:,idx) = V*Q(:,idx) */
     199          77 :     PetscCall(BVSetActiveColumns(eps->V,eps->nconv,nv));
     200          77 :     PetscCall(BVSetActiveColumns(eps->W,eps->nconv,nv));
     201          77 :     PetscCall(DSGetMat(eps->ds,DS_MAT_Q,&U));
     202          77 :     PetscCall(BVMultInPlace(eps->V,U,eps->nconv,k+l));
     203          77 :     PetscCall(DSRestoreMat(eps->ds,DS_MAT_Q,&U));
     204          77 :     PetscCall(DSGetMat(eps->ds,DS_MAT_Z,&U));
     205          77 :     PetscCall(BVMultInPlace(eps->W,U,eps->nconv,k+l));
     206          77 :     PetscCall(DSRestoreMat(eps->ds,DS_MAT_Z,&U));
     207          77 :     if (eps->reason == EPS_CONVERGED_ITERATING && !breakdown) {
     208          64 :       PetscCall(BVCopyColumn(eps->V,nv,k+l));
     209          64 :       PetscCall(BVCopyColumn(eps->W,nv,k+l));
     210             :     }
     211             : 
     212          77 :     if (eps->reason == EPS_CONVERGED_ITERATING) {
     213          64 :       if (breakdown || k==nv) {
     214             :         /* Start a new Arnoldi factorization */
     215           0 :         PetscCall(PetscInfo(eps,"Breakdown in Krylov-Schur method (it=%" PetscInt_FMT " norm=%g)\n",eps->its,(double)beta));
     216           0 :         if (k<eps->nev) {
     217           0 :           PetscCall(EPSGetStartVector(eps,k,&breakdown));
     218           0 :           PetscCall(EPSGetLeftStartVector(eps,k,&breakdownl));
     219           0 :           if (breakdown || breakdownl) {
     220           0 :             eps->reason = EPS_DIVERGED_BREAKDOWN;
     221           0 :             PetscCall(PetscInfo(eps,"Unable to generate more start vectors\n"));
     222             :           }
     223             :         }
     224             :       } else {
     225          64 :         PetscCall(DSGetDimensions(eps->ds,&dsn,NULL,&dsk,NULL));
     226          64 :         PetscCall(DSSetDimensions(eps->ds,dsn,k,dsk));
     227          64 :         PetscCall(DSTruncate(eps->ds,k+l,PETSC_FALSE));
     228             :       }
     229          64 :       PetscCall(EPSTwoSidedRQUpdate2(eps,M,k+l));
     230             :     }
     231          77 :     eps->nconv = k;
     232          90 :     PetscCall(EPSMonitor(eps,eps->its,nconv,eps->eigr,eps->eigi,eps->errest,nv));
     233             :   }
     234             : 
     235          13 :   PetscCall(STRestoreOperator(eps->st,&Op));
     236          13 :   PetscCall(MatDestroy(&OpHT));
     237             : 
     238          13 :   PetscCall(DSTruncate(eps->ds,eps->nconv,PETSC_TRUE));
     239          13 :   PetscCall(MatDestroy(&M));
     240          13 :   PetscFunctionReturn(PETSC_SUCCESS);
     241             : }

Generated by: LCOV version 1.14