Actual source code: ks-slice.c

slepc-3.7.1 2016-05-27
Report Typos and Errors
  1: /*

  3:    SLEPc eigensolver: "krylovschur"

  5:    Method: Krylov-Schur with spectrum slicing for symmetric eigenproblems

  7:    References:

  9:        [1] R.G. Grimes et al., "A shifted block Lanczos algorithm for
 10:            solving sparse symmetric generalized eigenproblems", SIAM J.
 11:            Matrix Anal. Appl. 15(1):228-272, 1994.

 13:        [2] C. Campos and J.E. Roman, "Spectrum slicing strategies based
 14:            on restarted Lanczos methods", Numer. Algor. 60(2):279-295,
 15:            2012.

 17:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 18:    SLEPc - Scalable Library for Eigenvalue Problem Computations
 19:    Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain

 21:    This file is part of SLEPc.

 23:    SLEPc is free software: you can redistribute it and/or modify it under  the
 24:    terms of version 3 of the GNU Lesser General Public License as published by
 25:    the Free Software Foundation.

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

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

 37: #include <slepc/private/epsimpl.h>
 38:  #include krylovschur.h

 40: static PetscBool  cited = PETSC_FALSE;
 41: static const char citation[] =
 42:   "@Article{slepc-slice,\n"
 43:   "   author = \"C. Campos and J. E. Roman\",\n"
 44:   "   title = \"Strategies for spectrum slicing based on restarted {Lanczos} methods\",\n"
 45:   "   journal = \"Numer. Algorithms\",\n"
 46:   "   volume = \"60\",\n"
 47:   "   number = \"2\",\n"
 48:   "   pages = \"279--295\",\n"
 49:   "   year = \"2012,\"\n"
 50:   "   doi = \"http://dx.doi.org/10.1007/s11075-012-9564-z\"\n"
 51:   "}\n";

 53: #define SLICE_PTOL PETSC_SQRT_MACHINE_EPSILON

 57: static PetscErrorCode EPSSliceResetSR(EPS eps) {
 58:   PetscErrorCode  ierr;
 59:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;
 60:   EPS_SR          sr=ctx->sr;
 61:   EPS_shift       s;

 64:   if (sr) {
 65:     if (ctx->npart>1) {
 66:       BVDestroy(&sr->V);
 67:       PetscFree4(sr->eigr,sr->eigi,sr->errest,sr->perm);
 68:     }
 69:     /* Reviewing list of shifts to free memory */
 70:     s = sr->s0;
 71:     if (s) {
 72:       while (s->neighb[1]) {
 73:         s = s->neighb[1];
 74:         PetscFree(s->neighb[0]);
 75:       }
 76:       PetscFree(s);
 77:     }
 78:     PetscFree(sr);
 79:   }
 80:   ctx->sr = NULL;
 81:   return(0);
 82: }

 86: PetscErrorCode EPSReset_KrylovSchur_Slice(EPS eps)
 87: {
 88:   PetscErrorCode  ierr;
 89:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;

 92:   if (!ctx->global) return(0);
 93:   /* Destroy auxiliary EPS */
 94:   EPSSliceResetSR(ctx->eps);
 95:   EPSDestroy(&ctx->eps);
 96:   if (ctx->npart>1) {
 97:     PetscSubcommDestroy(&ctx->subc);
 98:     if (ctx->commset) {
 99:       MPI_Comm_free(&ctx->commrank);
100:       ctx->commset = PETSC_FALSE;
101:     }
102:   }
103:   PetscFree(ctx->subintervals);
104:   PetscFree(ctx->nconv_loc);
105:   EPSSliceResetSR(eps);
106:   PetscFree(ctx->inertias);
107:   PetscFree(ctx->shifts);
108:   if (ctx->npart>1) {
109:     ISDestroy(&ctx->isrow);
110:     ISDestroy(&ctx->iscol);
111:     MatDestroyMatrices(1,&ctx->submata);
112:     MatDestroyMatrices(1,&ctx->submatb);
113:   }
114:   return(0);
115: }

119: /*
120:   EPSSliceAllocateSolution - Allocate memory storage for common variables such
121:   as eigenvalues and eigenvectors. The argument extra is used for methods
122:   that require a working basis slightly larger than ncv.
123: */
124: static PetscErrorCode EPSSliceAllocateSolution(EPS eps,PetscInt extra)
125: {
126:   PetscErrorCode     ierr;
127:   EPS_KRYLOVSCHUR    *ctx=(EPS_KRYLOVSCHUR*)eps->data;
128:   PetscReal          eta;
129:   PetscInt           k;
130:   PetscLogDouble     cnt;
131:   BVType             type;
132:   BVOrthogType       orthog_type;
133:   BVOrthogRefineType orthog_ref;
134:   BVOrthogBlockType  ob_type;
135:   Mat                matrix;
136:   Vec                t;
137:   EPS_SR             sr = ctx->sr;

140:   /* allocate space for eigenvalues and friends */
141:   k = PetscMax(1,sr->numEigs);
142:   PetscFree4(sr->eigr,sr->eigi,sr->errest,sr->perm);
143:   PetscMalloc4(k,&sr->eigr,k,&sr->eigi,k,&sr->errest,k,&sr->perm);
144:   cnt = 2*k*sizeof(PetscScalar) + 2*k*sizeof(PetscReal) + k*sizeof(PetscInt);
145:   PetscLogObjectMemory((PetscObject)eps,cnt);

147:   /* allocate sr->V and transfer options from eps->V */
148:   BVDestroy(&sr->V);
149:   BVCreate(PetscObjectComm((PetscObject)eps),&sr->V);
150:   PetscLogObjectParent((PetscObject)eps,(PetscObject)sr->V);
151:   if (!eps->V) { EPSGetBV(eps,&eps->V); }
152:   if (!((PetscObject)(eps->V))->type_name) {
153:     BVSetType(sr->V,BVSVEC);
154:   } else {
155:     BVGetType(eps->V,&type);
156:     BVSetType(sr->V,type);
157:   }
158:   STMatCreateVecs(eps->st,&t,NULL);
159:   BVSetSizesFromVec(sr->V,t,k);
160:   VecDestroy(&t);
161:   EPS_SetInnerProduct(eps);
162:   BVGetMatrix(eps->V,&matrix,NULL);
163:   BVSetMatrix(sr->V,matrix,PETSC_FALSE);
164:   BVGetOrthogonalization(eps->V,&orthog_type,&orthog_ref,&eta,&ob_type);
165:   BVSetOrthogonalization(sr->V,orthog_type,orthog_ref,eta,ob_type);
166:   return(0);
167: }

171: static PetscErrorCode EPSSliceGetEPS(EPS eps)
172: {
173:   PetscErrorCode     ierr;
174:   EPS_KRYLOVSCHUR    *ctx=(EPS_KRYLOVSCHUR*)eps->data,*ctx_local;
175:   BV                 V;
176:   BVType             type;
177:   PetscReal          eta;
178:   BVOrthogType       orthog_type;
179:   BVOrthogRefineType orthog_ref;
180:   BVOrthogBlockType  ob_type;
181:   Mat                A,B=NULL,Ar,Br=NULL;
182:   PetscInt           i;
183:   PetscReal          h,a,b;
184:   PetscMPIInt        rank;
185:   EPS_SR             sr=ctx->sr;
186:   PC                 pc;
187:   PCType             pctype;
188:   KSP                ksp;
189:   KSPType            ksptype;
190:   STType             sttype;
191:   PetscObjectState   Astate,Bstate=0;
192:   PetscObjectId      Aid,Bid=0;
193:   const MatSolverPackage stype;

196:   EPSGetOperators(eps,&A,&B);
197:   if (ctx->npart==1) {
198:     if (!ctx->eps) { EPSCreate(((PetscObject)eps)->comm,&ctx->eps); }
199:     EPSSetType(ctx->eps,((PetscObject)eps)->type_name);
200:     EPSSetOperators(ctx->eps,A,B);
201:     a = eps->inta; b = eps->intb;
202:   } else {
203:     PetscObjectStateGet((PetscObject)A,&Astate);
204:     PetscObjectGetId((PetscObject)A,&Aid);
205:     if (B) {
206:       PetscObjectStateGet((PetscObject)B,&Bstate);
207:       PetscObjectGetId((PetscObject)B,&Bid);
208:     }
209:     if (!ctx->subc) {
210:       /* Create context for subcommunicators */
211:       PetscSubcommCreate(PetscObjectComm((PetscObject)eps),&ctx->subc);
212:       PetscSubcommSetNumber(ctx->subc,ctx->npart);
213:       PetscSubcommSetType(ctx->subc,PETSC_SUBCOMM_CONTIGUOUS);
214:       PetscLogObjectMemory((PetscObject)eps,sizeof(PetscSubcomm));

216:       /* Duplicate matrices */
217:       MatCreateRedundantMatrix(A,0,PetscSubcommChild(ctx->subc),MAT_INITIAL_MATRIX,&Ar);
218:       ctx->Astate = Astate;
219:       ctx->Aid = Aid;
220:       if (B) {
221:         MatCreateRedundantMatrix(B,0,PetscSubcommChild(ctx->subc),MAT_INITIAL_MATRIX,&Br);
222:         ctx->Bstate = Bstate;
223:         ctx->Bid = Bid;
224:       }
225:     } else {
226:       if (ctx->Astate != Astate || (B && ctx->Bstate != Bstate) || ctx->Aid != Aid || (B && ctx->Bid != Bid)) {
227:         EPSGetOperators(ctx->eps,&Ar,&Br);
228:         MatCreateRedundantMatrix(A,0,PetscSubcommChild(ctx->subc),MAT_INITIAL_MATRIX,&Ar);
229:         ctx->Astate = Astate;
230:         ctx->Aid = Aid;
231:         if (B) {
232:           MatCreateRedundantMatrix(B,0,PetscSubcommChild(ctx->subc),MAT_INITIAL_MATRIX,&Br);
233:           ctx->Bstate = Bstate;
234:           ctx->Bid = Bid;
235:         }
236:         EPSSetOperators(ctx->eps,Ar,Br);
237:         MatDestroy(&Ar);
238:         MatDestroy(&Br);
239:       }
240:     }

242:     /* Determine subintervals */
243:     if (!ctx->subintset) { /* uniform distribution if no set by user */
244:       if (!sr->hasEnd) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_WRONG,"Global interval must be bounded for splitting it in uniform subintervals");
245:       h = (eps->intb-eps->inta)/ctx->npart;
246:       a = eps->inta+ctx->subc->color*h;
247:       b = (ctx->subc->color==ctx->npart-1)?eps->intb:eps->inta+(ctx->subc->color+1)*h;
248:       PetscFree(ctx->subintervals);
249:       PetscMalloc1(ctx->npart+1,&ctx->subintervals);
250:       for (i=0;i<ctx->npart;i++) ctx->subintervals[i] = eps->inta+h*i;
251:       ctx->subintervals[ctx->npart] = eps->intb;
252:     } else {
253:       a = ctx->subintervals[ctx->subc->color];
254:       b = ctx->subintervals[ctx->subc->color+1];
255:     }

257:     if (!ctx->eps) {
258:       /* Create auxiliary EPS */
259:       EPSCreate(PetscSubcommChild(ctx->subc),&ctx->eps);
260:       EPSSetOperators(ctx->eps,Ar,Br);
261:       MatDestroy(&Ar);
262:       MatDestroy(&Br);
263:     }

265:     /* Create subcommunicator grouping processes with same rank */
266:     if (ctx->commset) { MPI_Comm_free(&ctx->commrank); }
267:     MPI_Comm_rank(PetscSubcommChild(ctx->subc),&rank);
268:     MPI_Comm_split(((PetscObject)eps)->comm,rank,ctx->subc->color,&ctx->commrank);
269:     ctx->commset = PETSC_TRUE;
270:   }
271:   EPSSetType(ctx->eps,((PetscObject)eps)->type_name);

273:   /* Transfer options for ST, KSP and PC */
274:   STGetType(eps->st,&sttype);
275:   STSetType(ctx->eps->st,sttype);
276:   STGetKSP(eps->st,&ksp);
277:   KSPGetType(ksp,&ksptype);
278:   KSPGetPC(ksp,&pc);
279:   PCGetType(pc,&pctype);
280:   PCFactorGetMatSolverPackage(pc,&stype);
281:   STGetKSP(ctx->eps->st,&ksp);
282:   KSPSetType(ksp,ksptype);
283:   KSPGetPC(ksp,&pc);
284:   PCSetType(pc,pctype);
285:   if (stype) { PCFactorSetMatSolverPackage(pc,stype); }

287:   EPSSetConvergenceTest(ctx->eps,eps->conv);
288:   EPSSetInterval(ctx->eps,a,b);
289:   ctx_local = (EPS_KRYLOVSCHUR*)ctx->eps->data;
290:   ctx_local->npart = ctx->npart;
291:   ctx_local->detect = ctx->detect;
292:   ctx_local->global = PETSC_FALSE;
293:   ctx_local->eps = eps;
294:   ctx_local->subc = ctx->subc;
295:   ctx_local->commrank = ctx->commrank;

297:   EPSSetDimensions(ctx->eps,ctx->nev,ctx->ncv,ctx->mpd);
298:   EPSKrylovSchurSetLocking(ctx->eps,ctx->lock);

300:   /* transfer options from eps->V */
301:   EPSGetBV(ctx->eps,&V);
302:   if (!eps->V) { EPSGetBV(eps,&eps->V); }
303:   if (!((PetscObject)(eps->V))->type_name) {
304:     BVSetType(V,BVSVEC);
305:   } else {
306:     BVGetType(eps->V,&type);
307:     BVSetType(V,type);
308:   }
309:   BVGetOrthogonalization(eps->V,&orthog_type,&orthog_ref,&eta,&ob_type);
310:   BVSetOrthogonalization(V,orthog_type,orthog_ref,eta,ob_type);
311:   ctx->eps->which = eps->which;
312:   ctx->eps->max_it = eps->max_it;
313:   ctx->eps->tol = eps->tol;
314:   ctx->eps->purify = eps->purify;
315:   if (eps->tol==PETSC_DEFAULT) eps->tol = SLEPC_DEFAULT_TOL;
316:   EPSSetProblemType(ctx->eps,eps->problem_type);
317:   EPSSetUp(ctx->eps);
318:   ctx->eps->nconv = 0;
319:   ctx->eps->its   = 0;
320:   for (i=0;i<ctx->eps->ncv;i++) {
321:     ctx->eps->eigr[i]   = 0.0;
322:     ctx->eps->eigi[i]   = 0.0;
323:     ctx->eps->errest[i] = 0.0;
324:   }
325:   return(0);
326: }

330: static PetscErrorCode EPSSliceGetInertia(EPS eps,PetscReal shift,PetscInt *inertia,PetscInt *zeros)
331: {
333:   KSP            ksp;
334:   PC             pc;
335:   Mat            F;
336:   PetscReal      nzshift;

339:   if (shift >= PETSC_MAX_REAL) { /* Right-open interval */
340:     if (inertia) *inertia = eps->n;
341:   } else if (shift <= PETSC_MIN_REAL) {
342:     if (inertia) *inertia = 0;
343:     if (zeros) *zeros = 0;
344:   } else {
345:     /* If the shift is zero, perturb it to a very small positive value.
346:        The goal is that the nonzero pattern is the same in all cases and reuse
347:        the symbolic factorizations */
348:     nzshift = (shift==0.0)? 10.0/PETSC_MAX_REAL: shift;
349:     STSetShift(eps->st,nzshift);
350:     STSetUp(eps->st);
351:     STGetKSP(eps->st,&ksp);
352:     KSPGetPC(ksp,&pc);
353:     PCFactorGetMatrix(pc,&F);
354:     MatGetInertia(F,inertia,zeros,NULL);
355:   }
356:   return(0);
357: }

361: PetscErrorCode EPSSetUp_KrylovSchur_Slice(EPS eps)
362: {
363:   PetscErrorCode  ierr;
364:   PetscBool       issinv;
365:   EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data,*ctx_glob;
366:   EPS_SR          sr,sr_loc,sr_glob;
367:   PetscInt        nEigs,dssz=1,i,zeros=0,off=0;
368:   PetscMPIInt     nproc,rank,aux;
369:   PetscReal       r;
370:   MPI_Request     req;
371:   Mat             A,B=NULL;

374:   if (ctx->global) {
375:     if (eps->inta==0.0 && eps->intb==0.0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_WRONG,"Must define a computational interval when using EPS_ALL");
376:     if (eps->intb >= PETSC_MAX_REAL && eps->inta <= PETSC_MIN_REAL) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_WRONG,"The defined computational interval should have at least one of their sides bounded");
377:     if (!eps->ishermitian) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Spectrum slicing only available for symmetric/Hermitian eigenproblems");
378:     if (eps->arbitrary) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Arbitrary selection of eigenpairs cannot be used with spectrum slicing");
379:     if (!((PetscObject)(eps->st))->type_name) { /* default to shift-and-invert */
380:       STSetType(eps->st,STSINVERT);
381:     }
382:     PetscObjectTypeCompareAny((PetscObject)eps->st,&issinv,STSINVERT,STCAYLEY,"");
383:     if (!issinv) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Shift-and-invert or Cayley ST is needed for spectrum slicing");
384:     if (eps->tol==PETSC_DEFAULT) eps->tol = SLEPC_DEFAULT_TOL*1e-2;  /* use tighter tolerance */
385:     if (!eps->max_it) eps->max_it = 100;
386:     if (ctx->nev==1) ctx->nev = PetscMin(40,eps->n);  /* nev not set, use default value */
387:     if (eps->n>10 && ctx->nev<10) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_WRONG,"nev cannot be less than 10 in spectrum slicing runs");
388:   }
389:   eps->ops->backtransform = NULL;

391:   /* create spectrum slicing context and initialize it */
392:   EPSSliceResetSR(eps);
393:   PetscNewLog(eps,&sr);
394:   ctx->sr = sr;
395:   sr->itsKs = 0;
396:   sr->nleap = 0;
397:   sr->nMAXCompl = eps->nev/4;
398:   sr->iterCompl = eps->max_it/4;
399:   sr->sPres = NULL;
400:   sr->nS = 0;

402:   if (ctx->npart==1 || ctx->global) {
403:     /* check presence of ends and finding direction */
404:     if ((eps->inta > PETSC_MIN_REAL && eps->inta != 0.0) || eps->intb >= PETSC_MAX_REAL) {
405:       sr->int0 = eps->inta;
406:       sr->int1 = eps->intb;
407:       sr->dir = 1;
408:       if (eps->intb >= PETSC_MAX_REAL) { /* Right-open interval */
409:         sr->hasEnd = PETSC_FALSE;
410:       } else sr->hasEnd = PETSC_TRUE;
411:     } else {
412:       sr->int0 = eps->intb;
413:       sr->int1 = eps->inta;
414:       sr->dir = -1;
415:       sr->hasEnd = PetscNot(eps->inta <= PETSC_MIN_REAL);
416:     }
417:   }
418:   if (ctx->global) {
419:     /* prevent computation of factorization in global eps */
420:     STSetTransform(eps->st,PETSC_FALSE);
421:     EPSSetDimensions_Default(eps,ctx->nev,&ctx->ncv,&ctx->mpd);
422:     /* create subintervals and initialize auxiliary eps for slicing runs */
423:     EPSSliceGetEPS(eps);
424:     sr_loc = ((EPS_KRYLOVSCHUR*)ctx->eps->data)->sr;
425:     if (ctx->npart>1) {
426:       if ((sr->dir>0&&ctx->subc->color==0)||(sr->dir<0&&ctx->subc->color==ctx->npart-1)) sr->inertia0 = sr_loc->inertia0;
427:       MPI_Comm_rank(PetscSubcommChild(ctx->subc),&rank);
428:       if (rank==0) {
429:         MPI_Bcast(&sr->inertia0,1,MPIU_INT,(sr->dir>0)?0:ctx->npart-1,ctx->commrank);
430:       }
431:       MPI_Bcast(&sr->inertia0,1,MPIU_INT,0,PetscSubcommChild(ctx->subc));
432:       PetscFree(ctx->nconv_loc);
433:       PetscMalloc1(ctx->npart,&ctx->nconv_loc);
434:       MPI_Comm_size(((PetscObject)eps)->comm,&nproc);
435:       if (sr->dir<0) off = 1;
436:       if (nproc%ctx->npart==0) { /* subcommunicators with the same size */
437:         PetscMPIIntCast(sr_loc->numEigs,&aux);
438:         MPI_Allgather(&aux,1,MPI_INT,ctx->nconv_loc,1,MPI_INT,ctx->commrank);
439:         MPI_Allgather(&sr_loc->int0,1,MPIU_REAL,ctx->subintervals+off,1,MPIU_REAL,ctx->commrank);
440:       } else {
441:         MPI_Comm_rank(PetscSubcommChild(ctx->subc),&rank);
442:         if (!rank) {
443:           PetscMPIIntCast(sr_loc->numEigs,&aux);
444:           MPI_Allgather(&aux,1,MPI_INT,ctx->nconv_loc,1,MPI_INT,ctx->commrank);
445:           MPI_Allgather(&sr_loc->int0,1,MPIU_REAL,ctx->subintervals+off,1,MPIU_REAL,ctx->commrank);
446:         }
447:         PetscMPIIntCast(ctx->npart,&aux);
448:         MPI_Bcast(ctx->nconv_loc,aux,MPI_INT,0,PetscSubcommChild(ctx->subc));
449:         MPI_Bcast(ctx->subintervals+off,aux,MPIU_REAL,0,PetscSubcommChild(ctx->subc));
450:       }
451:       nEigs = 0;
452:       for (i=0;i<ctx->npart;i++) nEigs += ctx->nconv_loc[i];
453:     } else {
454:       nEigs = sr_loc->numEigs;
455:       sr->inertia0 = sr_loc->inertia0;
456:     }
457:     sr->inertia1 = sr->inertia0+sr->dir*nEigs;
458:     sr->numEigs = nEigs;
459:     eps->nev = nEigs;
460:     eps->ncv = nEigs;
461:     eps->mpd = nEigs;
462:   } else {
463:     ctx_glob = (EPS_KRYLOVSCHUR*)ctx->eps->data;
464:     sr_glob = ctx_glob->sr;
465:     if (ctx->npart>1) {
466:       sr->dir = sr_glob->dir;
467:       sr->int0 = (sr->dir==1)?eps->inta:eps->intb;
468:       sr->int1 = (sr->dir==1)?eps->intb:eps->inta;
469:       if ((sr->dir>0&&ctx->subc->color==ctx->npart-1)||(sr->dir<0&&ctx->subc->color==0)) sr->hasEnd = sr_glob->hasEnd;
470:       else sr->hasEnd = PETSC_TRUE;
471:     }

473:     /* compute inertia0 */
474:     EPSSliceGetInertia(eps,sr->int0,&sr->inertia0,ctx->detect?&zeros:NULL);
475:     if (zeros) { /* error in factorization */
476:       if (ctx->npart==1 || ctx_glob->subintset || ((sr->dir>0 && ctx->subc->color==0) || (sr->dir<0 && ctx->subc->color==ctx->npart-1))) SETERRQ(((PetscObject)eps)->comm,PETSC_ERR_USER,"Found singular matrix for the transformed problem in an interval endpoint defined by user");
477:       else { /* perturb shift */
478:         sr->int0 *= (1.0+SLICE_PTOL);
479:         EPSSliceGetInertia(eps,sr->int0,&sr->inertia0,&zeros);
480:         if (zeros) SETERRQ1(((PetscObject)eps)->comm,PETSC_ERR_CONV_FAILED,"Inertia computation fails in %g",sr->int1);
481:       }
482:     }
483:     if (ctx->npart>1) {
484:       /* inertia1 is received from neighbour */
485:       MPI_Comm_rank(PetscSubcommChild(ctx->subc),&rank);
486:       if (!rank) {
487:         if ((sr->dir>0 && ctx->subc->color>0) || (sr->dir<0 && ctx->subc->color<ctx->npart-1)) { /* send inertia0 to neighbour0 */
488:           MPI_Isend(&(sr->inertia0),1,MPIU_INT,ctx->subc->color-sr->dir,0,ctx->commrank,&req);
489:           MPI_Isend(&(sr->int0),1,MPIU_REAL,ctx->subc->color-sr->dir,0,ctx->commrank,&req);
490:         }
491:         if ((sr->dir>0 && ctx->subc->color<ctx->npart-1)|| (sr->dir<0 && ctx->subc->color>0)) { /* receive inertia1 from neighbour1 */
492:           MPI_Recv(&(sr->inertia1),1,MPIU_INT,ctx->subc->color+sr->dir,0,ctx->commrank,MPI_STATUS_IGNORE);
493:           MPI_Recv(&(sr->int1),1,MPIU_REAL,ctx->subc->color+sr->dir,0,ctx->commrank,MPI_STATUS_IGNORE);
494:         }
495:       }
496:       if ((sr->dir>0 && ctx->subc->color<ctx->npart-1)||(sr->dir<0 && ctx->subc->color>0)) {
497:         MPI_Bcast(&sr->inertia1,1,MPIU_INT,0,PetscSubcommChild(ctx->subc));
498:         MPI_Bcast(&sr->int1,1,MPIU_REAL,0,PetscSubcommChild(ctx->subc));
499:       } else sr_glob->inertia1 = sr->inertia1;
500:     }

502:     /* last process in eps comm computes inertia1 */
503:     if (ctx->npart==1 || ((sr->dir>0 && ctx->subc->color==ctx->npart-1) || (sr->dir<0 && ctx->subc->color==0))) {
504:       EPSSliceGetInertia(eps,sr->int1,&sr->inertia1,ctx->detect?&zeros:NULL);
505:       if (zeros) SETERRQ(((PetscObject)eps)->comm,PETSC_ERR_USER,"Found singular matrix for the transformed problem in an interval endpoint defined by user");
506:       sr->dir = -sr->dir; r = sr->int0; sr->int0 = sr->int1; sr->int1 = r;
507:       i = sr->inertia0; sr->inertia0 = sr->inertia1; sr->inertia1 = i;
508:     }

510:     /* number of eigenvalues in interval */
511:     sr->numEigs = (sr->dir)*(sr->inertia1 - sr->inertia0);
512:     if (ctx->npart>1) {
513:       /* memory allocate for subinterval eigenpairs */
514:       EPSSliceAllocateSolution(eps,1);
515:     }
516:     dssz = eps->ncv+1;
517:   }
518:   DSSetType(eps->ds,DSHEP);
519:   DSSetCompact(eps->ds,PETSC_TRUE);
520:   DSAllocate(eps->ds,dssz);
521:   /* keep state of subcomm matrices to check that the user does not modify them */
522:   EPSGetOperators(eps,&A,&B);
523:   PetscObjectStateGet((PetscObject)A,&ctx->Astate);
524:   PetscObjectGetId((PetscObject)A,&ctx->Aid);
525:   if (B) { 
526:     PetscObjectStateGet((PetscObject)B,&ctx->Bstate);
527:     PetscObjectGetId((PetscObject)B,&ctx->Bid);
528:   } else {
529:     ctx->Bstate=0;
530:     ctx->Bid=0;
531:   }
532:   return(0);
533: }

537: static PetscErrorCode EPSSliceGatherEigenVectors(EPS eps)
538: {
539:   PetscErrorCode  ierr;
540:   Vec             v,vg,v_loc;
541:   IS              is1,is2;
542:   VecScatter      vec_sc;
543:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;
544:   PetscInt        nloc,m0,n0,i,si,idx,*idx1,*idx2,j;
545:   PetscScalar     *array;
546:   EPS_SR          sr_loc;
547:   BV              V_loc;

550:   sr_loc = ((EPS_KRYLOVSCHUR*)ctx->eps->data)->sr;
551:   V_loc = sr_loc->V;

553:   /* Gather parallel eigenvectors */
554:   BVGetColumn(eps->V,0,&v);
555:   VecGetOwnershipRange(v,&n0,&m0);
556:   BVRestoreColumn(eps->V,0,&v);
557:   BVGetColumn(ctx->eps->V,0,&v);
558:   VecGetLocalSize(v,&nloc);
559:   BVRestoreColumn(ctx->eps->V,0,&v);
560:   PetscMalloc2(m0-n0,&idx1,m0-n0,&idx2);
561:   VecCreateMPI(PetscObjectComm((PetscObject)eps),nloc,PETSC_DECIDE,&vg);
562:   idx = -1;
563:   for (si=0;si<ctx->npart;si++) {
564:     j = 0;
565:     for (i=n0;i<m0;i++) {
566:       idx1[j]   = i;
567:       idx2[j++] = i+eps->n*si;
568:     }
569:     ISCreateGeneral(PetscObjectComm((PetscObject)eps),(m0-n0),idx1,PETSC_COPY_VALUES,&is1);
570:     ISCreateGeneral(PetscObjectComm((PetscObject)eps),(m0-n0),idx2,PETSC_COPY_VALUES,&is2);
571:     BVGetColumn(eps->V,0,&v);
572:     VecScatterCreate(v,is1,vg,is2,&vec_sc);
573:     BVRestoreColumn(eps->V,0,&v);
574:     ISDestroy(&is1);
575:     ISDestroy(&is2);
576:     for (i=0;i<ctx->nconv_loc[si];i++) {
577:       BVGetColumn(eps->V,++idx,&v);
578:       if (ctx->subc->color==si) {
579:         BVGetColumn(V_loc,i,&v_loc);
580:         VecGetArray(v_loc,&array);
581:         VecPlaceArray(vg,array);
582:       }
583:       VecScatterBegin(vec_sc,vg,v,INSERT_VALUES,SCATTER_REVERSE);
584:       VecScatterEnd(vec_sc,vg,v,INSERT_VALUES,SCATTER_REVERSE);
585:       if (ctx->subc->color==si) {
586:         VecResetArray(vg);
587:         VecRestoreArray(v_loc,&array);
588:         BVRestoreColumn(V_loc,i,&v_loc);
589:       }
590:       BVRestoreColumn(eps->V,idx,&v);
591:     }
592:     VecScatterDestroy(&vec_sc);
593:   }
594:   PetscFree2(idx1,idx2);
595:   VecDestroy(&vg);
596:   return(0);
597: }

601: /*
602:   EPSComputeVectors_Slice - Recover Eigenvectors from subcomunicators
603:  */
604: PetscErrorCode EPSComputeVectors_Slice(EPS eps)
605: {
606:   PetscErrorCode  ierr;
607:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;

610:   if (ctx->global && ctx->npart>1) {
611:     EPSComputeVectors(ctx->eps);
612:     EPSSliceGatherEigenVectors(eps);
613:   }
614:   return(0);
615: }

617: #define SWAP(a,b,t) {t=a;a=b;b=t;}

621: static PetscErrorCode EPSSliceGetInertias(EPS eps,PetscInt *n,PetscReal **shifts,PetscInt **inertias)
622: {
623:   PetscErrorCode  ierr;
624:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;
625:   PetscInt        i=0,j,tmpi;
626:   PetscReal       v,tmpr;
627:   EPS_shift       s;

630:   if (!eps->state) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_WRONGSTATE,"Must call EPSSetUp() first");
631:   if (!ctx->sr) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_WRONGSTATE,"Only available in interval computations, see EPSSetInterval()");
632:   if (!ctx->sr->s0) {  /* EPSSolve not called yet */
633:     *n = 2;
634:   } else {
635:     *n = 1;
636:     s = ctx->sr->s0;
637:     while (s) {
638:       (*n)++;
639:       s = s->neighb[1];
640:     }
641:   }
642:   PetscMalloc1(*n,shifts);
643:   PetscMalloc1(*n,inertias);
644:   if (!ctx->sr->s0) {  /* EPSSolve not called yet */
645:     (*shifts)[0]   = ctx->sr->int0;
646:     (*shifts)[1]   = ctx->sr->int1;
647:     (*inertias)[0] = ctx->sr->inertia0;
648:     (*inertias)[1] = ctx->sr->inertia1;
649:   } else {
650:     s = ctx->sr->s0;
651:     while (s) {
652:       (*shifts)[i]     = s->value;
653:       (*inertias)[i++] = s->inertia;
654:       s = s->neighb[1];
655:     }
656:     (*shifts)[i]   = ctx->sr->int1;
657:     (*inertias)[i] = ctx->sr->inertia1;
658:   }
659:   /* remove possible duplicate in last position */
660:   if ((*shifts)[(*n)-1]==(*shifts)[(*n)-2]) (*n)--;
661:   /* sort result */
662:   for (i=0;i<*n;i++) {
663:     v = (*shifts)[i];
664:     for (j=i+1;j<*n;j++) {
665:       if (v > (*shifts)[j]) {
666:         SWAP((*shifts)[i],(*shifts)[j],tmpr);
667:         SWAP((*inertias)[i],(*inertias)[j],tmpi);
668:         v = (*shifts)[i];
669:       }
670:     }
671:   }
672:   return(0);
673: }

677: static PetscErrorCode EPSSliceGatherSolution(EPS eps)
678: {
679:   PetscErrorCode  ierr;
680:   PetscMPIInt     rank,nproc;
681:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;
682:   PetscInt        i,idx,j;
683:   PetscInt        *perm_loc,off=0,*inertias_loc,ns;
684:   PetscScalar     *eigr_loc;
685:   EPS_SR          sr_loc;
686:   PetscReal       *shifts_loc;
687:   PetscMPIInt     *disp,*ns_loc,aux;

690:   eps->nconv = 0;
691:   for (i=0;i<ctx->npart;i++) eps->nconv += ctx->nconv_loc[i];
692:   sr_loc = ((EPS_KRYLOVSCHUR*)ctx->eps->data)->sr;

694:   /* Gather the shifts used and the inertias computed */
695:   EPSSliceGetInertias(ctx->eps,&ns,&shifts_loc,&inertias_loc);
696:   if (ctx->sr->dir>0 && shifts_loc[ns-1]==sr_loc->int1 && ctx->subc->color<ctx->npart-1) ns--;
697:   if (ctx->sr->dir<0 && shifts_loc[ns-1]==sr_loc->int0 && ctx->subc->color>0) {
698:     ns--;
699:     for (i=0;i<ns;i++) {
700:       inertias_loc[i] = inertias_loc[i+1];
701:       shifts_loc[i] = shifts_loc[i+1];
702:     }
703:   }
704:   PetscMalloc1(ctx->npart,&ns_loc);
705:   MPI_Comm_rank(PetscSubcommChild(ctx->subc),&rank);
706:   PetscMPIIntCast(ns,&aux);
707:   if (rank==0) { MPI_Allgather(&aux,1,MPI_INT,ns_loc,1,MPI_INT,ctx->commrank); }
708:   PetscMPIIntCast(ctx->npart,&aux);
709:   MPI_Bcast(ns_loc,aux,MPI_INT,0,PetscSubcommChild(ctx->subc));
710:   ctx->nshifts = 0;
711:   for (i=0;i<ctx->npart;i++) ctx->nshifts += ns_loc[i];
712:   PetscFree(ctx->inertias);
713:   PetscFree(ctx->shifts);
714:   PetscMalloc1(ctx->nshifts,&ctx->inertias);
715:   PetscMalloc1(ctx->nshifts,&ctx->shifts);

717:   /* Gather eigenvalues (same ranks have fully set of eigenvalues)*/
718:   eigr_loc = sr_loc->eigr;
719:   perm_loc = sr_loc->perm;
720:   MPI_Comm_size(((PetscObject)eps)->comm,&nproc);
721:   PetscMalloc1(ctx->npart,&disp);
722:   disp[0] = 0;
723:   for (i=1;i<ctx->npart;i++) disp[i] = disp[i-1]+ctx->nconv_loc[i-1];
724:   if (nproc%ctx->npart==0) { /* subcommunicators with the same size */
725:     PetscMPIIntCast(sr_loc->numEigs,&aux);
726:     MPI_Allgatherv(eigr_loc,aux,MPIU_SCALAR,eps->eigr,ctx->nconv_loc,disp,MPIU_SCALAR,ctx->commrank); /* eigenvalues */
727:     MPI_Allgatherv(perm_loc,aux,MPIU_INT,eps->perm,ctx->nconv_loc,disp,MPIU_INT,ctx->commrank); /* perm */
728:     for (i=1;i<ctx->npart;i++) disp[i] = disp[i-1]+ns_loc[i-1];
729:     PetscMPIIntCast(ns,&aux);
730:     MPI_Allgatherv(shifts_loc,aux,MPIU_REAL,ctx->shifts,ns_loc,disp,MPIU_REAL,ctx->commrank); /* shifts */
731:     MPI_Allgatherv(inertias_loc,aux,MPIU_INT,ctx->inertias,ns_loc,disp,MPIU_INT,ctx->commrank); /* inertias */
732:     MPI_Allreduce(&sr_loc->itsKs,&eps->its,1,MPIU_INT,MPI_SUM,ctx->commrank);
733:   } else { /* subcommunicators with different size */
734:     MPI_Comm_rank(PetscSubcommChild(ctx->subc),&rank);
735:     if (rank==0) {
736:       PetscMPIIntCast(sr_loc->numEigs,&aux);
737:       MPI_Allgatherv(eigr_loc,aux,MPIU_SCALAR,eps->eigr,ctx->nconv_loc,disp,MPIU_SCALAR,ctx->commrank); /* eigenvalues */
738:       MPI_Allgatherv(perm_loc,aux,MPIU_INT,eps->perm,ctx->nconv_loc,disp,MPIU_INT,ctx->commrank); /* perm */
739:       for (i=1;i<ctx->npart;i++) disp[i] = disp[i-1]+ns_loc[i-1];
740:       PetscMPIIntCast(ns,&aux);
741:       MPI_Allgatherv(shifts_loc,aux,MPIU_REAL,ctx->shifts,ns_loc,disp,MPIU_REAL,ctx->commrank); /* shifts */
742:       MPI_Allgatherv(inertias_loc,aux,MPIU_INT,ctx->inertias,ns_loc,disp,MPIU_INT,ctx->commrank); /* inertias */
743:       MPI_Allreduce(&sr_loc->itsKs,&eps->its,1,MPIU_INT,MPI_SUM,ctx->commrank);
744:     }
745:     PetscMPIIntCast(eps->nconv,&aux);
746:     MPI_Bcast(eps->eigr,aux,MPIU_SCALAR,0,PetscSubcommChild(ctx->subc));
747:     MPI_Bcast(eps->perm,aux,MPIU_INT,0,PetscSubcommChild(ctx->subc));
748:     MPI_Bcast(ctx->shifts,ctx->nshifts,MPIU_REAL,0,PetscSubcommChild(ctx->subc));
749:     PetscMPIIntCast(ctx->nshifts,&aux);
750:     MPI_Bcast(ctx->inertias,aux,MPIU_INT,0,PetscSubcommChild(ctx->subc));
751:     MPI_Bcast(&eps->its,1,MPIU_INT,0,PetscSubcommChild(ctx->subc));
752:   }
753:   /* Update global array eps->perm */
754:   idx = ctx->nconv_loc[0];
755:   for (i=1;i<ctx->npart;i++) {
756:     off += ctx->nconv_loc[i-1];
757:     for (j=0;j<ctx->nconv_loc[i];j++) eps->perm[idx++] += off;
758:   }

760:   /* Gather parallel eigenvectors */
761:   PetscFree(ns_loc);
762:   PetscFree(disp);
763:   PetscFree(shifts_loc);
764:   PetscFree(inertias_loc);
765:   return(0);
766: }

768: /*
769:    Fills the fields of a shift structure
770: */
773: static PetscErrorCode EPSCreateShift(EPS eps,PetscReal val,EPS_shift neighb0,EPS_shift neighb1)
774: {
775:   PetscErrorCode  ierr;
776:   EPS_shift       s,*pending2;
777:   PetscInt        i;
778:   EPS_SR          sr;
779:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;

782:   sr = ctx->sr;
783:   PetscNewLog(eps,&s);
784:   s->value = val;
785:   s->neighb[0] = neighb0;
786:   if (neighb0) neighb0->neighb[1] = s;
787:   s->neighb[1] = neighb1;
788:   if (neighb1) neighb1->neighb[0] = s;
789:   s->comp[0] = PETSC_FALSE;
790:   s->comp[1] = PETSC_FALSE;
791:   s->index = -1;
792:   s->neigs = 0;
793:   s->nconv[0] = s->nconv[1] = 0;
794:   s->nsch[0] = s->nsch[1]=0;
795:   /* Inserts in the stack of pending shifts */
796:   /* If needed, the array is resized */
797:   if (sr->nPend >= sr->maxPend) {
798:     sr->maxPend *= 2;
799:     PetscMalloc1(sr->maxPend,&pending2);
800:     PetscLogObjectMemory((PetscObject)eps,sizeof(EPS_shift));
801:     for (i=0;i<sr->nPend;i++) pending2[i] = sr->pending[i];
802:     PetscFree(sr->pending);
803:     sr->pending = pending2;
804:   }
805:   sr->pending[sr->nPend++]=s;
806:   return(0);
807: }

809: /* Prepare for Rational Krylov update */
812: static PetscErrorCode EPSPrepareRational(EPS eps)
813: {
814:   EPS_KRYLOVSCHUR  *ctx=(EPS_KRYLOVSCHUR*)eps->data;
815:   PetscErrorCode   ierr;
816:   PetscInt         dir,i,k,ld,nv;
817:   PetscScalar      *A;
818:   EPS_SR           sr = ctx->sr;
819:   Vec              v;

822:   DSGetLeadingDimension(eps->ds,&ld);
823:   dir = (sr->sPres->neighb[0] == sr->sPrev)?1:-1;
824:   dir*=sr->dir;
825:   k = 0;
826:   for (i=0;i<sr->nS;i++) {
827:     if (dir*PetscRealPart(sr->S[i])>0.0) {
828:       sr->S[k] = sr->S[i];
829:       sr->S[sr->nS+k] = sr->S[sr->nS+i];
830:       BVGetColumn(sr->Vnext,k,&v);
831:       BVCopyVec(eps->V,eps->nconv+i,v);
832:       BVRestoreColumn(sr->Vnext,k,&v);
833:       k++;
834:       if (k>=sr->nS/2)break;
835:     }
836:   }
837:   /* Copy to DS */
838:   DSGetArray(eps->ds,DS_MAT_A,&A);
839:   PetscMemzero(A,ld*ld*sizeof(PetscScalar));
840:   for (i=0;i<k;i++) {
841:     A[i*(1+ld)] = sr->S[i];
842:     A[k+i*ld] = sr->S[sr->nS+i];
843:   }
844:   sr->nS = k;
845:   DSRestoreArray(eps->ds,DS_MAT_A,&A);
846:   DSGetDimensions(eps->ds,&nv,NULL,NULL,NULL,NULL);
847:   DSSetDimensions(eps->ds,nv,0,0,k);
848:   /* Append u to V */
849:   BVGetColumn(sr->Vnext,sr->nS,&v);
850:   BVCopyVec(eps->V,sr->nv,v);
851:   BVRestoreColumn(sr->Vnext,sr->nS,&v);
852:   return(0);
853: }

855: /* Provides next shift to be computed */
858: static PetscErrorCode EPSExtractShift(EPS eps)
859: {
860:   PetscErrorCode   ierr;
861:   PetscInt         iner,zeros=0;
862:   EPS_KRYLOVSCHUR  *ctx=(EPS_KRYLOVSCHUR*)eps->data;
863:   EPS_SR           sr;
864:   PetscReal        newShift;
865:   EPS_shift        sPres;

868:   sr = ctx->sr;
869:   if (sr->nPend > 0) {
870:     sr->sPrev = sr->sPres;
871:     sr->sPres = sr->pending[--sr->nPend];
872:     sPres = sr->sPres;
873:     EPSSliceGetInertia(eps,sPres->value,&iner,ctx->detect?&zeros:NULL);
874:     if (zeros) {
875:       newShift = sPres->value*(1.0+SLICE_PTOL);
876:       if (sr->dir*(sPres->neighb[0] && newShift-sPres->neighb[0]->value) < 0) newShift = (sPres->value+sPres->neighb[0]->value)/2;
877:       else if (sPres->neighb[1] && sr->dir*(sPres->neighb[1]->value-newShift) < 0) newShift = (sPres->value+sPres->neighb[1]->value)/2;
878:       EPSSliceGetInertia(eps,newShift,&iner,&zeros);
879:       if (zeros) SETERRQ1(((PetscObject)eps)->comm,PETSC_ERR_CONV_FAILED,"Inertia computation fails in %g",newShift);
880:       sPres->value = newShift;
881:     }
882:     sr->sPres->inertia = iner;
883:     eps->target = sr->sPres->value;
884:     eps->reason = EPS_CONVERGED_ITERATING;
885:     eps->its = 0;
886:   } else sr->sPres = NULL;
887:   return(0);
888: }

890: /*
891:    Symmetric KrylovSchur adapted to spectrum slicing:
892:    Allows searching an specific amount of eigenvalues in the subintervals left and right.
893:    Returns whether the search has succeeded
894: */
897: static PetscErrorCode EPSKrylovSchur_Slice(EPS eps)
898: {
899:   PetscErrorCode  ierr;
900:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;
901:   PetscInt        i,conv,k,l,ld,nv,*iwork,j,p;
902:   Mat             U;
903:   PetscScalar     *Q,*A,rtmp;
904:   PetscReal       *a,*b,beta;
905:   PetscBool       breakdown;
906:   PetscInt        count0,count1;
907:   PetscReal       lambda;
908:   EPS_shift       sPres;
909:   PetscBool       complIterating;
910:   PetscBool       sch0,sch1;
911:   PetscInt        iterCompl=0,n0,n1;
912:   EPS_SR          sr = ctx->sr;

915:   /* Spectrum slicing data */
916:   sPres = sr->sPres;
917:   complIterating =PETSC_FALSE;
918:   sch1 = sch0 = PETSC_TRUE;
919:   DSGetLeadingDimension(eps->ds,&ld);
920:   PetscMalloc1(2*ld,&iwork);
921:   count0=0;count1=0; /* Found on both sides */
922:   if (sr->nS > 0 && (sPres->neighb[0] == sr->sPrev || sPres->neighb[1] == sr->sPrev)) {
923:     /* Rational Krylov */
924:     DSTranslateRKS(eps->ds,sr->sPrev->value-sPres->value);
925:     DSGetDimensions(eps->ds,NULL,NULL,NULL,&l,NULL);
926:     DSSetDimensions(eps->ds,l+1,0,0,0);
927:     BVSetActiveColumns(eps->V,0,l+1);
928:     DSGetMat(eps->ds,DS_MAT_Q,&U);
929:     BVMultInPlace(eps->V,U,0,l+1);
930:     MatDestroy(&U);
931:   } else {
932:     /* Get the starting Lanczos vector */
933:     EPSGetStartVector(eps,0,NULL);
934:     l = 0;
935:   }
936:   /* Restart loop */
937:   while (eps->reason == EPS_CONVERGED_ITERATING) {
938:     eps->its++; sr->itsKs++;
939:     /* Compute an nv-step Lanczos factorization */
940:     nv = PetscMin(eps->nconv+eps->mpd,eps->ncv);
941:     DSGetArrayReal(eps->ds,DS_MAT_T,&a);
942:     b = a + ld;
943:     EPSFullLanczos(eps,a,b,eps->nconv+l,&nv,&breakdown);
944:     sr->nv = nv;
945:     beta = b[nv-1];
946:     DSRestoreArrayReal(eps->ds,DS_MAT_T,&a);
947:     DSSetDimensions(eps->ds,nv,0,eps->nconv,eps->nconv+l);
948:     if (l==0) {
949:       DSSetState(eps->ds,DS_STATE_INTERMEDIATE);
950:     } else {
951:       DSSetState(eps->ds,DS_STATE_RAW);
952:     }
953:     BVSetActiveColumns(eps->V,eps->nconv,nv);

955:     /* Solve projected problem and compute residual norm estimates */
956:     if (eps->its == 1 && l > 0) {/* After rational update */
957:       DSGetArray(eps->ds,DS_MAT_A,&A);
958:       DSGetArrayReal(eps->ds,DS_MAT_T,&a);
959:       b = a + ld;
960:       k = eps->nconv+l;
961:       A[k*ld+k-1] = A[(k-1)*ld+k];
962:       A[k*ld+k] = a[k];
963:       for (j=k+1; j< nv; j++) {
964:         A[j*ld+j] = a[j];
965:         A[j*ld+j-1] = b[j-1] ;
966:         A[(j-1)*ld+j] = b[j-1];
967:       }
968:       DSRestoreArray(eps->ds,DS_MAT_A,&A);
969:       DSRestoreArrayReal(eps->ds,DS_MAT_T,&a);
970:       DSSolve(eps->ds,eps->eigr,NULL);
971:       DSSort(eps->ds,eps->eigr,NULL,NULL,NULL,NULL);
972:       DSSetCompact(eps->ds,PETSC_TRUE);
973:     } else { /* Restart */
974:       DSSolve(eps->ds,eps->eigr,NULL);
975:       DSSort(eps->ds,eps->eigr,NULL,NULL,NULL,NULL);
976:     }
977:     /* Residual */
978:     EPSKrylovConvergence(eps,PETSC_TRUE,eps->nconv,nv-eps->nconv,beta,1.0,&k);

980:     if (ctx->lock) {
981:       /* Check convergence */
982:       DSGetArrayReal(eps->ds,DS_MAT_T,&a);
983:       b = a + ld;
984:       conv = 0;
985:       j = k = eps->nconv;
986:       for (i=eps->nconv;i<nv;i++) if (eps->errest[i] < eps->tol) conv++;
987:       for (i=eps->nconv;i<nv;i++) {
988:         if (eps->errest[i] < eps->tol) {
989:           iwork[j++]=i;
990:         } else iwork[conv+k++]=i;
991:       }
992:       for (i=eps->nconv;i<nv;i++) {
993:         a[i]=PetscRealPart(eps->eigr[i]);
994:         b[i]=eps->errest[i];
995:       }
996:       for (i=eps->nconv;i<nv;i++) {
997:         eps->eigr[i] = a[iwork[i]];
998:         eps->errest[i] = b[iwork[i]];
999:       }
1000:       for (i=eps->nconv;i<nv;i++) {
1001:         a[i]=PetscRealPart(eps->eigr[i]);
1002:         b[i]=eps->errest[i];
1003:       }
1004:       DSRestoreArrayReal(eps->ds,DS_MAT_T,&a);
1005:       DSGetArray(eps->ds,DS_MAT_Q,&Q);
1006:       for (i=eps->nconv;i<nv;i++) {
1007:         p=iwork[i];
1008:         if (p!=i) {
1009:           j=i+1;
1010:           while (iwork[j]!=i) j++;
1011:           iwork[j]=p;iwork[i]=i;
1012:           for (k=0;k<nv;k++) {
1013:             rtmp=Q[k+p*ld];Q[k+p*ld]=Q[k+i*ld];Q[k+i*ld]=rtmp;
1014:           }
1015:         }
1016:       }
1017:       DSRestoreArray(eps->ds,DS_MAT_Q,&Q);
1018:       k=eps->nconv+conv;
1019:     }

1021:     /* Checking values obtained for completing */
1022:     for (i=0;i<k;i++) {
1023:       sr->back[i]=eps->eigr[i];
1024:     }
1025:     STBackTransform(eps->st,k,sr->back,eps->eigi);
1026:     count0=count1=0;
1027:     for (i=0;i<k;i++) {
1028:       lambda = PetscRealPart(sr->back[i]);
1029:       if (((sr->dir)*(sPres->value - lambda) > 0) && ((sr->dir)*(lambda - sPres->ext[0]) > 0)) count0++;
1030:       if (((sr->dir)*(lambda - sPres->value) > 0) && ((sr->dir)*(sPres->ext[1] - lambda) > 0)) count1++;
1031:     }
1032:     if (k>eps->nev && eps->ncv-k<5) eps->reason = EPS_CONVERGED_TOL;
1033:     else {
1034:       /* Checks completion */
1035:       if ((!sch0||count0 >= sPres->nsch[0]) && (!sch1 ||count1 >= sPres->nsch[1])) {
1036:         eps->reason = EPS_CONVERGED_TOL;
1037:       } else {
1038:         if (!complIterating && eps->its >= eps->max_it) eps->reason = EPS_DIVERGED_ITS;
1039:         if (complIterating) {
1040:           if (--iterCompl <= 0) eps->reason = EPS_DIVERGED_ITS;
1041:         } else if (k >= eps->nev) {
1042:           n0 = sPres->nsch[0]-count0;
1043:           n1 = sPres->nsch[1]-count1;
1044:           if (sr->iterCompl>0 && ((n0>0 && n0<= sr->nMAXCompl)||(n1>0&&n1<=sr->nMAXCompl))) {
1045:             /* Iterating for completion*/
1046:             complIterating = PETSC_TRUE;
1047:             if (n0 >sr->nMAXCompl)sch0 = PETSC_FALSE;
1048:             if (n1 >sr->nMAXCompl)sch1 = PETSC_FALSE;
1049:             iterCompl = sr->iterCompl;
1050:           } else eps->reason = EPS_CONVERGED_TOL;
1051:         }
1052:       }
1053:     }
1054:     /* Update l */
1055:     if (eps->reason == EPS_CONVERGED_ITERATING) l = PetscMax(1,(PetscInt)((nv-k)*ctx->keep));
1056:     else l = 0;
1057:     if (!ctx->lock && l>0) { l += k; k = 0; } /* non-locking variant: reset no. of converged pairs */
1058:     if (breakdown) l=0;

1060:     if (eps->reason == EPS_CONVERGED_ITERATING) {
1061:       if (breakdown) {
1062:         /* Start a new Lanczos factorization */
1063:         PetscInfo2(eps,"Breakdown in Krylov-Schur method (it=%D norm=%g)\n",eps->its,(double)beta);
1064:         EPSGetStartVector(eps,k,&breakdown);
1065:         if (breakdown) {
1066:           eps->reason = EPS_DIVERGED_BREAKDOWN;
1067:           PetscInfo(eps,"Unable to generate more start vectors\n");
1068:         }
1069:       } else {
1070:         /* Prepare the Rayleigh quotient for restart */
1071:         DSGetArrayReal(eps->ds,DS_MAT_T,&a);
1072:         DSGetArray(eps->ds,DS_MAT_Q,&Q);
1073:         b = a + ld;
1074:         for (i=k;i<k+l;i++) {
1075:           a[i] = PetscRealPart(eps->eigr[i]);
1076:           b[i] = PetscRealPart(Q[nv-1+i*ld]*beta);
1077:         }
1078:         DSRestoreArrayReal(eps->ds,DS_MAT_T,&a);
1079:         DSRestoreArray(eps->ds,DS_MAT_Q,&Q);
1080:       }
1081:     }
1082:     /* Update the corresponding vectors V(:,idx) = V*Q(:,idx) */
1083:     DSGetMat(eps->ds,DS_MAT_Q,&U);
1084:     BVMultInPlace(eps->V,U,eps->nconv,k+l);
1085:     MatDestroy(&U);

1087:     /* Normalize u and append it to V */
1088:     if (eps->reason == EPS_CONVERGED_ITERATING && !breakdown) {
1089:       BVCopyColumn(eps->V,nv,k+l);
1090:     }
1091:     eps->nconv = k;
1092:     if (eps->reason != EPS_CONVERGED_ITERATING) {
1093:       /* Store approximated values for next shift */
1094:       DSGetArray(eps->ds,DS_MAT_Q,&Q);
1095:       sr->nS = l;
1096:       for (i=0;i<l;i++) {
1097:         sr->S[i] = eps->eigr[i+k];/* Diagonal elements */
1098:         sr->S[i+l] = Q[nv-1+(i+k)*ld]*beta; /* Out of diagonal elements */
1099:       }
1100:       DSRestoreArray(eps->ds,DS_MAT_Q,&Q);
1101:     }
1102:   }
1103:   /* Check for completion */
1104:   for (i=0;i< eps->nconv; i++) {
1105:     if ((sr->dir)*PetscRealPart(eps->eigr[i])>0) sPres->nconv[1]++;
1106:     else sPres->nconv[0]++;
1107:   }
1108:   sPres->comp[0] = PetscNot(count0 < sPres->nsch[0]);
1109:   sPres->comp[1] = PetscNot(count1 < sPres->nsch[1]);
1110:   if (count0 > sPres->nsch[0] || count1 > sPres->nsch[1])SETERRQ(PetscObjectComm((PetscObject)eps),1,"Mismatch between number of values found and information from inertia, consider using EPSKrylovSchurSetDetectZeros()");
1111:   PetscFree(iwork);
1112:   return(0);
1113: }

1115: /*
1116:   Obtains value of subsequent shift
1117: */
1120: static PetscErrorCode EPSGetNewShiftValue(EPS eps,PetscInt side,PetscReal *newS)
1121: {
1122:   PetscReal       lambda,d_prev;
1123:   PetscInt        i,idxP;
1124:   EPS_SR          sr;
1125:   EPS_shift       sPres,s;
1126:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;

1129:   sr = ctx->sr;
1130:   sPres = sr->sPres;
1131:   if (sPres->neighb[side]) {
1132:   /* Completing a previous interval */
1133:     if (!sPres->neighb[side]->neighb[side] && sPres->neighb[side]->nconv[side]==0) { /* One of the ends might be too far from eigenvalues */
1134:       if (side) *newS = (sPres->value + PetscRealPart(sr->eigr[sr->perm[sr->indexEig-1]]))/2;
1135:       else *newS = (sPres->value + PetscRealPart(sr->eigr[sr->perm[0]]))/2;
1136:     } else *newS=(sPres->value + sPres->neighb[side]->value)/2;
1137:   } else { /* (Only for side=1). Creating a new interval. */
1138:     if (sPres->neigs==0) {/* No value has been accepted*/
1139:       if (sPres->neighb[0]) {
1140:         /* Multiplying by 10 the previous distance */
1141:         *newS = sPres->value + 10*(sr->dir)*PetscAbsReal(sPres->value - sPres->neighb[0]->value);
1142:         sr->nleap++;
1143:         /* Stops when the interval is open and no values are found in the last 5 shifts (there might be infinite eigenvalues) */
1144:         if (!sr->hasEnd && sr->nleap > 5) SETERRQ(PetscObjectComm((PetscObject)eps),1,"Unable to compute the wanted eigenvalues with open interval");
1145:       } else { /* First shift */
1146:         if (eps->nconv != 0) {
1147:           /* Unaccepted values give information for next shift */
1148:           idxP=0;/* Number of values left from shift */
1149:           for (i=0;i<eps->nconv;i++) {
1150:             lambda = PetscRealPart(sr->eigr[i]);
1151:             if ((sr->dir)*(lambda - sPres->value) <0) idxP++;
1152:             else break;
1153:           }
1154:           /* Avoiding subtraction of eigenvalues (might be the same).*/
1155:           if (idxP>0) {
1156:             d_prev = PetscAbsReal(sPres->value - PetscRealPart(sr->eigr[0]))/(idxP+0.3);
1157:           } else {
1158:             d_prev = PetscAbsReal(sPres->value - PetscRealPart(sr->eigr[eps->nconv-1]))/(eps->nconv+0.3);
1159:           }
1160:           *newS = sPres->value + ((sr->dir)*d_prev*eps->nev)/2;
1161:         } else { /* No values found, no information for next shift */
1162:           SETERRQ(PetscObjectComm((PetscObject)eps),1,"First shift renders no information");
1163:         }
1164:       }
1165:     } else { /* Accepted values found */
1166:       sr->nleap = 0;
1167:       /* Average distance of values in previous subinterval */
1168:       s = sPres->neighb[0];
1169:       while (s && PetscAbs(s->inertia - sPres->inertia)==0) {
1170:         s = s->neighb[0];/* Looking for previous shifts with eigenvalues within */
1171:       }
1172:       if (s) {
1173:         d_prev = PetscAbsReal((sPres->value - s->value)/(sPres->inertia - s->inertia));
1174:       } else { /* First shift. Average distance obtained with values in this shift */
1175:         /* first shift might be too far from first wanted eigenvalue (no values found outside the interval)*/
1176:         if ((sr->dir)*(PetscRealPart(sr->eigr[0])-sPres->value)>0 && PetscAbsReal((PetscRealPart(sr->eigr[sr->indexEig-1]) - PetscRealPart(sr->eigr[0]))/PetscRealPart(sr->eigr[0])) > PetscSqrtReal(eps->tol)) {
1177:           d_prev =  PetscAbsReal((PetscRealPart(sr->eigr[sr->indexEig-1]) - PetscRealPart(sr->eigr[0])))/(sPres->neigs+0.3);
1178:         } else {
1179:           d_prev = PetscAbsReal(PetscRealPart(sr->eigr[sr->indexEig-1]) - sPres->value)/(sPres->neigs+0.3);
1180:         }
1181:       }
1182:       /* Average distance is used for next shift by adding it to value on the right or to shift */
1183:       if ((sr->dir)*(PetscRealPart(sr->eigr[sPres->index + sPres->neigs -1]) - sPres->value)>0) {
1184:         *newS = PetscRealPart(sr->eigr[sPres->index + sPres->neigs -1])+ ((sr->dir)*d_prev*(eps->nev))/2;
1185:       } else { /* Last accepted value is on the left of shift. Adding to shift */
1186:         *newS = sPres->value + ((sr->dir)*d_prev*(eps->nev))/2;
1187:       }
1188:     }
1189:     /* End of interval can not be surpassed */
1190:     if ((sr->dir)*(sr->int1 - *newS) < 0) *newS = sr->int1;
1191:   }/* of neighb[side]==null */
1192:   return(0);
1193: }

1195: /*
1196:   Function for sorting an array of real values
1197: */
1200: static PetscErrorCode sortRealEigenvalues(PetscScalar *r,PetscInt *perm,PetscInt nr,PetscBool prev,PetscInt dir)
1201: {
1202:   PetscReal      re;
1203:   PetscInt       i,j,tmp;

1206:   if (!prev) for (i=0;i<nr;i++) perm[i] = i;
1207:   /* Insertion sort */
1208:   for (i=1;i<nr;i++) {
1209:     re = PetscRealPart(r[perm[i]]);
1210:     j = i-1;
1211:     while (j>=0 && dir*(re - PetscRealPart(r[perm[j]])) <= 0) {
1212:       tmp = perm[j]; perm[j] = perm[j+1]; perm[j+1] = tmp; j--;
1213:     }
1214:   }
1215:   return(0);
1216: }

1218: /* Stores the pairs obtained since the last shift in the global arrays */
1221: static PetscErrorCode EPSStoreEigenpairs(EPS eps)
1222: {
1223:   PetscErrorCode  ierr;
1224:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;
1225:   PetscReal       lambda,err,norm;
1226:   PetscInt        i,count;
1227:   PetscBool       iscayley;
1228:   EPS_SR          sr = ctx->sr;
1229:   EPS_shift       sPres;
1230:   Vec             v,w;

1233:   sPres = sr->sPres;
1234:   sPres->index = sr->indexEig;
1235:   count = sr->indexEig;
1236:   /* Back-transform */
1237:   STBackTransform(eps->st,eps->nconv,eps->eigr,eps->eigi);
1238:   PetscObjectTypeCompare((PetscObject)eps->st,STCAYLEY,&iscayley);
1239:   /* Sort eigenvalues */
1240:   sortRealEigenvalues(eps->eigr,eps->perm,eps->nconv,PETSC_FALSE,sr->dir);
1241:   /* Values stored in global array */
1242:   for (i=0;i<eps->nconv;i++) {
1243:     lambda = PetscRealPart(eps->eigr[eps->perm[i]]);
1244:     err = eps->errest[eps->perm[i]];

1246:     if ((sr->dir)*(lambda - sPres->ext[0]) > 0 && (sr->dir)*(sPres->ext[1] - lambda) > 0) {/* Valid value */
1247:       if (count>=sr->numEigs) SETERRQ(PetscObjectComm((PetscObject)eps),1,"Unexpected error in Spectrum Slicing");
1248:       sr->eigr[count] = lambda;
1249:       sr->errest[count] = err;
1250:       /* Explicit purification */
1251:       if (eps->purify) {
1252:         BVGetColumn(sr->V,count,&v);
1253:         BVGetColumn(eps->V,eps->perm[i],&w);
1254:         STApply(eps->st,w,v);
1255:         BVRestoreColumn(sr->V,count,&v);
1256:         BVRestoreColumn(eps->V,eps->perm[i],&w);
1257:         BVNormColumn(sr->V,count,NORM_2,&norm);
1258:         BVScaleColumn(sr->V,count,1.0/norm);
1259:       } else {
1260:         BVGetColumn(eps->V,eps->perm[i],&w);
1261:         BVInsertVec(sr->V,count,w);
1262:         BVRestoreColumn(eps->V,eps->perm[i],&w);
1263:         BVNormColumn(sr->V,count,NORM_2,&norm);
1264:         BVScaleColumn(sr->V,count,1.0/norm);
1265:       }
1266:       count++;
1267:     }
1268:   }
1269:   sPres->neigs = count - sr->indexEig;
1270:   sr->indexEig = count;
1271:   /* Global ordering array updating */
1272:   sortRealEigenvalues(sr->eigr,sr->perm,count,PETSC_TRUE,sr->dir);
1273:   return(0);
1274: }

1278: static PetscErrorCode EPSLookForDeflation(EPS eps)
1279: {
1280:   PetscErrorCode  ierr;
1281:   PetscReal       val;
1282:   PetscInt        i,count0=0,count1=0;
1283:   EPS_shift       sPres;
1284:   PetscInt        ini,fin,k,idx0,idx1;
1285:   EPS_SR          sr;
1286:   Vec             v;
1287:   EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;

1290:   sr = ctx->sr;
1291:   sPres = sr->sPres;

1293:   if (sPres->neighb[0]) ini = (sr->dir)*(sPres->neighb[0]->inertia - sr->inertia0);
1294:   else ini = 0;
1295:   fin = sr->indexEig;
1296:   /* Selection of ends for searching new values */
1297:   if (!sPres->neighb[0]) sPres->ext[0] = sr->int0;/* First shift */
1298:   else sPres->ext[0] = sPres->neighb[0]->value;
1299:   if (!sPres->neighb[1]) {
1300:     if (sr->hasEnd) sPres->ext[1] = sr->int1;
1301:     else sPres->ext[1] = (sr->dir > 0)?PETSC_MAX_REAL:PETSC_MIN_REAL;
1302:   } else sPres->ext[1] = sPres->neighb[1]->value;
1303:   /* Selection of values between right and left ends */
1304:   for (i=ini;i<fin;i++) {
1305:     val=PetscRealPart(sr->eigr[sr->perm[i]]);
1306:     /* Values to the right of left shift */
1307:     if ((sr->dir)*(val - sPres->ext[1]) < 0) {
1308:       if ((sr->dir)*(val - sPres->value) < 0) count0++;
1309:       else count1++;
1310:     } else break;
1311:   }
1312:   /* The number of values on each side are found */
1313:   if (sPres->neighb[0]) {
1314:     sPres->nsch[0] = (sr->dir)*(sPres->inertia - sPres->neighb[0]->inertia)-count0;
1315:     if (sPres->nsch[0]<0)SETERRQ(PetscObjectComm((PetscObject)eps),1,"Mismatch between number of values found and information from inertia, consider using EPSKrylovSchurSetDetectZeros()");
1316:   } else sPres->nsch[0] = 0;

1318:   if (sPres->neighb[1]) {
1319:     sPres->nsch[1] = (sr->dir)*(sPres->neighb[1]->inertia - sPres->inertia) - count1;
1320:     if (sPres->nsch[1]<0)SETERRQ(PetscObjectComm((PetscObject)eps),1,"Mismatch between number of values found and information from inertia, consider using EPSKrylovSchurSetDetectZeros()");
1321:   } else sPres->nsch[1] = (sr->dir)*(sr->inertia1 - sPres->inertia);

1323:   /* Completing vector of indexes for deflation */
1324:   idx0 = ini;
1325:   idx1 = ini+count0+count1;
1326:   k=0;
1327:   for (i=idx0;i<idx1;i++) sr->idxDef[k++]=sr->perm[i];
1328:   BVDuplicateResize(eps->V,k+eps->ncv+1,&sr->Vnext);
1329:   BVSetNumConstraints(sr->Vnext,k);
1330:   for (i=0;i<k;i++) {
1331:     BVGetColumn(sr->Vnext,-i-1,&v);
1332:     BVCopyVec(sr->V,sr->idxDef[i],v);
1333:     BVRestoreColumn(sr->Vnext,-i-1,&v);
1334:   }

1336:   /* For rational Krylov */
1337:   if (sr->nS>0 && (sr->sPrev == sr->sPres->neighb[0] || sr->sPrev == sr->sPres->neighb[1])) {
1338:     EPSPrepareRational(eps);
1339:   }
1340:   eps->nconv = 0;
1341:   /* Get rid of temporary Vnext */
1342:   BVDestroy(&eps->V);
1343:   eps->V = sr->Vnext;
1344:   sr->Vnext = NULL;
1345:   return(0);
1346: }

1350: PetscErrorCode EPSSolve_KrylovSchur_Slice(EPS eps)
1351: {
1352:   PetscErrorCode   ierr;
1353:   PetscInt         i,lds;
1354:   PetscReal        newS;
1355:   EPS_KRYLOVSCHUR  *ctx=(EPS_KRYLOVSCHUR*)eps->data;
1356:   EPS_SR           sr=ctx->sr;
1357:   Mat              A,B=NULL;
1358:   PetscObjectState Astate,Bstate=0;
1359:   PetscObjectId    Aid,Bid=0;

1362:   PetscCitationsRegister(citation,&cited);
1363:   if (ctx->global) {
1364:     EPSSolve_KrylovSchur_Slice(ctx->eps);
1365:     ctx->eps->state = EPS_STATE_SOLVED;
1366:     eps->reason = EPS_CONVERGED_TOL;
1367:     if (ctx->npart>1) {
1368:       /* Gather solution from subsolvers */
1369:       EPSSliceGatherSolution(eps);
1370:     } else {
1371:       eps->nconv = sr->numEigs;
1372:       eps->its   = ctx->eps->its;
1373:       PetscFree(ctx->inertias);
1374:       PetscFree(ctx->shifts);
1375:       EPSSliceGetInertias(ctx->eps,&ctx->nshifts,&ctx->shifts,&ctx->inertias);
1376:     }
1377:   } else {
1378:     if (ctx->npart==1) {
1379:       sr->eigr   = ctx->eps->eigr;
1380:       sr->eigi   = ctx->eps->eigi;
1381:       sr->perm   = ctx->eps->perm;
1382:       sr->errest = ctx->eps->errest;
1383:       sr->V      = ctx->eps->V;
1384:     }
1385:     /* Check that the user did not modify subcomm matrices */
1386:     EPSGetOperators(eps,&A,&B);
1387:     PetscObjectStateGet((PetscObject)A,&Astate);
1388:     PetscObjectGetId((PetscObject)A,&Aid);
1389:     if (B) { 
1390:       PetscObjectStateGet((PetscObject)B,&Bstate);
1391:       PetscObjectGetId((PetscObject)B,&Bid);
1392:     }
1393:     if (Astate!=ctx->Astate || (B && Bstate!=ctx->Bstate) || Aid!=ctx->Aid || (B && Bid!=ctx->Bid)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Subcomm matrices have been modified by user");
1394:     /* Only with eigenvalues present in the interval ...*/
1395:     if (sr->numEigs==0) {
1396:       eps->reason = EPS_CONVERGED_TOL;
1397:       return(0);
1398:     }
1399:     /* Array of pending shifts */
1400:     sr->maxPend = 100; /* Initial size */
1401:     sr->nPend = 0;
1402:     PetscMalloc1(sr->maxPend,&sr->pending);
1403:     PetscLogObjectMemory((PetscObject)eps,(sr->maxPend)*sizeof(EPS_shift));
1404:     EPSCreateShift(eps,sr->int0,NULL,NULL);
1405:     /* extract first shift */
1406:     sr->sPrev = NULL;
1407:     sr->sPres = sr->pending[--sr->nPend];
1408:     sr->sPres->inertia = sr->inertia0;
1409:     eps->target = sr->sPres->value;
1410:     sr->s0 = sr->sPres;
1411:     sr->indexEig = 0;
1412:     /* Memory reservation for auxiliary variables */
1413:     lds = PetscMin(eps->mpd,eps->ncv);
1414:     PetscCalloc1(lds*lds,&sr->S);
1415:     PetscMalloc1(eps->ncv,&sr->back);
1416:     PetscLogObjectMemory((PetscObject)eps,(sr->numEigs+2*eps->ncv)*sizeof(PetscScalar));
1417:     for (i=0;i<sr->numEigs;i++) {
1418:       sr->eigr[i]   = 0.0;
1419:       sr->eigi[i]   = 0.0;
1420:       sr->errest[i] = 0.0;
1421:       sr->perm[i]   = i;
1422:     }
1423:     /* Vectors for deflation */
1424:     PetscMalloc1(sr->numEigs,&sr->idxDef);
1425:     PetscLogObjectMemory((PetscObject)eps,sr->numEigs*sizeof(PetscInt));
1426:     sr->indexEig = 0;
1427:     /* Main loop */
1428:     while (sr->sPres) {
1429:       /* Search for deflation */
1430:       EPSLookForDeflation(eps);
1431:       /* KrylovSchur */
1432:       EPSKrylovSchur_Slice(eps);

1434:       EPSStoreEigenpairs(eps);
1435:       /* Select new shift */
1436:       if (!sr->sPres->comp[1]) {
1437:         EPSGetNewShiftValue(eps,1,&newS);
1438:         EPSCreateShift(eps,newS,sr->sPres,sr->sPres->neighb[1]);
1439:       }
1440:       if (!sr->sPres->comp[0]) {
1441:         /* Completing earlier interval */
1442:         EPSGetNewShiftValue(eps,0,&newS);
1443:         EPSCreateShift(eps,newS,sr->sPres->neighb[0],sr->sPres);
1444:       }
1445:       /* Preparing for a new search of values */
1446:       EPSExtractShift(eps);
1447:     }

1449:     /* Updating eps values prior to exit */
1450:     PetscFree(sr->S);
1451:     PetscFree(sr->idxDef);
1452:     PetscFree(sr->pending);
1453:     PetscFree(sr->back);
1454:     BVDuplicateResize(eps->V,eps->ncv+1,&sr->Vnext);
1455:     BVSetNumConstraints(sr->Vnext,0);
1456:     BVDestroy(&eps->V);
1457:     eps->V      = sr->Vnext;
1458:     eps->nconv  = sr->indexEig;
1459:     eps->reason = EPS_CONVERGED_TOL;
1460:     eps->its    = sr->itsKs;
1461:     eps->nds    = 0;
1462:   }
1463:   return(0);
1464: }