Line data Source code
1 : /*
2 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 : SLEPc - Scalable Library for Eigenvalue Problem Computations
4 : Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
5 :
6 : This file is part of SLEPc.
7 : SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 : */
10 : /*
11 : Skeleton of Davidson solver. Actual solvers are GD and JD.
12 :
13 : References:
14 :
15 : [1] E. Romero and J.E. Roman, "A parallel implementation of Davidson
16 : methods for large-scale eigenvalue problems in SLEPc", ACM Trans.
17 : Math. Software 40(2):13, 2014.
18 : */
19 :
20 : #include "davidson.h"
21 :
22 : static PetscBool cited = PETSC_FALSE;
23 : static const char citation[] =
24 : "@Article{slepc-davidson,\n"
25 : " author = \"E. Romero and J. E. Roman\",\n"
26 : " title = \"A parallel implementation of {Davidson} methods for large-scale eigenvalue problems in {SLEPc}\",\n"
27 : " journal = \"{ACM} Trans. Math. Software\",\n"
28 : " volume = \"40\",\n"
29 : " number = \"2\",\n"
30 : " pages = \"13:1--13:29\",\n"
31 : " year = \"2014,\"\n"
32 : " doi = \"https://doi.org/10.1145/2543696\"\n"
33 : "}\n";
34 :
35 95 : PetscErrorCode EPSSetUp_XD(EPS eps)
36 : {
37 95 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
38 95 : dvdDashboard *dvd = &data->ddb;
39 95 : dvdBlackboard b;
40 95 : PetscInt min_size_V,bs,initv,nmat;
41 95 : Mat A,B;
42 95 : KSP ksp;
43 95 : PetscBool ipB,ispositive;
44 95 : HarmType_t harm;
45 95 : InitType_t init;
46 95 : PetscScalar target;
47 :
48 95 : PetscFunctionBegin;
49 95 : EPSCheckNotStructured(eps);
50 : /* Setup EPS options and get the problem specification */
51 95 : bs = data->blocksize;
52 95 : if (bs <= 0) bs = 1;
53 95 : if (eps->ncv!=PETSC_DETERMINE) {
54 41 : PetscCheck(eps->ncv>=eps->nev,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"The value of ncv must be at least nev");
55 54 : } else if (eps->mpd!=PETSC_DETERMINE) eps->ncv = eps->mpd + eps->nev + bs;
56 53 : else if (eps->n < 10) eps->ncv = eps->n+eps->nev+bs;
57 53 : else if (eps->nev < 500) eps->ncv = PetscMax(eps->nev,PetscMin(eps->n-bs,PetscMax(2*eps->nev,eps->nev+15))+bs);
58 0 : else eps->ncv = PetscMax(eps->nev,PetscMin(eps->n-bs,eps->nev+500)+bs);
59 95 : if (eps->mpd==PETSC_DETERMINE) eps->mpd = PetscMin(eps->n,eps->ncv);
60 95 : PetscCheck(eps->mpd<=eps->ncv,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"The mpd parameter has to be less than or equal to ncv");
61 95 : PetscCheck(eps->mpd>=2,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"The mpd parameter has to be greater than 2");
62 95 : if (eps->max_it == PETSC_DETERMINE) eps->max_it = PetscMax(100*eps->ncv,2*eps->n);
63 95 : if (!eps->which) eps->which = EPS_LARGEST_MAGNITUDE;
64 95 : PetscCheck(eps->nev+bs<=eps->ncv,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"The value of ncv has to be greater than nev plus blocksize");
65 95 : PetscCheck(!eps->trueres,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"-eps_true_residual is disabled in this solver.");
66 95 : EPSCheckUnsupported(eps,EPS_FEATURE_REGION | EPS_FEATURE_TWOSIDED);
67 :
68 95 : if (!data->minv) data->minv = (eps->n && eps->n<10)? 1: PetscMin(PetscMax(bs,6),eps->mpd/2);
69 95 : min_size_V = data->minv;
70 95 : PetscCheck(min_size_V+bs<=eps->mpd,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"The value of minv must be less than mpd minus blocksize");
71 95 : if (data->plusk == PETSC_DETERMINE) {
72 62 : if (eps->problem_type == EPS_GHIEP || eps->nev+bs>eps->ncv) data->plusk = 0;
73 60 : else data->plusk = 1;
74 : }
75 157 : if (!data->initialsize) data->initialsize = (eps->n && eps->n<10)? 1: 6;
76 95 : initv = data->initialsize;
77 95 : PetscCheck(eps->mpd>=initv,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"The initv parameter has to be less than or equal to mpd");
78 :
79 : /* Change the default sigma to inf if necessary */
80 95 : if (eps->which == EPS_LARGEST_MAGNITUDE || eps->which == EPS_LARGEST_REAL || eps->which == EPS_LARGEST_IMAGINARY) PetscCall(STSetDefaultShift(eps->st,PETSC_MAX_REAL));
81 :
82 : /* Set up preconditioner */
83 95 : PetscCall(STSetUp(eps->st));
84 :
85 : /* Setup problem specification in dvd */
86 95 : PetscCall(STGetNumMatrices(eps->st,&nmat));
87 95 : PetscCall(STGetMatrix(eps->st,0,&A));
88 95 : if (nmat>1) PetscCall(STGetMatrix(eps->st,1,&B));
89 95 : PetscCall(EPSReset_XD(eps));
90 95 : PetscCall(PetscMemzero(dvd,sizeof(dvdDashboard)));
91 95 : dvd->A = A; dvd->B = eps->isgeneralized? B: NULL;
92 95 : ispositive = eps->ispositive;
93 118 : dvd->sA = DVD_MAT_IMPLICIT | (eps->ishermitian? DVD_MAT_HERMITIAN: 0) | ((ispositive && !eps->isgeneralized) ? DVD_MAT_POS_DEF: 0);
94 : /* Assume -eps_hermitian means hermitian-definite in generalized problems */
95 95 : if (!ispositive && !eps->isgeneralized && eps->ishermitian) ispositive = PETSC_TRUE;
96 95 : if (!eps->isgeneralized) dvd->sB = DVD_MAT_IMPLICIT | DVD_MAT_HERMITIAN | DVD_MAT_IDENTITY | DVD_MAT_UNITARY | DVD_MAT_POS_DEF;
97 24 : else dvd->sB = DVD_MAT_IMPLICIT | (eps->ishermitian? DVD_MAT_HERMITIAN: 0) | (ispositive? DVD_MAT_POS_DEF: 0);
98 95 : ipB = (dvd->B && data->ipB && DVD_IS(dvd->sB,DVD_MAT_HERMITIAN))?PETSC_TRUE:PETSC_FALSE;
99 124 : dvd->sEP = ((!eps->isgeneralized || (eps->isgeneralized && ipB))? DVD_EP_STD: 0) | (ispositive? DVD_EP_HERMITIAN: 0) | ((eps->problem_type == EPS_GHIEP && ipB) ? DVD_EP_INDEFINITE : 0);
100 95 : if (data->ipB && !ipB) data->ipB = PETSC_FALSE;
101 95 : dvd->correctXnorm = (dvd->B && (DVD_IS(dvd->sB,DVD_MAT_HERMITIAN)||DVD_IS(dvd->sEP,DVD_EP_INDEFINITE)))?PETSC_TRUE:PETSC_FALSE;
102 95 : dvd->nev = eps->nev;
103 95 : dvd->which = eps->which;
104 95 : dvd->withTarget = PETSC_TRUE;
105 95 : switch (eps->which) {
106 15 : case EPS_TARGET_MAGNITUDE:
107 : case EPS_TARGET_IMAGINARY:
108 15 : dvd->target[0] = target = eps->target;
109 15 : dvd->target[1] = 1.0;
110 15 : break;
111 0 : case EPS_TARGET_REAL:
112 0 : dvd->target[0] = PetscRealPart(target = eps->target);
113 0 : dvd->target[1] = 1.0;
114 0 : break;
115 46 : case EPS_LARGEST_REAL:
116 : case EPS_LARGEST_MAGNITUDE:
117 : case EPS_LARGEST_IMAGINARY: /* TODO: think about this case */
118 46 : dvd->target[0] = 1.0;
119 46 : dvd->target[1] = target = 0.0;
120 46 : break;
121 30 : case EPS_SMALLEST_MAGNITUDE:
122 : case EPS_SMALLEST_REAL:
123 : case EPS_SMALLEST_IMAGINARY: /* TODO: think about this case */
124 30 : dvd->target[0] = target = 0.0;
125 30 : dvd->target[1] = 1.0;
126 30 : break;
127 4 : case EPS_WHICH_USER:
128 4 : PetscCall(STGetShift(eps->st,&target));
129 4 : dvd->target[0] = target;
130 4 : dvd->target[1] = 1.0;
131 4 : break;
132 0 : case EPS_ALL:
133 0 : SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support computing all eigenvalues");
134 0 : default:
135 0 : SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Unsupported value of option 'which'");
136 : }
137 95 : dvd->tol = SlepcDefaultTol(eps->tol);
138 95 : dvd->eps = eps;
139 :
140 : /* Setup the extraction technique */
141 95 : if (!eps->extraction) {
142 86 : if (ipB || ispositive) eps->extraction = EPS_RITZ;
143 : else {
144 17 : switch (eps->which) {
145 6 : case EPS_TARGET_REAL:
146 : case EPS_TARGET_MAGNITUDE:
147 : case EPS_TARGET_IMAGINARY:
148 : case EPS_SMALLEST_MAGNITUDE:
149 : case EPS_SMALLEST_REAL:
150 : case EPS_SMALLEST_IMAGINARY:
151 6 : eps->extraction = EPS_HARMONIC;
152 6 : break;
153 8 : case EPS_LARGEST_REAL:
154 : case EPS_LARGEST_MAGNITUDE:
155 : case EPS_LARGEST_IMAGINARY:
156 8 : eps->extraction = EPS_HARMONIC_LARGEST;
157 8 : break;
158 3 : default:
159 3 : eps->extraction = EPS_RITZ;
160 : }
161 9 : }
162 : }
163 95 : switch (eps->extraction) {
164 : case EPS_RITZ: harm = DVD_HARM_NONE; break;
165 13 : case EPS_HARMONIC: harm = DVD_HARM_RR; break;
166 0 : case EPS_HARMONIC_RELATIVE: harm = DVD_HARM_RRR; break;
167 0 : case EPS_HARMONIC_RIGHT: harm = DVD_HARM_REIGS; break;
168 10 : case EPS_HARMONIC_LARGEST: harm = DVD_HARM_LEIGS; break;
169 0 : default: SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Unsupported extraction type");
170 : }
171 :
172 : /* Setup the type of starting subspace */
173 95 : init = data->krylovstart? DVD_INITV_KRYLOV: DVD_INITV_CLASSIC;
174 :
175 : /* Preconfigure dvd */
176 95 : PetscCall(STGetKSP(eps->st,&ksp));
177 95 : PetscCall(dvd_schm_basic_preconf(dvd,&b,eps->mpd,min_size_V,bs,initv,PetscAbs(eps->nini),data->plusk,harm,ksp,init,eps->trackall,data->ipB,data->doubleexp));
178 :
179 : /* Allocate memory */
180 95 : PetscCall(EPSAllocateSolution(eps,0));
181 :
182 : /* Setup orthogonalization */
183 95 : PetscCall(EPS_SetInnerProduct(eps));
184 95 : if (!(ipB && dvd->B)) PetscCall(BVSetMatrix(eps->V,NULL,PETSC_FALSE));
185 :
186 : /* Configure dvd for a basic GD */
187 95 : PetscCall(dvd_schm_basic_conf(dvd,&b,eps->mpd,min_size_V,bs,initv,PetscAbs(eps->nini),data->plusk,harm,dvd->withTarget,target,ksp,data->fix,init,eps->trackall,data->ipB,data->dynamic,data->doubleexp));
188 95 : PetscFunctionReturn(PETSC_SUCCESS);
189 : }
190 :
191 95 : PetscErrorCode EPSSolve_XD(EPS eps)
192 : {
193 95 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
194 95 : dvdDashboard *d = &data->ddb;
195 95 : PetscInt l,k;
196 :
197 95 : PetscFunctionBegin;
198 95 : PetscCall(PetscCitationsRegister(citation,&cited));
199 : /* Call the starting routines */
200 95 : PetscCall(EPSDavidsonFLCall(d->startList,d));
201 :
202 7946 : while (eps->reason == EPS_CONVERGED_ITERATING) {
203 :
204 : /* Initialize V, if it is needed */
205 7946 : PetscCall(BVGetActiveColumns(d->eps->V,&l,&k));
206 7946 : if (PetscUnlikely(l == k)) PetscCall(d->initV(d));
207 :
208 : /* Find the best approximated eigenpairs in V, X */
209 7946 : PetscCall(d->calcPairs(d));
210 :
211 : /* Test for convergence */
212 7946 : PetscCall((*eps->stopping)(eps,eps->its,eps->max_it,eps->nconv,eps->nev,&eps->reason,eps->stoppingctx));
213 7946 : if (eps->reason != EPS_CONVERGED_ITERATING) break;
214 :
215 : /* Expand the subspace */
216 7851 : PetscCall(d->updateV(d));
217 :
218 : /* Monitor progress */
219 7851 : eps->nconv = d->nconv;
220 7851 : eps->its++;
221 7851 : PetscCall(BVGetActiveColumns(d->eps->V,NULL,&k));
222 7946 : PetscCall(EPSMonitor(eps,eps->its,eps->nconv+d->npreconv,eps->eigr,eps->eigi,eps->errest,PetscMin(k,eps->nev)));
223 : }
224 :
225 : /* Call the ending routines */
226 95 : PetscCall(EPSDavidsonFLCall(d->endList,d));
227 95 : PetscFunctionReturn(PETSC_SUCCESS);
228 : }
229 :
230 165 : PetscErrorCode EPSReset_XD(EPS eps)
231 : {
232 165 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
233 165 : dvdDashboard *dvd = &data->ddb;
234 :
235 165 : PetscFunctionBegin;
236 : /* Call step destructors and destroys the list */
237 165 : PetscCall(EPSDavidsonFLCall(dvd->destroyList,dvd));
238 165 : PetscCall(EPSDavidsonFLDestroy(&dvd->destroyList));
239 165 : PetscCall(EPSDavidsonFLDestroy(&dvd->startList));
240 165 : PetscCall(EPSDavidsonFLDestroy(&dvd->endList));
241 165 : PetscFunctionReturn(PETSC_SUCCESS);
242 : }
243 :
244 4 : PetscErrorCode EPSXDSetKrylovStart_XD(EPS eps,PetscBool krylovstart)
245 : {
246 4 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
247 :
248 4 : PetscFunctionBegin;
249 4 : data->krylovstart = krylovstart;
250 4 : PetscFunctionReturn(PETSC_SUCCESS);
251 : }
252 :
253 60 : PetscErrorCode EPSXDGetKrylovStart_XD(EPS eps,PetscBool *krylovstart)
254 : {
255 60 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
256 :
257 60 : PetscFunctionBegin;
258 60 : *krylovstart = data->krylovstart;
259 60 : PetscFunctionReturn(PETSC_SUCCESS);
260 : }
261 :
262 3 : PetscErrorCode EPSXDSetBlockSize_XD(EPS eps,PetscInt blocksize)
263 : {
264 3 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
265 :
266 3 : PetscFunctionBegin;
267 3 : if (blocksize == PETSC_DEFAULT || blocksize == PETSC_DECIDE) blocksize = 1;
268 3 : PetscCheck(blocksize>0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"Invalid blocksize value, must be >0");
269 3 : if (data->blocksize != blocksize) {
270 3 : data->blocksize = blocksize;
271 3 : eps->state = EPS_STATE_INITIAL;
272 : }
273 3 : PetscFunctionReturn(PETSC_SUCCESS);
274 : }
275 :
276 60 : PetscErrorCode EPSXDGetBlockSize_XD(EPS eps,PetscInt *blocksize)
277 : {
278 60 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
279 :
280 60 : PetscFunctionBegin;
281 60 : *blocksize = data->blocksize;
282 60 : PetscFunctionReturn(PETSC_SUCCESS);
283 : }
284 :
285 3 : PetscErrorCode EPSXDSetRestart_XD(EPS eps,PetscInt minv,PetscInt plusk)
286 : {
287 3 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
288 :
289 3 : PetscFunctionBegin;
290 3 : if (minv == PETSC_DETERMINE) {
291 0 : if (data->minv != 0) eps->state = EPS_STATE_INITIAL;
292 0 : data->minv = 0;
293 3 : } else if (minv != PETSC_CURRENT) {
294 3 : PetscCheck(minv>0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"Invalid minv value, must be >0");
295 3 : if (data->minv != minv) eps->state = EPS_STATE_INITIAL;
296 3 : data->minv = minv;
297 : }
298 3 : if (plusk == PETSC_DETERMINE) {
299 1 : if (data->plusk != PETSC_DETERMINE) eps->state = EPS_STATE_INITIAL;
300 1 : data->plusk = PETSC_DETERMINE;
301 2 : } else if (plusk != PETSC_CURRENT) {
302 2 : PetscCheck(plusk>=0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"Invalid plusk value, must be >0");
303 2 : if (data->plusk != plusk) eps->state = EPS_STATE_INITIAL;
304 2 : data->plusk = plusk;
305 : }
306 3 : PetscFunctionReturn(PETSC_SUCCESS);
307 : }
308 :
309 60 : PetscErrorCode EPSXDGetRestart_XD(EPS eps,PetscInt *minv,PetscInt *plusk)
310 : {
311 60 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
312 :
313 60 : PetscFunctionBegin;
314 60 : if (minv) *minv = data->minv;
315 60 : if (plusk) *plusk = data->plusk;
316 60 : PetscFunctionReturn(PETSC_SUCCESS);
317 : }
318 :
319 59 : PetscErrorCode EPSXDGetInitialSize_XD(EPS eps,PetscInt *initialsize)
320 : {
321 59 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
322 :
323 59 : PetscFunctionBegin;
324 59 : *initialsize = data->initialsize;
325 59 : PetscFunctionReturn(PETSC_SUCCESS);
326 : }
327 :
328 2 : PetscErrorCode EPSXDSetInitialSize_XD(EPS eps,PetscInt initialsize)
329 : {
330 2 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
331 :
332 2 : PetscFunctionBegin;
333 2 : if (initialsize == PETSC_DEFAULT || initialsize == PETSC_DECIDE) initialsize = 0;
334 2 : else PetscCheck(initialsize>0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"Invalid initial size value, must be >0");
335 2 : if (data->initialsize != initialsize) {
336 2 : data->initialsize = initialsize;
337 2 : eps->state = EPS_STATE_INITIAL;
338 : }
339 2 : PetscFunctionReturn(PETSC_SUCCESS);
340 : }
341 :
342 2 : PetscErrorCode EPSXDSetBOrth_XD(EPS eps,PetscBool borth)
343 : {
344 2 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
345 :
346 2 : PetscFunctionBegin;
347 2 : data->ipB = borth;
348 2 : PetscFunctionReturn(PETSC_SUCCESS);
349 : }
350 :
351 60 : PetscErrorCode EPSXDGetBOrth_XD(EPS eps,PetscBool *borth)
352 : {
353 60 : EPS_DAVIDSON *data = (EPS_DAVIDSON*)eps->data;
354 :
355 60 : PetscFunctionBegin;
356 60 : *borth = data->ipB;
357 60 : PetscFunctionReturn(PETSC_SUCCESS);
358 : }
359 :
360 : /*
361 : EPSComputeVectors_XD - Compute eigenvectors from the vectors
362 : provided by the eigensolver. This version is intended for solvers
363 : that provide Schur vectors from the QZ decomposition. Given the partial
364 : Schur decomposition OP*V=V*T, the following steps are performed:
365 : 1) compute eigenvectors of (S,T): S*Z=T*Z*D
366 : 2) compute eigenvectors of OP: X=V*Z
367 : */
368 91 : PetscErrorCode EPSComputeVectors_XD(EPS eps)
369 : {
370 91 : Mat X;
371 91 : PetscBool symm;
372 :
373 91 : PetscFunctionBegin;
374 91 : PetscCall(PetscObjectTypeCompare((PetscObject)eps->ds,DSHEP,&symm));
375 91 : if (symm) PetscFunctionReturn(PETSC_SUCCESS);
376 26 : PetscCall(DSVectors(eps->ds,DS_MAT_X,NULL,NULL));
377 :
378 : /* V <- V * X */
379 26 : PetscCall(DSGetMat(eps->ds,DS_MAT_X,&X));
380 26 : PetscCall(BVSetActiveColumns(eps->V,0,eps->nconv));
381 26 : PetscCall(BVMultInPlace(eps->V,X,0,eps->nconv));
382 26 : PetscCall(DSRestoreMat(eps->ds,DS_MAT_X,&X));
383 26 : PetscFunctionReturn(PETSC_SUCCESS);
384 : }
|