Actual source code: hpddm.cxx
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 */
6: #if defined(PETSC_HAVE_FORTRAN)
7: #include <petsc/private/fortranimpl.h>
8: #endif
10: static PetscErrorCode (*loadedSym)(HPDDM::Schwarz<PetscScalar>* const, IS, Mat, Mat, Mat, std::vector<Vec>, PC_HPDDM_Level** const) = NULL;
12: static PetscBool PCHPDDMPackageInitialized = PETSC_FALSE;
14: PetscLogEvent PC_HPDDM_Strc;
15: PetscLogEvent PC_HPDDM_PtAP;
16: PetscLogEvent PC_HPDDM_PtBP;
17: PetscLogEvent PC_HPDDM_Next;
18: PetscLogEvent PC_HPDDM_SetUp[PETSC_HPDDM_MAXLEVELS];
19: PetscLogEvent PC_HPDDM_Solve[PETSC_HPDDM_MAXLEVELS];
21: const char *const PCHPDDMCoarseCorrectionTypes[] = { "DEFLATED", "ADDITIVE", "BALANCED", "PCHPDDMCoarseCorrectionType", "PC_HPDDM_COARSE_CORRECTION_", NULL };
23: static PetscErrorCode PCReset_HPDDM(PC pc)
24: {
25: PC_HPDDM *data = (PC_HPDDM*)pc->data;
26: PetscInt i;
30: if (data->levels) {
31: for (i = 0; i < PETSC_HPDDM_MAXLEVELS && data->levels[i]; ++i) {
32: KSPDestroy(&data->levels[i]->ksp);
33: PCDestroy(&data->levels[i]->pc);
34: PetscFree(data->levels[i]);
35: }
36: PetscFree(data->levels);
37: }
39: ISDestroy(&data->is);
40: MatDestroy(&data->aux);
41: MatDestroy(&data->B);
42: VecDestroy(&data->normal);
43: data->correction = PC_HPDDM_COARSE_CORRECTION_DEFLATED;
44: data->Neumann = PETSC_FALSE;
45: data->setup = NULL;
46: data->setup_ctx = NULL;
47: return(0);
48: }
50: static PetscErrorCode PCDestroy_HPDDM(PC pc)
51: {
52: PC_HPDDM *data = (PC_HPDDM*)pc->data;
56: PCReset_HPDDM(pc);
57: PetscFree(data);
58: PetscObjectChangeTypeName((PetscObject)pc, NULL);
59: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetAuxiliaryMat_C", NULL);
60: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMHasNeumannMat_C", NULL);
61: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetRHSMat_C", NULL);
62: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetCoarseCorrectionType_C", NULL);
63: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetCoarseCorrectionType_C", NULL);
64: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetSTShareSubKSP_C", NULL);
65: return(0);
66: }
68: static PetscErrorCode PCHPDDMSetAuxiliaryMat_HPDDM(PC pc, IS is, Mat A, PetscErrorCode (*setup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void*), void* setup_ctx)
69: {
70: PC_HPDDM *data = (PC_HPDDM*)pc->data;
74: if (is) {
75: PetscObjectReference((PetscObject)is);
76: if (data->is) { /* new overlap definition resets the PC */
77: PCReset_HPDDM(pc);
78: pc->setfromoptionscalled = 0;
79: }
80: ISDestroy(&data->is);
81: data->is = is;
82: }
83: if (A) {
84: PetscObjectReference((PetscObject)A);
85: MatDestroy(&data->aux);
86: data->aux = A;
87: }
88: if (setup) {
89: data->setup = setup;
90: data->setup_ctx = setup_ctx;
91: }
92: return(0);
93: }
95: /*@
96: PCHPDDMSetAuxiliaryMat - Sets the auxiliary matrix used by PCHPDDM for the concurrent GenEO problems 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.
98: Input Parameters:
99: + pc - preconditioner context
100: . is - index set of the local auxiliary, e.g., Neumann, matrix
101: . A - auxiliary sequential matrix
102: . setup - function for generating the auxiliary matrix
103: - setup_ctx - context for setup
105: Level: intermediate
107: .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, PCHPDDMSetRHSMat(), MATIS
108: @*/
109: PetscErrorCode PCHPDDMSetAuxiliaryMat(PC pc, IS is, Mat A, PetscErrorCode (*setup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void*), void* setup_ctx)
110: {
117: #if defined(PETSC_HAVE_FORTRAN)
118: if (reinterpret_cast<void*>(setup) == reinterpret_cast<void*>(PETSC_NULL_FUNCTION_Fortran)) setup = NULL;
119: if (setup_ctx == PETSC_NULL_INTEGER_Fortran) setup_ctx = NULL;
120: #endif
121: PetscTryMethod(pc, "PCHPDDMSetAuxiliaryMat_C", (PC, IS, Mat, PetscErrorCode (*)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void*), void*), (pc, is, A, setup, setup_ctx));
122: return(0);
123: }
125: static PetscErrorCode PCHPDDMHasNeumannMat_HPDDM(PC pc, PetscBool has)
126: {
127: PC_HPDDM *data = (PC_HPDDM*)pc->data;
130: data->Neumann = has;
131: return(0);
132: }
134: /*@
135: 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.
137: Input Parameters:
138: + pc - preconditioner context
139: - has - Boolean value
141: Level: intermediate
143: .seealso: PCHPDDM, PCHPDDMSetAuxiliaryMat()
144: @*/
145: PetscErrorCode PCHPDDMHasNeumannMat(PC pc, PetscBool has)
146: {
151: PetscTryMethod(pc, "PCHPDDMHasNeumannMat_C", (PC, PetscBool), (pc, has));
152: return(0);
153: }
155: static PetscErrorCode PCHPDDMSetRHSMat_HPDDM(PC pc, Mat B)
156: {
157: PC_HPDDM *data = (PC_HPDDM*)pc->data;
161: PetscObjectReference((PetscObject)B);
162: MatDestroy(&data->B);
163: data->B = B;
164: return(0);
165: }
167: /*@
168: PCHPDDMSetRHSMat - Sets the right-hand side matrix used by PCHPDDM for the concurrent GenEO problems 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.
170: Input Parameters:
171: + pc - preconditioner context
172: - B - right-hand side sequential matrix
174: Level: advanced
176: .seealso: PCHPDDMSetAuxiliaryMat(), PCHPDDM
177: @*/
178: PetscErrorCode PCHPDDMSetRHSMat(PC pc, Mat B)
179: {
184: if (B) {
186: PetscTryMethod(pc, "PCHPDDMSetRHSMat_C", (PC, Mat), (pc, B));
187: }
188: return(0);
189: }
191: static PetscErrorCode PCSetFromOptions_HPDDM(PetscOptionItems *PetscOptionsObject, PC pc)
192: {
193: PC_HPDDM *data = (PC_HPDDM*)pc->data;
194: PC_HPDDM_Level **levels = data->levels;
195: char prefix[256];
196: int i = 1;
197: PetscMPIInt size, previous;
198: PetscInt n;
199: PCHPDDMCoarseCorrectionType type;
200: PetscBool flg = PETSC_TRUE;
201: PetscErrorCode ierr;
204: if (!data->levels) {
205: PetscCalloc1(PETSC_HPDDM_MAXLEVELS, &levels);
206: data->levels = levels;
207: }
208: PetscOptionsHead(PetscOptionsObject, "PCHPDDM options");
209: MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size);
210: previous = size;
211: while (i < PETSC_HPDDM_MAXLEVELS) {
212: PetscInt p = 1;
214: if (!data->levels[i - 1]) {
215: PetscNewLog(pc, data->levels + i - 1);
216: }
217: data->levels[i - 1]->parent = data;
218: /* if the previous level has a single process, it is not possible to coarsen further */
219: if (previous == 1 || !flg) break;
220: data->levels[i - 1]->nu = 0;
221: data->levels[i - 1]->threshold = -1.0;
222: PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_nev", i);
223: PetscOptionsInt(prefix, "Local number of deflation vectors computed by SLEPc", "EPSSetDimensions", data->levels[i - 1]->nu, &data->levels[i - 1]->nu, NULL);
224: PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold", i);
225: PetscOptionsReal(prefix, "Local threshold for selecting deflation vectors returned by SLEPc", "PCHPDDM", data->levels[i - 1]->threshold, &data->levels[i - 1]->threshold, NULL);
226: if (i == 1) {
227: PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_1_st_share_sub_ksp");
228: PetscOptionsBool(prefix, "Shared KSP between SLEPc ST and the fine-level subdomain solver", "PCHPDDMGetSTShareSubKSP", PETSC_FALSE, &data->share, NULL);
229: }
230: /* if there is no prescribed coarsening, just break out of the loop */
231: if (data->levels[i - 1]->threshold <= 0.0 && data->levels[i - 1]->nu <= 0) break;
232: else {
233: ++i;
234: PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_nev", i);
235: PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg);
236: if (!flg) {
237: PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold", i);
238: PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg);
239: }
240: if (flg) {
241: /* if there are coarsening options for the next level, then register it */
242: /* otherwise, don't to avoid having both options levels_N_p and coarse_p */
243: PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_p", i);
244: PetscOptionsRangeInt(prefix, "Number of processes used to assemble the coarse operator at this level", "PCHPDDM", p, &p, &flg, 1, PetscMax(1, previous / 2));
245: previous = p;
246: }
247: }
248: }
249: data->N = i;
250: n = 1;
251: if (i > 1) {
252: PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_coarse_p");
253: PetscOptionsRangeInt(prefix, "Number of processes used to assemble the coarsest operator", "PCHPDDM", n, &n, NULL, 1, PetscMax(1, previous / 2));
254: #if defined(PETSC_HAVE_MUMPS)
255: PetscSNPrintf(prefix, sizeof(prefix), "pc_hpddm_coarse_");
256: PetscOptionsHasName(NULL, prefix, "-mat_mumps_use_omp_threads", &flg);
257: if (flg) {
258: char type[64]; /* same size as in src/ksp/pc/impls/factor/factimpl.c */
259: if (n == 1) { /* default solver for a sequential Mat */
260: PetscStrcpy(type, MATSOLVERPETSC);
261: }
262: PetscOptionsGetString(NULL, prefix, "-pc_factor_mat_solver_type", type, sizeof(type), &flg);
263: if (flg) {
264: PetscStrcmp(type, MATSOLVERMUMPS, &flg);
265: }
266: if (!flg) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "-%smat_mumps_use_omp_threads and -%spc_factor_mat_solver_type != %s", prefix, prefix, MATSOLVERMUMPS);
267: size = n;
268: n = -1;
269: PetscOptionsGetInt(NULL, prefix, "-mat_mumps_use_omp_threads", &n, NULL);
270: if (n < 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Need to specify a positive integer for -%smat_mumps_use_omp_threads", prefix);
271: if (n * size > previous) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "%d MPI process%s x %d OpenMP thread%s greater than %d available MPI process%s for the coarsest operator", (int)size, size > 1 ? "es" : "", (int)n, n > 1 ? "s" : "", (int)previous, previous > 1 ? "es" : "");
272: }
273: #endif
274: PetscOptionsEnum("-pc_hpddm_coarse_correction", "Type of coarse correction applied each iteration", "PCHPDDMSetCoarseCorrectionType", PCHPDDMCoarseCorrectionTypes, (PetscEnum)data->correction, (PetscEnum*)&type, &flg);
275: if (flg) {
276: PCHPDDMSetCoarseCorrectionType(pc, type);
277: }
278: PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_has_neumann");
279: PetscOptionsBool(prefix, "Is the auxiliary Mat the local Neumann matrix?", "PCHPDDMHasNeumannMat", data->Neumann, &data->Neumann, NULL);
280: data->log_separate = PETSC_FALSE;
281: if (PetscDefined(USE_LOG)) {
282: PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_log_separate");
283: PetscOptionsBool(prefix, "Log events level by level instead of inside PCSetUp()/KSPSolve()", NULL, data->log_separate, &data->log_separate, NULL);
284: }
285: }
286: PetscOptionsTail();
287: while (i < PETSC_HPDDM_MAXLEVELS && data->levels[i]) {
288: PetscFree(data->levels[i++]);
289: }
290: return(0);
291: }
293: static PetscErrorCode PCApply_HPDDM(PC pc, Vec x, Vec y)
294: {
295: PC_HPDDM *data = (PC_HPDDM*)pc->data;
299: PetscCitationsRegister(HPDDMCitation, &HPDDMCite);
300: if (data->levels[0]->ksp) {
301: if (data->log_separate) { /* coarser-level events are directly triggered in HPDDM */
302: PetscLogEventBegin(PC_HPDDM_Solve[0], data->levels[0]->ksp, 0, 0, 0);
303: }
304: KSPSolve(data->levels[0]->ksp, x, y);
305: if (data->log_separate) {
306: PetscLogEventEnd(PC_HPDDM_Solve[0], data->levels[0]->ksp, 0, 0, 0);
307: }
308: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "No KSP attached to PCHPDDM");
309: return(0);
310: }
312: static PetscErrorCode PCMatApply_HPDDM(PC pc, Mat X, Mat Y)
313: {
314: PC_HPDDM *data = (PC_HPDDM*)pc->data;
318: PetscCitationsRegister(HPDDMCitation, &HPDDMCite);
319: if (data->levels[0]->ksp) {
320: KSPMatSolve(data->levels[0]->ksp, X, Y);
321: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "No KSP attached to PCHPDDM");
322: return(0);
323: }
325: /*@C
326: PCHPDDMGetComplexities - Computes the grid and operator complexities.
328: Input Parameter:
329: . pc - preconditioner context
331: Output Parameters:
332: + gc - grid complexity = sum_i(m_i) / m_1
333: - oc - operator complexity = sum_i(nnz_i) / nnz_1
335: Notes:
336: 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.
338: Level: advanced
340: .seealso: PCMGGetGridComplexity(), PCHPDDM
341: @*/
342: static PetscErrorCode PCHPDDMGetComplexities(PC pc, PetscReal *gc, PetscReal *oc)
343: {
344: PC_HPDDM *data = (PC_HPDDM*)pc->data;
345: MatInfo info;
346: PetscInt n, m;
347: PetscLogDouble accumulate[2] { }, nnz1 = 1.0, m1 = 1.0;
351: for (n = 0, *gc = 0, *oc = 0; n < data->N; ++n) {
352: if (data->levels[n]->ksp) {
353: Mat P;
354: KSPGetOperators(data->levels[n]->ksp, NULL, &P);
355: MatGetSize(P, &m, NULL);
356: accumulate[0] += m;
357: if (P->ops->getinfo) {
358: MatGetInfo(P, MAT_GLOBAL_SUM, &info);
359: accumulate[1] += info.nz_used;
360: }
361: if (n == 0) {
362: m1 = m;
363: if (P->ops->getinfo) nnz1 = info.nz_used;
364: }
365: }
366: }
367: *gc = static_cast<PetscReal>(accumulate[0]/m1);
368: *oc = static_cast<PetscReal>(accumulate[1]/nnz1);
369: return(0);
370: }
372: static PetscErrorCode PCView_HPDDM(PC pc, PetscViewer viewer)
373: {
374: PC_HPDDM *data = (PC_HPDDM*)pc->data;
375: PetscViewer subviewer;
376: PetscSubcomm subcomm;
377: PetscReal oc, gc;
378: PetscInt i, tabs;
379: PetscMPIInt size, color, rank;
380: PetscBool ascii;
384: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &ascii);
385: if (ascii) {
386: PetscViewerASCIIPrintf(viewer, "level%s: %D\n", data->N > 1 ? "s" : "", data->N);
387: PCHPDDMGetComplexities(pc, &gc, &oc);
388: if (data->N > 1) {
389: PetscViewerASCIIPrintf(viewer, "Neumann matrix attached? %s\n", PetscBools[data->Neumann]);
390: PetscViewerASCIIPrintf(viewer, "coarse correction: %s\n", PCHPDDMCoarseCorrectionTypes[data->correction]);
391: PetscViewerASCIIPrintf(viewer, "on process #0, value%s (+ threshold%s if available) for selecting deflation vectors:", data->N > 2 ? "s" : "", data->N > 2 ? "s" : "");
392: PetscViewerASCIIGetTab(viewer, &tabs);
393: PetscViewerASCIISetTab(viewer, 0);
394: for (i = 1; i < data->N; ++i) {
395: PetscViewerASCIIPrintf(viewer, " %D", data->levels[i - 1]->nu);
396: if (data->levels[i - 1]->threshold > -0.1) {
397: PetscViewerASCIIPrintf(viewer, " (%g)", (double)data->levels[i - 1]->threshold);
398: }
399: }
400: PetscViewerASCIIPrintf(viewer, "\n");
401: PetscViewerASCIISetTab(viewer, tabs);
402: }
403: PetscViewerASCIIPrintf(viewer, "grid and operator complexities: %g %g\n", (double)gc, (double)oc);
404: if (data->levels[0]->ksp) {
405: KSPView(data->levels[0]->ksp, viewer);
406: if (data->levels[0]->pc) {
407: PCView(data->levels[0]->pc, viewer);
408: }
409: for (i = 1; i < data->N; ++i) {
410: if (data->levels[i]->ksp) color = 1;
411: else color = 0;
412: MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size);
413: MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank);
414: PetscSubcommCreate(PetscObjectComm((PetscObject)pc), &subcomm);
415: PetscSubcommSetNumber(subcomm, PetscMin(size, 2));
416: PetscSubcommSetTypeGeneral(subcomm, color, rank);
417: PetscViewerASCIIPushTab(viewer);
418: PetscViewerGetSubViewer(viewer, PetscSubcommChild(subcomm), &subviewer);
419: if (color == 1) {
420: KSPView(data->levels[i]->ksp, subviewer);
421: if (data->levels[i]->pc) {
422: PCView(data->levels[i]->pc, subviewer);
423: }
424: PetscViewerFlush(subviewer);
425: }
426: PetscViewerRestoreSubViewer(viewer, PetscSubcommChild(subcomm), &subviewer);
427: PetscViewerASCIIPopTab(viewer);
428: PetscSubcommDestroy(&subcomm);
429: PetscViewerFlush(viewer);
430: }
431: }
432: }
433: return(0);
434: }
436: static PetscErrorCode PCPreSolve_HPDDM(PC pc, KSP ksp, Vec, Vec)
437: {
438: PC_HPDDM *data = (PC_HPDDM*)pc->data;
439: PetscBool flg;
440: Mat A;
444: if (ksp) {
445: PetscObjectTypeCompare((PetscObject)ksp, KSPLSQR, &flg);
446: if (flg && !data->normal) {
447: KSPGetOperators(ksp, &A, NULL);
448: MatCreateVecs(A, NULL, &data->normal); /* temporary Vec used in PCHPDDMShellApply() for coarse grid corrections */
449: }
450: }
451: return(0);
452: }
454: static PetscErrorCode PCHPDDMShellSetUp(PC pc)
455: {
456: PC_HPDDM_Level *ctx;
457: Mat A, P;
458: Vec x;
459: const char *pcpre;
463: PCShellGetContext(pc, &ctx);
464: KSPGetOptionsPrefix(ctx->ksp, &pcpre);
465: KSPGetOperators(ctx->ksp, &A, &P);
466: /* smoother */
467: PCSetOptionsPrefix(ctx->pc, pcpre);
468: PCSetOperators(ctx->pc, A, P);
469: if (!ctx->v[0]) {
470: VecDuplicateVecs(ctx->D, 1, &ctx->v[0]);
471: if (!std::is_same<PetscScalar, PetscReal>::value) {
472: VecDestroy(&ctx->D);
473: }
474: MatCreateVecs(A, &x, NULL);
475: VecDuplicateVecs(x, 2, &ctx->v[1]);
476: VecDestroy(&x);
477: }
478: std::fill_n(ctx->V, 3, nullptr);
479: return(0);
480: }
482: template<class Type, typename std::enable_if<std::is_same<Type, Vec>::value>::type* = nullptr>
483: PETSC_STATIC_INLINE PetscErrorCode PCHPDDMDeflate_Private(PC pc, Type x, Type y)
484: {
485: PC_HPDDM_Level *ctx;
486: PetscScalar *out;
490: PCShellGetContext(pc, &ctx);
491: /* going from PETSc to HPDDM numbering */
492: VecScatterBegin(ctx->scatter, x, ctx->v[0][0], INSERT_VALUES, SCATTER_FORWARD);
493: VecScatterEnd(ctx->scatter, x, ctx->v[0][0], INSERT_VALUES, SCATTER_FORWARD);
494: VecGetArrayWrite(ctx->v[0][0], &out);
495: ctx->P->deflation<false>(NULL, out, 1); /* y = Q x */
496: VecRestoreArrayWrite(ctx->v[0][0], &out);
497: /* going from HPDDM to PETSc numbering */
498: VecScatterBegin(ctx->scatter, ctx->v[0][0], y, INSERT_VALUES, SCATTER_REVERSE);
499: VecScatterEnd(ctx->scatter, ctx->v[0][0], y, INSERT_VALUES, SCATTER_REVERSE);
500: return(0);
501: }
503: template<class Type, typename std::enable_if<std::is_same<Type, Mat>::value>::type* = nullptr>
504: PETSC_STATIC_INLINE PetscErrorCode PCHPDDMDeflate_Private(PC pc, Type X, Type Y)
505: {
506: PC_HPDDM_Level *ctx;
507: Vec vX, vY, vC;
508: PetscScalar *out;
509: PetscInt i, N;
513: PCShellGetContext(pc, &ctx);
514: MatGetSize(X, NULL, &N);
515: /* going from PETSc to HPDDM numbering */
516: for (i = 0; i < N; ++i) {
517: MatDenseGetColumnVecRead(X, i, &vX);
518: MatDenseGetColumnVecWrite(ctx->V[0], i, &vC);
519: VecScatterBegin(ctx->scatter, vX, vC, INSERT_VALUES, SCATTER_FORWARD);
520: VecScatterEnd(ctx->scatter, vX, vC, INSERT_VALUES, SCATTER_FORWARD);
521: MatDenseRestoreColumnVecWrite(ctx->V[0], i, &vC);
522: MatDenseRestoreColumnVecRead(X, i, &vX);
523: }
524: MatDenseGetArrayWrite(ctx->V[0], &out);
525: ctx->P->deflation<false>(NULL, out, N); /* Y = Q X */
526: MatDenseRestoreArrayWrite(ctx->V[0], &out);
527: /* going from HPDDM to PETSc numbering */
528: for (i = 0; i < N; ++i) {
529: MatDenseGetColumnVecRead(ctx->V[0], i, &vC);
530: MatDenseGetColumnVecWrite(Y, i, &vY);
531: VecScatterBegin(ctx->scatter, vC, vY, INSERT_VALUES, SCATTER_REVERSE);
532: VecScatterEnd(ctx->scatter, vC, vY, INSERT_VALUES, SCATTER_REVERSE);
533: MatDenseRestoreColumnVecWrite(Y, i, &vY);
534: MatDenseRestoreColumnVecRead(ctx->V[0], i, &vC);
535: }
536: return(0);
537: }
539: /*@C
540: 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.
542: .vb
543: (1) y = Pmat^-1 x + Q x,
544: (2) y = Pmat^-1 (I - Amat Q) x + Q x (default),
545: (3) y = (I - Q Amat^T) Pmat^-1 (I - Amat Q) x + Q x.
546: .ve
548: Input Parameters:
549: + pc - preconditioner context
550: - x - input vector
552: Output Parameter:
553: . y - output vector
555: Application Interface Routine: PCApply()
557: Notes:
558: 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.
559: 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.
560: (1) and (2) visit the "next" level (in terms of coarsening) once per application, while (3) visits it twice, so it is asymptotically twice costlier. (2) is not symmetric even if both Amat and Pmat are symmetric.
562: Level: advanced
564: .seealso: PCHPDDM, PCHPDDMCoarseCorrectionType
565: @*/
566: static PetscErrorCode PCHPDDMShellApply(PC pc, Vec x, Vec y)
567: {
568: PC_HPDDM_Level *ctx;
569: Mat A;
573: PCShellGetContext(pc, &ctx);
574: if (ctx->P) {
575: KSPGetOperators(ctx->ksp, &A, NULL);
576: PCHPDDMDeflate_Private(pc, x, y); /* y = Q x */
577: if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
578: if (!ctx->parent->normal || ctx != ctx->parent->levels[0]) {
579: MatMult(A, y, ctx->v[1][0]); /* y = A Q x */
580: } else { /* KSPLSQR and finest level */
581: MatMult(A, y, ctx->parent->normal); /* y = A Q x */
582: MatMultTranspose(A, ctx->parent->normal, ctx->v[1][0]); /* y = A^T A Q x */
583: }
584: VecWAXPY(ctx->v[1][1], -1.0, ctx->v[1][0], x); /* y = (I - A Q) x */
585: PCApply(ctx->pc, ctx->v[1][1], ctx->v[1][0]); /* y = M^-1 (I - A Q) x */
586: if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
587: if (!ctx->parent->normal || ctx != ctx->parent->levels[0]) {
588: MatMultTranspose(A, ctx->v[1][0], ctx->v[1][1]); /* z = A^T y */
589: } else {
590: MatMult(A, ctx->v[1][0], ctx->parent->normal);
591: MatMultTranspose(A, ctx->parent->normal, ctx->v[1][1]); /* z = A^T A y */
592: }
593: PCHPDDMDeflate_Private(pc, ctx->v[1][1], ctx->v[1][1]);
594: VecAXPBYPCZ(y, -1.0, 1.0, 1.0, ctx->v[1][1], ctx->v[1][0]); /* y = (I - Q A^T) y + Q x */
595: } else {
596: VecAXPY(y, 1.0, ctx->v[1][0]); /* y = Q M^-1 (I - A Q) x + Q x */
597: }
598: } else if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_ADDITIVE) {
599: PCApply(ctx->pc, x, ctx->v[1][0]);
600: VecAXPY(y, 1.0, ctx->v[1][0]); /* y = M^-1 x + Q x */
601: } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with an unknown PCHPDDMCoarseCorrectionType %d", ctx->parent->correction);
602: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object");
603: return(0);
604: }
606: /*@C
607: PCHPDDMShellMatApply - Variant of PCHPDDMShellApply() for blocks of vectors.
609: Input Parameters:
610: + pc - preconditioner context
611: - X - block of input vectors
613: Output Parameter:
614: . Y - block of output vectors
616: Application Interface Routine: PCApply()
618: Level: advanced
620: .seealso: PCHPDDM, PCHPDDMShellMatApply(), PCHPDDMCoarseCorrectionType
621: @*/
622: static PetscErrorCode PCHPDDMShellMatApply(PC pc, Mat X, Mat Y)
623: {
624: PC_HPDDM_Level *ctx;
625: Mat A, *ptr;
626: PetscContainer container = NULL;
627: PetscScalar *array;
628: PetscInt m, M, N, prev = 0;
629: PetscBool reset = PETSC_FALSE;
633: PCShellGetContext(pc, &ctx);
634: if (ctx->P) {
635: MatGetSize(X, NULL, &N);
636: KSPGetOperators(ctx->ksp, &A, NULL);
637: PetscObjectQuery((PetscObject)A, "_HPDDM_MatProduct", (PetscObject*)&container);
638: if (container) { /* MatProduct container already attached */
639: PetscContainerGetPointer(container, (void**)&ptr);
640: if (ptr[1] != ctx->V[2]) /* Mat has changed or may have been set first in KSPHPDDM */
641: for (m = 0; m < 2; ++m) {
642: MatDestroy(ctx->V + m + 1);
643: ctx->V[m + 1] = ptr[m];
644: PetscObjectReference((PetscObject)ctx->V[m + 1]);
645: }
646: }
647: if (ctx->V[1]) {
648: MatGetSize(ctx->V[1], NULL, &prev);
649: }
650: if (N != prev || !ctx->V[0]) {
651: MatDestroy(ctx->V);
652: VecGetLocalSize(ctx->v[0][0], &m);
653: MatCreateDense(PetscObjectComm((PetscObject)pc), m, PETSC_DECIDE, PETSC_DECIDE, N, NULL, ctx->V);
654: if (N != prev) {
655: MatDestroy(ctx->V + 1);
656: MatDestroy(ctx->V + 2);
657: MatGetLocalSize(X, &m, NULL);
658: MatGetSize(X, &M, NULL);
659: if (ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) {
660: MatDenseGetArrayWrite(ctx->V[0], &array);
661: } else array = NULL;
662: MatCreateDense(PetscObjectComm((PetscObject)pc), m, PETSC_DECIDE, M, N, array, ctx->V + 1);
663: if (ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) {
664: MatDenseRestoreArrayWrite(ctx->V[0], &array);
665: } else {
666: MatAssemblyBegin(ctx->V[1], MAT_FINAL_ASSEMBLY);
667: MatAssemblyEnd(ctx->V[1], MAT_FINAL_ASSEMBLY);
668: }
669: MatCreateDense(PetscObjectComm((PetscObject)pc), m, PETSC_DECIDE, M, N, NULL, ctx->V + 2);
670: MatAssemblyBegin(ctx->V[2], MAT_FINAL_ASSEMBLY);
671: MatAssemblyEnd(ctx->V[2], MAT_FINAL_ASSEMBLY);
672: MatProductCreateWithMat(A, Y, NULL, ctx->V[1]);
673: MatProductSetType(ctx->V[1], MATPRODUCT_AB);
674: MatProductSetFromOptions(ctx->V[1]);
675: MatProductSymbolic(ctx->V[1]);
676: if (!container) { /* no MatProduct container attached, create one to be queried in KSPHPDDM or at the next call to PCMatApply() */
677: PetscContainerCreate(PetscObjectComm((PetscObject)A), &container);
678: PetscObjectCompose((PetscObject)A, "_HPDDM_MatProduct", (PetscObject)container);
679: }
680: PetscContainerSetPointer(container, ctx->V + 1); /* need to compose B and D from MatProductCreateWithMath(A, B, NULL, D), which are stored in the contiguous array ctx->V */
681: }
682: if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
683: MatProductCreateWithMat(A, ctx->V[1], NULL, ctx->V[2]);
684: MatProductSetType(ctx->V[2], MATPRODUCT_AtB);
685: MatProductSetFromOptions(ctx->V[2]);
686: MatProductSymbolic(ctx->V[2]);
687: }
688: ctx->P->start(N);
689: }
690: if (N == prev || container) { /* when MatProduct container is attached, always need to MatProductReplaceMats() since KSPHPDDM may have replaced the Mat as well */
691: MatProductReplaceMats(NULL, Y, NULL, ctx->V[1]);
692: if (container && ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) {
693: MatDenseGetArrayWrite(ctx->V[0], &array);
694: MatDensePlaceArray(ctx->V[1], array);
695: MatDenseRestoreArrayWrite(ctx->V[0], &array);
696: reset = PETSC_TRUE;
697: }
698: }
699: PCHPDDMDeflate_Private(pc, X, Y);
700: if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
701: MatProductNumeric(ctx->V[1]);
702: MatCopy(ctx->V[1], ctx->V[2], SAME_NONZERO_PATTERN);
703: MatAXPY(ctx->V[2], -1.0, X, SAME_NONZERO_PATTERN);
704: PCMatApply(ctx->pc, ctx->V[2], ctx->V[1]);
705: if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
706: MatProductNumeric(ctx->V[2]);
707: PCHPDDMDeflate_Private(pc, ctx->V[2], ctx->V[2]);
708: MatAXPY(ctx->V[1], -1.0, ctx->V[2], SAME_NONZERO_PATTERN);
709: }
710: MatAXPY(Y, -1.0, ctx->V[1], SAME_NONZERO_PATTERN);
711: } else if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_ADDITIVE) {
712: PCMatApply(ctx->pc, X, ctx->V[1]);
713: MatAXPY(Y, 1.0, ctx->V[1], SAME_NONZERO_PATTERN);
714: } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with an unknown PCHPDDMCoarseCorrectionType %d", ctx->parent->correction);
715: if (reset) {
716: MatDenseResetArray(ctx->V[1]);
717: }
718: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object");
719: return(0);
720: }
722: static PetscErrorCode PCHPDDMShellDestroy(PC pc)
723: {
724: PC_HPDDM_Level *ctx;
725: PetscContainer container;
729: PCShellGetContext(pc, &ctx);
730: HPDDM::Schwarz<PetscScalar>::destroy(ctx, PETSC_TRUE);
731: VecDestroyVecs(1, &ctx->v[0]);
732: VecDestroyVecs(2, &ctx->v[1]);
733: PetscObjectQuery((PetscObject)(ctx->pc)->mat, "_HPDDM_MatProduct", (PetscObject*)&container);
734: PetscContainerDestroy(&container);
735: PetscObjectCompose((PetscObject)(ctx->pc)->mat, "_HPDDM_MatProduct", NULL);
736: MatDestroy(ctx->V);
737: MatDestroy(ctx->V + 1);
738: MatDestroy(ctx->V + 2);
739: VecDestroy(&ctx->D);
740: VecScatterDestroy(&ctx->scatter);
741: PCDestroy(&ctx->pc);
742: return(0);
743: }
745: static PetscErrorCode PCHPDDMSolve_Private(const PC_HPDDM_Level *ctx, PetscScalar *rhs, const unsigned short& mu)
746: {
747: Mat B, X;
748: PetscInt n, N, j = 0;
752: KSPGetOperators(ctx->ksp, &B, NULL);
753: MatGetLocalSize(B, &n, NULL);
754: MatGetSize(B, &N, NULL);
755: if (ctx->parent->log_separate) {
756: j = std::distance(ctx->parent->levels, std::find(ctx->parent->levels, ctx->parent->levels + ctx->parent->N, ctx));
757: PetscLogEventBegin(PC_HPDDM_Solve[j], ctx->ksp, 0, 0, 0);
758: }
759: if (mu == 1) {
760: if (!ctx->ksp->vec_rhs) {
761: VecCreateMPIWithArray(PetscObjectComm((PetscObject)ctx->ksp), 1, n, N, NULL, &ctx->ksp->vec_rhs);
762: VecCreateMPI(PetscObjectComm((PetscObject)ctx->ksp), n, N, &ctx->ksp->vec_sol);
763: }
764: VecPlaceArray(ctx->ksp->vec_rhs, rhs);
765: KSPSolve(ctx->ksp, NULL, NULL);
766: VecCopy(ctx->ksp->vec_sol, ctx->ksp->vec_rhs);
767: VecResetArray(ctx->ksp->vec_rhs);
768: } else {
769: MatCreateDense(PetscObjectComm((PetscObject)ctx->ksp), n, PETSC_DECIDE, N, mu, rhs, &B);
770: MatCreateDense(PetscObjectComm((PetscObject)ctx->ksp), n, PETSC_DECIDE, N, mu, NULL, &X);
771: KSPMatSolve(ctx->ksp, B, X);
772: MatCopy(X, B, SAME_NONZERO_PATTERN);
773: MatDestroy(&X);
774: MatDestroy(&B);
775: }
776: if (ctx->parent->log_separate) {
777: PetscLogEventEnd(PC_HPDDM_Solve[j], ctx->ksp, 0, 0, 0);
778: }
779: return(0);
780: }
782: static PetscErrorCode PCHPDDMSetUpNeumannOverlap_Private(PC pc)
783: {
784: PC_HPDDM *data = (PC_HPDDM*)pc->data;
788: if (data->setup) {
789: Mat P;
790: Vec x, xt = NULL;
791: PetscReal t = 0.0, s = 0.0;
793: PCGetOperators(pc, NULL, &P);
794: PetscObjectQuery((PetscObject)P, "__SNES_latest_X", (PetscObject*)&x);
795: PetscStackPush("PCHPDDM Neumann callback");
796: (*data->setup)(data->aux, t, x, xt, s, data->is, data->setup_ctx);
797: PetscStackPop;
798: }
799: return(0);
800: }
802: static PetscErrorCode PCHPDDMCreateSubMatrices_Private(Mat mat, PetscInt n, const IS*, const IS*, MatReuse scall, Mat *submat[])
803: {
804: Mat A;
808: if (n != 1) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatCreateSubMatrices() called to extract %D submatrices, which is different than 1", n);
809: /* previously composed Mat */
810: PetscObjectQuery((PetscObject)mat, "_PCHPDDM_SubMatrices", (PetscObject*)&A);
811: if (!A) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "SubMatrices not found in Mat");
812: if (scall == MAT_INITIAL_MATRIX) {
813: PetscCalloc1(1, submat);
814: MatDuplicate(A, MAT_COPY_VALUES, *submat);
815: } else {
816: MatCopy(A, (*submat)[0], SAME_NONZERO_PATTERN);
817: }
818: return(0);
819: }
821: static PetscErrorCode PCHPDDMPermute_Private(IS is, IS in_is, IS *out_is, Mat in_C, Mat *out_C)
822: {
823: IS perm;
824: const PetscInt *ptr;
825: PetscInt *concatenate, size, n;
826: std::map<PetscInt, PetscInt> order;
827: PetscBool sorted;
828: PetscErrorCode ierr;
831: ISSorted(is, &sorted);
832: if (!sorted) {
833: ISGetLocalSize(is, &size);
834: ISGetIndices(is, &ptr);
835: /* MatCreateSubMatrices(), called by PCASM, follows the global numbering of Pmat */
836: for (n = 0; n < size; ++n)
837: order.insert(std::make_pair(ptr[n], n));
838: ISRestoreIndices(is, &ptr);
839: if (out_C) {
840: PetscMalloc1(size, &concatenate);
841: for (const std::pair<const PetscInt, PetscInt>& i : order)
842: *concatenate++ = i.second;
843: concatenate -= size;
844: ISCreateGeneral(PetscObjectComm((PetscObject)in_C), size, concatenate, PETSC_OWN_POINTER, &perm);
845: ISSetPermutation(perm);
846: /* permute user-provided Mat so that it matches with MatCreateSubMatrices() numbering */
847: MatPermute(in_C, perm, perm, out_C);
848: ISDestroy(&perm);
849: }
850: if (out_is) {
851: PetscMalloc1(size, &concatenate);
852: for (const std::pair<const PetscInt, PetscInt>& i : order)
853: *concatenate++ = i.first;
854: concatenate -= size;
855: /* permute user-provided IS so that it matches with MatCreateSubMatrices() numbering */
856: ISCreateGeneral(PetscObjectComm((PetscObject)in_is), size, concatenate, PETSC_OWN_POINTER, out_is);
857: }
858: } else { /* input IS is sorted, nothing to permute, simply duplicate inputs when needed */
859: if (out_C) {
860: MatDuplicate(in_C, MAT_COPY_VALUES, out_C);
861: }
862: if (out_is) {
863: ISDuplicate(in_is, out_is);
864: }
865: }
866: return(0);
867: }
869: static PetscErrorCode PCHPDDMDestroySubMatrices_Private(PetscBool flg, PetscBool algebraic, Mat *sub)
870: {
871: IS is;
875: if (!flg) {
876: if (algebraic) {
877: PetscObjectQuery((PetscObject)sub[0], "_PCHPDDM_Embed", (PetscObject*)&is);
878: ISDestroy(&is);
879: PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Embed", NULL);
880: PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Compact", NULL);
881: }
882: MatDestroySubMatrices(algebraic ? 2 : 1, &sub);
883: }
884: return(0);
885: }
887: static PetscErrorCode PCHPDDMAlgebraicAuxiliaryMat_Private(Mat P, IS *is, Mat *sub[])
888: {
889: IS icol[3], irow[2];
890: Mat *M, Q = NULL;
891: PetscReal *ptr;
892: PetscInt *idx, p = 0, n;
893: PetscBool flg;
897: ISCreateStride(PETSC_COMM_SELF, P->cmap->N, 0, 1, icol + 2);
898: ISSetIdentity(icol[2]);
899: PetscObjectTypeCompare((PetscObject)P, MATMPISBAIJ, &flg);
900: if (flg) {
901: /* MatCreateSubMatrices() does not handle MATMPISBAIJ properly when iscol != isrow, so convert first to MATMPIBAIJ */
902: MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &Q);
903: std::swap(P, Q);
904: }
905: MatCreateSubMatrices(P, 1, is, icol + 2, MAT_INITIAL_MATRIX, &M);
906: if (flg) {
907: std::swap(P, Q);
908: MatDestroy(&Q);
909: }
910: ISDestroy(icol + 2);
911: PetscMalloc2(P->cmap->N, &ptr, P->cmap->N, &idx);
912: MatGetColumnNorms(M[0], NORM_INFINITY, ptr);
913: /* check for nonzero columns so that M[0] may be expressed in compact form */
914: for (n = 0; n < P->cmap->N; n += P->cmap->bs)
915: if (std::find_if(ptr + n, ptr + n + P->cmap->bs, [](PetscReal v) { return v > PETSC_MACHINE_EPSILON; }) != ptr + n + P->cmap->bs) {
916: std::iota(idx + p, idx + p + P->cmap->bs, n);
917: p += P->cmap->bs;
918: }
919: ISCreateGeneral(PETSC_COMM_SELF, p, idx, PETSC_USE_POINTER, icol + 1);
920: ISSetBlockSize(icol[1], P->cmap->bs);
921: ISSetInfo(icol[1], IS_SORTED, IS_GLOBAL, PETSC_TRUE, PETSC_TRUE);
922: ISEmbed(*is, icol[1], PETSC_FALSE, icol + 2);
923: ISCreateStride(PETSC_COMM_SELF, M[0]->rmap->N, 0, 1, irow);
924: ISSetInfo(irow[0], IS_SORTED, IS_GLOBAL, PETSC_TRUE, PETSC_TRUE);
925: irow[1] = irow[0];
926: /* first Mat will be used in PCASM (if it is used as a PC on this level) and as the left-hand side of GenEO */
927: icol[0] = is[0];
928: MatCreateSubMatrices(M[0], 2, irow, icol, MAT_INITIAL_MATRIX, sub);
929: ISDestroy(irow);
930: ISDestroy(icol + 1);
931: MatDestroySubMatrices(1, &M);
932: PetscFree2(ptr, idx);
933: /* IS used to go back and forth between the augmented and the original local linear system, see eq. (3.4) of [2021c] */
934: PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Embed", (PetscObject)icol[2]);
935: /* Mat used in eq. (3.1) of [2021c] */
936: PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Compact", (PetscObject)(*sub)[1]);
937: return(0);
938: }
940: static PetscErrorCode PCSetUp_HPDDM(PC pc)
941: {
942: PC_HPDDM *data = (PC_HPDDM*)pc->data;
943: PC inner;
944: KSP *ksp;
945: Mat *sub, A, P, N, C = NULL, uaux = NULL, weighted, subA[2];
946: Vec xin, v;
947: std::vector<Vec> initial;
948: IS is[1], loc, uis = data->is;
949: ISLocalToGlobalMapping l2g;
950: char prefix[256];
951: const char *pcpre;
952: const PetscScalar *const *ev;
953: PetscInt n, requested = data->N, reused = 0;
954: PetscBool subdomains = PETSC_FALSE, flg = PETSC_FALSE, ismatis, swap = PETSC_FALSE, algebraic = PETSC_FALSE;
955: DM dm;
956: PetscErrorCode ierr;
959: if (!data->levels || !data->levels[0]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not a single level allocated");
960: PCGetOptionsPrefix(pc, &pcpre);
961: PCGetOperators(pc, &A, &P);
962: if (!data->levels[0]->ksp) {
963: KSPCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->ksp);
964: PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_%s_", pcpre ? pcpre : "", data->N > 1 ? "levels_1" : "coarse");
965: KSPSetOptionsPrefix(data->levels[0]->ksp, prefix);
966: KSPSetType(data->levels[0]->ksp, KSPPREONLY);
967: } else if (data->levels[0]->ksp->pc && data->levels[0]->ksp->pc->setupcalled == 1 && data->levels[0]->ksp->pc->reusepreconditioner) {
968: /* if the fine-level PCSHELL exists, its setup has succeeded, and one wants to reuse it, */
969: /* then just propagate the appropriate flag to the coarser levels */
970: for (n = 0; n < PETSC_HPDDM_MAXLEVELS && data->levels[n]; ++n) {
971: /* the following KSP and PC may be NULL for some processes, hence the check */
972: if (data->levels[n]->ksp) {
973: KSPSetReusePreconditioner(data->levels[n]->ksp, PETSC_TRUE);
974: }
975: if (data->levels[n]->pc) {
976: PCSetReusePreconditioner(data->levels[n]->pc, PETSC_TRUE);
977: }
978: }
979: /* early bail out because there is nothing to do */
980: return(0);
981: } else {
982: /* reset coarser levels */
983: for (n = 1; n < PETSC_HPDDM_MAXLEVELS && data->levels[n]; ++n) {
984: 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) {
985: reused = data->N - n;
986: break;
987: }
988: KSPDestroy(&data->levels[n]->ksp);
989: PCDestroy(&data->levels[n]->pc);
990: }
991: /* check if some coarser levels are being reused */
992: MPIU_Allreduce(MPI_IN_PLACE, &reused, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)pc));
993: const int *addr = data->levels[0]->P ? data->levels[0]->P->getAddrLocal() : &HPDDM::i__0;
995: if (addr != &HPDDM::i__0 && reused != data->N - 1) {
996: /* reuse previously computed eigenvectors */
997: ev = data->levels[0]->P->getVectors();
998: if (ev) {
999: initial.reserve(*addr);
1000: VecCreateSeqWithArray(PETSC_COMM_SELF, 1, data->levels[0]->P->getDof(), ev[0], &xin);
1001: for (n = 0; n < *addr; ++n) {
1002: VecDuplicate(xin, &v);
1003: VecPlaceArray(xin, ev[n]);
1004: VecCopy(xin, v);
1005: initial.emplace_back(v);
1006: VecResetArray(xin);
1007: }
1008: VecDestroy(&xin);
1009: }
1010: }
1011: }
1012: data->N -= reused;
1013: KSPSetOperators(data->levels[0]->ksp, A, P);
1015: PetscObjectTypeCompare((PetscObject)P, MATIS, &ismatis);
1016: if (!data->is && !ismatis) {
1017: PetscErrorCode (*create)(DM, IS*, Mat*, PetscErrorCode (**)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void*), void**) = NULL;
1018: PetscErrorCode (*usetup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void*) = NULL;
1019: void *uctx = NULL;
1021: /* first see if we can get the data from the DM */
1022: MatGetDM(P, &dm);
1023: if (!dm) {
1024: MatGetDM(A, &dm);
1025: }
1026: if (!dm) {
1027: PCGetDM(pc, &dm);
1028: }
1029: if (dm) { /* this is the hook for DMPLEX and DMDA for which the auxiliary Mat is the local Neumann matrix */
1030: PetscObjectQueryFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", &create);
1031: if (create) {
1032: (*create)(dm, &uis, &uaux, &usetup, &uctx);
1033: data->Neumann = PETSC_TRUE;
1034: }
1035: }
1036: if (!create) {
1037: if (!uis) {
1038: PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_IS", (PetscObject*)&uis);
1039: PetscObjectReference((PetscObject)uis);
1040: }
1041: if (!uaux) {
1042: PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_Mat", (PetscObject*)&uaux);
1043: PetscObjectReference((PetscObject)uaux);
1044: }
1045: /* look inside the Pmat instead of the PC, needed for MatSchurComplementComputeExplicitOperator() */
1046: if (!uis) {
1047: PetscObjectQuery((PetscObject)P, "_PCHPDDM_Neumann_IS", (PetscObject*)&uis);
1048: PetscObjectReference((PetscObject)uis);
1049: }
1050: if (!uaux) {
1051: PetscObjectQuery((PetscObject)P, "_PCHPDDM_Neumann_Mat", (PetscObject*)&uaux);
1052: PetscObjectReference((PetscObject)uaux);
1053: }
1054: }
1055: PCHPDDMSetAuxiliaryMat(pc, uis, uaux, usetup, uctx);
1056: MatDestroy(&uaux);
1057: ISDestroy(&uis);
1058: }
1060: if (!ismatis) {
1061: PCHPDDMSetUpNeumannOverlap_Private(pc);
1062: if (!data->is && data->N > 1) {
1063: char type[256]; /* same size as in src/ksp/pc/interface/pcset.c */
1064: PetscOptionsGetString(NULL, pcpre, "-pc_hpddm_levels_1_st_pc_type", type, sizeof(type), NULL);
1065: PetscStrcmp(type, PCMAT, &algebraic);
1066: if (algebraic) {
1067: ISCreateStride(PETSC_COMM_SELF, P->rmap->n, P->rmap->rstart, 1, &data->is);
1068: MatIncreaseOverlap(P, 1, &data->is, 1);
1069: ISSort(data->is);
1070: ISSetInfo(data->is, IS_SORTED, IS_GLOBAL, PETSC_TRUE, PETSC_TRUE);
1071: } else {
1072: PetscInfo1(pc, "Cannot assemble a fully-algebraic coarse operator with an assembled Pmat and -%spc_hpddm_levels_1_st_pc_type != mat\n", pcpre ? pcpre : "");
1073: }
1074: }
1075: }
1077: if (data->is || (ismatis && data->N > 1)) {
1078: if (ismatis) {
1079: std::initializer_list<std::string> list = { MATSEQBAIJ, MATSEQSBAIJ };
1080: MatISGetLocalMat(P, &N);
1081: std::initializer_list<std::string>::const_iterator it = std::find(list.begin(), list.end(), ((PetscObject)N)->type_name);
1082: MatISRestoreLocalMat(P, &N);
1083: switch (std::distance(list.begin(), it)) {
1084: case 0:
1085: MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &C);
1086: break;
1087: case 1:
1088: /* MatCreateSubMatrices() does not work with MATSBAIJ and unsorted ISes, so convert to MPIBAIJ */
1089: MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &C);
1090: MatSetOption(C, MAT_SYMMETRIC, PETSC_TRUE);
1091: break;
1092: default:
1093: MatConvert(P, MATMPIAIJ, MAT_INITIAL_MATRIX, &C);
1094: }
1095: MatGetLocalToGlobalMapping(P, &l2g, NULL);
1096: PetscObjectReference((PetscObject)P);
1097: KSPSetOperators(data->levels[0]->ksp, A, C);
1098: std::swap(C, P);
1099: ISLocalToGlobalMappingGetSize(l2g, &n);
1100: ISCreateStride(PETSC_COMM_SELF, n, 0, 1, &loc);
1101: ISLocalToGlobalMappingApplyIS(l2g, loc, &is[0]);
1102: ISDestroy(&loc);
1103: /* the auxiliary Mat is _not_ the local Neumann matrix */
1104: /* it is the local Neumann matrix augmented (with zeros) through MatIncreaseOverlap() */
1105: data->Neumann = PETSC_FALSE;
1106: } else {
1107: is[0] = data->is;
1108: PetscOptionsGetBool(NULL, pcpre, "-pc_hpddm_define_subdomains", &subdomains, NULL);
1109: PetscOptionsGetBool(NULL, pcpre, "-pc_hpddm_has_neumann", &data->Neumann, NULL);
1110: if (algebraic && data->Neumann) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "-pc_hpddm_levels_1_st_pc_type mat and -pc_hpddm_has_neumann");
1111: ISCreateStride(PetscObjectComm((PetscObject)data->is), P->rmap->n, P->rmap->rstart, 1, &loc);
1112: }
1113: if (data->N > 1 && (data->aux || ismatis || algebraic)) {
1114: if (!loadedSym) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "HPDDM library not loaded, cannot use more than one level");
1115: MatSetOption(P, MAT_SUBMAT_SINGLEIS, PETSC_TRUE);
1116: if (ismatis) {
1117: /* needed by HPDDM (currently) so that the partition of unity is 0 on subdomain interfaces */
1118: MatIncreaseOverlap(P, 1, is, 1);
1119: ISDestroy(&data->is);
1120: data->is = is[0];
1121: } else {
1122: if (PetscDefined(USE_DEBUG)) {
1123: PetscBool equal;
1124: IS intersect;
1126: ISIntersect(data->is, loc, &intersect);
1127: ISEqualUnsorted(loc, intersect, &equal);
1128: ISDestroy(&intersect);
1129: if (!equal) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "IS of the auxiliary Mat does not include all local rows of A");
1130: }
1131: PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", PCHPDDMAlgebraicAuxiliaryMat_Private);
1132: if (!data->Neumann && !algebraic) {
1133: PetscObjectTypeCompare((PetscObject)P, MATMPISBAIJ, &flg);
1134: if (flg) {
1135: /* maybe better to ISSort(is[0]), MatCreateSubMatrices(), and then MatPermute() */
1136: /* but there is no MatPermute_SeqSBAIJ(), so as before, just use MATMPIBAIJ */
1137: MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &uaux);
1138: flg = PETSC_FALSE;
1139: }
1140: }
1141: }
1142: if (algebraic) {
1143: PetscUseMethod(pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", (Mat, IS*, Mat*[]), (P, is, &sub));
1144: } else if (!uaux) {
1145: if (data->Neumann) sub = &data->aux;
1146: else {
1147: MatCreateSubMatrices(P, 1, is, is, MAT_INITIAL_MATRIX, &sub);
1148: }
1149: } else {
1150: MatCreateSubMatrices(uaux, 1, is, is, MAT_INITIAL_MATRIX, &sub);
1151: MatDestroy(&uaux);
1152: MatConvert(sub[0], MATSEQSBAIJ, MAT_INPLACE_MATRIX, sub);
1153: }
1154: /* Vec holding the partition of unity */
1155: if (!data->levels[0]->D) {
1156: ISGetLocalSize(data->is, &n);
1157: VecCreateMPI(PETSC_COMM_SELF, n, PETSC_DETERMINE, &data->levels[0]->D);
1158: }
1159: /* it is possible to share the PC only given specific conditions, otherwise there is not warranty that the matrices have the same nonzero pattern */
1160: if (!ismatis && sub == &data->aux && !data->B && subdomains && data->share) {
1161: PetscInt size = -1;
1162: PCHPDDMPermute_Private(*is, data->is, &uis, data->aux, &C);
1163: if (!data->levels[0]->pc) {
1164: PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_", pcpre ? pcpre : "");
1165: PCCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->pc);
1166: PCSetOptionsPrefix(data->levels[0]->pc, prefix);
1167: PCSetOperators(data->levels[0]->pc, A, P);
1168: }
1169: PCSetType(data->levels[0]->pc, PCASM);
1170: if (!data->levels[0]->pc->setupcalled) {
1171: PCASMSetLocalSubdomains(data->levels[0]->pc, 1, is, &loc);
1172: }
1173: PCSetFromOptions(data->levels[0]->pc);
1174: PCSetUp(data->levels[0]->pc);
1175: PetscTryMethod(data->levels[0]->pc, "PCASMGetSubKSP_C", (PC, PetscInt*, PetscInt*, KSP**), (data->levels[0]->pc, &size, NULL, &ksp));
1176: if (size != 1) {
1177: PCDestroy(&data->levels[0]->pc);
1178: MatDestroy(&C);
1179: ISDestroy(&uis);
1180: data->share = PETSC_FALSE;
1181: if (size == -1) {
1182: PetscInfo(pc, "Cannot share PC between ST and subdomain solver since PCASMGetSubKSP() not found in fine-level PC\n");
1183: } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of subdomain solver %D != 1", size);
1184: } else {
1185: Mat D;
1186: const char *matpre;
1187: PetscBool cmp[2];
1188: KSPGetOperators(ksp[0], subA, subA + 1);
1189: MatDuplicate(subA[1], MAT_SHARE_NONZERO_PATTERN, &D);
1190: MatGetOptionsPrefix(subA[1], &matpre);
1191: MatSetOptionsPrefix(D, matpre);
1192: PetscObjectTypeCompare((PetscObject)D, MATNORMAL, cmp);
1193: PetscObjectTypeCompare((PetscObject)C, MATNORMAL, cmp + 1);
1194: if (!cmp[0] != !cmp[1]) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "-pc_hpddm_levels_1_pc_asm_sub_mat_type %s and auxiliary Mat of type %s",((PetscObject)D)->type_name,((PetscObject)C)->type_name);
1195: if (!cmp[0]) {
1196: MatAXPY(D, 1.0, C, SUBSET_NONZERO_PATTERN);
1197: } else {
1198: Mat mat[2];
1199: MatNormalGetMat(D, mat);
1200: MatNormalGetMat(C, mat + 1);
1201: MatAXPY(mat[0], 1.0, mat[1], SUBSET_NONZERO_PATTERN);
1202: }
1203: MatPropagateSymmetryOptions(C, D);
1204: MatDestroy(&C);
1205: C = D;
1206: /* swap pointers so that variables stay consistent throughout PCSetUp() */
1207: std::swap(C, data->aux);
1208: std::swap(uis, data->is);
1209: swap = PETSC_TRUE;
1210: }
1211: } else if (data->share) {
1212: PetscInfo(pc, "Cannot share PC between ST and subdomain solver\n");
1213: data->share = PETSC_FALSE;
1214: }
1215: if (!data->levels[0]->scatter) {
1216: MatCreateVecs(P, &xin, NULL);
1217: if (ismatis) {
1218: MatDestroy(&P);
1219: }
1220: VecScatterCreate(xin, data->is, data->levels[0]->D, NULL, &data->levels[0]->scatter);
1221: VecDestroy(&xin);
1222: }
1223: if (data->levels[0]->P) {
1224: /* if the pattern is the same and PCSetUp() has previously succeeded, reuse HPDDM buffers and connectivity */
1225: HPDDM::Schwarz<PetscScalar>::destroy(data->levels[0], pc->setupcalled < 1 || pc->flag == DIFFERENT_NONZERO_PATTERN ? PETSC_TRUE : PETSC_FALSE);
1226: }
1227: if (!data->levels[0]->P) data->levels[0]->P = new HPDDM::Schwarz<PetscScalar>();
1228: if (data->log_separate) {
1229: PetscLogEventBegin(PC_HPDDM_SetUp[0], data->levels[0]->ksp, 0, 0, 0);
1230: } else {
1231: PetscLogEventBegin(PC_HPDDM_Strc, data->levels[0]->ksp, 0, 0, 0);
1232: }
1233: /* HPDDM internal data structure */
1234: data->levels[0]->P->structure(loc, data->is, sub[0], ismatis ? C : data->aux, data->levels);
1235: if (!data->log_separate) {
1236: PetscLogEventEnd(PC_HPDDM_Strc, data->levels[0]->ksp, 0, 0, 0);
1237: }
1238: /* matrix pencil of the generalized eigenvalue problem on the overlap (GenEO) */
1239: if (!data->B) {
1240: MatDuplicate(sub[0], MAT_COPY_VALUES, &weighted);
1241: PetscObjectTypeCompare((PetscObject)weighted, MATNORMAL, &flg);
1242: if (!flg) {
1243: MatDiagonalScale(weighted, data->levels[0]->D, data->levels[0]->D);
1244: } else { /* MATNORMAL applies MatDiagonalScale() in a matrix-free fashion, not what is needed since this won't be passed to SLEPc during the eigensolve */
1245: MatNormalGetMat(weighted, &data->B);
1246: MatDiagonalScale(data->B, NULL, data->levels[0]->D);
1247: data->B = NULL;
1248: flg = PETSC_FALSE;
1249: }
1250: /* neither MatDuplicate() nor MatDiagonaleScale() handles the symmetry options, so propagate the options explicitly */
1251: /* only useful for -mat_type baij -pc_hpddm_levels_1_st_pc_type cholesky (no problem with MATAIJ or MATSBAIJ) */
1252: MatPropagateSymmetryOptions(sub[0], weighted);
1253: } else weighted = data->B;
1254: /* SLEPc is used inside the loaded symbol */
1255: (*loadedSym)(data->levels[0]->P, data->is, ismatis ? C : (algebraic ? sub[0] : data->aux), weighted, data->B, initial, data->levels);
1256: if (data->share) {
1257: Mat st[2];
1258: KSPGetOperators(ksp[0], st, st + 1);
1259: MatCopy(subA[0], st[0], SAME_NONZERO_PATTERN);
1260: if (subA[1] != subA[0] || st[1] != st[0]) {
1261: MatCopy(subA[1], st[1], SAME_NONZERO_PATTERN);
1262: }
1263: }
1264: if (data->log_separate) {
1265: PetscLogEventEnd(PC_HPDDM_SetUp[0], data->levels[0]->ksp, 0, 0, 0);
1266: }
1267: if (ismatis) {
1268: MatISGetLocalMat(C, &N);
1269: } else N = data->aux;
1270: P = sub[0];
1271: /* going through the grid hierarchy */
1272: for (n = 1; n < data->N; ++n) {
1273: if (data->log_separate) {
1274: PetscLogEventBegin(PC_HPDDM_SetUp[n], data->levels[n]->ksp, 0, 0, 0);
1275: }
1276: /* method composed in the loaded symbol since there, SLEPc is used as well */
1277: PetscTryMethod(data->levels[0]->ksp, "PCHPDDMSetUp_Private_C", (Mat*, Mat*, PetscInt, PetscInt* const, PC_HPDDM_Level** const), (&P, &N, n, &data->N, data->levels));
1278: if (data->log_separate) {
1279: PetscLogEventEnd(PC_HPDDM_SetUp[n], data->levels[n]->ksp, 0, 0, 0);
1280: }
1281: }
1282: /* reset to NULL to avoid any faulty use */
1283: PetscObjectComposeFunction((PetscObject)data->levels[0]->ksp, "PCHPDDMSetUp_Private_C", NULL);
1284: if (!ismatis) {
1285: PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_C", NULL);
1286: } else {
1287: /* matching PetscObjectReference() above */
1288: PetscObjectDereference((PetscObject)C);
1289: }
1290: for (n = 0; n < data->N - 1; ++n)
1291: if (data->levels[n]->P) {
1292: /* HPDDM internal work buffers */
1293: data->levels[n]->P->setBuffer();
1294: data->levels[n]->P->super::start();
1295: }
1296: if (ismatis || !subdomains) {
1297: PCHPDDMDestroySubMatrices_Private(data->Neumann, algebraic, sub);
1298: }
1299: if (ismatis) data->is = NULL;
1300: for (n = 0; n < data->N - 1 + (reused > 0); ++n) {
1301: if (data->levels[n]->P) {
1302: PC spc;
1304: /* force the PC to be PCSHELL to do the coarse grid corrections */
1305: KSPSetSkipPCSetFromOptions(data->levels[n]->ksp, PETSC_TRUE);
1306: KSPGetPC(data->levels[n]->ksp, &spc);
1307: PCSetType(spc, PCSHELL);
1308: PCShellSetContext(spc, data->levels[n]);
1309: PCShellSetSetUp(spc, PCHPDDMShellSetUp);
1310: PCShellSetApply(spc, PCHPDDMShellApply);
1311: PCShellSetMatApply(spc, PCHPDDMShellMatApply);
1312: PCShellSetDestroy(spc, PCHPDDMShellDestroy);
1313: if (!data->levels[n]->pc) {
1314: PCCreate(PetscObjectComm((PetscObject)data->levels[n]->ksp), &data->levels[n]->pc);
1315: }
1316: if (n < reused) {
1317: PCSetReusePreconditioner(spc, PETSC_TRUE);
1318: PCSetReusePreconditioner(data->levels[n]->pc, PETSC_TRUE);
1319: }
1320: PCSetUp(spc);
1321: }
1322: }
1323: } else flg = reused ? PETSC_FALSE : PETSC_TRUE;
1324: if (!ismatis && subdomains) {
1325: if (flg) {
1326: KSPGetPC(data->levels[0]->ksp, &inner);
1327: } else inner = data->levels[0]->pc;
1328: if (inner) {
1329: PCSetType(inner, PCASM);
1330: if (!inner->setupcalled) {
1331: PCASMSetLocalSubdomains(inner, 1, is, &loc);
1332: if (!data->Neumann) { /* subdomain matrices are already created for the eigenproblem, reuse them for the fine-level PC */
1333: PCHPDDMPermute_Private(*is, NULL, NULL, sub[0], &C);
1334: PetscObjectCompose((PetscObject)inner->pmat, "_PCHPDDM_SubMatrices", (PetscObject)C);
1335: MatSetOperation(inner->pmat, MATOP_CREATE_SUBMATRICES, (void(*)(void))PCHPDDMCreateSubMatrices_Private);
1336: PCSetUp(inner);
1337: MatSetOperation(inner->pmat, MATOP_CREATE_SUBMATRICES, (void(*)(void))MatCreateSubMatrices);
1338: MatDestroy(&C);
1339: PetscObjectCompose((PetscObject)inner->pmat, "_PCHPDDM_SubMatrices", NULL);
1340: }
1341: }
1342: }
1343: PCHPDDMDestroySubMatrices_Private(data->Neumann, algebraic, sub);
1344: }
1345: ISDestroy(&loc);
1346: } else data->N = 1 + reused; /* enforce this value to 1 + reused if there is no way to build another level */
1347: if (requested != data->N + reused) {
1348: 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 : "");
1349: 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);
1350: /* cannot use PCHPDDMShellDestroy() because PCSHELL not set for unassembled levels */
1351: for (n = data->N - 1; n < requested - 1; ++n) {
1352: if (data->levels[n]->P) {
1353: HPDDM::Schwarz<PetscScalar>::destroy(data->levels[n], PETSC_TRUE);
1354: VecDestroyVecs(1, &data->levels[n]->v[0]);
1355: VecDestroyVecs(2, &data->levels[n]->v[1]);
1356: MatDestroy(data->levels[n]->V);
1357: MatDestroy(data->levels[n]->V + 1);
1358: MatDestroy(data->levels[n]->V + 2);
1359: VecDestroy(&data->levels[n]->D);
1360: VecScatterDestroy(&data->levels[n]->scatter);
1361: }
1362: }
1363: if (reused) {
1364: for (n = reused; n < PETSC_HPDDM_MAXLEVELS && data->levels[n]; ++n) {
1365: KSPDestroy(&data->levels[n]->ksp);
1366: PCDestroy(&data->levels[n]->pc);
1367: }
1368: }
1369: if (PetscDefined(USE_DEBUG)) 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);
1370: }
1371: /* these solvers are created after PCSetFromOptions() is called */
1372: if (pc->setfromoptionscalled) {
1373: for (n = 0; n < data->N; ++n) {
1374: if (data->levels[n]->ksp) {
1375: KSPSetFromOptions(data->levels[n]->ksp);
1376: }
1377: if (data->levels[n]->pc) {
1378: PCSetFromOptions(data->levels[n]->pc);
1379: }
1380: }
1381: pc->setfromoptionscalled = 0;
1382: }
1383: data->N += reused;
1384: if (data->share && swap) {
1385: /* swap back pointers so that variables follow the user-provided numbering */
1386: std::swap(C, data->aux);
1387: std::swap(uis, data->is);
1388: MatDestroy(&C);
1389: ISDestroy(&uis);
1390: }
1391: return(0);
1392: }
1394: /*@
1395: PCHPDDMSetCoarseCorrectionType - Sets the coarse correction type.
1397: Input Parameters:
1398: + pc - preconditioner context
1399: - type - PC_HPDDM_COARSE_CORRECTION_DEFLATED, PC_HPDDM_COARSE_CORRECTION_ADDITIVE, or PC_HPDDM_COARSE_CORRECTION_BALANCED
1401: Options Database Key:
1402: . -pc_hpddm_coarse_correction <deflated, additive, balanced> - type of coarse correction to apply
1404: Level: intermediate
1406: .seealso: PCHPDDMGetCoarseCorrectionType(), PCHPDDM, PCHPDDMCoarseCorrectionType
1407: @*/
1408: PetscErrorCode PCHPDDMSetCoarseCorrectionType(PC pc, PCHPDDMCoarseCorrectionType type)
1409: {
1415: PetscTryMethod(pc, "PCHPDDMSetCoarseCorrectionType_C", (PC, PCHPDDMCoarseCorrectionType), (pc, type));
1416: return(0);
1417: }
1419: /*@
1420: PCHPDDMGetCoarseCorrectionType - Gets the coarse correction type.
1422: Input Parameter:
1423: . pc - preconditioner context
1425: Output Parameter:
1426: . type - PC_HPDDM_COARSE_CORRECTION_DEFLATED, PC_HPDDM_COARSE_CORRECTION_ADDITIVE, or PC_HPDDM_COARSE_CORRECTION_BALANCED
1428: Level: intermediate
1430: .seealso: PCHPDDMSetCoarseCorrectionType(), PCHPDDM, PCHPDDMCoarseCorrectionType
1431: @*/
1432: PetscErrorCode PCHPDDMGetCoarseCorrectionType(PC pc, PCHPDDMCoarseCorrectionType *type)
1433: {
1438: if (type) {
1440: PetscUseMethod(pc, "PCHPDDMGetCoarseCorrectionType_C", (PC, PCHPDDMCoarseCorrectionType*), (pc, type));
1441: }
1442: return(0);
1443: }
1445: static PetscErrorCode PCHPDDMSetCoarseCorrectionType_HPDDM(PC pc, PCHPDDMCoarseCorrectionType type)
1446: {
1447: PC_HPDDM *data = (PC_HPDDM*)pc->data;
1450: if (type < 0 || type > 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PCHPDDMCoarseCorrectionType %d", type);
1451: data->correction = type;
1452: return(0);
1453: }
1455: static PetscErrorCode PCHPDDMGetCoarseCorrectionType_HPDDM(PC pc, PCHPDDMCoarseCorrectionType *type)
1456: {
1457: PC_HPDDM *data = (PC_HPDDM*)pc->data;
1460: *type = data->correction;
1461: return(0);
1462: }
1464: /*@
1465: PCHPDDMGetSTShareSubKSP - Gets whether the KSP in SLEPc ST and the fine-level subdomain solver is shared.
1467: Input Parameter:
1468: . pc - preconditioner context
1470: Output Parameter:
1471: . share - whether the KSP is shared or not
1473: Notes:
1474: This is not the same as PCGetReusePreconditioner(). The return value is unlikely to be true, but when it is, a symbolic factorization can be skipped
1475: when using a subdomain PCType such as PCLU or PCCHOLESKY.
1477: Level: advanced
1479: @*/
1480: PetscErrorCode PCHPDDMGetSTShareSubKSP(PC pc, PetscBool *share)
1481: {
1486: if (share) {
1488: PetscUseMethod(pc, "PCHPDDMGetSTShareSubKSP_C", (PC, PetscBool*), (pc, share));
1489: }
1490: return(0);
1491: }
1493: static PetscErrorCode PCHPDDMGetSTShareSubKSP_HPDDM(PC pc, PetscBool *share)
1494: {
1495: PC_HPDDM *data = (PC_HPDDM*)pc->data;
1498: *share = data->share;
1499: return(0);
1500: }
1502: PetscErrorCode HPDDMLoadDL_Private(PetscBool *found)
1503: {
1504: char lib[PETSC_MAX_PATH_LEN], dlib[PETSC_MAX_PATH_LEN], dir[PETSC_MAX_PATH_LEN];
1508: PetscStrcpy(dir, "${PETSC_LIB_DIR}");
1509: PetscOptionsGetString(NULL, NULL, "-hpddm_dir", dir, sizeof(dir), NULL);
1510: PetscSNPrintf(lib, sizeof(lib), "%s/libhpddm_petsc", dir);
1511: PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found);
1512: if (*found) {
1513: PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, dlib);
1514: #if defined(SLEPC_LIB_DIR) /* this variable is passed during SLEPc ./configure since */
1515: } else { /* slepcconf.h is not yet built (and thus can't be included) */
1516: PetscStrcpy(dir, HPDDM_STR(SLEPC_LIB_DIR));
1517: PetscSNPrintf(lib, sizeof(lib), "%s/libhpddm_petsc", dir);
1518: PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found);
1519: if (*found) {
1520: PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, dlib);
1521: #endif
1522: } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "%s not found", lib);
1523: #if defined(SLEPC_LIB_DIR)
1524: }
1525: #endif
1526: return(0);
1527: }
1529: /*MC
1530: PCHPDDM - Interface with the HPDDM library.
1532: 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. The interface is explained in details in [2021a].
1534: The matrix to be preconditioned (Pmat) may be unassembled (MATIS), assembled (MATMPIAIJ, MATMPIBAIJ, or MATMPISBAIJ), hierarchical (MATHTOOL), or MATNORMAL. For multilevel preconditioning, when using an assembled or hierarchical 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().
1536: Options Database Keys:
1537: + -pc_hpddm_define_subdomains <true, default=false> - on the finest level, calls PCASMSetLocalSubdomains() with the IS supplied in PCHPDDMSetAuxiliaryMat() (not relevant with an unassembled Pmat)
1538: . -pc_hpddm_has_neumann <true, default=false> - on the finest level, informs the PC that the local Neumann matrix is supplied in PCHPDDMSetAuxiliaryMat()
1539: - -pc_hpddm_coarse_correction <type, default=deflated> - determines the PCHPDDMCoarseCorrectionType when calling PCApply
1541: Options for subdomain solvers, subdomain eigensolvers (for computing deflation vectors), and the coarse solver can be set with
1542: .vb
1543: -pc_hpddm_levels_%d_pc_
1544: -pc_hpddm_levels_%d_ksp_
1545: -pc_hpddm_levels_%d_eps_
1546: -pc_hpddm_levels_%d_p
1547: -pc_hpddm_levels_%d_mat_type_
1548: -pc_hpddm_coarse_
1549: -pc_hpddm_coarse_p
1550: -pc_hpddm_coarse_mat_type_
1551: .ve
1552: 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).
1554: 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.
1556: 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.
1558: References:
1559: + 2011 - A robust two-level domain decomposition preconditioner for systems of PDEs. Spillane, Dolean, Hauret, Nataf, Pechstein, and Scheichl. Comptes Rendus Mathematique.
1560: . 2013 - Scalable domain decomposition preconditioners for heterogeneous elliptic problems. Jolivet, Hecht, Nataf, and Prud'homme. SC13.
1561: . 2015 - An introduction to domain decomposition methods: algorithms, theory, and parallel implementation. Dolean, Jolivet, and Nataf. SIAM.
1562: . 2019 - A multilevel Schwarz preconditioner based on a hierarchy of robust coarse spaces. Al Daas, Grigori, Jolivet, and Tournier. SIAM Journal on Scientific Computing.
1563: . 2021a - KSPHPDDM and PCHPDDM: extending PETSc with advanced Krylov methods and robust multilevel overlapping Schwarz preconditioners. Jolivet, Roman, and Zampini. Computer & Mathematics with Applications.
1564: . 2021b - A robust algebraic domain decomposition preconditioner for sparse normal equations. Al Daas, Jolivet, and Scott.
1565: - 2021c - A robust algebraic multilevel domain decomposition preconditioner for sparse symmetric positive definite matrices. Al Daas and Jolivet.
1567: Level: intermediate
1569: .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, PCHPDDMSetAuxiliaryMat(), MATIS, PCBDDC, PCDEFLATION, PCTELESCOPE
1570: M*/
1571: PETSC_EXTERN PetscErrorCode PCCreate_HPDDM(PC pc)
1572: {
1573: PC_HPDDM *data;
1574: PetscBool found;
1578: if (!loadedSym) {
1579: HPDDMLoadDL_Private(&found);
1580: if (found) {
1581: PetscDLLibrarySym(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, NULL, "PCHPDDM_Internal", (void**)&loadedSym);
1582: }
1583: }
1584: if (!loadedSym) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCHPDDM_Internal symbol not found in loaded libhpddm_petsc");
1585: PetscNewLog(pc, &data);
1586: pc->data = data;
1587: pc->ops->reset = PCReset_HPDDM;
1588: pc->ops->destroy = PCDestroy_HPDDM;
1589: pc->ops->setfromoptions = PCSetFromOptions_HPDDM;
1590: pc->ops->setup = PCSetUp_HPDDM;
1591: pc->ops->apply = PCApply_HPDDM;
1592: pc->ops->matapply = PCMatApply_HPDDM;
1593: pc->ops->view = PCView_HPDDM;
1594: pc->ops->presolve = PCPreSolve_HPDDM;
1595: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetAuxiliaryMat_C", PCHPDDMSetAuxiliaryMat_HPDDM);
1596: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMHasNeumannMat_C", PCHPDDMHasNeumannMat_HPDDM);
1597: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetRHSMat_C", PCHPDDMSetRHSMat_HPDDM);
1598: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetCoarseCorrectionType_C", PCHPDDMSetCoarseCorrectionType_HPDDM);
1599: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetCoarseCorrectionType_C", PCHPDDMGetCoarseCorrectionType_HPDDM);
1600: PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetSTShareSubKSP_C", PCHPDDMGetSTShareSubKSP_HPDDM);
1601: return(0);
1602: }
1604: /*@C
1605: PCHPDDMInitializePackage - This function initializes everything in the PCHPDDM package. It is called from PCInitializePackage().
1607: Level: intermediate
1609: .seealso: PetscInitialize()
1610: @*/
1611: PetscErrorCode PCHPDDMInitializePackage(void)
1612: {
1613: char ename[32];
1614: PetscInt i;
1618: if (PCHPDDMPackageInitialized) return(0);
1619: PCHPDDMPackageInitialized = PETSC_TRUE;
1620: PetscRegisterFinalize(PCHPDDMFinalizePackage);
1621: /* general events registered once during package initialization */
1622: /* some of these events are not triggered in libpetsc, */
1623: /* but rather directly in libhpddm_petsc, */
1624: /* which is in charge of performing the following operations */
1626: /* domain decomposition structure from Pmat sparsity pattern */
1627: PetscLogEventRegister("PCHPDDMStrc", PC_CLASSID, &PC_HPDDM_Strc);
1628: /* Galerkin product, redistribution, and setup (not triggered in libpetsc) */
1629: PetscLogEventRegister("PCHPDDMPtAP", PC_CLASSID, &PC_HPDDM_PtAP);
1630: /* Galerkin product with summation, redistribution, and setup (not triggered in libpetsc) */
1631: PetscLogEventRegister("PCHPDDMPtBP", PC_CLASSID, &PC_HPDDM_PtBP);
1632: /* next level construction using PtAP and PtBP (not triggered in libpetsc) */
1633: PetscLogEventRegister("PCHPDDMNext", PC_CLASSID, &PC_HPDDM_Next);
1634: static_assert(PETSC_HPDDM_MAXLEVELS <= 9, "PETSC_HPDDM_MAXLEVELS value is too high");
1635: for (i = 1; i < PETSC_HPDDM_MAXLEVELS; ++i) {
1636: PetscSNPrintf(ename, sizeof(ename), "PCHPDDMSetUp L%1d", i);
1637: /* events during a PCSetUp() at level #i _except_ the assembly */
1638: /* of the Galerkin operator of the coarser level #(i + 1) */
1639: PetscLogEventRegister(ename, PC_CLASSID, &PC_HPDDM_SetUp[i - 1]);
1640: PetscSNPrintf(ename, sizeof(ename), "PCHPDDMSolve L%1d", i);
1641: /* events during a PCApply() at level #i _except_ */
1642: /* the KSPSolve() of the coarser level #(i + 1) */
1643: PetscLogEventRegister(ename, PC_CLASSID, &PC_HPDDM_Solve[i - 1]);
1644: }
1645: return(0);
1646: }
1648: /*@C
1649: PCHPDDMFinalizePackage - This function frees everything from the PCHPDDM package. It is called from PetscFinalize().
1651: Level: intermediate
1653: .seealso: PetscFinalize()
1654: @*/
1655: PetscErrorCode PCHPDDMFinalizePackage(void)
1656: {
1658: PCHPDDMPackageInitialized = PETSC_FALSE;
1659: return(0);
1660: }