Actual source code: krylovschur.c
slepc-3.17.1 2022-04-11
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
7: SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9: */
10: /*
11: SLEPc eigensolver: "krylovschur"
13: Method: Krylov-Schur
15: Algorithm:
17: Single-vector Krylov-Schur method for non-symmetric problems,
18: including harmonic extraction.
20: References:
22: [1] "Krylov-Schur Methods in SLEPc", SLEPc Technical Report STR-7,
23: available at https://slepc.upv.es.
25: [2] G.W. Stewart, "A Krylov-Schur Algorithm for Large Eigenproblems",
26: SIAM J. Matrix Anal. App. 23(3):601-614, 2001.
28: [3] "Practical Implementation of Harmonic Krylov-Schur", SLEPc Technical
29: Report STR-9, available at https://slepc.upv.es.
30: */
32: #include <slepc/private/epsimpl.h>
33: #include "krylovschur.h"
35: PetscErrorCode EPSGetArbitraryValues(EPS eps,PetscScalar *rr,PetscScalar *ri)
36: {
37: PetscInt i,newi,ld,n,l;
38: Vec xr=eps->work[0],xi=eps->work[1];
39: PetscScalar re,im,*Zr,*Zi,*X;
41: DSGetLeadingDimension(eps->ds,&ld);
42: DSGetDimensions(eps->ds,&n,&l,NULL,NULL);
43: for (i=l;i<n;i++) {
44: re = eps->eigr[i];
45: im = eps->eigi[i];
46: STBackTransform(eps->st,1,&re,&im);
47: newi = i;
48: DSVectors(eps->ds,DS_MAT_X,&newi,NULL);
49: DSGetArray(eps->ds,DS_MAT_X,&X);
50: Zr = X+i*ld;
51: if (newi==i+1) Zi = X+newi*ld;
52: else Zi = NULL;
53: EPSComputeRitzVector(eps,Zr,Zi,eps->V,xr,xi);
54: DSRestoreArray(eps->ds,DS_MAT_X,&X);
55: (*eps->arbitrary)(re,im,xr,xi,rr+i,ri+i,eps->arbitraryctx);
56: }
57: PetscFunctionReturn(0);
58: }
60: static PetscErrorCode EPSSetUp_KrylovSchur_Filter(EPS eps)
61: {
62: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
63: PetscBool estimaterange=PETSC_TRUE;
64: PetscReal rleft,rright;
65: Mat A;
67: EPSCheckHermitianCondition(eps,PETSC_TRUE," with polynomial filter");
68: EPSCheckStandardCondition(eps,PETSC_TRUE," with polynomial filter");
70: EPSCheckUnsupportedCondition(eps,EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_EXTRACTION,PETSC_TRUE," with polynomial filter");
71: if (eps->tol==PETSC_DEFAULT) eps->tol = SLEPC_DEFAULT_TOL*1e-2; /* use tighter tolerance */
72: STFilterSetInterval(eps->st,eps->inta,eps->intb);
73: if (!ctx->estimatedrange) {
74: STFilterGetRange(eps->st,&rleft,&rright);
75: estimaterange = (!rleft && !rright)? PETSC_TRUE: PETSC_FALSE;
76: }
77: if (estimaterange) { /* user did not set a range */
78: STGetMatrix(eps->st,0,&A);
79: MatEstimateSpectralRange_EPS(A,&rleft,&rright);
80: PetscInfo(eps,"Setting eigenvalue range to [%g,%g]\n",(double)rleft,(double)rright);
81: STFilterSetRange(eps->st,rleft,rright);
82: ctx->estimatedrange = PETSC_TRUE;
83: }
84: if (eps->ncv==PETSC_DEFAULT && eps->nev==1) eps->nev = 40; /* user did not provide nev estimation */
85: EPSSetDimensions_Default(eps,eps->nev,&eps->ncv,&eps->mpd);
87: if (eps->max_it==PETSC_DEFAULT) eps->max_it = PetscMax(100,2*eps->n/eps->ncv);
88: PetscFunctionReturn(0);
89: }
91: PetscErrorCode EPSSetUp_KrylovSchur(EPS eps)
92: {
93: PetscReal eta;
94: PetscBool isfilt=PETSC_FALSE;
95: BVOrthogType otype;
96: BVOrthogBlockType obtype;
97: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
98: enum { EPS_KS_DEFAULT,EPS_KS_SYMM,EPS_KS_SLICE,EPS_KS_FILTER,EPS_KS_INDEF,EPS_KS_TWOSIDED } variant;
100: if (eps->which==EPS_ALL) { /* default values in case of spectrum slicing or polynomial filter */
101: PetscObjectTypeCompare((PetscObject)eps->st,STFILTER,&isfilt);
102: if (isfilt) EPSSetUp_KrylovSchur_Filter(eps);
103: else EPSSetUp_KrylovSchur_Slice(eps);
104: } else {
105: EPSSetDimensions_Default(eps,eps->nev,&eps->ncv,&eps->mpd);
107: if (eps->max_it==PETSC_DEFAULT) eps->max_it = PetscMax(100,2*eps->n/eps->ncv);
108: if (!eps->which) EPSSetWhichEigenpairs_Default(eps);
109: }
112: EPSCheckDefiniteCondition(eps,eps->arbitrary," with arbitrary selection of eigenpairs");
116: if (!ctx->keep) ctx->keep = 0.5;
118: EPSAllocateSolution(eps,1);
119: EPS_SetInnerProduct(eps);
120: if (eps->arbitrary) EPSSetWorkVecs(eps,2);
121: else if (eps->ishermitian && !eps->ispositive) EPSSetWorkVecs(eps,1);
123: /* dispatch solve method */
124: if (eps->ishermitian) {
125: if (eps->which==EPS_ALL) {
127: else variant = isfilt? EPS_KS_FILTER: EPS_KS_SLICE;
128: } else if (eps->isgeneralized && !eps->ispositive) {
129: variant = EPS_KS_INDEF;
130: } else {
131: switch (eps->extraction) {
132: case EPS_RITZ: variant = EPS_KS_SYMM; break;
133: case EPS_HARMONIC: variant = EPS_KS_DEFAULT; break;
134: default: SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Unsupported extraction type");
135: }
136: }
137: } else if (eps->twosided) {
138: variant = EPS_KS_TWOSIDED;
139: } else {
140: switch (eps->extraction) {
141: case EPS_RITZ: variant = EPS_KS_DEFAULT; break;
142: case EPS_HARMONIC: variant = EPS_KS_DEFAULT; break;
143: default: SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Unsupported extraction type");
144: }
145: }
146: switch (variant) {
147: case EPS_KS_DEFAULT:
148: eps->ops->solve = EPSSolve_KrylovSchur_Default;
149: eps->ops->computevectors = EPSComputeVectors_Schur;
150: DSSetType(eps->ds,DSNHEP);
151: DSSetExtraRow(eps->ds,PETSC_TRUE);
152: DSAllocate(eps->ds,eps->ncv+1);
153: break;
154: case EPS_KS_SYMM:
155: case EPS_KS_FILTER:
156: eps->ops->solve = EPSSolve_KrylovSchur_Default;
157: eps->ops->computevectors = EPSComputeVectors_Hermitian;
158: DSSetType(eps->ds,DSHEP);
159: DSSetCompact(eps->ds,PETSC_TRUE);
160: DSSetExtraRow(eps->ds,PETSC_TRUE);
161: DSAllocate(eps->ds,eps->ncv+1);
162: break;
163: case EPS_KS_SLICE:
164: eps->ops->solve = EPSSolve_KrylovSchur_Slice;
165: eps->ops->computevectors = EPSComputeVectors_Slice;
166: break;
167: case EPS_KS_INDEF:
168: eps->ops->solve = EPSSolve_KrylovSchur_Indefinite;
169: eps->ops->computevectors = EPSComputeVectors_Indefinite;
170: DSSetType(eps->ds,DSGHIEP);
171: DSSetCompact(eps->ds,PETSC_TRUE);
172: DSSetExtraRow(eps->ds,PETSC_TRUE);
173: DSAllocate(eps->ds,eps->ncv+1);
174: /* force reorthogonalization for pseudo-Lanczos */
175: BVGetOrthogonalization(eps->V,&otype,NULL,&eta,&obtype);
176: BVSetOrthogonalization(eps->V,otype,BV_ORTHOG_REFINE_ALWAYS,eta,obtype);
177: break;
178: case EPS_KS_TWOSIDED:
179: eps->ops->solve = EPSSolve_KrylovSchur_TwoSided;
180: eps->ops->computevectors = EPSComputeVectors_Schur;
181: DSSetType(eps->ds,DSNHEPTS);
182: DSAllocate(eps->ds,eps->ncv+1);
183: DSSetExtraRow(eps->ds,PETSC_TRUE);
184: break;
185: default: SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_PLIB,"Unexpected error");
186: }
187: PetscFunctionReturn(0);
188: }
190: PetscErrorCode EPSSetUpSort_KrylovSchur(EPS eps)
191: {
192: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
193: SlepcSC sc;
194: PetscBool isfilt;
196: EPSSetUpSort_Default(eps);
197: if (eps->which==EPS_ALL) {
198: PetscObjectTypeCompare((PetscObject)eps->st,STFILTER,&isfilt);
199: if (isfilt) {
200: DSGetSlepcSC(eps->ds,&sc);
201: sc->rg = NULL;
202: sc->comparison = SlepcCompareLargestReal;
203: sc->comparisonctx = NULL;
204: sc->map = NULL;
205: sc->mapobj = NULL;
206: } else {
207: if (!ctx->global && ctx->sr->numEigs>0) {
208: DSGetSlepcSC(eps->ds,&sc);
209: sc->rg = NULL;
210: sc->comparison = SlepcCompareLargestMagnitude;
211: sc->comparisonctx = NULL;
212: sc->map = NULL;
213: sc->mapobj = NULL;
214: }
215: }
216: }
217: PetscFunctionReturn(0);
218: }
220: PetscErrorCode EPSSolve_KrylovSchur_Default(EPS eps)
221: {
222: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
223: PetscInt j,*pj,k,l,nv,ld,nconv;
224: Mat U,Op,H;
225: PetscScalar *g;
226: PetscReal beta,gamma=1.0,*a,*b;
227: PetscBool breakdown,harmonic,hermitian;
229: DSGetLeadingDimension(eps->ds,&ld);
230: harmonic = (eps->extraction==EPS_HARMONIC || eps->extraction==EPS_REFINED_HARMONIC)?PETSC_TRUE:PETSC_FALSE;
231: hermitian = (eps->ishermitian && !harmonic)?PETSC_TRUE:PETSC_FALSE;
232: if (harmonic) PetscMalloc1(ld,&g);
233: if (eps->arbitrary) pj = &j;
234: else pj = NULL;
236: /* Get the starting Arnoldi vector */
237: EPSGetStartVector(eps,0,NULL);
238: l = 0;
240: /* Restart loop */
241: while (eps->reason == EPS_CONVERGED_ITERATING) {
242: eps->its++;
244: /* Compute an nv-step Arnoldi factorization */
245: nv = PetscMin(eps->nconv+eps->mpd,eps->ncv);
246: DSSetDimensions(eps->ds,nv,eps->nconv,eps->nconv+l);
247: STGetOperator(eps->st,&Op);
248: if (hermitian) {
249: DSGetArrayReal(eps->ds,DS_MAT_T,&a);
250: b = a + ld;
251: BVMatLanczos(eps->V,Op,a,b,eps->nconv+l,&nv,&breakdown);
252: beta = b[nv-1];
253: DSRestoreArrayReal(eps->ds,DS_MAT_T,&a);
254: } else {
255: DSGetMat(eps->ds,DS_MAT_A,&H);
256: BVMatArnoldi(eps->V,Op,H,eps->nconv+l,&nv,&beta,&breakdown);
257: DSRestoreMat(eps->ds,DS_MAT_A,&H);
258: }
259: STRestoreOperator(eps->st,&Op);
260: DSSetDimensions(eps->ds,nv,eps->nconv,eps->nconv+l);
261: DSSetState(eps->ds,l?DS_STATE_RAW:DS_STATE_INTERMEDIATE);
262: BVSetActiveColumns(eps->V,eps->nconv,nv);
264: /* Compute translation of Krylov decomposition if harmonic extraction used */
265: if (PetscUnlikely(harmonic)) DSTranslateHarmonic(eps->ds,eps->target,beta,PETSC_FALSE,g,&gamma);
267: /* Solve projected problem */
268: DSSolve(eps->ds,eps->eigr,eps->eigi);
269: if (PetscUnlikely(eps->arbitrary)) {
270: EPSGetArbitraryValues(eps,eps->rr,eps->ri);
271: j=1;
272: }
273: DSSort(eps->ds,eps->eigr,eps->eigi,eps->rr,eps->ri,pj);
274: DSUpdateExtraRow(eps->ds);
275: DSSynchronize(eps->ds,eps->eigr,eps->eigi);
277: /* Check convergence */
278: EPSKrylovConvergence(eps,PETSC_FALSE,eps->nconv,nv-eps->nconv,beta,0.0,gamma,&k);
279: (*eps->stopping)(eps,eps->its,eps->max_it,k,eps->nev,&eps->reason,eps->stoppingctx);
280: nconv = k;
282: /* Update l */
283: if (eps->reason != EPS_CONVERGED_ITERATING || breakdown || k==nv) l = 0;
284: else {
285: l = PetscMax(1,(PetscInt)((nv-k)*ctx->keep));
286: if (!hermitian) DSGetTruncateSize(eps->ds,k,nv,&l);
287: }
288: if (!ctx->lock && l>0) { l += k; k = 0; } /* non-locking variant: reset no. of converged pairs */
289: if (l) PetscInfo(eps,"Preparing to restart keeping l=%" PetscInt_FMT " vectors\n",l);
291: if (eps->reason == EPS_CONVERGED_ITERATING) {
292: if (PetscUnlikely(breakdown || k==nv)) {
293: /* Start a new Arnoldi factorization */
294: PetscInfo(eps,"Breakdown in Krylov-Schur method (it=%" PetscInt_FMT " norm=%g)\n",eps->its,(double)beta);
295: if (k<eps->nev) {
296: EPSGetStartVector(eps,k,&breakdown);
297: if (breakdown) {
298: eps->reason = EPS_DIVERGED_BREAKDOWN;
299: PetscInfo(eps,"Unable to generate more start vectors\n");
300: }
301: }
302: } else {
303: /* Undo translation of Krylov decomposition */
304: if (PetscUnlikely(harmonic)) {
305: DSSetDimensions(eps->ds,nv,k,l);
306: DSTranslateHarmonic(eps->ds,0.0,beta,PETSC_TRUE,g,&gamma);
307: /* gamma u^ = u - U*g~ */
308: BVSetActiveColumns(eps->V,0,nv);
309: BVMultColumn(eps->V,-1.0,1.0,nv,g);
310: BVScaleColumn(eps->V,nv,1.0/gamma);
311: BVSetActiveColumns(eps->V,eps->nconv,nv);
312: DSSetDimensions(eps->ds,nv,k,nv);
313: }
314: /* Prepare the Rayleigh quotient for restart */
315: DSTruncate(eps->ds,k+l,PETSC_FALSE);
316: }
317: }
318: /* Update the corresponding vectors V(:,idx) = V*Q(:,idx) */
319: DSGetMat(eps->ds,DS_MAT_Q,&U);
320: BVMultInPlace(eps->V,U,eps->nconv,k+l);
321: MatDestroy(&U);
323: if (eps->reason == EPS_CONVERGED_ITERATING && !breakdown) BVCopyColumn(eps->V,nv,k+l);
324: eps->nconv = k;
325: EPSMonitor(eps,eps->its,nconv,eps->eigr,eps->eigi,eps->errest,nv);
326: }
328: if (harmonic) PetscFree(g);
329: DSTruncate(eps->ds,eps->nconv,PETSC_TRUE);
330: PetscFunctionReturn(0);
331: }
333: static PetscErrorCode EPSKrylovSchurSetRestart_KrylovSchur(EPS eps,PetscReal keep)
334: {
335: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
337: if (keep==PETSC_DEFAULT) ctx->keep = 0.5;
338: else {
340: ctx->keep = keep;
341: }
342: PetscFunctionReturn(0);
343: }
345: /*@
346: EPSKrylovSchurSetRestart - Sets the restart parameter for the Krylov-Schur
347: method, in particular the proportion of basis vectors that must be kept
348: after restart.
350: Logically Collective on eps
352: Input Parameters:
353: + eps - the eigenproblem solver context
354: - keep - the number of vectors to be kept at restart
356: Options Database Key:
357: . -eps_krylovschur_restart - Sets the restart parameter
359: Notes:
360: Allowed values are in the range [0.1,0.9]. The default is 0.5.
362: Level: advanced
364: .seealso: EPSKrylovSchurGetRestart()
365: @*/
366: PetscErrorCode EPSKrylovSchurSetRestart(EPS eps,PetscReal keep)
367: {
370: PetscTryMethod(eps,"EPSKrylovSchurSetRestart_C",(EPS,PetscReal),(eps,keep));
371: PetscFunctionReturn(0);
372: }
374: static PetscErrorCode EPSKrylovSchurGetRestart_KrylovSchur(EPS eps,PetscReal *keep)
375: {
376: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
378: *keep = ctx->keep;
379: PetscFunctionReturn(0);
380: }
382: /*@
383: EPSKrylovSchurGetRestart - Gets the restart parameter used in the
384: Krylov-Schur method.
386: Not Collective
388: Input Parameter:
389: . eps - the eigenproblem solver context
391: Output Parameter:
392: . keep - the restart parameter
394: Level: advanced
396: .seealso: EPSKrylovSchurSetRestart()
397: @*/
398: PetscErrorCode EPSKrylovSchurGetRestart(EPS eps,PetscReal *keep)
399: {
402: PetscUseMethod(eps,"EPSKrylovSchurGetRestart_C",(EPS,PetscReal*),(eps,keep));
403: PetscFunctionReturn(0);
404: }
406: static PetscErrorCode EPSKrylovSchurSetLocking_KrylovSchur(EPS eps,PetscBool lock)
407: {
408: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
410: ctx->lock = lock;
411: PetscFunctionReturn(0);
412: }
414: /*@
415: EPSKrylovSchurSetLocking - Choose between locking and non-locking variants of
416: the Krylov-Schur method.
418: Logically Collective on eps
420: Input Parameters:
421: + eps - the eigenproblem solver context
422: - lock - true if the locking variant must be selected
424: Options Database Key:
425: . -eps_krylovschur_locking - Sets the locking flag
427: Notes:
428: The default is to lock converged eigenpairs when the method restarts.
429: This behaviour can be changed so that all directions are kept in the
430: working subspace even if already converged to working accuracy (the
431: non-locking variant).
433: Level: advanced
435: .seealso: EPSKrylovSchurGetLocking()
436: @*/
437: PetscErrorCode EPSKrylovSchurSetLocking(EPS eps,PetscBool lock)
438: {
441: PetscTryMethod(eps,"EPSKrylovSchurSetLocking_C",(EPS,PetscBool),(eps,lock));
442: PetscFunctionReturn(0);
443: }
445: static PetscErrorCode EPSKrylovSchurGetLocking_KrylovSchur(EPS eps,PetscBool *lock)
446: {
447: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
449: *lock = ctx->lock;
450: PetscFunctionReturn(0);
451: }
453: /*@
454: EPSKrylovSchurGetLocking - Gets the locking flag used in the Krylov-Schur
455: method.
457: Not Collective
459: Input Parameter:
460: . eps - the eigenproblem solver context
462: Output Parameter:
463: . lock - the locking flag
465: Level: advanced
467: .seealso: EPSKrylovSchurSetLocking()
468: @*/
469: PetscErrorCode EPSKrylovSchurGetLocking(EPS eps,PetscBool *lock)
470: {
473: PetscUseMethod(eps,"EPSKrylovSchurGetLocking_C",(EPS,PetscBool*),(eps,lock));
474: PetscFunctionReturn(0);
475: }
477: static PetscErrorCode EPSKrylovSchurSetPartitions_KrylovSchur(EPS eps,PetscInt npart)
478: {
479: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
480: PetscMPIInt size;
482: if (ctx->npart!=npart) {
483: if (ctx->commset) PetscSubcommDestroy(&ctx->subc);
484: EPSDestroy(&ctx->eps);
485: }
486: if (npart == PETSC_DEFAULT || npart == PETSC_DECIDE) {
487: ctx->npart = 1;
488: } else {
489: MPI_Comm_size(PetscObjectComm((PetscObject)eps),&size);
491: ctx->npart = npart;
492: }
493: eps->state = EPS_STATE_INITIAL;
494: PetscFunctionReturn(0);
495: }
497: /*@
498: EPSKrylovSchurSetPartitions - Sets the number of partitions for the
499: case of doing spectrum slicing for a computational interval with the
500: communicator split in several sub-communicators.
502: Logically Collective on eps
504: Input Parameters:
505: + eps - the eigenproblem solver context
506: - npart - number of partitions
508: Options Database Key:
509: . -eps_krylovschur_partitions <npart> - Sets the number of partitions
511: Notes:
512: By default, npart=1 so all processes in the communicator participate in
513: the processing of the whole interval. If npart>1 then the interval is
514: divided into npart subintervals, each of them being processed by a
515: subset of processes.
517: The interval is split proportionally unless the separation points are
518: specified with EPSKrylovSchurSetSubintervals().
520: Level: advanced
522: .seealso: EPSKrylovSchurSetSubintervals(), EPSSetInterval()
523: @*/
524: PetscErrorCode EPSKrylovSchurSetPartitions(EPS eps,PetscInt npart)
525: {
528: PetscTryMethod(eps,"EPSKrylovSchurSetPartitions_C",(EPS,PetscInt),(eps,npart));
529: PetscFunctionReturn(0);
530: }
532: static PetscErrorCode EPSKrylovSchurGetPartitions_KrylovSchur(EPS eps,PetscInt *npart)
533: {
534: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
536: *npart = ctx->npart;
537: PetscFunctionReturn(0);
538: }
540: /*@
541: EPSKrylovSchurGetPartitions - Gets the number of partitions of the
542: communicator in case of spectrum slicing.
544: Not Collective
546: Input Parameter:
547: . eps - the eigenproblem solver context
549: Output Parameter:
550: . npart - number of partitions
552: Level: advanced
554: .seealso: EPSKrylovSchurSetPartitions()
555: @*/
556: PetscErrorCode EPSKrylovSchurGetPartitions(EPS eps,PetscInt *npart)
557: {
560: PetscUseMethod(eps,"EPSKrylovSchurGetPartitions_C",(EPS,PetscInt*),(eps,npart));
561: PetscFunctionReturn(0);
562: }
564: static PetscErrorCode EPSKrylovSchurSetDetectZeros_KrylovSchur(EPS eps,PetscBool detect)
565: {
566: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
568: ctx->detect = detect;
569: eps->state = EPS_STATE_INITIAL;
570: PetscFunctionReturn(0);
571: }
573: /*@
574: EPSKrylovSchurSetDetectZeros - Sets a flag to enforce detection of
575: zeros during the factorizations throughout the spectrum slicing computation.
577: Logically Collective on eps
579: Input Parameters:
580: + eps - the eigenproblem solver context
581: - detect - check for zeros
583: Options Database Key:
584: . -eps_krylovschur_detect_zeros - Check for zeros; this takes an optional
585: bool value (0/1/no/yes/true/false)
587: Notes:
588: A zero in the factorization indicates that a shift coincides with an eigenvalue.
590: This flag is turned off by default, and may be necessary in some cases,
591: especially when several partitions are being used. This feature currently
592: requires an external package for factorizations with support for zero
593: detection, e.g. MUMPS.
595: Level: advanced
597: .seealso: EPSKrylovSchurSetPartitions(), EPSSetInterval()
598: @*/
599: PetscErrorCode EPSKrylovSchurSetDetectZeros(EPS eps,PetscBool detect)
600: {
603: PetscTryMethod(eps,"EPSKrylovSchurSetDetectZeros_C",(EPS,PetscBool),(eps,detect));
604: PetscFunctionReturn(0);
605: }
607: static PetscErrorCode EPSKrylovSchurGetDetectZeros_KrylovSchur(EPS eps,PetscBool *detect)
608: {
609: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
611: *detect = ctx->detect;
612: PetscFunctionReturn(0);
613: }
615: /*@
616: EPSKrylovSchurGetDetectZeros - Gets the flag that enforces zero detection
617: in spectrum slicing.
619: Not Collective
621: Input Parameter:
622: . eps - the eigenproblem solver context
624: Output Parameter:
625: . detect - whether zeros detection is enforced during factorizations
627: Level: advanced
629: .seealso: EPSKrylovSchurSetDetectZeros()
630: @*/
631: PetscErrorCode EPSKrylovSchurGetDetectZeros(EPS eps,PetscBool *detect)
632: {
635: PetscUseMethod(eps,"EPSKrylovSchurGetDetectZeros_C",(EPS,PetscBool*),(eps,detect));
636: PetscFunctionReturn(0);
637: }
639: static PetscErrorCode EPSKrylovSchurSetDimensions_KrylovSchur(EPS eps,PetscInt nev,PetscInt ncv,PetscInt mpd)
640: {
641: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
644: ctx->nev = nev;
645: if (ncv == PETSC_DECIDE || ncv == PETSC_DEFAULT) {
646: ctx->ncv = PETSC_DEFAULT;
647: } else {
649: ctx->ncv = ncv;
650: }
651: if (mpd == PETSC_DECIDE || mpd == PETSC_DEFAULT) {
652: ctx->mpd = PETSC_DEFAULT;
653: } else {
655: ctx->mpd = mpd;
656: }
657: eps->state = EPS_STATE_INITIAL;
658: PetscFunctionReturn(0);
659: }
661: /*@
662: EPSKrylovSchurSetDimensions - Sets the dimensions used for each subsolve
663: step in case of doing spectrum slicing for a computational interval.
664: The meaning of the parameters is the same as in EPSSetDimensions().
666: Logically Collective on eps
668: Input Parameters:
669: + eps - the eigenproblem solver context
670: . nev - number of eigenvalues to compute
671: . ncv - the maximum dimension of the subspace to be used by the subsolve
672: - mpd - the maximum dimension allowed for the projected problem
674: Options Database Key:
675: + -eps_krylovschur_nev <nev> - Sets the number of eigenvalues
676: . -eps_krylovschur_ncv <ncv> - Sets the dimension of the subspace
677: - -eps_krylovschur_mpd <mpd> - Sets the maximum projected dimension
679: Level: advanced
681: .seealso: EPSKrylovSchurGetDimensions(), EPSSetDimensions(), EPSSetInterval()
682: @*/
683: PetscErrorCode EPSKrylovSchurSetDimensions(EPS eps,PetscInt nev,PetscInt ncv,PetscInt mpd)
684: {
689: PetscTryMethod(eps,"EPSKrylovSchurSetDimensions_C",(EPS,PetscInt,PetscInt,PetscInt),(eps,nev,ncv,mpd));
690: PetscFunctionReturn(0);
691: }
693: static PetscErrorCode EPSKrylovSchurGetDimensions_KrylovSchur(EPS eps,PetscInt *nev,PetscInt *ncv,PetscInt *mpd)
694: {
695: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
697: if (nev) *nev = ctx->nev;
698: if (ncv) *ncv = ctx->ncv;
699: if (mpd) *mpd = ctx->mpd;
700: PetscFunctionReturn(0);
701: }
703: /*@
704: EPSKrylovSchurGetDimensions - Gets the dimensions used for each subsolve
705: step in case of doing spectrum slicing for a computational interval.
707: Not Collective
709: Input Parameter:
710: . eps - the eigenproblem solver context
712: Output Parameters:
713: + nev - number of eigenvalues to compute
714: . ncv - the maximum dimension of the subspace to be used by the subsolve
715: - mpd - the maximum dimension allowed for the projected problem
717: Level: advanced
719: .seealso: EPSKrylovSchurSetDimensions()
720: @*/
721: PetscErrorCode EPSKrylovSchurGetDimensions(EPS eps,PetscInt *nev,PetscInt *ncv,PetscInt *mpd)
722: {
724: PetscUseMethod(eps,"EPSKrylovSchurGetDimensions_C",(EPS,PetscInt*,PetscInt*,PetscInt*),(eps,nev,ncv,mpd));
725: PetscFunctionReturn(0);
726: }
728: static PetscErrorCode EPSKrylovSchurSetSubintervals_KrylovSchur(EPS eps,PetscReal* subint)
729: {
730: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
731: PetscInt i;
735: if (ctx->subintervals) PetscFree(ctx->subintervals);
736: PetscMalloc1(ctx->npart+1,&ctx->subintervals);
737: for (i=0;i<ctx->npart+1;i++) ctx->subintervals[i] = subint[i];
738: ctx->subintset = PETSC_TRUE;
739: eps->state = EPS_STATE_INITIAL;
740: PetscFunctionReturn(0);
741: }
743: /*@C
744: EPSKrylovSchurSetSubintervals - Sets the points that delimit the
745: subintervals to be used in spectrum slicing with several partitions.
747: Logically Collective on eps
749: Input Parameters:
750: + eps - the eigenproblem solver context
751: - subint - array of real values specifying subintervals
753: Notes:
754: This function must be called after EPSKrylovSchurSetPartitions(). For npart
755: partitions, the argument subint must contain npart+1 real values sorted in
756: ascending order, subint_0, subint_1, ..., subint_npart, where the first
757: and last values must coincide with the interval endpoints set with
758: EPSSetInterval().
760: The subintervals are then defined by two consecutive points [subint_0,subint_1],
761: [subint_1,subint_2], and so on.
763: Level: advanced
765: .seealso: EPSKrylovSchurSetPartitions(), EPSKrylovSchurGetSubintervals(), EPSSetInterval()
766: @*/
767: PetscErrorCode EPSKrylovSchurSetSubintervals(EPS eps,PetscReal *subint)
768: {
770: PetscTryMethod(eps,"EPSKrylovSchurSetSubintervals_C",(EPS,PetscReal*),(eps,subint));
771: PetscFunctionReturn(0);
772: }
774: static PetscErrorCode EPSKrylovSchurGetSubintervals_KrylovSchur(EPS eps,PetscReal **subint)
775: {
776: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
777: PetscInt i;
779: if (!ctx->subintset) {
782: }
783: PetscMalloc1(ctx->npart+1,subint);
784: for (i=0;i<=ctx->npart;i++) (*subint)[i] = ctx->subintervals[i];
785: PetscFunctionReturn(0);
786: }
788: /*@C
789: EPSKrylovSchurGetSubintervals - Returns the points that delimit the
790: subintervals used in spectrum slicing with several partitions.
792: Logically Collective on eps
794: Input Parameter:
795: . eps - the eigenproblem solver context
797: Output Parameter:
798: . subint - array of real values specifying subintervals
800: Notes:
801: If the user passed values with EPSKrylovSchurSetSubintervals(), then the
802: same values are returned. Otherwise, the values computed internally are
803: obtained.
805: This function is only available for spectrum slicing runs.
807: The returned array has length npart+1 (see EPSKrylovSchurGetPartitions())
808: and should be freed by the user.
810: Fortran Notes:
811: The calling sequence from Fortran is
812: .vb
813: EPSKrylovSchurGetSubintervals(eps,subint,ierr)
814: double precision subint(npart+1) output
815: .ve
817: Level: advanced
819: .seealso: EPSKrylovSchurSetSubintervals(), EPSKrylovSchurGetPartitions(), EPSSetInterval()
820: @*/
821: PetscErrorCode EPSKrylovSchurGetSubintervals(EPS eps,PetscReal **subint)
822: {
825: PetscUseMethod(eps,"EPSKrylovSchurGetSubintervals_C",(EPS,PetscReal**),(eps,subint));
826: PetscFunctionReturn(0);
827: }
829: static PetscErrorCode EPSKrylovSchurGetInertias_KrylovSchur(EPS eps,PetscInt *n,PetscReal **shifts,PetscInt **inertias)
830: {
831: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
832: PetscInt i,numsh;
833: EPS_SR sr = ctx->sr;
837: switch (eps->state) {
838: case EPS_STATE_INITIAL:
839: break;
840: case EPS_STATE_SETUP:
841: numsh = ctx->npart+1;
842: if (n) *n = numsh;
843: if (shifts) {
844: PetscMalloc1(numsh,shifts);
845: (*shifts)[0] = eps->inta;
846: if (ctx->npart==1) (*shifts)[1] = eps->intb;
847: else for (i=1;i<numsh;i++) (*shifts)[i] = ctx->subintervals[i];
848: }
849: if (inertias) {
850: PetscMalloc1(numsh,inertias);
851: (*inertias)[0] = (sr->dir==1)?sr->inertia0:sr->inertia1;
852: if (ctx->npart==1) (*inertias)[1] = (sr->dir==1)?sr->inertia1:sr->inertia0;
853: else for (i=1;i<numsh;i++) (*inertias)[i] = (*inertias)[i-1]+ctx->nconv_loc[i-1];
854: }
855: break;
856: case EPS_STATE_SOLVED:
857: case EPS_STATE_EIGENVECTORS:
858: numsh = ctx->nshifts;
859: if (n) *n = numsh;
860: if (shifts) {
861: PetscMalloc1(numsh,shifts);
862: for (i=0;i<numsh;i++) (*shifts)[i] = ctx->shifts[i];
863: }
864: if (inertias) {
865: PetscMalloc1(numsh,inertias);
866: for (i=0;i<numsh;i++) (*inertias)[i] = ctx->inertias[i];
867: }
868: break;
869: }
870: PetscFunctionReturn(0);
871: }
873: /*@C
874: EPSKrylovSchurGetInertias - Gets the values of the shifts and their
875: corresponding inertias in case of doing spectrum slicing for a
876: computational interval.
878: Not Collective
880: Input Parameter:
881: . eps - the eigenproblem solver context
883: Output Parameters:
884: + n - number of shifts, including the endpoints of the interval
885: . shifts - the values of the shifts used internally in the solver
886: - inertias - the values of the inertia in each shift
888: Notes:
889: If called after EPSSolve(), all shifts used internally by the solver are
890: returned (including both endpoints and any intermediate ones). If called
891: before EPSSolve() and after EPSSetUp() then only the information of the
892: endpoints of subintervals is available.
894: This function is only available for spectrum slicing runs.
896: The returned arrays should be freed by the user. Can pass NULL in any of
897: the two arrays if not required.
899: Fortran Notes:
900: The calling sequence from Fortran is
901: .vb
902: EPSKrylovSchurGetInertias(eps,n,shifts,inertias,ierr)
903: integer n
904: double precision shifts(*)
905: integer inertias(*)
906: .ve
907: The arrays should be at least of length n. The value of n can be determined
908: by an initial call
909: .vb
910: EPSKrylovSchurGetInertias(eps,n,PETSC_NULL_REAL,PETSC_NULL_INTEGER,ierr)
911: .ve
913: Level: advanced
915: .seealso: EPSSetInterval(), EPSKrylovSchurSetSubintervals()
916: @*/
917: PetscErrorCode EPSKrylovSchurGetInertias(EPS eps,PetscInt *n,PetscReal **shifts,PetscInt **inertias)
918: {
921: PetscUseMethod(eps,"EPSKrylovSchurGetInertias_C",(EPS,PetscInt*,PetscReal**,PetscInt**),(eps,n,shifts,inertias));
922: PetscFunctionReturn(0);
923: }
925: static PetscErrorCode EPSKrylovSchurGetSubcommInfo_KrylovSchur(EPS eps,PetscInt *k,PetscInt *n,Vec *v)
926: {
927: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
928: EPS_SR sr = ((EPS_KRYLOVSCHUR*)ctx->eps->data)->sr;
932: if (k) *k = (ctx->npart==1)? 0: ctx->subc->color;
933: if (n) *n = sr->numEigs;
934: if (v) BVCreateVec(sr->V,v);
935: PetscFunctionReturn(0);
936: }
938: /*@C
939: EPSKrylovSchurGetSubcommInfo - Gets information related to the case of
940: doing spectrum slicing for a computational interval with multiple
941: communicators.
943: Collective on the subcommunicator (if v is given)
945: Input Parameter:
946: . eps - the eigenproblem solver context
948: Output Parameters:
949: + k - index of the subinterval for the calling process
950: . n - number of eigenvalues found in the k-th subinterval
951: - v - a vector owned by processes in the subcommunicator with dimensions
952: compatible for locally computed eigenvectors (or NULL)
954: Notes:
955: This function is only available for spectrum slicing runs.
957: The returned Vec should be destroyed by the user.
959: Level: advanced
961: .seealso: EPSSetInterval(), EPSKrylovSchurSetPartitions(), EPSKrylovSchurGetSubcommPairs()
962: @*/
963: PetscErrorCode EPSKrylovSchurGetSubcommInfo(EPS eps,PetscInt *k,PetscInt *n,Vec *v)
964: {
966: PetscUseMethod(eps,"EPSKrylovSchurGetSubcommInfo_C",(EPS,PetscInt*,PetscInt*,Vec*),(eps,k,n,v));
967: PetscFunctionReturn(0);
968: }
970: static PetscErrorCode EPSKrylovSchurGetSubcommPairs_KrylovSchur(EPS eps,PetscInt i,PetscScalar *eig,Vec v)
971: {
972: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
973: EPS_SR sr = ((EPS_KRYLOVSCHUR*)ctx->eps->data)->sr;
975: EPSCheckSolved(eps,1);
978: if (eig) *eig = sr->eigr[sr->perm[i]];
979: if (v) BVCopyVec(sr->V,sr->perm[i],v);
980: PetscFunctionReturn(0);
981: }
983: /*@
984: EPSKrylovSchurGetSubcommPairs - Gets the i-th eigenpair stored
985: internally in the subcommunicator to which the calling process belongs.
987: Collective on the subcommunicator (if v is given)
989: Input Parameters:
990: + eps - the eigenproblem solver context
991: - i - index of the solution
993: Output Parameters:
994: + eig - the eigenvalue
995: - v - the eigenvector
997: Notes:
998: It is allowed to pass NULL for v if the eigenvector is not required.
999: Otherwise, the caller must provide a valid Vec objects, i.e.,
1000: it must be created by the calling program with EPSKrylovSchurGetSubcommInfo().
1002: The index i should be a value between 0 and n-1, where n is the number of
1003: vectors in the local subinterval, see EPSKrylovSchurGetSubcommInfo().
1005: Level: advanced
1007: .seealso: EPSSetInterval(), EPSKrylovSchurSetPartitions(), EPSKrylovSchurGetSubcommInfo(), EPSKrylovSchurGetSubcommMats()
1008: @*/
1009: PetscErrorCode EPSKrylovSchurGetSubcommPairs(EPS eps,PetscInt i,PetscScalar *eig,Vec v)
1010: {
1013: PetscUseMethod(eps,"EPSKrylovSchurGetSubcommPairs_C",(EPS,PetscInt,PetscScalar*,Vec),(eps,i,eig,v));
1014: PetscFunctionReturn(0);
1015: }
1017: static PetscErrorCode EPSKrylovSchurGetSubcommMats_KrylovSchur(EPS eps,Mat *A,Mat *B)
1018: {
1019: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
1023: EPSGetOperators(ctx->eps,A,B);
1024: PetscFunctionReturn(0);
1025: }
1027: /*@C
1028: EPSKrylovSchurGetSubcommMats - Gets the eigenproblem matrices stored
1029: internally in the subcommunicator to which the calling process belongs.
1031: Collective on the subcommunicator
1033: Input Parameter:
1034: . eps - the eigenproblem solver context
1036: Output Parameters:
1037: + A - the matrix associated with the eigensystem
1038: - B - the second matrix in the case of generalized eigenproblems
1040: Notes:
1041: This is the analog of EPSGetOperators(), but returns the matrices distributed
1042: differently (in the subcommunicator rather than in the parent communicator).
1044: These matrices should not be modified by the user.
1046: Level: advanced
1048: .seealso: EPSSetInterval(), EPSKrylovSchurSetPartitions(), EPSKrylovSchurGetSubcommInfo()
1049: @*/
1050: PetscErrorCode EPSKrylovSchurGetSubcommMats(EPS eps,Mat *A,Mat *B)
1051: {
1053: PetscTryMethod(eps,"EPSKrylovSchurGetSubcommMats_C",(EPS,Mat*,Mat*),(eps,A,B));
1054: PetscFunctionReturn(0);
1055: }
1057: static PetscErrorCode EPSKrylovSchurUpdateSubcommMats_KrylovSchur(EPS eps,PetscScalar a,PetscScalar ap,Mat Au,PetscScalar b,PetscScalar bp, Mat Bu,MatStructure str,PetscBool globalup)
1058: {
1059: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data,*subctx;
1060: Mat A,B=NULL,Ag,Bg=NULL;
1061: PetscBool reuse=PETSC_TRUE;
1065: EPSGetOperators(eps,&Ag,&Bg);
1066: EPSGetOperators(ctx->eps,&A,&B);
1068: MatScale(A,a);
1069: if (Au) MatAXPY(A,ap,Au,str);
1070: if (B) MatScale(B,b);
1071: if (Bu) MatAXPY(B,bp,Bu,str);
1072: EPSSetOperators(ctx->eps,A,B);
1074: /* Update stored matrix state */
1075: subctx = (EPS_KRYLOVSCHUR*)ctx->eps->data;
1076: PetscObjectStateGet((PetscObject)A,&subctx->Astate);
1077: if (B) PetscObjectStateGet((PetscObject)B,&subctx->Bstate);
1079: /* Update matrices in the parent communicator if requested by user */
1080: if (globalup) {
1081: if (ctx->npart>1) {
1082: if (!ctx->isrow) {
1083: MatGetOwnershipIS(Ag,&ctx->isrow,&ctx->iscol);
1084: reuse = PETSC_FALSE;
1085: }
1086: if (str==DIFFERENT_NONZERO_PATTERN || str==UNKNOWN_NONZERO_PATTERN) reuse = PETSC_FALSE;
1087: if (ctx->submata && !reuse) MatDestroyMatrices(1,&ctx->submata);
1088: MatCreateSubMatrices(A,1,&ctx->isrow,&ctx->iscol,(reuse)?MAT_REUSE_MATRIX:MAT_INITIAL_MATRIX,&ctx->submata);
1089: MatCreateMPIMatConcatenateSeqMat(((PetscObject)Ag)->comm,ctx->submata[0],PETSC_DECIDE,MAT_REUSE_MATRIX,&Ag);
1090: if (B) {
1091: if (ctx->submatb && !reuse) MatDestroyMatrices(1,&ctx->submatb);
1092: MatCreateSubMatrices(B,1,&ctx->isrow,&ctx->iscol,(reuse)?MAT_REUSE_MATRIX:MAT_INITIAL_MATRIX,&ctx->submatb);
1093: MatCreateMPIMatConcatenateSeqMat(((PetscObject)Bg)->comm,ctx->submatb[0],PETSC_DECIDE,MAT_REUSE_MATRIX,&Bg);
1094: }
1095: }
1096: PetscObjectStateGet((PetscObject)Ag,&ctx->Astate);
1097: if (Bg) PetscObjectStateGet((PetscObject)Bg,&ctx->Bstate);
1098: }
1099: EPSSetOperators(eps,Ag,Bg);
1100: PetscFunctionReturn(0);
1101: }
1103: /*@
1104: EPSKrylovSchurUpdateSubcommMats - Update the eigenproblem matrices stored
1105: internally in the subcommunicator to which the calling process belongs.
1107: Collective on eps
1109: Input Parameters:
1110: + eps - the eigenproblem solver context
1111: . s - scalar that multiplies the existing A matrix
1112: . a - scalar used in the axpy operation on A
1113: . Au - matrix used in the axpy operation on A
1114: . t - scalar that multiplies the existing B matrix
1115: . b - scalar used in the axpy operation on B
1116: . Bu - matrix used in the axpy operation on B
1117: . str - structure flag
1118: - globalup - flag indicating if global matrices must be updated
1120: Notes:
1121: This function modifies the eigenproblem matrices at the subcommunicator level,
1122: and optionally updates the global matrices in the parent communicator. The updates
1123: are expressed as A <-- s*A + a*Au, B <-- t*B + b*Bu.
1125: It is possible to update one of the matrices, or both.
1127: The matrices Au and Bu must be equal in all subcommunicators.
1129: The str flag is passed to the MatAXPY() operations to perform the updates.
1131: If globalup is true, communication is carried out to reconstruct the updated
1132: matrices in the parent communicator. The user must be warned that if global
1133: matrices are not in sync with subcommunicator matrices, the errors computed
1134: by EPSComputeError() will be wrong even if the computed solution is correct
1135: (the synchronization may be done only once at the end).
1137: Level: advanced
1139: .seealso: EPSSetInterval(), EPSKrylovSchurSetPartitions(), EPSKrylovSchurGetSubcommMats()
1140: @*/
1141: PetscErrorCode EPSKrylovSchurUpdateSubcommMats(EPS eps,PetscScalar s,PetscScalar a,Mat Au,PetscScalar t,PetscScalar b,Mat Bu,MatStructure str,PetscBool globalup)
1142: {
1152: PetscTryMethod(eps,"EPSKrylovSchurUpdateSubcommMats_C",(EPS,PetscScalar,PetscScalar,Mat,PetscScalar,PetscScalar,Mat,MatStructure,PetscBool),(eps,s,a,Au,t,b,Bu,str,globalup));
1153: PetscFunctionReturn(0);
1154: }
1156: PetscErrorCode EPSKrylovSchurGetChildEPS(EPS eps,EPS *childeps)
1157: {
1158: EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data,*ctx_local;
1159: Mat A,B=NULL,Ar=NULL,Br=NULL;
1160: PetscMPIInt rank;
1161: PetscObjectState Astate,Bstate=0;
1162: PetscObjectId Aid,Bid=0;
1163: STType sttype;
1164: PetscInt nmat;
1165: const char *prefix;
1166: MPI_Comm child;
1168: EPSGetOperators(eps,&A,&B);
1169: if (ctx->npart==1) {
1170: if (!ctx->eps) EPSCreate(((PetscObject)eps)->comm,&ctx->eps);
1171: EPSGetOptionsPrefix(eps,&prefix);
1172: EPSSetOptionsPrefix(ctx->eps,prefix);
1173: EPSSetOperators(ctx->eps,A,B);
1174: } else {
1175: PetscObjectStateGet((PetscObject)A,&Astate);
1176: PetscObjectGetId((PetscObject)A,&Aid);
1177: if (B) {
1178: PetscObjectStateGet((PetscObject)B,&Bstate);
1179: PetscObjectGetId((PetscObject)B,&Bid);
1180: }
1181: if (!ctx->subc) {
1182: /* Create context for subcommunicators */
1183: PetscSubcommCreate(PetscObjectComm((PetscObject)eps),&ctx->subc);
1184: PetscSubcommSetNumber(ctx->subc,ctx->npart);
1185: PetscSubcommSetType(ctx->subc,PETSC_SUBCOMM_CONTIGUOUS);
1186: PetscLogObjectMemory((PetscObject)eps,sizeof(PetscSubcomm));
1187: PetscSubcommGetChild(ctx->subc,&child);
1189: /* Duplicate matrices */
1190: MatCreateRedundantMatrix(A,0,child,MAT_INITIAL_MATRIX,&Ar);
1191: PetscLogObjectParent((PetscObject)eps,(PetscObject)Ar);
1192: ctx->Astate = Astate;
1193: ctx->Aid = Aid;
1194: MatPropagateSymmetryOptions(A,Ar);
1195: if (B) {
1196: MatCreateRedundantMatrix(B,0,child,MAT_INITIAL_MATRIX,&Br);
1197: PetscLogObjectParent((PetscObject)eps,(PetscObject)Br);
1198: ctx->Bstate = Bstate;
1199: ctx->Bid = Bid;
1200: MatPropagateSymmetryOptions(B,Br);
1201: }
1202: } else {
1203: PetscSubcommGetChild(ctx->subc,&child);
1204: if (ctx->Astate != Astate || (B && ctx->Bstate != Bstate) || ctx->Aid != Aid || (B && ctx->Bid != Bid)) {
1205: STGetNumMatrices(ctx->eps->st,&nmat);
1206: if (nmat) EPSGetOperators(ctx->eps,&Ar,&Br);
1207: MatCreateRedundantMatrix(A,0,child,MAT_INITIAL_MATRIX,&Ar);
1208: ctx->Astate = Astate;
1209: ctx->Aid = Aid;
1210: MatPropagateSymmetryOptions(A,Ar);
1211: if (B) {
1212: MatCreateRedundantMatrix(B,0,child,MAT_INITIAL_MATRIX,&Br);
1213: ctx->Bstate = Bstate;
1214: ctx->Bid = Bid;
1215: MatPropagateSymmetryOptions(B,Br);
1216: }
1217: EPSSetOperators(ctx->eps,Ar,Br);
1218: MatDestroy(&Ar);
1219: MatDestroy(&Br);
1220: }
1221: }
1223: /* Create auxiliary EPS */
1224: if (!ctx->eps) {
1225: EPSCreate(child,&ctx->eps);
1226: EPSGetOptionsPrefix(eps,&prefix);
1227: EPSSetOptionsPrefix(ctx->eps,prefix);
1228: EPSSetOperators(ctx->eps,Ar,Br);
1229: MatDestroy(&Ar);
1230: MatDestroy(&Br);
1231: }
1232: /* Create subcommunicator grouping processes with same rank */
1233: if (ctx->commset) MPI_Comm_free(&ctx->commrank);
1234: MPI_Comm_rank(child,&rank);
1235: MPI_Comm_split(((PetscObject)eps)->comm,rank,ctx->subc->color,&ctx->commrank);
1236: ctx->commset = PETSC_TRUE;
1237: }
1238: EPSSetType(ctx->eps,((PetscObject)eps)->type_name);
1239: STGetType(eps->st,&sttype);
1240: STSetType(ctx->eps->st,sttype);
1242: ctx_local = (EPS_KRYLOVSCHUR*)ctx->eps->data;
1243: ctx_local->npart = ctx->npart;
1244: ctx_local->global = PETSC_FALSE;
1245: ctx_local->eps = eps;
1246: ctx_local->subc = ctx->subc;
1247: ctx_local->commrank = ctx->commrank;
1248: *childeps = ctx->eps;
1249: PetscFunctionReturn(0);
1250: }
1252: static PetscErrorCode EPSKrylovSchurGetKSP_KrylovSchur(EPS eps,KSP *ksp)
1253: {
1254: EPS_KRYLOVSCHUR *ctx=(EPS_KRYLOVSCHUR*)eps->data;
1255: ST st;
1256: PetscBool isfilt;
1258: PetscObjectTypeCompare((PetscObject)eps->st,STFILTER,&isfilt);
1260: EPSKrylovSchurGetChildEPS(eps,&ctx->eps);
1261: EPSGetST(ctx->eps,&st);
1262: STGetOperator(st,NULL);
1263: STGetKSP(st,ksp);
1264: PetscFunctionReturn(0);
1265: }
1267: /*@
1268: EPSKrylovSchurGetKSP - Retrieve the linear solver object associated with the
1269: internal EPS object in case of doing spectrum slicing for a computational interval.
1271: Collective on eps
1273: Input Parameter:
1274: . eps - the eigenproblem solver context
1276: Output Parameter:
1277: . ksp - the internal KSP object
1279: Notes:
1280: When invoked to compute all eigenvalues in an interval with spectrum
1281: slicing, EPSKRYLOVSCHUR creates another EPS object internally that is
1282: used to compute eigenvalues by chunks near selected shifts. This function
1283: allows access to the KSP object associated to this internal EPS object.
1285: This function is only available for spectrum slicing runs. In case of
1286: having more than one partition, the returned KSP will be different
1287: in MPI processes belonging to different partitions. Hence, if required,
1288: EPSKrylovSchurSetPartitions() must be called BEFORE this function.
1290: Level: advanced
1292: .seealso: EPSSetInterval(), EPSKrylovSchurSetPartitions()
1293: @*/
1294: PetscErrorCode EPSKrylovSchurGetKSP(EPS eps,KSP *ksp)
1295: {
1297: PetscUseMethod(eps,"EPSKrylovSchurGetKSP_C",(EPS,KSP*),(eps,ksp));
1298: PetscFunctionReturn(0);
1299: }
1301: PetscErrorCode EPSSetFromOptions_KrylovSchur(PetscOptionItems *PetscOptionsObject,EPS eps)
1302: {
1303: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
1304: PetscBool flg,lock,b,f1,f2,f3,isfilt;
1305: PetscReal keep;
1306: PetscInt i,j,k;
1307: KSP ksp;
1309: PetscOptionsHead(PetscOptionsObject,"EPS Krylov-Schur Options");
1311: PetscOptionsReal("-eps_krylovschur_restart","Proportion of vectors kept after restart","EPSKrylovSchurSetRestart",0.5,&keep,&flg);
1312: if (flg) EPSKrylovSchurSetRestart(eps,keep);
1314: PetscOptionsBool("-eps_krylovschur_locking","Choose between locking and non-locking variants","EPSKrylovSchurSetLocking",PETSC_TRUE,&lock,&flg);
1315: if (flg) EPSKrylovSchurSetLocking(eps,lock);
1317: i = ctx->npart;
1318: PetscOptionsInt("-eps_krylovschur_partitions","Number of partitions of the communicator for spectrum slicing","EPSKrylovSchurSetPartitions",ctx->npart,&i,&flg);
1319: if (flg) EPSKrylovSchurSetPartitions(eps,i);
1321: b = ctx->detect;
1322: PetscOptionsBool("-eps_krylovschur_detect_zeros","Check zeros during factorizations at subinterval boundaries","EPSKrylovSchurSetDetectZeros",ctx->detect,&b,&flg);
1323: if (flg) EPSKrylovSchurSetDetectZeros(eps,b);
1325: i = 1;
1326: j = k = PETSC_DECIDE;
1327: PetscOptionsInt("-eps_krylovschur_nev","Number of eigenvalues to compute in each subsolve (only for spectrum slicing)","EPSKrylovSchurSetDimensions",40,&i,&f1);
1328: PetscOptionsInt("-eps_krylovschur_ncv","Number of basis vectors in each subsolve (only for spectrum slicing)","EPSKrylovSchurSetDimensions",80,&j,&f2);
1329: PetscOptionsInt("-eps_krylovschur_mpd","Maximum dimension of projected problem in each subsolve (only for spectrum slicing)","EPSKrylovSchurSetDimensions",80,&k,&f3);
1330: if (f1 || f2 || f3) EPSKrylovSchurSetDimensions(eps,i,j,k);
1332: PetscOptionsTail();
1334: /* set options of child KSP in spectrum slicing */
1335: if (eps->which==EPS_ALL) {
1336: if (!eps->st) EPSGetST(eps,&eps->st);
1337: EPSSetDefaultST(eps);
1338: STSetFromOptions(eps->st); /* need to advance this to check ST type */
1339: PetscObjectTypeCompare((PetscObject)eps->st,STFILTER,&isfilt);
1340: if (!isfilt) {
1341: EPSKrylovSchurGetKSP_KrylovSchur(eps,&ksp);
1342: KSPSetFromOptions(ksp);
1343: }
1344: }
1345: PetscFunctionReturn(0);
1346: }
1348: PetscErrorCode EPSView_KrylovSchur(EPS eps,PetscViewer viewer)
1349: {
1350: EPS_KRYLOVSCHUR *ctx = (EPS_KRYLOVSCHUR*)eps->data;
1351: PetscBool isascii,isfilt;
1352: KSP ksp;
1353: PetscViewer sviewer;
1355: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
1356: if (isascii) {
1357: PetscViewerASCIIPrintf(viewer," %d%% of basis vectors kept after restart\n",(int)(100*ctx->keep));
1358: PetscViewerASCIIPrintf(viewer," using the %slocking variant\n",ctx->lock?"":"non-");
1359: if (eps->which==EPS_ALL) {
1360: PetscObjectTypeCompare((PetscObject)eps->st,STFILTER,&isfilt);
1361: if (isfilt) PetscViewerASCIIPrintf(viewer," using filtering to extract all eigenvalues in an interval\n");
1362: else {
1363: PetscViewerASCIIPrintf(viewer," doing spectrum slicing with nev=%" PetscInt_FMT ", ncv=%" PetscInt_FMT ", mpd=%" PetscInt_FMT "\n",ctx->nev,ctx->ncv,ctx->mpd);
1364: if (ctx->npart>1) {
1365: PetscViewerASCIIPrintf(viewer," multi-communicator spectrum slicing with %" PetscInt_FMT " partitions\n",ctx->npart);
1366: if (ctx->detect) PetscViewerASCIIPrintf(viewer," detecting zeros when factorizing at subinterval boundaries\n");
1367: }
1368: /* view child KSP */
1369: EPSKrylovSchurGetKSP_KrylovSchur(eps,&ksp);
1370: PetscViewerASCIIPushTab(viewer);
1371: if (ctx->npart>1 && ctx->subc) {
1372: PetscViewerGetSubViewer(viewer,ctx->subc->child,&sviewer);
1373: if (!ctx->subc->color) KSPView(ksp,sviewer);
1374: PetscViewerFlush(sviewer);
1375: PetscViewerRestoreSubViewer(viewer,ctx->subc->child,&sviewer);
1376: PetscViewerFlush(viewer);
1377: /* extra call needed because of the two calls to PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
1378: PetscViewerASCIIPopSynchronized(viewer);
1379: } else KSPView(ksp,viewer);
1380: PetscViewerASCIIPopTab(viewer);
1381: }
1382: }
1383: }
1384: PetscFunctionReturn(0);
1385: }
1387: PetscErrorCode EPSDestroy_KrylovSchur(EPS eps)
1388: {
1389: PetscBool isfilt;
1391: PetscObjectTypeCompare((PetscObject)eps->st,STFILTER,&isfilt);
1392: if (eps->which==EPS_ALL && !isfilt) EPSDestroy_KrylovSchur_Slice(eps);
1393: PetscFree(eps->data);
1394: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetRestart_C",NULL);
1395: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetRestart_C",NULL);
1396: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetLocking_C",NULL);
1397: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetLocking_C",NULL);
1398: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetPartitions_C",NULL);
1399: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetPartitions_C",NULL);
1400: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetDetectZeros_C",NULL);
1401: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetDetectZeros_C",NULL);
1402: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetDimensions_C",NULL);
1403: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetDimensions_C",NULL);
1404: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetSubintervals_C",NULL);
1405: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetSubintervals_C",NULL);
1406: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetInertias_C",NULL);
1407: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetSubcommInfo_C",NULL);
1408: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetSubcommPairs_C",NULL);
1409: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetSubcommMats_C",NULL);
1410: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurUpdateSubcommMats_C",NULL);
1411: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetKSP_C",NULL);
1412: PetscFunctionReturn(0);
1413: }
1415: PetscErrorCode EPSReset_KrylovSchur(EPS eps)
1416: {
1417: PetscBool isfilt;
1419: PetscObjectTypeCompare((PetscObject)eps->st,STFILTER,&isfilt);
1420: if (eps->which==EPS_ALL && !isfilt) EPSReset_KrylovSchur_Slice(eps);
1421: PetscFunctionReturn(0);
1422: }
1424: PetscErrorCode EPSSetDefaultST_KrylovSchur(EPS eps)
1425: {
1426: if (eps->which==EPS_ALL) {
1427: if (!((PetscObject)eps->st)->type_name) STSetType(eps->st,STSINVERT);
1428: }
1429: PetscFunctionReturn(0);
1430: }
1432: SLEPC_EXTERN PetscErrorCode EPSCreate_KrylovSchur(EPS eps)
1433: {
1434: EPS_KRYLOVSCHUR *ctx;
1436: PetscNewLog(eps,&ctx);
1437: eps->data = (void*)ctx;
1438: ctx->lock = PETSC_TRUE;
1439: ctx->nev = 1;
1440: ctx->ncv = PETSC_DEFAULT;
1441: ctx->mpd = PETSC_DEFAULT;
1442: ctx->npart = 1;
1443: ctx->detect = PETSC_FALSE;
1444: ctx->global = PETSC_TRUE;
1446: eps->useds = PETSC_TRUE;
1448: /* solve and computevectors determined at setup */
1449: eps->ops->setup = EPSSetUp_KrylovSchur;
1450: eps->ops->setupsort = EPSSetUpSort_KrylovSchur;
1451: eps->ops->setfromoptions = EPSSetFromOptions_KrylovSchur;
1452: eps->ops->destroy = EPSDestroy_KrylovSchur;
1453: eps->ops->reset = EPSReset_KrylovSchur;
1454: eps->ops->view = EPSView_KrylovSchur;
1455: eps->ops->backtransform = EPSBackTransform_Default;
1456: eps->ops->setdefaultst = EPSSetDefaultST_KrylovSchur;
1458: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetRestart_C",EPSKrylovSchurSetRestart_KrylovSchur);
1459: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetRestart_C",EPSKrylovSchurGetRestart_KrylovSchur);
1460: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetLocking_C",EPSKrylovSchurSetLocking_KrylovSchur);
1461: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetLocking_C",EPSKrylovSchurGetLocking_KrylovSchur);
1462: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetPartitions_C",EPSKrylovSchurSetPartitions_KrylovSchur);
1463: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetPartitions_C",EPSKrylovSchurGetPartitions_KrylovSchur);
1464: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetDetectZeros_C",EPSKrylovSchurSetDetectZeros_KrylovSchur);
1465: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetDetectZeros_C",EPSKrylovSchurGetDetectZeros_KrylovSchur);
1466: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetDimensions_C",EPSKrylovSchurSetDimensions_KrylovSchur);
1467: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetDimensions_C",EPSKrylovSchurGetDimensions_KrylovSchur);
1468: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurSetSubintervals_C",EPSKrylovSchurSetSubintervals_KrylovSchur);
1469: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetSubintervals_C",EPSKrylovSchurGetSubintervals_KrylovSchur);
1470: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetInertias_C",EPSKrylovSchurGetInertias_KrylovSchur);
1471: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetSubcommInfo_C",EPSKrylovSchurGetSubcommInfo_KrylovSchur);
1472: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetSubcommPairs_C",EPSKrylovSchurGetSubcommPairs_KrylovSchur);
1473: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetSubcommMats_C",EPSKrylovSchurGetSubcommMats_KrylovSchur);
1474: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurUpdateSubcommMats_C",EPSKrylovSchurUpdateSubcommMats_KrylovSchur);
1475: PetscObjectComposeFunction((PetscObject)eps,"EPSKrylovSchurGetKSP_C",EPSKrylovSchurGetKSP_KrylovSchur);
1476: PetscFunctionReturn(0);
1477: }