Actual source code: pod.c

  1: #include <petsc/private/kspimpl.h>
  2: #include <petsc/private/matimpl.h>
  3: #include <petscblaslapack.h>
  4: static PetscBool  cited      = PETSC_FALSE;
  5: static const char citation[] = "@phdthesis{zampini2010non,\n"
  6:                                "  title={Non-overlapping Domain Decomposition Methods for Cardiac Reaction-Diffusion Models and Applications},\n"
  7:                                "  author={Zampini, S},\n"
  8:                                "  year={2010},\n"
  9:                                "  school={PhD thesis, Universita degli Studi di Milano}\n"
 10:                                "}\n";

 12: typedef struct {
 13:   PetscInt      maxn;  /* maximum number of snapshots */
 14:   PetscInt      n;     /* number of active snapshots */
 15:   PetscInt      curr;  /* current tip of snapshots set */
 16:   Vec          *xsnap; /* snapshots */
 17:   Vec          *bsnap; /* rhs snapshots */
 18:   Vec          *work;  /* parallel work vectors */
 19:   PetscScalar  *dots_iallreduce;
 20:   MPI_Request   req_iallreduce;
 21:   PetscInt      ndots_iallreduce; /* if we have iallreduce we can hide the VecMDot communications */
 22:   PetscReal     tol;              /* relative tolerance to retain eigenvalues */
 23:   PetscBool     Aspd;             /* if true, uses the SPD operator as inner product */
 24:   PetscScalar  *corr;             /* correlation matrix */
 25:   PetscReal    *eigs;             /* eigenvalues */
 26:   PetscScalar  *eigv;             /* eigenvectors */
 27:   PetscBLASInt  nen;              /* dimension of lower dimensional system */
 28:   PetscInt      st;               /* first eigenvector of correlation matrix to be retained */
 29:   PetscBLASInt *iwork;            /* integer work vector */
 30:   PetscScalar  *yhay;             /* Y^H * A * Y */
 31:   PetscScalar  *low;              /* lower dimensional linear system */
 32: #if PetscDefined(USE_COMPLEX)
 33:   PetscReal *rwork;
 34: #endif
 35:   PetscBLASInt lwork;
 36:   PetscScalar *swork;
 37:   PetscBool    monitor;
 38: } KSPGuessPOD;

 40: static PetscErrorCode KSPGuessReset_POD(KSPGuess guess)
 41: {
 42:   KSPGuessPOD *pod  = (KSPGuessPOD *)guess->data;
 43:   PetscLayout  Alay = NULL, vlay = NULL;
 44:   PetscBool    cong;

 46:   PetscFunctionBegin;
 47:   pod->nen  = 0;
 48:   pod->n    = 0;
 49:   pod->curr = 0;
 50:   /* need to wait for completion of outstanding requests */
 51:   if (pod->ndots_iallreduce) PetscCallMPI(MPI_Wait(&pod->req_iallreduce, MPI_STATUS_IGNORE));
 52:   pod->ndots_iallreduce = 0;
 53:   /* destroy vectors if the size of the linear system has changed */
 54:   if (guess->A) PetscCall(MatGetLayouts(guess->A, &Alay, NULL));
 55:   if (pod->xsnap) PetscCall(VecGetLayout(pod->xsnap[0], &vlay));
 56:   cong = PETSC_FALSE;
 57:   if (vlay && Alay) PetscCall(PetscLayoutCompare(Alay, vlay, &cong));
 58:   if (!cong) {
 59:     PetscCall(VecDestroyVecs(pod->maxn, &pod->xsnap));
 60:     PetscCall(VecDestroyVecs(pod->maxn, &pod->bsnap));
 61:     PetscCall(VecDestroyVecs(1, &pod->work));
 62:   }
 63:   PetscFunctionReturn(PETSC_SUCCESS);
 64: }

 66: static PetscErrorCode KSPGuessSetUp_POD(KSPGuess guess)
 67: {
 68:   KSPGuessPOD *pod = (KSPGuessPOD *)guess->data;

 70:   PetscFunctionBegin;
 71:   if (!pod->corr) {
 72:     PetscScalar  sdummy;
 73:     PetscReal    rdummy = 0;
 74:     PetscBLASInt bN, idummy = 0;

 76:     PetscCall(PetscCalloc6(pod->maxn * pod->maxn, &pod->corr, pod->maxn, &pod->eigs, pod->maxn * pod->maxn, &pod->eigv, 6 * pod->maxn, &pod->iwork, pod->maxn * pod->maxn, &pod->yhay, pod->maxn * pod->maxn, &pod->low));
 77: #if PetscDefined(USE_COMPLEX)
 78:     PetscCall(PetscMalloc1(7 * pod->maxn, &pod->rwork));
 79: #endif
 80: #if PetscDefined(HAVE_MPI_NONBLOCKING_COLLECTIVES)
 81:     PetscCall(PetscMalloc1(3 * pod->maxn, &pod->dots_iallreduce));
 82: #endif
 83:     pod->lwork = -1;
 84:     PetscCall(PetscBLASIntCast(pod->maxn, &bN));
 85: #if !PetscDefined(USE_COMPLEX)
 86:     PetscCallLAPACKInfo("LAPACKsyevx", LAPACKsyevx_("V", "A", "L", &bN, pod->corr, &bN, &rdummy, &rdummy, &idummy, &idummy, &rdummy, &idummy, pod->eigs, pod->eigv, &bN, &sdummy, &pod->lwork, pod->iwork, pod->iwork + 5 * bN, &info));
 87: #else
 88:     PetscCallLAPACKInfo("LAPACKsyevx", LAPACKsyevx_("V", "A", "L", &bN, pod->corr, &bN, &rdummy, &rdummy, &idummy, &idummy, &rdummy, &idummy, pod->eigs, pod->eigv, &bN, &sdummy, &pod->lwork, pod->rwork, pod->iwork, pod->iwork + 5 * bN, &info));
 89: #endif
 90:     PetscCall(PetscBLASIntCast((PetscInt)PetscRealPart(sdummy), &pod->lwork));
 91:     PetscCall(PetscMalloc1(pod->lwork + PetscMax(bN * bN, 6 * bN), &pod->swork));
 92:   }
 93:   /* work vectors are sequential, we explicitly use MPI_Allreduce */
 94:   if (!pod->xsnap) {
 95:     Vec *v, vseq;

 97:     PetscCall(KSPCreateVecs(guess->ksp, 1, &v, 0, NULL));
 98:     PetscCall(VecCreateLocalVector(v[0], &vseq));
 99:     PetscCall(VecDestroyVecs(1, &v));
100:     PetscCall(VecDuplicateVecs(vseq, pod->maxn, &pod->xsnap));
101:     PetscCall(VecDestroy(&vseq));
102:   }
103:   if (!pod->bsnap) {
104:     Vec *v, vseq;

106:     PetscCall(KSPCreateVecs(guess->ksp, 0, NULL, 1, &v));
107:     PetscCall(VecCreateLocalVector(v[0], &vseq));
108:     PetscCall(VecDestroyVecs(1, &v));
109:     PetscCall(VecDuplicateVecs(vseq, pod->maxn, &pod->bsnap));
110:     PetscCall(VecDestroy(&vseq));
111:   }
112:   if (!pod->work) PetscCall(KSPCreateVecs(guess->ksp, 1, &pod->work, 0, NULL));
113:   PetscFunctionReturn(PETSC_SUCCESS);
114: }

116: static PetscErrorCode KSPGuessDestroy_POD(KSPGuess guess)
117: {
118:   KSPGuessPOD *pod = (KSPGuessPOD *)guess->data;

120:   PetscFunctionBegin;
121:   PetscCall(PetscFree6(pod->corr, pod->eigs, pod->eigv, pod->iwork, pod->yhay, pod->low));
122: #if PetscDefined(USE_COMPLEX)
123:   PetscCall(PetscFree(pod->rwork));
124: #endif
125:   /* need to wait for completion before destroying dots_iallreduce */
126:   if (pod->ndots_iallreduce) PetscCallMPI(MPI_Wait(&pod->req_iallreduce, MPI_STATUS_IGNORE));
127:   PetscCall(PetscFree(pod->dots_iallreduce));
128:   PetscCall(PetscFree(pod->swork));
129:   PetscCall(VecDestroyVecs(pod->maxn, &pod->bsnap));
130:   PetscCall(VecDestroyVecs(pod->maxn, &pod->xsnap));
131:   PetscCall(VecDestroyVecs(1, &pod->work));
132:   PetscCall(PetscFree(pod));
133:   PetscFunctionReturn(PETSC_SUCCESS);
134: }

136: static PetscErrorCode KSPGuessUpdate_POD(KSPGuess, Vec, Vec);

138: static PetscErrorCode KSPGuessFormGuess_POD(KSPGuess guess, Vec b, Vec x)
139: {
140:   KSPGuessPOD *pod = (KSPGuessPOD *)guess->data;
141:   PetscScalar  one = 1, zero = 0;
142:   PetscBLASInt bN, ione      = 1, bNen;
143:   PetscInt     i;

145:   PetscFunctionBegin;
146:   PetscCall(PetscCitationsRegister(citation, &cited));
147:   if (pod->ndots_iallreduce) { /* complete communication and project the linear system */
148:     PetscCall(KSPGuessUpdate_POD(guess, NULL, NULL));
149:   }
150:   if (!pod->nen) PetscFunctionReturn(PETSC_SUCCESS);
151:   /* b_low = S * V^T * X^T * b */
152:   PetscCall(VecGetLocalVectorRead(b, pod->bsnap[pod->curr]));
153:   PetscCall(VecMDot(pod->bsnap[pod->curr], pod->n, pod->xsnap, pod->swork));
154:   PetscCall(VecRestoreLocalVectorRead(b, pod->bsnap[pod->curr]));
155:   PetscCallMPI(MPIU_Allreduce(pod->swork, pod->swork + pod->n, pod->n, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)guess)));
156:   PetscCall(PetscBLASIntCast(pod->n, &bN));
157:   PetscCall(PetscBLASIntCast(pod->nen, &bNen));
158:   PetscCallBLAS("BLASgemv", BLASgemv_("T", &bN, &bNen, &one, pod->eigv + pod->st * pod->n, &bN, pod->swork + pod->n, &ione, &zero, pod->swork, &ione));
159:   if (pod->monitor) {
160:     PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "  KSPGuessPOD alphas = "));
161:     for (i = 0; i < pod->nen; i++) {
162:       if (PetscDefined(USE_COMPLEX)) PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "%g + %g i", (double)PetscRealPart(pod->swork[i]), (double)PetscImaginaryPart(pod->swork[i])));
163:       else PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "%g ", (double)PetscRealPart(pod->swork[i])));
164:     }
165:     PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "\n"));
166:   }
167:   /* A_low x_low = b_low */
168:   if (!pod->Aspd) { /* A is spd -> LOW = Identity */
169:     KSP       pksp = guess->ksp;
170:     PetscBool tsolve, symm, set;

172:     if (pod->monitor) {
173:       PetscMPIInt rank;
174:       Mat         L;

176:       PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)guess), &rank));
177:       if (rank == 0) {
178:         PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "  L = "));
179:         PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, pod->nen, pod->nen, pod->low, &L));
180:         PetscCall(MatView(L, NULL));
181:         PetscCall(MatDestroy(&L));
182:       }
183:     }
184:     PetscCall(MatIsSymmetricKnown(guess->A, &set, &symm));
185:     tsolve = (set && symm) ? PETSC_FALSE : pksp->transpose_solve;
186:     PetscCallLAPACKInfo("LAPACKgetrf", LAPACKgetrf_(&bNen, &bNen, pod->low, &bNen, pod->iwork, &info));
187:     PetscCallLAPACKInfo("LAPACKgetrs", LAPACKgetrs_(tsolve ? "T" : "N", &bNen, &ione, pod->low, &bNen, pod->iwork, pod->swork, &bNen, &info));
188:   }
189:   /* x = X * V * S * x_low */
190:   PetscCallBLAS("BLASgemv", BLASgemv_("N", &bN, &bNen, &one, pod->eigv + pod->st * pod->n, &bN, pod->swork, &ione, &zero, pod->swork + pod->n, &ione));
191:   if (pod->monitor) {
192:     PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "  KSPGuessPOD sol = "));
193:     for (i = 0; i < pod->nen; i++) {
194:       if (PetscDefined(USE_COMPLEX)) PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "%g + %g i", (double)PetscRealPart(pod->swork[i + pod->n]), (double)PetscImaginaryPart(pod->swork[i + pod->n])));
195:       else PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "%g ", (double)PetscRealPart(pod->swork[i + pod->n])));
196:     }
197:     PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "\n"));
198:   }
199:   PetscCall(VecGetLocalVector(x, pod->bsnap[pod->curr]));
200:   PetscCall(VecSet(pod->bsnap[pod->curr], 0));
201:   PetscCall(VecMAXPY(pod->bsnap[pod->curr], pod->n, pod->swork + pod->n, pod->xsnap));
202:   PetscCall(VecRestoreLocalVector(x, pod->bsnap[pod->curr]));
203:   PetscFunctionReturn(PETSC_SUCCESS);
204: }

206: static PetscErrorCode KSPGuessUpdate_POD(KSPGuess guess, Vec b, Vec x)
207: {
208:   KSPGuessPOD *pod = (KSPGuessPOD *)guess->data;
209:   PetscScalar  one = 1, zero = 0;
210:   PetscReal    toten, parten, reps = 0; /* dlamch? */
211:   PetscBLASInt bN, idummy = 0;
212:   PetscInt     i;
213:   PetscMPIInt  podn;

215:   PetscFunctionBegin;
216:   if (pod->ndots_iallreduce) goto complete_request;
217:   pod->n = pod->n < pod->maxn ? pod->n + 1 : pod->maxn;
218:   PetscCall(PetscMPIIntCast(pod->n, &podn));
219:   PetscCall(VecCopy(x, pod->xsnap[pod->curr]));
220:   PetscCall(KSP_MatMult(guess->ksp, guess->A, x, pod->work[0]));
221:   PetscCall(VecCopy(pod->work[0], pod->bsnap[pod->curr]));
222:   if (pod->Aspd) {
223:     PetscCall(VecMDot(pod->xsnap[pod->curr], pod->n, pod->bsnap, pod->swork));
224: #if !PetscDefined(HAVE_MPI_NONBLOCKING_COLLECTIVES)
225:     PetscCallMPI(MPIU_Allreduce(pod->swork, pod->swork + 3 * pod->n, podn, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)guess)));
226: #else
227:     PetscCallMPI(MPI_Iallreduce(pod->swork, pod->dots_iallreduce, podn, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)guess), &pod->req_iallreduce));
228:     pod->ndots_iallreduce = 1;
229: #endif
230:   } else {
231:     PetscInt  off;
232:     PetscBool set, herm;

234:     if (PetscDefined(USE_COMPLEX)) PetscCall(MatIsHermitianKnown(guess->A, &set, &herm));
235:     else PetscCall(MatIsSymmetricKnown(guess->A, &set, &herm));
236:     off = (guess->ksp->transpose_solve && (!set || !herm)) ? 2 * pod->n : pod->n;

238:     /* TODO: we may want to use a user-defined dot for the correlation matrix */
239:     PetscCall(VecMDot(pod->xsnap[pod->curr], pod->n, pod->xsnap, pod->swork));
240:     PetscCall(VecMDot(pod->bsnap[pod->curr], pod->n, pod->xsnap, pod->swork + off));
241:     if (!set || !herm) {
242:       off = (off == pod->n) ? 2 * pod->n : pod->n;
243:       PetscCall(VecMDot(pod->xsnap[pod->curr], pod->n, pod->bsnap, pod->swork + off));
244: #if !PetscDefined(HAVE_MPI_NONBLOCKING_COLLECTIVES)
245:       PetscCallMPI(MPIU_Allreduce(pod->swork, pod->swork + 3 * pod->n, 3 * podn, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)guess)));
246: #else
247:       PetscCallMPI(MPI_Iallreduce(pod->swork, pod->dots_iallreduce, 3 * podn, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)guess), &pod->req_iallreduce));
248:       pod->ndots_iallreduce = 3;
249: #endif
250:     } else {
251: #if !PetscDefined(HAVE_MPI_NONBLOCKING_COLLECTIVES)
252:       PetscCallMPI(MPIU_Allreduce(pod->swork, pod->swork + 3 * pod->n, 2 * podn, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)guess)));
253:       for (i = 0; i < pod->n; i++) pod->swork[5 * pod->n + i] = pod->swork[4 * pod->n + i];
254: #else
255:       PetscCallMPI(MPI_Iallreduce(pod->swork, pod->dots_iallreduce, 2 * podn, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)guess), &pod->req_iallreduce));
256:       pod->ndots_iallreduce = 2;
257: #endif
258:     }
259:   }
260:   if (pod->ndots_iallreduce) PetscFunctionReturn(PETSC_SUCCESS);

262: complete_request:
263:   if (pod->ndots_iallreduce) {
264:     PetscCallMPI(MPI_Wait(&pod->req_iallreduce, MPI_STATUS_IGNORE));
265:     switch (pod->ndots_iallreduce) {
266:     case 3:
267:       for (i = 0; i < pod->n; i++) pod->swork[3 * pod->n + i] = pod->dots_iallreduce[i];
268:       for (i = 0; i < pod->n; i++) pod->swork[4 * pod->n + i] = pod->dots_iallreduce[pod->n + i];
269:       for (i = 0; i < pod->n; i++) pod->swork[5 * pod->n + i] = pod->dots_iallreduce[2 * pod->n + i];
270:       break;
271:     case 2:
272:       for (i = 0; i < pod->n; i++) pod->swork[3 * pod->n + i] = pod->dots_iallreduce[i];
273:       for (i = 0; i < pod->n; i++) pod->swork[4 * pod->n + i] = pod->dots_iallreduce[pod->n + i];
274:       for (i = 0; i < pod->n; i++) pod->swork[5 * pod->n + i] = pod->dots_iallreduce[pod->n + i];
275:       break;
276:     case 1:
277:       for (i = 0; i < pod->n; i++) pod->swork[3 * pod->n + i] = pod->dots_iallreduce[i];
278:       break;
279:     default:
280:       SETERRQ(PetscObjectComm((PetscObject)guess), PETSC_ERR_PLIB, "Invalid number of outstanding dots operations: %" PetscInt_FMT, pod->ndots_iallreduce);
281:     }
282:   }
283:   pod->ndots_iallreduce = 0;

285:   /* correlation matrix and Y^H A Y (Galerkin) */
286:   for (i = 0; i < pod->n; i++) {
287:     pod->corr[pod->curr * pod->maxn + i] = pod->swork[3 * pod->n + i];
288:     pod->corr[i * pod->maxn + pod->curr] = PetscConj(pod->swork[3 * pod->n + i]);
289:     if (!pod->Aspd) {
290:       pod->yhay[pod->curr * pod->maxn + i] = pod->swork[4 * pod->n + i];
291:       pod->yhay[i * pod->maxn + pod->curr] = PetscConj(pod->swork[5 * pod->n + i]);
292:     }
293:   }
294:   /* syevx changes the input matrix */
295:   for (i = 0; i < pod->n; i++) {
296:     for (PetscInt j = i; j < pod->n; j++) pod->swork[i * pod->n + j] = pod->corr[i * pod->maxn + j];
297:   }
298:   PetscCall(PetscBLASIntCast(pod->n, &bN));
299: #if !PetscDefined(USE_COMPLEX)
300:   PetscCallLAPACKInfo("LAPACKsyevx", LAPACKsyevx_("V", "A", "L", &bN, pod->swork, &bN, &reps, &reps, &idummy, &idummy, &reps, &idummy, pod->eigs, pod->eigv, &bN, pod->swork + bN * bN, &pod->lwork, pod->iwork, pod->iwork + 5 * bN, &info));
301: #else
302:   PetscCallLAPACKInfo("LAPACKsyevx", LAPACKsyevx_("V", "A", "L", &bN, pod->swork, &bN, &reps, &reps, &idummy, &idummy, &reps, &idummy, pod->eigs, pod->eigv, &bN, pod->swork + bN * bN, &pod->lwork, pod->rwork, pod->iwork, pod->iwork + 5 * bN, &info));
303: #endif

305:   /* dimension of lower dimensional system */
306:   pod->st = -1;
307:   for (i = 0, toten = 0; i < pod->n; i++) {
308:     pod->eigs[i] = PetscMax(pod->eigs[i], 0.0);
309:     toten += pod->eigs[i];
310:     if (!pod->eigs[i]) pod->st = i;
311:   }
312:   pod->nen = 0;
313:   for (i = pod->n - 1, parten = 0; i > pod->st && toten > 0; i--) {
314:     pod->nen++;
315:     parten += pod->eigs[i];
316:     if (parten + toten * pod->tol >= toten) break;
317:   }
318:   pod->st = pod->n - pod->nen;

320:   /* Compute eigv = V * S */
321:   for (i = pod->st; i < pod->n; i++) {
322:     const PetscReal v  = 1.0 / PetscSqrtReal(pod->eigs[i]);
323:     const PetscInt  st = pod->n * i;

325:     for (PetscInt j = 0; j < pod->n; j++) pod->eigv[st + j] *= v;
326:   }

328:   /* compute S * V^T * X^T * A * X * V * S if needed */
329:   if (pod->nen && !pod->Aspd) {
330:     PetscBLASInt bNen, bMaxN;
331:     PetscInt     st = pod->st * pod->n;
332:     PetscCall(PetscBLASIntCast(pod->nen, &bNen));
333:     PetscCall(PetscBLASIntCast(pod->maxn, &bMaxN));
334:     PetscCallBLAS("BLASgemm", BLASgemm_("T", "N", &bNen, &bN, &bN, &one, pod->eigv + st, &bN, pod->yhay, &bMaxN, &zero, pod->swork, &bNen));
335:     PetscCallBLAS("BLASgemm", BLASgemm_("N", "N", &bNen, &bNen, &bN, &one, pod->swork, &bNen, pod->eigv + st, &bN, &zero, pod->low, &bNen));
336:   }

338:   if (pod->monitor) {
339:     PetscMPIInt rank;
340:     Mat         C;

342:     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)guess), &rank));
343:     if (rank == 0) {
344:       PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "  C = "));
345:       PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, pod->n, pod->n, pod->corr, &C));
346:       PetscCall(MatDenseSetLDA(C, pod->maxn));
347:       PetscCall(MatView(C, NULL));
348:       PetscCall(MatDestroy(&C));
349:       PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "  YHAY = "));
350:       PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, pod->n, pod->n, pod->yhay, &C));
351:       PetscCall(MatDenseSetLDA(C, pod->maxn));
352:       PetscCall(MatView(C, NULL));
353:       PetscCall(MatDestroy(&C));
354:     }
355:     PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "  KSPGuessPOD: basis %" PetscBLASInt_FMT ", energy fractions = ", pod->nen));
356:     for (i = pod->n - 1; i >= 0; i--) PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "%1.6e (%d) ", (double)(pod->eigs[i] / toten), i >= pod->st ? 1 : 0));
357:     PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "\n"));
358:     if (PetscDefined(USE_DEBUG)) {
359:       for (i = 0; i < pod->n; i++) {
360:         Vec          v;
361:         PetscBLASInt bNen, ione = 1;

363:         PetscCall(VecDuplicate(pod->xsnap[i], &v));
364:         PetscCall(VecCopy(pod->xsnap[i], v));
365:         PetscCall(PetscBLASIntCast(pod->nen, &bNen));
366:         PetscCallBLAS("BLASgemv", BLASgemv_("T", &bN, &bNen, &one, pod->eigv + pod->st * pod->n, &bN, pod->corr + pod->maxn * i, &ione, &zero, pod->swork, &ione));
367:         PetscCallBLAS("BLASgemv", BLASgemv_("N", &bN, &bNen, &one, pod->eigv + pod->st * pod->n, &bN, pod->swork, &ione, &zero, pod->swork + pod->n, &ione));
368:         for (PetscInt j = 0; j < pod->n; j++) pod->swork[j] = -pod->swork[pod->n + j];
369:         PetscCall(VecMAXPY(v, pod->n, pod->swork, pod->xsnap));
370:         PetscCall(VecDot(v, v, pod->swork));
371:         PetscCallMPI(MPIU_Allreduce(pod->swork, pod->swork + 1, 1, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)guess)));
372:         PetscCall(PetscPrintf(PetscObjectComm((PetscObject)guess), "  Error projection %" PetscInt_FMT ": %g (expected lower than %g)\n", i, (double)PetscRealPart(pod->swork[1]), (double)(toten - parten)));
373:         PetscCall(VecDestroy(&v));
374:       }
375:     }
376:   }
377:   /* new tip */
378:   pod->curr = (pod->curr + 1) % pod->maxn;
379:   PetscFunctionReturn(PETSC_SUCCESS);
380: }

382: static PetscErrorCode KSPGuessSetFromOptions_POD(KSPGuess guess)
383: {
384:   KSPGuessPOD *pod = (KSPGuessPOD *)guess->data;

386:   PetscFunctionBegin;
387:   PetscOptionsBegin(PetscObjectComm((PetscObject)guess), ((PetscObject)guess)->prefix, "POD initial guess options", "KSPGuess");
388:   PetscCall(PetscOptionsInt("-ksp_guess_pod_size", "Number of snapshots", NULL, pod->maxn, &pod->maxn, NULL));
389:   PetscCall(PetscOptionsBool("-ksp_guess_pod_monitor", "Monitor initial guess generator", NULL, pod->monitor, &pod->monitor, NULL));
390:   PetscCall(PetscOptionsReal("-ksp_guess_pod_tol", "Tolerance to retain eigenvectors", "KSPGuessSetTolerance", pod->tol, &pod->tol, NULL));
391:   PetscCall(PetscOptionsBool("-ksp_guess_pod_Ainner", "Use the operator as inner product (must be SPD)", NULL, pod->Aspd, &pod->Aspd, NULL));
392:   PetscOptionsEnd();
393:   PetscFunctionReturn(PETSC_SUCCESS);
394: }

396: static PetscErrorCode KSPGuessSetTolerance_POD(KSPGuess guess, PetscReal tol)
397: {
398:   KSPGuessPOD *pod = (KSPGuessPOD *)guess->data;

400:   PetscFunctionBegin;
401:   pod->tol = tol;
402:   PetscFunctionReturn(PETSC_SUCCESS);
403: }

405: static PetscErrorCode KSPGuessView_POD(KSPGuess guess, PetscViewer viewer)
406: {
407:   KSPGuessPOD *pod = (KSPGuessPOD *)guess->data;
408:   PetscBool    isascii;

410:   PetscFunctionBegin;
411:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
412:   if (isascii) PetscCall(PetscViewerASCIIPrintf(viewer, "Max size %" PetscInt_FMT ", tolerance %g, Ainner %d\n", pod->maxn, (double)pod->tol, pod->Aspd));
413:   PetscFunctionReturn(PETSC_SUCCESS);
414: }

416: /*MC
417:     KSPGUESSPOD - Implements a proper orthogonal decomposition based Galerkin scheme for repeated linear system solves.

419:   Options Database Keys:
420: +  -ksp_guess_pod_size size            - Number of snapshots
421: .  -ksp_guess_pod_monitor (true|false) - Monitor initial guess generator
422: .  -ksp_guess_pod_tol tol              - Tolerance to retain eigenvectors
423: -  -ksp_guess_pod_Ainner (true|false)  - Use the operator as inner product (must be SPD)

425:   Level: intermediate

427:   Note:
428:   The initial guess is obtained by solving a small and dense linear system, obtained by Galerkin projection on a lower dimensional space generated by the previous solutions as presented in {cite}`volkwein2013proper`.

430: .seealso: [](ch_ksp), `KSPGuess`, `KSPGuessType`, `KSPGuessCreate()`, `KSPSetGuess()`, `KSPGetGuess()`
431: M*/
432: PetscErrorCode KSPGuessCreate_POD(KSPGuess guess)
433: {
434:   KSPGuessPOD *pod;

436:   PetscFunctionBegin;
437:   PetscCall(PetscNew(&pod));
438:   pod->maxn   = 10;
439:   pod->tol    = PETSC_MACHINE_EPSILON;
440:   guess->data = pod;

442:   guess->ops->setfromoptions = KSPGuessSetFromOptions_POD;
443:   guess->ops->destroy        = KSPGuessDestroy_POD;
444:   guess->ops->settolerance   = KSPGuessSetTolerance_POD;
445:   guess->ops->setup          = KSPGuessSetUp_POD;
446:   guess->ops->view           = KSPGuessView_POD;
447:   guess->ops->reset          = KSPGuessReset_POD;
448:   guess->ops->update         = KSPGuessUpdate_POD;
449:   guess->ops->formguess      = KSPGuessFormGuess_POD;
450:   PetscFunctionReturn(PETSC_SUCCESS);
451: }