Actual source code: pipefgmres.c
1: #include <../src/ksp/ksp/impls/gmres/pipefgmres/pipefgmresimpl.h>
3: static PetscBool cited = PETSC_FALSE;
4: static const char citation[] = "@article{SSM2016,\n"
5: " author = {P. Sanan and S.M. Schnepp and D.A. May},\n"
6: " title = {Pipelined, Flexible Krylov Subspace Methods},\n"
7: " journal = {SIAM Journal on Scientific Computing},\n"
8: " volume = {38},\n"
9: " number = {5},\n"
10: " pages = {C441-C470},\n"
11: " year = {2016},\n"
12: " doi = {10.1137/15M1049130},\n"
13: " URL = {http://dx.doi.org/10.1137/15M1049130},\n"
14: " eprint = {http://dx.doi.org/10.1137/15M1049130}\n"
15: "}\n";
17: static PetscErrorCode KSPPIPEFGMRESGetNewVectors(KSP, PetscInt);
18: static PetscErrorCode KSPPIPEFGMRESUpdateHessenberg(KSP, PetscInt, PetscBool *, PetscReal *);
19: static PetscErrorCode KSPPIPEFGMRESBuildSoln(PetscScalar *, Vec, Vec, KSP, PetscInt);
20: extern PetscErrorCode KSPReset_PIPEFGMRES(KSP);
22: static PetscErrorCode KSPSetUp_PIPEFGMRES(KSP ksp)
23: {
24: PetscInt k;
25: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
26: const PetscInt max_k = pipefgmres->max_k;
28: PetscFunctionBegin;
29: PetscCall(KSPSetUp_GMRES(ksp));
31: PetscCall(PetscMalloc1(VEC_OFFSET + max_k, &pipefgmres->prevecs));
32: PetscCall(PetscMalloc1(VEC_OFFSET + max_k, &pipefgmres->prevecs_user_work));
34: PetscCall(KSPCreateVecs(ksp, pipefgmres->vv_allocated, &pipefgmres->prevecs_user_work[0], 0, NULL));
35: for (k = 0; k < pipefgmres->vv_allocated; k++) pipefgmres->prevecs[k] = pipefgmres->prevecs_user_work[0][k];
37: PetscCall(PetscMalloc1(VEC_OFFSET + max_k, &pipefgmres->zvecs));
38: PetscCall(PetscMalloc1(VEC_OFFSET + max_k, &pipefgmres->zvecs_user_work));
40: PetscCall(PetscMalloc1(VEC_OFFSET + max_k, &pipefgmres->redux));
42: PetscCall(KSPCreateVecs(ksp, pipefgmres->vv_allocated, &pipefgmres->zvecs_user_work[0], 0, NULL));
43: for (k = 0; k < pipefgmres->vv_allocated; k++) pipefgmres->zvecs[k] = pipefgmres->zvecs_user_work[0][k];
44: PetscFunctionReturn(PETSC_SUCCESS);
45: }
47: static PetscErrorCode KSPPIPEFGMRESCycle(PetscInt *itcount, KSP ksp)
48: {
49: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
50: PetscReal res_norm;
51: PetscReal hapbnd, tt;
52: PetscScalar *hh, *hes, *lhh, shift = pipefgmres->shift;
53: PetscBool hapend = PETSC_FALSE; /* indicates happy breakdown ending */
54: PetscInt loc_it; /* local count of # of dir. in Krylov space */
55: PetscInt max_k = pipefgmres->max_k; /* max # of directions Krylov space */
56: PetscInt i, j, k;
57: Mat Amat, Pmat;
58: Vec Q, W; /* Pipelining vectors */
59: Vec *redux = pipefgmres->redux; /* workspace for single reduction */
61: PetscFunctionBegin;
62: if (itcount) *itcount = 0;
64: /* Assign simpler names to these vectors, allocated as pipelining workspace */
65: Q = VEC_Q;
66: W = VEC_W;
68: /* Allocate memory for orthogonalization work */
69: /* Note that we add an extra value here to allow for a single reduction */
70: if (ksp->lorthogwork < pipefgmres->max_k + 2) {
71: PetscCall(PetscFree(ksp->orthogwork));
72: ksp->lorthogwork = pipefgmres->max_k + 2;
73: PetscCall(PetscMalloc1(ksp->lorthogwork, &ksp->orthogwork));
74: }
75: lhh = ksp->orthogwork;
76: /* Number of pseudo iterations since last restart is the number
77: of prestart directions */
78: loc_it = 0;
80: /* note: (pipefgmres->it) is always set one less than (loc_it) It is used in
81: KSPBUILDSolution_PIPEFGMRES, where it is passed to KSPPIPEFGMRESBuildSoln.
82: Note that when KSPPIPEFGMRESBuildSoln is called from this function,
83: (loc_it -1) is passed, so the two are equivalent */
84: pipefgmres->it = (loc_it - 1);
86: /* initial residual is in VEC_VV(0) - compute its norm*/
87: PetscCall(VecNorm(VEC_VV(0), NORM_2, &res_norm));
89: /* first entry in the right-hand side of the Hessenberg system is just
90: the initial residual norm */
91: *RS(0) = res_norm;
93: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
94: if (ksp->normtype != KSP_NORM_NONE) ksp->rnorm = res_norm;
95: else ksp->rnorm = 0;
96: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
97: PetscCall(KSPLogResidualHistory(ksp, ksp->rnorm));
98: PetscCall(KSPMonitor(ksp, ksp->its, ksp->rnorm));
100: /* check for the convergence - maybe the current guess is good enough */
101: PetscCall((*ksp->converged)(ksp, ksp->its, ksp->rnorm, &ksp->reason, ksp->cnvP));
102: if (ksp->reason) {
103: if (itcount) *itcount = 0;
104: PetscFunctionReturn(PETSC_SUCCESS);
105: }
107: /* scale VEC_VV (the initial residual) */
108: PetscCall(VecScale(VEC_VV(0), 1.0 / res_norm));
110: /* Fill the pipeline */
111: PetscCall(KSP_PCApply(ksp, VEC_VV(loc_it), PREVEC(loc_it)));
112: PetscCall(PCGetOperators(ksp->pc, &Amat, &Pmat));
113: PetscCall(KSP_MatMult(ksp, Amat, PREVEC(loc_it), ZVEC(loc_it)));
114: PetscCall(VecAXPY(ZVEC(loc_it), -shift, VEC_VV(loc_it))); /* Note shift */
116: /* MAIN ITERATION LOOP BEGINNING*/
117: /* keep iterating until we have converged OR generated the max number
118: of directions OR reached the max number of iterations for the method */
119: while (!ksp->reason && loc_it < max_k && ksp->its < ksp->max_it) {
120: if (loc_it) {
121: PetscCall(KSPLogResidualHistory(ksp, res_norm));
122: PetscCall(KSPMonitor(ksp, ksp->its, res_norm));
123: }
124: pipefgmres->it = (loc_it - 1);
126: /* see if more space is needed for work vectors */
127: if (pipefgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
128: PetscCall(KSPPIPEFGMRESGetNewVectors(ksp, loc_it + 1));
129: /* (loc_it+1) is passed in as number of the first vector that should be allocated */
130: }
132: /* Note that these inner products are with "Z" now, so
133: in particular, lhh[loc_it] is the 'barred' or 'shifted' value,
134: not the value from the equivalent FGMRES run (even in exact arithmetic)
135: That is, the H we need for the Arnoldi relation is different from the
136: coefficients we use in the orthogonalization process,because of the shift */
138: /* Do some local twiddling to allow for a single reduction */
139: for (i = 0; i < loc_it + 1; i++) redux[i] = VEC_VV(i);
140: redux[loc_it + 1] = ZVEC(loc_it);
142: /* note the extra dot product which ends up in lh[loc_it+1], which computes ||z||^2 */
143: PetscCall(VecMDotBegin(ZVEC(loc_it), loc_it + 2, redux, lhh));
145: /* Start the split reduction (This actually calls the MPI_Iallreduce, otherwise, the reduction is simply delayed until the "end" call)*/
146: PetscCall(PetscCommSplitReductionBegin(PetscObjectComm((PetscObject)ZVEC(loc_it))));
148: /* The work to be overlapped with the inner products follows.
149: This is application of the preconditioner and the operator
150: to compute intermediate quantities which will be combined (locally)
151: with the results of the inner products.
152: */
153: PetscCall(KSP_PCApply(ksp, ZVEC(loc_it), Q));
154: PetscCall(PCGetOperators(ksp->pc, &Amat, &Pmat));
155: PetscCall(KSP_MatMult(ksp, Amat, Q, W));
157: /* Compute inner products of the new direction with previous directions,
158: and the norm of the to-be-orthogonalized direction "Z".
159: This information is enough to build the required entries
160: of H. The inner product with VEC_VV(it_loc) is
161: *different* than in the standard FGMRES and need to be dealt with specially.
162: That is, for standard FGMRES the orthogonalization coefficients are the same
163: as the coefficients used in the Arnoldi relation to reconstruct, but here this
164: is not true (albeit only for the one entry of H which we "unshift" below. */
166: /* Finish the dot product, retrieving the extra entry */
167: PetscCall(VecMDotEnd(ZVEC(loc_it), loc_it + 2, redux, lhh));
168: tt = PetscRealPart(lhh[loc_it + 1]);
170: /* Hessenberg entries, and entries for (naive) classical Gram-Schmidt
171: Note that the Hessenberg entries require a shift, as these are for the
172: relation AU = VH, which is wrt unshifted basis vectors */
173: hh = HH(0, loc_it);
174: hes = HES(0, loc_it);
175: for (j = 0; j < loc_it; j++) {
176: hh[j] = lhh[j];
177: hes[j] = lhh[j];
178: }
179: hh[loc_it] = lhh[loc_it] + shift;
180: hes[loc_it] = lhh[loc_it] + shift;
182: /* we delay applying the shift here */
183: for (j = 0; j <= loc_it; j++) lhh[j] = -lhh[j]; /* flip sign */
185: /* Compute the norm of the un-normalized new direction using the rearranged formula
186: Note that these are shifted ("barred") quantities */
187: for (k = 0; k <= loc_it; k++) tt -= PetscAbsScalar(lhh[k]) * PetscAbsScalar(lhh[k]);
188: /* On AVX512 this is accumulating roundoff errors for eg: tt=-2.22045e-16 */
189: if ((tt < 0.0) && tt > -PETSC_SMALL) tt = 0.0;
190: if (tt < 0.0) {
191: /* If we detect square root breakdown in the norm, we must restart the algorithm.
192: Here this means we simply break the current loop and reconstruct the solution
193: using the basis we have computed thus far. Note that by breaking immediately,
194: we do not update the iteration count, so computation done in this iteration
195: should be disregarded.
196: */
197: PetscCall(PetscInfo(ksp, "Restart due to square root breakdown at it = %" PetscInt_FMT ", tt=%g\n", ksp->its, (double)tt));
198: break;
199: } else {
200: tt = PetscSqrtReal(tt);
201: }
203: /* new entry in Hessenberg is the 2-norm of our new direction */
204: hh[loc_it + 1] = tt;
205: hes[loc_it + 1] = tt;
207: /* The recurred computation for the new direction
208: The division by tt is delayed to the happy breakdown check later
209: Note placement BEFORE the unshift
210: */
211: PetscCall(VecCopy(ZVEC(loc_it), VEC_VV(loc_it + 1)));
212: PetscCall(VecMAXPY(VEC_VV(loc_it + 1), loc_it + 1, lhh, &VEC_VV(0)));
213: /* (VEC_VV(loc_it+1) is not normalized yet) */
215: /* The recurred computation for the preconditioned vector (u) */
216: PetscCall(VecCopy(Q, PREVEC(loc_it + 1)));
217: PetscCall(VecMAXPY(PREVEC(loc_it + 1), loc_it + 1, lhh, &PREVEC(0)));
218: if (tt) PetscCall(VecScale(PREVEC(loc_it + 1), 1.0 / tt));
220: /* Unshift an entry in the GS coefficients ("removing the bar") */
221: lhh[loc_it] -= shift;
223: /* The recurred computation for z (Au)
224: Note placement AFTER the "unshift" */
225: PetscCall(VecCopy(W, ZVEC(loc_it + 1)));
226: PetscCall(VecMAXPY(ZVEC(loc_it + 1), loc_it + 1, lhh, &ZVEC(0)));
227: if (tt) PetscCall(VecScale(ZVEC(loc_it + 1), 1.0 / tt));
229: /* Happy Breakdown Check */
230: hapbnd = PetscAbsScalar((tt) / *RS(loc_it));
231: /* RS(loc_it) contains the res_norm from the last iteration */
232: hapbnd = PetscMin(pipefgmres->haptol, hapbnd);
233: if (tt > hapbnd) {
234: /* scale new direction by its norm */
235: PetscCall(VecScale(VEC_VV(loc_it + 1), 1.0 / tt));
236: } else {
237: /* This happens when the solution is exactly reached. */
238: /* So there is no new direction... */
239: PetscCall(VecSet(VEC_TEMP, 0.0)); /* set VEC_TEMP to 0 */
240: hapend = PETSC_TRUE;
241: }
242: /* note that for pipefgmres we could get HES(loc_it+1, loc_it) = 0 and the
243: current solution would not be exact if HES was singular. Note that
244: HH non-singular implies that HES is not singular, and HES is guaranteed
245: to be nonsingular when PREVECS are linearly independent and A is
246: nonsingular (in GMRES, the nonsingularity of A implies the nonsingularity
247: of HES). So we should really add a check to verify that HES is nonsingular.*/
249: /* Note that to be thorough, in debug mode, one could call a LAPACK routine
250: here to check that the Hessenberg matrix is indeed non-singular (since
251: FGMRES does not guarantee this) */
253: /* Now apply rotations to new col of Hessenberg (and right side of system),
254: calculate new rotation, and get new residual norm at the same time*/
255: PetscCall(KSPPIPEFGMRESUpdateHessenberg(ksp, loc_it, &hapend, &res_norm));
256: if (ksp->reason) break;
258: loc_it++;
259: pipefgmres->it = (loc_it - 1); /* Add this here in case it has converged */
261: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
262: ksp->its++;
263: if (ksp->normtype != KSP_NORM_NONE) ksp->rnorm = res_norm;
264: else ksp->rnorm = 0;
265: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
267: PetscCall((*ksp->converged)(ksp, ksp->its, ksp->rnorm, &ksp->reason, ksp->cnvP));
269: /* Catch error in happy breakdown and signal convergence and break from loop */
270: if (hapend) {
271: if (!ksp->reason) {
272: PetscCheck(!ksp->errorifnotconverged, PetscObjectComm((PetscObject)ksp), PETSC_ERR_NOT_CONVERGED, "Reached happy break down, but convergence was not indicated. Residual norm = %g", (double)res_norm);
273: ksp->reason = KSP_DIVERGED_BREAKDOWN;
274: break;
275: }
276: }
277: }
278: /* END OF ITERATION LOOP */
280: if (itcount) *itcount = loc_it;
282: /*
283: Solve for the "best" coefficients of the Krylov
284: columns, add the solution values together, and possibly unwind the
285: preconditioning from the solution
286: */
288: /* Form the solution (or the solution so far) */
289: /* Note: must pass in (loc_it-1) for iteration count so that KSPPIPEGMRESIIBuildSoln properly navigates */
291: PetscCall(KSPPIPEFGMRESBuildSoln(RS(0), ksp->vec_sol, ksp->vec_sol, ksp, loc_it - 1));
293: /*
294: Monitor if we know that we will not return for a restart
295: */
296: if (ksp->reason == KSP_CONVERGED_ITERATING && ksp->its >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
297: if (loc_it && ksp->reason) {
298: PetscCall(KSPMonitor(ksp, ksp->its, ksp->rnorm));
299: PetscCall(KSPLogResidualHistory(ksp, ksp->rnorm));
300: }
301: PetscFunctionReturn(PETSC_SUCCESS);
302: }
304: static PetscErrorCode KSPSolve_PIPEFGMRES(KSP ksp)
305: {
306: PetscInt its, itcount;
307: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
308: PetscBool guess_zero = ksp->guess_zero;
310: PetscFunctionBegin;
311: /* We have not checked these routines for use with complex numbers. The inner products are likely not defined correctly for that case */
312: PetscCheck(!PetscDefined(USE_COMPLEX) || PetscDefined(SKIP_COMPLEX), PETSC_COMM_WORLD, PETSC_ERR_SUP, "PIPEFGMRES has not been implemented for use with complex scalars");
314: PetscCall(PetscCitationsRegister(citation, &cited));
316: PetscCheck(!ksp->calc_sings || pipefgmres->Rsvd, PetscObjectComm((PetscObject)ksp), PETSC_ERR_ORDER, "Must call KSPSetComputeSingularValues() before KSPSetUp() is called");
317: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)ksp));
318: ksp->its = 0;
319: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)ksp));
321: itcount = 0;
322: ksp->reason = KSP_CONVERGED_ITERATING;
323: while (!ksp->reason) {
324: PetscCall(KSPInitialResidual(ksp, ksp->vec_sol, VEC_TEMP, VEC_TEMP_MATOP, VEC_VV(0), ksp->vec_rhs));
325: PetscCall(KSPPIPEFGMRESCycle(&its, ksp));
326: itcount += its;
327: if (itcount >= ksp->max_it) {
328: if (!ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
329: break;
330: }
331: ksp->guess_zero = PETSC_FALSE; /* every future call to KSPInitialResidual() will have nonzero guess */
332: }
333: ksp->guess_zero = guess_zero; /* restore if user provided nonzero initial guess */
334: PetscFunctionReturn(PETSC_SUCCESS);
335: }
337: static PetscErrorCode KSPDestroy_PIPEFGMRES(KSP ksp)
338: {
339: PetscFunctionBegin;
340: PetscCall(KSPReset_PIPEFGMRES(ksp));
341: PetscCall(KSPDestroy_GMRES(ksp));
342: PetscFunctionReturn(PETSC_SUCCESS);
343: }
345: static PetscErrorCode KSPPIPEFGMRESBuildSoln(PetscScalar *nrs, Vec vguess, Vec vdest, KSP ksp, PetscInt it)
346: {
347: PetscScalar tt;
348: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
350: PetscFunctionBegin;
351: if (it < 0) { /* no pipefgmres steps have been performed */
352: PetscCall(VecCopy(vguess, vdest)); /* VecCopy() is smart, exits immediately if vguess == vdest */
353: PetscFunctionReturn(PETSC_SUCCESS);
354: }
356: /* Solve for solution vector that minimizes the residual */
357: /* solve the upper triangular system - RS is the right side and HH is
358: the upper triangular matrix - put soln in nrs */
359: if (*HH(it, it) != 0.0) nrs[it] = *RS(it) / *HH(it, it);
360: else nrs[it] = 0.0;
362: for (PetscInt k = it - 1; k >= 0; k--) {
363: tt = *RS(k);
364: for (PetscInt j = k + 1; j <= it; j++) tt -= *HH(k, j) * nrs[j];
365: nrs[k] = tt / *HH(k, k);
366: }
368: /* Accumulate the correction to the solution of the preconditioned problem in VEC_TEMP */
369: PetscCall(VecMAXPBY(VEC_TEMP, it + 1, nrs, 0, &PREVEC(0)));
371: /* add solution to previous solution */
372: if (vdest == vguess) PetscCall(VecAXPY(vdest, 1.0, VEC_TEMP));
373: else PetscCall(VecWAXPY(vdest, 1.0, VEC_TEMP, vguess));
374: PetscFunctionReturn(PETSC_SUCCESS);
375: }
377: static PetscErrorCode KSPPIPEFGMRESUpdateHessenberg(KSP ksp, PetscInt it, PetscBool *hapend, PetscReal *res)
378: {
379: PetscScalar *hh, *cc, *ss, *rs;
380: PetscReal hapbnd;
381: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
383: PetscFunctionBegin;
384: hh = HH(0, it); /* pointer to beginning of column to update */
385: cc = CC(0); /* beginning of cosine rotations */
386: ss = SS(0); /* beginning of sine rotations */
387: rs = RS(0); /* right-hand side of least squares system */
389: /* The Hessenberg matrix is now correct through column it, save that form for possible spectral analysis */
390: for (PetscInt j = 0; j <= it + 1; j++) *HES(j, it) = hh[j];
392: /* check for the happy breakdown */
393: hapbnd = PetscMin(PetscAbsScalar(hh[it + 1] / rs[it]), pipefgmres->haptol);
394: if (PetscAbsScalar(hh[it + 1]) < hapbnd) {
395: PetscCall(PetscInfo(ksp, "Detected happy breakdown, current hapbnd = %14.12e H(%" PetscInt_FMT ",%" PetscInt_FMT ") = %14.12e\n", (double)hapbnd, it + 1, it, (double)PetscAbsScalar(*HH(it + 1, it))));
396: *hapend = PETSC_TRUE;
397: }
399: /* Apply all the previously computed plane rotations to the new column of the Hessenberg matrix */
400: /* Note: this uses the rotation [conj(c) s ; -s c], c= cos(theta), s= sin(theta),
401: and some refs have [c s ; -conj(s) c] (don't be confused!) */
403: for (PetscInt j = 0; j < it; j++) {
404: PetscScalar hhj = hh[j];
405: hh[j] = PetscConj(cc[j]) * hhj + ss[j] * hh[j + 1];
406: hh[j + 1] = -ss[j] * hhj + cc[j] * hh[j + 1];
407: }
409: /*
410: compute the new plane rotation, and apply it to:
411: 1) the right-hand side of the Hessenberg system (RS)
412: note: it affects RS(it) and RS(it+1)
413: 2) the new column of the Hessenberg matrix
414: note: it affects HH(it,it) which is currently pointed to
415: by hh and HH(it+1, it) (*(hh+1))
416: thus obtaining the updated value of the residual...
417: */
419: /* compute new plane rotation */
421: if (!*hapend) {
422: PetscReal delta = PetscSqrtReal(PetscSqr(PetscAbsScalar(hh[it])) + PetscSqr(PetscAbsScalar(hh[it + 1])));
423: if (delta == 0.0) {
424: ksp->reason = KSP_DIVERGED_NULL;
425: PetscFunctionReturn(PETSC_SUCCESS);
426: }
428: cc[it] = hh[it] / delta; /* new cosine value */
429: ss[it] = hh[it + 1] / delta; /* new sine value */
431: hh[it] = PetscConj(cc[it]) * hh[it] + ss[it] * hh[it + 1];
432: rs[it + 1] = -ss[it] * rs[it];
433: rs[it] = PetscConj(cc[it]) * rs[it];
434: *res = PetscAbsScalar(rs[it + 1]);
435: } else { /* happy breakdown: HH(it+1, it) = 0, therefore we don't need to apply
436: another rotation matrix (so RH doesn't change). The new residual is
437: always the new sine term times the residual from last time (RS(it)),
438: but now the new sine rotation would be zero...so the residual should
439: be zero...so we will multiply "zero" by the last residual. This might
440: not be exactly what we want to do here -could just return "zero". */
441: *res = 0.0;
442: }
443: PetscFunctionReturn(PETSC_SUCCESS);
444: }
446: static PetscErrorCode KSPBuildSolution_PIPEFGMRES(KSP ksp, Vec ptr, Vec *result)
447: {
448: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
450: PetscFunctionBegin;
451: if (!ptr) {
452: if (!pipefgmres->sol_temp) PetscCall(VecDuplicate(ksp->vec_sol, &pipefgmres->sol_temp));
453: ptr = pipefgmres->sol_temp;
454: }
455: if (!pipefgmres->nrs) {
456: /* allocate the work area */
457: PetscCall(PetscMalloc1(pipefgmres->max_k, &pipefgmres->nrs));
458: }
460: PetscCall(KSPPIPEFGMRESBuildSoln(pipefgmres->nrs, ksp->vec_sol, ptr, ksp, pipefgmres->it));
461: if (result) *result = ptr;
462: PetscFunctionReturn(PETSC_SUCCESS);
463: }
465: static PetscErrorCode KSPSetFromOptions_PIPEFGMRES(KSP ksp, PetscOptionItems PetscOptionsObject)
466: {
467: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
468: PetscBool flg;
469: PetscScalar shift;
471: PetscFunctionBegin;
472: PetscCall(KSPSetFromOptions_GMRES(ksp, PetscOptionsObject));
473: PetscOptionsHeadBegin(PetscOptionsObject, "KSP pipelined FGMRES Options");
474: PetscCall(PetscOptionsScalar("-ksp_pipefgmres_shift", "shift parameter", "KSPPIPEFGMRESSetShift", pipefgmres->shift, &shift, &flg));
475: if (flg) PetscCall(KSPPIPEFGMRESSetShift(ksp, shift));
476: PetscOptionsHeadEnd();
477: PetscFunctionReturn(PETSC_SUCCESS);
478: }
480: static PetscErrorCode KSPView_PIPEFGMRES(KSP ksp, PetscViewer viewer)
481: {
482: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
483: PetscBool isascii, isstring;
485: PetscFunctionBegin;
486: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
487: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
489: if (isascii) {
490: PetscCall(PetscViewerASCIIPrintf(viewer, " restart=%" PetscInt_FMT "\n", pipefgmres->max_k));
491: PetscCall(PetscViewerASCIIPrintf(viewer, " happy breakdown tolerance=%g\n", (double)pipefgmres->haptol));
492: if (PetscDefined(USE_COMPLEX)) PetscCall(PetscViewerASCIIPrintf(viewer, " shift=%g+%gi\n", (double)PetscRealPart(pipefgmres->shift), (double)PetscImaginaryPart(pipefgmres->shift)));
493: else PetscCall(PetscViewerASCIIPrintf(viewer, " shift=%g\n", (double)PetscRealPart(pipefgmres->shift)));
494: } else if (isstring) {
495: PetscCall(PetscViewerStringSPrintf(viewer, "restart %" PetscInt_FMT, pipefgmres->max_k));
496: if (PetscDefined(USE_COMPLEX)) PetscCall(PetscViewerStringSPrintf(viewer, " shift=%g+%gi\n", (double)PetscRealPart(pipefgmres->shift), (double)PetscImaginaryPart(pipefgmres->shift)));
497: else PetscCall(PetscViewerStringSPrintf(viewer, " shift=%g\n", (double)PetscRealPart(pipefgmres->shift)));
498: }
499: PetscFunctionReturn(PETSC_SUCCESS);
500: }
502: PetscErrorCode KSPReset_PIPEFGMRES(KSP ksp)
503: {
504: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
506: PetscFunctionBegin;
507: PetscCall(PetscFree(pipefgmres->prevecs));
508: PetscCall(PetscFree(pipefgmres->zvecs));
509: for (PetscInt i = 0; i < pipefgmres->nwork_alloc; i++) {
510: PetscCall(VecDestroyVecs(pipefgmres->mwork_alloc[i], &pipefgmres->prevecs_user_work[i]));
511: PetscCall(VecDestroyVecs(pipefgmres->mwork_alloc[i], &pipefgmres->zvecs_user_work[i]));
512: }
513: PetscCall(PetscFree(pipefgmres->prevecs_user_work));
514: PetscCall(PetscFree(pipefgmres->zvecs_user_work));
515: PetscCall(PetscFree(pipefgmres->redux));
516: PetscCall(KSPReset_GMRES(ksp));
517: PetscFunctionReturn(PETSC_SUCCESS);
518: }
520: /*MC
521: KSPPIPEFGMRES - Implements the Pipelined (1-stage) Flexible Generalized Minimal Residual method {cite}`sananschneppmay2016`. [](sec_pipelineksp). [](sec_flexibleksp)
523: Options Database Keys:
524: + -ksp_gmres_restart restart - the number of Krylov directions to orthogonalize against
525: . -ksp_gmres_haptol tol - sets the tolerance for "happy breakdown" (exact convergence)
526: . -ksp_gmres_preallocate - preallocate all the Krylov search directions initially (otherwise groups of vectors are allocated as needed)
527: . -ksp_pipefgmres_shift - the shift to use (defaults to 1. See `KSPPIPEFGMRESSetShift()`
528: - -ksp_gmres_krylov_monitor - plot the Krylov space generated
530: Level: intermediate
532: Notes:
533: Compare to `KSPPGMRES` and `KSPFGMRES`
535: This variant is not "explicitly normalized" like `KSPPGMRES`, and requires a shift parameter.
537: A heuristic for choosing the shift parameter is the largest eigenvalue of the preconditioned operator.
539: Only right preconditioning is supported (but this preconditioner may be nonlinear/variable/inexact, as with `KSPFGMRES`).
541: MPI configuration may be necessary for reductions to make asynchronous progress, which is important for performance of pipelined methods.
542: See [](doc_faq_pipelined)
544: Developer Note:
545: This class is subclassed off of `KSPGMRES`, see the source code in src/ksp/ksp/impls/gmres for comments on the structure of the code
547: Contributed by:
548: P. Sanan and S.M. Schnepp
550: .seealso: [](ch_ksp), [](doc_faq_pipelined), [](sec_pipelineksp), [](sec_flexibleksp), `KSPCreate()`, `KSPSetType()`, `KSPType`, `KSP`, `KSPLGMRES`, `KSPPIPECG`, `KSPPIPECR`, `KSPPGMRES`, `KSPFGMRES`,
551: `KSPGMRESSetRestart()`, `KSPGMRESSetHapTol()`, `KSPGMRESSetPreAllocateVectors()`, `KSPGMRESMonitorKrylov()`, `KSPPIPEFGMRESSetShift()`
552: M*/
554: PETSC_EXTERN PetscErrorCode KSPCreate_PIPEFGMRES(KSP ksp)
555: {
556: KSP_PIPEFGMRES *pipefgmres;
558: PetscFunctionBegin;
559: PetscCall(PetscNew(&pipefgmres));
561: ksp->data = (void *)pipefgmres;
562: ksp->ops->buildsolution = KSPBuildSolution_PIPEFGMRES;
563: ksp->ops->setup = KSPSetUp_PIPEFGMRES;
564: ksp->ops->solve = KSPSolve_PIPEFGMRES;
565: ksp->ops->reset = KSPReset_PIPEFGMRES;
566: ksp->ops->destroy = KSPDestroy_PIPEFGMRES;
567: ksp->ops->view = KSPView_PIPEFGMRES;
568: ksp->ops->setfromoptions = KSPSetFromOptions_PIPEFGMRES;
569: ksp->ops->computeextremesingularvalues = KSPComputeExtremeSingularValues_GMRES;
570: ksp->ops->computeeigenvalues = KSPComputeEigenvalues_GMRES;
572: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_RIGHT, 3));
573: PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_NONE, PC_RIGHT, 1));
575: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetPreAllocateVectors_C", KSPGMRESSetPreAllocateVectors_GMRES));
576: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetRestart_C", KSPGMRESSetRestart_GMRES));
577: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESGetRestart_C", KSPGMRESGetRestart_GMRES));
578: PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPGMRESSetHapTol_C", KSPGMRESSetHapTol_GMRES));
580: pipefgmres->nextra_vecs = 1;
581: pipefgmres->haptol = 1.0e-30;
582: pipefgmres->q_preallocate = PETSC_FALSE;
583: pipefgmres->delta_allocate = PIPEFGMRES_DELTA_DIRECTIONS;
584: pipefgmres->nrs = NULL;
585: pipefgmres->sol_temp = NULL;
586: pipefgmres->max_k = PIPEFGMRES_DEFAULT_MAXK;
587: pipefgmres->Rsvd = NULL;
588: pipefgmres->shift = 1.0;
589: PetscFunctionReturn(PETSC_SUCCESS);
590: }
592: static PetscErrorCode KSPPIPEFGMRESGetNewVectors(KSP ksp, PetscInt it)
593: {
594: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
595: PetscInt nwork = pipefgmres->nwork_alloc; /* number of work vector chunks allocated */
596: PetscInt nalloc; /* number to allocate */
598: PetscFunctionBegin;
599: nalloc = pipefgmres->delta_allocate; /* number of vectors to allocate
600: in a single chunk */
602: /* Adjust the number to allocate to make sure that we don't exceed the
603: number of available slots (pipefgmres->vecs_allocated)*/
604: if (it + VEC_OFFSET + nalloc >= pipefgmres->vecs_allocated) nalloc = pipefgmres->vecs_allocated - it - VEC_OFFSET;
605: if (!nalloc) PetscFunctionReturn(PETSC_SUCCESS);
607: pipefgmres->vv_allocated += nalloc; /* vv_allocated is the number of vectors allocated */
609: /* work vectors */
610: PetscCall(KSPCreateVecs(ksp, nalloc, &pipefgmres->user_work[nwork], 0, NULL));
611: for (PetscInt k = 0; k < nalloc; k++) pipefgmres->vecs[it + VEC_OFFSET + k] = pipefgmres->user_work[nwork][k];
612: /* specify size of chunk allocated */
613: pipefgmres->mwork_alloc[nwork] = nalloc;
615: /* preconditioned vectors (note we don't use VEC_OFFSET) */
616: PetscCall(KSPCreateVecs(ksp, nalloc, &pipefgmres->prevecs_user_work[nwork], 0, NULL));
617: for (PetscInt k = 0; k < nalloc; k++) pipefgmres->prevecs[it + k] = pipefgmres->prevecs_user_work[nwork][k];
619: PetscCall(KSPCreateVecs(ksp, nalloc, &pipefgmres->zvecs_user_work[nwork], 0, NULL));
620: for (PetscInt k = 0; k < nalloc; k++) pipefgmres->zvecs[it + k] = pipefgmres->zvecs_user_work[nwork][k];
622: /* increment the number of work vector chunks */
623: pipefgmres->nwork_alloc++;
624: PetscFunctionReturn(PETSC_SUCCESS);
625: }
627: /*@
628: KSPPIPEFGMRESSetShift - Set the shift parameter for the flexible, pipelined `KSPPIPEFGMRES` solver.
630: Logically Collective
632: Input Parameters:
633: + ksp - the Krylov space context
634: - shift - the shift
636: Options Database Key:
637: . -ksp_pipefgmres_shift shift - set the shift parameter
639: Level: intermediate
641: Note:
642: A heuristic is to set this to be comparable to the largest eigenvalue of the preconditioned operator.
643: This can be achieved with PETSc itself by using a few iterations of a Krylov method.
644: See `KSPComputeEigenvalues()` (and note the caveats there).
646: .seealso: [](ch_ksp), `KSPPIPEFGMRES`, `KSPComputeEigenvalues()`
647: @*/
648: PetscErrorCode KSPPIPEFGMRESSetShift(KSP ksp, PetscScalar shift)
649: {
650: KSP_PIPEFGMRES *pipefgmres = (KSP_PIPEFGMRES *)ksp->data;
652: PetscFunctionBegin;
655: pipefgmres->shift = shift;
656: PetscFunctionReturn(PETSC_SUCCESS);
657: }