Actual source code: ks-symm.c

  1: /*                       

  3:    SLEPc eigensolver: "krylovschur"

  5:    Method: Krylov-Schur

  7:    Algorithm:

  9:        Single-vector Krylov-Schur method for both symmetric and non-symmetric
 10:        problems.

 12:    References:

 14:        [1] "Krylov-Schur Methods in SLEPc", SLEPc Technical Report STR-7, 
 15:            available at http://www.grycap.upv.es/slepc.

 17:        [2] G.W. Stewart, "A Krylov-Schur Algorithm for Large Eigenproblems",
 18:            SIAM J. Matrix Analysis and App., 23(3), pp. 601-614, 2001. 

 20:    Last update: Feb 2009

 22:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 23:    SLEPc - Scalable Library for Eigenvalue Problem Computations
 24:    Copyright (c) 2002-2010, Universidad Politecnica de Valencia, Spain

 26:    This file is part of SLEPc.
 27:       
 28:    SLEPc is free software: you can redistribute it and/or modify it under  the
 29:    terms of version 3 of the GNU Lesser General Public License as published by
 30:    the Free Software Foundation.

 32:    SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY 
 33:    WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS 
 34:    FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for 
 35:    more details.

 37:    You  should have received a copy of the GNU Lesser General  Public  License
 38:    along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
 39:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 40: */

 42:  #include private/epsimpl.h
 43:  #include slepcblaslapack.h

 47: /*
 48:    ArrowTridFlip - Solves the arrowhead-tridiagonal eigenproblem by flipping
 49:    the matrix and tridiagonalizing the bottom part.

 51:    On input:
 52:      l is the size of diagonal part
 53:      d contains diagonal elements (length n)
 54:      e contains offdiagonal elements (length n-1)

 56:    On output:
 57:      d contains the eigenvalues in ascending order
 58:      Q is the eigenvector matrix (order n)

 60:    Workspace:
 61:      S is workspace to store a copy of the full matrix (nxn reals)
 62: */
 63: PetscErrorCode ArrowTridFlip(PetscInt n_,PetscInt l,PetscReal *d,PetscReal *e,PetscReal *Q,PetscReal *S)
 64: {
 65: #if defined(SLEPC_MISSING_LAPACK_SYTRD) || defined(SLEPC_MISSING_LAPACK_ORGTR) || defined(SLEPC_MISSING_LAPACK_STEQR)
 67:   SETERRQ(PETSC_ERR_SUP,"SYTRD/ORGTR/STEQR - Lapack routine is unavailable.");
 68: #else
 70:   PetscInt       i,j;
 71:   PetscBLASInt   n,n1,n2,lwork,info;

 74:   PetscLogEventBegin(EPS_Dense,0,0,0,0);

 76:   n = PetscBLASIntCast(n_);
 77:   /* quick return */
 78:   if (n == 1) {
 79:     Q[0] = 1.0;
 80:     return(0);
 81:   }
 82:   n1 = PetscBLASIntCast(l+1);    /* size of leading block, including residuals */
 83:   n2 = PetscBLASIntCast(n-l-1);  /* size of trailing block */
 84:   PetscMemzero(S,n*n*sizeof(PetscReal));

 86:   /* Flip matrix S, copying the values saved in Q */
 87:   for (i=0;i<n;i++)
 88:     S[(n-1-i)+(n-1-i)*n] = d[i];
 89:   for (i=0;i<l;i++)
 90:     S[(n-1-i)+(n-1-l)*n] = e[i];
 91:   for (i=l;i<n-1;i++)
 92:     S[(n-1-i)+(n-1-i-1)*n] = e[i];

 94:   /* Reduce (2,2)-block of flipped S to tridiagonal form */
 95:   lwork = PetscBLASIntCast(n_*n_-n_);
 96:   LAPACKsytrd_("L",&n1,S+n2*(n+1),&n,d,e,Q,Q+n,&lwork,&info);
 97:   if (info) SETERRQ1(PETSC_ERR_LIB,"Error in Lapack xSYTRD %d",info);

 99:   /* Flip back diag and subdiag, put them in d and e */
100:   for (i=0;i<n-1;i++) {
101:     d[n-i-1] = S[i+i*n];
102:     e[n-i-2] = S[i+1+i*n];
103:   }
104:   d[0] = S[n-1+(n-1)*n];

106:   /* Compute the orthogonal matrix used for tridiagonalization */
107:   LAPACKorgtr_("L",&n1,S+n2*(n+1),&n,Q,Q+n,&lwork,&info);
108:   if (info) SETERRQ1(PETSC_ERR_LIB,"Error in Lapack xORGTR %d",info);

110:   /* Create full-size Q, flipped back to original order */
111:   for (i=0;i<n;i++)
112:     for (j=0;j<n;j++)
113:       Q[i+j*n] = 0.0;
114:   for (i=n1;i<n;i++)
115:     Q[i+i*n] = 1.0;
116:   for (i=0;i<n1;i++)
117:     for (j=0;j<n1;j++)
118:       Q[i+j*n] = S[n-i-1+(n-j-1)*n];

120:   /* Solve the tridiagonal eigenproblem */
121:   LAPACKsteqr_("V",&n,d,e,Q,&n,S,&info);
122:   if (info) SETERRQ1(PETSC_ERR_LIB,"Error in Lapack xSTEQR %d",info);

124:   PetscLogEventEnd(EPS_Dense,0,0,0,0);
125:   return(0);
126: #endif
127: }

131: /*
132:    EPSProjectedKSSym - Solves the projected eigenproblem in the Krylov-Schur
133:    method (symmetric case).

135:    On input:
136:      n is the matrix dimension
137:      l is the number of vectors kept in previous restart
138:      a contains diagonal elements (length n)
139:      b contains offdiagonal elements (length n-1)

141:    On output:
142:      eig is the sorted list of eigenvalues
143:      Q is the eigenvector matrix (order n, leading dimension n)

145:    Workspace:
146:      work is workspace to store a real square matrix of order n
147:      perm is workspace to store 2n integers
148: */
149: PetscErrorCode EPSProjectedKSSym(EPS eps,PetscInt n,PetscInt l,PetscReal *a,PetscReal *b,PetscScalar *eig,PetscScalar *Q,PetscReal *work,PetscInt *perm)
150: {
152:   PetscInt       i,j,k,p;
153:   PetscReal      rtmp,*Qreal = (PetscReal*)Q;

156:   /* Compute eigendecomposition of projected matrix */
157:   ArrowTridFlip(n,l,a,b,Qreal,work);

159:   /* Sort eigendecomposition according to eps->which */
160:   EPSSortEigenvaluesReal(eps,n,a,perm);
161:   for (i=0;i<n;i++)
162:     eig[i] = a[perm[i]];
163:   for (i=0;i<n;i++) {
164:     p = perm[i];
165:     if (p != i) {
166:       j = i + 1;
167:       while (perm[j] != i) j++;
168:       perm[j] = p; perm[i] = i;
169:       /* swap eigenvectors i and j */
170:       for (k=0;k<n;k++) {
171:         rtmp = Qreal[k+p*n]; Qreal[k+p*n] = Qreal[k+i*n]; Qreal[k+i*n] = rtmp;
172:       }
173:     }
174:   }

176: #if defined(PETSC_USE_COMPLEX)
177:   for (j=n-1;j>=0;j--)
178:     for (i=n-1;i>=0;i--)
179:       Q[i+j*n] = Qreal[i+j*n];
180: #endif
181:   return(0);
182: }

186: PetscErrorCode EPSSolve_KRYLOVSCHUR_SYMM(EPS eps)
187: {
189:   PetscInt       i,k,l,lds,lt,nv,m;
190:   Vec            u=eps->work[0];
191:   PetscScalar    *Q;
192:   PetscReal      *a,*b,*work,beta;
193:   PetscInt       *iwork;
194:   PetscTruth     breakdown;

197:   lds = PetscMin(eps->mpd,eps->ncv);
198:   PetscMalloc(lds*lds*sizeof(PetscReal),&work);
199:   PetscMalloc(lds*lds*sizeof(PetscScalar),&Q);
200:   PetscMalloc(2*lds*sizeof(PetscInt),&iwork);
201:   lt = PetscMin(eps->nev+eps->mpd,eps->ncv);
202:   PetscMalloc(lt*sizeof(PetscReal),&a);
203:   PetscMalloc(lt*sizeof(PetscReal),&b);

205:   /* Get the starting Lanczos vector */
206:   EPSGetStartVector(eps,0,eps->V[0],PETSC_NULL);
207:   l = 0;
208: 
209:   /* Restart loop */
210:   while (eps->reason == EPS_CONVERGED_ITERATING) {
211:     eps->its++;

213:     /* Compute an nv-step Lanczos factorization */
214:     m = PetscMin(eps->nconv+eps->mpd,eps->ncv);
215:     EPSFullLanczos(eps,a+l,b+l,eps->V,eps->nconv+l,&m,u,&breakdown);
216:     nv = m - eps->nconv;
217:     beta = b[nv-1];

219:     /* Solve projected problem and compute residual norm estimates */
220:     EPSProjectedKSSym(eps,nv,l,a,b,eps->eigr+eps->nconv,Q,work,iwork);

222:     /* Check convergence */
223:     EPSKrylovConvergence(eps,PETSC_TRUE,eps->nconv,nv,PETSC_NULL,nv,Q,eps->V+eps->nconv,nv,beta,1.0,&k,PETSC_NULL);
224:     if (eps->its >= eps->max_it) eps->reason = EPS_DIVERGED_ITS;
225:     if (k >= eps->nev) eps->reason = EPS_CONVERGED_TOL;
226: 
227:     /* Update l */
228:     if (eps->reason != EPS_CONVERGED_ITERATING || breakdown) l = 0;
229:     else l = (eps->nconv+nv-k)/2;

231:     if (eps->reason == EPS_CONVERGED_ITERATING) {
232:       if (breakdown) {
233:         /* Start a new Lanczos factorization */
234:         PetscInfo2(eps,"Breakdown in Krylov-Schur method (it=%i norm=%g)\n",eps->its,beta);
235:         EPSGetStartVector(eps,k,eps->V[k],&breakdown);
236:         if (breakdown) {
237:           eps->reason = EPS_DIVERGED_BREAKDOWN;
238:           PetscInfo(eps,"Unable to generate more start vectors\n");
239:         }
240:       } else {
241:         /* Prepare the Rayleigh quotient for restart */
242:         for (i=0;i<l;i++) {
243:           a[i] = PetscRealPart(eps->eigr[i+k]);
244:           b[i] = PetscRealPart(Q[nv-1+(i+k-eps->nconv)*nv]*beta);
245:         }
246:       }
247:     }
248:     /* Update the corresponding vectors V(:,idx) = V*Q(:,idx) */
249:     SlepcUpdateVectors(nv,eps->V+eps->nconv,0,k+l-eps->nconv,Q,nv,PETSC_FALSE);
250:     /* Normalize u and append it to V */
251:     if (eps->reason == EPS_CONVERGED_ITERATING && !breakdown) {
252:       VecAXPBY(eps->V[k+l],1.0/beta,0.0,u);
253:     }

255:     EPSMonitor(eps,eps->its,k,eps->eigr,eps->eigi,eps->errest,nv+eps->nconv);
256:     eps->nconv = k;
257: 
258:   }

260:   PetscFree(Q);
261:   PetscFree(a);
262:   PetscFree(b);
263:   PetscFree(work);
264:   PetscFree(iwork);
265:   return(0);
266: }