xref: /petsc/src/ksp/pc/impls/hpddm/pchpddm.cxx (revision 38fb34a17177baeebd893e1b56c70faaeeb78c7c)
1f1580f4eSBarry Smith #include <petsc/private/dmimpl.h>
2f1580f4eSBarry Smith #include <petsc/private/matimpl.h>
3f1580f4eSBarry Smith #include <petsc/private/petschpddm.h> /*I "petscpc.h" I*/
4f1580f4eSBarry Smith #include <petsc/private/pcimpl.h>     /* this must be included after petschpddm.h so that _PCIMPL_H is not defined            */
5f1580f4eSBarry Smith                                       /* otherwise, it is assumed that one is compiling libhpddm_petsc => circular dependency */
602800ff6SPierre Jolivet #if PetscDefined(HAVE_FORTRAN)
7f1580f4eSBarry Smith   #include <petsc/private/fortranimpl.h>
8f1580f4eSBarry Smith #endif
9f1580f4eSBarry Smith 
10f1580f4eSBarry Smith static PetscErrorCode (*loadedSym)(HPDDM::Schwarz<PetscScalar> *const, IS, Mat, Mat, Mat, std::vector<Vec>, PC_HPDDM_Level **const) = NULL;
11f1580f4eSBarry Smith 
12f1580f4eSBarry Smith static PetscBool PCHPDDMPackageInitialized = PETSC_FALSE;
13f1580f4eSBarry Smith 
14f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_Strc;
15f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_PtAP;
16f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_PtBP;
17f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_Next;
18f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_SetUp[PETSC_PCHPDDM_MAXLEVELS];
19f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_Solve[PETSC_PCHPDDM_MAXLEVELS];
20f1580f4eSBarry Smith 
21f1580f4eSBarry Smith const char *const PCHPDDMCoarseCorrectionTypes[] = {"DEFLATED", "ADDITIVE", "BALANCED", "PCHPDDMCoarseCorrectionType", "PC_HPDDM_COARSE_CORRECTION_", NULL};
22f1580f4eSBarry Smith 
23d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_HPDDM(PC pc)
24d71ae5a4SJacob Faibussowitsch {
25f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
26f1580f4eSBarry Smith   PetscInt  i;
27f1580f4eSBarry Smith 
28f1580f4eSBarry Smith   PetscFunctionBegin;
29f1580f4eSBarry Smith   if (data->levels) {
30f1580f4eSBarry Smith     for (i = 0; i < PETSC_PCHPDDM_MAXLEVELS && data->levels[i]; ++i) {
31f1580f4eSBarry Smith       PetscCall(KSPDestroy(&data->levels[i]->ksp));
32f1580f4eSBarry Smith       PetscCall(PCDestroy(&data->levels[i]->pc));
33f1580f4eSBarry Smith       PetscCall(PetscFree(data->levels[i]));
34f1580f4eSBarry Smith     }
35f1580f4eSBarry Smith     PetscCall(PetscFree(data->levels));
36f1580f4eSBarry Smith   }
37f1580f4eSBarry Smith 
38f1580f4eSBarry Smith   PetscCall(ISDestroy(&data->is));
39f1580f4eSBarry Smith   PetscCall(MatDestroy(&data->aux));
40f1580f4eSBarry Smith   PetscCall(MatDestroy(&data->B));
41f1580f4eSBarry Smith   PetscCall(VecDestroy(&data->normal));
42f1580f4eSBarry Smith   data->correction = PC_HPDDM_COARSE_CORRECTION_DEFLATED;
43c8ea6600SPierre Jolivet   data->Neumann    = PETSC_BOOL3_UNKNOWN;
44f1580f4eSBarry Smith   data->deflation  = PETSC_FALSE;
45f1580f4eSBarry Smith   data->setup      = NULL;
46f1580f4eSBarry Smith   data->setup_ctx  = NULL;
473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48f1580f4eSBarry Smith }
49f1580f4eSBarry Smith 
50d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_HPDDM(PC pc)
51d71ae5a4SJacob Faibussowitsch {
52f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
53f1580f4eSBarry Smith 
54f1580f4eSBarry Smith   PetscFunctionBegin;
55f1580f4eSBarry Smith   PetscCall(PCReset_HPDDM(pc));
56f1580f4eSBarry Smith   PetscCall(PetscFree(data));
57f1580f4eSBarry Smith   PetscCall(PetscObjectChangeTypeName((PetscObject)pc, NULL));
58f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetAuxiliaryMat_C", NULL));
59f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMHasNeumannMat_C", NULL));
60f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetRHSMat_C", NULL));
61f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetCoarseCorrectionType_C", NULL));
62f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetCoarseCorrectionType_C", NULL));
63e31fc274SPierre Jolivet   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetSTShareSubKSP_C", NULL));
64f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetSTShareSubKSP_C", NULL));
65f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetDeflationMat_C", NULL));
663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67f1580f4eSBarry Smith }
68f1580f4eSBarry Smith 
69d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PCHPDDMSetAuxiliaryMat_Private(PC pc, IS is, Mat A, PetscBool deflation)
70d71ae5a4SJacob Faibussowitsch {
71f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
72f1580f4eSBarry Smith 
73f1580f4eSBarry Smith   PetscFunctionBegin;
74f1580f4eSBarry Smith   if (is) {
75f1580f4eSBarry Smith     PetscCall(PetscObjectReference((PetscObject)is));
76f1580f4eSBarry Smith     if (data->is) { /* new overlap definition resets the PC */
77f1580f4eSBarry Smith       PetscCall(PCReset_HPDDM(pc));
78f1580f4eSBarry Smith       pc->setfromoptionscalled = 0;
79b853e353SPierre Jolivet       pc->setupcalled          = 0;
80f1580f4eSBarry Smith     }
81f1580f4eSBarry Smith     PetscCall(ISDestroy(&data->is));
82f1580f4eSBarry Smith     data->is = is;
83f1580f4eSBarry Smith   }
84f1580f4eSBarry Smith   if (A) {
85f1580f4eSBarry Smith     PetscCall(PetscObjectReference((PetscObject)A));
86f1580f4eSBarry Smith     PetscCall(MatDestroy(&data->aux));
87f1580f4eSBarry Smith     data->aux = A;
88f1580f4eSBarry Smith   }
89f1580f4eSBarry Smith   data->deflation = deflation;
903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91f1580f4eSBarry Smith }
92f1580f4eSBarry Smith 
93d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PCHPDDMSetAuxiliaryMatNormal_Private(PC pc, Mat A, Mat N, Mat *B, const char *pcpre)
94d71ae5a4SJacob Faibussowitsch {
95f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
96f1580f4eSBarry Smith   Mat      *splitting, *sub, aux;
97f1580f4eSBarry Smith   IS        is, cols[2], rows;
98f1580f4eSBarry Smith   PetscReal norm;
99f1580f4eSBarry Smith   PetscBool flg;
100f1580f4eSBarry Smith   char      type[256] = {}; /* same size as in src/ksp/pc/interface/pcset.c */
101f1580f4eSBarry Smith 
102f1580f4eSBarry Smith   PetscFunctionBegin;
103f1580f4eSBarry Smith   PetscCall(MatConvert(N, MATAIJ, MAT_INITIAL_MATRIX, B));
104f1580f4eSBarry Smith   PetscCall(ISCreateStride(PETSC_COMM_SELF, A->cmap->n, A->cmap->rstart, 1, cols));
105f1580f4eSBarry Smith   PetscCall(ISCreateStride(PETSC_COMM_SELF, A->rmap->N, 0, 1, &rows));
106f1580f4eSBarry Smith   PetscCall(MatSetOption(A, MAT_SUBMAT_SINGLEIS, PETSC_TRUE));
107f1580f4eSBarry Smith   PetscCall(MatIncreaseOverlap(*B, 1, cols, 1));
108f1580f4eSBarry Smith   PetscCall(MatCreateSubMatrices(A, 1, &rows, cols, MAT_INITIAL_MATRIX, &splitting));
109f1580f4eSBarry Smith   PetscCall(ISCreateStride(PETSC_COMM_SELF, A->cmap->n, A->cmap->rstart, 1, &is));
110f1580f4eSBarry Smith   PetscCall(ISEmbed(*cols, is, PETSC_TRUE, cols + 1));
111f1580f4eSBarry Smith   PetscCall(ISDestroy(&is));
112f1580f4eSBarry Smith   PetscCall(MatCreateSubMatrices(*splitting, 1, &rows, cols + 1, MAT_INITIAL_MATRIX, &sub));
113f1580f4eSBarry Smith   PetscCall(ISDestroy(cols + 1));
114f1580f4eSBarry Smith   PetscCall(MatFindZeroRows(*sub, &is));
115f1580f4eSBarry Smith   PetscCall(MatDestroySubMatrices(1, &sub));
116f1580f4eSBarry Smith   PetscCall(ISDestroy(&rows));
117f1580f4eSBarry Smith   PetscCall(MatSetOption(*splitting, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE));
118f1580f4eSBarry Smith   PetscCall(MatZeroRowsIS(*splitting, is, 0.0, NULL, NULL));
119f1580f4eSBarry Smith   PetscCall(ISDestroy(&is));
120f1580f4eSBarry Smith   PetscCall(PetscOptionsGetString(NULL, pcpre, "-pc_hpddm_levels_1_sub_pc_type", type, sizeof(type), NULL));
121f1580f4eSBarry Smith   PetscCall(PetscStrcmp(type, PCQR, &flg));
122f1580f4eSBarry Smith   if (!flg) {
123f1580f4eSBarry Smith     Mat conjugate = *splitting;
124f1580f4eSBarry Smith     if (PetscDefined(USE_COMPLEX)) {
125f1580f4eSBarry Smith       PetscCall(MatDuplicate(*splitting, MAT_COPY_VALUES, &conjugate));
126f1580f4eSBarry Smith       PetscCall(MatConjugate(conjugate));
127f1580f4eSBarry Smith     }
128f1580f4eSBarry Smith     PetscCall(MatTransposeMatMult(conjugate, *splitting, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &aux));
129f1580f4eSBarry Smith     if (PetscDefined(USE_COMPLEX)) PetscCall(MatDestroy(&conjugate));
130f1580f4eSBarry Smith     PetscCall(MatNorm(aux, NORM_FROBENIUS, &norm));
131f1580f4eSBarry Smith     PetscCall(MatSetOption(aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
132f1580f4eSBarry Smith     PetscCall(MatShift(aux, PETSC_SMALL * norm));
133f1580f4eSBarry Smith   } else {
134f1580f4eSBarry Smith     PetscBool flg;
135f1580f4eSBarry Smith     PetscCall(PetscObjectTypeCompare((PetscObject)N, MATNORMAL, &flg));
136f1580f4eSBarry Smith     if (flg) PetscCall(MatCreateNormal(*splitting, &aux));
137f1580f4eSBarry Smith     else PetscCall(MatCreateNormalHermitian(*splitting, &aux));
138f1580f4eSBarry Smith   }
139f1580f4eSBarry Smith   PetscCall(MatDestroySubMatrices(1, &splitting));
140f1580f4eSBarry Smith   PetscCall(PCHPDDMSetAuxiliaryMat(pc, *cols, aux, NULL, NULL));
141c8ea6600SPierre Jolivet   data->Neumann = PETSC_BOOL3_TRUE;
142f1580f4eSBarry Smith   PetscCall(ISDestroy(cols));
143f1580f4eSBarry Smith   PetscCall(MatDestroy(&aux));
1443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
145f1580f4eSBarry Smith }
146f1580f4eSBarry Smith 
147d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetAuxiliaryMat_HPDDM(PC pc, IS is, Mat A, PetscErrorCode (*setup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void *setup_ctx)
148d71ae5a4SJacob Faibussowitsch {
149f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
150f1580f4eSBarry Smith 
151f1580f4eSBarry Smith   PetscFunctionBegin;
152f1580f4eSBarry Smith   PetscCall(PCHPDDMSetAuxiliaryMat_Private(pc, is, A, PETSC_FALSE));
153f1580f4eSBarry Smith   if (setup) {
154f1580f4eSBarry Smith     data->setup     = setup;
155f1580f4eSBarry Smith     data->setup_ctx = setup_ctx;
156f1580f4eSBarry Smith   }
1573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
158f1580f4eSBarry Smith }
159f1580f4eSBarry Smith 
160f1580f4eSBarry Smith /*@
161f1580f4eSBarry Smith      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.
162f1580f4eSBarry Smith 
163f1580f4eSBarry Smith    Input Parameters:
164f1580f4eSBarry Smith +     pc - preconditioner context
165f1580f4eSBarry Smith .     is - index set of the local auxiliary, e.g., Neumann, matrix
166f1580f4eSBarry Smith .     A - auxiliary sequential matrix
167f1580f4eSBarry Smith .     setup - function for generating the auxiliary matrix
168f1580f4eSBarry Smith -     setup_ctx - context for setup
169f1580f4eSBarry Smith 
170f1580f4eSBarry Smith    Level: intermediate
171f1580f4eSBarry Smith 
172f1580f4eSBarry Smith .seealso: `PCHPDDM`, `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCHPDDMSetRHSMat()`, `MATIS`
173f1580f4eSBarry Smith @*/
174d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMSetAuxiliaryMat(PC pc, IS is, Mat A, PetscErrorCode (*setup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void *setup_ctx)
175d71ae5a4SJacob Faibussowitsch {
176f1580f4eSBarry Smith   PetscFunctionBegin;
177f1580f4eSBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
178f1580f4eSBarry Smith   if (is) PetscValidHeaderSpecific(is, IS_CLASSID, 2);
179f1580f4eSBarry Smith   if (A) PetscValidHeaderSpecific(A, MAT_CLASSID, 3);
18002800ff6SPierre Jolivet #if PetscDefined(HAVE_FORTRAN)
181f1580f4eSBarry Smith   if (reinterpret_cast<void *>(setup) == reinterpret_cast<void *>(PETSC_NULL_FUNCTION_Fortran)) setup = NULL;
182f1580f4eSBarry Smith   if (setup_ctx == PETSC_NULL_INTEGER_Fortran) setup_ctx = NULL;
183f1580f4eSBarry Smith #endif
184f1580f4eSBarry Smith   PetscTryMethod(pc, "PCHPDDMSetAuxiliaryMat_C", (PC, IS, Mat, PetscErrorCode(*)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void *), (pc, is, A, setup, setup_ctx));
1853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
186f1580f4eSBarry Smith }
187f1580f4eSBarry Smith 
188d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMHasNeumannMat_HPDDM(PC pc, PetscBool has)
189d71ae5a4SJacob Faibussowitsch {
190f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
191f1580f4eSBarry Smith 
192f1580f4eSBarry Smith   PetscFunctionBegin;
193c8ea6600SPierre Jolivet   data->Neumann = PetscBoolToBool3(has);
1943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
195f1580f4eSBarry Smith }
196f1580f4eSBarry Smith 
197f1580f4eSBarry Smith /*@
198f1580f4eSBarry Smith      PCHPDDMHasNeumannMat - Informs `PCHPDDM` that the `Mat` passed to `PCHPDDMSetAuxiliaryMat()` is the local Neumann matrix.
199f1580f4eSBarry Smith 
200f1580f4eSBarry Smith    Input Parameters:
201f1580f4eSBarry Smith +     pc - preconditioner context
202f1580f4eSBarry Smith -     has - Boolean value
203f1580f4eSBarry Smith 
204f1580f4eSBarry Smith    Level: intermediate
205f1580f4eSBarry Smith 
206f1580f4eSBarry Smith    Notes:
2077eb095acSPierre Jolivet    This may be used to bypass a call to `MatCreateSubMatrices()` and to `MatConvert()` for `MATSBAIJ` matrices.
208f1580f4eSBarry Smith 
209f1580f4eSBarry Smith    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`.
210f1580f4eSBarry Smith 
211f1580f4eSBarry Smith .seealso: `PCHPDDM`, `PCHPDDMSetAuxiliaryMat()`
212f1580f4eSBarry Smith @*/
213d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMHasNeumannMat(PC pc, PetscBool has)
214d71ae5a4SJacob Faibussowitsch {
215f1580f4eSBarry Smith   PetscFunctionBegin;
216f1580f4eSBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
217f1580f4eSBarry Smith   PetscTryMethod(pc, "PCHPDDMHasNeumannMat_C", (PC, PetscBool), (pc, has));
2183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
219f1580f4eSBarry Smith }
220f1580f4eSBarry Smith 
221d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetRHSMat_HPDDM(PC pc, Mat B)
222d71ae5a4SJacob Faibussowitsch {
223f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
224f1580f4eSBarry Smith 
225f1580f4eSBarry Smith   PetscFunctionBegin;
226f1580f4eSBarry Smith   PetscCall(PetscObjectReference((PetscObject)B));
227f1580f4eSBarry Smith   PetscCall(MatDestroy(&data->B));
228f1580f4eSBarry Smith   data->B = B;
2293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
230f1580f4eSBarry Smith }
231f1580f4eSBarry Smith 
232f1580f4eSBarry Smith /*@
233f1580f4eSBarry Smith      PCHPDDMSetRHSMat - Sets the right-hand side matrix used by `PCHPDDM` for the concurrent GenEO problems at the finest level. Must be used in conjunction 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.
234f1580f4eSBarry Smith 
235f1580f4eSBarry Smith    Input Parameters:
236f1580f4eSBarry Smith +     pc - preconditioner context
237f1580f4eSBarry Smith -     B - right-hand side sequential matrix
238f1580f4eSBarry Smith 
239f1580f4eSBarry Smith    Level: advanced
240f1580f4eSBarry Smith 
241f1580f4eSBarry Smith .seealso: `PCHPDDMSetAuxiliaryMat()`, `PCHPDDM`
242f1580f4eSBarry Smith @*/
243d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMSetRHSMat(PC pc, Mat B)
244d71ae5a4SJacob Faibussowitsch {
245f1580f4eSBarry Smith   PetscFunctionBegin;
246f1580f4eSBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
247f1580f4eSBarry Smith   if (B) {
248f1580f4eSBarry Smith     PetscValidHeaderSpecific(B, MAT_CLASSID, 2);
249f1580f4eSBarry Smith     PetscTryMethod(pc, "PCHPDDMSetRHSMat_C", (PC, Mat), (pc, B));
250f1580f4eSBarry Smith   }
2513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
252f1580f4eSBarry Smith }
253f1580f4eSBarry Smith 
254d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetFromOptions_HPDDM(PC pc, PetscOptionItems *PetscOptionsObject)
255d71ae5a4SJacob Faibussowitsch {
256f1580f4eSBarry Smith   PC_HPDDM                   *data   = (PC_HPDDM *)pc->data;
257f1580f4eSBarry Smith   PC_HPDDM_Level            **levels = data->levels;
258f1580f4eSBarry Smith   char                        prefix[256];
259f1580f4eSBarry Smith   int                         i = 1;
260f1580f4eSBarry Smith   PetscMPIInt                 size, previous;
261f1580f4eSBarry Smith   PetscInt                    n;
262f1580f4eSBarry Smith   PCHPDDMCoarseCorrectionType type;
263c8ea6600SPierre Jolivet   PetscBool                   flg = PETSC_TRUE, set;
264f1580f4eSBarry Smith 
265f1580f4eSBarry Smith   PetscFunctionBegin;
266f1580f4eSBarry Smith   if (!data->levels) {
267f1580f4eSBarry Smith     PetscCall(PetscCalloc1(PETSC_PCHPDDM_MAXLEVELS, &levels));
268f1580f4eSBarry Smith     data->levels = levels;
269f1580f4eSBarry Smith   }
270f1580f4eSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "PCHPDDM options");
271f1580f4eSBarry Smith   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
272f1580f4eSBarry Smith   previous = size;
273f1580f4eSBarry Smith   while (i < PETSC_PCHPDDM_MAXLEVELS) {
274f1580f4eSBarry Smith     PetscInt p = 1;
275f1580f4eSBarry Smith 
2764dfa11a4SJacob Faibussowitsch     if (!data->levels[i - 1]) PetscCall(PetscNew(data->levels + i - 1));
277f1580f4eSBarry Smith     data->levels[i - 1]->parent = data;
278f1580f4eSBarry Smith     /* if the previous level has a single process, it is not possible to coarsen further */
279f1580f4eSBarry Smith     if (previous == 1 || !flg) break;
280f1580f4eSBarry Smith     data->levels[i - 1]->nu        = 0;
281f1580f4eSBarry Smith     data->levels[i - 1]->threshold = -1.0;
282f1580f4eSBarry Smith     PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_nev", i));
28302800ff6SPierre Jolivet     PetscCall(PetscOptionsBoundedInt(prefix, "Local number of deflation vectors computed by SLEPc", "EPSSetDimensions", data->levels[i - 1]->nu, &data->levels[i - 1]->nu, NULL, 0));
284f1580f4eSBarry Smith     PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold", i));
285f1580f4eSBarry Smith     PetscCall(PetscOptionsReal(prefix, "Local threshold for selecting deflation vectors returned by SLEPc", "PCHPDDM", data->levels[i - 1]->threshold, &data->levels[i - 1]->threshold, NULL));
286f1580f4eSBarry Smith     if (i == 1) {
287f1580f4eSBarry Smith       PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_1_st_share_sub_ksp"));
288e31fc274SPierre Jolivet       PetscCall(PetscOptionsBool(prefix, "Shared KSP between SLEPc ST and the fine-level subdomain solver", "PCHPDDMSetSTShareSubKSP", PETSC_FALSE, &data->share, NULL));
289f1580f4eSBarry Smith     }
290f1580f4eSBarry Smith     /* if there is no prescribed coarsening, just break out of the loop */
291f1580f4eSBarry Smith     if (data->levels[i - 1]->threshold <= 0.0 && data->levels[i - 1]->nu <= 0 && !(data->deflation && i == 1)) break;
292f1580f4eSBarry Smith     else {
293f1580f4eSBarry Smith       ++i;
294f1580f4eSBarry Smith       PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_nev", i));
295f1580f4eSBarry Smith       PetscCall(PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg));
296f1580f4eSBarry Smith       if (!flg) {
297f1580f4eSBarry Smith         PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold", i));
298f1580f4eSBarry Smith         PetscCall(PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg));
299f1580f4eSBarry Smith       }
300f1580f4eSBarry Smith       if (flg) {
301f1580f4eSBarry Smith         /* if there are coarsening options for the next level, then register it  */
302f1580f4eSBarry Smith         /* otherwise, don't to avoid having both options levels_N_p and coarse_p */
303f1580f4eSBarry Smith         PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_p", i));
304f1580f4eSBarry Smith         PetscCall(PetscOptionsRangeInt(prefix, "Number of processes used to assemble the coarse operator at this level", "PCHPDDM", p, &p, &flg, 1, PetscMax(1, previous / 2)));
305f1580f4eSBarry Smith         previous = p;
306f1580f4eSBarry Smith       }
307f1580f4eSBarry Smith     }
308f1580f4eSBarry Smith   }
309f1580f4eSBarry Smith   data->N = i;
310f1580f4eSBarry Smith   n       = 1;
311f1580f4eSBarry Smith   if (i > 1) {
312f1580f4eSBarry Smith     PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_coarse_p"));
313f1580f4eSBarry Smith     PetscCall(PetscOptionsRangeInt(prefix, "Number of processes used to assemble the coarsest operator", "PCHPDDM", n, &n, NULL, 1, PetscMax(1, previous / 2)));
31402800ff6SPierre Jolivet #if PetscDefined(HAVE_MUMPS)
315f1580f4eSBarry Smith     PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "pc_hpddm_coarse_"));
316f1580f4eSBarry Smith     PetscCall(PetscOptionsHasName(NULL, prefix, "-mat_mumps_use_omp_threads", &flg));
317f1580f4eSBarry Smith     if (flg) {
318f1580f4eSBarry Smith       char type[64];                                                                                     /* same size as in src/ksp/pc/impls/factor/factimpl.c */
3193ce573a3SPierre Jolivet       PetscCall(PetscStrcpy(type, n > 1 && PetscDefined(HAVE_MUMPS) ? MATSOLVERMUMPS : MATSOLVERPETSC)); /* default solver for a MatMPIAIJ or a MatSeqAIJ */
3203ce573a3SPierre Jolivet       PetscCall(PetscOptionsGetString(NULL, prefix, "-pc_factor_mat_solver_type", type, sizeof(type), NULL));
3213ce573a3SPierre Jolivet       PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &flg));
322f1580f4eSBarry Smith       PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "-%smat_mumps_use_omp_threads and -%spc_factor_mat_solver_type != %s", prefix, prefix, MATSOLVERMUMPS);
323f1580f4eSBarry Smith       size = n;
324f1580f4eSBarry Smith       n    = -1;
325f1580f4eSBarry Smith       PetscCall(PetscOptionsGetInt(NULL, prefix, "-mat_mumps_use_omp_threads", &n, NULL));
326f1580f4eSBarry Smith       PetscCheck(n >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Need to specify a positive integer for -%smat_mumps_use_omp_threads", prefix);
327f1580f4eSBarry Smith       PetscCheck(n * size <= previous, 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" : "");
328f1580f4eSBarry Smith     }
329f1580f4eSBarry Smith #endif
330f1580f4eSBarry Smith     PetscCall(PetscOptionsEnum("-pc_hpddm_coarse_correction", "Type of coarse correction applied each iteration", "PCHPDDMSetCoarseCorrectionType", PCHPDDMCoarseCorrectionTypes, (PetscEnum)data->correction, (PetscEnum *)&type, &flg));
331f1580f4eSBarry Smith     if (flg) PetscCall(PCHPDDMSetCoarseCorrectionType(pc, type));
332f1580f4eSBarry Smith     PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_has_neumann"));
333c8ea6600SPierre Jolivet     PetscCall(PetscOptionsBool(prefix, "Is the auxiliary Mat the local Neumann matrix?", "PCHPDDMHasNeumannMat", PetscBool3ToBool(data->Neumann), &flg, &set));
334c8ea6600SPierre Jolivet     if (set) data->Neumann = PetscBoolToBool3(flg);
335f1580f4eSBarry Smith     data->log_separate = PETSC_FALSE;
336f1580f4eSBarry Smith     if (PetscDefined(USE_LOG)) {
337f1580f4eSBarry Smith       PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_log_separate"));
338f1580f4eSBarry Smith       PetscCall(PetscOptionsBool(prefix, "Log events level by level instead of inside PCSetUp()/KSPSolve()", NULL, data->log_separate, &data->log_separate, NULL));
339f1580f4eSBarry Smith     }
340f1580f4eSBarry Smith   }
341f1580f4eSBarry Smith   PetscOptionsHeadEnd();
342f1580f4eSBarry Smith   while (i < PETSC_PCHPDDM_MAXLEVELS && data->levels[i]) PetscCall(PetscFree(data->levels[i++]));
3433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
344f1580f4eSBarry Smith }
345f1580f4eSBarry Smith 
346d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_HPDDM(PC pc, Vec x, Vec y)
347d71ae5a4SJacob Faibussowitsch {
348f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
349f1580f4eSBarry Smith 
350f1580f4eSBarry Smith   PetscFunctionBegin;
351f1580f4eSBarry Smith   PetscCall(PetscCitationsRegister(HPDDMCitation, &HPDDMCite));
352f1580f4eSBarry Smith   PetscCheck(data->levels[0]->ksp, PETSC_COMM_SELF, PETSC_ERR_PLIB, "No KSP attached to PCHPDDM");
353f1580f4eSBarry Smith   if (data->log_separate) PetscCall(PetscLogEventBegin(PC_HPDDM_Solve[0], data->levels[0]->ksp, 0, 0, 0)); /* coarser-level events are directly triggered in HPDDM */
354f1580f4eSBarry Smith   PetscCall(KSPSolve(data->levels[0]->ksp, x, y));
355f1580f4eSBarry Smith   if (data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_Solve[0], data->levels[0]->ksp, 0, 0, 0));
3563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
357f1580f4eSBarry Smith }
358f1580f4eSBarry Smith 
359d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_HPDDM(PC pc, Mat X, Mat Y)
360d71ae5a4SJacob Faibussowitsch {
361f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
362f1580f4eSBarry Smith 
363f1580f4eSBarry Smith   PetscFunctionBegin;
364f1580f4eSBarry Smith   PetscCall(PetscCitationsRegister(HPDDMCitation, &HPDDMCite));
365f1580f4eSBarry Smith   PetscCheck(data->levels[0]->ksp, PETSC_COMM_SELF, PETSC_ERR_PLIB, "No KSP attached to PCHPDDM");
366f1580f4eSBarry Smith   PetscCall(KSPMatSolve(data->levels[0]->ksp, X, Y));
3673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
368f1580f4eSBarry Smith }
369f1580f4eSBarry Smith 
370f1580f4eSBarry Smith /*@C
371f1580f4eSBarry Smith      PCHPDDMGetComplexities - Computes the grid and operator complexities.
372f1580f4eSBarry Smith 
373f1580f4eSBarry Smith    Input Parameter:
374f1580f4eSBarry Smith .     pc - preconditioner context
375f1580f4eSBarry Smith 
376f1580f4eSBarry Smith    Output Parameters:
377f1580f4eSBarry Smith +     gc - grid complexity = sum_i(m_i) / m_1
378f1580f4eSBarry Smith -     oc - operator complexity = sum_i(nnz_i) / nnz_1
379f1580f4eSBarry Smith 
380f1580f4eSBarry Smith    Level: advanced
381f1580f4eSBarry Smith 
382f1580f4eSBarry Smith .seealso: `PCMGGetGridComplexity()`, `PCHPDDM`, `PCHYPRE`, `PCGAMG`
383f1580f4eSBarry Smith @*/
384d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMGetComplexities(PC pc, PetscReal *gc, PetscReal *oc)
385d71ae5a4SJacob Faibussowitsch {
386f1580f4eSBarry Smith   PC_HPDDM      *data = (PC_HPDDM *)pc->data;
387f1580f4eSBarry Smith   MatInfo        info;
388f1580f4eSBarry Smith   PetscInt       n, m;
389f1580f4eSBarry Smith   PetscLogDouble accumulate[2]{}, nnz1 = 1.0, m1 = 1.0;
390f1580f4eSBarry Smith 
391f1580f4eSBarry Smith   PetscFunctionBegin;
392f1580f4eSBarry Smith   for (n = 0, *gc = 0, *oc = 0; n < data->N; ++n) {
393f1580f4eSBarry Smith     if (data->levels[n]->ksp) {
394f1580f4eSBarry Smith       Mat P, A;
395f1580f4eSBarry Smith       PetscCall(KSPGetOperators(data->levels[n]->ksp, NULL, &P));
396f1580f4eSBarry Smith       PetscCall(MatGetSize(P, &m, NULL));
397f1580f4eSBarry Smith       accumulate[0] += m;
398f1580f4eSBarry Smith       if (n == 0) {
399f1580f4eSBarry Smith         PetscBool flg;
400f1580f4eSBarry Smith         PetscCall(PetscObjectTypeCompareAny((PetscObject)P, &flg, MATNORMAL, MATNORMALHERMITIAN, ""));
401f1580f4eSBarry Smith         if (flg) {
402f1580f4eSBarry Smith           PetscCall(MatConvert(P, MATAIJ, MAT_INITIAL_MATRIX, &A));
403f1580f4eSBarry Smith           P = A;
404f1580f4eSBarry Smith         } else PetscCall(PetscObjectReference((PetscObject)P));
405f1580f4eSBarry Smith       }
406f1580f4eSBarry Smith       if (P->ops->getinfo) {
407f1580f4eSBarry Smith         PetscCall(MatGetInfo(P, MAT_GLOBAL_SUM, &info));
408f1580f4eSBarry Smith         accumulate[1] += info.nz_used;
409f1580f4eSBarry Smith       }
410f1580f4eSBarry Smith       if (n == 0) {
411f1580f4eSBarry Smith         m1 = m;
412f1580f4eSBarry Smith         if (P->ops->getinfo) nnz1 = info.nz_used;
413f1580f4eSBarry Smith         PetscCall(MatDestroy(&P));
414f1580f4eSBarry Smith       }
415f1580f4eSBarry Smith     }
416f1580f4eSBarry Smith   }
417f1580f4eSBarry Smith   *gc = static_cast<PetscReal>(accumulate[0] / m1);
418f1580f4eSBarry Smith   *oc = static_cast<PetscReal>(accumulate[1] / nnz1);
4193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
420f1580f4eSBarry Smith }
421f1580f4eSBarry Smith 
422d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_HPDDM(PC pc, PetscViewer viewer)
423d71ae5a4SJacob Faibussowitsch {
424f1580f4eSBarry Smith   PC_HPDDM    *data = (PC_HPDDM *)pc->data;
425f1580f4eSBarry Smith   PetscViewer  subviewer;
426f1580f4eSBarry Smith   PetscSubcomm subcomm;
427f1580f4eSBarry Smith   PetscReal    oc, gc;
428f1580f4eSBarry Smith   PetscInt     i, tabs;
429f1580f4eSBarry Smith   PetscMPIInt  size, color, rank;
430f1580f4eSBarry Smith   PetscBool    ascii;
431f1580f4eSBarry Smith 
432f1580f4eSBarry Smith   PetscFunctionBegin;
433f1580f4eSBarry Smith   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &ascii));
434f1580f4eSBarry Smith   if (ascii) {
435f1580f4eSBarry Smith     PetscCall(PetscViewerASCIIPrintf(viewer, "level%s: %" PetscInt_FMT "\n", data->N > 1 ? "s" : "", data->N));
436f1580f4eSBarry Smith     PetscCall(PCHPDDMGetComplexities(pc, &gc, &oc));
437f1580f4eSBarry Smith     if (data->N > 1) {
438f1580f4eSBarry Smith       if (!data->deflation) {
439c8ea6600SPierre Jolivet         PetscCall(PetscViewerASCIIPrintf(viewer, "Neumann matrix attached? %s\n", PetscBools[PetscBool3ToBool(data->Neumann)]));
440f1580f4eSBarry Smith         PetscCall(PetscViewerASCIIPrintf(viewer, "shared subdomain KSP between SLEPc and PETSc? %s\n", PetscBools[data->share]));
441f1580f4eSBarry Smith       } else PetscCall(PetscViewerASCIIPrintf(viewer, "user-supplied deflation matrix\n"));
442f1580f4eSBarry Smith       PetscCall(PetscViewerASCIIPrintf(viewer, "coarse correction: %s\n", PCHPDDMCoarseCorrectionTypes[data->correction]));
443f1580f4eSBarry Smith       PetscCall(PetscViewerASCIIPrintf(viewer, "on process #0, value%s (+ threshold%s if available) for selecting deflation vectors:", data->N > 2 ? "s" : "", data->N > 2 ? "s" : ""));
444f1580f4eSBarry Smith       PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
445f1580f4eSBarry Smith       PetscCall(PetscViewerASCIISetTab(viewer, 0));
446f1580f4eSBarry Smith       for (i = 1; i < data->N; ++i) {
447f1580f4eSBarry Smith         PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, data->levels[i - 1]->nu));
448f1580f4eSBarry Smith         if (data->levels[i - 1]->threshold > -0.1) PetscCall(PetscViewerASCIIPrintf(viewer, " (%g)", (double)data->levels[i - 1]->threshold));
449f1580f4eSBarry Smith       }
450f1580f4eSBarry Smith       PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
451f1580f4eSBarry Smith       PetscCall(PetscViewerASCIISetTab(viewer, tabs));
452f1580f4eSBarry Smith     }
453f1580f4eSBarry Smith     PetscCall(PetscViewerASCIIPrintf(viewer, "grid and operator complexities: %g %g\n", (double)gc, (double)oc));
454f1580f4eSBarry Smith     if (data->levels[0]->ksp) {
455f1580f4eSBarry Smith       PetscCall(KSPView(data->levels[0]->ksp, viewer));
456f1580f4eSBarry Smith       if (data->levels[0]->pc) PetscCall(PCView(data->levels[0]->pc, viewer));
457f1580f4eSBarry Smith       for (i = 1; i < data->N; ++i) {
458f1580f4eSBarry Smith         if (data->levels[i]->ksp) color = 1;
459f1580f4eSBarry Smith         else color = 0;
460f1580f4eSBarry Smith         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
461f1580f4eSBarry Smith         PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
462f1580f4eSBarry Smith         PetscCall(PetscSubcommCreate(PetscObjectComm((PetscObject)pc), &subcomm));
463f1580f4eSBarry Smith         PetscCall(PetscSubcommSetNumber(subcomm, PetscMin(size, 2)));
464f1580f4eSBarry Smith         PetscCall(PetscSubcommSetTypeGeneral(subcomm, color, rank));
465f1580f4eSBarry Smith         PetscCall(PetscViewerASCIIPushTab(viewer));
466f1580f4eSBarry Smith         PetscCall(PetscViewerGetSubViewer(viewer, PetscSubcommChild(subcomm), &subviewer));
467f1580f4eSBarry Smith         if (color == 1) {
468f1580f4eSBarry Smith           PetscCall(KSPView(data->levels[i]->ksp, subviewer));
469f1580f4eSBarry Smith           if (data->levels[i]->pc) PetscCall(PCView(data->levels[i]->pc, subviewer));
470f1580f4eSBarry Smith           PetscCall(PetscViewerFlush(subviewer));
471f1580f4eSBarry Smith         }
472f1580f4eSBarry Smith         PetscCall(PetscViewerRestoreSubViewer(viewer, PetscSubcommChild(subcomm), &subviewer));
473f1580f4eSBarry Smith         PetscCall(PetscViewerASCIIPopTab(viewer));
474f1580f4eSBarry Smith         PetscCall(PetscSubcommDestroy(&subcomm));
475f1580f4eSBarry Smith         PetscCall(PetscViewerFlush(viewer));
476f1580f4eSBarry Smith       }
477f1580f4eSBarry Smith     }
478f1580f4eSBarry Smith   }
4793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
480f1580f4eSBarry Smith }
481f1580f4eSBarry Smith 
482d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPreSolve_HPDDM(PC pc, KSP ksp, Vec, Vec)
483d71ae5a4SJacob Faibussowitsch {
484f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
485f1580f4eSBarry Smith   PetscBool flg;
486f1580f4eSBarry Smith   Mat       A;
487f1580f4eSBarry Smith 
488f1580f4eSBarry Smith   PetscFunctionBegin;
489f1580f4eSBarry Smith   if (ksp) {
490f1580f4eSBarry Smith     PetscCall(PetscObjectTypeCompare((PetscObject)ksp, KSPLSQR, &flg));
491f1580f4eSBarry Smith     if (flg && !data->normal) {
492f1580f4eSBarry Smith       PetscCall(KSPGetOperators(ksp, &A, NULL));
493f1580f4eSBarry Smith       PetscCall(MatCreateVecs(A, NULL, &data->normal)); /* temporary Vec used in PCApply_HPDDMShell() for coarse grid corrections */
494f1580f4eSBarry Smith     }
495f1580f4eSBarry Smith   }
4963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
497f1580f4eSBarry Smith }
498f1580f4eSBarry Smith 
499d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_HPDDMShell(PC pc)
500d71ae5a4SJacob Faibussowitsch {
501f1580f4eSBarry Smith   PC_HPDDM_Level *ctx;
502f1580f4eSBarry Smith   Mat             A, P;
503f1580f4eSBarry Smith   Vec             x;
504f1580f4eSBarry Smith   const char     *pcpre;
505f1580f4eSBarry Smith 
506f1580f4eSBarry Smith   PetscFunctionBegin;
507f1580f4eSBarry Smith   PetscCall(PCShellGetContext(pc, &ctx));
508f1580f4eSBarry Smith   PetscCall(KSPGetOptionsPrefix(ctx->ksp, &pcpre));
509f1580f4eSBarry Smith   PetscCall(KSPGetOperators(ctx->ksp, &A, &P));
510f1580f4eSBarry Smith   /* smoother */
511f1580f4eSBarry Smith   PetscCall(PCSetOptionsPrefix(ctx->pc, pcpre));
512f1580f4eSBarry Smith   PetscCall(PCSetOperators(ctx->pc, A, P));
513f1580f4eSBarry Smith   if (!ctx->v[0]) {
514f1580f4eSBarry Smith     PetscCall(VecDuplicateVecs(ctx->D, 1, &ctx->v[0]));
515f1580f4eSBarry Smith     if (!std::is_same<PetscScalar, PetscReal>::value) PetscCall(VecDestroy(&ctx->D));
516f1580f4eSBarry Smith     PetscCall(MatCreateVecs(A, &x, NULL));
517f1580f4eSBarry Smith     PetscCall(VecDuplicateVecs(x, 2, &ctx->v[1]));
518f1580f4eSBarry Smith     PetscCall(VecDestroy(&x));
519f1580f4eSBarry Smith   }
520f1580f4eSBarry Smith   std::fill_n(ctx->V, 3, nullptr);
5213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
522f1580f4eSBarry Smith }
523f1580f4eSBarry Smith 
524f1580f4eSBarry Smith template <class Type, typename std::enable_if<std::is_same<Type, Vec>::value>::type * = nullptr>
525d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PCHPDDMDeflate_Private(PC pc, Type x, Type y)
526d71ae5a4SJacob Faibussowitsch {
527f1580f4eSBarry Smith   PC_HPDDM_Level *ctx;
528f1580f4eSBarry Smith   PetscScalar    *out;
529f1580f4eSBarry Smith 
530f1580f4eSBarry Smith   PetscFunctionBegin;
531f1580f4eSBarry Smith   PetscCall(PCShellGetContext(pc, &ctx));
532f1580f4eSBarry Smith   /* going from PETSc to HPDDM numbering */
533f1580f4eSBarry Smith   PetscCall(VecScatterBegin(ctx->scatter, x, ctx->v[0][0], INSERT_VALUES, SCATTER_FORWARD));
534f1580f4eSBarry Smith   PetscCall(VecScatterEnd(ctx->scatter, x, ctx->v[0][0], INSERT_VALUES, SCATTER_FORWARD));
535f1580f4eSBarry Smith   PetscCall(VecGetArrayWrite(ctx->v[0][0], &out));
536f1580f4eSBarry Smith   ctx->P->deflation<false>(NULL, out, 1); /* y = Q x */
537f1580f4eSBarry Smith   PetscCall(VecRestoreArrayWrite(ctx->v[0][0], &out));
538f1580f4eSBarry Smith   /* going from HPDDM to PETSc numbering */
539f1580f4eSBarry Smith   PetscCall(VecScatterBegin(ctx->scatter, ctx->v[0][0], y, INSERT_VALUES, SCATTER_REVERSE));
540f1580f4eSBarry Smith   PetscCall(VecScatterEnd(ctx->scatter, ctx->v[0][0], y, INSERT_VALUES, SCATTER_REVERSE));
5413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
542f1580f4eSBarry Smith }
543f1580f4eSBarry Smith 
544f1580f4eSBarry Smith template <class Type, typename std::enable_if<std::is_same<Type, Mat>::value>::type * = nullptr>
545d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PCHPDDMDeflate_Private(PC pc, Type X, Type Y)
546d71ae5a4SJacob Faibussowitsch {
547f1580f4eSBarry Smith   PC_HPDDM_Level *ctx;
548f1580f4eSBarry Smith   Vec             vX, vY, vC;
549f1580f4eSBarry Smith   PetscScalar    *out;
550f1580f4eSBarry Smith   PetscInt        i, N;
551f1580f4eSBarry Smith 
552f1580f4eSBarry Smith   PetscFunctionBegin;
553f1580f4eSBarry Smith   PetscCall(PCShellGetContext(pc, &ctx));
554f1580f4eSBarry Smith   PetscCall(MatGetSize(X, NULL, &N));
555f1580f4eSBarry Smith   /* going from PETSc to HPDDM numbering */
556f1580f4eSBarry Smith   for (i = 0; i < N; ++i) {
557f1580f4eSBarry Smith     PetscCall(MatDenseGetColumnVecRead(X, i, &vX));
558f1580f4eSBarry Smith     PetscCall(MatDenseGetColumnVecWrite(ctx->V[0], i, &vC));
559f1580f4eSBarry Smith     PetscCall(VecScatterBegin(ctx->scatter, vX, vC, INSERT_VALUES, SCATTER_FORWARD));
560f1580f4eSBarry Smith     PetscCall(VecScatterEnd(ctx->scatter, vX, vC, INSERT_VALUES, SCATTER_FORWARD));
561f1580f4eSBarry Smith     PetscCall(MatDenseRestoreColumnVecWrite(ctx->V[0], i, &vC));
562f1580f4eSBarry Smith     PetscCall(MatDenseRestoreColumnVecRead(X, i, &vX));
563f1580f4eSBarry Smith   }
564f1580f4eSBarry Smith   PetscCall(MatDenseGetArrayWrite(ctx->V[0], &out));
565f1580f4eSBarry Smith   ctx->P->deflation<false>(NULL, out, N); /* Y = Q X */
566f1580f4eSBarry Smith   PetscCall(MatDenseRestoreArrayWrite(ctx->V[0], &out));
567f1580f4eSBarry Smith   /* going from HPDDM to PETSc numbering */
568f1580f4eSBarry Smith   for (i = 0; i < N; ++i) {
569f1580f4eSBarry Smith     PetscCall(MatDenseGetColumnVecRead(ctx->V[0], i, &vC));
570f1580f4eSBarry Smith     PetscCall(MatDenseGetColumnVecWrite(Y, i, &vY));
571f1580f4eSBarry Smith     PetscCall(VecScatterBegin(ctx->scatter, vC, vY, INSERT_VALUES, SCATTER_REVERSE));
572f1580f4eSBarry Smith     PetscCall(VecScatterEnd(ctx->scatter, vC, vY, INSERT_VALUES, SCATTER_REVERSE));
573f1580f4eSBarry Smith     PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &vY));
574f1580f4eSBarry Smith     PetscCall(MatDenseRestoreColumnVecRead(ctx->V[0], i, &vC));
575f1580f4eSBarry Smith   }
5763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
577f1580f4eSBarry Smith }
578f1580f4eSBarry Smith 
579f1580f4eSBarry Smith /*
580f1580f4eSBarry Smith      PCApply_HPDDMShell - 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.
581f1580f4eSBarry Smith 
582f1580f4eSBarry Smith .vb
583f1580f4eSBarry Smith    (1) y =                Pmat^-1              x + Q x,
584f1580f4eSBarry Smith    (2) y =                Pmat^-1 (I - Amat Q) x + Q x (default),
585f1580f4eSBarry Smith    (3) y = (I - Q Amat^T) Pmat^-1 (I - Amat Q) x + Q x.
586f1580f4eSBarry Smith .ve
587f1580f4eSBarry Smith 
588f1580f4eSBarry Smith    Input Parameters:
589f1580f4eSBarry Smith +     pc - preconditioner context
590f1580f4eSBarry Smith -     x - input vector
591f1580f4eSBarry Smith 
592f1580f4eSBarry Smith    Output Parameter:
593f1580f4eSBarry Smith .     y - output vector
594f1580f4eSBarry Smith 
595f1580f4eSBarry Smith    Notes:
596f1580f4eSBarry Smith      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.
597f1580f4eSBarry Smith      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.
598f1580f4eSBarry Smith      (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.
599f1580f4eSBarry Smith 
600f1580f4eSBarry Smith    Level: advanced
601f1580f4eSBarry Smith 
602f1580f4eSBarry Smith    Developer Note:
603f1580f4eSBarry Smith    Since this is not an actual manual page the material below should be moved to an appropriate manual page with the appropriate context, i.e. explaining when it is used and how
604f1580f4eSBarry Smith    to trigger it. Likely the manual page is `PCHPDDM`
605f1580f4eSBarry Smith 
606f1580f4eSBarry Smith .seealso: `PCHPDDM`, `PCHPDDMCoarseCorrectionType`
607f1580f4eSBarry Smith */
608d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_HPDDMShell(PC pc, Vec x, Vec y)
609d71ae5a4SJacob Faibussowitsch {
610f1580f4eSBarry Smith   PC_HPDDM_Level *ctx;
611f1580f4eSBarry Smith   Mat             A;
612f1580f4eSBarry Smith 
613f1580f4eSBarry Smith   PetscFunctionBegin;
614f1580f4eSBarry Smith   PetscCall(PCShellGetContext(pc, &ctx));
615f1580f4eSBarry Smith   PetscCheck(ctx->P, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object");
616f1580f4eSBarry Smith   PetscCall(KSPGetOperators(ctx->ksp, &A, NULL));
617f1580f4eSBarry Smith   PetscCall(PCHPDDMDeflate_Private(pc, x, y)); /* y = Q x                          */
618f1580f4eSBarry Smith   if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
619f1580f4eSBarry Smith     if (!ctx->parent->normal || ctx != ctx->parent->levels[0]) PetscCall(MatMult(A, y, ctx->v[1][0])); /* y = A Q x */
620f1580f4eSBarry Smith     else { /* KSPLSQR and finest level */ PetscCall(MatMult(A, y, ctx->parent->normal));               /* y = A Q x                        */
621f1580f4eSBarry Smith       PetscCall(MatMultHermitianTranspose(A, ctx->parent->normal, ctx->v[1][0]));                      /* y = A^T A Q x    */
622f1580f4eSBarry Smith     }
623f1580f4eSBarry Smith     PetscCall(VecWAXPY(ctx->v[1][1], -1.0, ctx->v[1][0], x)); /* y = (I - A Q) x                  */
624f1580f4eSBarry Smith     PetscCall(PCApply(ctx->pc, ctx->v[1][1], ctx->v[1][0]));  /* y = M^-1 (I - A Q) x             */
625f1580f4eSBarry Smith     if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
626f1580f4eSBarry Smith       if (!ctx->parent->normal || ctx != ctx->parent->levels[0]) PetscCall(MatMultHermitianTranspose(A, ctx->v[1][0], ctx->v[1][1])); /* z = A^T y */
627f1580f4eSBarry Smith       else {
628f1580f4eSBarry Smith         PetscCall(MatMult(A, ctx->v[1][0], ctx->parent->normal));
629f1580f4eSBarry Smith         PetscCall(MatMultHermitianTranspose(A, ctx->parent->normal, ctx->v[1][1])); /* z = A^T A y    */
630f1580f4eSBarry Smith       }
631f1580f4eSBarry Smith       PetscCall(PCHPDDMDeflate_Private(pc, ctx->v[1][1], ctx->v[1][1]));
632f1580f4eSBarry Smith       PetscCall(VecAXPBYPCZ(y, -1.0, 1.0, 1.0, ctx->v[1][1], ctx->v[1][0])); /* y = (I - Q A^T) y + Q x */
633f1580f4eSBarry Smith     } else PetscCall(VecAXPY(y, 1.0, ctx->v[1][0]));                         /* y = Q M^-1 (I - A Q) x + Q x     */
634f1580f4eSBarry Smith   } else {
635f1580f4eSBarry Smith     PetscCheck(ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_ADDITIVE, PetscObjectComm((PetscObject)pc), PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with an unknown PCHPDDMCoarseCorrectionType %d", ctx->parent->correction);
636f1580f4eSBarry Smith     PetscCall(PCApply(ctx->pc, x, ctx->v[1][0]));
637f1580f4eSBarry Smith     PetscCall(VecAXPY(y, 1.0, ctx->v[1][0])); /* y = M^-1 x + Q x                 */
638f1580f4eSBarry Smith   }
6393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
640f1580f4eSBarry Smith }
641f1580f4eSBarry Smith 
642f1580f4eSBarry Smith /*
643f1580f4eSBarry Smith      PCMatApply_HPDDMShell - Variant of PCApply_HPDDMShell() for blocks of vectors.
644f1580f4eSBarry Smith 
645f1580f4eSBarry Smith    Input Parameters:
646f1580f4eSBarry Smith +     pc - preconditioner context
647f1580f4eSBarry Smith -     X - block of input vectors
648f1580f4eSBarry Smith 
649f1580f4eSBarry Smith    Output Parameter:
650f1580f4eSBarry Smith .     Y - block of output vectors
651f1580f4eSBarry Smith 
652f1580f4eSBarry Smith    Level: advanced
653f1580f4eSBarry Smith 
654f1580f4eSBarry Smith .seealso: `PCHPDDM`, `PCApply_HPDDMShell()`, `PCHPDDMCoarseCorrectionType`
655f1580f4eSBarry Smith */
656d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_HPDDMShell(PC pc, Mat X, Mat Y)
657d71ae5a4SJacob Faibussowitsch {
658f1580f4eSBarry Smith   PC_HPDDM_Level *ctx;
659f1580f4eSBarry Smith   Mat             A, *ptr;
660f1580f4eSBarry Smith   PetscContainer  container = NULL;
661f1580f4eSBarry Smith   PetscScalar    *array;
662f1580f4eSBarry Smith   PetscInt        m, M, N, prev = 0;
663f1580f4eSBarry Smith   PetscBool       reset = PETSC_FALSE;
664f1580f4eSBarry Smith 
665f1580f4eSBarry Smith   PetscFunctionBegin;
666f1580f4eSBarry Smith   PetscCall(PCShellGetContext(pc, &ctx));
667f1580f4eSBarry Smith   PetscCheck(ctx->P, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object");
668f1580f4eSBarry Smith   PetscCall(MatGetSize(X, NULL, &N));
669f1580f4eSBarry Smith   PetscCall(KSPGetOperators(ctx->ksp, &A, NULL));
670f1580f4eSBarry Smith   PetscCall(PetscObjectQuery((PetscObject)A, "_HPDDM_MatProduct", (PetscObject *)&container));
671f1580f4eSBarry Smith   if (container) { /* MatProduct container already attached */
672f1580f4eSBarry Smith     PetscCall(PetscContainerGetPointer(container, (void **)&ptr));
673f1580f4eSBarry Smith     if (ptr[1] != ctx->V[2]) /* Mat has changed or may have been set first in KSPHPDDM */
674f1580f4eSBarry Smith       for (m = 0; m < 2; ++m) {
675f1580f4eSBarry Smith         PetscCall(MatDestroy(ctx->V + m + 1));
676f1580f4eSBarry Smith         ctx->V[m + 1] = ptr[m];
677f1580f4eSBarry Smith         PetscCall(PetscObjectReference((PetscObject)ctx->V[m + 1]));
678f1580f4eSBarry Smith       }
679f1580f4eSBarry Smith   }
680f1580f4eSBarry Smith   if (ctx->V[1]) PetscCall(MatGetSize(ctx->V[1], NULL, &prev));
681f1580f4eSBarry Smith   if (N != prev || !ctx->V[0]) {
682f1580f4eSBarry Smith     PetscCall(MatDestroy(ctx->V));
683f1580f4eSBarry Smith     PetscCall(VecGetLocalSize(ctx->v[0][0], &m));
684f1580f4eSBarry Smith     PetscCall(MatCreateDense(PetscObjectComm((PetscObject)pc), m, PETSC_DECIDE, PETSC_DECIDE, N, NULL, ctx->V));
685f1580f4eSBarry Smith     if (N != prev) {
686f1580f4eSBarry Smith       PetscCall(MatDestroy(ctx->V + 1));
687f1580f4eSBarry Smith       PetscCall(MatDestroy(ctx->V + 2));
688f1580f4eSBarry Smith       PetscCall(MatGetLocalSize(X, &m, NULL));
689f1580f4eSBarry Smith       PetscCall(MatGetSize(X, &M, NULL));
690f1580f4eSBarry Smith       if (ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) PetscCall(MatDenseGetArrayWrite(ctx->V[0], &array));
691f1580f4eSBarry Smith       else array = NULL;
692f1580f4eSBarry Smith       PetscCall(MatCreateDense(PetscObjectComm((PetscObject)pc), m, PETSC_DECIDE, M, N, array, ctx->V + 1));
693f1580f4eSBarry Smith       if (ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) PetscCall(MatDenseRestoreArrayWrite(ctx->V[0], &array));
694f1580f4eSBarry Smith       PetscCall(MatCreateDense(PetscObjectComm((PetscObject)pc), m, PETSC_DECIDE, M, N, NULL, ctx->V + 2));
695f1580f4eSBarry Smith       PetscCall(MatProductCreateWithMat(A, Y, NULL, ctx->V[1]));
696f1580f4eSBarry Smith       PetscCall(MatProductSetType(ctx->V[1], MATPRODUCT_AB));
697f1580f4eSBarry Smith       PetscCall(MatProductSetFromOptions(ctx->V[1]));
698f1580f4eSBarry Smith       PetscCall(MatProductSymbolic(ctx->V[1]));
699f1580f4eSBarry Smith       if (!container) { /* no MatProduct container attached, create one to be queried in KSPHPDDM or at the next call to PCMatApply() */
700f1580f4eSBarry Smith         PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)A), &container));
701f1580f4eSBarry Smith         PetscCall(PetscObjectCompose((PetscObject)A, "_HPDDM_MatProduct", (PetscObject)container));
702f1580f4eSBarry Smith       }
703f1580f4eSBarry Smith       PetscCall(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 */
704f1580f4eSBarry Smith     }
705f1580f4eSBarry Smith     if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
706f1580f4eSBarry Smith       PetscCall(MatProductCreateWithMat(A, ctx->V[1], NULL, ctx->V[2]));
707f1580f4eSBarry Smith       PetscCall(MatProductSetType(ctx->V[2], MATPRODUCT_AtB));
708f1580f4eSBarry Smith       PetscCall(MatProductSetFromOptions(ctx->V[2]));
709f1580f4eSBarry Smith       PetscCall(MatProductSymbolic(ctx->V[2]));
710f1580f4eSBarry Smith     }
711f1580f4eSBarry Smith     ctx->P->start(N);
712f1580f4eSBarry Smith   }
713f1580f4eSBarry Smith   if (N == prev || container) { /* when MatProduct container is attached, always need to MatProductReplaceMats() since KSPHPDDM may have replaced the Mat as well */
714f1580f4eSBarry Smith     PetscCall(MatProductReplaceMats(NULL, Y, NULL, ctx->V[1]));
715f1580f4eSBarry Smith     if (container && ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) {
716f1580f4eSBarry Smith       PetscCall(MatDenseGetArrayWrite(ctx->V[0], &array));
717f1580f4eSBarry Smith       PetscCall(MatDensePlaceArray(ctx->V[1], array));
718f1580f4eSBarry Smith       PetscCall(MatDenseRestoreArrayWrite(ctx->V[0], &array));
719f1580f4eSBarry Smith       reset = PETSC_TRUE;
720f1580f4eSBarry Smith     }
721f1580f4eSBarry Smith   }
722f1580f4eSBarry Smith   PetscCall(PCHPDDMDeflate_Private(pc, X, Y));
723f1580f4eSBarry Smith   if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
724f1580f4eSBarry Smith     PetscCall(MatProductNumeric(ctx->V[1]));
725f1580f4eSBarry Smith     PetscCall(MatCopy(ctx->V[1], ctx->V[2], SAME_NONZERO_PATTERN));
726f1580f4eSBarry Smith     PetscCall(MatAXPY(ctx->V[2], -1.0, X, SAME_NONZERO_PATTERN));
727f1580f4eSBarry Smith     PetscCall(PCMatApply(ctx->pc, ctx->V[2], ctx->V[1]));
728f1580f4eSBarry Smith     if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
729f1580f4eSBarry Smith       PetscCall(MatProductNumeric(ctx->V[2]));
730f1580f4eSBarry Smith       PetscCall(PCHPDDMDeflate_Private(pc, ctx->V[2], ctx->V[2]));
731f1580f4eSBarry Smith       PetscCall(MatAXPY(ctx->V[1], -1.0, ctx->V[2], SAME_NONZERO_PATTERN));
732f1580f4eSBarry Smith     }
733f1580f4eSBarry Smith     PetscCall(MatAXPY(Y, -1.0, ctx->V[1], SAME_NONZERO_PATTERN));
734f1580f4eSBarry Smith   } else {
735f1580f4eSBarry Smith     PetscCheck(ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_ADDITIVE, PetscObjectComm((PetscObject)pc), PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with an unknown PCHPDDMCoarseCorrectionType %d", ctx->parent->correction);
736f1580f4eSBarry Smith     PetscCall(PCMatApply(ctx->pc, X, ctx->V[1]));
737f1580f4eSBarry Smith     PetscCall(MatAXPY(Y, 1.0, ctx->V[1], SAME_NONZERO_PATTERN));
738f1580f4eSBarry Smith   }
739f1580f4eSBarry Smith   if (reset) PetscCall(MatDenseResetArray(ctx->V[1]));
7403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
741f1580f4eSBarry Smith }
742f1580f4eSBarry Smith 
743d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_HPDDMShell(PC pc)
744d71ae5a4SJacob Faibussowitsch {
745f1580f4eSBarry Smith   PC_HPDDM_Level *ctx;
746f1580f4eSBarry Smith   PetscContainer  container;
747f1580f4eSBarry Smith 
748f1580f4eSBarry Smith   PetscFunctionBegin;
749f1580f4eSBarry Smith   PetscCall(PCShellGetContext(pc, &ctx));
750f1580f4eSBarry Smith   PetscCall(HPDDM::Schwarz<PetscScalar>::destroy(ctx, PETSC_TRUE));
751f1580f4eSBarry Smith   PetscCall(VecDestroyVecs(1, &ctx->v[0]));
752f1580f4eSBarry Smith   PetscCall(VecDestroyVecs(2, &ctx->v[1]));
753f1580f4eSBarry Smith   PetscCall(PetscObjectQuery((PetscObject)(ctx->pc)->mat, "_HPDDM_MatProduct", (PetscObject *)&container));
754f1580f4eSBarry Smith   PetscCall(PetscContainerDestroy(&container));
755f1580f4eSBarry Smith   PetscCall(PetscObjectCompose((PetscObject)(ctx->pc)->mat, "_HPDDM_MatProduct", NULL));
756f1580f4eSBarry Smith   PetscCall(MatDestroy(ctx->V));
757f1580f4eSBarry Smith   PetscCall(MatDestroy(ctx->V + 1));
758f1580f4eSBarry Smith   PetscCall(MatDestroy(ctx->V + 2));
759f1580f4eSBarry Smith   PetscCall(VecDestroy(&ctx->D));
760f1580f4eSBarry Smith   PetscCall(VecScatterDestroy(&ctx->scatter));
761f1580f4eSBarry Smith   PetscCall(PCDestroy(&ctx->pc));
7623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
763f1580f4eSBarry Smith }
764f1580f4eSBarry Smith 
765d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSolve_Private(const PC_HPDDM_Level *ctx, PetscScalar *rhs, const unsigned short &mu)
766d71ae5a4SJacob Faibussowitsch {
767f1580f4eSBarry Smith   Mat      B, X;
768f1580f4eSBarry Smith   PetscInt n, N, j = 0;
769f1580f4eSBarry Smith 
770f1580f4eSBarry Smith   PetscFunctionBegin;
771f1580f4eSBarry Smith   PetscCall(KSPGetOperators(ctx->ksp, &B, NULL));
772f1580f4eSBarry Smith   PetscCall(MatGetLocalSize(B, &n, NULL));
773f1580f4eSBarry Smith   PetscCall(MatGetSize(B, &N, NULL));
774f1580f4eSBarry Smith   if (ctx->parent->log_separate) {
775f1580f4eSBarry Smith     j = std::distance(ctx->parent->levels, std::find(ctx->parent->levels, ctx->parent->levels + ctx->parent->N, ctx));
776f1580f4eSBarry Smith     PetscCall(PetscLogEventBegin(PC_HPDDM_Solve[j], ctx->ksp, 0, 0, 0));
777f1580f4eSBarry Smith   }
778f1580f4eSBarry Smith   if (mu == 1) {
779f1580f4eSBarry Smith     if (!ctx->ksp->vec_rhs) {
780f1580f4eSBarry Smith       PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)ctx->ksp), 1, n, N, NULL, &ctx->ksp->vec_rhs));
781f1580f4eSBarry Smith       PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)ctx->ksp), n, N, &ctx->ksp->vec_sol));
782f1580f4eSBarry Smith     }
783f1580f4eSBarry Smith     PetscCall(VecPlaceArray(ctx->ksp->vec_rhs, rhs));
784f1580f4eSBarry Smith     PetscCall(KSPSolve(ctx->ksp, NULL, NULL));
785f1580f4eSBarry Smith     PetscCall(VecCopy(ctx->ksp->vec_sol, ctx->ksp->vec_rhs));
786f1580f4eSBarry Smith     PetscCall(VecResetArray(ctx->ksp->vec_rhs));
787f1580f4eSBarry Smith   } else {
788f1580f4eSBarry Smith     PetscCall(MatCreateDense(PetscObjectComm((PetscObject)ctx->ksp), n, PETSC_DECIDE, N, mu, rhs, &B));
789f1580f4eSBarry Smith     PetscCall(MatCreateDense(PetscObjectComm((PetscObject)ctx->ksp), n, PETSC_DECIDE, N, mu, NULL, &X));
790f1580f4eSBarry Smith     PetscCall(KSPMatSolve(ctx->ksp, B, X));
791f1580f4eSBarry Smith     PetscCall(MatCopy(X, B, SAME_NONZERO_PATTERN));
792f1580f4eSBarry Smith     PetscCall(MatDestroy(&X));
793f1580f4eSBarry Smith     PetscCall(MatDestroy(&B));
794f1580f4eSBarry Smith   }
795f1580f4eSBarry Smith   if (ctx->parent->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_Solve[j], ctx->ksp, 0, 0, 0));
7963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
797f1580f4eSBarry Smith }
798f1580f4eSBarry Smith 
799d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetUpNeumannOverlap_Private(PC pc)
800d71ae5a4SJacob Faibussowitsch {
801f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
802f1580f4eSBarry Smith 
803f1580f4eSBarry Smith   PetscFunctionBegin;
804f1580f4eSBarry Smith   if (data->setup) {
805f1580f4eSBarry Smith     Mat       P;
806f1580f4eSBarry Smith     Vec       x, xt = NULL;
807f1580f4eSBarry Smith     PetscReal t = 0.0, s = 0.0;
808f1580f4eSBarry Smith 
809f1580f4eSBarry Smith     PetscCall(PCGetOperators(pc, NULL, &P));
810f1580f4eSBarry Smith     PetscCall(PetscObjectQuery((PetscObject)P, "__SNES_latest_X", (PetscObject *)&x));
811f1580f4eSBarry Smith     PetscCallBack("PCHPDDM Neumann callback", (*data->setup)(data->aux, t, x, xt, s, data->is, data->setup_ctx));
812f1580f4eSBarry Smith   }
8133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
814f1580f4eSBarry Smith }
815f1580f4eSBarry Smith 
816d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMCreateSubMatrices_Private(Mat mat, PetscInt n, const IS *, const IS *, MatReuse scall, Mat *submat[])
817d71ae5a4SJacob Faibussowitsch {
818f1580f4eSBarry Smith   Mat A;
819f1580f4eSBarry Smith 
820f1580f4eSBarry Smith   PetscFunctionBegin;
821f1580f4eSBarry Smith   PetscCheck(n == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatCreateSubMatrices() called to extract %" PetscInt_FMT " submatrices, which is different than 1", n);
822f1580f4eSBarry Smith   /* previously composed Mat */
823f1580f4eSBarry Smith   PetscCall(PetscObjectQuery((PetscObject)mat, "_PCHPDDM_SubMatrices", (PetscObject *)&A));
824f1580f4eSBarry Smith   PetscCheck(A, PETSC_COMM_SELF, PETSC_ERR_PLIB, "SubMatrices not found in Mat");
825f1580f4eSBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
826f1580f4eSBarry Smith     PetscCall(PetscCalloc1(1, submat));
827f1580f4eSBarry Smith     PetscCall(MatDuplicate(A, MAT_COPY_VALUES, *submat));
828f1580f4eSBarry Smith   } else PetscCall(MatCopy(A, (*submat)[0], SAME_NONZERO_PATTERN));
8293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
830f1580f4eSBarry Smith }
831f1580f4eSBarry Smith 
832d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMCommunicationAvoidingPCASM_Private(PC pc, Mat C, PetscBool sorted)
833d71ae5a4SJacob Faibussowitsch {
834f1580f4eSBarry Smith   void (*op)(void);
835f1580f4eSBarry Smith 
836f1580f4eSBarry Smith   PetscFunctionBegin;
837f1580f4eSBarry Smith   /* previously-composed Mat */
838f1580f4eSBarry Smith   PetscCall(PetscObjectCompose((PetscObject)pc->pmat, "_PCHPDDM_SubMatrices", (PetscObject)C));
839f1580f4eSBarry Smith   PetscCall(MatGetOperation(pc->pmat, MATOP_CREATE_SUBMATRICES, &op));
840f1580f4eSBarry Smith   /* trick suggested by Barry https://lists.mcs.anl.gov/pipermail/petsc-dev/2020-January/025491.html */
841f1580f4eSBarry Smith   PetscCall(MatSetOperation(pc->pmat, MATOP_CREATE_SUBMATRICES, (void (*)(void))PCHPDDMCreateSubMatrices_Private));
842f1580f4eSBarry Smith   if (sorted) PetscCall(PCASMSetSortIndices(pc, PETSC_FALSE)); /* everything is already sorted */
843f1580f4eSBarry Smith   PetscCall(PCSetFromOptions(pc));                             /* otherwise -pc_hpddm_levels_1_pc_asm_sub_mat_type is not used */
844f1580f4eSBarry Smith   PetscCall(PCSetUp(pc));
845f1580f4eSBarry Smith   /* reset MatCreateSubMatrices() */
846f1580f4eSBarry Smith   PetscCall(MatSetOperation(pc->pmat, MATOP_CREATE_SUBMATRICES, op));
847f1580f4eSBarry Smith   PetscCall(PetscObjectCompose((PetscObject)pc->pmat, "_PCHPDDM_SubMatrices", NULL));
8483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
849f1580f4eSBarry Smith }
850f1580f4eSBarry Smith 
851d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMPermute_Private(IS is, IS in_is, IS *out_is, Mat in_C, Mat *out_C, IS *p)
852d71ae5a4SJacob Faibussowitsch {
853f1580f4eSBarry Smith   IS                           perm;
854f1580f4eSBarry Smith   const PetscInt              *ptr;
855f1580f4eSBarry Smith   PetscInt                    *concatenate, size, n;
856f1580f4eSBarry Smith   std::map<PetscInt, PetscInt> order;
857f1580f4eSBarry Smith   PetscBool                    sorted;
858f1580f4eSBarry Smith 
859f1580f4eSBarry Smith   PetscFunctionBegin;
860f1580f4eSBarry Smith   PetscCall(ISSorted(is, &sorted));
861f1580f4eSBarry Smith   if (!sorted) {
862f1580f4eSBarry Smith     PetscCall(ISGetLocalSize(is, &size));
863f1580f4eSBarry Smith     PetscCall(ISGetIndices(is, &ptr));
864f1580f4eSBarry Smith     /* MatCreateSubMatrices(), called by PCASM, follows the global numbering of Pmat */
865f1580f4eSBarry Smith     for (n = 0; n < size; ++n) order.insert(std::make_pair(ptr[n], n));
866f1580f4eSBarry Smith     PetscCall(ISRestoreIndices(is, &ptr));
867f1580f4eSBarry Smith     if (out_C) {
868f1580f4eSBarry Smith       PetscCall(PetscMalloc1(size, &concatenate));
869f1580f4eSBarry Smith       for (const std::pair<const PetscInt, PetscInt> &i : order) *concatenate++ = i.second;
870f1580f4eSBarry Smith       concatenate -= size;
871f1580f4eSBarry Smith       PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)in_C), size, concatenate, PETSC_OWN_POINTER, &perm));
872f1580f4eSBarry Smith       PetscCall(ISSetPermutation(perm));
873f1580f4eSBarry Smith       /* permute user-provided Mat so that it matches with MatCreateSubMatrices() numbering */
874f1580f4eSBarry Smith       PetscCall(MatPermute(in_C, perm, perm, out_C));
875f1580f4eSBarry Smith       if (p) *p = perm;
876f1580f4eSBarry Smith       else PetscCall(ISDestroy(&perm)); /* no need to save the permutation */
877f1580f4eSBarry Smith     }
878f1580f4eSBarry Smith     if (out_is) {
879f1580f4eSBarry Smith       PetscCall(PetscMalloc1(size, &concatenate));
880f1580f4eSBarry Smith       for (const std::pair<const PetscInt, PetscInt> &i : order) *concatenate++ = i.first;
881f1580f4eSBarry Smith       concatenate -= size;
882f1580f4eSBarry Smith       /* permute user-provided IS so that it matches with MatCreateSubMatrices() numbering */
883f1580f4eSBarry Smith       PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)in_is), size, concatenate, PETSC_OWN_POINTER, out_is));
884f1580f4eSBarry Smith     }
885f1580f4eSBarry Smith   } else { /* input IS is sorted, nothing to permute, simply duplicate inputs when needed */
886f1580f4eSBarry Smith     if (out_C) PetscCall(MatDuplicate(in_C, MAT_COPY_VALUES, out_C));
887f1580f4eSBarry Smith     if (out_is) PetscCall(ISDuplicate(in_is, out_is));
888f1580f4eSBarry Smith   }
8893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
890f1580f4eSBarry Smith }
891f1580f4eSBarry Smith 
892d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMDestroySubMatrices_Private(PetscBool flg, PetscBool algebraic, Mat *sub)
893d71ae5a4SJacob Faibussowitsch {
894f1580f4eSBarry Smith   IS is;
895f1580f4eSBarry Smith 
896f1580f4eSBarry Smith   PetscFunctionBegin;
897f1580f4eSBarry Smith   if (!flg) {
898f1580f4eSBarry Smith     if (algebraic) {
899f1580f4eSBarry Smith       PetscCall(PetscObjectQuery((PetscObject)sub[0], "_PCHPDDM_Embed", (PetscObject *)&is));
900f1580f4eSBarry Smith       PetscCall(ISDestroy(&is));
901f1580f4eSBarry Smith       PetscCall(PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Embed", NULL));
902f1580f4eSBarry Smith       PetscCall(PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Compact", NULL));
903f1580f4eSBarry Smith     }
904f1580f4eSBarry Smith     PetscCall(MatDestroySubMatrices(algebraic ? 2 : 1, &sub));
905f1580f4eSBarry Smith   }
9063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
907f1580f4eSBarry Smith }
908f1580f4eSBarry Smith 
909d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMAlgebraicAuxiliaryMat_Private(Mat P, IS *is, Mat *sub[], PetscBool block)
910d71ae5a4SJacob Faibussowitsch {
911f1580f4eSBarry Smith   IS         icol[3], irow[2];
912f1580f4eSBarry Smith   Mat       *M, Q;
913f1580f4eSBarry Smith   PetscReal *ptr;
914f1580f4eSBarry Smith   PetscInt  *idx, p = 0, n, bs = PetscAbs(P->cmap->bs);
915f1580f4eSBarry Smith   PetscBool  flg;
916f1580f4eSBarry Smith 
917f1580f4eSBarry Smith   PetscFunctionBegin;
918f1580f4eSBarry Smith   PetscCall(ISCreateStride(PETSC_COMM_SELF, P->cmap->N, 0, 1, icol + 2));
919f1580f4eSBarry Smith   PetscCall(ISSetBlockSize(icol[2], bs));
920f1580f4eSBarry Smith   PetscCall(ISSetIdentity(icol[2]));
921f1580f4eSBarry Smith   PetscCall(PetscObjectTypeCompare((PetscObject)P, MATMPISBAIJ, &flg));
922f1580f4eSBarry Smith   if (flg) {
923f1580f4eSBarry Smith     /* MatCreateSubMatrices() does not handle MATMPISBAIJ properly when iscol != isrow, so convert first to MATMPIBAIJ */
924f1580f4eSBarry Smith     PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &Q));
925f1580f4eSBarry Smith     std::swap(P, Q);
926f1580f4eSBarry Smith   }
927f1580f4eSBarry Smith   PetscCall(MatCreateSubMatrices(P, 1, is, icol + 2, MAT_INITIAL_MATRIX, &M));
928f1580f4eSBarry Smith   if (flg) {
929f1580f4eSBarry Smith     std::swap(P, Q);
930f1580f4eSBarry Smith     PetscCall(MatDestroy(&Q));
931f1580f4eSBarry Smith   }
932f1580f4eSBarry Smith   PetscCall(ISDestroy(icol + 2));
933f1580f4eSBarry Smith   PetscCall(ISCreateStride(PETSC_COMM_SELF, M[0]->rmap->N, 0, 1, irow));
934f1580f4eSBarry Smith   PetscCall(ISSetBlockSize(irow[0], bs));
935f1580f4eSBarry Smith   PetscCall(ISSetIdentity(irow[0]));
936f1580f4eSBarry Smith   if (!block) {
937f1580f4eSBarry Smith     PetscCall(PetscMalloc2(P->cmap->N, &ptr, P->cmap->N, &idx));
938f1580f4eSBarry Smith     PetscCall(MatGetColumnNorms(M[0], NORM_INFINITY, ptr));
939f1580f4eSBarry Smith     /* check for nonzero columns so that M[0] may be expressed in compact form */
940f1580f4eSBarry Smith     for (n = 0; n < P->cmap->N; n += bs)
941f1580f4eSBarry Smith       if (std::find_if(ptr + n, ptr + n + bs, [](PetscReal v) { return v > PETSC_MACHINE_EPSILON; }) != ptr + n + bs) {
942f1580f4eSBarry Smith         std::iota(idx + p, idx + p + bs, n);
943f1580f4eSBarry Smith         p += bs;
944f1580f4eSBarry Smith       }
945f1580f4eSBarry Smith     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, p, idx, PETSC_USE_POINTER, icol + 1));
946f1580f4eSBarry Smith     PetscCall(ISSetBlockSize(icol[1], bs));
947f1580f4eSBarry Smith     PetscCall(ISSetInfo(icol[1], IS_SORTED, IS_GLOBAL, PETSC_TRUE, PETSC_TRUE));
948f1580f4eSBarry Smith     PetscCall(ISEmbed(*is, icol[1], PETSC_FALSE, icol + 2));
949f1580f4eSBarry Smith     irow[1] = irow[0];
950f1580f4eSBarry Smith     /* 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 */
951f1580f4eSBarry Smith     icol[0] = is[0];
952f1580f4eSBarry Smith     PetscCall(MatCreateSubMatrices(M[0], 2, irow, icol, MAT_INITIAL_MATRIX, sub));
953f1580f4eSBarry Smith     PetscCall(ISDestroy(icol + 1));
954f1580f4eSBarry Smith     PetscCall(PetscFree2(ptr, idx));
955f1580f4eSBarry Smith     /* IS used to go back and forth between the augmented and the original local linear system, see eq. (3.4) of [2022b] */
956f1580f4eSBarry Smith     PetscCall(PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Embed", (PetscObject)icol[2]));
957f1580f4eSBarry Smith     /* Mat used in eq. (3.1) of [2022b] */
958f1580f4eSBarry Smith     PetscCall(PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Compact", (PetscObject)(*sub)[1]));
959f1580f4eSBarry Smith   } else {
960f1580f4eSBarry Smith     Mat aux;
961f1580f4eSBarry Smith     PetscCall(MatSetOption(M[0], MAT_SUBMAT_SINGLEIS, PETSC_TRUE));
962f1580f4eSBarry Smith     /* diagonal block of the overlapping rows */
963f1580f4eSBarry Smith     PetscCall(MatCreateSubMatrices(M[0], 1, irow, is, MAT_INITIAL_MATRIX, sub));
964f1580f4eSBarry Smith     PetscCall(MatDuplicate((*sub)[0], MAT_COPY_VALUES, &aux));
965f1580f4eSBarry Smith     PetscCall(MatSetOption(aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
966f1580f4eSBarry Smith     if (bs == 1) { /* scalar case */
967f1580f4eSBarry Smith       Vec sum[2];
968f1580f4eSBarry Smith       PetscCall(MatCreateVecs(aux, sum, sum + 1));
969f1580f4eSBarry Smith       PetscCall(MatGetRowSum(M[0], sum[0]));
970f1580f4eSBarry Smith       PetscCall(MatGetRowSum(aux, sum[1]));
971f1580f4eSBarry Smith       /* off-diagonal block row sum (full rows - diagonal block rows) */
972f1580f4eSBarry Smith       PetscCall(VecAXPY(sum[0], -1.0, sum[1]));
973f1580f4eSBarry Smith       /* subdomain matrix plus off-diagonal block row sum */
974f1580f4eSBarry Smith       PetscCall(MatDiagonalSet(aux, sum[0], ADD_VALUES));
975f1580f4eSBarry Smith       PetscCall(VecDestroy(sum));
976f1580f4eSBarry Smith       PetscCall(VecDestroy(sum + 1));
977f1580f4eSBarry Smith     } else { /* vectorial case */
978f1580f4eSBarry Smith       /* TODO: missing MatGetValuesBlocked(), so the code below is     */
979f1580f4eSBarry Smith       /* an extension of the scalar case for when bs > 1, but it could */
980f1580f4eSBarry Smith       /* be more efficient by avoiding all these MatMatMult()          */
981f1580f4eSBarry Smith       Mat          sum[2], ones;
982f1580f4eSBarry Smith       PetscScalar *ptr;
983f1580f4eSBarry Smith       PetscCall(PetscCalloc1(M[0]->cmap->n * bs, &ptr));
984f1580f4eSBarry Smith       PetscCall(MatCreateDense(PETSC_COMM_SELF, M[0]->cmap->n, bs, M[0]->cmap->n, bs, ptr, &ones));
985f1580f4eSBarry Smith       for (n = 0; n < M[0]->cmap->n; n += bs) {
986f1580f4eSBarry Smith         for (p = 0; p < bs; ++p) ptr[n + p * (M[0]->cmap->n + 1)] = 1.0;
987f1580f4eSBarry Smith       }
988f1580f4eSBarry Smith       PetscCall(MatMatMult(M[0], ones, MAT_INITIAL_MATRIX, PETSC_DEFAULT, sum));
989f1580f4eSBarry Smith       PetscCall(MatDestroy(&ones));
990f1580f4eSBarry Smith       PetscCall(MatCreateDense(PETSC_COMM_SELF, aux->cmap->n, bs, aux->cmap->n, bs, ptr, &ones));
991f1580f4eSBarry Smith       PetscCall(MatDenseSetLDA(ones, M[0]->cmap->n));
992f1580f4eSBarry Smith       PetscCall(MatMatMult(aux, ones, MAT_INITIAL_MATRIX, PETSC_DEFAULT, sum + 1));
993f1580f4eSBarry Smith       PetscCall(MatDestroy(&ones));
994f1580f4eSBarry Smith       PetscCall(PetscFree(ptr));
995f1580f4eSBarry Smith       /* off-diagonal block row sum (full rows - diagonal block rows) */
996f1580f4eSBarry Smith       PetscCall(MatAXPY(sum[0], -1.0, sum[1], SAME_NONZERO_PATTERN));
997f1580f4eSBarry Smith       PetscCall(MatDestroy(sum + 1));
998f1580f4eSBarry Smith       /* re-order values to be consistent with MatSetValuesBlocked()           */
999f1580f4eSBarry Smith       /* equivalent to MatTranspose() which does not truly handle              */
1000f1580f4eSBarry Smith       /* MAT_INPLACE_MATRIX in the rectangular case, as it calls PetscMalloc() */
1001f1580f4eSBarry Smith       PetscCall(MatDenseGetArrayWrite(sum[0], &ptr));
1002f1580f4eSBarry Smith       HPDDM::Wrapper<PetscScalar>::imatcopy<'T'>(bs, sum[0]->rmap->n, ptr, sum[0]->rmap->n, bs);
1003f1580f4eSBarry Smith       /* subdomain matrix plus off-diagonal block row sum */
1004f1580f4eSBarry Smith       for (n = 0; n < aux->cmap->n / bs; ++n) PetscCall(MatSetValuesBlocked(aux, 1, &n, 1, &n, ptr + n * bs * bs, ADD_VALUES));
1005f1580f4eSBarry Smith       PetscCall(MatAssemblyBegin(aux, MAT_FINAL_ASSEMBLY));
1006f1580f4eSBarry Smith       PetscCall(MatAssemblyEnd(aux, MAT_FINAL_ASSEMBLY));
1007f1580f4eSBarry Smith       PetscCall(MatDenseRestoreArrayWrite(sum[0], &ptr));
1008f1580f4eSBarry Smith       PetscCall(MatDestroy(sum));
1009f1580f4eSBarry Smith     }
1010f1580f4eSBarry Smith     PetscCall(MatSetOption(aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
1011f1580f4eSBarry Smith     /* left-hand side of GenEO, with the same sparsity pattern as PCASM subdomain solvers  */
1012f1580f4eSBarry Smith     PetscCall(PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Neumann_Mat", (PetscObject)aux));
1013f1580f4eSBarry Smith   }
1014f1580f4eSBarry Smith   PetscCall(ISDestroy(irow));
1015f1580f4eSBarry Smith   PetscCall(MatDestroySubMatrices(1, &M));
10163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1017f1580f4eSBarry Smith }
1018f1580f4eSBarry Smith 
1019d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_HPDDM(PC pc)
1020d71ae5a4SJacob Faibussowitsch {
1021f1580f4eSBarry Smith   PC_HPDDM                 *data = (PC_HPDDM *)pc->data;
1022f1580f4eSBarry Smith   PC                        inner;
1023f1580f4eSBarry Smith   KSP                      *ksp;
1024f1580f4eSBarry Smith   Mat                      *sub, A, P, N, C = NULL, uaux = NULL, weighted, subA[2];
1025f1580f4eSBarry Smith   Vec                       xin, v;
1026f1580f4eSBarry Smith   std::vector<Vec>          initial;
1027398c7888SPierre Jolivet   IS                        is[1], loc, uis = data->is, unsorted = NULL;
1028f1580f4eSBarry Smith   ISLocalToGlobalMapping    l2g;
1029f1580f4eSBarry Smith   char                      prefix[256];
1030f1580f4eSBarry Smith   const char               *pcpre;
1031f1580f4eSBarry Smith   const PetscScalar *const *ev;
1032f1580f4eSBarry Smith   PetscInt                  n, requested = data->N, reused = 0;
1033f1580f4eSBarry Smith   MatStructure              structure  = UNKNOWN_NONZERO_PATTERN;
1034f1580f4eSBarry Smith   PetscBool                 subdomains = PETSC_FALSE, flg = PETSC_FALSE, ismatis, swap = PETSC_FALSE, algebraic = PETSC_FALSE, block = PETSC_FALSE;
1035f1580f4eSBarry Smith   DM                        dm;
1036398c7888SPierre Jolivet #if PetscDefined(USE_DEBUG)
1037398c7888SPierre Jolivet   IS  dis  = NULL;
1038398c7888SPierre Jolivet   Mat daux = NULL;
1039398c7888SPierre Jolivet #endif
1040f1580f4eSBarry Smith 
1041f1580f4eSBarry Smith   PetscFunctionBegin;
1042f1580f4eSBarry Smith   PetscCheck(data->levels && data->levels[0], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not a single level allocated");
1043f1580f4eSBarry Smith   PetscCall(PCGetOptionsPrefix(pc, &pcpre));
1044f1580f4eSBarry Smith   PetscCall(PCGetOperators(pc, &A, &P));
1045f1580f4eSBarry Smith   if (!data->levels[0]->ksp) {
1046f1580f4eSBarry Smith     PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->ksp));
1047f1580f4eSBarry Smith     PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_%s_", pcpre ? pcpre : "", data->N > 1 ? "levels_1" : "coarse"));
1048f1580f4eSBarry Smith     PetscCall(KSPSetOptionsPrefix(data->levels[0]->ksp, prefix));
1049f1580f4eSBarry Smith     PetscCall(KSPSetType(data->levels[0]->ksp, KSPPREONLY));
1050f1580f4eSBarry Smith   } else if (data->levels[0]->ksp->pc && data->levels[0]->ksp->pc->setupcalled == 1 && data->levels[0]->ksp->pc->reusepreconditioner) {
1051f1580f4eSBarry Smith     /* if the fine-level PCSHELL exists, its setup has succeeded, and one wants to reuse it, */
1052f1580f4eSBarry Smith     /* then just propagate the appropriate flag to the coarser levels                        */
1053f1580f4eSBarry Smith     for (n = 0; n < PETSC_PCHPDDM_MAXLEVELS && data->levels[n]; ++n) {
1054f1580f4eSBarry Smith       /* the following KSP and PC may be NULL for some processes, hence the check            */
1055f1580f4eSBarry Smith       if (data->levels[n]->ksp) PetscCall(KSPSetReusePreconditioner(data->levels[n]->ksp, PETSC_TRUE));
1056f1580f4eSBarry Smith       if (data->levels[n]->pc) PetscCall(PCSetReusePreconditioner(data->levels[n]->pc, PETSC_TRUE));
1057f1580f4eSBarry Smith     }
1058f1580f4eSBarry Smith     /* early bail out because there is nothing to do */
10593ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
1060f1580f4eSBarry Smith   } else {
1061f1580f4eSBarry Smith     /* reset coarser levels */
1062f1580f4eSBarry Smith     for (n = 1; n < PETSC_PCHPDDM_MAXLEVELS && data->levels[n]; ++n) {
1063f1580f4eSBarry Smith       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) {
1064f1580f4eSBarry Smith         reused = data->N - n;
1065f1580f4eSBarry Smith         break;
1066f1580f4eSBarry Smith       }
1067f1580f4eSBarry Smith       PetscCall(KSPDestroy(&data->levels[n]->ksp));
1068f1580f4eSBarry Smith       PetscCall(PCDestroy(&data->levels[n]->pc));
1069f1580f4eSBarry Smith     }
1070f1580f4eSBarry Smith     /* check if some coarser levels are being reused */
1071f1580f4eSBarry Smith     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &reused, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)pc)));
1072f1580f4eSBarry Smith     const int *addr = data->levels[0]->P ? data->levels[0]->P->getAddrLocal() : &HPDDM::i__0;
1073f1580f4eSBarry Smith 
1074f1580f4eSBarry Smith     if (addr != &HPDDM::i__0 && reused != data->N - 1) {
1075f1580f4eSBarry Smith       /* reuse previously computed eigenvectors */
1076f1580f4eSBarry Smith       ev = data->levels[0]->P->getVectors();
1077f1580f4eSBarry Smith       if (ev) {
1078f1580f4eSBarry Smith         initial.reserve(*addr);
1079f1580f4eSBarry Smith         PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, data->levels[0]->P->getDof(), ev[0], &xin));
1080f1580f4eSBarry Smith         for (n = 0; n < *addr; ++n) {
1081f1580f4eSBarry Smith           PetscCall(VecDuplicate(xin, &v));
1082f1580f4eSBarry Smith           PetscCall(VecPlaceArray(xin, ev[n]));
1083f1580f4eSBarry Smith           PetscCall(VecCopy(xin, v));
1084f1580f4eSBarry Smith           initial.emplace_back(v);
1085f1580f4eSBarry Smith           PetscCall(VecResetArray(xin));
1086f1580f4eSBarry Smith         }
1087f1580f4eSBarry Smith         PetscCall(VecDestroy(&xin));
1088f1580f4eSBarry Smith       }
1089f1580f4eSBarry Smith     }
1090f1580f4eSBarry Smith   }
1091f1580f4eSBarry Smith   data->N -= reused;
1092f1580f4eSBarry Smith   PetscCall(KSPSetOperators(data->levels[0]->ksp, A, P));
1093f1580f4eSBarry Smith 
1094f1580f4eSBarry Smith   PetscCall(PetscObjectTypeCompare((PetscObject)P, MATIS, &ismatis));
1095f1580f4eSBarry Smith   if (!data->is && !ismatis) {
1096f1580f4eSBarry Smith     PetscErrorCode (*create)(DM, IS *, Mat *, PetscErrorCode(**)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void **) = NULL;
1097f1580f4eSBarry Smith     PetscErrorCode (*usetup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *)                                               = NULL;
1098f1580f4eSBarry Smith     void *uctx                                                                                                              = NULL;
1099f1580f4eSBarry Smith 
1100f1580f4eSBarry Smith     /* first see if we can get the data from the DM */
1101f1580f4eSBarry Smith     PetscCall(MatGetDM(P, &dm));
1102f1580f4eSBarry Smith     if (!dm) PetscCall(MatGetDM(A, &dm));
1103f1580f4eSBarry Smith     if (!dm) PetscCall(PCGetDM(pc, &dm));
1104f1580f4eSBarry Smith     if (dm) { /* this is the hook for DMPLEX and DMDA for which the auxiliary Mat is the local Neumann matrix */
1105f1580f4eSBarry Smith       PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", &create));
1106f1580f4eSBarry Smith       if (create) {
1107f1580f4eSBarry Smith         PetscCall((*create)(dm, &uis, &uaux, &usetup, &uctx));
1108c8ea6600SPierre Jolivet         if (data->Neumann == PETSC_BOOL3_UNKNOWN) data->Neumann = PETSC_BOOL3_TRUE; /* set the value only if it was not already provided by the user */
1109f1580f4eSBarry Smith       }
1110f1580f4eSBarry Smith     }
1111f1580f4eSBarry Smith     if (!create) {
1112f1580f4eSBarry Smith       if (!uis) {
1113f1580f4eSBarry Smith         PetscCall(PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_IS", (PetscObject *)&uis));
1114f1580f4eSBarry Smith         PetscCall(PetscObjectReference((PetscObject)uis));
1115f1580f4eSBarry Smith       }
1116f1580f4eSBarry Smith       if (!uaux) {
1117f1580f4eSBarry Smith         PetscCall(PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_Mat", (PetscObject *)&uaux));
1118f1580f4eSBarry Smith         PetscCall(PetscObjectReference((PetscObject)uaux));
1119f1580f4eSBarry Smith       }
1120f1580f4eSBarry Smith       /* look inside the Pmat instead of the PC, needed for MatSchurComplementComputeExplicitOperator() */
1121f1580f4eSBarry Smith       if (!uis) {
1122f1580f4eSBarry Smith         PetscCall(PetscObjectQuery((PetscObject)P, "_PCHPDDM_Neumann_IS", (PetscObject *)&uis));
1123f1580f4eSBarry Smith         PetscCall(PetscObjectReference((PetscObject)uis));
1124f1580f4eSBarry Smith       }
1125f1580f4eSBarry Smith       if (!uaux) {
1126f1580f4eSBarry Smith         PetscCall(PetscObjectQuery((PetscObject)P, "_PCHPDDM_Neumann_Mat", (PetscObject *)&uaux));
1127f1580f4eSBarry Smith         PetscCall(PetscObjectReference((PetscObject)uaux));
1128f1580f4eSBarry Smith       }
1129f1580f4eSBarry Smith     }
1130f1580f4eSBarry Smith     PetscCall(PCHPDDMSetAuxiliaryMat(pc, uis, uaux, usetup, uctx));
1131f1580f4eSBarry Smith     PetscCall(MatDestroy(&uaux));
1132f1580f4eSBarry Smith     PetscCall(ISDestroy(&uis));
1133f1580f4eSBarry Smith   }
1134f1580f4eSBarry Smith 
1135f1580f4eSBarry Smith   if (!ismatis) {
1136f1580f4eSBarry Smith     PetscCall(PCHPDDMSetUpNeumannOverlap_Private(pc));
113702800ff6SPierre Jolivet     PetscCall(PetscOptionsGetBool(NULL, pcpre, "-pc_hpddm_block_splitting", &block, NULL));
113802800ff6SPierre Jolivet     if (data->is && block) {
113902800ff6SPierre Jolivet       PetscCall(ISDestroy(&data->is));
114002800ff6SPierre Jolivet       PetscCall(MatDestroy(&data->aux));
114102800ff6SPierre Jolivet     }
1142f1580f4eSBarry Smith     if (!data->is && data->N > 1) {
1143f1580f4eSBarry Smith       char type[256] = {}; /* same size as in src/ksp/pc/interface/pcset.c */
1144f1580f4eSBarry Smith       PetscCall(PetscObjectTypeCompareAny((PetscObject)P, &flg, MATNORMAL, MATNORMALHERMITIAN, ""));
1145f1580f4eSBarry Smith       if (flg || (A->rmap->N != A->cmap->N && P->rmap->N == P->cmap->N && P->rmap->N == A->cmap->N)) {
1146f1580f4eSBarry Smith         Mat B;
1147f1580f4eSBarry Smith         PetscCall(PCHPDDMSetAuxiliaryMatNormal_Private(pc, A, P, &B, pcpre));
1148f1580f4eSBarry Smith         if (data->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED) data->correction = PC_HPDDM_COARSE_CORRECTION_BALANCED;
1149f1580f4eSBarry Smith         PetscCall(MatDestroy(&B));
1150f1580f4eSBarry Smith       } else {
1151f1580f4eSBarry Smith         PetscCall(PetscObjectTypeCompare((PetscObject)P, MATSCHURCOMPLEMENT, &flg));
1152f1580f4eSBarry Smith         if (flg) {
1153f1580f4eSBarry Smith           Mat                        A00, P00, A01, A10, A11, B, N;
1154f1580f4eSBarry Smith           const PetscScalar         *array;
1155f1580f4eSBarry Smith           PetscReal                  norm;
1156f1580f4eSBarry Smith           MatSchurComplementAinvType type;
1157f1580f4eSBarry Smith 
1158f1580f4eSBarry Smith           PetscCall(MatSchurComplementGetSubMatrices(P, &A00, &P00, &A01, &A10, &A11));
1159f1580f4eSBarry Smith           if (A11) {
1160f1580f4eSBarry Smith             PetscCall(MatNorm(A11, NORM_INFINITY, &norm));
1161f1580f4eSBarry Smith             PetscCheck(norm < PETSC_SMALL, PetscObjectComm((PetscObject)P), PETSC_ERR_SUP, "Nonzero A11 block");
1162f1580f4eSBarry Smith           }
1163f1580f4eSBarry Smith           if (PetscDefined(USE_DEBUG)) {
1164f1580f4eSBarry Smith             Mat T, U = NULL;
1165f1580f4eSBarry Smith             IS  z;
1166f1580f4eSBarry Smith             PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATTRANSPOSEVIRTUAL, &flg));
1167f1580f4eSBarry Smith             if (flg) PetscCall(MatTransposeGetMat(A10, &U));
1168f1580f4eSBarry Smith             else {
1169f1580f4eSBarry Smith               PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATHERMITIANTRANSPOSEVIRTUAL, &flg));
1170f1580f4eSBarry Smith               if (flg) PetscCall(MatHermitianTransposeGetMat(A10, &U));
1171f1580f4eSBarry Smith             }
1172f1580f4eSBarry Smith             if (U) PetscCall(MatDuplicate(U, MAT_COPY_VALUES, &T));
1173f1580f4eSBarry Smith             else PetscCall(MatHermitianTranspose(A10, MAT_INITIAL_MATRIX, &T));
1174f1580f4eSBarry Smith             PetscCall(PetscLayoutCompare(T->rmap, A01->rmap, &flg));
1175f1580f4eSBarry Smith             if (flg) {
1176f1580f4eSBarry Smith               PetscCall(PetscLayoutCompare(T->cmap, A01->cmap, &flg));
1177f1580f4eSBarry Smith               if (flg) {
1178f1580f4eSBarry Smith                 PetscCall(MatFindZeroRows(A01, &z)); /* for essential boundary conditions, some implementations will */
1179f1580f4eSBarry Smith                 if (z) {                             /*  zero rows in [P00 A01] except for the diagonal of P00       */
1180f1580f4eSBarry Smith                   PetscCall(MatSetOption(T, MAT_NO_OFF_PROC_ZERO_ROWS, PETSC_TRUE));
1181f1580f4eSBarry Smith                   PetscCall(MatZeroRowsIS(T, z, 0.0, NULL, NULL)); /* corresponding zero rows from A01 */
1182f1580f4eSBarry Smith                   PetscCall(ISDestroy(&z));
1183f1580f4eSBarry Smith                 }
1184f1580f4eSBarry Smith                 PetscCall(MatMultEqual(A01, T, 10, &flg));
1185f1580f4eSBarry Smith                 PetscCheck(flg, PetscObjectComm((PetscObject)P), PETSC_ERR_SUP, "A01 != A10^T");
1186f1580f4eSBarry Smith               } else PetscCall(PetscInfo(pc, "A01 and A10^T have non-congruent column layouts, cannot test for equality\n"));
1187f1580f4eSBarry Smith             }
1188f1580f4eSBarry Smith             PetscCall(MatDestroy(&T));
1189f1580f4eSBarry Smith           }
1190f1580f4eSBarry Smith           PetscCall(MatCreateVecs(P00, &v, NULL));
1191f1580f4eSBarry Smith           PetscCall(MatSchurComplementGetAinvType(P, &type));
1192f1580f4eSBarry Smith           PetscCheck(type == MAT_SCHUR_COMPLEMENT_AINV_DIAG || type == MAT_SCHUR_COMPLEMENT_AINV_LUMP, PetscObjectComm((PetscObject)P), PETSC_ERR_SUP, "-%smat_schur_complement_ainv_type %s", ((PetscObject)P)->prefix ? ((PetscObject)P)->prefix : "", MatSchurComplementAinvTypes[type]);
1193f1580f4eSBarry Smith           if (type == MAT_SCHUR_COMPLEMENT_AINV_LUMP) {
1194f1580f4eSBarry Smith             PetscCall(MatGetRowSum(P00, v));
1195f1580f4eSBarry Smith             if (A00 == P00) PetscCall(PetscObjectReference((PetscObject)A00));
1196f1580f4eSBarry Smith             PetscCall(MatDestroy(&P00));
1197f1580f4eSBarry Smith             PetscCall(VecGetArrayRead(v, &array));
1198f1580f4eSBarry Smith             PetscCall(MatCreateAIJ(PetscObjectComm((PetscObject)A00), A00->rmap->n, A00->cmap->n, A00->rmap->N, A00->cmap->N, 1, NULL, 0, NULL, &P00));
1199f1580f4eSBarry Smith             PetscCall(MatSetOption(P00, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
1200f1580f4eSBarry Smith             for (n = A00->rmap->rstart; n < A00->rmap->rend; ++n) PetscCall(MatSetValue(P00, n, n, array[n - A00->rmap->rstart], INSERT_VALUES));
1201f1580f4eSBarry Smith             PetscCall(MatAssemblyBegin(P00, MAT_FINAL_ASSEMBLY));
1202f1580f4eSBarry Smith             PetscCall(MatAssemblyEnd(P00, MAT_FINAL_ASSEMBLY));
1203f1580f4eSBarry Smith             PetscCall(VecRestoreArrayRead(v, &array));
1204f1580f4eSBarry Smith             PetscCall(MatSchurComplementUpdateSubMatrices(P, A00, P00, A01, A10, A11)); /* replace P00 by diag(sum of each row of P00) */
1205f1580f4eSBarry Smith             PetscCall(MatDestroy(&P00));
1206f1580f4eSBarry Smith           } else PetscCall(MatGetDiagonal(P00, v));
1207f1580f4eSBarry Smith           PetscCall(VecReciprocal(v)); /* inv(diag(P00))       */
1208f1580f4eSBarry Smith           PetscCall(VecSqrtAbs(v));    /* sqrt(inv(diag(P00))) */
1209f1580f4eSBarry Smith           PetscCall(MatDuplicate(A01, MAT_COPY_VALUES, &B));
1210f1580f4eSBarry Smith           PetscCall(MatDiagonalScale(B, v, NULL));
1211f1580f4eSBarry Smith           PetscCall(VecDestroy(&v));
1212f1580f4eSBarry Smith           PetscCall(MatCreateNormalHermitian(B, &N));
1213f1580f4eSBarry Smith           PetscCall(PCHPDDMSetAuxiliaryMatNormal_Private(pc, B, N, &P, pcpre));
1214f1580f4eSBarry Smith           PetscCall(PetscObjectTypeCompare((PetscObject)data->aux, MATSEQAIJ, &flg));
1215f1580f4eSBarry Smith           if (!flg) {
1216f1580f4eSBarry Smith             PetscCall(MatDestroy(&P));
1217f1580f4eSBarry Smith             P = N;
1218f1580f4eSBarry Smith             PetscCall(PetscObjectReference((PetscObject)P));
1219f1580f4eSBarry Smith           } else PetscCall(MatScale(P, -1.0));
1220f1580f4eSBarry Smith           PetscCall(MatScale(N, -1.0));
1221f1580f4eSBarry Smith           PetscCall(PCSetOperators(pc, N, P)); /* replace P by -A01' inv(diag(P00)) A01 */
1222f1580f4eSBarry Smith           PetscCall(MatDestroy(&N));
1223f1580f4eSBarry Smith           PetscCall(MatDestroy(&P));
1224f1580f4eSBarry Smith           PetscCall(MatDestroy(&B));
12253ba16761SJacob Faibussowitsch           PetscFunctionReturn(PETSC_SUCCESS);
1226f1580f4eSBarry Smith         } else {
1227f1580f4eSBarry Smith           PetscCall(PetscOptionsGetString(NULL, pcpre, "-pc_hpddm_levels_1_st_pc_type", type, sizeof(type), NULL));
1228f1580f4eSBarry Smith           PetscCall(PetscStrcmp(type, PCMAT, &algebraic));
1229f1580f4eSBarry Smith           PetscCall(PetscOptionsGetBool(NULL, pcpre, "-pc_hpddm_block_splitting", &block, NULL));
1230f1580f4eSBarry Smith           PetscCheck(!algebraic || !block, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "-pc_hpddm_levels_1_st_pc_type mat and -pc_hpddm_block_splitting");
1231f1580f4eSBarry Smith           if (block) algebraic = PETSC_TRUE;
1232f1580f4eSBarry Smith           if (algebraic) {
1233f1580f4eSBarry Smith             PetscCall(ISCreateStride(PETSC_COMM_SELF, P->rmap->n, P->rmap->rstart, 1, &data->is));
1234f1580f4eSBarry Smith             PetscCall(MatIncreaseOverlap(P, 1, &data->is, 1));
1235f1580f4eSBarry Smith             PetscCall(ISSort(data->is));
1236f1580f4eSBarry Smith           } else PetscCall(PetscInfo(pc, "Cannot assemble a fully-algebraic coarse operator with an assembled Pmat and -%spc_hpddm_levels_1_st_pc_type != mat and -%spc_hpddm_block_splitting != true\n", pcpre ? pcpre : "", pcpre ? pcpre : ""));
1237f1580f4eSBarry Smith         }
1238f1580f4eSBarry Smith       }
1239f1580f4eSBarry Smith     }
1240f1580f4eSBarry Smith   }
1241398c7888SPierre Jolivet #if PetscDefined(USE_DEBUG)
1242398c7888SPierre Jolivet   if (data->is) PetscCall(ISDuplicate(data->is, &dis));
1243398c7888SPierre Jolivet   if (data->aux) PetscCall(MatDuplicate(data->aux, MAT_COPY_VALUES, &daux));
1244398c7888SPierre Jolivet #endif
1245f1580f4eSBarry Smith   if (data->is || (ismatis && data->N > 1)) {
1246f1580f4eSBarry Smith     if (ismatis) {
1247f1580f4eSBarry Smith       std::initializer_list<std::string> list = {MATSEQBAIJ, MATSEQSBAIJ};
1248f1580f4eSBarry Smith       PetscCall(MatISGetLocalMat(P, &N));
1249f1580f4eSBarry Smith       std::initializer_list<std::string>::const_iterator it = std::find(list.begin(), list.end(), ((PetscObject)N)->type_name);
1250f1580f4eSBarry Smith       PetscCall(MatISRestoreLocalMat(P, &N));
1251f1580f4eSBarry Smith       switch (std::distance(list.begin(), it)) {
1252d71ae5a4SJacob Faibussowitsch       case 0:
1253d71ae5a4SJacob Faibussowitsch         PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &C));
1254d71ae5a4SJacob Faibussowitsch         break;
1255f1580f4eSBarry Smith       case 1:
1256f1580f4eSBarry Smith         /* MatCreateSubMatrices() does not work with MATSBAIJ and unsorted ISes, so convert to MPIBAIJ */
1257f1580f4eSBarry Smith         PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &C));
1258f1580f4eSBarry Smith         PetscCall(MatSetOption(C, MAT_SYMMETRIC, PETSC_TRUE));
1259f1580f4eSBarry Smith         break;
1260d71ae5a4SJacob Faibussowitsch       default:
1261d71ae5a4SJacob Faibussowitsch         PetscCall(MatConvert(P, MATMPIAIJ, MAT_INITIAL_MATRIX, &C));
1262f1580f4eSBarry Smith       }
1263f1580f4eSBarry Smith       PetscCall(MatISGetLocalToGlobalMapping(P, &l2g, NULL));
1264f1580f4eSBarry Smith       PetscCall(PetscObjectReference((PetscObject)P));
1265f1580f4eSBarry Smith       PetscCall(KSPSetOperators(data->levels[0]->ksp, A, C));
1266f1580f4eSBarry Smith       std::swap(C, P);
1267f1580f4eSBarry Smith       PetscCall(ISLocalToGlobalMappingGetSize(l2g, &n));
1268f1580f4eSBarry Smith       PetscCall(ISCreateStride(PETSC_COMM_SELF, n, 0, 1, &loc));
1269f1580f4eSBarry Smith       PetscCall(ISLocalToGlobalMappingApplyIS(l2g, loc, &is[0]));
1270f1580f4eSBarry Smith       PetscCall(ISDestroy(&loc));
1271f1580f4eSBarry Smith       /* the auxiliary Mat is _not_ the local Neumann matrix                                */
1272f1580f4eSBarry Smith       /* it is the local Neumann matrix augmented (with zeros) through MatIncreaseOverlap() */
1273c8ea6600SPierre Jolivet       data->Neumann = PETSC_BOOL3_FALSE;
1274f1580f4eSBarry Smith       structure     = SAME_NONZERO_PATTERN;
1275f1580f4eSBarry Smith     } else {
1276f1580f4eSBarry Smith       is[0] = data->is;
1277f1580f4eSBarry Smith       if (algebraic) subdomains = PETSC_TRUE;
1278f1580f4eSBarry Smith       PetscCall(PetscOptionsGetBool(NULL, pcpre, "-pc_hpddm_define_subdomains", &subdomains, NULL));
1279c8ea6600SPierre Jolivet       if (PetscBool3ToBool(data->Neumann)) {
1280f1580f4eSBarry Smith         PetscCheck(!block, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "-pc_hpddm_block_splitting and -pc_hpddm_has_neumann");
1281f1580f4eSBarry Smith         PetscCheck(!algebraic, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "-pc_hpddm_levels_1_st_pc_type mat and -pc_hpddm_has_neumann");
1282f1580f4eSBarry Smith       }
1283c8ea6600SPierre Jolivet       if (PetscBool3ToBool(data->Neumann) || block) structure = SAME_NONZERO_PATTERN;
1284f1580f4eSBarry Smith       PetscCall(ISCreateStride(PetscObjectComm((PetscObject)data->is), P->rmap->n, P->rmap->rstart, 1, &loc));
1285f1580f4eSBarry Smith     }
1286f1580f4eSBarry Smith     PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_", pcpre ? pcpre : ""));
1287f1580f4eSBarry Smith     PetscCall(PetscOptionsGetEnum(NULL, prefix, "-st_matstructure", MatStructures, (PetscEnum *)&structure, &flg)); /* if not user-provided, force its value when possible */
1288f1580f4eSBarry Smith     if (!flg && structure == SAME_NONZERO_PATTERN) {                                                                /* cannot call STSetMatStructure() yet, insert the appropriate option in the database, parsed by STSetFromOptions() */
1289f1580f4eSBarry Smith       PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-%spc_hpddm_levels_1_st_matstructure", pcpre ? pcpre : ""));
1290f1580f4eSBarry Smith       PetscCall(PetscOptionsSetValue(NULL, prefix, MatStructures[structure]));
1291f1580f4eSBarry Smith     }
1292398c7888SPierre Jolivet     flg = PETSC_FALSE;
1293398c7888SPierre Jolivet     if (data->share && !algebraic) {
1294398c7888SPierre Jolivet       data->share = PETSC_FALSE; /* will be reset to PETSC_TRUE if none of the conditions below are true */
1295398c7888SPierre Jolivet       if (!subdomains) PetscCall(PetscInfo(pc, "Cannot share subdomain KSP between SLEPc and PETSc since -%spc_hpddm_define_subdomains is not true\n", pcpre ? pcpre : ""));
1296398c7888SPierre Jolivet       else if (data->deflation) PetscCall(PetscInfo(pc, "Nothing to share since PCHPDDMSetDeflationMat() has been called\n"));
1297398c7888SPierre Jolivet       else if (ismatis) PetscCall(PetscInfo(pc, "Cannot share subdomain KSP between SLEPc and PETSc with a Pmat of type MATIS\n"));
1298398c7888SPierre Jolivet       else if (structure != SAME_NONZERO_PATTERN)
1299398c7888SPierre Jolivet         PetscCall(PetscInfo(pc, "Cannot share subdomain KSP between SLEPc and PETSc since -%spc_hpddm_levels_1_st_matstructure %s (!= %s)\n", pcpre ? pcpre : "", MatStructures[structure], MatStructures[SAME_NONZERO_PATTERN]));
1300398c7888SPierre Jolivet       else data->share = PETSC_TRUE;
1301398c7888SPierre Jolivet     }
1302398c7888SPierre Jolivet     if (!ismatis) {
1303398c7888SPierre Jolivet       if (data->share || (!PetscBool3ToBool(data->Neumann) && subdomains)) PetscCall(ISDuplicate(is[0], &unsorted));
1304398c7888SPierre Jolivet       else unsorted = is[0];
1305398c7888SPierre Jolivet     }
1306f1580f4eSBarry Smith     if (data->N > 1 && (data->aux || ismatis || algebraic)) {
1307f1580f4eSBarry Smith       PetscCheck(loadedSym, PETSC_COMM_SELF, PETSC_ERR_PLIB, "HPDDM library not loaded, cannot use more than one level");
1308f1580f4eSBarry Smith       PetscCall(MatSetOption(P, MAT_SUBMAT_SINGLEIS, PETSC_TRUE));
1309f1580f4eSBarry Smith       if (ismatis) {
1310f1580f4eSBarry Smith         /* needed by HPDDM (currently) so that the partition of unity is 0 on subdomain interfaces */
1311f1580f4eSBarry Smith         PetscCall(MatIncreaseOverlap(P, 1, is, 1));
1312f1580f4eSBarry Smith         PetscCall(ISDestroy(&data->is));
1313f1580f4eSBarry Smith         data->is = is[0];
1314f1580f4eSBarry Smith       } else {
1315f1580f4eSBarry Smith         if (PetscDefined(USE_DEBUG)) {
1316f1580f4eSBarry Smith           PetscBool equal;
1317f1580f4eSBarry Smith           IS        intersect;
1318f1580f4eSBarry Smith 
1319f1580f4eSBarry Smith           PetscCall(ISIntersect(data->is, loc, &intersect));
1320f1580f4eSBarry Smith           PetscCall(ISEqualUnsorted(loc, intersect, &equal));
1321f1580f4eSBarry Smith           PetscCall(ISDestroy(&intersect));
1322f1580f4eSBarry Smith           PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "IS of the auxiliary Mat does not include all local rows of A");
1323f1580f4eSBarry Smith         }
1324f1580f4eSBarry Smith         PetscCall(PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", PCHPDDMAlgebraicAuxiliaryMat_Private));
1325c8ea6600SPierre Jolivet         if (!PetscBool3ToBool(data->Neumann) && !algebraic) {
1326f1580f4eSBarry Smith           PetscCall(PetscObjectTypeCompare((PetscObject)P, MATMPISBAIJ, &flg));
1327f1580f4eSBarry Smith           if (flg) {
1328f1580f4eSBarry Smith             /* maybe better to ISSort(is[0]), MatCreateSubMatrices(), and then MatPermute() */
1329f1580f4eSBarry Smith             /* but there is no MatPermute_SeqSBAIJ(), so as before, just use MATMPIBAIJ     */
1330f1580f4eSBarry Smith             PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &uaux));
1331f1580f4eSBarry Smith             flg = PETSC_FALSE;
1332f1580f4eSBarry Smith           }
1333f1580f4eSBarry Smith         }
1334f1580f4eSBarry Smith       }
1335f1580f4eSBarry Smith       if (algebraic) {
1336f1580f4eSBarry Smith         PetscUseMethod(pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", (Mat, IS *, Mat *[], PetscBool), (P, is, &sub, block));
1337f1580f4eSBarry Smith         if (block) {
1338f1580f4eSBarry Smith           PetscCall(PetscObjectQuery((PetscObject)sub[0], "_PCHPDDM_Neumann_Mat", (PetscObject *)&data->aux));
1339f1580f4eSBarry Smith           PetscCall(PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Neumann_Mat", NULL));
1340f1580f4eSBarry Smith         }
1341f1580f4eSBarry Smith       } else if (!uaux) {
1342c8ea6600SPierre Jolivet         if (PetscBool3ToBool(data->Neumann)) sub = &data->aux;
1343f1580f4eSBarry Smith         else PetscCall(MatCreateSubMatrices(P, 1, is, is, MAT_INITIAL_MATRIX, &sub));
1344f1580f4eSBarry Smith       } else {
1345f1580f4eSBarry Smith         PetscCall(MatCreateSubMatrices(uaux, 1, is, is, MAT_INITIAL_MATRIX, &sub));
1346f1580f4eSBarry Smith         PetscCall(MatDestroy(&uaux));
1347f1580f4eSBarry Smith         PetscCall(MatConvert(sub[0], MATSEQSBAIJ, MAT_INPLACE_MATRIX, sub));
1348f1580f4eSBarry Smith       }
1349f1580f4eSBarry Smith       /* Vec holding the partition of unity */
1350f1580f4eSBarry Smith       if (!data->levels[0]->D) {
1351f1580f4eSBarry Smith         PetscCall(ISGetLocalSize(data->is, &n));
1352f1580f4eSBarry Smith         PetscCall(VecCreateMPI(PETSC_COMM_SELF, n, PETSC_DETERMINE, &data->levels[0]->D));
1353f1580f4eSBarry Smith       }
1354398c7888SPierre Jolivet       if (data->share) {
1355f1580f4eSBarry Smith         Mat      D;
1356f1580f4eSBarry Smith         IS       perm = NULL;
1357f1580f4eSBarry Smith         PetscInt size = -1;
1358398c7888SPierre Jolivet         if (!data->levels[0]->pc) {
1359398c7888SPierre Jolivet           PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_", pcpre ? pcpre : ""));
1360398c7888SPierre Jolivet           PetscCall(PCCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->pc));
1361398c7888SPierre Jolivet           PetscCall(PCSetOptionsPrefix(data->levels[0]->pc, prefix));
1362398c7888SPierre Jolivet           PetscCall(PCSetOperators(data->levels[0]->pc, A, P));
1363398c7888SPierre Jolivet         }
1364398c7888SPierre Jolivet         PetscCall(PCSetType(data->levels[0]->pc, PCASM));
1365398c7888SPierre Jolivet         if (!data->levels[0]->pc->setupcalled) {
1366398c7888SPierre Jolivet           IS sorted; /* PCASM will sort the input IS, duplicate it to return an unmodified (PCHPDDM) input IS */
1367398c7888SPierre Jolivet           PetscCall(ISDuplicate(is[0], &sorted));
1368398c7888SPierre Jolivet           PetscCall(PCASMSetLocalSubdomains(data->levels[0]->pc, 1, &sorted, &loc));
1369398c7888SPierre Jolivet           PetscCall(PetscObjectDereference((PetscObject)sorted));
1370398c7888SPierre Jolivet         }
1371398c7888SPierre Jolivet         PetscCall(PCSetFromOptions(data->levels[0]->pc));
1372398c7888SPierre Jolivet         if (block) {
1373398c7888SPierre Jolivet           PetscCall(PCHPDDMPermute_Private(unsorted, data->is, &uis, sub[0], &C, &perm));
1374398c7888SPierre Jolivet           PetscCall(PCHPDDMCommunicationAvoidingPCASM_Private(data->levels[0]->pc, C, algebraic));
1375398c7888SPierre Jolivet         } else PetscCall(PCSetUp(data->levels[0]->pc));
1376398c7888SPierre Jolivet         PetscTryMethod(data->levels[0]->pc, "PCASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (data->levels[0]->pc, &size, NULL, &ksp));
1377398c7888SPierre Jolivet         if (size != 1) {
1378398c7888SPierre Jolivet           data->share = PETSC_FALSE;
1379398c7888SPierre Jolivet           PetscCheck(size == -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of subdomain solver %" PetscInt_FMT " != 1", size);
1380398c7888SPierre Jolivet           PetscCall(PetscInfo(pc, "Cannot share subdomain KSP between SLEPc and PETSc since PCASMGetSubKSP() not found in fine-level PC\n"));
1381398c7888SPierre Jolivet           PetscCall(ISDestroy(&unsorted));
1382398c7888SPierre Jolivet           unsorted = is[0];
1383398c7888SPierre Jolivet         } else {
1384398c7888SPierre Jolivet           if (!block) PetscCall(PCHPDDMPermute_Private(unsorted, data->is, &uis, PetscBool3ToBool(data->Neumann) ? sub[0] : data->aux, &C, &perm));
1385c8ea6600SPierre Jolivet           if (!PetscBool3ToBool(data->Neumann) && !block) {
1386f1580f4eSBarry Smith             PetscCall(MatPermute(sub[0], perm, perm, &D)); /* permute since PCASM will call ISSort() */
1387f1580f4eSBarry Smith             PetscCall(MatHeaderReplace(sub[0], &D));
1388f1580f4eSBarry Smith           }
1389f1580f4eSBarry Smith           if (data->B) { /* see PCHPDDMSetRHSMat() */
1390f1580f4eSBarry Smith             PetscCall(MatPermute(data->B, perm, perm, &D));
1391f1580f4eSBarry Smith             PetscCall(MatHeaderReplace(data->B, &D));
1392f1580f4eSBarry Smith           }
1393f1580f4eSBarry Smith           PetscCall(ISDestroy(&perm));
1394f1580f4eSBarry Smith           const char *matpre;
1395f1580f4eSBarry Smith           PetscBool   cmp[4];
1396f1580f4eSBarry Smith           PetscCall(KSPGetOperators(ksp[0], subA, subA + 1));
1397f1580f4eSBarry Smith           PetscCall(MatDuplicate(subA[1], MAT_SHARE_NONZERO_PATTERN, &D));
1398f1580f4eSBarry Smith           PetscCall(MatGetOptionsPrefix(subA[1], &matpre));
1399f1580f4eSBarry Smith           PetscCall(MatSetOptionsPrefix(D, matpre));
1400f1580f4eSBarry Smith           PetscCall(PetscObjectTypeCompare((PetscObject)D, MATNORMAL, cmp));
1401f1580f4eSBarry Smith           PetscCall(PetscObjectTypeCompare((PetscObject)C, MATNORMAL, cmp + 1));
1402f1580f4eSBarry Smith           if (!cmp[0]) PetscCall(PetscObjectTypeCompare((PetscObject)D, MATNORMALHERMITIAN, cmp + 2));
1403f1580f4eSBarry Smith           else cmp[2] = PETSC_FALSE;
1404f1580f4eSBarry Smith           if (!cmp[1]) PetscCall(PetscObjectTypeCompare((PetscObject)C, MATNORMALHERMITIAN, cmp + 3));
1405f1580f4eSBarry Smith           else cmp[3] = PETSC_FALSE;
1406f1580f4eSBarry Smith           PetscCheck(cmp[0] == cmp[1] && cmp[2] == cmp[3], PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_levels_1_pc_asm_sub_mat_type %s and auxiliary Mat of type %s", pcpre ? pcpre : "", ((PetscObject)D)->type_name, ((PetscObject)C)->type_name);
1407f1580f4eSBarry Smith           if (!cmp[0] && !cmp[2]) {
1408f1580f4eSBarry Smith             if (!block) PetscCall(MatAXPY(D, 1.0, C, SUBSET_NONZERO_PATTERN));
14095c7345deSPierre Jolivet             else {
14105c7345deSPierre Jolivet               PetscCall(MatMissingDiagonal(D, cmp, NULL));
14115c7345deSPierre Jolivet               if (cmp[0]) structure = DIFFERENT_NONZERO_PATTERN; /* data->aux has no missing diagonal entry */
14125c7345deSPierre Jolivet               PetscCall(MatAXPY(D, 1.0, data->aux, structure));
14135c7345deSPierre Jolivet             }
1414f1580f4eSBarry Smith           } else {
1415f1580f4eSBarry Smith             Mat mat[2];
1416f1580f4eSBarry Smith             if (cmp[0]) {
1417f1580f4eSBarry Smith               PetscCall(MatNormalGetMat(D, mat));
1418f1580f4eSBarry Smith               PetscCall(MatNormalGetMat(C, mat + 1));
1419f1580f4eSBarry Smith             } else {
1420f1580f4eSBarry Smith               PetscCall(MatNormalHermitianGetMat(D, mat));
1421f1580f4eSBarry Smith               PetscCall(MatNormalHermitianGetMat(C, mat + 1));
1422f1580f4eSBarry Smith             }
1423f1580f4eSBarry Smith             PetscCall(MatAXPY(mat[0], 1.0, mat[1], SUBSET_NONZERO_PATTERN));
1424f1580f4eSBarry Smith           }
1425f1580f4eSBarry Smith           PetscCall(MatPropagateSymmetryOptions(C, D));
1426f1580f4eSBarry Smith           PetscCall(MatDestroy(&C));
1427f1580f4eSBarry Smith           C = D;
1428f1580f4eSBarry Smith           /* swap pointers so that variables stay consistent throughout PCSetUp() */
1429f1580f4eSBarry Smith           std::swap(C, data->aux);
1430f1580f4eSBarry Smith           std::swap(uis, data->is);
1431f1580f4eSBarry Smith           swap = PETSC_TRUE;
1432f1580f4eSBarry Smith         }
1433f1580f4eSBarry Smith       }
1434f1580f4eSBarry Smith       if (!data->levels[0]->scatter) {
1435f1580f4eSBarry Smith         PetscCall(MatCreateVecs(P, &xin, NULL));
1436f1580f4eSBarry Smith         if (ismatis) PetscCall(MatDestroy(&P));
1437f1580f4eSBarry Smith         PetscCall(VecScatterCreate(xin, data->is, data->levels[0]->D, NULL, &data->levels[0]->scatter));
1438f1580f4eSBarry Smith         PetscCall(VecDestroy(&xin));
1439f1580f4eSBarry Smith       }
1440f1580f4eSBarry Smith       if (data->levels[0]->P) {
1441f1580f4eSBarry Smith         /* if the pattern is the same and PCSetUp() has previously succeeded, reuse HPDDM buffers and connectivity */
1442f1580f4eSBarry Smith         PetscCall(HPDDM::Schwarz<PetscScalar>::destroy(data->levels[0], pc->setupcalled < 1 || pc->flag == DIFFERENT_NONZERO_PATTERN ? PETSC_TRUE : PETSC_FALSE));
1443f1580f4eSBarry Smith       }
1444f1580f4eSBarry Smith       if (!data->levels[0]->P) data->levels[0]->P = new HPDDM::Schwarz<PetscScalar>();
1445f1580f4eSBarry Smith       if (data->log_separate) PetscCall(PetscLogEventBegin(PC_HPDDM_SetUp[0], data->levels[0]->ksp, 0, 0, 0));
1446f1580f4eSBarry Smith       else PetscCall(PetscLogEventBegin(PC_HPDDM_Strc, data->levels[0]->ksp, 0, 0, 0));
1447f1580f4eSBarry Smith       /* HPDDM internal data structure */
1448f1580f4eSBarry Smith       PetscCall(data->levels[0]->P->structure(loc, data->is, sub[0], ismatis ? C : data->aux, data->levels));
1449f1580f4eSBarry Smith       if (!data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_Strc, data->levels[0]->ksp, 0, 0, 0));
1450f1580f4eSBarry Smith       /* matrix pencil of the generalized eigenvalue problem on the overlap (GenEO) */
1451f1580f4eSBarry Smith       if (data->deflation) weighted = data->aux;
1452f1580f4eSBarry Smith       else if (!data->B) {
1453f1580f4eSBarry Smith         PetscBool cmp[2];
1454f1580f4eSBarry Smith         PetscCall(MatDuplicate(sub[0], MAT_COPY_VALUES, &weighted));
1455f1580f4eSBarry Smith         PetscCall(PetscObjectTypeCompare((PetscObject)weighted, MATNORMAL, cmp));
1456f1580f4eSBarry Smith         if (!cmp[0]) PetscCall(PetscObjectTypeCompare((PetscObject)weighted, MATNORMALHERMITIAN, cmp + 1));
1457f1580f4eSBarry Smith         else cmp[1] = PETSC_FALSE;
1458f1580f4eSBarry Smith         if (!cmp[0] && !cmp[1]) PetscCall(MatDiagonalScale(weighted, data->levels[0]->D, data->levels[0]->D));
1459f1580f4eSBarry Smith         else { /* MATNORMAL applies MatDiagonalScale() in a matrix-free fashion, not what is needed since this won't be passed to SLEPc during the eigensolve */
1460f1580f4eSBarry Smith           if (cmp[0]) PetscCall(MatNormalGetMat(weighted, &data->B));
1461f1580f4eSBarry Smith           else PetscCall(MatNormalHermitianGetMat(weighted, &data->B));
1462f1580f4eSBarry Smith           PetscCall(MatDiagonalScale(data->B, NULL, data->levels[0]->D));
1463f1580f4eSBarry Smith           data->B = NULL;
1464f1580f4eSBarry Smith           flg     = PETSC_FALSE;
1465f1580f4eSBarry Smith         }
1466f1580f4eSBarry Smith         /* neither MatDuplicate() nor MatDiagonaleScale() handles the symmetry options, so propagate the options explicitly */
1467f1580f4eSBarry Smith         /* only useful for -mat_type baij -pc_hpddm_levels_1_st_pc_type cholesky (no problem with MATAIJ or MATSBAIJ)       */
1468f1580f4eSBarry Smith         PetscCall(MatPropagateSymmetryOptions(sub[0], weighted));
1469f1580f4eSBarry Smith       } else weighted = data->B;
1470f1580f4eSBarry Smith       /* SLEPc is used inside the loaded symbol */
1471f1580f4eSBarry Smith       PetscCall((*loadedSym)(data->levels[0]->P, data->is, ismatis ? C : (algebraic && !block ? sub[0] : data->aux), weighted, data->B, initial, data->levels));
1472f1580f4eSBarry Smith       if (data->share) {
1473f1580f4eSBarry Smith         Mat st[2];
1474f1580f4eSBarry Smith         PetscCall(KSPGetOperators(ksp[0], st, st + 1));
14755c7345deSPierre Jolivet         PetscCall(MatCopy(subA[0], st[0], structure));
1476f1580f4eSBarry Smith         if (subA[1] != subA[0] || st[1] != st[0]) PetscCall(MatCopy(subA[1], st[1], SAME_NONZERO_PATTERN));
1477f1580f4eSBarry Smith       }
1478f1580f4eSBarry Smith       if (data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_SetUp[0], data->levels[0]->ksp, 0, 0, 0));
1479f1580f4eSBarry Smith       if (ismatis) PetscCall(MatISGetLocalMat(C, &N));
1480f1580f4eSBarry Smith       else N = data->aux;
1481f1580f4eSBarry Smith       P = sub[0];
1482f1580f4eSBarry Smith       /* going through the grid hierarchy */
1483f1580f4eSBarry Smith       for (n = 1; n < data->N; ++n) {
1484f1580f4eSBarry Smith         if (data->log_separate) PetscCall(PetscLogEventBegin(PC_HPDDM_SetUp[n], data->levels[n]->ksp, 0, 0, 0));
1485f1580f4eSBarry Smith         /* method composed in the loaded symbol since there, SLEPc is used as well */
1486f1580f4eSBarry Smith         PetscTryMethod(data->levels[0]->ksp, "PCHPDDMSetUp_Private_C", (Mat *, Mat *, PetscInt, PetscInt *const, PC_HPDDM_Level **const), (&P, &N, n, &data->N, data->levels));
1487f1580f4eSBarry Smith         if (data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_SetUp[n], data->levels[n]->ksp, 0, 0, 0));
1488f1580f4eSBarry Smith       }
1489f1580f4eSBarry Smith       /* reset to NULL to avoid any faulty use */
1490f1580f4eSBarry Smith       PetscCall(PetscObjectComposeFunction((PetscObject)data->levels[0]->ksp, "PCHPDDMSetUp_Private_C", NULL));
1491f1580f4eSBarry Smith       if (!ismatis) PetscCall(PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_C", NULL));
1492f1580f4eSBarry Smith       else PetscCall(PetscObjectDereference((PetscObject)C)); /* matching PetscObjectReference() above */
1493f1580f4eSBarry Smith       for (n = 0; n < data->N - 1; ++n)
1494f1580f4eSBarry Smith         if (data->levels[n]->P) {
1495f1580f4eSBarry Smith           /* HPDDM internal work buffers */
1496f1580f4eSBarry Smith           data->levels[n]->P->setBuffer();
1497f1580f4eSBarry Smith           data->levels[n]->P->super::start();
1498f1580f4eSBarry Smith         }
1499c8ea6600SPierre Jolivet       if (ismatis || !subdomains) PetscCall(PCHPDDMDestroySubMatrices_Private(PetscBool3ToBool(data->Neumann), PetscBool(algebraic && !block), sub));
1500f1580f4eSBarry Smith       if (ismatis) data->is = NULL;
1501f1580f4eSBarry Smith       for (n = 0; n < data->N - 1 + (reused > 0); ++n) {
1502f1580f4eSBarry Smith         if (data->levels[n]->P) {
1503f1580f4eSBarry Smith           PC spc;
1504f1580f4eSBarry Smith 
1505f1580f4eSBarry Smith           /* force the PC to be PCSHELL to do the coarse grid corrections */
1506f1580f4eSBarry Smith           PetscCall(KSPSetSkipPCSetFromOptions(data->levels[n]->ksp, PETSC_TRUE));
1507f1580f4eSBarry Smith           PetscCall(KSPGetPC(data->levels[n]->ksp, &spc));
1508f1580f4eSBarry Smith           PetscCall(PCSetType(spc, PCSHELL));
1509f1580f4eSBarry Smith           PetscCall(PCShellSetContext(spc, data->levels[n]));
1510f1580f4eSBarry Smith           PetscCall(PCShellSetSetUp(spc, PCSetUp_HPDDMShell));
1511f1580f4eSBarry Smith           PetscCall(PCShellSetApply(spc, PCApply_HPDDMShell));
1512f1580f4eSBarry Smith           PetscCall(PCShellSetMatApply(spc, PCMatApply_HPDDMShell));
1513f1580f4eSBarry Smith           PetscCall(PCShellSetDestroy(spc, PCDestroy_HPDDMShell));
1514f1580f4eSBarry Smith           if (!data->levels[n]->pc) PetscCall(PCCreate(PetscObjectComm((PetscObject)data->levels[n]->ksp), &data->levels[n]->pc));
1515f1580f4eSBarry Smith           if (n < reused) {
1516f1580f4eSBarry Smith             PetscCall(PCSetReusePreconditioner(spc, PETSC_TRUE));
1517f1580f4eSBarry Smith             PetscCall(PCSetReusePreconditioner(data->levels[n]->pc, PETSC_TRUE));
1518f1580f4eSBarry Smith           }
1519f1580f4eSBarry Smith           PetscCall(PCSetUp(spc));
1520f1580f4eSBarry Smith         }
1521f1580f4eSBarry Smith       }
1522f1580f4eSBarry Smith       PetscCall(PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", NULL));
1523f1580f4eSBarry Smith     } else flg = reused ? PETSC_FALSE : PETSC_TRUE;
1524f1580f4eSBarry Smith     if (!ismatis && subdomains) {
1525f1580f4eSBarry Smith       if (flg) PetscCall(KSPGetPC(data->levels[0]->ksp, &inner));
1526f1580f4eSBarry Smith       else inner = data->levels[0]->pc;
1527f1580f4eSBarry Smith       if (inner) {
1528398c7888SPierre Jolivet         if (!inner->setupcalled) PetscCall(PCSetType(inner, PCASM));
1529398c7888SPierre Jolivet         PetscCall(PCSetFromOptions(inner));
1530398c7888SPierre Jolivet         PetscCall(PetscStrcmp(((PetscObject)inner)->type_name, PCASM, &flg));
1531398c7888SPierre Jolivet         if (flg) {
1532f1580f4eSBarry Smith           if (!inner->setupcalled) { /* evaluates to PETSC_FALSE when -pc_hpddm_block_splitting */
1533398c7888SPierre Jolivet             IS sorted;               /* PCASM will sort the input IS, duplicate it to return an unmodified (PCHPDDM) input IS */
1534398c7888SPierre Jolivet             PetscCall(ISDuplicate(is[0], &sorted));
1535398c7888SPierre Jolivet             PetscCall(PCASMSetLocalSubdomains(inner, 1, &sorted, &loc));
1536398c7888SPierre Jolivet             PetscCall(PetscObjectDereference((PetscObject)sorted));
1537398c7888SPierre Jolivet           }
1538c8ea6600SPierre Jolivet           if (!PetscBool3ToBool(data->Neumann) && data->N > 1) { /* subdomain matrices are already created for the eigenproblem, reuse them for the fine-level PC */
1539398c7888SPierre Jolivet             PetscCall(PCHPDDMPermute_Private(*is, NULL, NULL, sub[0], &P, NULL));
1540398c7888SPierre Jolivet             PetscCall(PCHPDDMCommunicationAvoidingPCASM_Private(inner, P, algebraic));
1541398c7888SPierre Jolivet             PetscCall(PetscObjectDereference((PetscObject)P));
1542f1580f4eSBarry Smith           }
1543f1580f4eSBarry Smith         }
1544f1580f4eSBarry Smith       }
1545c8ea6600SPierre Jolivet       if (data->N > 1) PetscCall(PCHPDDMDestroySubMatrices_Private(PetscBool3ToBool(data->Neumann), PetscBool(algebraic && !block), sub));
1546f1580f4eSBarry Smith     }
1547f1580f4eSBarry Smith     PetscCall(ISDestroy(&loc));
1548f1580f4eSBarry Smith   } else data->N = 1 + reused; /* enforce this value to 1 + reused if there is no way to build another level */
1549f1580f4eSBarry Smith   if (requested != data->N + reused) {
1550f1580f4eSBarry Smith     PetscCall(PetscInfo(pc, "%" PetscInt_FMT " levels requested, only %" PetscInt_FMT " built + %" PetscInt_FMT " reused. Options for level(s) > %" PetscInt_FMT ", including -%spc_hpddm_coarse_ will not be taken into account\n", requested, data->N, reused,
1551f1580f4eSBarry Smith                         data->N, pcpre ? pcpre : ""));
1552f1580f4eSBarry Smith     PetscCall(PetscInfo(pc, "It is best to tune parameters, e.g., a higher value for -%spc_hpddm_levels_%" PetscInt_FMT "_eps_threshold so that at least one local deflation vector will be selected\n", pcpre ? pcpre : "", data->N));
1553f1580f4eSBarry Smith     /* cannot use PCDestroy_HPDDMShell() because PCSHELL not set for unassembled levels */
1554f1580f4eSBarry Smith     for (n = data->N - 1; n < requested - 1; ++n) {
1555f1580f4eSBarry Smith       if (data->levels[n]->P) {
1556f1580f4eSBarry Smith         PetscCall(HPDDM::Schwarz<PetscScalar>::destroy(data->levels[n], PETSC_TRUE));
1557f1580f4eSBarry Smith         PetscCall(VecDestroyVecs(1, &data->levels[n]->v[0]));
1558f1580f4eSBarry Smith         PetscCall(VecDestroyVecs(2, &data->levels[n]->v[1]));
1559f1580f4eSBarry Smith         PetscCall(MatDestroy(data->levels[n]->V));
1560f1580f4eSBarry Smith         PetscCall(MatDestroy(data->levels[n]->V + 1));
1561f1580f4eSBarry Smith         PetscCall(MatDestroy(data->levels[n]->V + 2));
1562f1580f4eSBarry Smith         PetscCall(VecDestroy(&data->levels[n]->D));
1563f1580f4eSBarry Smith         PetscCall(VecScatterDestroy(&data->levels[n]->scatter));
1564f1580f4eSBarry Smith       }
1565f1580f4eSBarry Smith     }
1566f1580f4eSBarry Smith     if (reused) {
1567f1580f4eSBarry Smith       for (n = reused; n < PETSC_PCHPDDM_MAXLEVELS && data->levels[n]; ++n) {
1568f1580f4eSBarry Smith         PetscCall(KSPDestroy(&data->levels[n]->ksp));
1569f1580f4eSBarry Smith         PetscCall(PCDestroy(&data->levels[n]->pc));
1570f1580f4eSBarry Smith       }
1571f1580f4eSBarry Smith     }
1572f1580f4eSBarry Smith     PetscCheck(!PetscDefined(USE_DEBUG), PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "%" PetscInt_FMT " levels requested, only %" PetscInt_FMT " built + %" PetscInt_FMT " reused. Options for level(s) > %" PetscInt_FMT ", 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_%" PetscInt_FMT "_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,
1573f1580f4eSBarry Smith                data->N, reused, data->N, pcpre ? pcpre : "", pcpre ? pcpre : "", data->N);
1574f1580f4eSBarry Smith   }
1575f1580f4eSBarry Smith   /* these solvers are created after PCSetFromOptions() is called */
1576f1580f4eSBarry Smith   if (pc->setfromoptionscalled) {
1577f1580f4eSBarry Smith     for (n = 0; n < data->N; ++n) {
1578f1580f4eSBarry Smith       if (data->levels[n]->ksp) PetscCall(KSPSetFromOptions(data->levels[n]->ksp));
1579f1580f4eSBarry Smith       if (data->levels[n]->pc) PetscCall(PCSetFromOptions(data->levels[n]->pc));
1580f1580f4eSBarry Smith     }
1581f1580f4eSBarry Smith     pc->setfromoptionscalled = 0;
1582f1580f4eSBarry Smith   }
1583f1580f4eSBarry Smith   data->N += reused;
1584f1580f4eSBarry Smith   if (data->share && swap) {
1585f1580f4eSBarry Smith     /* swap back pointers so that variables follow the user-provided numbering */
1586f1580f4eSBarry Smith     std::swap(C, data->aux);
1587f1580f4eSBarry Smith     std::swap(uis, data->is);
1588f1580f4eSBarry Smith     PetscCall(MatDestroy(&C));
1589f1580f4eSBarry Smith     PetscCall(ISDestroy(&uis));
1590f1580f4eSBarry Smith   }
1591398c7888SPierre Jolivet   if (algebraic) PetscCall(MatDestroy(&data->aux));
1592*38fb34a1SPierre Jolivet   if (unsorted && unsorted != is[0]) {
1593398c7888SPierre Jolivet     PetscCall(ISCopy(unsorted, data->is));
1594398c7888SPierre Jolivet     PetscCall(ISDestroy(&unsorted));
1595398c7888SPierre Jolivet   }
1596398c7888SPierre Jolivet #if PetscDefined(USE_DEBUG)
1597398c7888SPierre Jolivet   PetscCheck((data->is && dis) || (!data->is && !dis), PETSC_COMM_SELF, PETSC_ERR_PLIB, "An IS pointer is NULL but not the other: input IS pointer (%p) v. output IS pointer (%p)", (void *)dis, (void *)data->is);
1598398c7888SPierre Jolivet   if (data->is) {
1599398c7888SPierre Jolivet     PetscCall(ISEqualUnsorted(data->is, dis, &flg));
1600398c7888SPierre Jolivet     PetscCall(ISDestroy(&dis));
1601398c7888SPierre Jolivet     PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input IS and output IS are not equal");
1602398c7888SPierre Jolivet   }
1603398c7888SPierre Jolivet   PetscCheck((data->aux && daux) || (!data->aux && !daux), PETSC_COMM_SELF, PETSC_ERR_PLIB, "A Mat pointer is NULL but not the other: input Mat pointer (%p) v. output Mat pointer (%p)", (void *)daux, (void *)data->aux);
1604398c7888SPierre Jolivet   if (data->aux) {
1605398c7888SPierre Jolivet     PetscCall(MatMultEqual(data->aux, daux, 20, &flg));
1606398c7888SPierre Jolivet     PetscCall(MatDestroy(&daux));
1607398c7888SPierre Jolivet     PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input Mat and output Mat are not equal");
1608398c7888SPierre Jolivet   }
1609398c7888SPierre Jolivet #endif
16103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1611f1580f4eSBarry Smith }
1612f1580f4eSBarry Smith 
1613f1580f4eSBarry Smith /*@
1614f1580f4eSBarry Smith      PCHPDDMSetCoarseCorrectionType - Sets the coarse correction type.
1615f1580f4eSBarry Smith 
1616c3339decSBarry Smith    Collective
1617f1580f4eSBarry Smith 
1618f1580f4eSBarry Smith    Input Parameters:
1619f1580f4eSBarry Smith +     pc - preconditioner context
1620f1580f4eSBarry Smith -     type - `PC_HPDDM_COARSE_CORRECTION_DEFLATED`, `PC_HPDDM_COARSE_CORRECTION_ADDITIVE`, or `PC_HPDDM_COARSE_CORRECTION_BALANCED`
1621f1580f4eSBarry Smith 
1622f1580f4eSBarry Smith    Options Database Key:
1623f1580f4eSBarry Smith .   -pc_hpddm_coarse_correction <deflated, additive, balanced> - type of coarse correction to apply
1624f1580f4eSBarry Smith 
1625f1580f4eSBarry Smith    Level: intermediate
1626f1580f4eSBarry Smith 
1627f1580f4eSBarry Smith .seealso: `PCHPDDMGetCoarseCorrectionType()`, `PCHPDDM`, `PCHPDDMCoarseCorrectionType`
1628f1580f4eSBarry Smith @*/
1629d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMSetCoarseCorrectionType(PC pc, PCHPDDMCoarseCorrectionType type)
1630d71ae5a4SJacob Faibussowitsch {
1631f1580f4eSBarry Smith   PetscFunctionBegin;
1632f1580f4eSBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1633f1580f4eSBarry Smith   PetscValidLogicalCollectiveEnum(pc, type, 2);
1634f1580f4eSBarry Smith   PetscTryMethod(pc, "PCHPDDMSetCoarseCorrectionType_C", (PC, PCHPDDMCoarseCorrectionType), (pc, type));
16353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1636f1580f4eSBarry Smith }
1637f1580f4eSBarry Smith 
1638f1580f4eSBarry Smith /*@
1639f1580f4eSBarry Smith      PCHPDDMGetCoarseCorrectionType - Gets the coarse correction type.
1640f1580f4eSBarry Smith 
1641f1580f4eSBarry Smith    Input Parameter:
1642f1580f4eSBarry Smith .     pc - preconditioner context
1643f1580f4eSBarry Smith 
1644f1580f4eSBarry Smith    Output Parameter:
1645f1580f4eSBarry Smith .     type - `PC_HPDDM_COARSE_CORRECTION_DEFLATED`, `PC_HPDDM_COARSE_CORRECTION_ADDITIVE`, or `PC_HPDDM_COARSE_CORRECTION_BALANCED`
1646f1580f4eSBarry Smith 
1647f1580f4eSBarry Smith    Level: intermediate
1648f1580f4eSBarry Smith 
1649f1580f4eSBarry Smith .seealso: `PCHPDDMSetCoarseCorrectionType()`, `PCHPDDM`, `PCHPDDMCoarseCorrectionType`
1650f1580f4eSBarry Smith @*/
1651d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMGetCoarseCorrectionType(PC pc, PCHPDDMCoarseCorrectionType *type)
1652d71ae5a4SJacob Faibussowitsch {
1653f1580f4eSBarry Smith   PetscFunctionBegin;
1654f1580f4eSBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1655f1580f4eSBarry Smith   if (type) {
1656f1580f4eSBarry Smith     PetscValidPointer(type, 2);
1657f1580f4eSBarry Smith     PetscUseMethod(pc, "PCHPDDMGetCoarseCorrectionType_C", (PC, PCHPDDMCoarseCorrectionType *), (pc, type));
1658f1580f4eSBarry Smith   }
16593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1660f1580f4eSBarry Smith }
1661f1580f4eSBarry Smith 
1662d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetCoarseCorrectionType_HPDDM(PC pc, PCHPDDMCoarseCorrectionType type)
1663d71ae5a4SJacob Faibussowitsch {
1664f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
1665f1580f4eSBarry Smith 
1666f1580f4eSBarry Smith   PetscFunctionBegin;
1667f1580f4eSBarry Smith   PetscCheck(type >= 0 && type <= 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PCHPDDMCoarseCorrectionType %d", type);
1668f1580f4eSBarry Smith   data->correction = type;
16693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1670f1580f4eSBarry Smith }
1671f1580f4eSBarry Smith 
1672d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMGetCoarseCorrectionType_HPDDM(PC pc, PCHPDDMCoarseCorrectionType *type)
1673d71ae5a4SJacob Faibussowitsch {
1674f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
1675f1580f4eSBarry Smith 
1676f1580f4eSBarry Smith   PetscFunctionBegin;
1677f1580f4eSBarry Smith   *type = data->correction;
16783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1679f1580f4eSBarry Smith }
1680f1580f4eSBarry Smith 
1681f1580f4eSBarry Smith /*@
1682e31fc274SPierre Jolivet      PCHPDDMSetSTShareSubKSP - Sets whether the `KSP` in SLEPc `ST` and the fine-level subdomain solver should be shared.
1683e31fc274SPierre Jolivet 
1684e31fc274SPierre Jolivet    Input Parameters:
1685e31fc274SPierre Jolivet +     pc - preconditioner context
1686e31fc274SPierre Jolivet -     share - whether the `KSP` should be shared or not
1687e31fc274SPierre Jolivet 
1688e31fc274SPierre Jolivet    Note:
1689e31fc274SPierre Jolivet      This is not the same as `PCSetReusePreconditioner()`. Given certain conditions (visible using -info), a symbolic factorization can be skipped
1690e31fc274SPierre Jolivet      when using a subdomain `PCType` such as `PCLU` or `PCCHOLESKY`.
1691e31fc274SPierre Jolivet 
1692e31fc274SPierre Jolivet    Level: advanced
1693e31fc274SPierre Jolivet 
1694e31fc274SPierre Jolivet .seealso: `PCHPDDM`, `PCHPDDMGetSTShareSubKSP()`
1695e31fc274SPierre Jolivet @*/
1696e31fc274SPierre Jolivet PetscErrorCode PCHPDDMSetSTShareSubKSP(PC pc, PetscBool share)
1697e31fc274SPierre Jolivet {
1698e31fc274SPierre Jolivet   PetscFunctionBegin;
1699e31fc274SPierre Jolivet   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1700e31fc274SPierre Jolivet   PetscTryMethod(pc, "PCHPDDMSetSTShareSubKSP_C", (PC, PetscBool), (pc, share));
17013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1702e31fc274SPierre Jolivet }
1703e31fc274SPierre Jolivet 
1704e31fc274SPierre Jolivet /*@
1705f1580f4eSBarry Smith      PCHPDDMGetSTShareSubKSP - Gets whether the `KSP` in SLEPc `ST` and the fine-level subdomain solver is shared.
1706f1580f4eSBarry Smith 
1707f1580f4eSBarry Smith    Input Parameter:
1708f1580f4eSBarry Smith .     pc - preconditioner context
1709f1580f4eSBarry Smith 
1710f1580f4eSBarry Smith    Output Parameter:
1711f1580f4eSBarry Smith .     share - whether the `KSP` is shared or not
1712f1580f4eSBarry Smith 
1713f1580f4eSBarry Smith    Note:
1714f1580f4eSBarry Smith      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
1715f1580f4eSBarry Smith      when using a subdomain `PCType` such as `PCLU` or `PCCHOLESKY`.
1716f1580f4eSBarry Smith 
1717f1580f4eSBarry Smith    Level: advanced
1718f1580f4eSBarry Smith 
1719e31fc274SPierre Jolivet .seealso: `PCHPDDM`, `PCHPDDMSetSTShareSubKSP()`
1720f1580f4eSBarry Smith @*/
1721d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMGetSTShareSubKSP(PC pc, PetscBool *share)
1722d71ae5a4SJacob Faibussowitsch {
1723f1580f4eSBarry Smith   PetscFunctionBegin;
1724f1580f4eSBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1725f1580f4eSBarry Smith   if (share) {
1726f1580f4eSBarry Smith     PetscValidBoolPointer(share, 2);
1727f1580f4eSBarry Smith     PetscUseMethod(pc, "PCHPDDMGetSTShareSubKSP_C", (PC, PetscBool *), (pc, share));
1728f1580f4eSBarry Smith   }
17293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1730f1580f4eSBarry Smith }
1731f1580f4eSBarry Smith 
1732e31fc274SPierre Jolivet static PetscErrorCode PCHPDDMSetSTShareSubKSP_HPDDM(PC pc, PetscBool share)
1733e31fc274SPierre Jolivet {
1734e31fc274SPierre Jolivet   PC_HPDDM *data = (PC_HPDDM *)pc->data;
1735e31fc274SPierre Jolivet 
1736e31fc274SPierre Jolivet   PetscFunctionBegin;
1737e31fc274SPierre Jolivet   data->share = share;
17383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1739e31fc274SPierre Jolivet }
1740e31fc274SPierre Jolivet 
1741d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMGetSTShareSubKSP_HPDDM(PC pc, PetscBool *share)
1742d71ae5a4SJacob Faibussowitsch {
1743f1580f4eSBarry Smith   PC_HPDDM *data = (PC_HPDDM *)pc->data;
1744f1580f4eSBarry Smith 
1745f1580f4eSBarry Smith   PetscFunctionBegin;
1746f1580f4eSBarry Smith   *share = data->share;
17473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1748f1580f4eSBarry Smith }
1749f1580f4eSBarry Smith 
1750f1580f4eSBarry Smith /*@
1751f1580f4eSBarry Smith      PCHPDDMSetDeflationMat - Sets the deflation space used to assemble a coarser operator.
1752f1580f4eSBarry Smith 
1753f1580f4eSBarry Smith    Input Parameters:
1754f1580f4eSBarry Smith +     pc - preconditioner context
1755f1580f4eSBarry Smith .     is - index set of the local deflation matrix
1756f1580f4eSBarry Smith -     U - deflation sequential matrix stored as a `MATSEQDENSE`
1757f1580f4eSBarry Smith 
1758f1580f4eSBarry Smith    Level: advanced
1759f1580f4eSBarry Smith 
1760f1580f4eSBarry Smith .seealso: `PCHPDDM`, `PCDeflationSetSpace()`, `PCMGSetRestriction()`
1761f1580f4eSBarry Smith @*/
1762d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMSetDeflationMat(PC pc, IS is, Mat U)
1763d71ae5a4SJacob Faibussowitsch {
1764f1580f4eSBarry Smith   PetscFunctionBegin;
1765f1580f4eSBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1766f1580f4eSBarry Smith   PetscValidHeaderSpecific(is, IS_CLASSID, 2);
1767f1580f4eSBarry Smith   PetscValidHeaderSpecific(U, MAT_CLASSID, 3);
1768e31fc274SPierre Jolivet   PetscTryMethod(pc, "PCHPDDMSetDeflationMat_C", (PC, IS, Mat), (pc, is, U));
17693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1770f1580f4eSBarry Smith }
1771f1580f4eSBarry Smith 
1772d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetDeflationMat_HPDDM(PC pc, IS is, Mat U)
1773d71ae5a4SJacob Faibussowitsch {
1774f1580f4eSBarry Smith   PetscFunctionBegin;
1775f1580f4eSBarry Smith   PetscCall(PCHPDDMSetAuxiliaryMat_Private(pc, is, U, PETSC_TRUE));
17763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1777f1580f4eSBarry Smith }
1778f1580f4eSBarry Smith 
1779d71ae5a4SJacob Faibussowitsch PetscErrorCode HPDDMLoadDL_Private(PetscBool *found)
1780d71ae5a4SJacob Faibussowitsch {
1781f1580f4eSBarry Smith   char lib[PETSC_MAX_PATH_LEN], dlib[PETSC_MAX_PATH_LEN], dir[PETSC_MAX_PATH_LEN];
1782f1580f4eSBarry Smith 
1783f1580f4eSBarry Smith   PetscFunctionBegin;
1784f1580f4eSBarry Smith   PetscValidBoolPointer(found, 1);
1785f1580f4eSBarry Smith   PetscCall(PetscStrcpy(dir, "${PETSC_LIB_DIR}"));
1786f1580f4eSBarry Smith   PetscCall(PetscOptionsGetString(NULL, NULL, "-hpddm_dir", dir, sizeof(dir), NULL));
1787f1580f4eSBarry Smith   PetscCall(PetscSNPrintf(lib, sizeof(lib), "%s/libhpddm_petsc", dir));
1788f1580f4eSBarry Smith   PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found));
1789f1580f4eSBarry Smith #if defined(SLEPC_LIB_DIR) /* this variable is passed during SLEPc ./configure since    */
1790f1580f4eSBarry Smith   if (!*found) {           /* slepcconf.h is not yet built (and thus can't be included) */
1791f1580f4eSBarry Smith     PetscCall(PetscStrcpy(dir, HPDDM_STR(SLEPC_LIB_DIR)));
1792f1580f4eSBarry Smith     PetscCall(PetscSNPrintf(lib, sizeof(lib), "%s/libhpddm_petsc", dir));
1793f1580f4eSBarry Smith     PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found));
1794f1580f4eSBarry Smith   }
1795f1580f4eSBarry Smith #endif
1796f1580f4eSBarry Smith   PetscCheck(*found, PETSC_COMM_SELF, PETSC_ERR_PLIB, "%s not found", lib);
1797f1580f4eSBarry Smith   PetscCall(PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, dlib));
17983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1799f1580f4eSBarry Smith }
1800f1580f4eSBarry Smith 
1801f1580f4eSBarry Smith /*MC
1802f1580f4eSBarry Smith      PCHPDDM - Interface with the HPDDM library.
1803f1580f4eSBarry Smith 
1804f1580f4eSBarry Smith    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
1805f1580f4eSBarry Smith    AMGe or `PCBDDC` with adaptive selection of constraints. The interface is explained in details in [2021] (see references below)
1806f1580f4eSBarry Smith 
18077eb095acSPierre Jolivet    The matrix to be preconditioned (Pmat) may be unassembled (`MATIS`), assembled (`MATAIJ`, `MATBAIJ`, or `MATSBAIJ`), hierarchical (`MATHTOOL`), or `MATNORMAL`.
1808f1580f4eSBarry Smith 
1809f1580f4eSBarry Smith    For multilevel preconditioning, when using an assembled or hierarchical Pmat, one must provide an auxiliary local `Mat` (unassembled local operator for GenEO) using
1810f1580f4eSBarry Smith    `PCHPDDMSetAuxiliaryMat()`. Calling this routine is not needed when using a `MATIS` Pmat, assembly is done internally using `MatConvert()`.
1811f1580f4eSBarry Smith 
1812f1580f4eSBarry Smith    Options Database Keys:
1813f1580f4eSBarry Smith +   -pc_hpddm_define_subdomains <true, default=false> - on the finest level, calls `PCASMSetLocalSubdomains()` with the `IS` supplied in `PCHPDDMSetAuxiliaryMat()`
1814f1580f4eSBarry Smith     (not relevant with an unassembled Pmat)
1815f1580f4eSBarry Smith .   -pc_hpddm_has_neumann <true, default=false> - on the finest level, informs the `PC` that the local Neumann matrix is supplied in `PCHPDDMSetAuxiliaryMat()`
1816f1580f4eSBarry Smith -   -pc_hpddm_coarse_correction <type, default=deflated> - determines the `PCHPDDMCoarseCorrectionType` when calling `PCApply()`
1817f1580f4eSBarry Smith 
181838bf2a8cSPierre Jolivet    Options for subdomain solvers, subdomain eigensolvers (for computing deflation vectors), and the coarse solver can be set using the following options database prefixes.
1819f1580f4eSBarry Smith .vb
1820f1580f4eSBarry Smith       -pc_hpddm_levels_%d_pc_
1821f1580f4eSBarry Smith       -pc_hpddm_levels_%d_ksp_
1822f1580f4eSBarry Smith       -pc_hpddm_levels_%d_eps_
1823f1580f4eSBarry Smith       -pc_hpddm_levels_%d_p
18244ec2a359SPierre Jolivet       -pc_hpddm_levels_%d_mat_type
1825f1580f4eSBarry Smith       -pc_hpddm_coarse_
1826f1580f4eSBarry Smith       -pc_hpddm_coarse_p
18274ec2a359SPierre Jolivet       -pc_hpddm_coarse_mat_type
18284ec2a359SPierre Jolivet       -pc_hpddm_coarse_mat_chop
1829f1580f4eSBarry Smith .ve
1830f1580f4eSBarry Smith 
183138bf2a8cSPierre Jolivet    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
1832f1580f4eSBarry Smith     -pc_hpddm_coarse_p 2 -pc_hpddm_coarse_mat_type baij will use 10 deflation vectors per subdomain on the fine "level 1",
1833f1580f4eSBarry Smith     aggregate the fine subdomains into 4 "level 2" subdomains, then use 10 deflation vectors per subdomain on "level 2",
18347eb095acSPierre Jolivet     and assemble the coarse matrix (of dimension 4 x 10 = 40) on two processes as a `MATBAIJ` (default is `MATSBAIJ`).
1835f1580f4eSBarry Smith 
1836f1580f4eSBarry Smith    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.
1837f1580f4eSBarry Smith 
183838bf2a8cSPierre Jolivet    This preconditioner requires that you build PETSc with SLEPc (``--download-slepc``). By default, the underlying concurrent eigenproblems
183938bf2a8cSPierre Jolivet    are solved using SLEPc shift-and-invert spectral transformation. This is usually what gives the best performance for GenEO, cf. [2011, 2013]. As
184038bf2a8cSPierre Jolivet    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_nev 10
184138bf2a8cSPierre Jolivet    -pc_hpddm_levels_1_st_type sinvert. There are furthermore three options related to the (subdomain-wise local) eigensolver that are not described in
184238bf2a8cSPierre Jolivet    SLEPc documentation since they are specific to `PCHPDDM`.
184338bf2a8cSPierre Jolivet .vb
184438bf2a8cSPierre Jolivet       -pc_hpddm_levels_1_st_share_sub_ksp
184538bf2a8cSPierre Jolivet       -pc_hpddm_levels_%d_eps_threshold
184638bf2a8cSPierre Jolivet       -pc_hpddm_levels_1_eps_use_inertia
184738bf2a8cSPierre Jolivet .ve
184838bf2a8cSPierre Jolivet 
184938bf2a8cSPierre Jolivet    The first option from the list only applies to the fine-level eigensolver, see `PCHPDDMSetSTShareSubKSP()`. The second option from the list is
185038bf2a8cSPierre Jolivet    used to filter eigenmodes retrieved after convergence of `EPSSolve()` at "level N" such that eigenvectors used to define a "level N+1" coarse
185138bf2a8cSPierre Jolivet    correction are associated to eigenvalues whose magnitude are lower or equal than -pc_hpddm_levels_N_eps_threshold. When using an `EPS` which cannot
185238bf2a8cSPierre Jolivet    determine a priori the proper -pc_hpddm_levels_N_eps_nev such that all wanted eigenmodes are retrieved, it is possible to get an estimation of the
185338bf2a8cSPierre Jolivet    correct value using the third option from the list, -pc_hpddm_levels_1_eps_use_inertia, see `MatGetInertia()`. In that case, there is no need
185438bf2a8cSPierre Jolivet    to supply -pc_hpddm_levels_1_eps_nev. This last option also only applies to the fine-level (N = 1) eigensolver.
1855f1580f4eSBarry Smith 
1856f1580f4eSBarry Smith    References:
1857f1580f4eSBarry Smith +   2011 - A robust two-level domain decomposition preconditioner for systems of PDEs. Spillane, Dolean, Hauret, Nataf, Pechstein, and Scheichl. Comptes Rendus Mathematique.
1858f1580f4eSBarry Smith .   2013 - Scalable domain decomposition preconditioners for heterogeneous elliptic problems. Jolivet, Hecht, Nataf, and Prud'homme. SC13.
1859f1580f4eSBarry Smith .   2015 - An introduction to domain decomposition methods: algorithms, theory, and parallel implementation. Dolean, Jolivet, and Nataf. SIAM.
1860f1580f4eSBarry Smith .   2019 - A multilevel Schwarz preconditioner based on a hierarchy of robust coarse spaces. Al Daas, Grigori, Jolivet, and Tournier. SIAM Journal on Scientific Computing.
1861f1580f4eSBarry Smith .   2021 - KSPHPDDM and PCHPDDM: extending PETSc with advanced Krylov methods and robust multilevel overlapping Schwarz preconditioners. Jolivet, Roman, and Zampini. Computer & Mathematics with Applications.
1862f1580f4eSBarry Smith .   2022a - A robust algebraic domain decomposition preconditioner for sparse normal equations. Al Daas, Jolivet, and Scott. SIAM Journal on Scientific Computing.
1863f1580f4eSBarry Smith -   2022b - A robust algebraic multilevel domain decomposition preconditioner for sparse symmetric positive definite matrices. Al Daas and Jolivet.
1864f1580f4eSBarry Smith 
1865f1580f4eSBarry Smith    Level: intermediate
1866f1580f4eSBarry Smith 
1867f1580f4eSBarry Smith .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCHPDDMSetAuxiliaryMat()`, `MATIS`, `PCBDDC`, `PCDEFLATION`, `PCTELESCOPE`, `PCASM`,
1868e31fc274SPierre Jolivet           `PCHPDDMSetCoarseCorrectionType()`, `PCHPDDMHasNeumannMat()`, `PCHPDDMSetRHSMat()`, `PCHPDDMSetDeflationMat()`, `PCHPDDMSetSTShareSubKSP()`,
1869e31fc274SPierre Jolivet           `PCHPDDMGetSTShareSubKSP()`, `PCHPDDMGetCoarseCorrectionType()`, `PCHPDDMGetComplexities()`
1870f1580f4eSBarry Smith M*/
1871d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_HPDDM(PC pc)
1872d71ae5a4SJacob Faibussowitsch {
1873f1580f4eSBarry Smith   PC_HPDDM *data;
1874f1580f4eSBarry Smith   PetscBool found;
1875f1580f4eSBarry Smith 
1876f1580f4eSBarry Smith   PetscFunctionBegin;
1877f1580f4eSBarry Smith   if (!loadedSym) {
1878f1580f4eSBarry Smith     PetscCall(HPDDMLoadDL_Private(&found));
1879f1580f4eSBarry Smith     if (found) PetscCall(PetscDLLibrarySym(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, NULL, "PCHPDDM_Internal", (void **)&loadedSym));
1880f1580f4eSBarry Smith   }
1881f1580f4eSBarry Smith   PetscCheck(loadedSym, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCHPDDM_Internal symbol not found in loaded libhpddm_petsc");
18824dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&data));
1883f1580f4eSBarry Smith   pc->data                = data;
1884c8ea6600SPierre Jolivet   data->Neumann           = PETSC_BOOL3_UNKNOWN;
1885f1580f4eSBarry Smith   pc->ops->reset          = PCReset_HPDDM;
1886f1580f4eSBarry Smith   pc->ops->destroy        = PCDestroy_HPDDM;
1887f1580f4eSBarry Smith   pc->ops->setfromoptions = PCSetFromOptions_HPDDM;
1888f1580f4eSBarry Smith   pc->ops->setup          = PCSetUp_HPDDM;
1889f1580f4eSBarry Smith   pc->ops->apply          = PCApply_HPDDM;
1890f1580f4eSBarry Smith   pc->ops->matapply       = PCMatApply_HPDDM;
1891f1580f4eSBarry Smith   pc->ops->view           = PCView_HPDDM;
1892f1580f4eSBarry Smith   pc->ops->presolve       = PCPreSolve_HPDDM;
1893f1580f4eSBarry Smith 
1894f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetAuxiliaryMat_C", PCHPDDMSetAuxiliaryMat_HPDDM));
1895f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMHasNeumannMat_C", PCHPDDMHasNeumannMat_HPDDM));
1896f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetRHSMat_C", PCHPDDMSetRHSMat_HPDDM));
1897f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetCoarseCorrectionType_C", PCHPDDMSetCoarseCorrectionType_HPDDM));
1898f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetCoarseCorrectionType_C", PCHPDDMGetCoarseCorrectionType_HPDDM));
1899e31fc274SPierre Jolivet   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetSTShareSubKSP_C", PCHPDDMSetSTShareSubKSP_HPDDM));
1900f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetSTShareSubKSP_C", PCHPDDMGetSTShareSubKSP_HPDDM));
1901f1580f4eSBarry Smith   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetDeflationMat_C", PCHPDDMSetDeflationMat_HPDDM));
19023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1903f1580f4eSBarry Smith }
1904f1580f4eSBarry Smith 
1905f1580f4eSBarry Smith /*@C
1906f1580f4eSBarry Smith      PCHPDDMInitializePackage - This function initializes everything in the `PCHPDDM` package. It is called from `PCInitializePackage()`.
1907f1580f4eSBarry Smith 
1908f1580f4eSBarry Smith    Level: developer
1909f1580f4eSBarry Smith 
1910f1580f4eSBarry Smith .seealso: `PetscInitialize()`
1911f1580f4eSBarry Smith @*/
1912d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMInitializePackage(void)
1913d71ae5a4SJacob Faibussowitsch {
1914f1580f4eSBarry Smith   char     ename[32];
1915f1580f4eSBarry Smith   PetscInt i;
1916f1580f4eSBarry Smith 
1917f1580f4eSBarry Smith   PetscFunctionBegin;
19183ba16761SJacob Faibussowitsch   if (PCHPDDMPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
1919f1580f4eSBarry Smith   PCHPDDMPackageInitialized = PETSC_TRUE;
1920f1580f4eSBarry Smith   PetscCall(PetscRegisterFinalize(PCHPDDMFinalizePackage));
1921f1580f4eSBarry Smith   /* general events registered once during package initialization */
1922f1580f4eSBarry Smith   /* some of these events are not triggered in libpetsc,          */
1923f1580f4eSBarry Smith   /* but rather directly in libhpddm_petsc,                       */
1924f1580f4eSBarry Smith   /* which is in charge of performing the following operations    */
1925f1580f4eSBarry Smith 
1926f1580f4eSBarry Smith   /* domain decomposition structure from Pmat sparsity pattern    */
1927f1580f4eSBarry Smith   PetscCall(PetscLogEventRegister("PCHPDDMStrc", PC_CLASSID, &PC_HPDDM_Strc));
1928f1580f4eSBarry Smith   /* Galerkin product, redistribution, and setup (not triggered in libpetsc)                */
1929f1580f4eSBarry Smith   PetscCall(PetscLogEventRegister("PCHPDDMPtAP", PC_CLASSID, &PC_HPDDM_PtAP));
1930f1580f4eSBarry Smith   /* Galerkin product with summation, redistribution, and setup (not triggered in libpetsc) */
1931f1580f4eSBarry Smith   PetscCall(PetscLogEventRegister("PCHPDDMPtBP", PC_CLASSID, &PC_HPDDM_PtBP));
1932f1580f4eSBarry Smith   /* next level construction using PtAP and PtBP (not triggered in libpetsc)                */
1933f1580f4eSBarry Smith   PetscCall(PetscLogEventRegister("PCHPDDMNext", PC_CLASSID, &PC_HPDDM_Next));
1934f1580f4eSBarry Smith   static_assert(PETSC_PCHPDDM_MAXLEVELS <= 9, "PETSC_PCHPDDM_MAXLEVELS value is too high");
1935f1580f4eSBarry Smith   for (i = 1; i < PETSC_PCHPDDM_MAXLEVELS; ++i) {
1936f1580f4eSBarry Smith     PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCHPDDMSetUp L%1" PetscInt_FMT, i));
1937f1580f4eSBarry Smith     /* events during a PCSetUp() at level #i _except_ the assembly */
1938f1580f4eSBarry Smith     /* of the Galerkin operator of the coarser level #(i + 1)      */
1939f1580f4eSBarry Smith     PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &PC_HPDDM_SetUp[i - 1]));
1940f1580f4eSBarry Smith     PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCHPDDMSolve L%1" PetscInt_FMT, i));
1941f1580f4eSBarry Smith     /* events during a PCApply() at level #i _except_              */
1942f1580f4eSBarry Smith     /* the KSPSolve() of the coarser level #(i + 1)                */
1943f1580f4eSBarry Smith     PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &PC_HPDDM_Solve[i - 1]));
1944f1580f4eSBarry Smith   }
19453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1946f1580f4eSBarry Smith }
1947f1580f4eSBarry Smith 
1948f1580f4eSBarry Smith /*@C
1949f1580f4eSBarry Smith      PCHPDDMFinalizePackage - This function frees everything from the `PCHPDDM` package. It is called from `PetscFinalize()`.
1950f1580f4eSBarry Smith 
1951f1580f4eSBarry Smith    Level: developer
1952f1580f4eSBarry Smith 
1953f1580f4eSBarry Smith .seealso: `PetscFinalize()`
1954f1580f4eSBarry Smith @*/
1955d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMFinalizePackage(void)
1956d71ae5a4SJacob Faibussowitsch {
1957f1580f4eSBarry Smith   PetscFunctionBegin;
1958f1580f4eSBarry Smith   PCHPDDMPackageInitialized = PETSC_FALSE;
19593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1960f1580f4eSBarry Smith }
1961