Actual source code: lgmres.c
1: #include <../src/ksp/ksp/impls/gmres/lgmres/lgmresimpl.h>
3: static PetscErrorCode KSPLGMRESGetNewVectors(KSP, PetscInt);
4: static PetscErrorCode KSPLGMRESUpdateHessenberg(KSP, PetscInt, PetscBool, PetscReal *);
5: static PetscErrorCode KSPLGMRESBuildSoln(PetscScalar *, Vec, Vec, KSP, PetscInt);
7: /*@
8: KSPLGMRESSetAugDim - Set the number of error approximations to include in the approximation space (default is 2) for `KSPLGMRES`
10: Collective
12: Input Parameters:
13: + ksp - the `KSP` context
14: - dim - the number of vectors to use
16: Options Database Key:
17: . -ksp_lgmres_augment dim - the number of error approximations to include
19: Level: intermediate
21: Note:
22: If this is set to zero, then this method is equivalent to `KSPGMRES`
24: .seealso: [](ch_ksp), `KSPLGMRES`, `KSPLGMRESSetConstant()`
25: @*/
26: PetscErrorCode KSPLGMRESSetAugDim(KSP ksp, PetscInt dim)
27: {
28: PetscFunctionBegin;
29: PetscTryMethod(ksp, "KSPLGMRESSetAugDim_C", (KSP, PetscInt), (ksp, dim));
30: PetscFunctionReturn(PETSC_SUCCESS);
31: }
33: /*@
34: KSPLGMRESSetConstant - keep the error approximation space a constant size for every restart cycle
36: Collective
38: Input Parameters:
39: . ksp - the `KSP` context
41: Options Database Key:
42: . -ksp_lgmres_constant - set the size to be constant
44: Level: intermediate
46: Note:
47: This only affects the first couple of restart cycles when the total number of desired error approximations may not
48: be available.
50: .seealso: [](ch_ksp), `KSPLGMRES`, `KSPLGMRESSetAugDim()`
51: @*/
52: PetscErrorCode KSPLGMRESSetConstant(KSP ksp)
53: {
54: PetscFunctionBegin;
55: PetscTryMethod(ksp, "KSPLGMRESSetConstant_C", (KSP), (ksp));
56: PetscFunctionReturn(PETSC_SUCCESS);
57: }
59: static PetscErrorCode KSPSetUp_LGMRES(KSP ksp)
60: {
61: PetscInt max_k, k, aug_dim;
62: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
64: PetscFunctionBegin;
65: max_k = lgmres->max_k;
66: aug_dim = lgmres->aug_dim;
67: PetscCall(KSPSetUp_GMRES(ksp));
69: /* need array of pointers to augvecs*/
70: PetscCall(PetscMalloc1(2 * aug_dim + AUG_OFFSET, &lgmres->augvecs));
72: lgmres->aug_vecs_allocated = 2 * aug_dim + AUG_OFFSET;
74: PetscCall(PetscMalloc1(2 * aug_dim + AUG_OFFSET, &lgmres->augvecs_user_work));
75: PetscCall(PetscMalloc1(aug_dim, &lgmres->aug_order));
77: /* for now we will preallocate the augvecs - because aug_dim << restart
78: ... also keep in mind that we need to keep augvecs from cycle to cycle*/
79: lgmres->aug_vv_allocated = 2 * aug_dim + AUG_OFFSET;
80: lgmres->augwork_alloc = 2 * aug_dim + AUG_OFFSET;
82: PetscCall(KSPCreateVecs(ksp, lgmres->aug_vv_allocated, &lgmres->augvecs_user_work[0], 0, NULL));
83: PetscCall(PetscMalloc1(max_k + 1, &lgmres->hwork));
84: for (k = 0; k < lgmres->aug_vv_allocated; k++) lgmres->augvecs[k] = lgmres->augvecs_user_work[0][k];
85: PetscFunctionReturn(PETSC_SUCCESS);
86: }
88: static PetscErrorCode KSPLGMRESCycle(PetscInt *itcount, KSP ksp)
89: {
90: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
91: PetscReal res_norm, res;
92: PetscReal hapbnd, tt;
93: PetscScalar tmp;
94: PetscBool hapend = PETSC_FALSE; /* indicates happy breakdown ending */
95: PetscInt loc_it; /* local count of # of dir. in Krylov space */
96: PetscInt max_k = lgmres->max_k; /* max approx space size */
97: PetscInt max_it = ksp->max_it; /* max # of overall iterations for the method */
99: /* LGMRES_MOD - new variables*/
100: PetscInt aug_dim = lgmres->aug_dim;
101: PetscInt spot = 0;
102: PetscInt order = 0;
103: PetscInt it_arnoldi; /* number of arnoldi steps to take */
104: PetscInt it_total; /* total number of its to take (=approx space size)*/
105: PetscInt ii, jj;
106: PetscReal tmp_norm;
107: PetscScalar inv_tmp_norm;
108: PetscScalar *avec;
110: PetscFunctionBegin;
111: /* Number of pseudo iterations since last restart is the number of prestart directions */
112: loc_it = 0;
114: /* LGMRES_MOD: determine number of arnoldi steps to take */
115: /* if approx_constant then we keep the space the same size even if
116: we don't have the full number of aug vectors yet*/
117: if (lgmres->approx_constant) it_arnoldi = max_k - lgmres->aug_ct;
118: else it_arnoldi = max_k - aug_dim;
120: it_total = it_arnoldi + lgmres->aug_ct;
122: /* initial residual is in VEC_VV(0) - compute its norm*/
123: PetscCall(VecNorm(VEC_VV(0), NORM_2, &res_norm));
124: KSPCheckNorm(ksp, res_norm);
125: res = res_norm;
127: /* first entry in the right-hand side of the Hessenberg system is just the initial residual norm */
128: *GRS(0) = res_norm;
130: /* check for the convergence */
131: if (!res) {
132: if (itcount) *itcount = 0;
133: ksp->reason = KSP_CONVERGED_ATOL;
134: PetscCall(PetscInfo(ksp, "Converged due to zero residual norm on entry\n"));
135: PetscFunctionReturn(PETSC_SUCCESS);
136: }
138: /* scale VEC_VV (the initial residual) */
139: tmp = 1.0 / res_norm;
140: PetscCall(VecScale(VEC_VV(0), tmp));
142: if (ksp->normtype != KSP_NORM_NONE) ksp->rnorm = res;
143: else ksp->rnorm = 0.0;
145: /* note: (lgmres->it) is always set one less than (loc_it) It is used in
146: KSPBUILDSolution_LGMRES, where it is passed to KSPLGMRESBuildSoln.
147: Note that when KSPLGMRESBuildSoln is called from this function,
148: (loc_it -1) is passed, so the two are equivalent */
149: lgmres->it = (loc_it - 1);
151: /* MAIN ITERATION LOOP BEGINNING*/
153: /* keep iterating until we have converged OR generated the max number
154: of directions OR reached the max number of iterations for the method */
155: PetscCall((*ksp->converged)(ksp, ksp->its, res, &ksp->reason, ksp->cnvP));
157: while (!ksp->reason && loc_it < it_total && ksp->its < max_it) { /* LGMRES_MOD: changed to it_total */
158: PetscCall(KSPLogResidualHistory(ksp, res));
159: lgmres->it = (loc_it - 1);
160: PetscCall(KSPMonitor(ksp, ksp->its, res));
162: /* see if more space is needed for work vectors */
163: if (lgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
164: PetscCall(KSPLGMRESGetNewVectors(ksp, loc_it + 1));
165: /* (loc_it+1) is passed in as number of the first vector that should be allocated */
166: }
168: /* LGMRES_MOD: decide whether this is an arnoldi step or an aug step */
169: if (loc_it < it_arnoldi) { /* Arnoldi */
170: PetscCall(KSP_PCApplyBAorAB(ksp, VEC_VV(loc_it), VEC_VV(1 + loc_it), VEC_TEMP_MATOP));
171: } else { /* aug step */
172: order = loc_it - it_arnoldi + 1; /* which aug step */
173: for (ii = 0; ii < aug_dim; ii++) {
174: if (lgmres->aug_order[ii] == order) {
175: spot = ii;
176: break; /* must have this because there will be duplicates before aug_ct = aug_dim */
177: }
178: }
180: PetscCall(VecCopy(A_AUGVEC(spot), VEC_VV(1 + loc_it)));
181: /* note: an alternate implementation choice would be to only save the AUGVECS and
182: not A_AUGVEC and then apply the PC here to the augvec */
183: }
185: /* update Hessenberg matrix and do Gram-Schmidt - new direction is in VEC_VV(1+loc_it)*/
186: PetscCall((*ksp->orthog)(ksp, &VEC_VV(0), loc_it + 1, NULL, HH(0, loc_it)));
187: PetscCall(PetscArraycpy(HES(0, loc_it), HH(0, loc_it), loc_it + 1));
189: /* new entry in Hessenberg is the 2-norm of our new direction */
190: PetscCall(VecNorm(VEC_VV(loc_it + 1), NORM_2, &tt));
192: *HH(loc_it + 1, loc_it) = tt;
193: *HES(loc_it + 1, loc_it) = tt;
195: /* check for the happy breakdown */
196: hapbnd = PetscAbsScalar(tt / *GRS(loc_it)); /* GRS(loc_it) contains the res_norm from the last iteration */
197: if (hapbnd > lgmres->haptol) hapbnd = lgmres->haptol;
198: if (tt > hapbnd) {
199: tmp = 1.0 / tt;
200: PetscCall(VecScale(VEC_VV(loc_it + 1), tmp)); /* scale new direction by its norm */
201: } else {
202: PetscCall(PetscInfo(ksp, "Detected happy breakdown, current hapbnd = %g tt = %g\n", (double)hapbnd, (double)tt));
203: hapend = PETSC_TRUE;
204: }
206: /* apply rotations to the new column of the Hessenberg (and the right-hand side of the system),
207: calculate new rotation, and get new residual norm at the same time*/
208: PetscCall(KSPLGMRESUpdateHessenberg(ksp, loc_it, hapend, &res));
209: if (ksp->reason) break;
211: loc_it++;
212: lgmres->it = (loc_it - 1); /* Add this here in case it has converged */
214: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
215: ksp->its++;
216: if (ksp->normtype != KSP_NORM_NONE) ksp->rnorm = res;
217: else ksp->rnorm = 0.0;
218: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
220: PetscCall((*ksp->converged)(ksp, ksp->its, res, &ksp->reason, ksp->cnvP));
222: /* Catch error in happy breakdown and signal convergence and break from loop */
223: if (hapend) {
224: if (!ksp->reason) {
225: PetscCheck(!ksp->errorifnotconverged, PetscObjectComm((PetscObject)ksp), PETSC_ERR_NOT_CONVERGED, "Reached happy break down, but convergence was not indicated. Residual norm = %g", (double)res);
226: ksp->reason = KSP_DIVERGED_BREAKDOWN;
227: break;
228: }
229: }
230: }
231: /* END OF ITERATION LOOP */
232: PetscCall(KSPLogResidualHistory(ksp, res));
234: if (itcount) *itcount = loc_it;
236: /*
237: Solve for the "best" coefficients of the Krylov
238: columns, add the solution values together, and possibly unwind the
239: preconditioning from the solution
240: */
242: /* Form the solution (or the solution so far) */
243: /* Note: must pass in (loc_it-1) for iteration count so that KSPLGMRESBuildSoln properly navigates */
245: PetscCall(KSPLGMRESBuildSoln(GRS(0), ksp->vec_sol, ksp->vec_sol, ksp, loc_it - 1));
247: /* Monitor if we know that we will not return for a restart */
248: if (ksp->reason == KSP_CONVERGED_ITERATING && ksp->its >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
249: if (ksp->reason) PetscCall(KSPMonitor(ksp, ksp->its, res));
251: /* LGMRES_MOD collect aug vector and A*augvector for future restarts -
252: only if we will be restarting (i.e. this cycle performed it_total iterations) */
253: if (!ksp->reason && ksp->its < max_it && aug_dim > 0) {
254: /* AUG_TEMP contains the new augmentation vector (assigned in KSPLGMRESBuildSoln) */
255: if (!lgmres->aug_ct) {
256: spot = 0;
257: lgmres->aug_ct++;
258: } else if (lgmres->aug_ct < aug_dim) {
259: spot = lgmres->aug_ct;
260: lgmres->aug_ct++;
261: } else { /* truncate */
262: for (ii = 0; ii < aug_dim; ii++) {
263: if (lgmres->aug_order[ii] == aug_dim) spot = ii;
264: }
265: }
267: PetscCall(VecCopy(AUG_TEMP, AUGVEC(spot)));
268: /* need to normalize */
269: PetscCall(VecNorm(AUGVEC(spot), NORM_2, &tmp_norm));
271: inv_tmp_norm = 1.0 / tmp_norm;
273: PetscCall(VecScale(AUGVEC(spot), inv_tmp_norm));
275: /* set new aug vector to order 1 - move all others back one */
276: for (ii = 0; ii < aug_dim; ii++) AUG_ORDER(ii)++;
277: AUG_ORDER(spot) = 1;
279: /* now add the A*aug vector to A_AUGVEC(spot) - this is independent of preconditioning type */
280: /* want V*H*y - y is in GRS, V is in VEC_VV and H is in HES */
282: /* do H+*y */
283: avec = lgmres->hwork;
284: PetscCall(PetscArrayzero(avec, it_total + 1));
285: for (ii = 0; ii < it_total + 1; ii++) {
286: for (jj = 0; jj <= ii + 1 && jj < it_total + 1; jj++) avec[jj] += *HES(jj, ii) * *GRS(ii);
287: }
289: /* multiply result by V+ */
290: PetscCall(VecMAXPBY(VEC_TEMP, it_total + 1, avec, 0, &VEC_VV(0))); /* answer is in VEC_TEMP */
292: /* copy answer to aug location and scale */
293: PetscCall(VecCopy(VEC_TEMP, A_AUGVEC(spot)));
294: PetscCall(VecScale(A_AUGVEC(spot), inv_tmp_norm));
295: }
296: PetscFunctionReturn(PETSC_SUCCESS);
297: }
299: static PetscErrorCode KSPSolve_LGMRES(KSP ksp)
300: {
301: PetscInt itcount; /* running total of iterations, incl. those in restarts */
302: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
303: PetscBool guess_zero = ksp->guess_zero;
304: PetscInt ii; /* LGMRES_MOD variable */
306: PetscFunctionBegin;
307: PetscCheck(!ksp->calc_sings || lgmres->Rsvd, PetscObjectComm((PetscObject)ksp), PETSC_ERR_ORDER, "Must call KSPSetComputeSingularValues() before KSPSetUp() is called");
309: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
311: ksp->its = 0;
312: lgmres->aug_ct = 0;
313: lgmres->matvecs = 0;
315: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
317: /* initialize */
318: itcount = 0;
319: /* LGMRES_MOD */
320: for (ii = 0; ii < lgmres->aug_dim; ii++) lgmres->aug_order[ii] = 0;
322: while (!ksp->reason) {
323: PetscInt cycle_its = 0; /* iterations done in a call to KSPLGMRESCycle */
324: /* calc residual - puts in VEC_VV(0) */
325: PetscCall(KSPInitialResidual(ksp, ksp->vec_sol, VEC_TEMP, VEC_TEMP_MATOP, VEC_VV(0), ksp->vec_rhs));
326: PetscCall(KSPLGMRESCycle(&cycle_its, ksp));
327: itcount += cycle_its;
328: if (itcount >= ksp->max_it) {
329: if (!ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
330: break;
331: }
332: ksp->guess_zero = PETSC_FALSE; /* every future call to KSPInitialResidual() will have nonzero guess */
333: }
334: ksp->guess_zero = guess_zero; /* restore if user provided nonzero initial guess */
335: PetscFunctionReturn(PETSC_SUCCESS);
336: }
338: static PetscErrorCode KSPDestroy_LGMRES(KSP ksp)
339: {
340: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
342: PetscFunctionBegin;
343: PetscCall(PetscFree(lgmres->augvecs));
344: if (lgmres->augwork_alloc) PetscCall(VecDestroyVecs(lgmres->augwork_alloc, &lgmres->augvecs_user_work[0]));
345: PetscCall(PetscFree(lgmres->augvecs_user_work));
346: PetscCall(PetscFree(lgmres->aug_order));
347: PetscCall(PetscFree(lgmres->hwork));
348: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPLGMRESSetConstant_C", NULL));
349: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPLGMRESSetAugDim_C", NULL));
350: PetscCall(KSPDestroy_GMRES(ksp));
351: PetscFunctionReturn(PETSC_SUCCESS);
352: }
354: static PetscErrorCode KSPLGMRESBuildSoln(PetscScalar *nrs, Vec vguess, Vec vdest, KSP ksp, PetscInt it)
355: {
356: PetscScalar tt;
357: PetscInt ii, k, j;
358: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
359: /* LGMRES_MOD */
360: PetscInt it_arnoldi, it_aug;
361: PetscInt jj, spot = 0;
363: PetscFunctionBegin;
364: /* Solve for solution vector that minimizes the residual */
366: /* If it is < 0, no lgmres steps have been performed */
367: if (it < 0) {
368: PetscCall(VecCopy(vguess, vdest)); /* VecCopy() is smart, exists immediately if vguess == vdest */
369: PetscFunctionReturn(PETSC_SUCCESS);
370: }
372: /* so (it+1) lgmres steps HAVE been performed */
374: /* LGMRES_MOD - determine if we need to use augvecs for the soln - do not assume that
375: this is called after the total its allowed for an approx space */
376: if (lgmres->approx_constant) {
377: it_arnoldi = lgmres->max_k - lgmres->aug_ct;
378: } else {
379: it_arnoldi = lgmres->max_k - lgmres->aug_dim;
380: }
381: if (it_arnoldi >= it + 1) {
382: it_aug = 0;
383: it_arnoldi = it + 1;
384: } else {
385: it_aug = (it + 1) - it_arnoldi;
386: }
388: /* now it_arnoldi indicates the number of matvecs that took place */
389: lgmres->matvecs += it_arnoldi;
391: /* solve the upper triangular system - GRS is the right side and HH is
392: the upper triangular matrix - put soln in nrs */
393: PetscCheck(*HH(it, it) != 0.0, PETSC_COMM_SELF, PETSC_ERR_CONV_FAILED, "HH(it,it) is identically zero; it = %" PetscInt_FMT " GRS(it) = %g", it, (double)PetscAbsScalar(*GRS(it)));
394: if (*HH(it, it) != 0.0) {
395: nrs[it] = *GRS(it) / *HH(it, it);
396: } else {
397: nrs[it] = 0.0;
398: }
400: for (ii = 1; ii <= it; ii++) {
401: k = it - ii;
402: tt = *GRS(k);
403: for (j = k + 1; j <= it; j++) tt = tt - *HH(k, j) * nrs[j];
404: nrs[k] = tt / *HH(k, k);
405: }
407: /* Accumulate the correction to the soln of the preconditioned prob. in VEC_TEMP */
409: /* LGMRES_MOD - if augmenting has happened we need to form the solution using the augvecs */
410: if (!it_aug) { /* all its are from arnoldi */
411: PetscCall(VecMAXPBY(VEC_TEMP, it + 1, nrs, 0, &VEC_VV(0)));
412: } else { /* use aug vecs */
413: /* first do regular Krylov directions */
414: PetscCall(VecMAXPBY(VEC_TEMP, it_arnoldi, nrs, 0, &VEC_VV(0)));
415: /* now add augmented portions - add contribution of aug vectors one at a time*/
417: for (ii = 0; ii < it_aug; ii++) {
418: for (jj = 0; jj < lgmres->aug_dim; jj++) {
419: if (lgmres->aug_order[jj] == (ii + 1)) {
420: spot = jj;
421: break; /* must have this because there will be duplicates before aug_ct = aug_dim */
422: }
423: }
424: PetscCall(VecAXPY(VEC_TEMP, nrs[it_arnoldi + ii], AUGVEC(spot)));
425: }
426: }
427: /* now VEC_TEMP is what we want to keep for augmenting purposes - grab before the
428: preconditioner is "unwound" from right-precondtioning*/
429: PetscCall(VecCopy(VEC_TEMP, AUG_TEMP));
431: PetscCall(KSPUnwindPreconditioner(ksp, VEC_TEMP, VEC_TEMP_MATOP));
433: /* add solution to previous solution */
434: /* put updated solution into vdest.*/
435: PetscCall(VecCopy(vguess, vdest));
436: PetscCall(VecAXPY(vdest, 1.0, VEC_TEMP));
437: PetscFunctionReturn(PETSC_SUCCESS);
438: }
440: static PetscErrorCode KSPLGMRESUpdateHessenberg(KSP ksp, PetscInt it, PetscBool hapend, PetscReal *res)
441: {
442: PetscScalar *hh, *cc, *ss, tt;
443: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
445: PetscFunctionBegin;
446: hh = HH(0, it); /* pointer to beginning of column to update - so incrementing hh "steps down" the (it+1)th col of HH*/
447: cc = CC(0); /* beginning of cosine rotations */
448: ss = SS(0); /* beginning of sine rotations */
450: /* Apply all the previously computed plane rotations to the new column
451: of the Hessenberg matrix */
452: /* Note: this uses the rotation [conj(c) s ; -s c], c= cos(theta), s= sin(theta) */
454: for (PetscInt j = 1; j <= it; j++) {
455: tt = *hh;
456: *hh = PetscConj(*cc) * tt + *ss * *(hh + 1);
457: hh++;
458: *hh = *cc++ * *hh - (*ss++ * tt);
459: /* hh, cc, and ss have all been incremented one by end of loop */
460: }
462: /*
463: compute the new plane rotation, and apply it to:
464: 1) the right-hand side of the Hessenberg system (GRS)
465: note: it affects GRS(it) and GRS(it+1)
466: 2) the new column of the Hessenberg matrix
467: note: it affects HH(it,it) which is currently pointed to
468: by hh and HH(it+1, it) (*(hh+1))
469: thus obtaining the updated value of the residual...
470: */
472: /* compute new plane rotation */
474: if (!hapend) {
475: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh + 1)) * *(hh + 1));
476: if (tt == 0.0) {
477: ksp->reason = KSP_DIVERGED_NULL;
478: PetscFunctionReturn(PETSC_SUCCESS);
479: }
480: *cc = *hh / tt; /* new cosine value */
481: *ss = *(hh + 1) / tt; /* new sine value */
483: /* apply to 1) and 2) */
484: *GRS(it + 1) = -(*ss * *GRS(it));
485: *GRS(it) = PetscConj(*cc) * *GRS(it);
486: *hh = PetscConj(*cc) * *hh + *ss * *(hh + 1);
488: /* residual is the last element (it+1) of right-hand side! */
489: *res = PetscAbsScalar(*GRS(it + 1));
491: } else { /* happy breakdown: HH(it+1, it) = 0, therefore we don't need to apply
492: another rotation matrix (so RH doesn't change). The new residual is
493: always the new sine term times the residual from last time (GRS(it)),
494: but now the new sine rotation would be zero...so the residual should
495: be zero...so we will multiply "zero" by the last residual. This might
496: not be exactly what we want to do here -could just return "zero". */
498: *res = 0.0;
499: }
500: PetscFunctionReturn(PETSC_SUCCESS);
501: }
503: /*
504: KSPLGMRESGetNewVectors - Allocates more work vectors, starting from VEC_VV(it)
506: */
507: static PetscErrorCode KSPLGMRESGetNewVectors(KSP ksp, PetscInt it)
508: {
509: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
510: PetscInt nwork = lgmres->nwork_alloc; /* number of work vector chunks allocated */
511: PetscInt nalloc; /* number to allocate */
513: PetscFunctionBegin;
514: nalloc = lgmres->delta_allocate; /* number of vectors to allocate in a single chunk */
516: /* Adjust the number to allocate to make sure that we don't exceed the
517: number of available slots (lgmres->vecs_allocated)*/
518: if (it + VEC_OFFSET + nalloc >= lgmres->vecs_allocated) nalloc = lgmres->vecs_allocated - it - VEC_OFFSET;
519: if (!nalloc) PetscFunctionReturn(PETSC_SUCCESS);
521: lgmres->vv_allocated += nalloc; /* vv_allocated is the number of vectors allocated */
523: /* work vectors */
524: PetscCall(KSPCreateVecs(ksp, nalloc, &lgmres->user_work[nwork], 0, NULL));
525: /* specify size of chunk allocated */
526: lgmres->mwork_alloc[nwork] = nalloc;
528: for (PetscInt k = 0; k < nalloc; k++) lgmres->vecs[it + VEC_OFFSET + k] = lgmres->user_work[nwork][k];
530: /* LGMRES_MOD - for now we are preallocating the augmentation vectors */
532: /* increment the number of work vector chunks */
533: lgmres->nwork_alloc++;
534: PetscFunctionReturn(PETSC_SUCCESS);
535: }
537: static PetscErrorCode KSPBuildSolution_LGMRES(KSP ksp, Vec ptr, Vec *result)
538: {
539: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
541: PetscFunctionBegin;
542: if (!ptr) {
543: if (!lgmres->sol_temp) PetscCall(VecDuplicate(ksp->vec_sol, &lgmres->sol_temp));
544: ptr = lgmres->sol_temp;
545: }
546: if (!lgmres->nrs) {
547: /* allocate the work area */
548: PetscCall(PetscMalloc1(lgmres->max_k, &lgmres->nrs));
549: }
551: PetscCall(KSPLGMRESBuildSoln(lgmres->nrs, ksp->vec_sol, ptr, ksp, lgmres->it));
552: if (result) *result = ptr;
553: PetscFunctionReturn(PETSC_SUCCESS);
554: }
556: static PetscErrorCode KSPView_LGMRES(KSP ksp, PetscViewer viewer)
557: {
558: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
559: PetscBool isascii;
561: PetscFunctionBegin;
562: PetscCall(KSPView_GMRES(ksp, viewer));
563: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
564: if (isascii) {
565: /* LGMRES_MOD */
566: PetscCall(PetscViewerASCIIPrintf(viewer, " aug. dimension=%" PetscInt_FMT "\n", lgmres->aug_dim));
567: if (lgmres->approx_constant) PetscCall(PetscViewerASCIIPrintf(viewer, " approx. space size was kept constant.\n"));
568: PetscCall(PetscViewerASCIIPrintf(viewer, " number of matvecs=%" PetscInt_FMT "\n", lgmres->matvecs));
569: }
570: PetscFunctionReturn(PETSC_SUCCESS);
571: }
573: static PetscErrorCode KSPSetFromOptions_LGMRES(KSP ksp, PetscOptionItems PetscOptionsObject)
574: {
575: PetscInt aug;
576: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
577: PetscBool flg = PETSC_FALSE;
579: PetscFunctionBegin;
580: PetscCall(KSPSetFromOptions_GMRES(ksp, PetscOptionsObject));
581: PetscOptionsHeadBegin(PetscOptionsObject, "KSP LGMRES Options");
582: PetscCall(PetscOptionsBool("-ksp_lgmres_constant", "Use constant approx. space size", "KSPGMRESSetConstant", lgmres->approx_constant, &lgmres->approx_constant, NULL));
583: PetscCall(PetscOptionsInt("-ksp_lgmres_augment", "Number of error approximations to augment the Krylov space with", "KSPLGMRESSetAugDim", lgmres->aug_dim, &aug, &flg));
584: if (flg) PetscCall(KSPLGMRESSetAugDim(ksp, aug));
585: PetscOptionsHeadEnd();
586: PetscFunctionReturn(PETSC_SUCCESS);
587: }
589: static PetscErrorCode KSPLGMRESSetConstant_LGMRES(KSP ksp)
590: {
591: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
593: PetscFunctionBegin;
594: lgmres->approx_constant = PETSC_TRUE;
595: PetscFunctionReturn(PETSC_SUCCESS);
596: }
598: static PetscErrorCode KSPLGMRESSetAugDim_LGMRES(KSP ksp, PetscInt aug_dim)
599: {
600: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
602: PetscFunctionBegin;
603: PetscCheck(aug_dim >= 0, PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_OUTOFRANGE, "Augmentation dimension must be nonegative");
604: PetscCheck(aug_dim <= (lgmres->max_k - 1), PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_OUTOFRANGE, "Augmentation dimension must be <= (restart size-1)");
605: lgmres->aug_dim = aug_dim;
606: PetscFunctionReturn(PETSC_SUCCESS);
607: }
609: /*MC
610: KSPLGMRES - Augments the standard GMRES approximation space with approximations to the error from previous restart cycles as in {cite}`bjm2005`.
612: Options Database Keys:
613: + -ksp_gmres_restart restart - total approximation space size (Krylov directions + error approximations)
614: . -ksp_gmres_haptol tol - sets the tolerance for "happy breakdown" (exact convergence)
615: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially
616: (otherwise groups of vectors are allocated as needed)
617: . -ksp_gmres_krylov_monitor - plot the Krylov space generated
618: . -ksp_lgmres_augment k - number of error approximations to augment the Krylov space with
619: - -ksp_lgmres_constant - use a constant approximate space size
620: (only affects restart cycles < num. error approx.(k), i.e. the first k restarts)
622: Level: beginner
624: Notes:
625: Supports both left and right preconditioning, but not symmetric.
627: Augmenting with 1,2, or 3 approximations is generally optimal.
629: This method is an accelerator for `KSPGMRES` - it is not useful for problems that stall. When gmres(m) stalls then lgmres with a size m
630: approximation space will also generally stall.
632: If gmres(m) converges in a small number of restart cycles, then lgmres also tends not to be very helpful.
634: Developer Notes:
635: To run LGMRES(m, k) as described in {cite}`bjm2005`, use\:
636: .vb
637: -ksp_gmres_restart <m+k>
638: -ksp_lgmres_augment <k>
639: .ve
641: This object is subclassed off of `KSPGMRES`, see the source code in src/ksp/ksp/impls/gmres for comments on the structure of the code
643: Contributed by:
644: Allison Baker
646: .seealso: [](ch_ksp), `KSPCreate()`, `KSPSetType()`, `KSPType`, `KSP`, `KSPFGMRES`, `KSPGMRES`,
647: `KSPGMRESSetRestart()`, `KSPGMRESSetHapTol()`, `KSPGMRESSetPreAllocateVectors()`, `KSPOrthogonalizationSet()`, `KSPOrthogonalizationGet()`,
648: `KSPOrthogonalizationClassicalGramSchmidt()`, `KSPOrthogonalizationModifiedGramSchmidt()`,
649: `KSPOrthogonalizationCGSRefinementType`, `KSPOrthogonalizationSetCGSRefinementType()`, `KSPOrthogonalizationGetCGSRefinementType()`, `KSPGMRESMonitorKrylov()`, `KSPLGMRESSetAugDim()`,
650: `KSPGMRESSetConstant()`, `KSPLGMRESSetConstant()`
651: M*/
653: PETSC_EXTERN PetscErrorCode KSPCreate_LGMRES(KSP ksp)
654: {
655: KSP_LGMRES *lgmres;
657: PetscFunctionBegin;
658: PetscCall(PetscNew(&lgmres));
660: ksp->data = (void *)lgmres;
661: ksp->ops->buildsolution = KSPBuildSolution_LGMRES;
663: ksp->ops->setup = KSPSetUp_LGMRES;
664: ksp->ops->solve = KSPSolve_LGMRES;
665: ksp->ops->destroy = KSPDestroy_LGMRES;
666: ksp->ops->view = KSPView_LGMRES;
667: ksp->ops->setfromoptions = KSPSetFromOptions_LGMRES;
668: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
669: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
671: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_PRECONDITIONED, PC_LEFT, 3));
672: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_RIGHT, 2));
673: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_RIGHT, 1));
675: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetPreAllocateVectors_C", KSPGMRESSetPreAllocateVectors_GMRES));
676: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetRestart_C", KSPGMRESSetRestart_GMRES));
677: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESGetRestart_C", KSPGMRESGetRestart_GMRES));
678: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetHapTol_C", KSPGMRESSetHapTol_GMRES));
680: /* LGMRES_MOD add extra functions here - like the one to set num of aug vectors */
681: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPLGMRESSetConstant_C", KSPLGMRESSetConstant_LGMRES));
682: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPLGMRESSetAugDim_C", KSPLGMRESSetAugDim_LGMRES));
684: /* defaults */
685: lgmres->haptol = 1.0e-30;
686: lgmres->q_preallocate = PETSC_FALSE;
687: lgmres->delta_allocate = LGMRES_DELTA_DIRECTIONS;
688: lgmres->nrs = NULL;
689: lgmres->sol_temp = NULL;
690: lgmres->max_k = LGMRES_DEFAULT_MAXK;
691: lgmres->Rsvd = NULL;
693: /* LGMRES_MOD - new defaults */
694: lgmres->aug_dim = LGMRES_DEFAULT_AUGDIM;
695: lgmres->aug_ct = 0; /* start with no aug vectors */
696: lgmres->approx_constant = PETSC_FALSE;
697: lgmres->matvecs = 0;
698: PetscFunctionReturn(PETSC_SUCCESS);
699: }