Actual source code: hpddm.cxx

petsc-3.13.6 2020-09-29
Report Typos and Errors
  1:  #include <petsc/private/dmimpl.h>
  2:  #include <petsc/private/matimpl.h>
  3:  #include <petsc/private/petschpddm.h>
  4:  #include <petsc/private/pcimpl.h>
  5:                                   /* otherwise, it is assumed that one is compiling libhpddm_petsc => circular dependency */

  7: static PetscErrorCode (*loadedSym)(HPDDM::Schwarz<PetscScalar>* const, IS const, IS, const Mat, Mat, std::vector<Vec>, PetscInt* const, PC_HPDDM_Level** const) = NULL;

  9: static PetscBool PCHPDDMPackageInitialized = PETSC_FALSE;
 10: static PetscBool citePC = PETSC_FALSE;
 11: static const char hpddmCitationPC[] = "@inproceedings{jolivet2013scalable,\n\tTitle = {{Scalable Domain Decomposition Preconditioners For Heterogeneous Elliptic Problems}},\n\tAuthor = {Jolivet, Pierre and Hecht, Fr\'ed\'eric and Nataf, Fr\'ed\'eric and Prud'homme, Christophe},\n\tOrganization = {ACM},\n\tYear = {2013},\n\tSeries = {SC13},\n\tBooktitle = {Proceedings of the 2013 International Conference for High Performance Computing, Networking, Storage and Analysis}\n}\n";

 13: PetscLogEvent PC_HPDDM_Strc;
 14: PetscLogEvent PC_HPDDM_PtAP;
 15: PetscLogEvent PC_HPDDM_PtBP;
 16: PetscLogEvent PC_HPDDM_Next;

 18: static const char *PCHPDDMCoarseCorrectionTypes[] = { "deflated", "additive", "balanced" };

 20: static PetscErrorCode PCReset_HPDDM(PC pc)
 21: {
 22:   PC_HPDDM       *data = (PC_HPDDM*)pc->data;
 23:   PetscInt       i;

 27:   if (data->levels) {
 28:     for (i = 0; i < PETSC_HPDDM_MAXLEVELS && data->levels[i]; ++i) {
 29:       KSPDestroy(&data->levels[i]->ksp);
 30:       PCDestroy(&data->levels[i]->pc);
 31:       PetscFree(data->levels[i]);
 32:     }
 33:     PetscFree(data->levels);
 34:   }

 36:   ISDestroy(&data->is);
 37:   MatDestroy(&data->aux);
 38:   MatDestroy(&data->B);
 39:   data->correction = PC_HPDDM_COARSE_CORRECTION_DEFLATED;
 40:   data->Neumann    = PETSC_FALSE;
 41:   data->setup      = NULL;
 42:   data->setup_ctx  = NULL;
 43:   return(0);
 44: }

 46: static PetscErrorCode PCDestroy_HPDDM(PC pc)
 47: {
 48:   PC_HPDDM       *data = (PC_HPDDM*)pc->data;

 52:   PCReset_HPDDM(pc);
 53:   PetscFree(data);
 54:   PetscObjectChangeTypeName((PetscObject)pc, 0);
 55:   PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetAuxiliaryMat_C", NULL);
 56:   PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMHasNeumannMat_C", NULL);
 57:   PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetRHSMat_C", NULL);
 58:   PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetCoarseCorrectionType_C", NULL);
 59:   PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetCoarseCorrectionType_C", NULL);
 60:   return(0);
 61: }

 63: static PetscErrorCode PCHPDDMSetAuxiliaryMat_HPDDM(PC pc, IS is, Mat A, PetscErrorCode (*setup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void*), void* setup_ctx)
 64: {
 65:   PC_HPDDM       *data = (PC_HPDDM*)pc->data;

 69:   if (is) {
 70:     PetscObjectReference((PetscObject)is);
 71:     if (data->is) { /* new overlap definition resets the PC */
 72:       PCReset_HPDDM(pc);
 73:       pc->setfromoptionscalled = 0;
 74:     }
 75:     ISDestroy(&data->is);
 76:     data->is = is;
 77:   }
 78:   if (A) {
 79:     PetscObjectReference((PetscObject)A);
 80:     MatDestroy(&data->aux);
 81:     data->aux = A;
 82:   }
 83:   if (setup) {
 84:     data->setup = setup;
 85:     data->setup_ctx = setup_ctx;
 86:   }
 87:   return(0);
 88: }

 90: /*@
 91:      PCHPDDMSetAuxiliaryMat - Sets the auxiliary matrix used by PCHPDDM for the concurrent GenEO eigenproblems at the finest level. As an example, in a finite element context with nonoverlapping subdomains plus (overlapping) ghost elements, this could be the unassembled (Neumann) local overlapping operator. As opposed to the assembled (Dirichlet) local overlapping operator obtained by summing neighborhood contributions at the interface of ghost elements.

 93:    Input Parameters:
 94: +     pc - preconditioner context
 95: .     is - index set of the local auxiliary, e.g., Neumann, matrix
 96: .     A - auxiliary sequential matrix
 97: .     setup - function for generating the auxiliary matrix
 98: -     setup_ctx - context for setup

100:    Level: intermediate

102: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCHPDDMSetRHSMat(), MATIS
103: @*/
104: PetscErrorCode PCHPDDMSetAuxiliaryMat(PC pc, IS is, Mat A, PetscErrorCode (*setup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void*), void* setup_ctx)
105: {

112:   PetscTryMethod(pc, "PCHPDDMSetAuxiliaryMat_C", (PC, IS, Mat, PetscErrorCode (*)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void*), void*), (pc, is, A, setup, setup_ctx));
113:   return(0);
114: }

116: static PetscErrorCode PCHPDDMHasNeumannMat_HPDDM(PC pc, PetscBool has)
117: {
118:   PC_HPDDM *data = (PC_HPDDM*)pc->data;

121:   data->Neumann = has;
122:   return(0);
123: }

125: /*@
126:      PCHPDDMHasNeumannMat - Informs PCHPDDM that the Mat passed to PCHPDDMSetAuxiliaryMat() is the local Neumann matrix. This may be used to bypass a call to MatCreateSubMatrices() and to MatConvert() for MATMPISBAIJ matrices. If a DMCreateNeumannOverlap() implementation is available in the DM attached to the Pmat, or the Amat, or the PC, the flag is internally set to PETSC_TRUE. Its default value is otherwise PETSC_FALSE.

128:    Input Parameters:
129: +     pc - preconditioner context
130: -     has - Boolean value

132:    Level: intermediate

134: .seealso:  PCHPDDM, PCHPDDMSetAuxiliaryMat()
135: @*/
136: PetscErrorCode PCHPDDMHasNeumannMat(PC pc, PetscBool has)
137: {

142:   PetscTryMethod(pc, "PCHPDDMHasNeumannMat_C", (PC, PetscBool), (pc, has));
143:   return(0);
144: }

146: static PetscErrorCode PCHPDDMSetRHSMat_HPDDM(PC pc, Mat B)
147: {
148:   PC_HPDDM       *data = (PC_HPDDM*)pc->data;

152:   PetscObjectReference((PetscObject)B);
153:   MatDestroy(&data->B);
154:   data->B = B;
155:   return(0);
156: }

158: /*@
159:      PCHPDDMSetRHSMat - Sets the right-hand side matrix used by PCHPDDM for the concurrent GenEO eigenproblems at the finest level. Must be used in conjuction with PCHPDDMSetAuxiliaryMat(N), so that Nv = lambda Bv is solved using EPSSetOperators(N, B). It is assumed that N and B are provided using the same numbering. This provides a means to try more advanced methods such as GenEO-II or H-GenEO.

161:    Input Parameters:
162: +     pc - preconditioner context
163: -     B - right-hand side sequential matrix

165:    Level: advanced

167: .seealso:  PCHPDDMSetAuxiliaryMat(), PCHPDDM
168: @*/
169: PetscErrorCode PCHPDDMSetRHSMat(PC pc, Mat B)
170: {

175:   if (B) {
177:     PetscTryMethod(pc, "PCHPDDMSetRHSMat_C", (PC, Mat), (pc, B));
178:   }
179:   return(0);
180: }

182: static PetscErrorCode PCSetFromOptions_HPDDM(PetscOptionItems *PetscOptionsObject, PC pc)
183: {
184:   PC_HPDDM       *data = (PC_HPDDM*)pc->data;
185:   PC_HPDDM_Level **levels = data->levels;
186:   char           prefix[256];
187:   int            i = 1;
188:   PetscMPIInt    size, previous;
189:   PetscInt       n;
190:   PetscBool      flg = PETSC_TRUE;

194:   if (!data->levels) {
195:     PetscCalloc1(PETSC_HPDDM_MAXLEVELS, &levels);
196:     data->levels = levels;
197:   }
198:   PetscOptionsHead(PetscOptionsObject, "PCHPDDM options");
199:   MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size);
200:   previous = size;
201:   while (i < PETSC_HPDDM_MAXLEVELS) {
202:     PetscInt p = 1;

204:     if (!data->levels[i - 1]) {
205:       PetscNewLog(pc, data->levels + i - 1);
206:     }
207:     data->levels[i - 1]->parent = data;
208:     /* if the previous level has a single process, it is not possible to coarsen further */
209:     if (previous == 1 || !flg) break;
210:     data->levels[i - 1]->nu = 0;
211:     data->levels[i - 1]->threshold = -1.0;
212:     PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_nev", i);
213:     PetscOptionsInt(prefix, "Local number of deflation vectors computed by SLEPc", "none", data->levels[i - 1]->nu, &data->levels[i - 1]->nu, NULL);
214:     PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold", i);
215:     PetscOptionsReal(prefix, "Local threshold for selecting deflation vectors returned by SLEPc", "none", data->levels[i - 1]->threshold, &data->levels[i - 1]->threshold, NULL);
216:     /* if there is no prescribed coarsening, just break out of the loop */
217:     if (data->levels[i - 1]->threshold <= 0.0 && data->levels[i - 1]->nu <= 0) break;
218:     else {
219:       ++i;
220:       PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_nev", i);
221:       PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg);
222:       if (!flg) {
223:         PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold", i);
224:         PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg);
225:       }
226:       if (flg) {
227:         /* if there are coarsening options for the next level, then register it  */
228:         /* otherwise, don't to avoid having both options levels_N_p and coarse_p */
229:         PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_p", i);
230:         PetscOptionsRangeInt(prefix, "Number of processes used to assemble the coarse operator at this level", "none", p, &p, &flg, 1, PetscMax(1, previous / 2));
231:         previous = p;
232:       }
233:     }
234:   }
235:   data->N = i;
236:   n = 1;
237:   if (i > 1) {
238:     PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_coarse_p");
239:     PetscOptionsRangeInt(prefix, "Number of processes used to assemble the coarsest operator", "none", n, &n, NULL, 1, PetscMax(1, previous / 2));
240:     PetscOptionsEList("-pc_hpddm_coarse_correction", "Type of coarse correction applied each iteration", "PCHPDDMSetCoarseCorrectionType", PCHPDDMCoarseCorrectionTypes, 3, PCHPDDMCoarseCorrectionTypes[PC_HPDDM_COARSE_CORRECTION_DEFLATED], &n, &flg);
241:     if (flg) data->correction = PCHPDDMCoarseCorrectionType(n);
242:     PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_has_neumann");
243:     PetscOptionsBool(prefix, "Is the auxiliary Mat the local Neumann matrix?", "PCHPDDMHasNeumannMat", data->Neumann, &data->Neumann, NULL);
244:   }
245:   PetscOptionsTail();
246:   while (i < PETSC_HPDDM_MAXLEVELS && data->levels[i]) {
247:     PetscFree(data->levels[i++]);
248:   }
249:   return(0);
250: }

252: static PetscErrorCode PCApply_HPDDM(PC pc, Vec x, Vec y)
253: {
254:   PC_HPDDM       *data = (PC_HPDDM*)pc->data;

258:   PetscCitationsRegister(hpddmCitationPC, &citePC);
259:   if (data->levels[0]->ksp) {
260:     KSPSolve(data->levels[0]->ksp, x, y);
261:   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "No KSP attached to PCHPDDM");
262:   return(0);
263: }

265: /*
266:      PCHPDDMGetComplexities - Computes the grid and operator complexities.

268:    Input Parameter:
269: .     pc - preconditioner context

271:    Output Parameters:
272: +     gc - grid complexity = sum_i(m_i) / m_1
273: .     oc - operator complexity = sum_i(nnz_i) / nnz_1

275:    Notes:
276:      PCGAMG does not follow the usual convention and names the grid complexity what is usually referred to as the operator complexity. PCHPDDM follows what is found in the literature, and in particular, what you get with PCHYPRE and -pc_hypre_boomeramg_print_statistics.

278:    Level: advanced

280: .seealso:  PCMGGetGridComplexity(), PCHPDDM
281: */
282: static PetscErrorCode PCHPDDMGetComplexities(PC pc, PetscReal *gc, PetscReal *oc)
283: {
284:   PC_HPDDM       *data = (PC_HPDDM*)pc->data;
285:   MatInfo        info;
286:   PetscInt       n, m;
287:   PetscLogDouble accumulate[2] { }, nnz1 = 1.0, m1 = 1.0;

291:   for (n = 0, *gc = 0, *oc = 0; n < data->N; ++n) {
292:     if (data->levels[n]->ksp) {
293:       Mat P;
294:       KSPGetOperators(data->levels[n]->ksp, NULL, &P);
295:       MatGetSize(P, &m, NULL);
296:       accumulate[0] += m;
297:       MatGetInfo(P, MAT_GLOBAL_SUM, &info);
298:       accumulate[1] += info.nz_used;
299:       if (n == 0) {
300:         m1 = m;
301:         nnz1 = info.nz_used;
302:       }
303:     }
304:   }
305:   *gc = static_cast<PetscReal>(accumulate[0]/m1);
306:   *oc = static_cast<PetscReal>(accumulate[1]/nnz1);
307:   return(0);
308: }

310: static PetscErrorCode PCView_HPDDM(PC pc, PetscViewer viewer)
311: {
312:   PC_HPDDM       *data = (PC_HPDDM*)pc->data;
313:   PetscViewer    subviewer;
314:   PetscSubcomm   subcomm;
315:   PetscReal      oc, gc;
316:   PetscInt       i, tabs;
317:   PetscMPIInt    size, color, rank;
318:   PetscBool      ascii;

322:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &ascii);
323:   if (ascii) {
324:     PetscViewerASCIIPrintf(viewer, "level%s: %D\n", data->N > 1 ? "s" : "", data->N);
325:     PCHPDDMGetComplexities(pc, &gc, &oc);
326:     if (data->N > 1) {
327:       PetscViewerASCIIPrintf(viewer, "Neumann matrix attached? %s\n", PetscBools[data->Neumann]);
328:       PetscViewerASCIIPrintf(viewer, "coarse correction: %s\n", PCHPDDMCoarseCorrectionTypes[data->correction]);
329:       PetscViewerASCIIPrintf(viewer, "on process #0, value%s (+ threshold%s if available) for selecting deflation vectors:", data->N > 2 ? "s" : "", data->N > 2 ? "s" : "");
330:       PetscViewerASCIIGetTab(viewer, &tabs);
331:       PetscViewerASCIISetTab(viewer, 0);
332:       for (i = 1; i < data->N; ++i) {
333:         PetscViewerASCIIPrintf(viewer, " %D", data->levels[i - 1]->nu);
334:         if (data->levels[i - 1]->threshold > -0.1) {
335:           PetscViewerASCIIPrintf(viewer, " (%g)", (double)data->levels[i - 1]->threshold);
336:         }
337:       }
338:       PetscViewerASCIIPrintf(viewer, "\n");
339:       PetscViewerASCIISetTab(viewer, tabs);
340:     }
341:     PetscViewerASCIIPrintf(viewer, "grid and operator complexities: %g %g\n", (double)gc, (double)oc);
342:     if (data->levels[0]->ksp) {
343:       KSPView(data->levels[0]->ksp, viewer);
344:       if (data->levels[0]->pc) {
345:         PCView(data->levels[0]->pc, viewer);
346:       }
347:       for (i = 1; i < data->N; ++i) {
348:         if (data->levels[i]->ksp) color = 1;
349:         else color = 0;
350:         MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size);
351:         MPI_Comm_size(PetscObjectComm((PetscObject)pc), &rank);
352:         PetscSubcommCreate(PetscObjectComm((PetscObject)pc), &subcomm);
353:         PetscSubcommSetNumber(subcomm, PetscMin(size, 2));
354:         PetscSubcommSetTypeGeneral(subcomm, color, rank);
355:         PetscViewerASCIIPushTab(viewer);
356:         PetscViewerGetSubViewer(viewer, PetscSubcommChild(subcomm), &subviewer);
357:         if (color == 1) {
358:           KSPView(data->levels[i]->ksp, subviewer);
359:           if (data->levels[i]->pc) {
360:             PCView(data->levels[i]->pc, subviewer);
361:           }
362:           PetscViewerFlush(subviewer);
363:         }
364:         PetscViewerRestoreSubViewer(viewer, PetscSubcommChild(subcomm), &subviewer);
365:         PetscViewerASCIIPopTab(viewer);
366:         PetscSubcommDestroy(&subcomm);
367:         PetscViewerFlush(viewer);
368:       }
369:     }
370:   }
371:   return(0);
372: }

374: static PetscErrorCode PCHPDDMShellSetUp(PC pc)
375: {
376:   PC_HPDDM_Level *ctx;
377:   Mat            A, P;
378:   Vec            x;
379:   const char     *pcpre;

383:   PCShellGetContext(pc, (void**)&ctx);
384:   KSPGetOptionsPrefix(ctx->ksp, &pcpre);
385:   KSPGetOperators(ctx->ksp, &A, &P);
386:   /* smoother */
387:   PCSetOptionsPrefix(ctx->pc, pcpre);
388:   PCSetOperators(ctx->pc, A, P);
389:   if (!ctx->v[0]) {
390:     VecDuplicateVecs(ctx->D, 1, &ctx->v[0]);
391:     if (!std::is_same<PetscScalar, PetscReal>::value) {
392:       VecDestroy(&ctx->D);
393:     }
394:     MatCreateVecs(A, &x, NULL);
395:     VecDuplicateVecs(x, 2, &ctx->v[1]);
396:     VecDestroy(&x);
397:   }
398:   return(0);
399: }

401: PETSC_STATIC_INLINE PetscErrorCode PCHPDDMDeflate_Private(PC pc, Vec x, Vec y)
402: {
403:   PC_HPDDM_Level *ctx;
404:   PetscScalar    *out;

408:   PCShellGetContext(pc, (void**)&ctx);
409:   /* going from PETSc to HPDDM numbering */
410:   VecScatterBegin(ctx->scatter, x, ctx->v[0][0], INSERT_VALUES, SCATTER_FORWARD);
411:   VecScatterEnd(ctx->scatter, x, ctx->v[0][0], INSERT_VALUES, SCATTER_FORWARD);
412:   VecGetArray(ctx->v[0][0], &out);
413:   ctx->P->deflation<false>(NULL, out, 1); /* y = Q x */
414:   VecRestoreArray(ctx->v[0][0], &out);
415:   /* going from HPDDM to PETSc numbering */
416:   VecScatterBegin(ctx->scatter, ctx->v[0][0], y, INSERT_VALUES, SCATTER_REVERSE);
417:   VecScatterEnd(ctx->scatter, ctx->v[0][0], y, INSERT_VALUES, SCATTER_REVERSE);
418:   return(0);
419: }

421: /*@C
422:      PCHPDDMShellApply - Applies a (2) deflated, (1) additive, or (3) balanced coarse correction. In what follows, E = Z Pmat Z^T and Q = Z^T E^-1 Z.

424: .vb
425:    (1) y =                Pmat^-1              x + Q x,
426:    (2) y =                Pmat^-1 (I - Amat Q) x + Q x (default),
427:    (3) y = (I - Q Amat^T) Pmat^-1 (I - Amat Q) x + Q x.
428: .ve

430:    Input Parameters:
431: +     pc - preconditioner context
432: .     x - input vector
433: -     y - output vector

435:    Application Interface Routine: PCApply()

437:    Notes:
438:      The options of Pmat^1 = pc(Pmat) are prefixed by -pc_hpddm_levels_1_pc_. Z is a tall-and-skiny matrix assembled by HPDDM. The number of processes on which (Z Pmat Z^T) is aggregated is set via -pc_hpddm_coarse_p.
439:      The options of (Z Pmat Z^T)^-1 = ksp(Z Pmat Z^T) are prefixed by -pc_hpddm_coarse_ (KSPPREONLY and PCCHOLESKY by default), unless a multilevel correction is turned on, in which case, this function is called recursively at each level except the coarsest one.
440:      (1) and (2) visit the "next" level (in terms of coarsening) once per Section 1.5 Writing Application Codes with PETSc, while (3) visits it twice, so it is asymptotically twice costlier. (2) is not symmetric even if both Amat and Pmat are symmetric.

442:    Level: advanced

444: .seealso:  PCHPDDM, PCHPDDMCoarseCorrectionType
445: @*/
446: static PetscErrorCode PCHPDDMShellApply(PC pc, Vec x, Vec y)
447: {
448:   PC_HPDDM_Level *ctx;
449:   Mat            A;

453:   PCShellGetContext(pc, (void**)&ctx);
454:   if (ctx->P) {
455:     KSPGetOperators(ctx->ksp, &A, NULL);
456:     PCHPDDMDeflate_Private(pc, x, y);                    /* y = Q x                          */
457:     if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
458:       MatMult(A, y, ctx->v[1][0]);                       /* y = A Q x                        */
459:       VecWAXPY(ctx->v[1][1], -1.0, ctx->v[1][0], x);     /* y = (I - A Q) x                  */
460:       PCApply(ctx->pc, ctx->v[1][1], ctx->v[1][0]);      /* y = M^-1 (I - A Q) x             */
461:       if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
462:         MatMultTranspose(A, ctx->v[1][0], ctx->v[1][1]); /* z = A^T M^-1 (I - A Q) x         */
463:         PCHPDDMDeflate_Private(pc, ctx->v[1][1], ctx->v[1][1]);
464:         VecAXPY(ctx->v[1][0], -1.0, ctx->v[1][1]);       /* y = (I - Q A^T) M^-1 (I - A Q) x */
465:       }
466:       VecAXPY(y, 1.0, ctx->v[1][0]);                     /* y = y + Q x                      */
467:     } else if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_ADDITIVE) {
468:       PCApply(ctx->pc, x, ctx->v[1][0]);
469:       VecAXPY(y, 1.0, ctx->v[1][0]);                     /* y = M^-1 x + Q x                 */
470:     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with an unknown PCHPDDMCoarseCorrectionType %d", ctx->parent->correction);
471:   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object");
472:   return(0);
473: }

475: static PetscErrorCode PCHPDDMShellDestroy(PC pc)
476: {
477:   PC_HPDDM_Level *ctx;

481:   PCShellGetContext(pc, (void**)&ctx);
482:   HPDDM::Schwarz<PetscScalar>::destroy(ctx, PETSC_TRUE);
483:   VecDestroyVecs(1, &ctx->v[0]);
484:   VecDestroyVecs(2, &ctx->v[1]);
485:   VecDestroy(&ctx->D);
486:   VecScatterDestroy(&ctx->scatter);
487:   PCDestroy(&ctx->pc);
488:   return(0);
489: }

491: static PetscErrorCode PCHPDDMSetUpNeumannOverlap_Private(PC pc)
492: {
493:   PC_HPDDM       *data = (PC_HPDDM*)pc->data;

497:   if (data->setup) {
498:     Mat       P;
499:     Vec       x, xt = NULL;
500:     PetscReal t = 0.0, s = 0.0;

502:     PCGetOperators(pc, NULL, &P);
503:     PetscObjectQuery((PetscObject)P, "__SNES_latest_X", (PetscObject*)&x);
504:     PetscStackPush("PCHPDDM Neumann callback");
505:     (*data->setup)(data->aux, t, x, xt, s, data->is, data->setup_ctx);
506:     PetscStackPop;
507:   }
508:   return(0);
509: }

511: static PetscErrorCode PCSetUp_HPDDM(PC pc)
512: {
513:   PC_HPDDM                 *data = (PC_HPDDM*)pc->data;
514:   PC                       inner;
515:   Mat                      *sub, A, P, N, C, uaux = NULL;
516:   Vec                      xin, v;
517:   std::vector<Vec>         initial;
518:   IS                       is[1], loc, uis = data->is;
519:   ISLocalToGlobalMapping   l2g;
520:   char                     prefix[256];
521:   const char               *pcpre;
522:   const PetscScalar* const *ev;
523:   PetscInt                 n, requested = data->N, reused = 0;
524:   PetscBool                subdomains = PETSC_FALSE, flag = PETSC_FALSE, ismatis;
525:   DM                       dm;
526:   PetscErrorCode           ierr;

529:   if (!data->levels[0]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not a single level allocated");
530:   PCGetOptionsPrefix(pc, &pcpre);
531:   PCGetOperators(pc, &A, &P);
532:   if (!data->levels[0]->ksp) {
533:     KSPCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->ksp);
534:     PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_%s_", pcpre ? pcpre : "", data->N > 1 ? "levels_1" : "coarse");
535:     KSPSetOptionsPrefix(data->levels[0]->ksp, prefix);
536:     KSPSetType(data->levels[0]->ksp, KSPPREONLY);
537:   } else if(data->levels[0]->ksp->pc && data->levels[0]->ksp->pc->setupcalled == 1 && data->levels[0]->ksp->pc->reusepreconditioner) {
538:     /* if the fine level PCSHELL exists, its setup has succeeded, and one wants to reuse it, */
539:     /* then just propagate the appropriate flag to the coarser levels                        */
540:     for (n = 0; n < PETSC_HPDDM_MAXLEVELS && data->levels[n]; ++n) {
541:       /* the following KSP and PC may be NULL for some processes, hence the check            */
542:       if (data->levels[n]->ksp) {
543:         KSPSetReusePreconditioner(data->levels[n]->ksp, PETSC_TRUE);
544:       }
545:       if (data->levels[n]->pc) {
546:         PCSetReusePreconditioner(data->levels[n]->pc, PETSC_TRUE);
547:       }
548:     }
549:     /* early bail out because there is nothing to do */
550:     return(0);
551:   } else {
552:     /* reset coarser levels */
553:     for (n = 1; n < PETSC_HPDDM_MAXLEVELS && data->levels[n]; ++n) {
554:       if(data->levels[n]->ksp && data->levels[n]->ksp->pc && data->levels[n]->ksp->pc->setupcalled == 1 && data->levels[n]->ksp->pc->reusepreconditioner && n < data->N) {
555:         reused = data->N - n;
556:         break;
557:       }
558:       KSPDestroy(&data->levels[n]->ksp);
559:       PCDestroy(&data->levels[n]->pc);
560:     }
561:     /* check if some coarser levels are being reused */
562:     MPI_Allreduce(MPI_IN_PLACE, &reused, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)pc));
563:     const int *addr = data->levels[0]->P ? data->levels[0]->P->getAddrLocal() : &HPDDM::i__0;

565:     if (addr != &HPDDM::i__0 && reused != data->N - 1) {
566:       /* reuse previously computed eigenvectors */
567:       ev = data->levels[0]->P->getVectors();
568:       if (ev) {
569:         initial.reserve(*addr);
570:         VecCreateSeqWithArray(PETSC_COMM_SELF, 1, data->levels[0]->P->getDof(), ev[0], &xin);
571:         for (n = 0; n < *addr; ++n) {
572:           VecDuplicate(xin, &v);
573:           VecPlaceArray(xin, ev[n]);
574:           VecCopy(xin, v);
575:           initial.emplace_back(v);
576:           VecResetArray(xin);
577:         }
578:         VecDestroy(&xin);
579:       }
580:     }
581:   }
582:   data->N -= reused;
583:   KSPSetOperators(data->levels[0]->ksp, A, P);

585:   PetscObjectTypeCompare((PetscObject)P, MATIS, &ismatis);
586:   if (!data->is && !ismatis) {
587:     PetscErrorCode (*create)(DM, IS*, Mat*, PetscErrorCode (**)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void*), void**) = NULL;
588:     PetscErrorCode (*usetup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void*) = NULL;
589:     void           *uctx = NULL;

591:     /* first see if we can get the data from the DM */
592:     MatGetDM(P, &dm);
593:     if (!dm) {
594:       MatGetDM(A, &dm);
595:     }
596:     if (!dm) {
597:       PCGetDM(pc, &dm);
598:     }
599:     if (dm) { /* this is the hook for DMPLEX and DMDA for which the auxiliary Mat is the local Neumann matrix */
600:       PetscObjectQueryFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", &create);
601:       if (create) {
602:         (*create)(dm, &uis, &uaux, &usetup, &uctx);
603:         data->Neumann = PETSC_TRUE;
604:       }
605:     }
606:     if (!create) {
607:       if (!uis) {
608:         PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_IS", (PetscObject*)&uis);
609:         PetscObjectReference((PetscObject)uis);
610:       }
611:       if (!uaux) {
612:         PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_Mat", (PetscObject*)&uaux);
613:         PetscObjectReference((PetscObject)uaux);
614:       }
615:     }
616:     PCHPDDMSetAuxiliaryMat(pc, uis, uaux, usetup, uctx);
617:     MatDestroy(&uaux);
618:     ISDestroy(&uis);
619:   }

621:   if (!ismatis) {
622:     PCHPDDMSetUpNeumannOverlap_Private(pc);
623:   }

625:   if (data->is || (ismatis && data->N > 1)) {
626:     if (ismatis) {
627:       std::initializer_list<std::string> list = { MATSEQBAIJ, MATSEQSBAIJ };
628:       MatISGetLocalMat(P, &N);
629:       std::initializer_list<std::string>::const_iterator it = std::find(list.begin(), list.end(), ((PetscObject)N)->type_name);
630:       MatISRestoreLocalMat(P, &N);
631:       switch (std::distance(list.begin(), it)) {
632:       case 0:
633:         MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &C);
634:         break;
635:       case 1:
636:         /* MatCreateSubMatrices does not work with MATSBAIJ and unsorted ISes, so convert to MPIBAIJ */
637:         MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &C);
638:         MatSetOption(C, MAT_SYMMETRIC, PETSC_TRUE);
639:         break;
640:       default:
641:         MatConvert(P, MATMPIAIJ, MAT_INITIAL_MATRIX, &C);
642:       }
643:       MatGetLocalToGlobalMapping(P, &l2g, NULL);
644:       PetscObjectReference((PetscObject)P);
645:       KSPSetOperators(data->levels[0]->ksp, A, C);
646:       std::swap(C, P);
647:       ISLocalToGlobalMappingGetSize(l2g, &n);
648:       ISCreateStride(PETSC_COMM_SELF, n, 0, 1, &loc);
649:       ISLocalToGlobalMappingApplyIS(l2g, loc, &is[0]);
650:       ISDestroy(&loc);
651:       /* the auxiliary Mat is _not_ the local Neumann matrix                                */
652:       /* it is the local Neumann matrix augmented (with zeros) through MatIncreaseOverlap() */
653:       data->Neumann = PETSC_FALSE;
654:     } else {
655:       is[0] = data->is;
656:       PetscOptionsGetBool(NULL, pcpre, "-pc_hpddm_define_subdomains", &subdomains, NULL);
657:       PetscOptionsGetBool(NULL, pcpre, "-pc_hpddm_has_neumann", &data->Neumann, NULL);
658:       ISCreateStride(PetscObjectComm((PetscObject)data->is), P->rmap->n, P->rmap->rstart, 1, &loc);
659:     }
660:     if (data->N > 1 && (data->aux || ismatis)) {
661:       if (!loadedSym) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "HPDDM library not loaded, cannot use more than one level");
662:       MatSetOption(P, MAT_SUBMAT_SINGLEIS, PETSC_TRUE);
663:       if (ismatis) {
664:         /* needed by HPDDM (currently) so that the partition of unity is 0 on subdomain interfaces */
665:         MatIncreaseOverlap(P, 1, is, 1);
666:         ISDestroy(&data->is);
667:         data->is = is[0];
668:       } else {
669: #if defined(PETSC_USE_DEBUG)
670:         PetscBool equal;
671:         IS        intersect;

673:         ISIntersect(data->is, loc, &intersect);
674:         ISEqualUnsorted(loc, intersect, &equal);
675:         ISDestroy(&intersect);
676:         if (!equal) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "IS of the auxiliary Mat does not include all local rows of A");
677: #endif
678:         if (!data->Neumann) {
679:           PetscObjectTypeCompare((PetscObject)P, MATMPISBAIJ, &flag);
680:           if (flag) {
681:             /* maybe better to ISSort(is[0]), MatCreateSubMatrices, and then MatPermute */
682:             /* but there is no MatPermute_SeqSBAIJ, so as before, just use MATMPIBAIJ   */
683:             MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &uaux);
684:             flag = PETSC_FALSE;
685:           }
686:         }
687:       }
688:       if (!uaux) {
689:         if (data->Neumann) sub = &data->aux;
690:         else {
691:           MatCreateSubMatrices(P, 1, is, is, MAT_INITIAL_MATRIX, &sub);
692:         }
693:       } else {
694:         MatCreateSubMatrices(uaux, 1, is, is, MAT_INITIAL_MATRIX, &sub);
695:         MatDestroy(&uaux);
696:         MatConvert(sub[0], MATSEQSBAIJ, MAT_INPLACE_MATRIX, sub);
697:       }
698:       /* Vec holding the partition of unity */
699:       if (!data->levels[0]->D) {
700:         ISGetLocalSize(data->is, &n);
701:         VecCreateMPI(PETSC_COMM_SELF, n, PETSC_DETERMINE, &data->levels[0]->D);
702:       }
703:       if (!data->levels[0]->scatter) {
704:         MatCreateVecs(P, &xin, NULL);
705:         if (ismatis) {
706:           MatDestroy(&P);
707:         }
708:         VecScatterCreate(xin, data->is, data->levels[0]->D, NULL, &data->levels[0]->scatter);
709:         VecDestroy(&xin);
710:       }
711:       if (data->levels[0]->P) {
712:         /* if the pattern is the same and PCSetUp has previously succeeded, reuse HPDDM buffers and connectivity */
713:         HPDDM::Schwarz<PetscScalar>::destroy(data->levels[0], pc->setupcalled < 1 || pc->flag == DIFFERENT_NONZERO_PATTERN ? PETSC_TRUE : PETSC_FALSE);
714:       }
715:       if (!data->levels[0]->P) data->levels[0]->P = new HPDDM::Schwarz<PetscScalar>();
716:       (*loadedSym)(data->levels[0]->P, loc, data->is, sub[0], ismatis ? C : data->aux, initial, &data->N, data->levels);
717:       if (!data->Neumann)
718:         MatDestroySubMatrices(1, &sub);
719:       if (ismatis) data->is = NULL;
720:       for (n = 0; n < data->N - 1 + (reused > 0); ++n) {
721:         if (data->levels[n]->P) {
722:           PC spc;

724:           /* force the PC to be PCSHELL to do the coarse grid corrections */
725:           KSPSetSkipPCSetFromOptions(data->levels[n]->ksp, PETSC_TRUE);
726:           KSPGetPC(data->levels[n]->ksp, &spc);
727:           PCSetType(spc, PCSHELL);
728:           PCShellSetContext(spc, data->levels[n]);
729:           PCShellSetSetUp(spc, PCHPDDMShellSetUp);
730:           PCShellSetApply(spc, PCHPDDMShellApply);
731:           PCShellSetDestroy(spc, PCHPDDMShellDestroy);
732:           if (!data->levels[n]->pc) {
733:             PCCreate(PetscObjectComm((PetscObject)data->levels[n]->ksp), &data->levels[n]->pc);
734:           }
735:           if (n < reused) {
736:             PCSetReusePreconditioner(spc, PETSC_TRUE);
737:             PCSetReusePreconditioner(data->levels[n]->pc, PETSC_TRUE);
738:           }
739:           PCSetUp(spc);
740:         }
741:       }
742:     } else flag = reused ? PETSC_FALSE : PETSC_TRUE;
743:     if (!ismatis && subdomains) {
744:       if (flag) {
745:         KSPGetPC(data->levels[0]->ksp, &inner);
746:       } else inner = data->levels[0]->pc;
747:       if (inner) {
748:         PCSetType(inner, PCASM);
749:         if (!inner->setupcalled) {
750:           PCASMSetLocalSubdomains(inner, 1, is, &loc);
751:         }
752:       }
753:     }
754:     ISDestroy(&loc);
755:   } else data->N = 1 + reused; /* enforce this value to 1 + reused if there is no way to build another level */
756:   if (requested != data->N + reused) {
757:     PetscInfo5(pc, "%D levels requested, only %D built + %D reused. Options for level(s) > %D, including -%spc_hpddm_coarse_ will not be taken into account.\n", requested, data->N, reused, data->N, pcpre ? pcpre : "");
758:     PetscInfo2(pc, "It is best to tune parameters, e.g., a higher value for -%spc_hpddm_levels_%D_eps_threshold so that at least one local deflation vector will be selected.\n", pcpre ? pcpre : "", data->N);
759:     /* cannot use PCHPDDMShellDestroy because PCSHELL not set for unassembled levels */
760:     for (n = data->N - 1; n < requested - 1; ++n) {
761:       if (data->levels[n]->P) {
762:         HPDDM::Schwarz<PetscScalar>::destroy(data->levels[n], PETSC_TRUE);
763:         VecDestroyVecs(1, &data->levels[n]->v[0]);
764:         VecDestroyVecs(2, &data->levels[n]->v[1]);
765:         VecDestroy(&data->levels[n]->D);
766:         VecScatterDestroy(&data->levels[n]->scatter);
767:       }
768:     }
769:     if (reused) {
770:       for (n = reused; n < PETSC_HPDDM_MAXLEVELS && data->levels[n]; ++n) {
771:         KSPDestroy(&data->levels[n]->ksp);
772:         PCDestroy(&data->levels[n]->pc);
773:       }
774:     }
775: #if defined(PETSC_USE_DEBUG)
776:     SETERRQ7(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "%D levels requested, only %D built + %D reused. Options for level(s) > %D, including -%spc_hpddm_coarse_ will not be taken into account. It is best to tune parameters, e.g., a higher value for -%spc_hpddm_levels_%D_eps_threshold so that at least one local deflation vector will be selected. If you don't want this to error out, compile --with-debugging=0", requested, data->N, reused, data->N, pcpre ? pcpre : "", pcpre ? pcpre : "", data->N);
777: #endif
778:   }

780:   /* these solvers are created after PCSetFromOptions is called */
781:   if (pc->setfromoptionscalled) {
782:     for (n = 0; n < data->N; ++n) {
783:       if (data->levels[n]->ksp) {
784:         KSPSetFromOptions(data->levels[n]->ksp);
785:       }
786:       if (data->levels[n]->pc) {
787:         PCSetFromOptions(data->levels[n]->pc);
788:       }
789:     }
790:     pc->setfromoptionscalled = 0;
791:   }
792:   data->N += reused;
793:   return(0);
794: }

796: /*@
797:      PCHPDDMSetCoarseCorrectionType - Sets the coarse correction type.

799:    Input Parameters:
800: +     pc - preconditioner context
801: -     type - PC_HPDDM_COARSE_CORRECTION_DEFLATED, PC_HPDDM_COARSE_CORRECTION_ADDITIVE, or PC_HPDDM_COARSE_CORRECTION_BALANCED

803:    Options Database Key:
804: .   -pc_hpddm_coarse_correction <deflated, additive, balanced> - type of coarse correction to apply

806:    Level: intermediate

808: .seealso:  PCHPDDMGetCoarseCorrectionType(), PCHPDDM, PCHPDDMCoarseCorrectionType
809: @*/
810: PetscErrorCode PCHPDDMSetCoarseCorrectionType(PC pc, PCHPDDMCoarseCorrectionType type)
811: {

816:   PetscTryMethod(pc, "PCHPDDMSetCoarseCorrectionType_C", (PC, PCHPDDMCoarseCorrectionType), (pc, type));
817:   return(0);
818: }

820: /*@
821:      PCHPDDMGetCoarseCorrectionType - Gets the coarse correction type.

823:    Input Parameter:
824: .     pc - preconditioner context

826:    Output Parameter:
827: -     type - PC_HPDDM_COARSE_CORRECTION_DEFLATED, PC_HPDDM_COARSE_CORRECTION_ADDITIVE, or PC_HPDDM_COARSE_CORRECTION_BALANCED

829:    Level: intermediate

831: .seealso:  PCHPDDMSetCoarseCorrectionType(), PCHPDDM, PCHPDDMCoarseCorrectionType
832: @*/
833: PetscErrorCode PCHPDDMGetCoarseCorrectionType(PC pc, PCHPDDMCoarseCorrectionType *type)
834: {

839:   PetscUseMethod(pc, "PCHPDDMGetCoarseCorrectionType_C", (PC, PCHPDDMCoarseCorrectionType*), (pc, type));
840:   return(0);
841: }

843: static PetscErrorCode PCHPDDMSetCoarseCorrectionType_HPDDM(PC pc, PCHPDDMCoarseCorrectionType type)
844: {
845:   PC_HPDDM       *data = (PC_HPDDM*)pc->data;

848:   if (type < 0 || type > 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PCHPDDMCoarseCorrectionType %d", type);
849:   data->correction = type;
850:   return(0);
851: }

853: static PetscErrorCode PCHPDDMGetCoarseCorrectionType_HPDDM(PC pc, PCHPDDMCoarseCorrectionType *type)
854: {
855:   PC_HPDDM *data = (PC_HPDDM*)pc->data;

858:   *type = data->correction;
859:   return(0);
860: }

862: /*MC
863:      PCHPDDM - Interface with the HPDDM library.

865:    This PC may be used to build multilevel spectral domain decomposition methods based on the GenEO framework [2011, 2019]. It may be viewed as an alternative to spectral AMGe or PCBDDC with adaptive selection of constraints. A chronological bibliography of relevant publications linked with PC available in HPDDM through PCHPDDM may be found below.

867:    The matrix to be preconditioned (Pmat) may be unassembled (MATIS) or assembled (MATMPIAIJ, MATMPIBAIJ, or MATMPISBAIJ). For multilevel preconditioning, when using an assembled Pmat, one must provide an auxiliary local Mat (unassembled local operator for GenEO) using PCHPDDMSetAuxiliaryMat. Calling this routine is not needed when using a MATIS Pmat (assembly done internally using MatConvert).

869:    Options Database Keys:
870: +   -pc_hpddm_define_subdomains <true, default=false> - on the finest level, calls PCASMSetLocalSubdomains with the IS supplied in PCHPDDMSetAuxiliaryMat (only relevant with an assembled Pmat)
871: .   -pc_hpddm_has_neumann <true, default=false> - on the finest level, informs the PC that the local Neumann matrix is supplied in PCHPDDMSetAuxiliaryMat 
872: -   -pc_hpddm_coarse_correction <type, default=deflated> - determines the PCHPDDMCoarseCorrectionType when calling PCApply

874:    Options for subdomain solvers, subdomain eigensolvers (for computing deflation vectors), and the coarse solver can be set with
875: .vb
876:       -pc_hpddm_levels_%d_pc_
877:       -pc_hpddm_levels_%d_ksp_
878:       -pc_hpddm_levels_%d_eps_
879:       -pc_hpddm_levels_%d_p
880:       -pc_hpddm_levels_%d_mat_type_
881:       -pc_hpddm_coarse_
882:       -pc_hpddm_coarse_p
883:       -pc_hpddm_coarse_mat_type_
884: .ve
885:    e.g., -pc_hpddm_levels_1_sub_pc_type lu -pc_hpddm_levels_1_eps_nev 10 -pc_hpddm_levels_2_p 4 -pc_hpddm_levels_2_sub_pc_type lu -pc_hpddm_levels_2_eps_nev 10 -pc_hpddm_coarse_p 2 -pc_hpddm_coarse_mat_type baij will use 10 deflation vectors per subdomain on the fine "level 1", aggregate the fine subdomains into 4 "level 2" subdomains, then use 10 deflation vectors per subdomain on "level 2", and assemble the coarse matrix (of dimension 4 x 10 = 40) on two processes as a MATMPIBAIJ (default is MATMPISBAIJ).

887:    In order to activate a "level N+1" coarse correction, it is mandatory to call -pc_hpddm_levels_N_eps_nev <nu> or -pc_hpddm_levels_N_eps_threshold <val>. The default -pc_hpddm_coarse_p value is 1, meaning that the coarse operator is aggregated on a single process.

889:    This preconditioner requires that you build PETSc with SLEPc (--download-slepc=1). By default, the underlying concurrent eigenproblems are solved using SLEPc shift-and-invert spectral transformation. This is usually what gives the best performance for GenEO, cf. [2011, 2013]. As stated above, SLEPc options are available through -pc_hpddm_levels_%d_, e.g., -pc_hpddm_levels_1_eps_type arpack -pc_hpddm_levels_1_eps_threshold 0.1 -pc_hpddm_levels_1_st_type sinvert.

891:    References:
892: +   2011 - A robust two-level domain decomposition preconditioner for systems of PDEs. Spillane, Dolean, Hauret, Nataf, Pechstein, and Scheichl. Comptes Rendus Mathematique.
893: .   2013 - Scalable Domain Decomposition Preconditioners For Heterogeneous Elliptic Problems. Jolivet, Hecht, Nataf, and Prud'homme. SC13.
894: .   2015 - An Introduction to Domain Decomposition Methods: Algorithms, Theory, and Parallel Implementation. Dolean, Jolivet, and Nataf. SIAM.
895: -   2019 - A Multilevel Schwarz Preconditioner Based on a Hierarchy of Robust Coarse Spaces. Al Daas, Grigori, Jolivet, and Tournier.

897:    Level: intermediate

899: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCHPDDMSetAuxiliaryMat(), MATIS, PCBDDC, PCDEFLATION, PCTELESCOPE
900: M*/
901: PETSC_EXTERN PetscErrorCode PCCreate_HPDDM(PC pc)
902: {
903:   PetscBool      found;
904:   char           lib[PETSC_MAX_PATH_LEN], dlib[PETSC_MAX_PATH_LEN], dir[PETSC_MAX_PATH_LEN];
905:   PC_HPDDM       *data;

909:   if (!loadedSym) {
910:     PetscStrcpy(dir, "${PETSC_LIB_DIR}");
911:     PetscOptionsGetString(NULL, NULL, "-hpddm_dir", dir, sizeof(dir), NULL);
912:     PetscSNPrintf(lib, sizeof(lib), "%s/libhpddm_petsc", dir);
913:     PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, &found);
914:     if (found) {
915:       PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, dlib);
916: #if defined(SLEPC_LIB_DIR) /* this variable is passed during SLEPc ./configure since    */
917:     } else {               /* slepcconf.h is not yet build (and thus can't be included) */
918:       PetscStrcpy(dir, HPDDM_STR(SLEPC_LIB_DIR));
919:       PetscSNPrintf(lib, sizeof(lib), "%s/libhpddm_petsc", dir);
920:       PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, &found);
921:       if (found) {
922:         PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, dlib);
923: #endif
924:       } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "%s not found", lib);
925: #if defined(SLEPC_LIB_DIR)
926:     }
927: #endif
928:     PetscDLLibrarySym(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, NULL, "PCHPDDM_Internal", (void**)&loadedSym);
929:   }
930:   if (!loadedSym) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCHPDDM_Internal symbol not found in %s", lib);
931:   PetscNewLog(pc, &data);
932:   pc->data                     = data;
933:   pc->ops->reset               = PCReset_HPDDM;
934:   pc->ops->destroy             = PCDestroy_HPDDM;
935:   pc->ops->setfromoptions      = PCSetFromOptions_HPDDM;
936:   pc->ops->setup               = PCSetUp_HPDDM;
937:   pc->ops->apply               = PCApply_HPDDM;
938:   pc->ops->view                = PCView_HPDDM;
939:   pc->ops->applytranspose      = 0;
940:   pc->ops->applysymmetricleft  = 0;
941:   pc->ops->applysymmetricright = 0;
942:   PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetAuxiliaryMat_C", PCHPDDMSetAuxiliaryMat_HPDDM);
943:   PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMHasNeumannMat_C", PCHPDDMHasNeumannMat_HPDDM);
944:   PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetRHSMat_C", PCHPDDMSetRHSMat_HPDDM);
945:   PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetCoarseCorrectionType_C", PCHPDDMSetCoarseCorrectionType_HPDDM);
946:   PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetCoarseCorrectionType_C", PCHPDDMGetCoarseCorrectionType_HPDDM);
947:   return(0);
948: }

950: /*@C
951:      PCHPDDMInitializePackage - This function initializes everything in the PCHPDDM package. It is called from PCInitializePackage().

953:    Level: intermediate

955: .seealso:  PetscInitialize()
956: @*/
957: PetscErrorCode PCHPDDMInitializePackage(void)
958: {

962:   if (PCHPDDMPackageInitialized) return(0);
963:   PCHPDDMPackageInitialized = PETSC_TRUE;
964:   PetscRegisterFinalize(PCHPDDMFinalizePackage);
965:   /* general events registered once during package initialization */
966:   /* these events are not triggered in libpetsc,                  */
967:   /* but rather directly in libhpddm_petsc,                       */
968:   /* which is in charge of performing the following operations    */

970:   /* domain decomposition structure from Pmat sparsity pattern    */
971:   PetscLogEventRegister("PCHPDDMStrc", PC_CLASSID, &PC_HPDDM_Strc);
972:   /* Galerkin product, redistribution, and setup                  */
973:   PetscLogEventRegister("PCHPDDMPtAP", PC_CLASSID, &PC_HPDDM_PtAP);
974:   /* Galerkin product with summation, redistribution, and setup   */
975:   PetscLogEventRegister("PCHPDDMPtBP", PC_CLASSID, &PC_HPDDM_PtBP);
976:   /* next level construction using PtAP and PtBP                  */
977:   PetscLogEventRegister("PCHPDDMNext", PC_CLASSID, &PC_HPDDM_Next);
978:   return(0);
979: }

981: /*@C
982:      PCHPDDMFinalizePackage - This function frees everything from the PCHPDDM package. It is called from PetscFinalize().

984:    Level: intermediate

986: .seealso:  PetscFinalize()
987: @*/
988: PetscErrorCode PCHPDDMFinalizePackage(void)
989: {
991:   PCHPDDMPackageInitialized = PETSC_FALSE;
992:   return(0);
993: }