Actual source code: lgmres.c
2: #include <../src/ksp/ksp/impls/gmres/lgmres/lgmresimpl.h>
4: #define LGMRES_DELTA_DIRECTIONS 10
5: #define LGMRES_DEFAULT_MAXK 30
6: #define LGMRES_DEFAULT_AUGDIM 2 /*default number of augmentation vectors */
7: static PetscErrorCode KSPLGMRESGetNewVectors(KSP, PetscInt);
8: static PetscErrorCode KSPLGMRESUpdateHessenberg(KSP, PetscInt, PetscBool, PetscReal *);
9: static PetscErrorCode KSPLGMRESBuildSoln(PetscScalar *, Vec, Vec, KSP, PetscInt);
11: PetscErrorCode KSPLGMRESSetAugDim(KSP ksp, PetscInt dim)
12: {
13: PetscFunctionBegin;
14: PetscTryMethod((ksp), "KSPLGMRESSetAugDim_C", (KSP, PetscInt), (ksp, dim));
15: PetscFunctionReturn(PETSC_SUCCESS);
16: }
18: PetscErrorCode KSPLGMRESSetConstant(KSP ksp)
19: {
20: PetscFunctionBegin;
21: PetscTryMethod((ksp), "KSPLGMRESSetConstant_C", (KSP), (ksp));
22: PetscFunctionReturn(PETSC_SUCCESS);
23: }
25: /*
26: KSPSetUp_LGMRES - Sets up the workspace needed by lgmres.
28: This is called once, usually automatically by KSPSolve() or KSPSetUp(),
29: but can be called directly by KSPSetUp().
31: */
32: PetscErrorCode KSPSetUp_LGMRES(KSP ksp)
33: {
34: PetscInt max_k, k, aug_dim;
35: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
37: PetscFunctionBegin;
38: max_k = lgmres->max_k;
39: aug_dim = lgmres->aug_dim;
40: PetscCall(KSPSetUp_GMRES(ksp));
42: /* need array of pointers to augvecs*/
43: PetscCall(PetscMalloc1(2 * aug_dim + AUG_OFFSET, &lgmres->augvecs));
45: lgmres->aug_vecs_allocated = 2 * aug_dim + AUG_OFFSET;
47: PetscCall(PetscMalloc1(2 * aug_dim + AUG_OFFSET, &lgmres->augvecs_user_work));
48: PetscCall(PetscMalloc1(aug_dim, &lgmres->aug_order));
50: /* for now we will preallocate the augvecs - because aug_dim << restart
51: ... also keep in mind that we need to keep augvecs from cycle to cycle*/
52: lgmres->aug_vv_allocated = 2 * aug_dim + AUG_OFFSET;
53: lgmres->augwork_alloc = 2 * aug_dim + AUG_OFFSET;
55: PetscCall(KSPCreateVecs(ksp, lgmres->aug_vv_allocated, &lgmres->augvecs_user_work[0], 0, NULL));
56: PetscCall(PetscMalloc1(max_k + 1, &lgmres->hwork));
57: for (k = 0; k < lgmres->aug_vv_allocated; k++) lgmres->augvecs[k] = lgmres->augvecs_user_work[0][k];
58: PetscFunctionReturn(PETSC_SUCCESS);
59: }
61: /*
62: KSPLGMRESCycle - Run lgmres, possibly with restart. Return residual
63: history if requested.
65: input parameters:
66: . lgmres - structure containing parameters and work areas
68: output parameters:
69: . nres - residuals (from preconditioned system) at each step.
70: If restarting, consider passing nres+it. If null,
71: ignored
72: . itcount - number of iterations used. nres[0] to nres[itcount]
73: are defined. If null, ignored. If null, ignored.
74: . converged - 0 if not converged
76: Notes:
77: On entry, the value in vector VEC_VV(0) should be
78: the initial residual.
79: */
80: PetscErrorCode KSPLGMRESCycle(PetscInt *itcount, KSP ksp)
81: {
82: KSP_LGMRES *lgmres = (KSP_LGMRES *)(ksp->data);
83: PetscReal res_norm, res;
84: PetscReal hapbnd, tt;
85: PetscScalar tmp;
86: PetscBool hapend = PETSC_FALSE; /* indicates happy breakdown ending */
87: PetscInt loc_it; /* local count of # of dir. in Krylov space */
88: PetscInt max_k = lgmres->max_k; /* max approx space size */
89: PetscInt max_it = ksp->max_it; /* max # of overall iterations for the method */
91: /* LGMRES_MOD - new variables*/
92: PetscInt aug_dim = lgmres->aug_dim;
93: PetscInt spot = 0;
94: PetscInt order = 0;
95: PetscInt it_arnoldi; /* number of arnoldi steps to take */
96: PetscInt it_total; /* total number of its to take (=approx space size)*/
97: PetscInt ii, jj;
98: PetscReal tmp_norm;
99: PetscScalar inv_tmp_norm;
100: PetscScalar *avec;
102: PetscFunctionBegin;
103: /* Number of pseudo iterations since last restart is the number
104: of prestart directions */
105: loc_it = 0;
107: /* LGMRES_MOD: determine number of arnoldi steps to take */
108: /* if approx_constant then we keep the space the same size even if
109: we don't have the full number of aug vectors yet*/
110: if (lgmres->approx_constant) it_arnoldi = max_k - lgmres->aug_ct;
111: else it_arnoldi = max_k - aug_dim;
113: it_total = it_arnoldi + lgmres->aug_ct;
115: /* initial residual is in VEC_VV(0) - compute its norm*/
116: PetscCall(VecNorm(VEC_VV(0), NORM_2, &res_norm));
117: KSPCheckNorm(ksp, res_norm);
118: res = res_norm;
120: /* first entry in right-hand-side of hessenberg system is just
121: the initial residual norm */
122: *GRS(0) = res_norm;
124: /* check for the convergence */
125: if (!res) {
126: if (itcount) *itcount = 0;
127: ksp->reason = KSP_CONVERGED_ATOL;
128: PetscCall(PetscInfo(ksp, "Converged due to zero residual norm on entry\n"));
129: PetscFunctionReturn(PETSC_SUCCESS);
130: }
132: /* scale VEC_VV (the initial residual) */
133: tmp = 1.0 / res_norm;
134: PetscCall(VecScale(VEC_VV(0), tmp));
136: if (ksp->normtype != KSP_NORM_NONE) ksp->rnorm = res;
137: else ksp->rnorm = 0.0;
139: /* note: (lgmres->it) is always set one less than (loc_it) It is used in
140: KSPBUILDSolution_LGMRES, where it is passed to KSPLGMRESBuildSoln.
141: Note that when KSPLGMRESBuildSoln is called from this function,
142: (loc_it -1) is passed, so the two are equivalent */
143: lgmres->it = (loc_it - 1);
145: /* MAIN ITERATION LOOP BEGINNING*/
147: /* keep iterating until we have converged OR generated the max number
148: of directions OR reached the max number of iterations for the method */
149: PetscCall((*ksp->converged)(ksp, ksp->its, res, &ksp->reason, ksp->cnvP));
151: while (!ksp->reason && loc_it < it_total && ksp->its < max_it) { /* LGMRES_MOD: changed to it_total */
152: PetscCall(KSPLogResidualHistory(ksp, res));
153: lgmres->it = (loc_it - 1);
154: PetscCall(KSPMonitor(ksp, ksp->its, res));
156: /* see if more space is needed for work vectors */
157: if (lgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
158: PetscCall(KSPLGMRESGetNewVectors(ksp, loc_it + 1));
159: /* (loc_it+1) is passed in as number of the first vector that should
160: be allocated */
161: }
163: /*LGMRES_MOD: decide whether this is an arnoldi step or an aug step */
164: if (loc_it < it_arnoldi) { /* Arnoldi */
165: PetscCall(KSP_PCApplyBAorAB(ksp, VEC_VV(loc_it), VEC_VV(1 + loc_it), VEC_TEMP_MATOP));
166: } else { /*aug step */
167: order = loc_it - it_arnoldi + 1; /* which aug step */
168: for (ii = 0; ii < aug_dim; ii++) {
169: if (lgmres->aug_order[ii] == order) {
170: spot = ii;
171: break; /* must have this because there will be duplicates before aug_ct = aug_dim */
172: }
173: }
175: PetscCall(VecCopy(A_AUGVEC(spot), VEC_VV(1 + loc_it)));
176: /*note: an alternate implementation choice would be to only save the AUGVECS and
177: not A_AUGVEC and then apply the PC here to the augvec */
178: }
180: /* update hessenberg matrix and do Gram-Schmidt - new direction is in
181: VEC_VV(1+loc_it)*/
182: PetscCall((*lgmres->orthog)(ksp, loc_it));
184: /* new entry in hessenburg is the 2-norm of our new direction */
185: PetscCall(VecNorm(VEC_VV(loc_it + 1), NORM_2, &tt));
187: *HH(loc_it + 1, loc_it) = tt;
188: *HES(loc_it + 1, loc_it) = tt;
190: /* check for the happy breakdown */
191: hapbnd = PetscAbsScalar(tt / *GRS(loc_it)); /* GRS(loc_it) contains the res_norm from the last iteration */
192: if (hapbnd > lgmres->haptol) hapbnd = lgmres->haptol;
193: if (tt > hapbnd) {
194: tmp = 1.0 / tt;
195: PetscCall(VecScale(VEC_VV(loc_it + 1), tmp)); /* scale new direction by its norm */
196: } else {
197: PetscCall(PetscInfo(ksp, "Detected happy breakdown, current hapbnd = %g tt = %g\n", (double)hapbnd, (double)tt));
198: hapend = PETSC_TRUE;
199: }
201: /* Now apply rotations to new col of hessenberg (and right side of system),
202: calculate new rotation, and get new residual norm at the same time*/
203: PetscCall(KSPLGMRESUpdateHessenberg(ksp, loc_it, hapend, &res));
204: if (ksp->reason) break;
206: loc_it++;
207: lgmres->it = (loc_it - 1); /* Add this here in case it has converged */
209: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
210: ksp->its++;
211: if (ksp->normtype != KSP_NORM_NONE) ksp->rnorm = res;
212: else ksp->rnorm = 0.0;
213: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
215: PetscCall((*ksp->converged)(ksp, ksp->its, res, &ksp->reason, ksp->cnvP));
217: /* Catch error in happy breakdown and signal convergence and break from loop */
218: if (hapend) {
219: if (!ksp->reason) {
220: PetscCheck(!ksp->errorifnotconverged, PetscObjectComm((PetscObject)ksp), PETSC_ERR_NOT_CONVERGED, "You reached the happy break down, but convergence was not indicated. Residual norm = %g", (double)res);
221: ksp->reason = KSP_DIVERGED_BREAKDOWN;
222: break;
223: }
224: }
225: }
226: /* END OF ITERATION LOOP */
227: PetscCall(KSPLogResidualHistory(ksp, res));
229: /* Monitor if we know that we will not return for a restart */
230: if (ksp->reason || ksp->its >= max_it) PetscCall(KSPMonitor(ksp, ksp->its, res));
232: if (itcount) *itcount = loc_it;
234: /*
235: Down here we have to solve for the "best" coefficients of the Krylov
236: columns, add the solution values together, and possibly unwind the
237: preconditioning from the solution
238: */
240: /* Form the solution (or the solution so far) */
241: /* Note: must pass in (loc_it-1) for iteration count so that KSPLGMRESBuildSoln
242: properly navigates */
244: PetscCall(KSPLGMRESBuildSoln(GRS(0), ksp->vec_sol, ksp->vec_sol, ksp, loc_it - 1));
246: /* LGMRES_MOD collect aug vector and A*augvector for future restarts -
247: only if we will be restarting (i.e. this cycle performed it_total
248: iterations) */
249: if (!ksp->reason && ksp->its < max_it && aug_dim > 0) {
250: /*AUG_TEMP contains the new augmentation vector (assigned in KSPLGMRESBuildSoln) */
251: if (!lgmres->aug_ct) {
252: spot = 0;
253: lgmres->aug_ct++;
254: } else if (lgmres->aug_ct < aug_dim) {
255: spot = lgmres->aug_ct;
256: lgmres->aug_ct++;
257: } else { /* truncate */
258: for (ii = 0; ii < aug_dim; ii++) {
259: if (lgmres->aug_order[ii] == aug_dim) spot = ii;
260: }
261: }
263: PetscCall(VecCopy(AUG_TEMP, AUGVEC(spot)));
264: /*need to normalize */
265: PetscCall(VecNorm(AUGVEC(spot), NORM_2, &tmp_norm));
267: inv_tmp_norm = 1.0 / tmp_norm;
269: PetscCall(VecScale(AUGVEC(spot), inv_tmp_norm));
271: /*set new aug vector to order 1 - move all others back one */
272: for (ii = 0; ii < aug_dim; ii++) AUG_ORDER(ii)++;
273: AUG_ORDER(spot) = 1;
275: /*now add the A*aug vector to A_AUGVEC(spot) - this is independent of preconditioning type*/
276: /* want V*H*y - y is in GRS, V is in VEC_VV and H is in HES */
278: /* first do H+*y */
279: avec = lgmres->hwork;
280: PetscCall(PetscArrayzero(avec, it_total + 1));
281: for (ii = 0; ii < it_total + 1; ii++) {
282: for (jj = 0; jj <= ii + 1 && jj < it_total + 1; jj++) avec[jj] += *HES(jj, ii) * *GRS(ii);
283: }
285: /*now multiply result by V+ */
286: PetscCall(VecSet(VEC_TEMP, 0.0));
287: PetscCall(VecMAXPY(VEC_TEMP, it_total + 1, avec, &VEC_VV(0))); /*answer is in VEC_TEMP*/
289: /*copy answer to aug location and scale*/
290: PetscCall(VecCopy(VEC_TEMP, A_AUGVEC(spot)));
291: PetscCall(VecScale(A_AUGVEC(spot), inv_tmp_norm));
292: }
293: PetscFunctionReturn(PETSC_SUCCESS);
294: }
296: /*
297: KSPSolve_LGMRES - This routine applies the LGMRES method.
299: Input Parameter:
300: . ksp - the Krylov space object that was set to use lgmres
302: Output Parameter:
303: . outits - number of iterations used
305: */
306: PetscErrorCode KSPSolve_LGMRES(KSP ksp)
307: {
308: PetscInt cycle_its; /* iterations done in a call to KSPLGMRESCycle */
309: PetscInt itcount; /* running total of iterations, incl. those in restarts */
310: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
311: PetscBool guess_zero = ksp->guess_zero;
312: PetscInt ii; /*LGMRES_MOD variable */
314: PetscFunctionBegin;
315: PetscCheck(!ksp->calc_sings || lgmres->Rsvd, PetscObjectComm((PetscObject)ksp), PETSC_ERR_ORDER, "Must call KSPSetComputeSingularValues() before KSPSetUp() is called");
317: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
319: ksp->its = 0;
320: lgmres->aug_ct = 0;
321: lgmres->matvecs = 0;
323: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
325: /* initialize */
326: itcount = 0;
327: /*LGMRES_MOD*/
328: for (ii = 0; ii < lgmres->aug_dim; ii++) lgmres->aug_order[ii] = 0;
330: while (!ksp->reason) {
331: /* calc residual - puts in VEC_VV(0) */
332: PetscCall(KSPInitialResidual(ksp, ksp->vec_sol, VEC_TEMP, VEC_TEMP_MATOP, VEC_VV(0), ksp->vec_rhs));
333: PetscCall(KSPLGMRESCycle(&cycle_its, ksp));
334: itcount += cycle_its;
335: if (itcount >= ksp->max_it) {
336: if (!ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
337: break;
338: }
339: ksp->guess_zero = PETSC_FALSE; /* every future call to KSPInitialResidual() will have nonzero guess */
340: }
341: ksp->guess_zero = guess_zero; /* restore if user provided nonzero initial guess */
342: PetscFunctionReturn(PETSC_SUCCESS);
343: }
345: /*
346: KSPDestroy_LGMRES - Frees all memory space used by the Krylov method.
347: */
348: PetscErrorCode KSPDestroy_LGMRES(KSP ksp)
349: {
350: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
352: PetscFunctionBegin;
353: PetscCall(PetscFree(lgmres->augvecs));
354: if (lgmres->augwork_alloc) PetscCall(VecDestroyVecs(lgmres->augwork_alloc, &lgmres->augvecs_user_work[0]));
355: PetscCall(PetscFree(lgmres->augvecs_user_work));
356: PetscCall(PetscFree(lgmres->aug_order));
357: PetscCall(PetscFree(lgmres->hwork));
358: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPLGMRESSetConstant_C", NULL));
359: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPLGMRESSetAugDim_C", NULL));
360: PetscCall(KSPDestroy_GMRES(ksp));
361: PetscFunctionReturn(PETSC_SUCCESS);
362: }
364: /*
365: KSPLGMRESBuildSoln - create the solution from the starting vector and the
366: current iterates.
368: Input parameters:
369: nrs - work area of size it + 1.
370: vguess - index of initial guess
371: vdest - index of result. Note that vguess may == vdest (replace
372: guess with the solution).
373: it - HH upper triangular part is a block of size (it+1) x (it+1)
375: This is an internal routine that knows about the LGMRES internals.
376: */
377: static PetscErrorCode KSPLGMRESBuildSoln(PetscScalar *nrs, Vec vguess, Vec vdest, KSP ksp, PetscInt it)
378: {
379: PetscScalar tt;
380: PetscInt ii, k, j;
381: KSP_LGMRES *lgmres = (KSP_LGMRES *)(ksp->data);
382: /*LGMRES_MOD */
383: PetscInt it_arnoldi, it_aug;
384: PetscInt jj, spot = 0;
386: PetscFunctionBegin;
387: /* Solve for solution vector that minimizes the residual */
389: /* If it is < 0, no lgmres steps have been performed */
390: if (it < 0) {
391: PetscCall(VecCopy(vguess, vdest)); /* VecCopy() is smart, exists immediately if vguess == vdest */
392: PetscFunctionReturn(PETSC_SUCCESS);
393: }
395: /* so (it+1) lgmres steps HAVE been performed */
397: /* LGMRES_MOD - determine if we need to use augvecs for the soln - do not assume that
398: this is called after the total its allowed for an approx space */
399: if (lgmres->approx_constant) {
400: it_arnoldi = lgmres->max_k - lgmres->aug_ct;
401: } else {
402: it_arnoldi = lgmres->max_k - lgmres->aug_dim;
403: }
404: if (it_arnoldi >= it + 1) {
405: it_aug = 0;
406: it_arnoldi = it + 1;
407: } else {
408: it_aug = (it + 1) - it_arnoldi;
409: }
411: /* now it_arnoldi indicates the number of matvecs that took place */
412: lgmres->matvecs += it_arnoldi;
414: /* solve the upper triangular system - GRS is the right side and HH is
415: the upper triangular matrix - put soln in nrs */
416: 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)));
417: if (*HH(it, it) != 0.0) {
418: nrs[it] = *GRS(it) / *HH(it, it);
419: } else {
420: nrs[it] = 0.0;
421: }
423: for (ii = 1; ii <= it; ii++) {
424: k = it - ii;
425: tt = *GRS(k);
426: for (j = k + 1; j <= it; j++) tt = tt - *HH(k, j) * nrs[j];
427: nrs[k] = tt / *HH(k, k);
428: }
430: /* Accumulate the correction to the soln of the preconditioned prob. in VEC_TEMP */
431: PetscCall(VecSet(VEC_TEMP, 0.0)); /* set VEC_TEMP components to 0 */
433: /*LGMRES_MOD - if augmenting has happened we need to form the solution
434: using the augvecs */
435: if (!it_aug) { /* all its are from arnoldi */
436: PetscCall(VecMAXPY(VEC_TEMP, it + 1, nrs, &VEC_VV(0)));
437: } else { /*use aug vecs */
438: /*first do regular krylov directions */
439: PetscCall(VecMAXPY(VEC_TEMP, it_arnoldi, nrs, &VEC_VV(0)));
440: /*now add augmented portions - add contribution of aug vectors one at a time*/
442: for (ii = 0; ii < it_aug; ii++) {
443: for (jj = 0; jj < lgmres->aug_dim; jj++) {
444: if (lgmres->aug_order[jj] == (ii + 1)) {
445: spot = jj;
446: break; /* must have this because there will be duplicates before aug_ct = aug_dim */
447: }
448: }
449: PetscCall(VecAXPY(VEC_TEMP, nrs[it_arnoldi + ii], AUGVEC(spot)));
450: }
451: }
452: /* now VEC_TEMP is what we want to keep for augmenting purposes - grab before the
453: preconditioner is "unwound" from right-precondtioning*/
454: PetscCall(VecCopy(VEC_TEMP, AUG_TEMP));
456: PetscCall(KSPUnwindPreconditioner(ksp, VEC_TEMP, VEC_TEMP_MATOP));
458: /* add solution to previous solution */
459: /* put updated solution into vdest.*/
460: PetscCall(VecCopy(vguess, vdest));
461: PetscCall(VecAXPY(vdest, 1.0, VEC_TEMP));
462: PetscFunctionReturn(PETSC_SUCCESS);
463: }
465: /*
466: KSPLGMRESUpdateHessenberg - Do the scalar work for the orthogonalization.
467: Return new residual.
469: input parameters:
471: . ksp - Krylov space object
472: . it - plane rotations are applied to the (it+1)th column of the
473: modified hessenberg (i.e. HH(:,it))
474: . hapend - PETSC_FALSE not happy breakdown ending.
476: output parameters:
477: . res - the new residual
479: */
480: static PetscErrorCode KSPLGMRESUpdateHessenberg(KSP ksp, PetscInt it, PetscBool hapend, PetscReal *res)
481: {
482: PetscScalar *hh, *cc, *ss, tt;
483: PetscInt j;
484: KSP_LGMRES *lgmres = (KSP_LGMRES *)(ksp->data);
486: PetscFunctionBegin;
487: hh = HH(0, it); /* pointer to beginning of column to update - so
488: incrementing hh "steps down" the (it+1)th col of HH*/
489: cc = CC(0); /* beginning of cosine rotations */
490: ss = SS(0); /* beginning of sine rotations */
492: /* Apply all the previously computed plane rotations to the new column
493: of the Hessenberg matrix */
494: /* Note: this uses the rotation [conj(c) s ; -s c], c= cos(theta), s= sin(theta) */
496: for (j = 1; j <= it; j++) {
497: tt = *hh;
498: *hh = PetscConj(*cc) * tt + *ss * *(hh + 1);
499: hh++;
500: *hh = *cc++ * *hh - (*ss++ * tt);
501: /* hh, cc, and ss have all been incremented one by end of loop */
502: }
504: /*
505: compute the new plane rotation, and apply it to:
506: 1) the right-hand-side of the Hessenberg system (GRS)
507: note: it affects GRS(it) and GRS(it+1)
508: 2) the new column of the Hessenberg matrix
509: note: it affects HH(it,it) which is currently pointed to
510: by hh and HH(it+1, it) (*(hh+1))
511: thus obtaining the updated value of the residual...
512: */
514: /* compute new plane rotation */
516: if (!hapend) {
517: tt = PetscSqrtScalar(PetscConj(*hh) * *hh + PetscConj(*(hh + 1)) * *(hh + 1));
518: if (tt == 0.0) {
519: ksp->reason = KSP_DIVERGED_NULL;
520: PetscFunctionReturn(PETSC_SUCCESS);
521: }
522: *cc = *hh / tt; /* new cosine value */
523: *ss = *(hh + 1) / tt; /* new sine value */
525: /* apply to 1) and 2) */
526: *GRS(it + 1) = -(*ss * *GRS(it));
527: *GRS(it) = PetscConj(*cc) * *GRS(it);
528: *hh = PetscConj(*cc) * *hh + *ss * *(hh + 1);
530: /* residual is the last element (it+1) of right-hand side! */
531: *res = PetscAbsScalar(*GRS(it + 1));
533: } else { /* happy breakdown: HH(it+1, it) = 0, therefore we don't need to apply
534: another rotation matrix (so RH doesn't change). The new residual is
535: always the new sine term times the residual from last time (GRS(it)),
536: but now the new sine rotation would be zero...so the residual should
537: be zero...so we will multiply "zero" by the last residual. This might
538: not be exactly what we want to do here -could just return "zero". */
540: *res = 0.0;
541: }
542: PetscFunctionReturn(PETSC_SUCCESS);
543: }
545: /*
547: KSPLGMRESGetNewVectors - This routine allocates more work vectors, starting from
548: VEC_VV(it)
550: */
551: static PetscErrorCode KSPLGMRESGetNewVectors(KSP ksp, PetscInt it)
552: {
553: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
554: PetscInt nwork = lgmres->nwork_alloc; /* number of work vector chunks allocated */
555: PetscInt nalloc; /* number to allocate */
556: PetscInt k;
558: PetscFunctionBegin;
559: nalloc = lgmres->delta_allocate; /* number of vectors to allocate
560: in a single chunk */
562: /* Adjust the number to allocate to make sure that we don't exceed the
563: number of available slots (lgmres->vecs_allocated)*/
564: if (it + VEC_OFFSET + nalloc >= lgmres->vecs_allocated) nalloc = lgmres->vecs_allocated - it - VEC_OFFSET;
565: if (!nalloc) PetscFunctionReturn(PETSC_SUCCESS);
567: lgmres->vv_allocated += nalloc; /* vv_allocated is the number of vectors allocated */
569: /* work vectors */
570: PetscCall(KSPCreateVecs(ksp, nalloc, &lgmres->user_work[nwork], 0, NULL));
571: /* specify size of chunk allocated */
572: lgmres->mwork_alloc[nwork] = nalloc;
574: for (k = 0; k < nalloc; k++) lgmres->vecs[it + VEC_OFFSET + k] = lgmres->user_work[nwork][k];
576: /* LGMRES_MOD - for now we are preallocating the augmentation vectors */
578: /* increment the number of work vector chunks */
579: lgmres->nwork_alloc++;
580: PetscFunctionReturn(PETSC_SUCCESS);
581: }
583: /*
584: KSPBuildSolution_LGMRES
586: Input Parameter:
587: . ksp - the Krylov space object
588: . ptr-
590: Output Parameter:
591: . result - the solution
593: Note: this calls KSPLGMRESBuildSoln - the same function that KSPLGMRESCycle
594: calls directly.
596: */
597: PetscErrorCode KSPBuildSolution_LGMRES(KSP ksp, Vec ptr, Vec *result)
598: {
599: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
601: PetscFunctionBegin;
602: if (!ptr) {
603: if (!lgmres->sol_temp) PetscCall(VecDuplicate(ksp->vec_sol, &lgmres->sol_temp));
604: ptr = lgmres->sol_temp;
605: }
606: if (!lgmres->nrs) {
607: /* allocate the work area */
608: PetscCall(PetscMalloc1(lgmres->max_k, &lgmres->nrs));
609: }
611: PetscCall(KSPLGMRESBuildSoln(lgmres->nrs, ksp->vec_sol, ptr, ksp, lgmres->it));
612: if (result) *result = ptr;
613: PetscFunctionReturn(PETSC_SUCCESS);
614: }
616: PetscErrorCode KSPView_LGMRES(KSP ksp, PetscViewer viewer)
617: {
618: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
619: PetscBool iascii;
621: PetscFunctionBegin;
622: PetscCall(KSPView_GMRES(ksp, viewer));
623: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
624: if (iascii) {
625: /*LGMRES_MOD */
626: PetscCall(PetscViewerASCIIPrintf(viewer, " aug. dimension=%" PetscInt_FMT "\n", lgmres->aug_dim));
627: if (lgmres->approx_constant) PetscCall(PetscViewerASCIIPrintf(viewer, " approx. space size was kept constant.\n"));
628: PetscCall(PetscViewerASCIIPrintf(viewer, " number of matvecs=%" PetscInt_FMT "\n", lgmres->matvecs));
629: }
630: PetscFunctionReturn(PETSC_SUCCESS);
631: }
633: PetscErrorCode KSPSetFromOptions_LGMRES(KSP ksp, PetscOptionItems *PetscOptionsObject)
634: {
635: PetscInt aug;
636: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
637: PetscBool flg = PETSC_FALSE;
639: PetscFunctionBegin;
640: PetscCall(KSPSetFromOptions_GMRES(ksp, PetscOptionsObject));
641: PetscOptionsHeadBegin(PetscOptionsObject, "KSP LGMRES Options");
642: PetscCall(PetscOptionsBool("-ksp_lgmres_constant", "Use constant approx. space size", "KSPGMRESSetConstant", lgmres->approx_constant, &lgmres->approx_constant, NULL));
643: PetscCall(PetscOptionsInt("-ksp_lgmres_augment", "Number of error approximations to augment the Krylov space with", "KSPLGMRESSetAugDim", lgmres->aug_dim, &aug, &flg));
644: if (flg) PetscCall(KSPLGMRESSetAugDim(ksp, aug));
645: PetscOptionsHeadEnd();
646: PetscFunctionReturn(PETSC_SUCCESS);
647: }
649: /*functions for extra lgmres options here*/
650: static PetscErrorCode KSPLGMRESSetConstant_LGMRES(KSP ksp)
651: {
652: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
654: PetscFunctionBegin;
655: lgmres->approx_constant = PETSC_TRUE;
656: PetscFunctionReturn(PETSC_SUCCESS);
657: }
659: static PetscErrorCode KSPLGMRESSetAugDim_LGMRES(KSP ksp, PetscInt aug_dim)
660: {
661: KSP_LGMRES *lgmres = (KSP_LGMRES *)ksp->data;
663: PetscFunctionBegin;
664: PetscCheck(aug_dim >= 0, PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_OUTOFRANGE, "Augmentation dimension must be positive");
665: PetscCheck(aug_dim <= (lgmres->max_k - 1), PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_OUTOFRANGE, "Augmentation dimension must be <= (restart size-1)");
666: lgmres->aug_dim = aug_dim;
667: PetscFunctionReturn(PETSC_SUCCESS);
668: }
670: /*MC
671: KSPLGMRES - Augments the standard GMRES approximation space with approximations to
672: the error from previous restart cycles.
674: Options Database Keys:
675: + -ksp_gmres_restart <restart> - total approximation space size (Krylov directions + error approximations)
676: . -ksp_gmres_haptol <tol> - sets the tolerance for "happy ending" (exact convergence)
677: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of
678: vectors are allocated as needed)
679: . -ksp_gmres_classicalgramschmidt - use classical (unmodified) Gram-Schmidt to orthogonalize against the Krylov space (fast) (the default)
680: . -ksp_gmres_modifiedgramschmidt - use modified Gram-Schmidt in the orthogonalization (more stable, but slower)
681: . -ksp_gmres_cgs_refinement_type <refine_never,refine_ifneeded,refine_always> - determine if iterative refinement is used to increase the
682: stability of the classical Gram-Schmidt orthogonalization.
683: . -ksp_gmres_krylov_monitor - plot the Krylov space generated
684: . -ksp_lgmres_augment <k> - number of error approximations to augment the Krylov space with
685: - -ksp_lgmres_constant - use a constant approx. space size (only affects restart cycles < num. error approx.(k), i.e. the first k restarts)
687: To run LGMRES(m, k) as described in [1], use:
688: -ksp_gmres_restart <m+k>
689: -ksp_lgmres_augment <k>
691: Level: beginner
693: Note:
694: Supports both left and right preconditioning, but not symmetric.
696: Developer Note:
697: This object is subclassed off of `KSPGMRES`
699: Contributed by:
700: Allison Baker
702: References:
703: . [1] - A. H. Baker, E.R. Jessup, and T.A. Manteuffel. A technique for accelerating the convergence of restarted GMRES. SIAM Journal on Matrix Analysis and Applications, 26 (2005).
705: .seealso: [](ch_ksp), `KSPCreate()`, `KSPSetType()`, `KSPType`, `KSP`, `KSPFGMRES`, `KSPGMRES`,
706: `KSPGMRESSetRestart()`, `KSPGMRESSetHapTol()`, `KSPGMRESSetPreAllocateVectors()`, `KSPGMRESSetOrthogonalization()`, `KSPGMRESGetOrthogonalization()`,
707: `KSPGMRESClassicalGramSchmidtOrthogonalization()`, `KSPGMRESModifiedGramSchmidtOrthogonalization()`,
708: `KSPGMRESCGSRefinementType`, `KSPGMRESSetCGSRefinementType()`, `KSPGMRESGetCGSRefinementType()`, `KSPGMRESMonitorKrylov()`, `KSPLGMRESSetAugDim()`,
709: `KSPGMRESSetConstant()`
710: M*/
712: PETSC_EXTERN PetscErrorCode KSPCreate_LGMRES(KSP ksp)
713: {
714: KSP_LGMRES *lgmres;
716: PetscFunctionBegin;
717: PetscCall(PetscNew(&lgmres));
719: ksp->data = (void *)lgmres;
720: ksp->ops->buildsolution = KSPBuildSolution_LGMRES;
722: ksp->ops->setup = KSPSetUp_LGMRES;
723: ksp->ops->solve = KSPSolve_LGMRES;
724: ksp->ops->destroy = KSPDestroy_LGMRES;
725: ksp->ops->view = KSPView_LGMRES;
726: ksp->ops->setfromoptions = KSPSetFromOptions_LGMRES;
727: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
728: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
730: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_PRECONDITIONED, PC_LEFT, 3));
731: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_RIGHT, 2));
732: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_RIGHT, 1));
734: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetPreAllocateVectors_C", KSPGMRESSetPreAllocateVectors_GMRES));
735: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetOrthogonalization_C", KSPGMRESSetOrthogonalization_GMRES));
736: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESGetOrthogonalization_C", KSPGMRESGetOrthogonalization_GMRES));
737: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetRestart_C", KSPGMRESSetRestart_GMRES));
738: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESGetRestart_C", KSPGMRESGetRestart_GMRES));
739: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetHapTol_C", KSPGMRESSetHapTol_GMRES));
740: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetCGSRefinementType_C", KSPGMRESSetCGSRefinementType_GMRES));
741: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESGetCGSRefinementType_C", KSPGMRESGetCGSRefinementType_GMRES));
743: /*LGMRES_MOD add extra functions here - like the one to set num of aug vectors */
744: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPLGMRESSetConstant_C", KSPLGMRESSetConstant_LGMRES));
745: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPLGMRESSetAugDim_C", KSPLGMRESSetAugDim_LGMRES));
747: /*defaults */
748: lgmres->haptol = 1.0e-30;
749: lgmres->q_preallocate = 0;
750: lgmres->delta_allocate = LGMRES_DELTA_DIRECTIONS;
751: lgmres->orthog = KSPGMRESClassicalGramSchmidtOrthogonalization;
752: lgmres->nrs = NULL;
753: lgmres->sol_temp = NULL;
754: lgmres->max_k = LGMRES_DEFAULT_MAXK;
755: lgmres->Rsvd = NULL;
756: lgmres->cgstype = KSP_GMRES_CGS_REFINE_NEVER;
757: lgmres->orthogwork = NULL;
759: /*LGMRES_MOD - new defaults */
760: lgmres->aug_dim = LGMRES_DEFAULT_AUGDIM;
761: lgmres->aug_ct = 0; /* start with no aug vectors */
762: lgmres->approx_constant = PETSC_FALSE;
763: lgmres->matvecs = 0;
764: PetscFunctionReturn(PETSC_SUCCESS);
765: }