Actual source code: dgmres.c
1: /*
2: Implements deflated GMRES.
3: */
5: #include <../src/ksp/ksp/impls/gmres/dgmres/dgmresimpl.h>
7: PetscLogEvent KSP_DGMRESComputeDeflationData, KSP_DGMRESApplyDeflation;
9: static PetscErrorCode KSPDGMRESGetNewVectors(KSP, PetscInt);
10: static PetscErrorCode KSPDGMRESUpdateHessenberg(KSP, PetscInt, PetscBool, PetscReal *);
11: static PetscErrorCode KSPDGMRESBuildSoln(PetscScalar *, Vec, Vec, KSP, PetscInt);
13: static PetscErrorCode KSPDGMRESSetEigen(KSP ksp, PetscInt nb_eig)
14: {
15: PetscFunctionBegin;
16: PetscTryMethod(ksp, "KSPDGMRESSetEigen_C", (KSP, PetscInt), (ksp, nb_eig));
17: PetscFunctionReturn(PETSC_SUCCESS);
18: }
19: static PetscErrorCode KSPDGMRESSetMaxEigen(KSP ksp, PetscInt max_neig)
20: {
21: PetscFunctionBegin;
22: PetscTryMethod(ksp, "KSPDGMRESSetMaxEigen_C", (KSP, PetscInt), (ksp, max_neig));
23: PetscFunctionReturn(PETSC_SUCCESS);
24: }
25: static PetscErrorCode KSPDGMRESComputeSchurForm(KSP ksp, PetscInt *neig)
26: {
27: PetscFunctionBegin;
28: PetscUseMethod(ksp, "KSPDGMRESComputeSchurForm_C", (KSP, PetscInt *), (ksp, neig));
29: PetscFunctionReturn(PETSC_SUCCESS);
30: }
31: static PetscErrorCode KSPDGMRESComputeDeflationData(KSP ksp, PetscInt *curneigh)
32: {
33: PetscFunctionBegin;
34: PetscUseMethod(ksp, "KSPDGMRESComputeDeflationData_C", (KSP, PetscInt *), (ksp, curneigh));
35: PetscFunctionReturn(PETSC_SUCCESS);
36: }
37: static PetscErrorCode KSPDGMRESApplyDeflation(KSP ksp, Vec x, Vec y)
38: {
39: PetscFunctionBegin;
40: PetscUseMethod(ksp, "KSPDGMRESApplyDeflation_C", (KSP, Vec, Vec), (ksp, x, y));
41: PetscFunctionReturn(PETSC_SUCCESS);
42: }
44: static PetscErrorCode KSPDGMRESImproveEig(KSP ksp, PetscInt neig)
45: {
46: PetscFunctionBegin;
47: PetscUseMethod(ksp, "KSPDGMRESImproveEig_C", (KSP, PetscInt), (ksp, neig));
48: PetscFunctionReturn(PETSC_SUCCESS);
49: }
51: static PetscErrorCode KSPSetUp_DGMRES(KSP ksp)
52: {
53: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
54: PetscInt neig = dgmres->neig + EIG_OFFSET;
55: PetscInt max_k = dgmres->max_k + 1;
57: PetscFunctionBegin;
58: PetscCall(KSPSetUp_GMRES(ksp));
59: if (!dgmres->neig) PetscFunctionReturn(PETSC_SUCCESS);
61: /* Allocate workspace for the Schur vectors*/
62: PetscCall(PetscMalloc1(neig * max_k, &SR));
63: dgmres->wr = NULL;
64: dgmres->wi = NULL;
65: dgmres->perm = NULL;
66: dgmres->modul = NULL;
67: dgmres->Q = NULL;
68: dgmres->Z = NULL;
70: UU = NULL;
71: XX = NULL;
72: MX = NULL;
73: AUU = NULL;
74: XMX = NULL;
75: XMU = NULL;
76: UMX = NULL;
77: AUAU = NULL;
78: TT = NULL;
79: TTF = NULL;
80: INVP = NULL;
81: X1 = NULL;
82: X2 = NULL;
83: MU = NULL;
84: PetscFunctionReturn(PETSC_SUCCESS);
85: }
87: /*
88: Run GMRES, possibly with restart. Return residual history if requested.
89: input parameters:
91: . gmres - structure containing parameters and work areas
93: output parameters:
94: . nres - residuals (from preconditioned system) at each step.
95: If restarting, consider passing nres+it. If null,
96: ignored
97: . itcount - number of iterations used. nres[0] to nres[itcount]
98: are defined. If null, ignored.
100: Notes:
101: On entry, the value in vector VEC_VV(0) should be the initial residual
102: (this allows shortcuts where the initial preconditioned residual is 0).
103: */
104: static PetscErrorCode KSPDGMRESCycle(PetscInt *itcount, KSP ksp)
105: {
106: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
107: PetscReal res_norm, res, hapbnd, tt;
108: PetscInt it = 0;
109: PetscInt max_k = dgmres->max_k;
110: PetscBool hapend = PETSC_FALSE;
111: PetscReal res_old;
112: PetscInt test = 0;
114: PetscFunctionBegin;
115: PetscCall(VecNormalize(VEC_VV(0), &res_norm));
116: KSPCheckNorm(ksp, res_norm);
117: res = res_norm;
118: *GRS(0) = res_norm;
120: /* check for the convergence */
121: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
122: if (ksp->normtype != KSP_NORM_NONE) ksp->rnorm = res;
123: else ksp->rnorm = 0.0;
124: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
125: dgmres->it = (it - 1);
126: PetscCall(KSPLogResidualHistory(ksp, ksp->rnorm));
127: PetscCall(KSPMonitor(ksp, ksp->its, ksp->rnorm));
128: if (!res) {
129: if (itcount) *itcount = 0;
130: ksp->reason = KSP_CONVERGED_ATOL;
131: PetscCall(PetscInfo(ksp, "Converged due to zero residual norm on entry\n"));
132: PetscFunctionReturn(PETSC_SUCCESS);
133: }
134: /* record the residual norm to test if deflation is needed */
135: res_old = res;
137: PetscCall((*ksp->converged)(ksp, ksp->its, ksp->rnorm, &ksp->reason, ksp->cnvP));
138: while (!ksp->reason && it < max_k && ksp->its < ksp->max_it) {
139: if (it) {
140: PetscCall(KSPLogResidualHistory(ksp, ksp->rnorm));
141: PetscCall(KSPMonitor(ksp, ksp->its, ksp->rnorm));
142: }
143: dgmres->it = (it - 1);
144: if (dgmres->vv_allocated <= it + VEC_OFFSET + 1) PetscCall(KSPDGMRESGetNewVectors(ksp, it + 1));
145: if (dgmres->r > 0) {
146: if (ksp->pc_side == PC_LEFT) {
147: /* Apply the first preconditioner */
148: PetscCall(KSP_PCApplyBAorAB(ksp, VEC_VV(it), VEC_TEMP, VEC_TEMP_MATOP));
149: /* Then apply Deflation as a preconditioner */
150: PetscCall(KSPDGMRESApplyDeflation(ksp, VEC_TEMP, VEC_VV(1 + it)));
151: } else if (ksp->pc_side == PC_RIGHT) {
152: PetscCall(KSPDGMRESApplyDeflation(ksp, VEC_VV(it), VEC_TEMP));
153: PetscCall(KSP_PCApplyBAorAB(ksp, VEC_TEMP, VEC_VV(1 + it), VEC_TEMP_MATOP));
154: }
155: } else {
156: PetscCall(KSP_PCApplyBAorAB(ksp, VEC_VV(it), VEC_VV(1 + it), VEC_TEMP_MATOP));
157: }
158: dgmres->matvecs += 1;
159: /* update Hessenberg matrix and do Gram-Schmidt */
160: PetscCall((*ksp->orthog)(ksp, &VEC_VV(0), it + 1, NULL, HH(0, it)));
161: PetscCall(PetscArraycpy(HES(0, it), HH(0, it), it + 1));
163: /* vv(i+1) . vv(i+1) */
164: PetscCall(VecNormalize(VEC_VV(it + 1), &tt));
165: /* save the magnitude */
166: *HH(it + 1, it) = tt;
167: *HES(it + 1, it) = tt;
169: /* check for the happy breakdown */
170: hapbnd = PetscAbsScalar(tt / *GRS(it));
171: if (hapbnd > dgmres->haptol) hapbnd = dgmres->haptol;
172: if (tt < hapbnd) {
173: PetscCall(PetscInfo(ksp, "Detected happy breakdown, current hapbnd = %g tt = %g\n", (double)hapbnd, (double)tt));
174: hapend = PETSC_TRUE;
175: }
176: PetscCall(KSPDGMRESUpdateHessenberg(ksp, it, hapend, &res));
178: it++;
179: dgmres->it = (it - 1); /* For converged */
180: ksp->its++;
181: if (ksp->normtype != KSP_NORM_NONE) ksp->rnorm = res;
182: else ksp->rnorm = 0.0;
183: if (ksp->reason) break;
185: PetscCall((*ksp->converged)(ksp, ksp->its, ksp->rnorm, &ksp->reason, ksp->cnvP));
187: /* Catch error in happy breakdown and signal convergence and break from loop */
188: if (hapend) {
189: if (!ksp->reason) {
190: PetscCheck(!ksp->errorifnotconverged, PetscObjectComm((PetscObject)ksp), PETSC_ERR_NOT_CONVERGED, "Reached happy break down, but convergence was not indicated. Residual norm = %g", (double)res);
191: ksp->reason = KSP_DIVERGED_BREAKDOWN;
192: break;
193: }
194: }
195: }
197: if (itcount) *itcount = it;
199: /*
200: Down here we have to solve for the "best" coefficients of the Krylov
201: columns, add the solution values together, and possibly unwind the
202: preconditioning from the solution
203: */
204: /* Form the solution (or the solution so far) */
205: PetscCall(KSPDGMRESBuildSoln(GRS(0), ksp->vec_sol, ksp->vec_sol, ksp, it - 1));
207: /* Monitor if we know that we will not return for a restart */
208: if (ksp->reason == KSP_CONVERGED_ITERATING && ksp->its >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
209: if (it && ksp->reason) {
210: PetscCall(KSPLogResidualHistory(ksp, ksp->rnorm));
211: PetscCall(KSPMonitor(ksp, ksp->its, ksp->rnorm));
212: }
214: /* Compute data for the deflation to be used during the next restart */
215: if (!ksp->reason && ksp->its < ksp->max_it) {
216: test = max_k * PetscLogReal(ksp->rtol / res) / PetscLogReal(res / res_old);
217: /* Compute data for the deflation if the residual rtol will not be reached in the remaining number of steps allowed */
218: if ((test > dgmres->smv * (ksp->max_it - ksp->its)) || dgmres->force) PetscCall(KSPDGMRESComputeDeflationData(ksp, NULL));
219: }
220: PetscFunctionReturn(PETSC_SUCCESS);
221: }
223: static PetscErrorCode KSPSolve_DGMRES(KSP ksp)
224: {
225: PetscInt i, its = 0, itcount;
226: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
227: PetscBool guess_zero = ksp->guess_zero;
229: PetscFunctionBegin;
230: PetscCheck(!ksp->calc_sings || dgmres->Rsvd, PetscObjectComm((PetscObject)ksp), PETSC_ERR_ORDER, "Must call KSPSetComputeSingularValues() before KSPSetUp() is called");
232: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
233: ksp->its = 0;
234: dgmres->matvecs = 0;
235: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
237: itcount = 0;
238: while (!ksp->reason) {
239: PetscCall(KSPInitialResidual(ksp, ksp->vec_sol, VEC_TEMP, VEC_TEMP_MATOP, VEC_VV(0), ksp->vec_rhs));
240: if (ksp->pc_side == PC_LEFT) {
241: dgmres->matvecs += 1;
242: if (dgmres->r > 0) {
243: PetscCall(KSPDGMRESApplyDeflation(ksp, VEC_VV(0), VEC_TEMP));
244: PetscCall(VecCopy(VEC_TEMP, VEC_VV(0)));
245: }
246: }
248: PetscCall(KSPDGMRESCycle(&its, ksp));
249: itcount += its;
250: if (itcount >= ksp->max_it) {
251: if (!ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
252: break;
253: }
254: ksp->guess_zero = PETSC_FALSE; /* every future call to KSPInitialResidual() will have nonzero guess */
255: }
256: ksp->guess_zero = guess_zero; /* restore if user provided nonzero initial guess */
258: for (i = 0; i < dgmres->r; i++) PetscCall(VecViewFromOptions(UU[i], (PetscObject)ksp, "-ksp_dgmres_view_deflation_vecs"));
259: PetscFunctionReturn(PETSC_SUCCESS);
260: }
262: static PetscErrorCode KSPDestroy_DGMRES(KSP ksp)
263: {
264: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
265: PetscInt neig1 = dgmres->neig + EIG_OFFSET;
266: PetscInt max_neig = dgmres->max_neig;
268: PetscFunctionBegin;
269: if (dgmres->r) {
270: PetscCall(VecDestroyVecs(max_neig, &UU));
271: PetscCall(VecDestroyVecs(max_neig, &MU));
272: if (XX) {
273: PetscCall(VecDestroyVecs(neig1, &XX));
274: PetscCall(VecDestroyVecs(neig1, &MX));
275: }
276: PetscCall(PetscFree(TT));
277: PetscCall(PetscFree(TTF));
278: PetscCall(PetscFree(INVP));
279: PetscCall(PetscFree(XMX));
280: PetscCall(PetscFree(UMX));
281: PetscCall(PetscFree(XMU));
282: PetscCall(PetscFree(X1));
283: PetscCall(PetscFree(X2));
284: PetscCall(PetscFree(dgmres->work));
285: PetscCall(PetscFree(dgmres->iwork));
286: PetscCall(PetscFree(dgmres->wr));
287: PetscCall(PetscFree(dgmres->wi));
288: PetscCall(PetscFree(dgmres->modul));
289: PetscCall(PetscFree(dgmres->Q));
290: PetscCall(PetscFree(ORTH));
291: PetscCall(PetscFree(AUAU));
292: PetscCall(PetscFree(AUU));
293: PetscCall(PetscFree(SR2));
294: }
295: PetscCall(PetscFree(SR));
296: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESSetEigen_C", NULL));
297: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESSetMaxEigen_C", NULL));
298: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESSetRatio_C", NULL));
299: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESForce_C", NULL));
300: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESComputeSchurForm_C", NULL));
301: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESComputeDeflationData_C", NULL));
302: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESApplyDeflation_C", NULL));
303: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESImproveEig_C", NULL));
304: PetscCall(KSPDestroy_GMRES(ksp));
305: PetscFunctionReturn(PETSC_SUCCESS);
306: }
308: /*
309: KSPDGMRESBuildSoln - create the solution from the starting vector and the
310: current iterates.
312: Input parameters:
313: nrs - work area of size it + 1.
314: vs - index of initial guess
315: vdest - index of result. Note that vs may == vdest (replace
316: guess with the solution).
318: This is an internal routine that knows about the GMRES internals.
319: */
320: static PetscErrorCode KSPDGMRESBuildSoln(PetscScalar *nrs, Vec vs, Vec vdest, KSP ksp, PetscInt it)
321: {
322: PetscScalar tt;
323: PetscInt ii, k, j;
324: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
326: /* Solve for solution vector that minimizes the residual */
328: PetscFunctionBegin;
329: /* If it is < 0, no gmres steps have been performed */
330: if (it < 0) {
331: PetscCall(VecCopy(vs, vdest)); /* VecCopy() is smart, exists immediately if vguess == vdest */
332: PetscFunctionReturn(PETSC_SUCCESS);
333: }
334: PetscCheck(*HH(it, it) != 0.0, PetscObjectComm((PetscObject)ksp), PETSC_ERR_CONV_FAILED, "Likely your matrix is the zero operator. HH(it,it) is identically zero; it = %" PetscInt_FMT " GRS(it) = %g", it, (double)PetscAbsScalar(*GRS(it)));
335: if (*HH(it, it) != 0.0) nrs[it] = *GRS(it) / *HH(it, it);
336: else nrs[it] = 0.0;
338: for (ii = 1; ii <= it; ii++) {
339: k = it - ii;
340: tt = *GRS(k);
341: for (j = k + 1; j <= it; j++) tt = tt - *HH(k, j) * nrs[j];
342: PetscCheck(*HH(k, k) != 0.0, PetscObjectComm((PetscObject)ksp), PETSC_ERR_CONV_FAILED, "Likely your matrix is singular. HH(k,k) is identically zero; it = %" PetscInt_FMT " k = %" PetscInt_FMT, it, k);
343: nrs[k] = tt / *HH(k, k);
344: }
346: /* Accumulate the correction to the solution of the preconditioned problem in TEMP */
347: PetscCall(VecMAXPBY(VEC_TEMP, it + 1, nrs, 0, &VEC_VV(0)));
349: /* Apply deflation */
350: if (ksp->pc_side == PC_RIGHT && dgmres->r > 0) {
351: PetscCall(KSPDGMRESApplyDeflation(ksp, VEC_TEMP, VEC_TEMP_MATOP));
352: PetscCall(VecCopy(VEC_TEMP_MATOP, VEC_TEMP));
353: }
354: PetscCall(KSPUnwindPreconditioner(ksp, VEC_TEMP, VEC_TEMP_MATOP));
356: /* add solution to previous solution */
357: if (vdest != vs) PetscCall(VecCopy(vs, vdest));
358: PetscCall(VecAXPY(vdest, 1.0, VEC_TEMP));
359: PetscFunctionReturn(PETSC_SUCCESS);
360: }
362: /*
363: Do the scalar work for the orthogonalization. Return new residual norm.
364: */
365: static PetscErrorCode KSPDGMRESUpdateHessenberg(KSP ksp, PetscInt it, PetscBool hapend, PetscReal *res)
366: {
367: PetscScalar *hh, *cc, *ss, tt;
368: PetscInt j;
369: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
371: PetscFunctionBegin;
372: hh = HH(0, it);
373: cc = CC(0);
374: ss = SS(0);
376: /* Apply all the previously computed plane rotations to the new column
377: of the Hessenberg matrix */
378: for (j = 1; j <= it; j++) {
379: tt = *hh;
380: *hh = PetscConj(*cc) * tt + *ss * *(hh + 1);
381: hh++;
382: *hh = *cc++ * *hh - (*ss++ * tt);
383: }
385: /*
386: compute the new plane rotation, and apply it to:
387: 1) the right-hand side of the Hessenberg system
388: 2) the new column of the Hessenberg matrix
389: thus obtaining the updated value of the residual
390: */
391: if (!hapend) {
392: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh + 1)) * *(hh + 1));
393: if (tt == 0.0) {
394: ksp->reason = KSP_DIVERGED_NULL;
395: PetscFunctionReturn(PETSC_SUCCESS);
396: }
397: *cc = *hh / tt;
398: *ss = *(hh + 1) / tt;
399: *GRS(it + 1) = -(*ss * *GRS(it));
400: *GRS(it) = PetscConj(*cc) * *GRS(it);
401: *hh = PetscConj(*cc) * *hh + *ss * *(hh + 1);
402: *res = PetscAbsScalar(*GRS(it + 1));
403: } else {
404: /* happy breakdown: HH(it+1, it) = 0, therefore we don't need to apply
405: another rotation matrix (so RH doesn't change). The new residual is
406: always the new sine term times the residual from last time (GRS(it)),
407: but now the new sine rotation would be zero...so the residual should
408: be zero...so we will multiply "zero" by the last residual. This might
409: not be exactly what we want to do here -could just return "zero". */
410: *res = 0.0;
411: }
412: PetscFunctionReturn(PETSC_SUCCESS);
413: }
415: /*
416: Allocates more work vectors, starting from VEC_VV(it).
417: */
418: static PetscErrorCode KSPDGMRESGetNewVectors(KSP ksp, PetscInt it)
419: {
420: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
421: PetscInt nwork = dgmres->nwork_alloc, k, nalloc;
423: PetscFunctionBegin;
424: nalloc = PetscMin(ksp->max_it, dgmres->delta_allocate);
425: /* Adjust the number to allocate to make sure that we don't exceed the
426: number of available slots */
427: if (it + VEC_OFFSET + nalloc >= dgmres->vecs_allocated) nalloc = dgmres->vecs_allocated - it - VEC_OFFSET;
428: if (!nalloc) PetscFunctionReturn(PETSC_SUCCESS);
430: dgmres->vv_allocated += nalloc;
432: PetscCall(KSPCreateVecs(ksp, nalloc, &dgmres->user_work[nwork], 0, NULL));
434: dgmres->mwork_alloc[nwork] = nalloc;
435: for (k = 0; k < nalloc; k++) dgmres->vecs[it + VEC_OFFSET + k] = dgmres->user_work[nwork][k];
436: dgmres->nwork_alloc++;
437: PetscFunctionReturn(PETSC_SUCCESS);
438: }
440: static PetscErrorCode KSPBuildSolution_DGMRES(KSP ksp, Vec ptr, Vec *result)
441: {
442: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
444: PetscFunctionBegin;
445: if (!ptr) {
446: if (!dgmres->sol_temp) PetscCall(VecDuplicate(ksp->vec_sol, &dgmres->sol_temp));
447: ptr = dgmres->sol_temp;
448: }
449: if (!dgmres->nrs) {
450: /* allocate the work area */
451: PetscCall(PetscMalloc1(dgmres->max_k, &dgmres->nrs));
452: }
453: PetscCall(KSPDGMRESBuildSoln(dgmres->nrs, ksp->vec_sol, ptr, ksp, dgmres->it));
454: if (result) *result = ptr;
455: PetscFunctionReturn(PETSC_SUCCESS);
456: }
458: static PetscErrorCode KSPView_DGMRES(KSP ksp, PetscViewer viewer)
459: {
460: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
461: PetscBool isascii, isharmonic;
463: PetscFunctionBegin;
464: PetscCall(KSPView_GMRES(ksp, viewer));
465: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
466: if (isascii) {
467: PetscCall(PetscViewerASCIIPrintf(viewer, " Adaptive strategy is used: %s\n", PetscBools[dgmres->force]));
468: PetscCall(PetscOptionsHasName(((PetscObject)ksp)->options, ((PetscObject)ksp)->prefix, "-ksp_dgmres_harmonic_ritz", &isharmonic));
469: if (isharmonic) {
470: PetscCall(PetscViewerASCIIPrintf(viewer, " Frequency of extracted eigenvalues = %" PetscInt_FMT " using Harmonic Ritz values \n", dgmres->neig));
471: } else {
472: PetscCall(PetscViewerASCIIPrintf(viewer, " Frequency of extracted eigenvalues = %" PetscInt_FMT " using Ritz values \n", dgmres->neig));
473: }
474: PetscCall(PetscViewerASCIIPrintf(viewer, " Total number of extracted eigenvalues = %" PetscInt_FMT "\n", dgmres->r));
475: PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum number of eigenvalues set to be extracted = %" PetscInt_FMT "\n", dgmres->max_neig));
476: PetscCall(PetscViewerASCIIPrintf(viewer, " relaxation parameter for the adaptive strategy(smv) = %g\n", (double)dgmres->smv));
477: PetscCall(PetscViewerASCIIPrintf(viewer, " Number of matvecs : %" PetscInt_FMT "\n", dgmres->matvecs));
478: }
479: PetscFunctionReturn(PETSC_SUCCESS);
480: }
482: static PetscErrorCode KSPDGMRESSetEigen_DGMRES(KSP ksp, PetscInt neig)
483: {
484: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
486: PetscFunctionBegin;
487: PetscCheck(neig >= 0 && neig <= dgmres->max_k, PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_OUTOFRANGE, "The value of neig must be positive and less than the restart value ");
488: dgmres->neig = neig;
489: PetscFunctionReturn(PETSC_SUCCESS);
490: }
492: static PetscErrorCode KSPDGMRESSetMaxEigen_DGMRES(KSP ksp, PetscInt max_neig)
493: {
494: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
496: PetscFunctionBegin;
497: PetscCheck(max_neig >= 0 && max_neig <= dgmres->max_k, PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_OUTOFRANGE, "The value of max_neig must be positive and less than the restart value ");
498: dgmres->max_neig = max_neig;
499: PetscFunctionReturn(PETSC_SUCCESS);
500: }
502: static PetscErrorCode KSPDGMRESSetRatio_DGMRES(KSP ksp, PetscReal ratio)
503: {
504: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
506: PetscFunctionBegin;
507: PetscCheck(ratio > 0, PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_OUTOFRANGE, "The relaxation parameter value must be positive");
508: dgmres->smv = ratio;
509: PetscFunctionReturn(PETSC_SUCCESS);
510: }
512: static PetscErrorCode KSPDGMRESForce_DGMRES(KSP ksp, PetscBool force)
513: {
514: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
516: PetscFunctionBegin;
517: dgmres->force = force;
518: PetscFunctionReturn(PETSC_SUCCESS);
519: }
521: static PetscErrorCode KSPSetFromOptions_DGMRES(KSP ksp, PetscOptionItems PetscOptionsObject)
522: {
523: PetscInt neig;
524: PetscInt max_neig;
525: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
526: PetscBool flg;
528: PetscFunctionBegin;
529: PetscCall(KSPSetFromOptions_GMRES(ksp, PetscOptionsObject));
530: PetscOptionsHeadBegin(PetscOptionsObject, "KSP DGMRES Options");
531: PetscCall(PetscOptionsInt("-ksp_dgmres_eigen", "Number of smallest eigenvalues to extract at each restart", "KSPDGMRESSetEigen", dgmres->neig, &neig, &flg));
532: if (flg) PetscCall(KSPDGMRESSetEigen(ksp, neig));
533: PetscCall(PetscOptionsInt("-ksp_dgmres_max_eigen", "Maximum Number of smallest eigenvalues to extract ", "KSPDGMRESSetMaxEigen", dgmres->max_neig, &max_neig, &flg));
534: if (flg) PetscCall(KSPDGMRESSetMaxEigen(ksp, max_neig));
535: PetscCall(PetscOptionsReal("-ksp_dgmres_ratio", "Relaxation parameter for the smaller number of matrix-vectors product allowed", "KSPDGMRESSetRatio", dgmres->smv, &dgmres->smv, NULL));
536: PetscCall(PetscOptionsBool("-ksp_dgmres_improve", "Improve the computation of eigenvalues by solving a new generalized eigenvalue problem (experimental - not stable at this time)", NULL, dgmres->improve, &dgmres->improve, NULL));
537: PetscCall(PetscOptionsBool("-ksp_dgmres_force", "Sets DGMRES always at restart active, i.e do not use the adaptive strategy", "KSPDGMRESForce", dgmres->force, &dgmres->force, NULL));
538: PetscOptionsHeadEnd();
539: PetscFunctionReturn(PETSC_SUCCESS);
540: }
542: static PetscErrorCode KSPDGMRESComputeDeflationData_DGMRES(KSP ksp, PetscInt *ExtrNeig)
543: {
544: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
545: PetscInt i, j, k;
546: PetscBLASInt nr, bmax;
547: PetscInt r = dgmres->r;
548: PetscInt neig; /* number of eigenvalues to extract at each restart */
549: PetscInt neig1 = dgmres->neig + EIG_OFFSET; /* max number of eig that can be extracted at each restart */
550: PetscInt max_neig = dgmres->max_neig; /* Max number of eigenvalues to extract during the iterative process */
551: PetscInt N = dgmres->max_k + 1;
552: PetscInt n = dgmres->it + 1;
553: PetscReal alpha;
555: PetscFunctionBegin;
556: PetscCall(PetscLogEventBegin(KSP_DGMRESComputeDeflationData, ksp, 0, 0, 0));
557: if (dgmres->neig == 0 || (max_neig < (r + neig1) && !dgmres->improve)) {
558: PetscCall(PetscLogEventEnd(KSP_DGMRESComputeDeflationData, ksp, 0, 0, 0));
559: PetscFunctionReturn(PETSC_SUCCESS);
560: }
562: PetscCall(KSPDGMRESComputeSchurForm(ksp, &neig));
563: /* Form the extended Schur vectors X=VV*Sr */
564: if (!XX) PetscCall(VecDuplicateVecs(VEC_VV(0), neig1, &XX));
565: for (j = 0; j < neig; j++) PetscCall(VecMAXPBY(XX[j], n, &SR[j * N], 0, &VEC_VV(0)));
567: /* Orthogonalize X against U */
568: if (!ORTH) PetscCall(PetscMalloc1(max_neig, &ORTH));
569: if (r > 0) {
570: /* modified Gram-Schmidt */
571: for (j = 0; j < neig; j++) {
572: for (i = 0; i < r; i++) {
573: /* First, compute U'*X[j] */
574: PetscCall(VecDot(XX[j], UU[i], &alpha));
575: /* Then, compute X(j)=X(j)-U*U'*X(j) */
576: PetscCall(VecAXPY(XX[j], -alpha, UU[i]));
577: }
578: }
579: }
580: /* Compute MX = M^{-1}*A*X */
581: if (!MX) PetscCall(VecDuplicateVecs(VEC_VV(0), neig1, &MX));
582: for (j = 0; j < neig; j++) PetscCall(KSP_PCApplyBAorAB(ksp, XX[j], MX[j], VEC_TEMP_MATOP));
583: dgmres->matvecs += neig;
585: if ((r + neig1) > max_neig && dgmres->improve) { /* Improve the approximate eigenvectors in X by solving a new generalized eigenvalue -- expensive to do this */
586: PetscCall(KSPDGMRESImproveEig(ksp, neig));
587: PetscCall(PetscLogEventEnd(KSP_DGMRESComputeDeflationData, ksp, 0, 0, 0));
588: PetscFunctionReturn(PETSC_SUCCESS); /* We return here since data for M have been improved in KSPDGMRESImproveEig()*/
589: }
591: /* Compute XMX = X'*M^{-1}*A*X -- size (neig, neig) */
592: if (!XMX) PetscCall(PetscMalloc1(neig1 * neig1, &XMX));
593: for (j = 0; j < neig; j++) PetscCall(VecMDot(MX[j], neig, XX, &XMX[j * neig1]));
595: if (r > 0) {
596: /* Compute UMX = U'*M^{-1}*A*X -- size (r, neig) */
597: if (!UMX) PetscCall(PetscMalloc1(max_neig * neig1, &UMX));
598: for (j = 0; j < neig; j++) PetscCall(VecMDot(MX[j], r, UU, &UMX[j * max_neig]));
599: /* Compute XMU = X'*M^{-1}*A*U -- size(neig, r) */
600: if (!XMU) PetscCall(PetscMalloc1(max_neig * neig1, &XMU));
601: for (j = 0; j < r; j++) PetscCall(VecMDot(MU[j], neig, XX, &XMU[j * neig1]));
602: }
604: /* Form the new matrix T = [T UMX; XMU XMX]; */
605: if (!TT) PetscCall(PetscMalloc1(max_neig * max_neig, &TT));
606: if (r > 0) {
607: /* Add XMU to T */
608: for (j = 0; j < r; j++) PetscCall(PetscArraycpy(&TT[max_neig * j + r], &XMU[neig1 * j], neig));
609: /* Add [UMX; XMX] to T */
610: for (j = 0; j < neig; j++) {
611: k = r + j;
612: PetscCall(PetscArraycpy(&TT[max_neig * k], &UMX[max_neig * j], r));
613: PetscCall(PetscArraycpy(&TT[max_neig * k + r], &XMX[neig1 * j], neig));
614: }
615: } else { /* Add XMX to T */
616: for (j = 0; j < neig; j++) PetscCall(PetscArraycpy(&TT[max_neig * j], &XMX[neig1 * j], neig));
617: }
619: dgmres->r += neig;
620: r = dgmres->r;
621: PetscCall(PetscBLASIntCast(r, &nr));
622: /*LU Factorize T with Lapack xgetrf routine */
624: PetscCall(PetscBLASIntCast(max_neig, &bmax));
625: if (!TTF) PetscCall(PetscMalloc1(bmax * bmax, &TTF));
626: PetscCall(PetscArraycpy(TTF, TT, bmax * r));
627: if (!INVP) PetscCall(PetscMalloc1(bmax, &INVP));
628: PetscCallLAPACKInfo("LAPACKgetrf", LAPACKgetrf_(&nr, &nr, TTF, &bmax, INVP, &info));
630: /* Save X in U and MX in MU for the next cycles and increase the size of the invariant subspace */
631: if (!UU) {
632: PetscCall(VecDuplicateVecs(VEC_VV(0), max_neig, &UU));
633: PetscCall(VecDuplicateVecs(VEC_VV(0), max_neig, &MU));
634: }
635: for (j = 0; j < neig; j++) {
636: PetscCall(VecCopy(XX[j], UU[r - neig + j]));
637: PetscCall(VecCopy(MX[j], MU[r - neig + j]));
638: }
639: PetscCall(PetscLogEventEnd(KSP_DGMRESComputeDeflationData, ksp, 0, 0, 0));
640: PetscFunctionReturn(PETSC_SUCCESS);
641: }
643: static PetscErrorCode KSPDGMRESComputeSchurForm_DGMRES(KSP ksp, PetscInt *neig)
644: {
645: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
646: PetscInt N = dgmres->max_k + 1, n = dgmres->it + 1;
647: PetscBLASInt bn;
648: PetscReal *A;
649: PetscBLASInt ihi;
650: PetscBLASInt ldA = 0; /* leading dimension of A */
651: PetscBLASInt ldQ; /* leading dimension of Q */
652: PetscReal *Q; /* orthogonal matrix of (left) Schur vectors */
653: PetscReal *work; /* working vector */
654: PetscBLASInt lwork; /* size of the working vector */
655: PetscInt *perm; /* Permutation vector to sort eigenvalues */
656: PetscInt i, j;
657: PetscBLASInt NbrEig; /* Number of eigenvalues really extracted */
658: PetscReal *wr, *wi, *modul; /* Real and imaginary part and modulus of the eigenvalues of A */
659: PetscBLASInt *select;
660: PetscBLASInt *iwork;
661: PetscBLASInt liwork;
662: PetscScalar *Ht; /* Transpose of the Hessenberg matrix */
663: PetscScalar *t; /* Store the result of the solution of H^T*t=h_{m+1,m}e_m */
664: PetscBLASInt *ipiv; /* Permutation vector to be used in LAPACK */
665: PetscBool flag; /* determine whether to use Ritz vectors or harmonic Ritz vectors */
667: PetscFunctionBegin;
668: PetscCall(PetscBLASIntCast(n, &bn));
669: PetscCall(PetscBLASIntCast(N, &ldA));
670: ihi = ldQ = bn;
671: PetscCall(PetscBLASIntCast(5 * N, &lwork));
673: PetscCheck(!PetscDefined(USE_COMPLEX), PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "No support for complex numbers.");
675: PetscCall(PetscMalloc1(ldA * ldA, &A));
676: PetscCall(PetscMalloc1(ldQ * n, &Q));
677: PetscCall(PetscMalloc1(lwork, &work));
678: if (!dgmres->wr) {
679: PetscCall(PetscMalloc1(n, &dgmres->wr));
680: PetscCall(PetscMalloc1(n, &dgmres->wi));
681: }
682: wr = dgmres->wr;
683: wi = dgmres->wi;
684: PetscCall(PetscMalloc1(n, &modul));
685: PetscCall(PetscMalloc1(n, &perm));
686: /* copy the Hessenberg matrix to work space */
687: PetscCall(PetscArraycpy(A, dgmres->hes_origin, ldA * ldA));
688: PetscCall(PetscOptionsHasName(((PetscObject)ksp)->options, ((PetscObject)ksp)->prefix, "-ksp_dgmres_harmonic_ritz", &flag));
689: if (flag) {
690: /* Compute the matrix H + H^{-T}*h^2_{m+1,m}e_m*e_m^T */
691: /* Transpose the Hessenberg matrix */
692: PetscCall(PetscMalloc1(bn * bn, &Ht));
693: for (i = 0; i < bn; i++) {
694: for (j = 0; j < bn; j++) Ht[i * bn + j] = dgmres->hes_origin[j * ldA + i];
695: }
697: /* Solve the system H^T*t = h_{m+1,m}e_m */
698: PetscCall(PetscCalloc1(bn, &t));
699: t[bn - 1] = dgmres->hes_origin[(bn - 1) * ldA + bn]; /* Pick the last element H(m+1,m) */
700: PetscCall(PetscMalloc1(bn, &ipiv));
701: /* Call the LAPACK routine dgesv to solve the system Ht^-1 * t */
702: {
703: PetscBLASInt nrhs = 1;
704: PetscCallLAPACKInfo("LAPACKgesv", LAPACKgesv_(&bn, &nrhs, Ht, &bn, ipiv, t, &bn, &info));
705: }
706: /* Now form H + H^{-T}*h^2_{m+1,m}e_m*e_m^T */
707: for (i = 0; i < bn; i++) A[(bn - 1) * bn + i] += t[i];
708: PetscCall(PetscFree(t));
709: PetscCall(PetscFree(Ht));
710: }
711: /* Compute eigenvalues with the Schur form */
712: {
713: PetscBLASInt ilo = 1;
714: PetscCallLAPACKInfo("LAPACKhseqr", LAPACKhseqr_("S", "I", &bn, &ilo, &ihi, A, &ldA, wr, wi, Q, &ldQ, work, &lwork, &info));
715: }
716: PetscCall(PetscFree(work));
718: /* sort the eigenvalues */
719: for (i = 0; i < n; i++) modul[i] = PetscSqrtReal(wr[i] * wr[i] + wi[i] * wi[i]);
720: for (i = 0; i < n; i++) perm[i] = i;
722: PetscCall(PetscSortRealWithPermutation(n, modul, perm));
723: /* save the complex modulus of the largest eigenvalue in magnitude */
724: if (dgmres->lambdaN < modul[perm[n - 1]]) dgmres->lambdaN = modul[perm[n - 1]];
725: /* count the number of extracted eigenvalues (with complex conjugates) */
726: NbrEig = 0;
727: while (NbrEig < dgmres->neig) {
728: if (wi[perm[NbrEig]] != 0) NbrEig += 2;
729: else NbrEig += 1;
730: }
731: /* Reorder the Schur decomposition so that the cluster of smallest eigenvalues appears in the leading diagonal blocks of A */
733: PetscCall(PetscCalloc1(n, &select));
735: if (!dgmres->GreatestEig) {
736: for (j = 0; j < NbrEig; j++) select[perm[j]] = 1;
737: } else {
738: for (j = 0; j < NbrEig; j++) select[perm[n - j - 1]] = 1;
739: }
740: /* call Lapack dtrsen */
741: lwork = PetscMax(1, 4 * NbrEig * (bn - NbrEig));
742: liwork = PetscMax(1, 2 * NbrEig * (bn - NbrEig));
743: PetscCall(PetscMalloc1(lwork, &work));
744: PetscCall(PetscMalloc1(liwork, &iwork));
745: {
746: PetscReal CondEig; /* lower bound on the reciprocal condition number for the selected cluster of eigenvalues */
747: PetscReal CondSub; /* estimated reciprocal condition number of the specified invariant subspace. */
748: PetscCallLAPACKInfo("LAPACKtrsen", LAPACKtrsen_("B", "V", select, &bn, A, &ldA, Q, &ldQ, wr, wi, &NbrEig, &CondEig, &CondSub, work, &lwork, iwork, &liwork, &info));
749: }
750: PetscCall(PetscFree(select));
752: /* Extract the Schur vectors */
753: for (j = 0; j < NbrEig; j++) PetscCall(PetscArraycpy(&SR[j * N], &Q[j * ldQ], n));
754: *neig = NbrEig;
755: PetscCall(PetscFree(A));
756: PetscCall(PetscFree(work));
757: PetscCall(PetscFree(perm));
758: PetscCall(PetscFree(work));
759: PetscCall(PetscFree(iwork));
760: PetscCall(PetscFree(modul));
761: PetscCall(PetscFree(Q));
762: PetscFunctionReturn(PETSC_SUCCESS);
763: }
765: static PetscErrorCode KSPDGMRESApplyDeflation_DGMRES(KSP ksp, Vec x, Vec y)
766: {
767: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
768: PetscInt i, r = dgmres->r;
769: PetscReal alpha = 1.0;
770: PetscInt max_neig = dgmres->max_neig;
771: PetscBLASInt br, bmax;
772: PetscReal lambda = dgmres->lambdaN;
774: PetscFunctionBegin;
775: PetscCall(PetscBLASIntCast(r, &br));
776: PetscCall(PetscBLASIntCast(max_neig, &bmax));
777: PetscCall(PetscLogEventBegin(KSP_DGMRESApplyDeflation, ksp, 0, 0, 0));
778: if (!r) {
779: PetscCall(VecCopy(x, y));
780: PetscFunctionReturn(PETSC_SUCCESS);
781: }
782: /* Compute U'*x */
783: if (!X1) {
784: PetscCall(PetscMalloc1(bmax, &X1));
785: PetscCall(PetscMalloc1(bmax, &X2));
786: }
787: PetscCall(VecMDot(x, r, UU, X1));
789: /* Solve T*X1=X2 for X1*/
790: PetscCall(PetscArraycpy(X2, X1, br));
791: {
792: PetscBLASInt nrhs = 1;
793: PetscCallLAPACKInfo("LAPACKgetrs", LAPACKgetrs_("N", &br, &nrhs, TTF, &bmax, INVP, X1, &bmax, &info));
794: }
795: /* Iterative refinement -- is it really necessary ?? */
796: if (!WORK) {
797: PetscCall(PetscMalloc1(3 * bmax, &WORK));
798: PetscCall(PetscMalloc1(bmax, &IWORK));
799: }
800: {
801: PetscReal berr, ferr;
802: PetscBLASInt nrhs = 1;
803: PetscCallLAPACKInfo("LAPACKgerfs", LAPACKgerfs_("N", &br, &nrhs, TT, &bmax, TTF, &bmax, INVP, X2, &bmax, X1, &bmax, &ferr, &berr, WORK, IWORK, &info));
804: }
806: for (i = 0; i < r; i++) X2[i] = X1[i] / lambda - X2[i];
808: /* Compute X2=U*X2 */
809: PetscCall(VecMAXPBY(y, r, X2, 0, UU));
810: PetscCall(VecAXPY(y, alpha, x));
812: PetscCall(PetscLogEventEnd(KSP_DGMRESApplyDeflation, ksp, 0, 0, 0));
813: PetscFunctionReturn(PETSC_SUCCESS);
814: }
816: static PetscErrorCode KSPDGMRESImproveEig_DGMRES(KSP ksp, PetscInt neig)
817: {
818: KSP_DGMRES *dgmres = (KSP_DGMRES *)ksp->data;
819: PetscInt j, r_old, r = dgmres->r;
820: PetscBLASInt i = 0;
821: PetscInt neig1 = dgmres->neig + EIG_OFFSET;
822: PetscInt bmax = dgmres->max_neig;
823: PetscInt aug = r + neig; /* actual size of the augmented invariant basis */
824: PetscInt aug1 = bmax + neig1; /* maximum size of the augmented invariant basis */
825: PetscBLASInt ldA; /* leading dimension of AUAU and AUU*/
826: PetscBLASInt N; /* size of AUAU */
827: PetscReal *Q; /* orthogonal matrix of (left) schur vectors */
828: PetscReal *Z; /* orthogonal matrix of (right) schur vectors */
829: PetscReal *work; /* working vector */
830: PetscBLASInt lwork; /* size of the working vector */
831: PetscInt *perm; /* Permutation vector to sort eigenvalues */
832: PetscReal *wr, *wi, *beta, *modul; /* Real and imaginary part and modulus of the eigenvalues of A*/
833: PetscBLASInt NbrEig = 0, nr, bm;
834: PetscBLASInt *select;
835: PetscBLASInt liwork, *iwork;
837: PetscFunctionBegin;
838: /* Block construction of the matrices AUU=(AU)'*U and (AU)'*AU*/
839: if (!AUU) {
840: PetscCall(PetscMalloc1(aug1 * aug1, &AUU));
841: PetscCall(PetscMalloc1(aug1 * aug1, &AUAU));
842: }
843: /* AUU = (AU)'*U = [(MU)'*U (MU)'*X; (MX)'*U (MX)'*X]
844: * Note that MU and MX have been computed previously either in ComputeDataDeflation() or down here in a previous call to this function */
845: /* (MU)'*U size (r x r) -- store in the <r> first columns of AUU*/
846: for (j = 0; j < r; j++) PetscCall(VecMDot(UU[j], r, MU, &AUU[j * aug1]));
847: /* (MU)'*X size (r x neig) -- store in AUU from the column <r>*/
848: for (j = 0; j < neig; j++) PetscCall(VecMDot(XX[j], r, MU, &AUU[(r + j) * aug1]));
849: /* (MX)'*U size (neig x r) -- store in the <r> first columns of AUU from the row <r>*/
850: for (j = 0; j < r; j++) PetscCall(VecMDot(UU[j], neig, MX, &AUU[j * aug1 + r]));
851: /* (MX)'*X size (neig neig) -- store in AUU from the column <r> and the row <r>*/
852: for (j = 0; j < neig; j++) PetscCall(VecMDot(XX[j], neig, MX, &AUU[(r + j) * aug1 + r]));
854: /* AUAU = (AU)'*AU = [(MU)'*MU (MU)'*MX; (MX)'*MU (MX)'*MX] */
855: /* (MU)'*MU size (r x r) -- store in the <r> first columns of AUAU*/
856: for (j = 0; j < r; j++) PetscCall(VecMDot(MU[j], r, MU, &AUAU[j * aug1]));
857: /* (MU)'*MX size (r x neig) -- store in AUAU from the column <r>*/
858: for (j = 0; j < neig; j++) PetscCall(VecMDot(MX[j], r, MU, &AUAU[(r + j) * aug1]));
859: /* (MX)'*MU size (neig x r) -- store in the <r> first columns of AUAU from the row <r>*/
860: for (j = 0; j < r; j++) PetscCall(VecMDot(MU[j], neig, MX, &AUAU[j * aug1 + r]));
861: /* (MX)'*MX size (neig neig) -- store in AUAU from the column <r> and the row <r>*/
862: for (j = 0; j < neig; j++) PetscCall(VecMDot(MX[j], neig, MX, &AUAU[(r + j) * aug1 + r]));
864: /* Computation of the eigenvectors */
865: PetscCall(PetscBLASIntCast(aug1, &ldA));
866: PetscCall(PetscBLASIntCast(aug, &N));
867: lwork = 8 * N + 20; /* size of the working space */
868: PetscCall(PetscMalloc1(N, &wr));
869: PetscCall(PetscMalloc1(N, &wi));
870: PetscCall(PetscMalloc1(N, &beta));
871: PetscCall(PetscMalloc1(N, &modul));
872: PetscCall(PetscMalloc1(N, &perm));
873: PetscCall(PetscMalloc1(N * N, &Q));
874: PetscCall(PetscMalloc1(N * N, &Z));
875: PetscCall(PetscMalloc1(lwork, &work));
876: PetscCallLAPACKInfo("LAPACKgges", LAPACKgges_("V", "V", "N", NULL, &N, AUAU, &ldA, AUU, &ldA, &i, wr, wi, beta, Q, &N, Z, &N, work, &lwork, NULL, &info));
877: for (i = 0; i < N; i++) {
878: if (beta[i] != 0.0) {
879: wr[i] /= beta[i];
880: wi[i] /= beta[i];
881: }
882: }
883: /* sort the eigenvalues */
884: for (i = 0; i < N; i++) modul[i] = PetscSqrtReal(wr[i] * wr[i] + wi[i] * wi[i]);
885: for (i = 0; i < N; i++) perm[i] = i;
886: PetscCall(PetscSortRealWithPermutation(N, modul, perm));
887: /* Save the norm of the largest eigenvalue */
888: if (dgmres->lambdaN < modul[perm[N - 1]]) dgmres->lambdaN = modul[perm[N - 1]];
889: /* Allocate space to extract the first r schur vectors */
890: if (!SR2) PetscCall(PetscMalloc1(aug1 * bmax, &SR2));
891: /* count the number of extracted eigenvalues (complex conjugates count as 2) */
892: while (NbrEig < bmax) {
893: if (wi[perm[NbrEig]] == 0) NbrEig += 1;
894: else NbrEig += 2;
895: }
896: if (NbrEig > bmax) PetscCall(PetscBLASIntCast(bmax - 1, &NbrEig));
897: r_old = r; /* previous size of r */
898: dgmres->r = r = NbrEig;
900: /* Select the eigenvalues to reorder */
901: PetscCall(PetscCalloc1(N, &select));
902: if (!dgmres->GreatestEig) {
903: for (j = 0; j < NbrEig; j++) select[perm[j]] = 1;
904: } else {
905: for (j = 0; j < NbrEig; j++) select[perm[N - j - 1]] = 1;
906: }
907: /* Reorder and extract the new <r> schur vectors */
908: lwork = PetscMax(4 * N + 16, 2 * NbrEig * (N - NbrEig));
909: liwork = PetscMax(N + 6, 2 * NbrEig * (N - NbrEig));
910: PetscCall(PetscFree(work));
911: PetscCall(PetscMalloc1(lwork, &work));
912: PetscCall(PetscMalloc1(liwork, &iwork));
913: {
914: PetscReal Dif[2];
915: PetscBLASInt ijob = 2;
916: PetscBLASInt wantQ = 1, wantZ = 1;
917: PetscCallLAPACKInfo("LAPACKtgsen", LAPACKtgsen_(&ijob, &wantQ, &wantZ, select, &N, AUAU, &ldA, AUU, &ldA, wr, wi, beta, Q, &N, Z, &N, &NbrEig, NULL, NULL, &Dif[0], work, &lwork, iwork, &liwork, &info));
918: }
919: PetscCall(PetscFree(select));
921: for (j = 0; j < r; j++) PetscCall(PetscArraycpy(&SR2[j * aug1], &Z[j * N], N));
923: /* Multiply the Schur vectors SR2 by U (and X) to get a new U
924: -- save it temporarily in MU */
925: for (j = 0; j < r; j++) {
926: PetscCall(VecMAXPBY(MU[j], r_old, &SR2[j * aug1], 0, UU));
927: PetscCall(VecMAXPY(MU[j], neig, &SR2[j * aug1 + r_old], XX));
928: }
929: /* Form T = U'*MU*U */
930: for (j = 0; j < r; j++) {
931: PetscCall(VecCopy(MU[j], UU[j]));
932: PetscCall(KSP_PCApplyBAorAB(ksp, UU[j], MU[j], VEC_TEMP_MATOP));
933: }
934: dgmres->matvecs += r;
935: for (j = 0; j < r; j++) PetscCall(VecMDot(MU[j], r, UU, &TT[j * bmax]));
936: /* Factorize T */
937: PetscCall(PetscArraycpy(TTF, TT, bmax * r));
938: PetscCall(PetscBLASIntCast(r, &nr));
939: PetscCall(PetscBLASIntCast(bmax, &bm));
940: PetscCallLAPACKInfo("LAPACKgetrf", LAPACKgetrf_(&nr, &nr, TTF, &bm, INVP, &info));
942: PetscCall(PetscFree(wr));
943: PetscCall(PetscFree(wi));
944: PetscCall(PetscFree(beta));
945: PetscCall(PetscFree(modul));
946: PetscCall(PetscFree(perm));
947: PetscCall(PetscFree(Q));
948: PetscCall(PetscFree(Z));
949: PetscCall(PetscFree(work));
950: PetscCall(PetscFree(iwork));
951: PetscFunctionReturn(PETSC_SUCCESS);
952: }
954: /*MC
955: KSPDGMRES - Implements the deflated GMRES as defined in {cite}`erhel1996restarted` and {cite}`wakam2013memory`
957: Options Database Keys:
958: GMRES Options (inherited):
959: + -ksp_gmres_restart restart - the number of Krylov directions to orthogonalize against
960: . -ksp_gmres_haptol tol - sets the tolerance for "happy breakdown" (exact convergence)
961: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially
962: (otherwise groups of vectors are allocated as needed)
963: - -ksp_gmres_krylov_monitor - plot the Krylov space generated
965: DGMRES Options Database Keys:
966: + -ksp_dgmres_eigen neig - number of smallest eigenvalues to extract at each restart
967: . -ksp_dgmres_max_eigen max_neig - maximum number of eigenvalues that can be extracted during the iterative process
968: . -ksp_dgmres_force - use the deflation at each restart; switch off the adaptive strategy.
969: - -ksp_dgmres_view_deflation_vecs viewerspec - View the deflation vectors, where viewerspec is a key that can be
970: parsed by `PetscOptionsCreateViewer()`. If neig > 1, viewerspec should
971: end with ":append". No vectors will be viewed if the adaptive
972: strategy chooses not to deflate, so -ksp_dgmres_force should also
973: be given.
974: The deflation vectors span a subspace that may be a good
975: approximation of the subspace of smallest eigenvectors of the
976: preconditioned operator, so this option can aid in understanding
977: the performance of a preconditioner.
979: Level: beginner
981: Notes:
982: Left and right preconditioning are supported, but not symmetric preconditioning. Complex arithmetic is not supported
984: In this implementation, the adaptive strategy allows switching to deflated GMRES when the stagnation occurs.
986: Contributed by:
987: Desire NUENTSA WAKAM, INRIA
989: .seealso: [](ch_ksp), `KSPCreate()`, `KSPSetType()`, `KSPType`, `KSP`, `KSPFGMRES`, `KSPLGMRES`,
990: `KSPGMRESSetRestart()`, `KSPGMRESSetHapTol()`, `KSPGMRESSetPreAllocateVectors()`, `KSPOrthogonalizationSet()`, `KSPOrthogonalizationGet()`,
991: `KSPOrthogonalizationClassicalGramSchmidt()`, `KSPOrthogonalizationModifiedGramSchmidt()`,
992: `KSPOrthogonalizationCGSRefinementType`, `KSPOrthogonalizationSetCGSRefinementType()`, `KSPOrthogonalizationGetCGSRefinementType()`, `KSPGMRESMonitorKrylov()`, `KSPSetPCSide()`
993: M*/
995: PETSC_EXTERN PetscErrorCode KSPCreate_DGMRES(KSP ksp)
996: {
997: KSP_DGMRES *dgmres;
999: PetscFunctionBegin;
1000: PetscCall(PetscNew(&dgmres));
1001: ksp->data = (void *)dgmres;
1003: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_PRECONDITIONED, PC_LEFT, 3));
1004: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_RIGHT, 2));
1005: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_RIGHT, 1));
1007: ksp->ops->buildsolution = KSPBuildSolution_DGMRES;
1008: ksp->ops->setup = KSPSetUp_DGMRES;
1009: ksp->ops->solve = KSPSolve_DGMRES;
1010: ksp->ops->destroy = KSPDestroy_DGMRES;
1011: ksp->ops->view = KSPView_DGMRES;
1012: ksp->ops->setfromoptions = KSPSetFromOptions_DGMRES;
1013: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
1014: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
1016: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetPreAllocateVectors_C", KSPGMRESSetPreAllocateVectors_GMRES));
1017: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetRestart_C", KSPGMRESSetRestart_GMRES));
1018: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetHapTol_C", KSPGMRESSetHapTol_GMRES));
1019: /* -- New functions defined in DGMRES -- */
1020: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESSetEigen_C", KSPDGMRESSetEigen_DGMRES));
1021: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESSetMaxEigen_C", KSPDGMRESSetMaxEigen_DGMRES));
1022: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESSetRatio_C", KSPDGMRESSetRatio_DGMRES));
1023: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESForce_C", KSPDGMRESForce_DGMRES));
1024: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESComputeSchurForm_C", KSPDGMRESComputeSchurForm_DGMRES));
1025: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESComputeDeflationData_C", KSPDGMRESComputeDeflationData_DGMRES));
1026: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESApplyDeflation_C", KSPDGMRESApplyDeflation_DGMRES));
1027: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPDGMRESImproveEig_C", KSPDGMRESImproveEig_DGMRES));
1029: PetscCall(PetscLogEventRegister("DGMRESCompDefl", KSP_CLASSID, &KSP_DGMRESComputeDeflationData));
1030: PetscCall(PetscLogEventRegister("DGMRESApplyDefl", KSP_CLASSID, &KSP_DGMRESApplyDeflation));
1032: dgmres->haptol = 1.0e-30;
1033: dgmres->q_preallocate = PETSC_FALSE;
1034: dgmres->delta_allocate = GMRES_DELTA_DIRECTIONS;
1035: dgmres->nrs = NULL;
1036: dgmres->sol_temp = NULL;
1037: dgmres->max_k = GMRES_DEFAULT_MAXK;
1038: dgmres->Rsvd = NULL;
1040: /* Default values for the deflation */
1041: dgmres->r = 0;
1042: dgmres->neig = DGMRES_DEFAULT_EIG;
1043: dgmres->max_neig = DGMRES_DEFAULT_MAXEIG - 1;
1044: dgmres->lambdaN = 0.0;
1045: dgmres->smv = SMV;
1046: dgmres->matvecs = 0;
1047: dgmres->GreatestEig = PETSC_FALSE; /* experimental */
1048: dgmres->HasSchur = PETSC_FALSE;
1049: PetscFunctionReturn(PETSC_SUCCESS);
1050: }