1ee25009cSBoris Martin #include <petscsf.h>
213044ca3SPierre Jolivet #include <petsc/private/vecimpl.h>
3f1580f4eSBarry Smith #include <petsc/private/matimpl.h>
4f1580f4eSBarry Smith #include <petsc/private/petschpddm.h> /*I "petscpc.h" I*/
5e905f78bSJacob Faibussowitsch #include <petsc/private/pcimpl.h>
6e905f78bSJacob Faibussowitsch #include <petsc/private/dmimpl.h> /* this must be included after petschpddm.h so that DM_MAX_WORK_VECTORS is not defined */
7f1580f4eSBarry Smith /* otherwise, it is assumed that one is compiling libhpddm_petsc => circular dependency */
8f1580f4eSBarry Smith
9db4a47b3SPierre Jolivet static PetscErrorCode (*loadedSym)(HPDDM::Schwarz<PetscScalar> *const, IS, Mat, Mat, Mat, std::vector<Vec>, PC_HPDDM_Level **const) = nullptr;
10f1580f4eSBarry Smith
11f1580f4eSBarry Smith static PetscBool PCHPDDMPackageInitialized = PETSC_FALSE;
12f1580f4eSBarry Smith
13f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_Strc;
14f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_PtAP;
15f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_PtBP;
16f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_Next;
17f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_SetUp[PETSC_PCHPDDM_MAXLEVELS];
18f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_Solve[PETSC_PCHPDDM_MAXLEVELS];
19f1580f4eSBarry Smith
20aa1539e9SPierre Jolivet const char *const PCHPDDMCoarseCorrectionTypes[] = {"DEFLATED", "ADDITIVE", "BALANCED", "NONE", "PCHPDDMCoarseCorrectionType", "PC_HPDDM_COARSE_CORRECTION_", nullptr};
2113044ca3SPierre Jolivet const char *const PCHPDDMSchurPreTypes[] = {"LEAST_SQUARES", "GENEO", "PCHPDDMSchurPreType", "PC_HPDDM_SCHUR_PRE", nullptr};
22f1580f4eSBarry Smith
PCReset_HPDDM(PC pc)23d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_HPDDM(PC pc)
24d71ae5a4SJacob Faibussowitsch {
25f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
26f1580f4eSBarry Smith
27f1580f4eSBarry Smith PetscFunctionBegin;
28f1580f4eSBarry Smith if (data->levels) {
29811e8887SPierre Jolivet for (PetscInt i = 0; i < PETSC_PCHPDDM_MAXLEVELS && data->levels[i]; ++i) {
30f1580f4eSBarry Smith PetscCall(KSPDestroy(&data->levels[i]->ksp));
31f1580f4eSBarry Smith PetscCall(PCDestroy(&data->levels[i]->pc));
32f1580f4eSBarry Smith PetscCall(PetscFree(data->levels[i]));
33f1580f4eSBarry Smith }
34f1580f4eSBarry Smith PetscCall(PetscFree(data->levels));
35f1580f4eSBarry Smith }
36f1580f4eSBarry Smith PetscCall(ISDestroy(&data->is));
37f1580f4eSBarry Smith PetscCall(MatDestroy(&data->aux));
38f1580f4eSBarry Smith PetscCall(MatDestroy(&data->B));
39f1580f4eSBarry Smith PetscCall(VecDestroy(&data->normal));
40f1580f4eSBarry Smith data->correction = PC_HPDDM_COARSE_CORRECTION_DEFLATED;
41c8ea6600SPierre Jolivet data->Neumann = PETSC_BOOL3_UNKNOWN;
42f1580f4eSBarry Smith data->deflation = PETSC_FALSE;
43db4a47b3SPierre Jolivet data->setup = nullptr;
44db4a47b3SPierre Jolivet data->setup_ctx = nullptr;
453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
46f1580f4eSBarry Smith }
47f1580f4eSBarry Smith
PCDestroy_HPDDM(PC pc)48d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_HPDDM(PC pc)
49d71ae5a4SJacob Faibussowitsch {
50f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
51f1580f4eSBarry Smith
52f1580f4eSBarry Smith PetscFunctionBegin;
53f1580f4eSBarry Smith PetscCall(PCReset_HPDDM(pc));
54f1580f4eSBarry Smith PetscCall(PetscFree(data));
55db4a47b3SPierre Jolivet PetscCall(PetscObjectChangeTypeName((PetscObject)pc, nullptr));
56db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetAuxiliaryMat_C", nullptr));
57db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMHasNeumannMat_C", nullptr));
58db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetRHSMat_C", nullptr));
59db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetCoarseCorrectionType_C", nullptr));
60db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetCoarseCorrectionType_C", nullptr));
61db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetSTShareSubKSP_C", nullptr));
62db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetSTShareSubKSP_C", nullptr));
63db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetDeflationMat_C", nullptr));
6413044ca3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)pc, "_PCHPDDM_Schur", nullptr));
653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
66f1580f4eSBarry Smith }
67f1580f4eSBarry Smith
PCHPDDMSetAuxiliaryMat_Private(PC pc,IS is,Mat A,PetscBool deflation)68d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PCHPDDMSetAuxiliaryMat_Private(PC pc, IS is, Mat A, PetscBool deflation)
69d71ae5a4SJacob Faibussowitsch {
70f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
71cdbd50ebSPierre Jolivet PCHPDDMCoarseCorrectionType type = data->correction;
72f1580f4eSBarry Smith
73f1580f4eSBarry Smith PetscFunctionBegin;
74c30dab7bSPierre Jolivet PetscValidLogicalCollectiveBool(pc, deflation, 4);
75c30dab7bSPierre Jolivet if (is && A) {
76c30dab7bSPierre Jolivet PetscInt m[2];
77c30dab7bSPierre Jolivet
78c30dab7bSPierre Jolivet PetscCall(ISGetLocalSize(is, m));
79c30dab7bSPierre Jolivet PetscCall(MatGetLocalSize(A, m + 1, nullptr));
80c30dab7bSPierre Jolivet PetscCheck(m[0] == m[1], PETSC_COMM_SELF, PETSC_ERR_USER_INPUT, "Inconsistent IS and Mat sizes (%" PetscInt_FMT " v. %" PetscInt_FMT ")", m[0], m[1]);
81c30dab7bSPierre Jolivet }
82f1580f4eSBarry Smith if (is) {
83f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)is));
84f1580f4eSBarry Smith if (data->is) { /* new overlap definition resets the PC */
85f1580f4eSBarry Smith PetscCall(PCReset_HPDDM(pc));
86f1580f4eSBarry Smith pc->setfromoptionscalled = 0;
87371d2eb7SMartin Diehl pc->setupcalled = PETSC_FALSE;
88cdbd50ebSPierre Jolivet data->correction = type;
89f1580f4eSBarry Smith }
90f1580f4eSBarry Smith PetscCall(ISDestroy(&data->is));
91f1580f4eSBarry Smith data->is = is;
92f1580f4eSBarry Smith }
93f1580f4eSBarry Smith if (A) {
94f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)A));
95f1580f4eSBarry Smith PetscCall(MatDestroy(&data->aux));
96f1580f4eSBarry Smith data->aux = A;
97f1580f4eSBarry Smith }
98f1580f4eSBarry Smith data->deflation = deflation;
993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
100f1580f4eSBarry Smith }
101f1580f4eSBarry Smith
PCHPDDMSplittingMatNormal_Private(Mat A,IS * is,Mat * splitting[])102281f8ce6SPierre Jolivet static inline PetscErrorCode PCHPDDMSplittingMatNormal_Private(Mat A, IS *is, Mat *splitting[])
103281f8ce6SPierre Jolivet {
104281f8ce6SPierre Jolivet Mat *sub;
105281f8ce6SPierre Jolivet IS zero;
106281f8ce6SPierre Jolivet
107281f8ce6SPierre Jolivet PetscFunctionBegin;
108281f8ce6SPierre Jolivet PetscCall(MatSetOption(A, MAT_SUBMAT_SINGLEIS, PETSC_TRUE));
109281f8ce6SPierre Jolivet PetscCall(MatCreateSubMatrices(A, 1, is + 2, is, MAT_INITIAL_MATRIX, splitting));
110281f8ce6SPierre Jolivet PetscCall(MatCreateSubMatrices(**splitting, 1, is + 2, is + 1, MAT_INITIAL_MATRIX, &sub));
111281f8ce6SPierre Jolivet PetscCall(MatFindZeroRows(*sub, &zero));
112281f8ce6SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &sub));
113281f8ce6SPierre Jolivet PetscCall(MatSetOption(**splitting, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE));
114281f8ce6SPierre Jolivet PetscCall(MatZeroRowsIS(**splitting, zero, 0.0, nullptr, nullptr));
115281f8ce6SPierre Jolivet PetscCall(ISDestroy(&zero));
116281f8ce6SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
117281f8ce6SPierre Jolivet }
118281f8ce6SPierre Jolivet
PCHPDDMSetAuxiliaryMatNormal_Private(PC pc,Mat A,Mat N,Mat * B,const char * pcpre,Vec * diagonal=nullptr,Mat B01=nullptr)119281f8ce6SPierre Jolivet static inline PetscErrorCode PCHPDDMSetAuxiliaryMatNormal_Private(PC pc, Mat A, Mat N, Mat *B, const char *pcpre, Vec *diagonal = nullptr, Mat B01 = nullptr)
120d71ae5a4SJacob Faibussowitsch {
121f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
122281f8ce6SPierre Jolivet Mat *splitting[2] = {}, aux;
1233df4cd7bSPierre Jolivet Vec d;
124281f8ce6SPierre Jolivet IS is[3];
125f1580f4eSBarry Smith PetscReal norm;
126f1580f4eSBarry Smith PetscBool flg;
127f1580f4eSBarry Smith char type[256] = {}; /* same size as in src/ksp/pc/interface/pcset.c */
128f1580f4eSBarry Smith
129f1580f4eSBarry Smith PetscFunctionBegin;
130281f8ce6SPierre Jolivet if (!B01) PetscCall(MatConvert(N, MATAIJ, MAT_INITIAL_MATRIX, B));
131281f8ce6SPierre Jolivet else PetscCall(MatTransposeMatMult(B01, A, MAT_INITIAL_MATRIX, PETSC_DETERMINE, B));
132feebddf4SPierre Jolivet PetscCall(MatEliminateZeros(*B, PETSC_TRUE));
133281f8ce6SPierre Jolivet PetscCall(ISCreateStride(PETSC_COMM_SELF, A->cmap->n, A->cmap->rstart, 1, is));
134281f8ce6SPierre Jolivet PetscCall(MatIncreaseOverlap(*B, 1, is, 1));
135281f8ce6SPierre Jolivet PetscCall(ISCreateStride(PETSC_COMM_SELF, A->cmap->n, A->cmap->rstart, 1, is + 2));
136281f8ce6SPierre Jolivet PetscCall(ISEmbed(is[0], is[2], PETSC_TRUE, is + 1));
137281f8ce6SPierre Jolivet PetscCall(ISDestroy(is + 2));
138281f8ce6SPierre Jolivet PetscCall(ISCreateStride(PETSC_COMM_SELF, A->rmap->N, 0, 1, is + 2));
139281f8ce6SPierre Jolivet PetscCall(PCHPDDMSplittingMatNormal_Private(A, is, &splitting[0]));
140281f8ce6SPierre Jolivet if (B01) {
141281f8ce6SPierre Jolivet PetscCall(PCHPDDMSplittingMatNormal_Private(B01, is, &splitting[1]));
142281f8ce6SPierre Jolivet PetscCall(MatDestroy(&B01));
143281f8ce6SPierre Jolivet }
144281f8ce6SPierre Jolivet PetscCall(ISDestroy(is + 2));
145281f8ce6SPierre Jolivet PetscCall(ISDestroy(is + 1));
1468dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetString(((PetscObject)pc)->options, pcpre, "-pc_hpddm_levels_1_sub_pc_type", type, sizeof(type), nullptr));
147f1580f4eSBarry Smith PetscCall(PetscStrcmp(type, PCQR, &flg));
148f1580f4eSBarry Smith if (!flg) {
149281f8ce6SPierre Jolivet Mat conjugate = *splitting[splitting[1] ? 1 : 0];
150811e8887SPierre Jolivet
151281f8ce6SPierre Jolivet if (PetscDefined(USE_COMPLEX) && !splitting[1]) {
152281f8ce6SPierre Jolivet PetscCall(MatDuplicate(*splitting[0], MAT_COPY_VALUES, &conjugate));
153f1580f4eSBarry Smith PetscCall(MatConjugate(conjugate));
154f1580f4eSBarry Smith }
155281f8ce6SPierre Jolivet PetscCall(MatTransposeMatMult(conjugate, *splitting[0], MAT_INITIAL_MATRIX, PETSC_DETERMINE, &aux));
156281f8ce6SPierre Jolivet if (PetscDefined(USE_COMPLEX) && !splitting[1]) PetscCall(MatDestroy(&conjugate));
157281f8ce6SPierre Jolivet else if (splitting[1]) PetscCall(MatDestroySubMatrices(1, &splitting[1]));
158f1580f4eSBarry Smith PetscCall(MatNorm(aux, NORM_FROBENIUS, &norm));
159f1580f4eSBarry Smith PetscCall(MatSetOption(aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
1603df4cd7bSPierre Jolivet if (diagonal) {
161811e8887SPierre Jolivet PetscReal norm;
162811e8887SPierre Jolivet
163feebddf4SPierre Jolivet PetscCall(VecScale(*diagonal, -1.0));
1643df4cd7bSPierre Jolivet PetscCall(VecNorm(*diagonal, NORM_INFINITY, &norm));
1653df4cd7bSPierre Jolivet if (norm > PETSC_SMALL) {
16601e3c840SPierre Jolivet PetscSF scatter;
1673df4cd7bSPierre Jolivet PetscInt n;
168811e8887SPierre Jolivet
169281f8ce6SPierre Jolivet PetscCall(ISGetLocalSize(*is, &n));
1703df4cd7bSPierre Jolivet PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)pc), n, PETSC_DECIDE, &d));
171281f8ce6SPierre Jolivet PetscCall(VecScatterCreate(*diagonal, *is, d, nullptr, &scatter));
1723df4cd7bSPierre Jolivet PetscCall(VecScatterBegin(scatter, *diagonal, d, INSERT_VALUES, SCATTER_FORWARD));
1733df4cd7bSPierre Jolivet PetscCall(VecScatterEnd(scatter, *diagonal, d, INSERT_VALUES, SCATTER_FORWARD));
17401e3c840SPierre Jolivet PetscCall(PetscSFDestroy(&scatter));
1753df4cd7bSPierre Jolivet PetscCall(MatDiagonalSet(aux, d, ADD_VALUES));
1763df4cd7bSPierre Jolivet PetscCall(VecDestroy(&d));
1773df4cd7bSPierre Jolivet } else PetscCall(VecDestroy(diagonal));
1783df4cd7bSPierre Jolivet }
1793df4cd7bSPierre Jolivet if (!diagonal) PetscCall(MatShift(aux, PETSC_SMALL * norm));
180e5634b20SPierre Jolivet PetscCall(MatEliminateZeros(aux, PETSC_TRUE));
181f1580f4eSBarry Smith } else {
182f1580f4eSBarry Smith PetscBool flg;
183811e8887SPierre Jolivet
184281f8ce6SPierre Jolivet PetscCheck(!splitting[1], PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Cannot use PCQR when A01 != A10^T");
1853df4cd7bSPierre Jolivet if (diagonal) {
1863df4cd7bSPierre Jolivet PetscCall(VecNorm(*diagonal, NORM_INFINITY, &norm));
1873df4cd7bSPierre Jolivet PetscCheck(norm < PETSC_SMALL, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Nonzero diagonal A11 block");
1883df4cd7bSPierre Jolivet PetscCall(VecDestroy(diagonal));
1893df4cd7bSPierre Jolivet }
190f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)N, MATNORMAL, &flg));
191281f8ce6SPierre Jolivet if (flg) PetscCall(MatCreateNormal(*splitting[0], &aux));
192281f8ce6SPierre Jolivet else PetscCall(MatCreateNormalHermitian(*splitting[0], &aux));
193f1580f4eSBarry Smith }
194281f8ce6SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &splitting[0]));
195281f8ce6SPierre Jolivet PetscCall(PCHPDDMSetAuxiliaryMat(pc, *is, aux, nullptr, nullptr));
196c8ea6600SPierre Jolivet data->Neumann = PETSC_BOOL3_TRUE;
197281f8ce6SPierre Jolivet PetscCall(ISDestroy(is));
198f1580f4eSBarry Smith PetscCall(MatDestroy(&aux));
1993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
200f1580f4eSBarry Smith }
201f1580f4eSBarry Smith
PCHPDDMSetAuxiliaryMat_HPDDM(PC pc,IS is,Mat A,PetscErrorCode (* setup)(Mat,PetscReal,Vec,Vec,PetscReal,IS,void *),void * setup_ctx)202d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetAuxiliaryMat_HPDDM(PC pc, IS is, Mat A, PetscErrorCode (*setup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void *setup_ctx)
203d71ae5a4SJacob Faibussowitsch {
204f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
205f1580f4eSBarry Smith
206f1580f4eSBarry Smith PetscFunctionBegin;
207f1580f4eSBarry Smith PetscCall(PCHPDDMSetAuxiliaryMat_Private(pc, is, A, PETSC_FALSE));
208f1580f4eSBarry Smith if (setup) {
209f1580f4eSBarry Smith data->setup = setup;
210f1580f4eSBarry Smith data->setup_ctx = setup_ctx;
211f1580f4eSBarry Smith }
2123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
213f1580f4eSBarry Smith }
214f1580f4eSBarry Smith
21570009435SPierre Jolivet /*@C
21604c3f3b8SBarry Smith PCHPDDMSetAuxiliaryMat - Sets the auxiliary matrix used by `PCHPDDM` for the concurrent GenEO problems at the finest level.
217f1580f4eSBarry Smith
218f1580f4eSBarry Smith Input Parameters:
219f1580f4eSBarry Smith + pc - preconditioner context
220f1580f4eSBarry Smith . is - index set of the local auxiliary, e.g., Neumann, matrix
221f1580f4eSBarry Smith . A - auxiliary sequential matrix
22204c3f3b8SBarry Smith . setup - function for generating the auxiliary matrix entries, may be `NULL`
22304c3f3b8SBarry Smith - ctx - context for `setup`, may be `NULL`
22404c3f3b8SBarry Smith
22504c3f3b8SBarry Smith Calling sequence of `setup`:
22604c3f3b8SBarry Smith + J - matrix whose values are to be set
22704c3f3b8SBarry Smith . t - time
22804c3f3b8SBarry Smith . X - linearization point
22904c3f3b8SBarry Smith . X_t - time-derivative of the linearization point
23004c3f3b8SBarry Smith . s - step
23104c3f3b8SBarry Smith . ovl - index set of the local auxiliary, e.g., Neumann, matrix
23204c3f3b8SBarry Smith - ctx - context for `setup`, may be `NULL`
233f1580f4eSBarry Smith
234f1580f4eSBarry Smith Level: intermediate
235f1580f4eSBarry Smith
23604c3f3b8SBarry Smith Note:
23704c3f3b8SBarry Smith As an example, in a finite element context with nonoverlapping subdomains plus (overlapping) ghost elements, this could be the unassembled (Neumann)
23804c3f3b8SBarry Smith local overlapping operator. As opposed to the assembled (Dirichlet) local overlapping operator obtained by summing neighborhood contributions
23904c3f3b8SBarry Smith at the interface of ghost elements.
24004c3f3b8SBarry Smith
24170009435SPierre Jolivet Fortran Notes:
24204c3f3b8SBarry Smith Only `PETSC_NULL_FUNCTION` is supported for `setup` and `ctx` is never accessed
24370009435SPierre Jolivet
244562efe2eSBarry Smith .seealso: [](ch_ksp), `PCHPDDM`, `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCHPDDMSetRHSMat()`, `MATIS`
245f1580f4eSBarry Smith @*/
PCHPDDMSetAuxiliaryMat(PC pc,IS is,Mat A,PetscErrorCode (* setup)(Mat J,PetscReal t,Vec X,Vec X_t,PetscReal s,IS ovl,PetscCtx ctx),PetscCtx ctx)2462a8381b2SBarry Smith PetscErrorCode PCHPDDMSetAuxiliaryMat(PC pc, IS is, Mat A, PetscErrorCode (*setup)(Mat J, PetscReal t, Vec X, Vec X_t, PetscReal s, IS ovl, PetscCtx ctx), PetscCtx ctx)
247d71ae5a4SJacob Faibussowitsch {
248f1580f4eSBarry Smith PetscFunctionBegin;
249f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
250f1580f4eSBarry Smith if (is) PetscValidHeaderSpecific(is, IS_CLASSID, 2);
251f1580f4eSBarry Smith if (A) PetscValidHeaderSpecific(A, MAT_CLASSID, 3);
25204c3f3b8SBarry Smith PetscTryMethod(pc, "PCHPDDMSetAuxiliaryMat_C", (PC, IS, Mat, PetscErrorCode (*)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void *), (pc, is, A, setup, ctx));
2533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
254f1580f4eSBarry Smith }
255f1580f4eSBarry Smith
PCHPDDMHasNeumannMat_HPDDM(PC pc,PetscBool has)256d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMHasNeumannMat_HPDDM(PC pc, PetscBool has)
257d71ae5a4SJacob Faibussowitsch {
258f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
259f1580f4eSBarry Smith
260f1580f4eSBarry Smith PetscFunctionBegin;
261c8ea6600SPierre Jolivet data->Neumann = PetscBoolToBool3(has);
2623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
263f1580f4eSBarry Smith }
264f1580f4eSBarry Smith
265f1580f4eSBarry Smith /*@
266f1580f4eSBarry Smith PCHPDDMHasNeumannMat - Informs `PCHPDDM` that the `Mat` passed to `PCHPDDMSetAuxiliaryMat()` is the local Neumann matrix.
267f1580f4eSBarry Smith
268f1580f4eSBarry Smith Input Parameters:
269f1580f4eSBarry Smith + pc - preconditioner context
270f1580f4eSBarry Smith - has - Boolean value
271f1580f4eSBarry Smith
272f1580f4eSBarry Smith Level: intermediate
273f1580f4eSBarry Smith
274f1580f4eSBarry Smith Notes:
2757eb095acSPierre Jolivet This may be used to bypass a call to `MatCreateSubMatrices()` and to `MatConvert()` for `MATSBAIJ` matrices.
276f1580f4eSBarry Smith
277907a3e9cSStefano Zampini If a function is composed with DMCreateNeumannOverlap_C 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`.
278f1580f4eSBarry Smith
279562efe2eSBarry Smith .seealso: [](ch_ksp), `PCHPDDM`, `PCHPDDMSetAuxiliaryMat()`
280f1580f4eSBarry Smith @*/
PCHPDDMHasNeumannMat(PC pc,PetscBool has)281d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMHasNeumannMat(PC pc, PetscBool has)
282d71ae5a4SJacob Faibussowitsch {
283f1580f4eSBarry Smith PetscFunctionBegin;
284f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
285f1580f4eSBarry Smith PetscTryMethod(pc, "PCHPDDMHasNeumannMat_C", (PC, PetscBool), (pc, has));
2863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
287f1580f4eSBarry Smith }
288f1580f4eSBarry Smith
PCHPDDMSetRHSMat_HPDDM(PC pc,Mat B)289d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetRHSMat_HPDDM(PC pc, Mat B)
290d71ae5a4SJacob Faibussowitsch {
291f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
292f1580f4eSBarry Smith
293f1580f4eSBarry Smith PetscFunctionBegin;
294f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)B));
295f1580f4eSBarry Smith PetscCall(MatDestroy(&data->B));
296f1580f4eSBarry Smith data->B = B;
2973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
298f1580f4eSBarry Smith }
299f1580f4eSBarry Smith
300f1580f4eSBarry Smith /*@
30104c3f3b8SBarry Smith PCHPDDMSetRHSMat - Sets the right-hand side matrix used by `PCHPDDM` for the concurrent GenEO problems at the finest level.
302f1580f4eSBarry Smith
303f1580f4eSBarry Smith Input Parameters:
304f1580f4eSBarry Smith + pc - preconditioner context
305f1580f4eSBarry Smith - B - right-hand side sequential matrix
306f1580f4eSBarry Smith
307f1580f4eSBarry Smith Level: advanced
308f1580f4eSBarry Smith
30904c3f3b8SBarry Smith Note:
31004c3f3b8SBarry Smith Must be used in conjunction with `PCHPDDMSetAuxiliaryMat`(N), so that Nv = lambda Bv is solved using `EPSSetOperators`(N, B).
31104c3f3b8SBarry Smith 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.
31204c3f3b8SBarry Smith
313562efe2eSBarry Smith .seealso: [](ch_ksp), `PCHPDDMSetAuxiliaryMat()`, `PCHPDDM`
314f1580f4eSBarry Smith @*/
PCHPDDMSetRHSMat(PC pc,Mat B)315d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMSetRHSMat(PC pc, Mat B)
316d71ae5a4SJacob Faibussowitsch {
317f1580f4eSBarry Smith PetscFunctionBegin;
318f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
319f1580f4eSBarry Smith if (B) {
320f1580f4eSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 2);
321f1580f4eSBarry Smith PetscTryMethod(pc, "PCHPDDMSetRHSMat_C", (PC, Mat), (pc, B));
322f1580f4eSBarry Smith }
3233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
324f1580f4eSBarry Smith }
325f1580f4eSBarry Smith
PCSetFromOptions_HPDDM(PC pc,PetscOptionItems PetscOptionsObject)326ce78bad3SBarry Smith static PetscErrorCode PCSetFromOptions_HPDDM(PC pc, PetscOptionItems PetscOptionsObject)
327d71ae5a4SJacob Faibussowitsch {
328f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
329f1580f4eSBarry Smith PC_HPDDM_Level **levels = data->levels;
330b5a302b3SPierre Jolivet char prefix[256], deprecated[256];
331f1580f4eSBarry Smith int i = 1;
332f1580f4eSBarry Smith PetscMPIInt size, previous;
3339bb5c669SPierre Jolivet PetscInt n, overlap = 1;
334f1580f4eSBarry Smith PCHPDDMCoarseCorrectionType type;
335c8ea6600SPierre Jolivet PetscBool flg = PETSC_TRUE, set;
336f1580f4eSBarry Smith
337f1580f4eSBarry Smith PetscFunctionBegin;
338f1580f4eSBarry Smith if (!data->levels) {
339f1580f4eSBarry Smith PetscCall(PetscCalloc1(PETSC_PCHPDDM_MAXLEVELS, &levels));
340f1580f4eSBarry Smith data->levels = levels;
341f1580f4eSBarry Smith }
342f1580f4eSBarry Smith PetscOptionsHeadBegin(PetscOptionsObject, "PCHPDDM options");
3439bb5c669SPierre Jolivet PetscCall(PetscOptionsBoundedInt("-pc_hpddm_harmonic_overlap", "Overlap prior to computing local harmonic extensions", "PCHPDDM", overlap, &overlap, &set, 1));
3449bb5c669SPierre Jolivet if (!set) overlap = -1;
345f1580f4eSBarry Smith PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
346f1580f4eSBarry Smith previous = size;
347f1580f4eSBarry Smith while (i < PETSC_PCHPDDM_MAXLEVELS) {
348f1580f4eSBarry Smith PetscInt p = 1;
349f1580f4eSBarry Smith
3504dfa11a4SJacob Faibussowitsch if (!data->levels[i - 1]) PetscCall(PetscNew(data->levels + i - 1));
351f1580f4eSBarry Smith data->levels[i - 1]->parent = data;
352f1580f4eSBarry Smith /* if the previous level has a single process, it is not possible to coarsen further */
353f1580f4eSBarry Smith if (previous == 1 || !flg) break;
354f1580f4eSBarry Smith data->levels[i - 1]->nu = 0;
355f1580f4eSBarry Smith data->levels[i - 1]->threshold = -1.0;
356f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_nev", i));
357db4a47b3SPierre Jolivet PetscCall(PetscOptionsBoundedInt(prefix, "Local number of deflation vectors computed by SLEPc", "EPSSetDimensions", data->levels[i - 1]->nu, &data->levels[i - 1]->nu, nullptr, 0));
358b5a302b3SPierre Jolivet PetscCall(PetscSNPrintf(deprecated, sizeof(deprecated), "-pc_hpddm_levels_%d_eps_threshold", i));
359b5a302b3SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold_absolute", i));
3608cb7430dSRaphael Zanella PetscCall(PetscOptionsDeprecated(deprecated, prefix, "3.24", nullptr));
361b5a302b3SPierre Jolivet PetscCall(PetscOptionsReal(prefix, "Local absolute threshold for selecting deflation vectors returned by SLEPc", "PCHPDDM", data->levels[i - 1]->threshold, &data->levels[i - 1]->threshold, nullptr));
362f1580f4eSBarry Smith if (i == 1) {
363b5a302b3SPierre Jolivet PetscCheck(overlap == -1 || PetscAbsReal(data->levels[i - 1]->threshold + static_cast<PetscReal>(1.0)) < PETSC_MACHINE_EPSILON, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot supply both -pc_hpddm_levels_1_eps_threshold_absolute and -pc_hpddm_harmonic_overlap");
364fc31f830SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_svd_nsv", i));
3659bb5c669SPierre Jolivet if (overlap != -1) {
3669bb5c669SPierre Jolivet PetscInt nsv = 0;
367b5a302b3SPierre Jolivet PetscBool set[2] = {PETSC_FALSE, PETSC_FALSE};
368811e8887SPierre Jolivet
3699bb5c669SPierre Jolivet PetscCall(PetscOptionsBoundedInt(prefix, "Local number of deflation vectors computed by SLEPc", "SVDSetDimensions", nsv, &nsv, nullptr, 0));
370b5a302b3SPierre Jolivet PetscCheck(data->levels[0]->nu == 0 || nsv == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot supply both -pc_hpddm_levels_1_eps_nev and -pc_hpddm_levels_1_svd_nsv");
371b5a302b3SPierre Jolivet if (data->levels[0]->nu == 0) { /* -eps_nev has not been used, so nu is 0 */
372b5a302b3SPierre Jolivet data->levels[0]->nu = nsv; /* nu may still be 0 if -svd_nsv has not been used */
373b5a302b3SPierre Jolivet PetscCall(PetscSNPrintf(deprecated, sizeof(deprecated), "-pc_hpddm_levels_%d_svd_relative_threshold", i));
374b5a302b3SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_svd_threshold_relative", i));
3758cb7430dSRaphael Zanella PetscCall(PetscOptionsDeprecated(deprecated, prefix, "3.24", nullptr));
376b5a302b3SPierre Jolivet PetscCall(PetscOptionsReal(prefix, "Local relative threshold for selecting deflation vectors returned by SLEPc", "PCHPDDM", data->levels[0]->threshold, &data->levels[0]->threshold, set)); /* cache whether this option has been used or not to error out in case of exclusive options being used simultaneously later on */
377b5a302b3SPierre Jolivet }
378b5a302b3SPierre Jolivet if (data->levels[0]->nu == 0 || nsv == 0) { /* if neither -eps_nev nor -svd_nsv has been used */
379b5a302b3SPierre Jolivet PetscCall(PetscSNPrintf(deprecated, sizeof(deprecated), "-pc_hpddm_levels_%d_eps_relative_threshold", i));
380b5a302b3SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold_relative", i));
3818cb7430dSRaphael Zanella PetscCall(PetscOptionsDeprecated(deprecated, prefix, "3.24", nullptr));
382b5a302b3SPierre Jolivet PetscCall(PetscOptionsReal(prefix, "Local relative threshold for selecting deflation vectors returned by SLEPc", "PCHPDDM", data->levels[0]->threshold, &data->levels[0]->threshold, set + 1));
383b5a302b3SPierre Jolivet PetscCheck(!set[0] || !set[1], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot supply both -pc_hpddm_levels_1_eps_threshold_relative and -pc_hpddm_levels_1_svd_threshold_relative");
384b5a302b3SPierre Jolivet }
385b5a302b3SPierre Jolivet PetscCheck(data->levels[0]->nu || PetscAbsReal(data->levels[i - 1]->threshold + static_cast<PetscReal>(1.0)) > PETSC_MACHINE_EPSILON, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Need to supply at least one of 1) -pc_hpddm_levels_1_eps_nev, 2) -pc_hpddm_levels_1_svd_nsv, 3) -pc_hpddm_levels_1_eps_threshold_relative, 4) -pc_hpddm_levels_1_svd_threshold_relative (for nonsymmetric matrices, only option 2 and option 4 are appropriate)");
386fc31f830SPierre Jolivet } else if (PetscDefined(USE_DEBUG)) {
387fc31f830SPierre Jolivet PetscCall(PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg));
388fc31f830SPierre Jolivet PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot supply -%spc_hpddm_levels_%d_svd_nsv without -%spc_hpddm_harmonic_overlap", PetscOptionsObject->prefix ? PetscOptionsObject->prefix : "", i,
389fc31f830SPierre Jolivet PetscOptionsObject->prefix ? PetscOptionsObject->prefix : "");
390fc31f830SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_svd_threshold_relative", i));
391fc31f830SPierre Jolivet PetscCall(PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg));
392fc31f830SPierre Jolivet PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot supply -%spc_hpddm_levels_%d_svd_threshold_relative without -%spc_hpddm_harmonic_overlap", PetscOptionsObject->prefix ? PetscOptionsObject->prefix : "", i,
393fc31f830SPierre Jolivet PetscOptionsObject->prefix ? PetscOptionsObject->prefix : "");
394fc31f830SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold_relative", i));
395fc31f830SPierre Jolivet PetscCall(PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg));
396fc31f830SPierre Jolivet PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot supply -%spc_hpddm_levels_%d_eps_threshold_relative without -%spc_hpddm_harmonic_overlap, maybe you meant to use -%spc_hpddm_levels_%d_eps_threshold_absolute?",
397fc31f830SPierre Jolivet PetscOptionsObject->prefix ? PetscOptionsObject->prefix : "", i, PetscOptionsObject->prefix ? PetscOptionsObject->prefix : "", PetscOptionsObject->prefix ? PetscOptionsObject->prefix : "", i);
3989bb5c669SPierre Jolivet }
399f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_1_st_share_sub_ksp"));
400db4a47b3SPierre Jolivet PetscCall(PetscOptionsBool(prefix, "Shared KSP between SLEPc ST and the fine-level subdomain solver", "PCHPDDMSetSTShareSubKSP", PETSC_FALSE, &data->share, nullptr));
401f1580f4eSBarry Smith }
402f1580f4eSBarry Smith /* if there is no prescribed coarsening, just break out of the loop */
4030594bca0SPierre Jolivet if (data->levels[i - 1]->threshold <= PetscReal() && data->levels[i - 1]->nu <= 0 && !(data->deflation && i == 1)) break;
404f1580f4eSBarry Smith else {
405f1580f4eSBarry Smith ++i;
406f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_nev", i));
407f1580f4eSBarry Smith PetscCall(PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg));
408f1580f4eSBarry Smith if (!flg) {
409b5a302b3SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold_absolute", i));
410f1580f4eSBarry Smith PetscCall(PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg));
411f1580f4eSBarry Smith }
412f1580f4eSBarry Smith if (flg) {
413f1580f4eSBarry Smith /* if there are coarsening options for the next level, then register it */
414f1580f4eSBarry Smith /* otherwise, don't to avoid having both options levels_N_p and coarse_p */
415f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_p", i));
416f1580f4eSBarry 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)));
417f1580f4eSBarry Smith previous = p;
418f1580f4eSBarry Smith }
419f1580f4eSBarry Smith }
420f1580f4eSBarry Smith }
421f1580f4eSBarry Smith data->N = i;
422f1580f4eSBarry Smith n = 1;
423f1580f4eSBarry Smith if (i > 1) {
424f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_coarse_p"));
425db4a47b3SPierre Jolivet PetscCall(PetscOptionsRangeInt(prefix, "Number of processes used to assemble the coarsest operator", "PCHPDDM", n, &n, nullptr, 1, PetscMax(1, previous / 2)));
42602800ff6SPierre Jolivet #if PetscDefined(HAVE_MUMPS)
427f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "pc_hpddm_coarse_"));
4288dc3fbeeSPierre Jolivet PetscCall(PetscOptionsHasName(PetscOptionsObject->options, prefix, "-mat_mumps_use_omp_threads", &flg));
429f1580f4eSBarry Smith if (flg) {
430f1580f4eSBarry Smith char type[64]; /* same size as in src/ksp/pc/impls/factor/factimpl.c */
431811e8887SPierre Jolivet
432c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(type, n > 1 && PetscDefined(HAVE_MUMPS) ? MATSOLVERMUMPS : MATSOLVERPETSC, sizeof(type))); /* default solver for a MatMPIAIJ or a MatSeqAIJ */
4338dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetString(PetscOptionsObject->options, prefix, "-pc_factor_mat_solver_type", type, sizeof(type), nullptr));
4343ce573a3SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &flg));
435f1580f4eSBarry 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);
436f1580f4eSBarry Smith size = n;
437f1580f4eSBarry Smith n = -1;
4388dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetInt(PetscOptionsObject->options, prefix, "-mat_mumps_use_omp_threads", &n, nullptr));
439f1580f4eSBarry Smith PetscCheck(n >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Need to specify a positive integer for -%smat_mumps_use_omp_threads", prefix);
440f1580f4eSBarry 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" : "");
441f1580f4eSBarry Smith }
442f1580f4eSBarry Smith #endif
443f1580f4eSBarry Smith PetscCall(PetscOptionsEnum("-pc_hpddm_coarse_correction", "Type of coarse correction applied each iteration", "PCHPDDMSetCoarseCorrectionType", PCHPDDMCoarseCorrectionTypes, (PetscEnum)data->correction, (PetscEnum *)&type, &flg));
444f1580f4eSBarry Smith if (flg) PetscCall(PCHPDDMSetCoarseCorrectionType(pc, type));
445f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_has_neumann"));
446c8ea6600SPierre Jolivet PetscCall(PetscOptionsBool(prefix, "Is the auxiliary Mat the local Neumann matrix?", "PCHPDDMHasNeumannMat", PetscBool3ToBool(data->Neumann), &flg, &set));
447c8ea6600SPierre Jolivet if (set) data->Neumann = PetscBoolToBool3(flg);
448f1580f4eSBarry Smith data->log_separate = PETSC_FALSE;
449f1580f4eSBarry Smith if (PetscDefined(USE_LOG)) {
450f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_log_separate"));
451db4a47b3SPierre Jolivet PetscCall(PetscOptionsBool(prefix, "Log events level by level instead of inside PCSetUp()/KSPSolve()", nullptr, data->log_separate, &data->log_separate, nullptr));
452f1580f4eSBarry Smith }
453f1580f4eSBarry Smith }
454f1580f4eSBarry Smith PetscOptionsHeadEnd();
455f1580f4eSBarry Smith while (i < PETSC_PCHPDDM_MAXLEVELS && data->levels[i]) PetscCall(PetscFree(data->levels[i++]));
4563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
457f1580f4eSBarry Smith }
458f1580f4eSBarry Smith
459d4f06b61SRaphael Zanella template <bool transpose>
PCApply_HPDDM(PC pc,Vec x,Vec y)460d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_HPDDM(PC pc, Vec x, Vec y)
461d71ae5a4SJacob Faibussowitsch {
462f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
463f1580f4eSBarry Smith
464f1580f4eSBarry Smith PetscFunctionBegin;
465f1580f4eSBarry Smith PetscCall(PetscCitationsRegister(HPDDMCitation, &HPDDMCite));
466f1580f4eSBarry Smith PetscCheck(data->levels[0]->ksp, PETSC_COMM_SELF, PETSC_ERR_PLIB, "No KSP attached to PCHPDDM");
467db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventBegin(PC_HPDDM_Solve[0], data->levels[0]->ksp, nullptr, nullptr, nullptr)); /* coarser-level events are directly triggered in HPDDM */
468d4f06b61SRaphael Zanella if (!transpose) PetscCall(KSPSolve(data->levels[0]->ksp, x, y));
469d4f06b61SRaphael Zanella else PetscCall(KSPSolveTranspose(data->levels[0]->ksp, x, y));
470db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_Solve[0], data->levels[0]->ksp, nullptr, nullptr, nullptr));
4713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
472f1580f4eSBarry Smith }
473f1580f4eSBarry Smith
4748cb7430dSRaphael Zanella template <bool transpose>
PCMatApply_HPDDM(PC pc,Mat X,Mat Y)475d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_HPDDM(PC pc, Mat X, Mat Y)
476d71ae5a4SJacob Faibussowitsch {
477f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
478f1580f4eSBarry Smith
479f1580f4eSBarry Smith PetscFunctionBegin;
480f1580f4eSBarry Smith PetscCall(PetscCitationsRegister(HPDDMCitation, &HPDDMCite));
481f1580f4eSBarry Smith PetscCheck(data->levels[0]->ksp, PETSC_COMM_SELF, PETSC_ERR_PLIB, "No KSP attached to PCHPDDM");
4828cb7430dSRaphael Zanella if (!transpose) PetscCall(KSPMatSolve(data->levels[0]->ksp, X, Y));
4838cb7430dSRaphael Zanella else PetscCall(KSPMatSolveTranspose(data->levels[0]->ksp, X, Y));
4843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
485f1580f4eSBarry Smith }
486f1580f4eSBarry Smith
487cc4c1da9SBarry Smith /*@
488f1580f4eSBarry Smith PCHPDDMGetComplexities - Computes the grid and operator complexities.
489f1580f4eSBarry Smith
490c71f06a7SPierre Jolivet Collective
491c71f06a7SPierre Jolivet
492f1580f4eSBarry Smith Input Parameter:
493f1580f4eSBarry Smith . pc - preconditioner context
494f1580f4eSBarry Smith
495f1580f4eSBarry Smith Output Parameters:
496cc4c1da9SBarry Smith + gc - grid complexity $ \sum_i m_i / m_1 $
497cc4c1da9SBarry Smith - oc - operator complexity $ \sum_i nnz_i / nnz_1 $
498f1580f4eSBarry Smith
499f1580f4eSBarry Smith Level: advanced
500f1580f4eSBarry Smith
501562efe2eSBarry Smith .seealso: [](ch_ksp), `PCMGGetGridComplexity()`, `PCHPDDM`, `PCHYPRE`, `PCGAMG`
502f1580f4eSBarry Smith @*/
PCHPDDMGetComplexities(PC pc,PetscReal * gc,PetscReal * oc)503cc4c1da9SBarry Smith PetscErrorCode PCHPDDMGetComplexities(PC pc, PetscReal *gc, PetscReal *oc)
504d71ae5a4SJacob Faibussowitsch {
505f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
506f1580f4eSBarry Smith MatInfo info;
507f1580f4eSBarry Smith PetscLogDouble accumulate[2]{}, nnz1 = 1.0, m1 = 1.0;
508f1580f4eSBarry Smith
509f1580f4eSBarry Smith PetscFunctionBegin;
510715c1178SPierre Jolivet if (gc) {
511715c1178SPierre Jolivet PetscAssertPointer(gc, 2);
512715c1178SPierre Jolivet *gc = 0;
513715c1178SPierre Jolivet }
514715c1178SPierre Jolivet if (oc) {
515715c1178SPierre Jolivet PetscAssertPointer(oc, 3);
516715c1178SPierre Jolivet *oc = 0;
517715c1178SPierre Jolivet }
518715c1178SPierre Jolivet for (PetscInt n = 0; n < data->N; ++n) {
519f1580f4eSBarry Smith if (data->levels[n]->ksp) {
52013044ca3SPierre Jolivet Mat P, A = nullptr;
521715c1178SPierre Jolivet PetscInt m;
52213044ca3SPierre Jolivet PetscBool flg = PETSC_FALSE;
52313044ca3SPierre Jolivet
524db4a47b3SPierre Jolivet PetscCall(KSPGetOperators(data->levels[n]->ksp, nullptr, &P));
525db4a47b3SPierre Jolivet PetscCall(MatGetSize(P, &m, nullptr));
526f1580f4eSBarry Smith accumulate[0] += m;
527f1580f4eSBarry Smith if (n == 0) {
528f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompareAny((PetscObject)P, &flg, MATNORMAL, MATNORMALHERMITIAN, ""));
529f1580f4eSBarry Smith if (flg) {
530f1580f4eSBarry Smith PetscCall(MatConvert(P, MATAIJ, MAT_INITIAL_MATRIX, &A));
531f1580f4eSBarry Smith P = A;
53213044ca3SPierre Jolivet } else {
53313044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)P, MATSCHURCOMPLEMENT, &flg));
53413044ca3SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)P));
535f1580f4eSBarry Smith }
53613044ca3SPierre Jolivet }
53713044ca3SPierre Jolivet if (!A && flg) accumulate[1] += m * m; /* assumption that a MATSCHURCOMPLEMENT is dense if stored explicitly */
53813044ca3SPierre Jolivet else if (P->ops->getinfo) {
539f1580f4eSBarry Smith PetscCall(MatGetInfo(P, MAT_GLOBAL_SUM, &info));
540f1580f4eSBarry Smith accumulate[1] += info.nz_used;
541f1580f4eSBarry Smith }
542f1580f4eSBarry Smith if (n == 0) {
543f1580f4eSBarry Smith m1 = m;
54413044ca3SPierre Jolivet if (!A && flg) nnz1 = m * m;
54513044ca3SPierre Jolivet else if (P->ops->getinfo) nnz1 = info.nz_used;
546f1580f4eSBarry Smith PetscCall(MatDestroy(&P));
547f1580f4eSBarry Smith }
548f1580f4eSBarry Smith }
549f1580f4eSBarry Smith }
550715c1178SPierre Jolivet /* only process #0 has access to the full hierarchy by construction, so broadcast to ensure consistent outputs */
551715c1178SPierre Jolivet PetscCallMPI(MPI_Bcast(accumulate, 2, MPIU_PETSCLOGDOUBLE, 0, PetscObjectComm((PetscObject)pc)));
552715c1178SPierre Jolivet if (gc) *gc = static_cast<PetscReal>(accumulate[0] / m1);
553715c1178SPierre Jolivet if (oc) *oc = static_cast<PetscReal>(accumulate[1] / nnz1);
5543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
555f1580f4eSBarry Smith }
556f1580f4eSBarry Smith
PCView_HPDDM(PC pc,PetscViewer viewer)557d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_HPDDM(PC pc, PetscViewer viewer)
558d71ae5a4SJacob Faibussowitsch {
559f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
560f1580f4eSBarry Smith PetscViewer subviewer;
561aa21023fSPierre Jolivet PetscViewerFormat format;
562f1580f4eSBarry Smith PetscSubcomm subcomm;
563f1580f4eSBarry Smith PetscReal oc, gc;
564811e8887SPierre Jolivet PetscInt tabs;
565f1580f4eSBarry Smith PetscMPIInt size, color, rank;
566aa21023fSPierre Jolivet PetscBool flg;
567aa21023fSPierre Jolivet const char *name;
568f1580f4eSBarry Smith
569f1580f4eSBarry Smith PetscFunctionBegin;
570aa21023fSPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &flg));
571aa21023fSPierre Jolivet if (flg) {
572f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "level%s: %" PetscInt_FMT "\n", data->N > 1 ? "s" : "", data->N));
573f1580f4eSBarry Smith PetscCall(PCHPDDMGetComplexities(pc, &gc, &oc));
574f1580f4eSBarry Smith if (data->N > 1) {
575f1580f4eSBarry Smith if (!data->deflation) {
576c8ea6600SPierre Jolivet PetscCall(PetscViewerASCIIPrintf(viewer, "Neumann matrix attached? %s\n", PetscBools[PetscBool3ToBool(data->Neumann)]));
577f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "shared subdomain KSP between SLEPc and PETSc? %s\n", PetscBools[data->share]));
578f1580f4eSBarry Smith } else PetscCall(PetscViewerASCIIPrintf(viewer, "user-supplied deflation matrix\n"));
579f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "coarse correction: %s\n", PCHPDDMCoarseCorrectionTypes[data->correction]));
580f1580f4eSBarry 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" : ""));
581f1580f4eSBarry Smith PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
582f1580f4eSBarry Smith PetscCall(PetscViewerASCIISetTab(viewer, 0));
583811e8887SPierre Jolivet for (PetscInt i = 1; i < data->N; ++i) {
584f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, data->levels[i - 1]->nu));
5850594bca0SPierre Jolivet if (data->levels[i - 1]->threshold > static_cast<PetscReal>(-0.1)) PetscCall(PetscViewerASCIIPrintf(viewer, " (%g)", (double)data->levels[i - 1]->threshold));
586f1580f4eSBarry Smith }
587f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
588f1580f4eSBarry Smith PetscCall(PetscViewerASCIISetTab(viewer, tabs));
589f1580f4eSBarry Smith }
590f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "grid and operator complexities: %g %g\n", (double)gc, (double)oc));
5911fe44b27SPierre Jolivet PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
592f1580f4eSBarry Smith if (data->levels[0]->ksp) {
593f1580f4eSBarry Smith PetscCall(KSPView(data->levels[0]->ksp, viewer));
594f1580f4eSBarry Smith if (data->levels[0]->pc) PetscCall(PCView(data->levels[0]->pc, viewer));
595eea1a2f1SRaphael Zanella PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
596811e8887SPierre Jolivet for (PetscInt i = 1; i < data->N; ++i) {
597f1580f4eSBarry Smith if (data->levels[i]->ksp) color = 1;
598f1580f4eSBarry Smith else color = 0;
599f1580f4eSBarry Smith PetscCall(PetscSubcommCreate(PetscObjectComm((PetscObject)pc), &subcomm));
600f1580f4eSBarry Smith PetscCall(PetscSubcommSetNumber(subcomm, PetscMin(size, 2)));
601f1580f4eSBarry Smith PetscCall(PetscSubcommSetTypeGeneral(subcomm, color, rank));
602f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPushTab(viewer));
603f1580f4eSBarry Smith PetscCall(PetscViewerGetSubViewer(viewer, PetscSubcommChild(subcomm), &subviewer));
604f1580f4eSBarry Smith if (color == 1) {
605f1580f4eSBarry Smith PetscCall(KSPView(data->levels[i]->ksp, subviewer));
606f1580f4eSBarry Smith if (data->levels[i]->pc) PetscCall(PCView(data->levels[i]->pc, subviewer));
607f1580f4eSBarry Smith PetscCall(PetscViewerFlush(subviewer));
608f1580f4eSBarry Smith }
609f1580f4eSBarry Smith PetscCall(PetscViewerRestoreSubViewer(viewer, PetscSubcommChild(subcomm), &subviewer));
610f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPopTab(viewer));
611f1580f4eSBarry Smith PetscCall(PetscSubcommDestroy(&subcomm));
612f1580f4eSBarry Smith }
613f1580f4eSBarry Smith }
614aa21023fSPierre Jolivet PetscCall(PetscViewerGetFormat(viewer, &format));
615aa21023fSPierre Jolivet if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
616aa21023fSPierre Jolivet PetscCall(PetscViewerFileGetName(viewer, &name));
617aa21023fSPierre Jolivet if (name) {
618eea1a2f1SRaphael Zanella Mat aux[2];
619aa21023fSPierre Jolivet IS is;
620eea1a2f1SRaphael Zanella const PetscInt *indices;
621eea1a2f1SRaphael Zanella PetscInt m, n, sizes[5] = {pc->mat->rmap->n, pc->mat->cmap->n, pc->mat->rmap->N, pc->mat->cmap->N, 0};
622aa21023fSPierre Jolivet char *tmp;
623aa21023fSPierre Jolivet std::string prefix, suffix;
624aa21023fSPierre Jolivet size_t pos;
625aa21023fSPierre Jolivet
626aa21023fSPierre Jolivet PetscCall(PetscStrstr(name, ".", &tmp));
627aa21023fSPierre Jolivet if (tmp) {
628aa21023fSPierre Jolivet pos = std::distance(const_cast<char *>(name), tmp);
629aa21023fSPierre Jolivet prefix = std::string(name, pos);
630aa21023fSPierre Jolivet suffix = std::string(name + pos + 1);
631aa21023fSPierre Jolivet } else prefix = name;
632aa21023fSPierre Jolivet if (data->aux) {
633eea1a2f1SRaphael Zanella PetscCall(MatGetSize(data->aux, &m, &n));
634eea1a2f1SRaphael Zanella PetscCall(MatCreate(PetscObjectComm((PetscObject)pc), aux));
635eea1a2f1SRaphael Zanella PetscCall(MatSetSizes(aux[0], m, n, PETSC_DETERMINE, PETSC_DETERMINE));
636eea1a2f1SRaphael Zanella PetscCall(PetscObjectBaseTypeCompare((PetscObject)data->aux, MATSEQAIJ, &flg));
637eea1a2f1SRaphael Zanella if (flg) PetscCall(MatSetType(aux[0], MATMPIAIJ));
638eea1a2f1SRaphael Zanella else {
639eea1a2f1SRaphael Zanella PetscCall(PetscObjectBaseTypeCompare((PetscObject)data->aux, MATSEQBAIJ, &flg));
640eea1a2f1SRaphael Zanella if (flg) PetscCall(MatSetType(aux[0], MATMPIBAIJ));
641eea1a2f1SRaphael Zanella else {
642eea1a2f1SRaphael Zanella PetscCall(PetscObjectBaseTypeCompare((PetscObject)data->aux, MATSEQSBAIJ, &flg));
643eea1a2f1SRaphael Zanella PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "MatType of auxiliary Mat (%s) is not any of the following: MATSEQAIJ, MATSEQBAIJ, or MATSEQSBAIJ", ((PetscObject)data->aux)->type_name);
644eea1a2f1SRaphael Zanella PetscCall(MatSetType(aux[0], MATMPISBAIJ));
645eea1a2f1SRaphael Zanella }
646eea1a2f1SRaphael Zanella }
647eea1a2f1SRaphael Zanella PetscCall(MatSetBlockSizesFromMats(aux[0], data->aux, data->aux));
648eea1a2f1SRaphael Zanella PetscCall(MatAssemblyBegin(aux[0], MAT_FINAL_ASSEMBLY));
649eea1a2f1SRaphael Zanella PetscCall(MatAssemblyEnd(aux[0], MAT_FINAL_ASSEMBLY));
650eea1a2f1SRaphael Zanella PetscCall(MatGetDiagonalBlock(aux[0], aux + 1));
651eea1a2f1SRaphael Zanella PetscCall(MatCopy(data->aux, aux[1], DIFFERENT_NONZERO_PATTERN));
652eea1a2f1SRaphael Zanella PetscCall(PetscViewerBinaryOpen(PetscObjectComm((PetscObject)pc), std::string(prefix + "_aux_" + std::to_string(size) + (tmp ? ("." + suffix) : "")).c_str(), FILE_MODE_WRITE, &subviewer));
653eea1a2f1SRaphael Zanella PetscCall(MatView(aux[0], subviewer));
654aa21023fSPierre Jolivet PetscCall(PetscViewerDestroy(&subviewer));
655eea1a2f1SRaphael Zanella PetscCall(MatDestroy(aux));
656aa21023fSPierre Jolivet }
657aa21023fSPierre Jolivet if (data->is) {
658eea1a2f1SRaphael Zanella PetscCall(ISGetIndices(data->is, &indices));
659eea1a2f1SRaphael Zanella PetscCall(ISGetSize(data->is, sizes + 4));
660eea1a2f1SRaphael Zanella PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), sizes[4], indices, PETSC_USE_POINTER, &is));
661eea1a2f1SRaphael Zanella PetscCall(PetscViewerBinaryOpen(PetscObjectComm((PetscObject)pc), std::string(prefix + "_is_" + std::to_string(size) + (tmp ? ("." + suffix) : "")).c_str(), FILE_MODE_WRITE, &subviewer));
662eea1a2f1SRaphael Zanella PetscCall(ISView(is, subviewer));
663aa21023fSPierre Jolivet PetscCall(PetscViewerDestroy(&subviewer));
664eea1a2f1SRaphael Zanella PetscCall(ISDestroy(&is));
665eea1a2f1SRaphael Zanella PetscCall(ISRestoreIndices(data->is, &indices));
666aa21023fSPierre Jolivet }
667eea1a2f1SRaphael Zanella PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), PETSC_STATIC_ARRAY_LENGTH(sizes), sizes, PETSC_USE_POINTER, &is));
668eea1a2f1SRaphael Zanella PetscCall(PetscViewerBinaryOpen(PetscObjectComm((PetscObject)pc), std::string(prefix + "_sizes_" + std::to_string(size) + (tmp ? ("." + suffix) : "")).c_str(), FILE_MODE_WRITE, &subviewer));
669aa21023fSPierre Jolivet PetscCall(ISView(is, subviewer));
670aa21023fSPierre Jolivet PetscCall(PetscViewerDestroy(&subviewer));
671aa21023fSPierre Jolivet PetscCall(ISDestroy(&is));
672aa21023fSPierre Jolivet }
673aa21023fSPierre Jolivet }
674f1580f4eSBarry Smith }
6753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
676f1580f4eSBarry Smith }
677f1580f4eSBarry Smith
PCPreSolve_HPDDM(PC pc,KSP ksp,Vec,Vec)678d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPreSolve_HPDDM(PC pc, KSP ksp, Vec, Vec)
679d71ae5a4SJacob Faibussowitsch {
680f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
681f1580f4eSBarry Smith Mat A;
682cdbd50ebSPierre Jolivet PetscBool flg;
683f1580f4eSBarry Smith
684f1580f4eSBarry Smith PetscFunctionBegin;
685f1580f4eSBarry Smith if (ksp) {
686f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)ksp, KSPLSQR, &flg));
687f1580f4eSBarry Smith if (flg && !data->normal) {
688db4a47b3SPierre Jolivet PetscCall(KSPGetOperators(ksp, &A, nullptr));
689db4a47b3SPierre Jolivet PetscCall(MatCreateVecs(A, nullptr, &data->normal)); /* temporary Vec used in PCApply_HPDDMShell() for coarse grid corrections */
690cdbd50ebSPierre Jolivet } else if (!flg) {
691cdbd50ebSPierre Jolivet PetscCall(PetscObjectTypeCompareAny((PetscObject)ksp, &flg, KSPCG, KSPGROPPCG, KSPPIPECG, KSPPIPECGRR, KSPPIPELCG, KSPPIPEPRCG, KSPPIPECG2, KSPSTCG, KSPFCG, KSPPIPEFCG, KSPMINRES, KSPNASH, KSPSYMMLQ, ""));
692cdbd50ebSPierre Jolivet if (!flg) {
693cdbd50ebSPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)ksp, KSPHPDDM, &flg));
694cdbd50ebSPierre Jolivet if (flg) {
695cdbd50ebSPierre Jolivet KSPHPDDMType type;
696811e8887SPierre Jolivet
697cdbd50ebSPierre Jolivet PetscCall(KSPHPDDMGetType(ksp, &type));
698cdbd50ebSPierre Jolivet flg = (type == KSP_HPDDM_TYPE_CG || type == KSP_HPDDM_TYPE_BCG || type == KSP_HPDDM_TYPE_BFBCG ? PETSC_TRUE : PETSC_FALSE);
699cdbd50ebSPierre Jolivet }
700cdbd50ebSPierre Jolivet }
701cdbd50ebSPierre Jolivet }
702cdbd50ebSPierre Jolivet if (flg) {
703cdbd50ebSPierre Jolivet if (data->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED) {
704cdbd50ebSPierre Jolivet PetscCall(PetscOptionsHasName(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_hpddm_coarse_correction", &flg));
705cdbd50ebSPierre Jolivet PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "PCHPDDMCoarseCorrectionType %s is known to be not symmetric, but KSPType %s requires a symmetric PC, if you insist on using this configuration, use the additional option -%spc_hpddm_coarse_correction %s, or alternatively, switch to a symmetric PCHPDDMCoarseCorrectionType such as %s",
706cdbd50ebSPierre Jolivet PCHPDDMCoarseCorrectionTypes[data->correction], ((PetscObject)ksp)->type_name, ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", PCHPDDMCoarseCorrectionTypes[data->correction], PCHPDDMCoarseCorrectionTypes[PC_HPDDM_COARSE_CORRECTION_BALANCED]);
707cdbd50ebSPierre Jolivet }
708cdbd50ebSPierre Jolivet for (PetscInt n = 0; n < data->N; ++n) {
709cdbd50ebSPierre Jolivet if (data->levels[n]->pc) {
710cdbd50ebSPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)data->levels[n]->pc, PCASM, &flg));
711cdbd50ebSPierre Jolivet if (flg) {
712cdbd50ebSPierre Jolivet PCASMType type;
713811e8887SPierre Jolivet
714cdbd50ebSPierre Jolivet PetscCall(PCASMGetType(data->levels[n]->pc, &type));
715cdbd50ebSPierre Jolivet if (type == PC_ASM_RESTRICT || type == PC_ASM_INTERPOLATE) {
716cdbd50ebSPierre Jolivet PetscCall(PetscOptionsHasName(((PetscObject)data->levels[n]->pc)->options, ((PetscObject)data->levels[n]->pc)->prefix, "-pc_asm_type", &flg));
717cdbd50ebSPierre Jolivet PetscCheck(flg, PetscObjectComm((PetscObject)data->levels[n]->pc), PETSC_ERR_ARG_INCOMP, "PCASMType %s is known to be not symmetric, but KSPType %s requires a symmetric PC, if you insist on using this configuration, use the additional option -%spc_asm_type %s, or alternatively, switch to a symmetric PCASMType such as %s", PCASMTypes[type],
718cdbd50ebSPierre Jolivet ((PetscObject)ksp)->type_name, ((PetscObject)data->levels[n]->pc)->prefix, PCASMTypes[type], PCASMTypes[PC_ASM_BASIC]);
719cdbd50ebSPierre Jolivet }
720cdbd50ebSPierre Jolivet }
721cdbd50ebSPierre Jolivet }
722cdbd50ebSPierre Jolivet }
723f1580f4eSBarry Smith }
724f1580f4eSBarry Smith }
7253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
726f1580f4eSBarry Smith }
727f1580f4eSBarry Smith
PCSetUp_HPDDMShell(PC pc)728d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_HPDDMShell(PC pc)
729d71ae5a4SJacob Faibussowitsch {
730f1580f4eSBarry Smith PC_HPDDM_Level *ctx;
731f1580f4eSBarry Smith Mat A, P;
732f1580f4eSBarry Smith Vec x;
733f1580f4eSBarry Smith const char *pcpre;
734f1580f4eSBarry Smith
735f1580f4eSBarry Smith PetscFunctionBegin;
736f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx));
737f1580f4eSBarry Smith PetscCall(KSPGetOptionsPrefix(ctx->ksp, &pcpre));
738f1580f4eSBarry Smith PetscCall(KSPGetOperators(ctx->ksp, &A, &P));
739f1580f4eSBarry Smith /* smoother */
740f1580f4eSBarry Smith PetscCall(PCSetOptionsPrefix(ctx->pc, pcpre));
741f1580f4eSBarry Smith PetscCall(PCSetOperators(ctx->pc, A, P));
742f1580f4eSBarry Smith if (!ctx->v[0]) {
743f1580f4eSBarry Smith PetscCall(VecDuplicateVecs(ctx->D, 1, &ctx->v[0]));
744f1580f4eSBarry Smith if (!std::is_same<PetscScalar, PetscReal>::value) PetscCall(VecDestroy(&ctx->D));
745db4a47b3SPierre Jolivet PetscCall(MatCreateVecs(A, &x, nullptr));
746f1580f4eSBarry Smith PetscCall(VecDuplicateVecs(x, 2, &ctx->v[1]));
747f1580f4eSBarry Smith PetscCall(VecDestroy(&x));
748f1580f4eSBarry Smith }
749f1580f4eSBarry Smith std::fill_n(ctx->V, 3, nullptr);
7503ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
751f1580f4eSBarry Smith }
752f1580f4eSBarry Smith
753d4f06b61SRaphael Zanella template <bool transpose = false, class Type = Vec, typename std::enable_if<std::is_same<Type, Vec>::value>::type * = nullptr>
PCHPDDMDeflate_Private(PC pc,Type x,Type y)754d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PCHPDDMDeflate_Private(PC pc, Type x, Type y)
755d71ae5a4SJacob Faibussowitsch {
756f1580f4eSBarry Smith PC_HPDDM_Level *ctx;
757f1580f4eSBarry Smith PetscScalar *out;
758f1580f4eSBarry Smith
759f1580f4eSBarry Smith PetscFunctionBegin;
760f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx));
761f1580f4eSBarry Smith /* going from PETSc to HPDDM numbering */
762f1580f4eSBarry Smith PetscCall(VecScatterBegin(ctx->scatter, x, ctx->v[0][0], INSERT_VALUES, SCATTER_FORWARD));
763f1580f4eSBarry Smith PetscCall(VecScatterEnd(ctx->scatter, x, ctx->v[0][0], INSERT_VALUES, SCATTER_FORWARD));
764f1580f4eSBarry Smith PetscCall(VecGetArrayWrite(ctx->v[0][0], &out));
76501962aebSPierre Jolivet PetscCallCXX(ctx->P->deflation<false, transpose>(nullptr, out, 1)); /* y = Q x */
766f1580f4eSBarry Smith PetscCall(VecRestoreArrayWrite(ctx->v[0][0], &out));
767f1580f4eSBarry Smith /* going from HPDDM to PETSc numbering */
768f1580f4eSBarry Smith PetscCall(VecScatterBegin(ctx->scatter, ctx->v[0][0], y, INSERT_VALUES, SCATTER_REVERSE));
769f1580f4eSBarry Smith PetscCall(VecScatterEnd(ctx->scatter, ctx->v[0][0], y, INSERT_VALUES, SCATTER_REVERSE));
7703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
771f1580f4eSBarry Smith }
772f1580f4eSBarry Smith
7738cb7430dSRaphael Zanella template <bool transpose = false, class Type = Mat, typename std::enable_if<std::is_same<Type, Mat>::value>::type * = nullptr>
PCHPDDMDeflate_Private(PC pc,Type X,Type Y)774d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PCHPDDMDeflate_Private(PC pc, Type X, Type Y)
775d71ae5a4SJacob Faibussowitsch {
776f1580f4eSBarry Smith PC_HPDDM_Level *ctx;
777ee25009cSBoris Martin PetscScalar *out;
778b04479c2SPierre Jolivet PetscInt N, ld[2];
779ee25009cSBoris Martin PetscMemType type;
780f1580f4eSBarry Smith
781f1580f4eSBarry Smith PetscFunctionBegin;
782f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx));
783db4a47b3SPierre Jolivet PetscCall(MatGetSize(X, nullptr, &N));
784b04479c2SPierre Jolivet PetscCall(MatDenseGetLDA(X, ld));
785b04479c2SPierre Jolivet PetscCall(MatDenseGetLDA(Y, ld + 1));
786b04479c2SPierre Jolivet PetscCheck(ld[0] == ld[1], PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Leading dimension of input Mat different than the one of output Mat");
787f1580f4eSBarry Smith /* going from PETSc to HPDDM numbering */
788ee25009cSBoris Martin PetscCall(MatDenseScatter_Private(ctx->scatter, X, ctx->V[0], INSERT_VALUES, SCATTER_FORWARD));
789ee25009cSBoris Martin PetscCall(MatDenseGetArrayAndMemType(ctx->V[0], &out, &type));
790ee25009cSBoris Martin PetscCallCXX(ctx->P->deflation<false, transpose>(nullptr, out, N)); /* Y = Q X */
791ee25009cSBoris Martin PetscCall(MatDenseRestoreArrayAndMemType(ctx->V[0], &out));
792f1580f4eSBarry Smith /* going from HPDDM to PETSc numbering */
793ee25009cSBoris Martin PetscCall(MatDenseScatter_Private(ctx->scatter, ctx->V[0], Y, INSERT_VALUES, SCATTER_REVERSE));
7943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
795f1580f4eSBarry Smith }
796f1580f4eSBarry Smith
797f1580f4eSBarry Smith /*
798aa1539e9SPierre Jolivet PCApply_HPDDMShell - Applies a (2) deflated, (1) additive, (3) balanced, or (4) no coarse correction. In what follows, E = Z Pmat Z^T and Q = Z^T E^-1 Z.
799f1580f4eSBarry Smith
800f1580f4eSBarry Smith .vb
801f1580f4eSBarry Smith (1) y = Pmat^-1 x + Q x,
802f1580f4eSBarry Smith (2) y = Pmat^-1 (I - Amat Q) x + Q x (default),
80301962aebSPierre Jolivet (3) y = (I - Q^T Amat^T) Pmat^-1 (I - Amat Q) x + Q x,
804aa1539e9SPierre Jolivet (4) y = Pmat^-1 x .
805f1580f4eSBarry Smith .ve
806f1580f4eSBarry Smith
807f1580f4eSBarry Smith Input Parameters:
808f1580f4eSBarry Smith + pc - preconditioner context
809f1580f4eSBarry Smith - x - input vector
810f1580f4eSBarry Smith
811f1580f4eSBarry Smith Output Parameter:
812f1580f4eSBarry Smith . y - output vector
813f1580f4eSBarry Smith
814f1580f4eSBarry Smith Notes:
815f1580f4eSBarry 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.
816f1580f4eSBarry 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.
817f1580f4eSBarry 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.
818f1580f4eSBarry Smith
819f1580f4eSBarry Smith Level: advanced
820f1580f4eSBarry Smith
821f1580f4eSBarry Smith Developer Note:
822f1580f4eSBarry 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
823f1580f4eSBarry Smith to trigger it. Likely the manual page is `PCHPDDM`
824f1580f4eSBarry Smith
825562efe2eSBarry Smith .seealso: [](ch_ksp), `PCHPDDM`, `PCHPDDMCoarseCorrectionType`
826f1580f4eSBarry Smith */
PCApply_HPDDMShell(PC pc,Vec x,Vec y)827d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_HPDDMShell(PC pc, Vec x, Vec y)
828d71ae5a4SJacob Faibussowitsch {
829f1580f4eSBarry Smith PC_HPDDM_Level *ctx;
830f1580f4eSBarry Smith Mat A;
831f1580f4eSBarry Smith
832f1580f4eSBarry Smith PetscFunctionBegin;
833f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx));
834f1580f4eSBarry Smith PetscCheck(ctx->P, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object");
835db4a47b3SPierre Jolivet PetscCall(KSPGetOperators(ctx->ksp, &A, nullptr));
836aa1539e9SPierre Jolivet if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_NONE) PetscCall(PCApply(ctx->pc, x, y)); /* y = M^-1 x */
837aa1539e9SPierre Jolivet else {
838f1580f4eSBarry Smith PetscCall(PCHPDDMDeflate_Private(pc, x, y)); /* y = Q x */
839f1580f4eSBarry Smith if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
840f1580f4eSBarry Smith if (!ctx->parent->normal || ctx != ctx->parent->levels[0]) PetscCall(MatMult(A, y, ctx->v[1][0])); /* y = A Q x */
8418a8e6071SPierre Jolivet else {
8428a8e6071SPierre Jolivet /* KSPLSQR and finest level */
8438a8e6071SPierre Jolivet PetscCall(MatMult(A, y, ctx->parent->normal)); /* y = A Q x */
844f1580f4eSBarry Smith PetscCall(MatMultHermitianTranspose(A, ctx->parent->normal, ctx->v[1][0])); /* y = A^T A Q x */
845f1580f4eSBarry Smith }
846f1580f4eSBarry Smith PetscCall(VecWAXPY(ctx->v[1][1], -1.0, ctx->v[1][0], x)); /* y = (I - A Q) x */
847f1580f4eSBarry Smith PetscCall(PCApply(ctx->pc, ctx->v[1][1], ctx->v[1][0])); /* y = M^-1 (I - A Q) x */
848f1580f4eSBarry Smith if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
849f1580f4eSBarry 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 */
850f1580f4eSBarry Smith else {
851f1580f4eSBarry Smith PetscCall(MatMult(A, ctx->v[1][0], ctx->parent->normal));
852f1580f4eSBarry Smith PetscCall(MatMultHermitianTranspose(A, ctx->parent->normal, ctx->v[1][1])); /* z = A^T A y */
853f1580f4eSBarry Smith }
85401962aebSPierre Jolivet PetscCall(PCHPDDMDeflate_Private<true>(pc, ctx->v[1][1], ctx->v[1][1])); /* z = Q^T z */
85501962aebSPierre Jolivet PetscCall(VecAXPBYPCZ(y, -1.0, 1.0, 1.0, ctx->v[1][1], ctx->v[1][0])); /* y = (I - Q^T A^T) y + Q x */
856f1580f4eSBarry Smith } else PetscCall(VecAXPY(y, 1.0, ctx->v[1][0])); /* y = Q M^-1 (I - A Q) x + Q x */
857f1580f4eSBarry Smith } else {
858f1580f4eSBarry 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);
859f1580f4eSBarry Smith PetscCall(PCApply(ctx->pc, x, ctx->v[1][0]));
860f1580f4eSBarry Smith PetscCall(VecAXPY(y, 1.0, ctx->v[1][0])); /* y = M^-1 x + Q x */
861f1580f4eSBarry Smith }
862aa1539e9SPierre Jolivet }
8633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
864f1580f4eSBarry Smith }
865f1580f4eSBarry Smith
8668cb7430dSRaphael Zanella template <bool transpose>
PCHPDDMMatApply_Private(PC_HPDDM_Level * ctx,Mat Y,PetscBool * reset)8678cb7430dSRaphael Zanella static PetscErrorCode PCHPDDMMatApply_Private(PC_HPDDM_Level *ctx, Mat Y, PetscBool *reset)
8688cb7430dSRaphael Zanella {
8698cb7430dSRaphael Zanella Mat A, *ptr;
8708cb7430dSRaphael Zanella PetscScalar *array;
8718cb7430dSRaphael Zanella PetscInt m, M, N, prev = 0;
8728cb7430dSRaphael Zanella PetscContainer container = nullptr;
8738cb7430dSRaphael Zanella
8748cb7430dSRaphael Zanella PetscFunctionBegin;
8758cb7430dSRaphael Zanella PetscCall(KSPGetOperators(ctx->ksp, &A, nullptr));
8768cb7430dSRaphael Zanella PetscCall(MatGetSize(Y, nullptr, &N));
8778cb7430dSRaphael Zanella PetscCall(PetscObjectQuery((PetscObject)A, "_HPDDM_MatProduct", (PetscObject *)&container));
8788cb7430dSRaphael Zanella if (container) { /* MatProduct container already attached */
8792a8381b2SBarry Smith PetscCall(PetscContainerGetPointer(container, &ptr));
8808cb7430dSRaphael Zanella if (ptr[1] != ctx->V[2]) /* Mat has changed or may have been set first in KSPHPDDM */
8818cb7430dSRaphael Zanella for (m = 0; m < 2; ++m) {
8828cb7430dSRaphael Zanella PetscCall(MatDestroy(ctx->V + m + 1));
8838cb7430dSRaphael Zanella ctx->V[m + 1] = ptr[m];
8848cb7430dSRaphael Zanella PetscCall(PetscObjectReference((PetscObject)ctx->V[m + 1]));
8858cb7430dSRaphael Zanella }
8868cb7430dSRaphael Zanella }
8878cb7430dSRaphael Zanella if (ctx->V[1]) PetscCall(MatGetSize(ctx->V[1], nullptr, &prev));
8888cb7430dSRaphael Zanella if (N != prev || !ctx->V[0]) {
8898cb7430dSRaphael Zanella PetscCall(MatDestroy(ctx->V));
8908cb7430dSRaphael Zanella PetscCall(VecGetLocalSize(ctx->v[0][0], &m));
8918cb7430dSRaphael Zanella PetscCall(MatCreateDense(PetscObjectComm((PetscObject)Y), m, PETSC_DECIDE, PETSC_DECIDE, N, nullptr, ctx->V));
8928cb7430dSRaphael Zanella if (N != prev) {
8938cb7430dSRaphael Zanella PetscCall(MatDestroy(ctx->V + 1));
8948cb7430dSRaphael Zanella PetscCall(MatDestroy(ctx->V + 2));
8958cb7430dSRaphael Zanella PetscCall(MatGetLocalSize(Y, &m, nullptr));
8968cb7430dSRaphael Zanella PetscCall(MatGetSize(Y, &M, nullptr));
8978cb7430dSRaphael Zanella if (ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) PetscCall(MatDenseGetArrayWrite(ctx->V[0], &array));
8988cb7430dSRaphael Zanella else array = nullptr;
8998cb7430dSRaphael Zanella PetscCall(MatCreateDense(PetscObjectComm((PetscObject)Y), m, PETSC_DECIDE, M, N, array, ctx->V + 1));
9008cb7430dSRaphael Zanella if (ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) PetscCall(MatDenseRestoreArrayWrite(ctx->V[0], &array));
9018cb7430dSRaphael Zanella PetscCall(MatCreateDense(PetscObjectComm((PetscObject)Y), m, PETSC_DECIDE, M, N, nullptr, ctx->V + 2));
9028cb7430dSRaphael Zanella PetscCall(MatProductCreateWithMat(A, !transpose ? Y : ctx->V[2], nullptr, ctx->V[1]));
9038cb7430dSRaphael Zanella PetscCall(MatProductSetType(ctx->V[1], !transpose ? MATPRODUCT_AB : MATPRODUCT_AtB));
9048cb7430dSRaphael Zanella PetscCall(MatProductSetFromOptions(ctx->V[1]));
9058cb7430dSRaphael Zanella PetscCall(MatProductSymbolic(ctx->V[1]));
9068cb7430dSRaphael Zanella if (!container) PetscCall(PetscObjectContainerCompose((PetscObject)A, "_HPDDM_MatProduct", ctx->V + 1, nullptr)); /* no MatProduct container attached, create one to be queried in KSPHPDDM or at the next call to PCMatApply() */
9078cb7430dSRaphael Zanella else PetscCall(PetscContainerSetPointer(container, ctx->V + 1)); /* need to compose B and D from MatProductCreateWithMat(A, B, NULL, D), which are stored in the contiguous array ctx->V */
9088cb7430dSRaphael Zanella }
9098cb7430dSRaphael Zanella if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
9108cb7430dSRaphael Zanella PetscCall(MatProductCreateWithMat(A, !transpose ? ctx->V[1] : Y, nullptr, ctx->V[2]));
9118cb7430dSRaphael Zanella PetscCall(MatProductSetType(ctx->V[2], !transpose ? MATPRODUCT_AtB : MATPRODUCT_AB));
9128cb7430dSRaphael Zanella PetscCall(MatProductSetFromOptions(ctx->V[2]));
9138cb7430dSRaphael Zanella PetscCall(MatProductSymbolic(ctx->V[2]));
9148cb7430dSRaphael Zanella }
91501962aebSPierre Jolivet PetscCallCXX(ctx->P->start(N));
9168cb7430dSRaphael Zanella }
9178cb7430dSRaphael Zanella if (N == prev || container) { /* when MatProduct container is attached, always need to MatProductReplaceMats() since KSPHPDDM may have replaced the Mat as well */
9188cb7430dSRaphael Zanella PetscCall(MatProductReplaceMats(nullptr, !transpose ? Y : ctx->V[2], nullptr, ctx->V[1]));
9198cb7430dSRaphael Zanella if (container && ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) {
9208cb7430dSRaphael Zanella PetscCall(MatDenseGetArrayWrite(ctx->V[0], &array));
9218cb7430dSRaphael Zanella PetscCall(MatDensePlaceArray(ctx->V[1], array));
9228cb7430dSRaphael Zanella PetscCall(MatDenseRestoreArrayWrite(ctx->V[0], &array));
9238cb7430dSRaphael Zanella *reset = PETSC_TRUE;
9248cb7430dSRaphael Zanella }
9258cb7430dSRaphael Zanella }
9268cb7430dSRaphael Zanella PetscFunctionReturn(PETSC_SUCCESS);
9278cb7430dSRaphael Zanella }
9288cb7430dSRaphael Zanella
929f1580f4eSBarry Smith /*
930f1580f4eSBarry Smith PCMatApply_HPDDMShell - Variant of PCApply_HPDDMShell() for blocks of vectors.
931f1580f4eSBarry Smith
932f1580f4eSBarry Smith Input Parameters:
933f1580f4eSBarry Smith + pc - preconditioner context
934f1580f4eSBarry Smith - X - block of input vectors
935f1580f4eSBarry Smith
936f1580f4eSBarry Smith Output Parameter:
937f1580f4eSBarry Smith . Y - block of output vectors
938f1580f4eSBarry Smith
939f1580f4eSBarry Smith Level: advanced
940f1580f4eSBarry Smith
941562efe2eSBarry Smith .seealso: [](ch_ksp), `PCHPDDM`, `PCApply_HPDDMShell()`, `PCHPDDMCoarseCorrectionType`
942f1580f4eSBarry Smith */
PCMatApply_HPDDMShell(PC pc,Mat X,Mat Y)943d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_HPDDMShell(PC pc, Mat X, Mat Y)
944d71ae5a4SJacob Faibussowitsch {
945f1580f4eSBarry Smith PC_HPDDM_Level *ctx;
946f1580f4eSBarry Smith PetscBool reset = PETSC_FALSE;
947f1580f4eSBarry Smith
948f1580f4eSBarry Smith PetscFunctionBegin;
949f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx));
950f1580f4eSBarry Smith PetscCheck(ctx->P, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object");
951aa1539e9SPierre Jolivet if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_NONE) PetscCall(PCMatApply(ctx->pc, X, Y));
952aa1539e9SPierre Jolivet else {
9538cb7430dSRaphael Zanella PetscCall(PCHPDDMMatApply_Private<false>(ctx, Y, &reset));
954f1580f4eSBarry Smith PetscCall(PCHPDDMDeflate_Private(pc, X, Y));
955f1580f4eSBarry Smith if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
956f1580f4eSBarry Smith PetscCall(MatProductNumeric(ctx->V[1]));
957f1580f4eSBarry Smith PetscCall(MatCopy(ctx->V[1], ctx->V[2], SAME_NONZERO_PATTERN));
958f1580f4eSBarry Smith PetscCall(MatAXPY(ctx->V[2], -1.0, X, SAME_NONZERO_PATTERN));
959f1580f4eSBarry Smith PetscCall(PCMatApply(ctx->pc, ctx->V[2], ctx->V[1]));
960f1580f4eSBarry Smith if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
961f1580f4eSBarry Smith PetscCall(MatProductNumeric(ctx->V[2]));
96201962aebSPierre Jolivet PetscCall(PCHPDDMDeflate_Private<true>(pc, ctx->V[2], ctx->V[2]));
963f1580f4eSBarry Smith PetscCall(MatAXPY(ctx->V[1], -1.0, ctx->V[2], SAME_NONZERO_PATTERN));
964f1580f4eSBarry Smith }
965f1580f4eSBarry Smith PetscCall(MatAXPY(Y, -1.0, ctx->V[1], SAME_NONZERO_PATTERN));
966f1580f4eSBarry Smith } else {
967f1580f4eSBarry 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);
968f1580f4eSBarry Smith PetscCall(PCMatApply(ctx->pc, X, ctx->V[1]));
969f1580f4eSBarry Smith PetscCall(MatAXPY(Y, 1.0, ctx->V[1], SAME_NONZERO_PATTERN));
970f1580f4eSBarry Smith }
971f1580f4eSBarry Smith if (reset) PetscCall(MatDenseResetArray(ctx->V[1]));
972aa1539e9SPierre Jolivet }
9733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
974f1580f4eSBarry Smith }
975f1580f4eSBarry Smith
976d4f06b61SRaphael Zanella /*
977d4f06b61SRaphael Zanella PCApplyTranspose_HPDDMShell - Applies the transpose of a (2) deflated, (1) additive, (3) balanced, or (4) no coarse correction. In what follows, E = Z Pmat Z^T and Q = Z^T E^-1 Z.
978d4f06b61SRaphael Zanella
979d4f06b61SRaphael Zanella .vb
980d4f06b61SRaphael Zanella (1) y = Pmat^-T x + Q^T x,
981d4f06b61SRaphael Zanella (2) y = (I - Q^T Amat^T) Pmat^-T x + Q^T x (default),
98201962aebSPierre Jolivet (3) y = (I - Q^T Amat^T) Pmat^-T (I - Amat Q) x + Q^T x,
983d4f06b61SRaphael Zanella (4) y = Pmat^-T x .
984d4f06b61SRaphael Zanella .ve
985d4f06b61SRaphael Zanella
986d4f06b61SRaphael Zanella Input Parameters:
987d4f06b61SRaphael Zanella + pc - preconditioner context
988d4f06b61SRaphael Zanella - x - input vector
989d4f06b61SRaphael Zanella
990d4f06b61SRaphael Zanella Output Parameter:
991d4f06b61SRaphael Zanella . y - output vector
992d4f06b61SRaphael Zanella
993d4f06b61SRaphael Zanella Level: advanced
994d4f06b61SRaphael Zanella
995d4f06b61SRaphael Zanella Developer Note:
996d4f06b61SRaphael Zanella 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
997d4f06b61SRaphael Zanella to trigger it. Likely the manual page is `PCHPDDM`
998d4f06b61SRaphael Zanella
999d4f06b61SRaphael Zanella .seealso: [](ch_ksp), `PCHPDDM`, `PCApply_HPDDMShell()`, `PCHPDDMCoarseCorrectionType`
1000d4f06b61SRaphael Zanella */
PCApplyTranspose_HPDDMShell(PC pc,Vec x,Vec y)1001d4f06b61SRaphael Zanella static PetscErrorCode PCApplyTranspose_HPDDMShell(PC pc, Vec x, Vec y)
1002d4f06b61SRaphael Zanella {
1003d4f06b61SRaphael Zanella PC_HPDDM_Level *ctx;
1004d4f06b61SRaphael Zanella Mat A;
1005d4f06b61SRaphael Zanella
1006d4f06b61SRaphael Zanella PetscFunctionBegin;
1007d4f06b61SRaphael Zanella PetscCall(PCShellGetContext(pc, &ctx));
1008d4f06b61SRaphael Zanella PetscCheck(ctx->P, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object");
10098cb7430dSRaphael Zanella PetscCheck(!ctx->parent->normal, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not implemented for the normal equations");
1010d4f06b61SRaphael Zanella PetscCall(KSPGetOperators(ctx->ksp, &A, nullptr));
1011d4f06b61SRaphael Zanella if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_NONE) PetscCall(PCApplyTranspose(ctx->pc, x, y)); /* y = M^-T x */
1012d4f06b61SRaphael Zanella else {
1013d4f06b61SRaphael Zanella PetscCall(PCHPDDMDeflate_Private<true>(pc, x, y)); /* y = Q^T x */
1014d4f06b61SRaphael Zanella if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
1015d4f06b61SRaphael Zanella if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
101601962aebSPierre Jolivet /* TODO: checking whether Q^T = Q would make it possible to skip this coarse correction */
101701962aebSPierre Jolivet PetscCall(PCHPDDMDeflate_Private(pc, x, ctx->v[1][1])); /* y = Q x */
101801962aebSPierre Jolivet PetscCall(MatMult(A, ctx->v[1][1], ctx->v[1][0])); /* y = A Q x */
101901962aebSPierre Jolivet PetscCall(VecWAXPY(ctx->v[1][1], -1.0, ctx->v[1][0], x)); /* y = (I - A Q) x */
102001962aebSPierre Jolivet PetscCall(PCApplyTranspose(ctx->pc, ctx->v[1][1], ctx->v[1][0])); /* y = M^-T (I - A Q) x */
1021d4f06b61SRaphael Zanella } else PetscCall(PCApplyTranspose(ctx->pc, x, ctx->v[1][0])); /* y = M^-T x */
10228cb7430dSRaphael Zanella PetscCall(MatMultHermitianTranspose(A, ctx->v[1][0], ctx->v[1][1])); /* z = A^T y */
1023d4f06b61SRaphael Zanella PetscCall(PCHPDDMDeflate_Private<true>(pc, ctx->v[1][1], ctx->v[1][1])); /* z = Q^T z */
1024d4f06b61SRaphael Zanella PetscCall(VecAXPBYPCZ(y, -1.0, 1.0, 1.0, ctx->v[1][1], ctx->v[1][0])); /* y = (I - Q^T A^T) y + Q^T x */
1025d4f06b61SRaphael Zanella } else {
1026d4f06b61SRaphael Zanella 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);
1027d4f06b61SRaphael Zanella PetscCall(PCApplyTranspose(ctx->pc, x, ctx->v[1][0]));
1028d4f06b61SRaphael Zanella PetscCall(VecAXPY(y, 1.0, ctx->v[1][0])); /* y = M^-T x + Q^T x */
1029d4f06b61SRaphael Zanella }
1030d4f06b61SRaphael Zanella }
1031d4f06b61SRaphael Zanella PetscFunctionReturn(PETSC_SUCCESS);
1032d4f06b61SRaphael Zanella }
1033d4f06b61SRaphael Zanella
10348cb7430dSRaphael Zanella /*
10358cb7430dSRaphael Zanella PCMatApplyTranspose_HPDDMShell - Variant of PCApplyTranspose_HPDDMShell() for blocks of vectors.
10368cb7430dSRaphael Zanella
10378cb7430dSRaphael Zanella Input Parameters:
10388cb7430dSRaphael Zanella + pc - preconditioner context
10398cb7430dSRaphael Zanella - X - block of input vectors
10408cb7430dSRaphael Zanella
10418cb7430dSRaphael Zanella Output Parameter:
10428cb7430dSRaphael Zanella . Y - block of output vectors
10438cb7430dSRaphael Zanella
10448cb7430dSRaphael Zanella Level: advanced
10458cb7430dSRaphael Zanella
10468cb7430dSRaphael Zanella .seealso: [](ch_ksp), `PCHPDDM`, `PCApplyTranspose_HPDDMShell()`, `PCHPDDMCoarseCorrectionType`
10478cb7430dSRaphael Zanella */
PCMatApplyTranspose_HPDDMShell(PC pc,Mat X,Mat Y)10488cb7430dSRaphael Zanella static PetscErrorCode PCMatApplyTranspose_HPDDMShell(PC pc, Mat X, Mat Y)
10498cb7430dSRaphael Zanella {
10508cb7430dSRaphael Zanella PC_HPDDM_Level *ctx;
10518cb7430dSRaphael Zanella PetscScalar *array;
10528cb7430dSRaphael Zanella PetscBool reset = PETSC_FALSE;
10538cb7430dSRaphael Zanella
10548cb7430dSRaphael Zanella PetscFunctionBegin;
10558cb7430dSRaphael Zanella PetscCall(PCShellGetContext(pc, &ctx));
10568cb7430dSRaphael Zanella PetscCheck(ctx->P, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object");
10578cb7430dSRaphael Zanella if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_NONE) PetscCall(PCMatApplyTranspose(ctx->pc, X, Y));
105801962aebSPierre Jolivet else if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) {
105901962aebSPierre Jolivet /* similar code as in PCMatApply_HPDDMShell() with an extra call to PCHPDDMDeflate_Private<true>() */
106001962aebSPierre Jolivet PetscCall(PCHPDDMMatApply_Private<false>(ctx, Y, &reset));
106101962aebSPierre Jolivet PetscCall(PCHPDDMDeflate_Private(pc, X, Y));
106201962aebSPierre Jolivet PetscCall(MatProductNumeric(ctx->V[1]));
106301962aebSPierre Jolivet PetscCall(MatCopy(ctx->V[1], ctx->V[2], SAME_NONZERO_PATTERN));
106401962aebSPierre Jolivet PetscCall(MatAXPY(ctx->V[2], -1.0, X, SAME_NONZERO_PATTERN));
106501962aebSPierre Jolivet PetscCall(PCMatApplyTranspose(ctx->pc, ctx->V[2], ctx->V[1]));
106601962aebSPierre Jolivet PetscCall(MatProductNumeric(ctx->V[2]));
106701962aebSPierre Jolivet PetscCall(PCHPDDMDeflate_Private<true>(pc, ctx->V[2], ctx->V[2]));
106801962aebSPierre Jolivet PetscCall(MatAXPY(ctx->V[1], -1.0, ctx->V[2], SAME_NONZERO_PATTERN));
106901962aebSPierre Jolivet PetscCall(PCHPDDMDeflate_Private<true>(pc, X, Y)); /* TODO: checking whether Q^T = Q would make it possible to skip this coarse correction */
107001962aebSPierre Jolivet PetscCall(MatAXPY(Y, -1.0, ctx->V[1], SAME_NONZERO_PATTERN));
107101962aebSPierre Jolivet } else {
10728cb7430dSRaphael Zanella PetscCall(PCHPDDMMatApply_Private<true>(ctx, Y, &reset));
10738cb7430dSRaphael Zanella PetscCall(PCHPDDMDeflate_Private<true>(pc, X, Y));
107401962aebSPierre Jolivet if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED) {
107501962aebSPierre Jolivet PetscCall(PCMatApplyTranspose(ctx->pc, X, ctx->V[2]));
10768cb7430dSRaphael Zanella PetscCall(MatAXPY(Y, 1.0, ctx->V[2], SAME_NONZERO_PATTERN));
10778cb7430dSRaphael Zanella PetscCall(MatProductNumeric(ctx->V[1]));
1078847b6ef2SPierre Jolivet /* ctx->V[0] and ctx->V[1] memory regions overlap, so need to copy to ctx->V[2] and switch array */
10798cb7430dSRaphael Zanella PetscCall(MatCopy(ctx->V[1], ctx->V[2], SAME_NONZERO_PATTERN));
1080847b6ef2SPierre Jolivet if (reset) PetscCall(MatDenseResetArray(ctx->V[1]));
10818cb7430dSRaphael Zanella PetscCall(MatDenseGetArrayWrite(ctx->V[2], &array));
10828cb7430dSRaphael Zanella PetscCall(MatDensePlaceArray(ctx->V[1], array));
10838cb7430dSRaphael Zanella PetscCall(MatDenseRestoreArrayWrite(ctx->V[2], &array));
1084847b6ef2SPierre Jolivet reset = PETSC_TRUE;
10858cb7430dSRaphael Zanella PetscCall(PCHPDDMDeflate_Private<true>(pc, ctx->V[1], ctx->V[1]));
10868cb7430dSRaphael Zanella PetscCall(MatAXPY(Y, -1.0, ctx->V[1], SAME_NONZERO_PATTERN));
10878cb7430dSRaphael Zanella } else {
10888cb7430dSRaphael Zanella 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);
10898cb7430dSRaphael Zanella PetscCall(PCMatApplyTranspose(ctx->pc, X, ctx->V[1]));
10908cb7430dSRaphael Zanella PetscCall(MatAXPY(Y, 1.0, ctx->V[1], SAME_NONZERO_PATTERN));
10918cb7430dSRaphael Zanella }
10928cb7430dSRaphael Zanella if (reset) PetscCall(MatDenseResetArray(ctx->V[1]));
10938cb7430dSRaphael Zanella }
10948cb7430dSRaphael Zanella PetscFunctionReturn(PETSC_SUCCESS);
10958cb7430dSRaphael Zanella }
10968cb7430dSRaphael Zanella
PCDestroy_HPDDMShell(PC pc)1097d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_HPDDMShell(PC pc)
1098d71ae5a4SJacob Faibussowitsch {
1099f1580f4eSBarry Smith PC_HPDDM_Level *ctx;
1100f1580f4eSBarry Smith
1101f1580f4eSBarry Smith PetscFunctionBegin;
1102f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx));
1103f1580f4eSBarry Smith PetscCall(HPDDM::Schwarz<PetscScalar>::destroy(ctx, PETSC_TRUE));
1104f1580f4eSBarry Smith PetscCall(VecDestroyVecs(1, &ctx->v[0]));
1105f1580f4eSBarry Smith PetscCall(VecDestroyVecs(2, &ctx->v[1]));
1106f4f49eeaSPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)ctx->pc->mat, "_HPDDM_MatProduct", nullptr));
1107f1580f4eSBarry Smith PetscCall(MatDestroy(ctx->V));
1108f1580f4eSBarry Smith PetscCall(MatDestroy(ctx->V + 1));
1109f1580f4eSBarry Smith PetscCall(MatDestroy(ctx->V + 2));
1110f1580f4eSBarry Smith PetscCall(VecDestroy(&ctx->D));
111101e3c840SPierre Jolivet PetscCall(PetscSFDestroy(&ctx->scatter));
1112f1580f4eSBarry Smith PetscCall(PCDestroy(&ctx->pc));
11133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1114f1580f4eSBarry Smith }
1115f1580f4eSBarry Smith
11169bb5c669SPierre Jolivet template <class Type, bool T = false, typename std::enable_if<std::is_same<Type, Vec>::value>::type * = nullptr>
PCApply_Schur_Private(std::tuple<KSP,IS,Vec[2]> * p,PC factor,Type x,Type y)11179bb5c669SPierre Jolivet static inline PetscErrorCode PCApply_Schur_Private(std::tuple<KSP, IS, Vec[2]> *p, PC factor, Type x, Type y)
11189bb5c669SPierre Jolivet {
11199bb5c669SPierre Jolivet PetscFunctionBegin;
11209bb5c669SPierre Jolivet PetscCall(VecISCopy(std::get<2>(*p)[0], std::get<1>(*p), SCATTER_FORWARD, x));
11219bb5c669SPierre Jolivet if (!T) PetscCall(PCApply(factor, std::get<2>(*p)[0], std::get<2>(*p)[1]));
11229bb5c669SPierre Jolivet else PetscCall(PCApplyTranspose(factor, std::get<2>(*p)[0], std::get<2>(*p)[1]));
11239bb5c669SPierre Jolivet PetscCall(VecISCopy(std::get<2>(*p)[1], std::get<1>(*p), SCATTER_REVERSE, y));
11249bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
11259bb5c669SPierre Jolivet }
11269bb5c669SPierre Jolivet
11279bb5c669SPierre Jolivet template <class Type, bool = false, typename std::enable_if<std::is_same<Type, Mat>::value>::type * = nullptr>
PCApply_Schur_Private(std::tuple<KSP,IS,Vec[2]> * p,PC factor,Type X,Type Y)11289bb5c669SPierre Jolivet static inline PetscErrorCode PCApply_Schur_Private(std::tuple<KSP, IS, Vec[2]> *p, PC factor, Type X, Type Y)
11299bb5c669SPierre Jolivet {
11309bb5c669SPierre Jolivet Mat B[2];
11319bb5c669SPierre Jolivet Vec x, y;
11329bb5c669SPierre Jolivet
11339bb5c669SPierre Jolivet PetscFunctionBegin;
11349bb5c669SPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, factor->mat->rmap->n, X->cmap->n, nullptr, B));
11359bb5c669SPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, factor->mat->rmap->n, X->cmap->n, nullptr, B + 1));
11369bb5c669SPierre Jolivet for (PetscInt i = 0; i < X->cmap->n; ++i) {
11379bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecRead(X, i, &x));
11389bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecWrite(B[0], i, &y));
11399bb5c669SPierre Jolivet PetscCall(VecISCopy(y, std::get<1>(*p), SCATTER_FORWARD, x));
11409bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecWrite(B[0], i, &y));
11419bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecRead(X, i, &x));
11429bb5c669SPierre Jolivet }
11439bb5c669SPierre Jolivet PetscCall(PCMatApply(factor, B[0], B[1]));
11449bb5c669SPierre Jolivet PetscCall(MatDestroy(B));
11459bb5c669SPierre Jolivet for (PetscInt i = 0; i < X->cmap->n; ++i) {
11469bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecRead(B[1], i, &x));
11479bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecWrite(Y, i, &y));
11489bb5c669SPierre Jolivet PetscCall(VecISCopy(x, std::get<1>(*p), SCATTER_REVERSE, y));
11499bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &y));
11509bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecRead(B[1], i, &x));
11519bb5c669SPierre Jolivet }
11529bb5c669SPierre Jolivet PetscCall(MatDestroy(B + 1));
11539bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
11549bb5c669SPierre Jolivet }
11559bb5c669SPierre Jolivet
11569bb5c669SPierre Jolivet template <class Type = Vec, bool T = false>
PCApply_Schur(PC pc,Type x,Type y)11579bb5c669SPierre Jolivet static PetscErrorCode PCApply_Schur(PC pc, Type x, Type y)
11589bb5c669SPierre Jolivet {
11599bb5c669SPierre Jolivet PC factor;
11609bb5c669SPierre Jolivet Mat A;
11619bb5c669SPierre Jolivet MatSolverType type;
11629bb5c669SPierre Jolivet PetscBool flg;
11639bb5c669SPierre Jolivet std::tuple<KSP, IS, Vec[2]> *p;
11649bb5c669SPierre Jolivet
11659bb5c669SPierre Jolivet PetscFunctionBegin;
11669bb5c669SPierre Jolivet PetscCall(PCShellGetContext(pc, &p));
11679bb5c669SPierre Jolivet PetscCall(KSPGetPC(std::get<0>(*p), &factor));
11689bb5c669SPierre Jolivet PetscCall(PCFactorGetMatSolverType(factor, &type));
11699bb5c669SPierre Jolivet PetscCall(PCFactorGetMatrix(factor, &A));
11709bb5c669SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &flg));
11719bb5c669SPierre Jolivet if (flg) {
11729bb5c669SPierre Jolivet PetscCheck(PetscDefined(HAVE_MUMPS), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent MatSolverType");
11739bb5c669SPierre Jolivet PetscCall(MatMumpsSetIcntl(A, 26, 0));
11749bb5c669SPierre Jolivet } else {
11759bb5c669SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMKL_PARDISO, &flg));
11769bb5c669SPierre Jolivet PetscCheck(flg && PetscDefined(HAVE_MKL_PARDISO), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent MatSolverType");
11779bb5c669SPierre Jolivet flg = PETSC_FALSE;
11789bb5c669SPierre Jolivet #if PetscDefined(HAVE_MKL_PARDISO)
11799bb5c669SPierre Jolivet PetscCall(MatMkl_PardisoSetCntl(A, 70, 1));
11809bb5c669SPierre Jolivet #endif
11819bb5c669SPierre Jolivet }
11829bb5c669SPierre Jolivet PetscCall(PCApply_Schur_Private<Type, T>(p, factor, x, y));
11839bb5c669SPierre Jolivet if (flg) {
11849bb5c669SPierre Jolivet PetscCall(MatMumpsSetIcntl(A, 26, -1));
11859bb5c669SPierre Jolivet } else {
11869bb5c669SPierre Jolivet #if PetscDefined(HAVE_MKL_PARDISO)
11879bb5c669SPierre Jolivet PetscCall(MatMkl_PardisoSetCntl(A, 70, 0));
11889bb5c669SPierre Jolivet #endif
11899bb5c669SPierre Jolivet }
11909bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
11919bb5c669SPierre Jolivet }
11929bb5c669SPierre Jolivet
PCDestroy_Schur(PC pc)11939bb5c669SPierre Jolivet static PetscErrorCode PCDestroy_Schur(PC pc)
11949bb5c669SPierre Jolivet {
11959bb5c669SPierre Jolivet std::tuple<KSP, IS, Vec[2]> *p;
11969bb5c669SPierre Jolivet
11979bb5c669SPierre Jolivet PetscFunctionBegin;
11989bb5c669SPierre Jolivet PetscCall(PCShellGetContext(pc, &p));
11999bb5c669SPierre Jolivet PetscCall(ISDestroy(&std::get<1>(*p)));
12009bb5c669SPierre Jolivet PetscCall(VecDestroy(std::get<2>(*p)));
12019bb5c669SPierre Jolivet PetscCall(VecDestroy(std::get<2>(*p) + 1));
12029bb5c669SPierre Jolivet PetscCall(PetscFree(p));
12039bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
12049bb5c669SPierre Jolivet }
12059bb5c669SPierre Jolivet
1206d4f06b61SRaphael Zanella template <bool transpose>
PCHPDDMSolve_Private(const PC_HPDDM_Level * ctx,PetscScalar * rhs,const unsigned short & mu)1207d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSolve_Private(const PC_HPDDM_Level *ctx, PetscScalar *rhs, const unsigned short &mu)
1208d71ae5a4SJacob Faibussowitsch {
1209f1580f4eSBarry Smith Mat B, X;
1210f1580f4eSBarry Smith PetscInt n, N, j = 0;
1211f1580f4eSBarry Smith
1212f1580f4eSBarry Smith PetscFunctionBegin;
1213db4a47b3SPierre Jolivet PetscCall(KSPGetOperators(ctx->ksp, &B, nullptr));
1214db4a47b3SPierre Jolivet PetscCall(MatGetLocalSize(B, &n, nullptr));
1215db4a47b3SPierre Jolivet PetscCall(MatGetSize(B, &N, nullptr));
1216f1580f4eSBarry Smith if (ctx->parent->log_separate) {
1217f1580f4eSBarry Smith j = std::distance(ctx->parent->levels, std::find(ctx->parent->levels, ctx->parent->levels + ctx->parent->N, ctx));
1218db4a47b3SPierre Jolivet PetscCall(PetscLogEventBegin(PC_HPDDM_Solve[j], ctx->ksp, nullptr, nullptr, nullptr));
1219f1580f4eSBarry Smith }
1220f1580f4eSBarry Smith if (mu == 1) {
1221f1580f4eSBarry Smith if (!ctx->ksp->vec_rhs) {
1222db4a47b3SPierre Jolivet PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)ctx->ksp), 1, n, N, nullptr, &ctx->ksp->vec_rhs));
1223f1580f4eSBarry Smith PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)ctx->ksp), n, N, &ctx->ksp->vec_sol));
1224f1580f4eSBarry Smith }
1225f1580f4eSBarry Smith PetscCall(VecPlaceArray(ctx->ksp->vec_rhs, rhs));
1226d4f06b61SRaphael Zanella if (!transpose) PetscCall(KSPSolve(ctx->ksp, nullptr, nullptr));
122701962aebSPierre Jolivet else {
122801962aebSPierre Jolivet PetscCall(VecConjugate(ctx->ksp->vec_rhs));
122901962aebSPierre Jolivet PetscCall(KSPSolveTranspose(ctx->ksp, nullptr, nullptr)); /* TODO: missing KSPSolveHermitianTranspose() */
123001962aebSPierre Jolivet PetscCall(VecConjugate(ctx->ksp->vec_sol));
123101962aebSPierre Jolivet }
1232f1580f4eSBarry Smith PetscCall(VecCopy(ctx->ksp->vec_sol, ctx->ksp->vec_rhs));
1233f1580f4eSBarry Smith PetscCall(VecResetArray(ctx->ksp->vec_rhs));
1234f1580f4eSBarry Smith } else {
1235f1580f4eSBarry Smith PetscCall(MatCreateDense(PetscObjectComm((PetscObject)ctx->ksp), n, PETSC_DECIDE, N, mu, rhs, &B));
1236db4a47b3SPierre Jolivet PetscCall(MatCreateDense(PetscObjectComm((PetscObject)ctx->ksp), n, PETSC_DECIDE, N, mu, nullptr, &X));
123701962aebSPierre Jolivet if (!transpose) PetscCall(KSPMatSolve(ctx->ksp, B, X));
123801962aebSPierre Jolivet else {
123901962aebSPierre Jolivet PetscCall(MatConjugate(B));
124001962aebSPierre Jolivet PetscCall(KSPMatSolveTranspose(ctx->ksp, B, X)); /* TODO: missing KSPMatSolveHermitianTranspose() */
124101962aebSPierre Jolivet PetscCall(MatConjugate(X));
124201962aebSPierre Jolivet }
1243f1580f4eSBarry Smith PetscCall(MatCopy(X, B, SAME_NONZERO_PATTERN));
1244f1580f4eSBarry Smith PetscCall(MatDestroy(&X));
1245f1580f4eSBarry Smith PetscCall(MatDestroy(&B));
1246f1580f4eSBarry Smith }
1247db4a47b3SPierre Jolivet if (ctx->parent->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_Solve[j], ctx->ksp, nullptr, nullptr, nullptr));
12483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1249f1580f4eSBarry Smith }
1250f1580f4eSBarry Smith
PCHPDDMSetUpNeumannOverlap_Private(PC pc)1251d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetUpNeumannOverlap_Private(PC pc)
1252d71ae5a4SJacob Faibussowitsch {
1253f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
1254f1580f4eSBarry Smith
1255f1580f4eSBarry Smith PetscFunctionBegin;
1256f1580f4eSBarry Smith if (data->setup) {
1257f1580f4eSBarry Smith Mat P;
1258db4a47b3SPierre Jolivet Vec x, xt = nullptr;
1259f1580f4eSBarry Smith PetscReal t = 0.0, s = 0.0;
1260f1580f4eSBarry Smith
1261db4a47b3SPierre Jolivet PetscCall(PCGetOperators(pc, nullptr, &P));
1262f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)P, "__SNES_latest_X", (PetscObject *)&x));
1263f1580f4eSBarry Smith PetscCallBack("PCHPDDM Neumann callback", (*data->setup)(data->aux, t, x, xt, s, data->is, data->setup_ctx));
1264f1580f4eSBarry Smith }
12653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1266f1580f4eSBarry Smith }
1267f1580f4eSBarry Smith
PCHPDDMCreateSubMatrices_Private(Mat mat,PetscInt n,const IS *,const IS *,MatReuse scall,Mat * submat[])1268d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMCreateSubMatrices_Private(Mat mat, PetscInt n, const IS *, const IS *, MatReuse scall, Mat *submat[])
1269d71ae5a4SJacob Faibussowitsch {
1270f1580f4eSBarry Smith Mat A;
127113044ca3SPierre Jolivet PetscBool flg;
1272f1580f4eSBarry Smith
1273f1580f4eSBarry Smith PetscFunctionBegin;
1274f1580f4eSBarry Smith PetscCheck(n == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatCreateSubMatrices() called to extract %" PetscInt_FMT " submatrices, which is different than 1", n);
1275f1580f4eSBarry Smith /* previously composed Mat */
1276f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)mat, "_PCHPDDM_SubMatrices", (PetscObject *)&A));
1277f1580f4eSBarry Smith PetscCheck(A, PETSC_COMM_SELF, PETSC_ERR_PLIB, "SubMatrices not found in Mat");
127813044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A, MATSCHURCOMPLEMENT, &flg)); /* MATSCHURCOMPLEMENT has neither a MatDuplicate() nor a MatCopy() implementation */
1279f1580f4eSBarry Smith if (scall == MAT_INITIAL_MATRIX) {
12806f2c871aSStefano Zampini PetscCall(PetscCalloc1(2, submat)); /* allocate an extra Mat to avoid errors in MatDestroySubMatrices_Dummy() */
128113044ca3SPierre Jolivet if (!flg) PetscCall(MatDuplicate(A, MAT_COPY_VALUES, *submat));
128213044ca3SPierre Jolivet } else if (!flg) PetscCall(MatCopy(A, (*submat)[0], SAME_NONZERO_PATTERN));
128313044ca3SPierre Jolivet if (flg) {
1284bf583f0cSPierre Jolivet PetscCall(MatDestroy(*submat)); /* previously created Mat has to be destroyed */
128513044ca3SPierre Jolivet (*submat)[0] = A;
128613044ca3SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)A));
128713044ca3SPierre Jolivet }
12883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1289f1580f4eSBarry Smith }
1290f1580f4eSBarry Smith
PCHPDDMCommunicationAvoidingPCASM_Private(PC pc,Mat C,PetscBool sorted)1291d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMCommunicationAvoidingPCASM_Private(PC pc, Mat C, PetscBool sorted)
1292d71ae5a4SJacob Faibussowitsch {
129357d50842SBarry Smith PetscErrorCodeFn *op;
1294f1580f4eSBarry Smith
1295f1580f4eSBarry Smith PetscFunctionBegin;
1296f1580f4eSBarry Smith /* previously-composed Mat */
1297f1580f4eSBarry Smith PetscCall(PetscObjectCompose((PetscObject)pc->pmat, "_PCHPDDM_SubMatrices", (PetscObject)C));
1298f1580f4eSBarry Smith PetscCall(MatGetOperation(pc->pmat, MATOP_CREATE_SUBMATRICES, &op));
1299f1580f4eSBarry Smith /* trick suggested by Barry https://lists.mcs.anl.gov/pipermail/petsc-dev/2020-January/025491.html */
130057d50842SBarry Smith PetscCall(MatSetOperation(pc->pmat, MATOP_CREATE_SUBMATRICES, (PetscErrorCodeFn *)PCHPDDMCreateSubMatrices_Private));
1301f1580f4eSBarry Smith if (sorted) PetscCall(PCASMSetSortIndices(pc, PETSC_FALSE)); /* everything is already sorted */
1302f1580f4eSBarry Smith PetscCall(PCSetFromOptions(pc)); /* otherwise -pc_hpddm_levels_1_pc_asm_sub_mat_type is not used */
1303f1580f4eSBarry Smith PetscCall(PCSetUp(pc));
1304f1580f4eSBarry Smith /* reset MatCreateSubMatrices() */
1305f1580f4eSBarry Smith PetscCall(MatSetOperation(pc->pmat, MATOP_CREATE_SUBMATRICES, op));
1306db4a47b3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)pc->pmat, "_PCHPDDM_SubMatrices", nullptr));
13073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1308f1580f4eSBarry Smith }
1309f1580f4eSBarry Smith
PCHPDDMPermute_Private(IS is,IS in_is,IS * out_is,Mat in_C,Mat * out_C,IS * p)1310d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMPermute_Private(IS is, IS in_is, IS *out_is, Mat in_C, Mat *out_C, IS *p)
1311d71ae5a4SJacob Faibussowitsch {
1312f1580f4eSBarry Smith IS perm;
1313f1580f4eSBarry Smith const PetscInt *ptr;
1314811e8887SPierre Jolivet PetscInt *concatenate, size, bs;
1315f1580f4eSBarry Smith std::map<PetscInt, PetscInt> order;
1316f1580f4eSBarry Smith PetscBool sorted;
1317f1580f4eSBarry Smith
1318f1580f4eSBarry Smith PetscFunctionBegin;
1319cf67ef9dSPierre Jolivet PetscValidHeaderSpecific(is, IS_CLASSID, 1);
1320cf67ef9dSPierre Jolivet PetscValidHeaderSpecific(in_C, MAT_CLASSID, 4);
1321f1580f4eSBarry Smith PetscCall(ISSorted(is, &sorted));
1322f1580f4eSBarry Smith if (!sorted) {
1323f1580f4eSBarry Smith PetscCall(ISGetLocalSize(is, &size));
1324f1580f4eSBarry Smith PetscCall(ISGetIndices(is, &ptr));
1325b07dfdedSPierre Jolivet PetscCall(ISGetBlockSize(is, &bs));
1326f1580f4eSBarry Smith /* MatCreateSubMatrices(), called by PCASM, follows the global numbering of Pmat */
1327811e8887SPierre Jolivet for (PetscInt n = 0; n < size; n += bs) order.insert(std::make_pair(ptr[n] / bs, n / bs));
1328f1580f4eSBarry Smith PetscCall(ISRestoreIndices(is, &ptr));
1329b07dfdedSPierre Jolivet size /= bs;
1330f1580f4eSBarry Smith if (out_C) {
1331f1580f4eSBarry Smith PetscCall(PetscMalloc1(size, &concatenate));
1332f1580f4eSBarry Smith for (const std::pair<const PetscInt, PetscInt> &i : order) *concatenate++ = i.second;
1333f1580f4eSBarry Smith concatenate -= size;
1334b07dfdedSPierre Jolivet PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)in_C), bs, size, concatenate, PETSC_OWN_POINTER, &perm));
1335f1580f4eSBarry Smith PetscCall(ISSetPermutation(perm));
1336f1580f4eSBarry Smith /* permute user-provided Mat so that it matches with MatCreateSubMatrices() numbering */
1337f1580f4eSBarry Smith PetscCall(MatPermute(in_C, perm, perm, out_C));
1338f1580f4eSBarry Smith if (p) *p = perm;
1339f1580f4eSBarry Smith else PetscCall(ISDestroy(&perm)); /* no need to save the permutation */
1340f1580f4eSBarry Smith }
1341f1580f4eSBarry Smith if (out_is) {
1342f1580f4eSBarry Smith PetscCall(PetscMalloc1(size, &concatenate));
1343f1580f4eSBarry Smith for (const std::pair<const PetscInt, PetscInt> &i : order) *concatenate++ = i.first;
1344f1580f4eSBarry Smith concatenate -= size;
1345f1580f4eSBarry Smith /* permute user-provided IS so that it matches with MatCreateSubMatrices() numbering */
1346b07dfdedSPierre Jolivet PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)in_is), bs, size, concatenate, PETSC_OWN_POINTER, out_is));
1347f1580f4eSBarry Smith }
1348f1580f4eSBarry Smith } else { /* input IS is sorted, nothing to permute, simply duplicate inputs when needed */
1349f1580f4eSBarry Smith if (out_C) PetscCall(MatDuplicate(in_C, MAT_COPY_VALUES, out_C));
1350f1580f4eSBarry Smith if (out_is) PetscCall(ISDuplicate(in_is, out_is));
1351f1580f4eSBarry Smith }
13523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1353f1580f4eSBarry Smith }
1354f1580f4eSBarry Smith
PCHPDDMCheckSymmetry_Private(PC pc,Mat A01,Mat A10,Mat * B01=nullptr)1355281f8ce6SPierre Jolivet static PetscErrorCode PCHPDDMCheckSymmetry_Private(PC pc, Mat A01, Mat A10, Mat *B01 = nullptr)
135613044ca3SPierre Jolivet {
135713044ca3SPierre Jolivet Mat T, U = nullptr, B = nullptr;
135813044ca3SPierre Jolivet IS z;
1359281f8ce6SPierre Jolivet PetscBool flg, conjugate = PETSC_FALSE;
136013044ca3SPierre Jolivet
136113044ca3SPierre Jolivet PetscFunctionBegin;
1362281f8ce6SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATTRANSPOSEVIRTUAL, &flg));
1363281f8ce6SPierre Jolivet if (B01) *B01 = nullptr;
1364281f8ce6SPierre Jolivet if (flg) {
1365811e8887SPierre Jolivet PetscCall(MatShellGetScalingShifts(A10, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
1366811e8887SPierre Jolivet PetscCall(MatTransposeGetMat(A10, &U));
1367811e8887SPierre Jolivet } else {
1368281f8ce6SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATHERMITIANTRANSPOSEVIRTUAL, &flg));
1369281f8ce6SPierre Jolivet if (flg) {
1370811e8887SPierre Jolivet PetscCall(MatShellGetScalingShifts(A10, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
1371811e8887SPierre Jolivet PetscCall(MatHermitianTransposeGetMat(A10, &U));
1372281f8ce6SPierre Jolivet conjugate = PETSC_TRUE;
1373811e8887SPierre Jolivet }
137413044ca3SPierre Jolivet }
137513044ca3SPierre Jolivet if (U) PetscCall(MatDuplicate(U, MAT_COPY_VALUES, &T));
137613044ca3SPierre Jolivet else PetscCall(MatHermitianTranspose(A10, MAT_INITIAL_MATRIX, &T));
1377281f8ce6SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A01, MATTRANSPOSEVIRTUAL, &flg));
1378281f8ce6SPierre Jolivet if (flg) {
1379811e8887SPierre Jolivet PetscCall(MatShellGetScalingShifts(A01, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
138013044ca3SPierre Jolivet PetscCall(MatTransposeGetMat(A01, &A01));
138113044ca3SPierre Jolivet PetscCall(MatTranspose(A01, MAT_INITIAL_MATRIX, &B));
138213044ca3SPierre Jolivet A01 = B;
138313044ca3SPierre Jolivet } else {
1384281f8ce6SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A01, MATHERMITIANTRANSPOSEVIRTUAL, &flg));
1385281f8ce6SPierre Jolivet if (flg) {
1386811e8887SPierre Jolivet PetscCall(MatShellGetScalingShifts(A01, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
138713044ca3SPierre Jolivet PetscCall(MatHermitianTransposeGetMat(A01, &A01));
138813044ca3SPierre Jolivet PetscCall(MatHermitianTranspose(A01, MAT_INITIAL_MATRIX, &B));
138913044ca3SPierre Jolivet A01 = B;
139013044ca3SPierre Jolivet }
139113044ca3SPierre Jolivet }
1392281f8ce6SPierre Jolivet PetscCall(PetscLayoutCompare(T->rmap, A01->rmap, &flg));
1393281f8ce6SPierre Jolivet if (flg) {
1394281f8ce6SPierre Jolivet PetscCall(PetscLayoutCompare(T->cmap, A01->cmap, &flg));
1395281f8ce6SPierre Jolivet if (flg) {
139613044ca3SPierre Jolivet PetscCall(MatFindZeroRows(A01, &z)); /* for essential boundary conditions, some implementations will */
139713044ca3SPierre Jolivet if (z) { /* zero rows in [P00 A01] except for the diagonal of P00 */
1398281f8ce6SPierre Jolivet if (B01) PetscCall(MatDuplicate(T, MAT_COPY_VALUES, B01));
139913044ca3SPierre Jolivet PetscCall(MatSetOption(T, MAT_NO_OFF_PROC_ZERO_ROWS, PETSC_TRUE));
140013044ca3SPierre Jolivet PetscCall(MatZeroRowsIS(T, z, 0.0, nullptr, nullptr)); /* corresponding zero rows from A01 */
1401281f8ce6SPierre Jolivet }
1402281f8ce6SPierre Jolivet PetscCall(MatMultEqual(A01, T, 20, &flg));
1403281f8ce6SPierre Jolivet if (!B01) PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "A01 != A10^T");
1404281f8ce6SPierre Jolivet else {
1405281f8ce6SPierre Jolivet PetscCall(PetscInfo(pc, "A01 and A10^T are equal? %s\n", PetscBools[flg]));
1406281f8ce6SPierre Jolivet if (!flg) {
1407281f8ce6SPierre Jolivet if (z) PetscCall(MatDestroy(&T));
1408281f8ce6SPierre Jolivet else *B01 = T;
1409281f8ce6SPierre Jolivet flg = PETSC_TRUE;
1410281f8ce6SPierre Jolivet } else PetscCall(MatDestroy(B01));
1411281f8ce6SPierre Jolivet }
141213044ca3SPierre Jolivet PetscCall(ISDestroy(&z));
141313044ca3SPierre Jolivet }
141413044ca3SPierre Jolivet }
1415281f8ce6SPierre Jolivet if (!flg) PetscCall(PetscInfo(pc, "A01 and A10^T have non-congruent layouts, cannot test for equality\n"));
1416281f8ce6SPierre Jolivet if (!B01 || !*B01) PetscCall(MatDestroy(&T));
1417281f8ce6SPierre Jolivet else if (conjugate) PetscCall(MatConjugate(T));
141813044ca3SPierre Jolivet PetscCall(MatDestroy(&B));
141913044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
142013044ca3SPierre Jolivet }
142113044ca3SPierre Jolivet
PCHPDDMCheckInclusion_Private(PC pc,IS is,IS is_local,PetscBool check)1422d16c0b94SPierre Jolivet static PetscErrorCode PCHPDDMCheckInclusion_Private(PC pc, IS is, IS is_local, PetscBool check)
1423d16c0b94SPierre Jolivet {
1424d16c0b94SPierre Jolivet IS intersect;
1425d16c0b94SPierre Jolivet const char *str = "IS of the auxiliary Mat does not include all local rows of A";
1426d16c0b94SPierre Jolivet PetscBool equal;
1427d16c0b94SPierre Jolivet
1428d16c0b94SPierre Jolivet PetscFunctionBegin;
1429d16c0b94SPierre Jolivet PetscCall(ISIntersect(is, is_local, &intersect));
1430d16c0b94SPierre Jolivet PetscCall(ISEqualUnsorted(is_local, intersect, &equal));
1431d16c0b94SPierre Jolivet PetscCall(ISDestroy(&intersect));
1432d16c0b94SPierre Jolivet if (check) PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "%s", str);
1433d16c0b94SPierre Jolivet else if (!equal) PetscCall(PetscInfo(pc, "%s\n", str));
1434d16c0b94SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
1435d16c0b94SPierre Jolivet }
1436d16c0b94SPierre Jolivet
PCHPDDMCheckMatStructure_Private(PC pc,Mat A,Mat B)14378a8e6071SPierre Jolivet static PetscErrorCode PCHPDDMCheckMatStructure_Private(PC pc, Mat A, Mat B)
14388a8e6071SPierre Jolivet {
14398a8e6071SPierre Jolivet Mat X, Y;
14408a8e6071SPierre Jolivet const PetscInt *i[2], *j[2];
14418a8e6071SPierre Jolivet PetscBool flg = PETSC_TRUE;
14428a8e6071SPierre Jolivet
14438a8e6071SPierre Jolivet PetscFunctionBegin;
14448a8e6071SPierre Jolivet PetscCall(MatConvert(A, MATAIJ, MAT_INITIAL_MATRIX, &X)); /* no common way to compare sparsity pattern, so just convert to MATSEQAIJ */
14458a8e6071SPierre Jolivet PetscCall(MatConvert(B, MATAIJ, MAT_INITIAL_MATRIX, &Y)); /* the second Mat (B = Neumann) should have a SUBSET_NONZERO_PATTERN MatStructure of the first one (A = Dirichlet) */
14468a8e6071SPierre Jolivet PetscCall(MatSeqAIJGetCSRAndMemType(X, &i[0], &j[0], nullptr, nullptr));
14478a8e6071SPierre Jolivet PetscCall(MatSeqAIJGetCSRAndMemType(Y, &i[1], &j[1], nullptr, nullptr));
14488a8e6071SPierre Jolivet for (PetscInt row = 0; (row < X->rmap->n) && flg; ++row) {
14498a8e6071SPierre Jolivet const PetscInt n = i[0][row + 1] - i[0][row];
14508a8e6071SPierre Jolivet
145101962aebSPierre Jolivet for (PetscInt k = i[1][row], location; k < i[1][row + 1]; ++k) {
145201962aebSPierre Jolivet PetscCall(PetscFindInt(j[1][k], n, j[0] + i[0][row], &location));
145301962aebSPierre Jolivet if (location < 0) {
14548a8e6071SPierre Jolivet flg = PETSC_FALSE;
14558a8e6071SPierre Jolivet break;
14568a8e6071SPierre Jolivet }
14578a8e6071SPierre Jolivet }
14588a8e6071SPierre Jolivet }
14598a8e6071SPierre Jolivet PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &flg, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)pc)));
14608a8e6071SPierre Jolivet PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_USER_INPUT, "Auxiliary Mat is supposedly the local Neumann matrix but it has a sparsity pattern which is not a subset of the one of the local assembled matrix");
14618a8e6071SPierre Jolivet PetscCall(MatDestroy(&Y));
14628a8e6071SPierre Jolivet PetscCall(MatDestroy(&X));
14638a8e6071SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
14648a8e6071SPierre Jolivet }
14658a8e6071SPierre Jolivet
PCHPDDMDestroySubMatrices_Private(PetscBool flg,PetscBool algebraic,Mat * sub)1466d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMDestroySubMatrices_Private(PetscBool flg, PetscBool algebraic, Mat *sub)
1467d71ae5a4SJacob Faibussowitsch {
1468f1580f4eSBarry Smith IS is;
1469f1580f4eSBarry Smith
1470f1580f4eSBarry Smith PetscFunctionBegin;
1471f1580f4eSBarry Smith if (!flg) {
1472f1580f4eSBarry Smith if (algebraic) {
1473f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)sub[0], "_PCHPDDM_Embed", (PetscObject *)&is));
1474f1580f4eSBarry Smith PetscCall(ISDestroy(&is));
1475db4a47b3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Embed", nullptr));
1476db4a47b3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Compact", nullptr));
1477f1580f4eSBarry Smith }
1478f1580f4eSBarry Smith PetscCall(MatDestroySubMatrices(algebraic ? 2 : 1, &sub));
1479f1580f4eSBarry Smith }
14803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1481f1580f4eSBarry Smith }
1482f1580f4eSBarry Smith
PCHPDDMAlgebraicAuxiliaryMat_Private(Mat P,IS * is,Mat * sub[],PetscBool block)1483d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMAlgebraicAuxiliaryMat_Private(Mat P, IS *is, Mat *sub[], PetscBool block)
1484d71ae5a4SJacob Faibussowitsch {
1485f1580f4eSBarry Smith IS icol[3], irow[2];
1486f1580f4eSBarry Smith Mat *M, Q;
1487f1580f4eSBarry Smith PetscReal *ptr;
148858b7e2c1SStefano Zampini PetscInt *idx, p = 0, bs = P->cmap->bs;
1489f1580f4eSBarry Smith PetscBool flg;
1490f1580f4eSBarry Smith
1491f1580f4eSBarry Smith PetscFunctionBegin;
1492f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, P->cmap->N, 0, 1, icol + 2));
1493f1580f4eSBarry Smith PetscCall(ISSetBlockSize(icol[2], bs));
1494f1580f4eSBarry Smith PetscCall(ISSetIdentity(icol[2]));
1495f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)P, MATMPISBAIJ, &flg));
1496f1580f4eSBarry Smith if (flg) {
1497f1580f4eSBarry Smith /* MatCreateSubMatrices() does not handle MATMPISBAIJ properly when iscol != isrow, so convert first to MATMPIBAIJ */
1498f1580f4eSBarry Smith PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &Q));
1499f1580f4eSBarry Smith std::swap(P, Q);
1500f1580f4eSBarry Smith }
1501f1580f4eSBarry Smith PetscCall(MatCreateSubMatrices(P, 1, is, icol + 2, MAT_INITIAL_MATRIX, &M));
1502f1580f4eSBarry Smith if (flg) {
1503f1580f4eSBarry Smith std::swap(P, Q);
1504f1580f4eSBarry Smith PetscCall(MatDestroy(&Q));
1505f1580f4eSBarry Smith }
1506f1580f4eSBarry Smith PetscCall(ISDestroy(icol + 2));
1507f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, M[0]->rmap->N, 0, 1, irow));
1508f1580f4eSBarry Smith PetscCall(ISSetBlockSize(irow[0], bs));
1509f1580f4eSBarry Smith PetscCall(ISSetIdentity(irow[0]));
1510f1580f4eSBarry Smith if (!block) {
1511b07dfdedSPierre Jolivet PetscCall(PetscMalloc2(P->cmap->N, &ptr, P->cmap->N / bs, &idx));
1512f1580f4eSBarry Smith PetscCall(MatGetColumnNorms(M[0], NORM_INFINITY, ptr));
1513f1580f4eSBarry Smith /* check for nonzero columns so that M[0] may be expressed in compact form */
1514811e8887SPierre Jolivet for (PetscInt n = 0; n < P->cmap->N; n += bs) {
1515b07dfdedSPierre Jolivet if (std::find_if(ptr + n, ptr + n + bs, [](PetscReal v) { return v > PETSC_MACHINE_EPSILON; }) != ptr + n + bs) idx[p++] = n / bs;
1516f1580f4eSBarry Smith }
1517b07dfdedSPierre Jolivet PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, p, idx, PETSC_USE_POINTER, icol + 1));
1518f1580f4eSBarry Smith PetscCall(ISSetInfo(icol[1], IS_SORTED, IS_GLOBAL, PETSC_TRUE, PETSC_TRUE));
1519f1580f4eSBarry Smith PetscCall(ISEmbed(*is, icol[1], PETSC_FALSE, icol + 2));
1520f1580f4eSBarry Smith irow[1] = irow[0];
1521f1580f4eSBarry 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 */
1522f1580f4eSBarry Smith icol[0] = is[0];
1523f1580f4eSBarry Smith PetscCall(MatCreateSubMatrices(M[0], 2, irow, icol, MAT_INITIAL_MATRIX, sub));
1524f1580f4eSBarry Smith PetscCall(ISDestroy(icol + 1));
1525f1580f4eSBarry Smith PetscCall(PetscFree2(ptr, idx));
1526f1580f4eSBarry Smith /* IS used to go back and forth between the augmented and the original local linear system, see eq. (3.4) of [2022b] */
1527f1580f4eSBarry Smith PetscCall(PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Embed", (PetscObject)icol[2]));
1528f1580f4eSBarry Smith /* Mat used in eq. (3.1) of [2022b] */
1529f1580f4eSBarry Smith PetscCall(PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Compact", (PetscObject)(*sub)[1]));
1530f1580f4eSBarry Smith } else {
1531f1580f4eSBarry Smith Mat aux;
1532811e8887SPierre Jolivet
1533f1580f4eSBarry Smith PetscCall(MatSetOption(M[0], MAT_SUBMAT_SINGLEIS, PETSC_TRUE));
1534f1580f4eSBarry Smith /* diagonal block of the overlapping rows */
1535f1580f4eSBarry Smith PetscCall(MatCreateSubMatrices(M[0], 1, irow, is, MAT_INITIAL_MATRIX, sub));
1536f1580f4eSBarry Smith PetscCall(MatDuplicate((*sub)[0], MAT_COPY_VALUES, &aux));
1537f1580f4eSBarry Smith PetscCall(MatSetOption(aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
1538f1580f4eSBarry Smith if (bs == 1) { /* scalar case */
1539f1580f4eSBarry Smith Vec sum[2];
1540811e8887SPierre Jolivet
1541f1580f4eSBarry Smith PetscCall(MatCreateVecs(aux, sum, sum + 1));
1542f1580f4eSBarry Smith PetscCall(MatGetRowSum(M[0], sum[0]));
1543f1580f4eSBarry Smith PetscCall(MatGetRowSum(aux, sum[1]));
1544f1580f4eSBarry Smith /* off-diagonal block row sum (full rows - diagonal block rows) */
1545f1580f4eSBarry Smith PetscCall(VecAXPY(sum[0], -1.0, sum[1]));
1546f1580f4eSBarry Smith /* subdomain matrix plus off-diagonal block row sum */
1547f1580f4eSBarry Smith PetscCall(MatDiagonalSet(aux, sum[0], ADD_VALUES));
1548f1580f4eSBarry Smith PetscCall(VecDestroy(sum));
1549f1580f4eSBarry Smith PetscCall(VecDestroy(sum + 1));
1550f1580f4eSBarry Smith } else { /* vectorial case */
1551f1580f4eSBarry Smith /* TODO: missing MatGetValuesBlocked(), so the code below is */
1552f1580f4eSBarry Smith /* an extension of the scalar case for when bs > 1, but it could */
1553f1580f4eSBarry Smith /* be more efficient by avoiding all these MatMatMult() */
1554f1580f4eSBarry Smith Mat sum[2], ones;
1555f1580f4eSBarry Smith PetscScalar *ptr;
1556811e8887SPierre Jolivet
1557f1580f4eSBarry Smith PetscCall(PetscCalloc1(M[0]->cmap->n * bs, &ptr));
1558f1580f4eSBarry Smith PetscCall(MatCreateDense(PETSC_COMM_SELF, M[0]->cmap->n, bs, M[0]->cmap->n, bs, ptr, &ones));
1559811e8887SPierre Jolivet for (PetscInt n = 0; n < M[0]->cmap->n; n += bs) {
1560f1580f4eSBarry Smith for (p = 0; p < bs; ++p) ptr[n + p * (M[0]->cmap->n + 1)] = 1.0;
1561f1580f4eSBarry Smith }
1562fb842aefSJose E. Roman PetscCall(MatMatMult(M[0], ones, MAT_INITIAL_MATRIX, PETSC_CURRENT, sum));
1563f1580f4eSBarry Smith PetscCall(MatDestroy(&ones));
1564f1580f4eSBarry Smith PetscCall(MatCreateDense(PETSC_COMM_SELF, aux->cmap->n, bs, aux->cmap->n, bs, ptr, &ones));
1565f1580f4eSBarry Smith PetscCall(MatDenseSetLDA(ones, M[0]->cmap->n));
1566fb842aefSJose E. Roman PetscCall(MatMatMult(aux, ones, MAT_INITIAL_MATRIX, PETSC_CURRENT, sum + 1));
1567f1580f4eSBarry Smith PetscCall(MatDestroy(&ones));
1568f1580f4eSBarry Smith PetscCall(PetscFree(ptr));
1569f1580f4eSBarry Smith /* off-diagonal block row sum (full rows - diagonal block rows) */
1570f1580f4eSBarry Smith PetscCall(MatAXPY(sum[0], -1.0, sum[1], SAME_NONZERO_PATTERN));
1571f1580f4eSBarry Smith PetscCall(MatDestroy(sum + 1));
1572f1580f4eSBarry Smith /* re-order values to be consistent with MatSetValuesBlocked() */
1573f1580f4eSBarry Smith /* equivalent to MatTranspose() which does not truly handle */
1574f1580f4eSBarry Smith /* MAT_INPLACE_MATRIX in the rectangular case, as it calls PetscMalloc() */
1575f1580f4eSBarry Smith PetscCall(MatDenseGetArrayWrite(sum[0], &ptr));
1576f1580f4eSBarry Smith HPDDM::Wrapper<PetscScalar>::imatcopy<'T'>(bs, sum[0]->rmap->n, ptr, sum[0]->rmap->n, bs);
1577f1580f4eSBarry Smith /* subdomain matrix plus off-diagonal block row sum */
1578811e8887SPierre Jolivet for (PetscInt n = 0; n < aux->cmap->n / bs; ++n) PetscCall(MatSetValuesBlocked(aux, 1, &n, 1, &n, ptr + n * bs * bs, ADD_VALUES));
1579f1580f4eSBarry Smith PetscCall(MatAssemblyBegin(aux, MAT_FINAL_ASSEMBLY));
1580f1580f4eSBarry Smith PetscCall(MatAssemblyEnd(aux, MAT_FINAL_ASSEMBLY));
1581f1580f4eSBarry Smith PetscCall(MatDenseRestoreArrayWrite(sum[0], &ptr));
1582f1580f4eSBarry Smith PetscCall(MatDestroy(sum));
1583f1580f4eSBarry Smith }
1584f1580f4eSBarry Smith PetscCall(MatSetOption(aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
1585f1580f4eSBarry Smith /* left-hand side of GenEO, with the same sparsity pattern as PCASM subdomain solvers */
1586f1580f4eSBarry Smith PetscCall(PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Neumann_Mat", (PetscObject)aux));
1587f1580f4eSBarry Smith }
1588f1580f4eSBarry Smith PetscCall(ISDestroy(irow));
1589f1580f4eSBarry Smith PetscCall(MatDestroySubMatrices(1, &M));
15903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1591f1580f4eSBarry Smith }
1592f1580f4eSBarry Smith
PCApply_Nest(PC pc,Vec x,Vec y)159313044ca3SPierre Jolivet static PetscErrorCode PCApply_Nest(PC pc, Vec x, Vec y)
159413044ca3SPierre Jolivet {
159513044ca3SPierre Jolivet Mat A;
159613044ca3SPierre Jolivet MatSolverType type;
159713044ca3SPierre Jolivet IS is[2];
159813044ca3SPierre Jolivet PetscBool flg;
159913044ca3SPierre Jolivet std::pair<PC, Vec[2]> *p;
160013044ca3SPierre Jolivet
160113044ca3SPierre Jolivet PetscFunctionBegin;
160213044ca3SPierre Jolivet PetscCall(PCShellGetContext(pc, &p));
160351ea4bc8SPierre Jolivet if (p->second[0]) { /* in case of a centralized Schur complement, some processes may have no local operator */
160413044ca3SPierre Jolivet PetscCall(PCGetOperators(p->first, &A, nullptr));
160513044ca3SPierre Jolivet PetscCall(MatNestGetISs(A, is, nullptr));
160651ea4bc8SPierre Jolivet PetscCall(PetscObjectTypeCompareAny((PetscObject)p->first, &flg, PCLU, PCCHOLESKY, ""));
160751ea4bc8SPierre Jolivet if (flg) { /* partial solve currently only makes sense with exact factorizations */
160813044ca3SPierre Jolivet PetscCall(PCFactorGetMatSolverType(p->first, &type));
160913044ca3SPierre Jolivet PetscCall(PCFactorGetMatrix(p->first, &A));
161051ea4bc8SPierre Jolivet if (A->schur) {
161113044ca3SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &flg));
161251ea4bc8SPierre Jolivet if (flg) PetscCall(MatMumpsSetIcntl(A, 26, 1)); /* reduction/condensation phase followed by Schur complement solve */
161351ea4bc8SPierre Jolivet } else flg = PETSC_FALSE;
161413044ca3SPierre Jolivet }
161513044ca3SPierre Jolivet PetscCall(VecISCopy(p->second[0], is[1], SCATTER_FORWARD, x)); /* assign the RHS associated to the Schur complement */
161613044ca3SPierre Jolivet PetscCall(PCApply(p->first, p->second[0], p->second[1]));
161713044ca3SPierre Jolivet PetscCall(VecISCopy(p->second[1], is[1], SCATTER_REVERSE, y)); /* retrieve the partial solution associated to the Schur complement */
161879578405SBarry Smith if (flg) PetscCall(MatMumpsSetIcntl(A, 26, -1)); /* default ICNTL(26) value in PETSc */
161913044ca3SPierre Jolivet }
162013044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
162113044ca3SPierre Jolivet }
162213044ca3SPierre Jolivet
PCView_Nest(PC pc,PetscViewer viewer)162313044ca3SPierre Jolivet static PetscErrorCode PCView_Nest(PC pc, PetscViewer viewer)
162413044ca3SPierre Jolivet {
162513044ca3SPierre Jolivet std::pair<PC, Vec[2]> *p;
162613044ca3SPierre Jolivet
162713044ca3SPierre Jolivet PetscFunctionBegin;
162813044ca3SPierre Jolivet PetscCall(PCShellGetContext(pc, &p));
162913044ca3SPierre Jolivet PetscCall(PCView(p->first, viewer));
163013044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
163113044ca3SPierre Jolivet }
163213044ca3SPierre Jolivet
PCDestroy_Nest(PC pc)163313044ca3SPierre Jolivet static PetscErrorCode PCDestroy_Nest(PC pc)
163413044ca3SPierre Jolivet {
163513044ca3SPierre Jolivet std::pair<PC, Vec[2]> *p;
163613044ca3SPierre Jolivet
163713044ca3SPierre Jolivet PetscFunctionBegin;
163813044ca3SPierre Jolivet PetscCall(PCShellGetContext(pc, &p));
163913044ca3SPierre Jolivet PetscCall(VecDestroy(p->second));
164013044ca3SPierre Jolivet PetscCall(VecDestroy(p->second + 1));
164113044ca3SPierre Jolivet PetscCall(PCDestroy(&p->first));
164213044ca3SPierre Jolivet PetscCall(PetscFree(p));
164313044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
164413044ca3SPierre Jolivet }
164513044ca3SPierre Jolivet
164613044ca3SPierre Jolivet template <bool T = false>
MatMult_Schur(Mat A,Vec x,Vec y)164713044ca3SPierre Jolivet static PetscErrorCode MatMult_Schur(Mat A, Vec x, Vec y)
164813044ca3SPierre Jolivet {
164901e3c840SPierre Jolivet std::tuple<Mat, PetscSF, Vec[2]> *ctx;
165013044ca3SPierre Jolivet
165113044ca3SPierre Jolivet PetscFunctionBegin;
165213044ca3SPierre Jolivet PetscCall(MatShellGetContext(A, &ctx));
165313044ca3SPierre Jolivet PetscCall(VecScatterBegin(std::get<1>(*ctx), x, std::get<2>(*ctx)[0], INSERT_VALUES, SCATTER_FORWARD)); /* local Vec with overlap */
165413044ca3SPierre Jolivet PetscCall(VecScatterEnd(std::get<1>(*ctx), x, std::get<2>(*ctx)[0], INSERT_VALUES, SCATTER_FORWARD));
165513044ca3SPierre Jolivet if (!T) PetscCall(MatMult(std::get<0>(*ctx), std::get<2>(*ctx)[0], std::get<2>(*ctx)[1])); /* local Schur complement */
165613044ca3SPierre Jolivet else PetscCall(MatMultTranspose(std::get<0>(*ctx), std::get<2>(*ctx)[0], std::get<2>(*ctx)[1]));
165713044ca3SPierre Jolivet PetscCall(VecSet(y, 0.0));
165813044ca3SPierre Jolivet PetscCall(VecScatterBegin(std::get<1>(*ctx), std::get<2>(*ctx)[1], y, ADD_VALUES, SCATTER_REVERSE)); /* global Vec with summed up contributions on the overlap */
165913044ca3SPierre Jolivet PetscCall(VecScatterEnd(std::get<1>(*ctx), std::get<2>(*ctx)[1], y, ADD_VALUES, SCATTER_REVERSE));
166013044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
166113044ca3SPierre Jolivet }
166213044ca3SPierre Jolivet
MatDestroy_Schur(Mat A)166313044ca3SPierre Jolivet static PetscErrorCode MatDestroy_Schur(Mat A)
166413044ca3SPierre Jolivet {
166501e3c840SPierre Jolivet std::tuple<Mat, PetscSF, Vec[2]> *ctx;
166613044ca3SPierre Jolivet
166713044ca3SPierre Jolivet PetscFunctionBegin;
166813044ca3SPierre Jolivet PetscCall(MatShellGetContext(A, &ctx));
166913044ca3SPierre Jolivet PetscCall(VecDestroy(std::get<2>(*ctx)));
167013044ca3SPierre Jolivet PetscCall(VecDestroy(std::get<2>(*ctx) + 1));
167113044ca3SPierre Jolivet PetscCall(PetscFree(ctx));
167213044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
167313044ca3SPierre Jolivet }
167413044ca3SPierre Jolivet
MatMult_SchurCorrection(Mat A,Vec x,Vec y)167513044ca3SPierre Jolivet static PetscErrorCode MatMult_SchurCorrection(Mat A, Vec x, Vec y)
167613044ca3SPierre Jolivet {
167713044ca3SPierre Jolivet PC pc;
167813044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx;
167913044ca3SPierre Jolivet
168013044ca3SPierre Jolivet PetscFunctionBegin;
168113044ca3SPierre Jolivet PetscCall(MatShellGetContext(A, &ctx));
168213044ca3SPierre Jolivet pc = ((PC_HPDDM *)std::get<0>(*ctx)[0]->data)->levels[0]->ksp->pc;
168313044ca3SPierre Jolivet if (std::get<2>(*ctx) == PC_LEFT || std::get<2>(*ctx) == PC_SIDE_DEFAULT) { /* Q_0 is the coarse correction associated to the A00 block from PCFIELDSPLIT */
168413044ca3SPierre Jolivet PetscCall(MatMult(std::get<1>(*ctx)[0], x, std::get<3>(*ctx)[1])); /* A_01 x */
168513044ca3SPierre Jolivet PetscCall(PCHPDDMDeflate_Private(pc, std::get<3>(*ctx)[1], std::get<3>(*ctx)[1])); /* Q_0 A_01 x */
168613044ca3SPierre Jolivet PetscCall(MatMult(std::get<1>(*ctx)[1], std::get<3>(*ctx)[1], std::get<3>(*ctx)[0])); /* A_10 Q_0 A_01 x */
168713044ca3SPierre Jolivet PetscCall(PCApply(std::get<0>(*ctx)[1], std::get<3>(*ctx)[0], y)); /* y = M_S^-1 A_10 Q_0 A_01 x */
168813044ca3SPierre Jolivet } else {
168913044ca3SPierre Jolivet PetscCall(PCApply(std::get<0>(*ctx)[1], x, std::get<3>(*ctx)[0])); /* M_S^-1 x */
169013044ca3SPierre Jolivet PetscCall(MatMult(std::get<1>(*ctx)[0], std::get<3>(*ctx)[0], std::get<3>(*ctx)[1])); /* A_01 M_S^-1 x */
169113044ca3SPierre Jolivet PetscCall(PCHPDDMDeflate_Private(pc, std::get<3>(*ctx)[1], std::get<3>(*ctx)[1])); /* Q_0 A_01 M_S^-1 x */
169213044ca3SPierre Jolivet PetscCall(MatMult(std::get<1>(*ctx)[1], std::get<3>(*ctx)[1], y)); /* y = A_10 Q_0 A_01 M_S^-1 x */
169313044ca3SPierre Jolivet }
169413044ca3SPierre Jolivet PetscCall(VecAXPY(y, -1.0, x)); /* y -= x, preconditioned eq. (24) of https://hal.science/hal-02343808v6/document (with a sign flip) */
169513044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
169613044ca3SPierre Jolivet }
169713044ca3SPierre Jolivet
MatView_SchurCorrection(Mat A,PetscViewer viewer)169813044ca3SPierre Jolivet static PetscErrorCode MatView_SchurCorrection(Mat A, PetscViewer viewer)
169913044ca3SPierre Jolivet {
170013044ca3SPierre Jolivet PetscBool ascii;
170113044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx;
170213044ca3SPierre Jolivet
170313044ca3SPierre Jolivet PetscFunctionBegin;
170413044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &ascii));
170513044ca3SPierre Jolivet if (ascii) {
170613044ca3SPierre Jolivet PetscCall(MatShellGetContext(A, &ctx));
170713044ca3SPierre Jolivet PetscCall(PetscViewerASCIIPrintf(viewer, "action of %s\n", std::get<2>(*ctx) == PC_LEFT || std::get<2>(*ctx) == PC_SIDE_DEFAULT ? "(I - M_S^-1 A_10 Q_0 A_01)" : "(I - A_10 Q_0 A_01 M_S^-1)"));
170813044ca3SPierre Jolivet PetscCall(PCView(std::get<0>(*ctx)[1], viewer)); /* no need to PCView(Q_0) since it will be done by PCFIELDSPLIT */
170913044ca3SPierre Jolivet }
171013044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
171113044ca3SPierre Jolivet }
171213044ca3SPierre Jolivet
MatDestroy_SchurCorrection(Mat A)171313044ca3SPierre Jolivet static PetscErrorCode MatDestroy_SchurCorrection(Mat A)
171413044ca3SPierre Jolivet {
171513044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx;
171613044ca3SPierre Jolivet
171713044ca3SPierre Jolivet PetscFunctionBegin;
171813044ca3SPierre Jolivet PetscCall(MatShellGetContext(A, &ctx));
171913044ca3SPierre Jolivet PetscCall(VecDestroy(std::get<3>(*ctx)));
172013044ca3SPierre Jolivet PetscCall(VecDestroy(std::get<3>(*ctx) + 1));
172113044ca3SPierre Jolivet PetscCall(VecDestroy(std::get<3>(*ctx) + 2));
172213044ca3SPierre Jolivet PetscCall(PCDestroy(std::get<0>(*ctx) + 1));
172313044ca3SPierre Jolivet PetscCall(PetscFree(ctx));
172413044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
172513044ca3SPierre Jolivet }
172613044ca3SPierre Jolivet
PCPostSolve_SchurPreLeastSquares(PC,KSP,Vec,Vec x)1727feebddf4SPierre Jolivet static PetscErrorCode PCPostSolve_SchurPreLeastSquares(PC, KSP, Vec, Vec x)
1728feebddf4SPierre Jolivet {
1729feebddf4SPierre Jolivet PetscFunctionBegin;
1730feebddf4SPierre Jolivet PetscCall(VecScale(x, -1.0));
1731feebddf4SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
1732feebddf4SPierre Jolivet }
1733feebddf4SPierre Jolivet
KSPPreSolve_SchurCorrection(KSP,Vec b,Vec,void * context)173413044ca3SPierre Jolivet static PetscErrorCode KSPPreSolve_SchurCorrection(KSP, Vec b, Vec, void *context)
173513044ca3SPierre Jolivet {
173613044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx = reinterpret_cast<std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *>(context);
173713044ca3SPierre Jolivet
1738a6b3e571SPierre Jolivet PetscFunctionBegin;
173913044ca3SPierre Jolivet if (std::get<2>(*ctx) == PC_LEFT || std::get<2>(*ctx) == PC_SIDE_DEFAULT) {
174013044ca3SPierre Jolivet PetscCall(PCApply(std::get<0>(*ctx)[1], b, std::get<3>(*ctx)[2]));
174113044ca3SPierre Jolivet std::swap(*b, *std::get<3>(*ctx)[2]); /* replace b by M^-1 b, but need to keep a copy of the original RHS, so swap it with the work Vec */
174213044ca3SPierre Jolivet }
174313044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
174413044ca3SPierre Jolivet }
174513044ca3SPierre Jolivet
KSPPostSolve_SchurCorrection(KSP,Vec b,Vec x,void * context)174613044ca3SPierre Jolivet static PetscErrorCode KSPPostSolve_SchurCorrection(KSP, Vec b, Vec x, void *context)
174713044ca3SPierre Jolivet {
174813044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx = reinterpret_cast<std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *>(context);
174913044ca3SPierre Jolivet
1750a6b3e571SPierre Jolivet PetscFunctionBegin;
175113044ca3SPierre Jolivet if (std::get<2>(*ctx) == PC_LEFT || std::get<2>(*ctx) == PC_SIDE_DEFAULT) std::swap(*b, *std::get<3>(*ctx)[2]); /* put back the original RHS where it belongs */
175213044ca3SPierre Jolivet else {
175313044ca3SPierre Jolivet PetscCall(PCApply(std::get<0>(*ctx)[1], x, std::get<3>(*ctx)[2]));
175413044ca3SPierre Jolivet PetscCall(VecCopy(std::get<3>(*ctx)[2], x)); /* replace x by M^-1 x */
175513044ca3SPierre Jolivet }
175613044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
175713044ca3SPierre Jolivet }
175813044ca3SPierre Jolivet
17599bb5c669SPierre Jolivet static PetscErrorCode MatMult_Harmonic(Mat, Vec, Vec);
17609bb5c669SPierre Jolivet static PetscErrorCode MatMultTranspose_Harmonic(Mat, Vec, Vec);
17619bb5c669SPierre Jolivet static PetscErrorCode MatProduct_AB_Harmonic(Mat, Mat, Mat, void *);
1762f21e3f8aSPierre Jolivet static PetscErrorCode MatProduct_AtB_Harmonic(Mat, Mat, Mat, void *);
17639bb5c669SPierre Jolivet static PetscErrorCode MatDestroy_Harmonic(Mat);
17649bb5c669SPierre Jolivet
PCSetUp_HPDDM(PC pc)1765d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_HPDDM(PC pc)
1766d71ae5a4SJacob Faibussowitsch {
1767f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
1768f1580f4eSBarry Smith PC inner;
1769f1580f4eSBarry Smith KSP *ksp;
177013044ca3SPierre Jolivet Mat *sub, A, P, N, C = nullptr, uaux = nullptr, weighted, subA[2], S;
1771f1580f4eSBarry Smith Vec xin, v;
1772f1580f4eSBarry Smith std::vector<Vec> initial;
1773db4a47b3SPierre Jolivet IS is[1], loc, uis = data->is, unsorted = nullptr;
1774f1580f4eSBarry Smith ISLocalToGlobalMapping l2g;
1775f1580f4eSBarry Smith char prefix[256];
1776f1580f4eSBarry Smith const char *pcpre;
1777f1580f4eSBarry Smith const PetscScalar *const *ev;
17789bb5c669SPierre Jolivet PetscInt n, requested = data->N, reused = 0, overlap = -1;
1779f1580f4eSBarry Smith MatStructure structure = UNKNOWN_NONZERO_PATTERN;
1780f1580f4eSBarry Smith PetscBool subdomains = PETSC_FALSE, flg = PETSC_FALSE, ismatis, swap = PETSC_FALSE, algebraic = PETSC_FALSE, block = PETSC_FALSE;
1781f1580f4eSBarry Smith DM dm;
178213044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx = nullptr;
1783398c7888SPierre Jolivet #if PetscDefined(USE_DEBUG)
1784db4a47b3SPierre Jolivet IS dis = nullptr;
1785db4a47b3SPierre Jolivet Mat daux = nullptr;
1786398c7888SPierre Jolivet #endif
1787f1580f4eSBarry Smith
1788f1580f4eSBarry Smith PetscFunctionBegin;
1789f1580f4eSBarry Smith PetscCheck(data->levels && data->levels[0], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not a single level allocated");
1790f1580f4eSBarry Smith PetscCall(PCGetOptionsPrefix(pc, &pcpre));
1791f1580f4eSBarry Smith PetscCall(PCGetOperators(pc, &A, &P));
1792f1580f4eSBarry Smith if (!data->levels[0]->ksp) {
1793f1580f4eSBarry Smith PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->ksp));
17943821be0aSBarry Smith PetscCall(KSPSetNestLevel(data->levels[0]->ksp, pc->kspnestlevel));
1795f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_%s_", pcpre ? pcpre : "", data->N > 1 ? "levels_1" : "coarse"));
1796f1580f4eSBarry Smith PetscCall(KSPSetOptionsPrefix(data->levels[0]->ksp, prefix));
1797f1580f4eSBarry Smith PetscCall(KSPSetType(data->levels[0]->ksp, KSPPREONLY));
1798371d2eb7SMartin Diehl } else if (data->levels[0]->ksp->pc && data->levels[0]->ksp->pc->setupcalled && data->levels[0]->ksp->pc->reusepreconditioner) {
1799f1580f4eSBarry Smith /* if the fine-level PCSHELL exists, its setup has succeeded, and one wants to reuse it, */
1800f1580f4eSBarry Smith /* then just propagate the appropriate flag to the coarser levels */
1801f1580f4eSBarry Smith for (n = 0; n < PETSC_PCHPDDM_MAXLEVELS && data->levels[n]; ++n) {
1802f1580f4eSBarry Smith /* the following KSP and PC may be NULL for some processes, hence the check */
1803f1580f4eSBarry Smith if (data->levels[n]->ksp) PetscCall(KSPSetReusePreconditioner(data->levels[n]->ksp, PETSC_TRUE));
1804f1580f4eSBarry Smith if (data->levels[n]->pc) PetscCall(PCSetReusePreconditioner(data->levels[n]->pc, PETSC_TRUE));
1805f1580f4eSBarry Smith }
1806f1580f4eSBarry Smith /* early bail out because there is nothing to do */
18073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
1808f1580f4eSBarry Smith } else {
1809f1580f4eSBarry Smith /* reset coarser levels */
1810f1580f4eSBarry Smith for (n = 1; n < PETSC_PCHPDDM_MAXLEVELS && data->levels[n]; ++n) {
1811371d2eb7SMartin Diehl if (data->levels[n]->ksp && data->levels[n]->ksp->pc && data->levels[n]->ksp->pc->setupcalled && data->levels[n]->ksp->pc->reusepreconditioner && n < data->N) {
1812f1580f4eSBarry Smith reused = data->N - n;
1813f1580f4eSBarry Smith break;
1814f1580f4eSBarry Smith }
1815f1580f4eSBarry Smith PetscCall(KSPDestroy(&data->levels[n]->ksp));
1816f1580f4eSBarry Smith PetscCall(PCDestroy(&data->levels[n]->pc));
1817f1580f4eSBarry Smith }
1818f1580f4eSBarry Smith /* check if some coarser levels are being reused */
1819462c564dSBarry Smith PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &reused, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)pc)));
1820f1580f4eSBarry Smith const int *addr = data->levels[0]->P ? data->levels[0]->P->getAddrLocal() : &HPDDM::i__0;
1821f1580f4eSBarry Smith
1822f1580f4eSBarry Smith if (addr != &HPDDM::i__0 && reused != data->N - 1) {
1823f1580f4eSBarry Smith /* reuse previously computed eigenvectors */
1824f1580f4eSBarry Smith ev = data->levels[0]->P->getVectors();
1825f1580f4eSBarry Smith if (ev) {
1826f1580f4eSBarry Smith initial.reserve(*addr);
1827f1580f4eSBarry Smith PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, data->levels[0]->P->getDof(), ev[0], &xin));
1828f1580f4eSBarry Smith for (n = 0; n < *addr; ++n) {
1829f1580f4eSBarry Smith PetscCall(VecDuplicate(xin, &v));
1830f1580f4eSBarry Smith PetscCall(VecPlaceArray(xin, ev[n]));
1831f1580f4eSBarry Smith PetscCall(VecCopy(xin, v));
1832f1580f4eSBarry Smith initial.emplace_back(v);
1833f1580f4eSBarry Smith PetscCall(VecResetArray(xin));
1834f1580f4eSBarry Smith }
1835f1580f4eSBarry Smith PetscCall(VecDestroy(&xin));
1836f1580f4eSBarry Smith }
1837f1580f4eSBarry Smith }
1838f1580f4eSBarry Smith }
1839f1580f4eSBarry Smith data->N -= reused;
1840f1580f4eSBarry Smith PetscCall(KSPSetOperators(data->levels[0]->ksp, A, P));
1841f1580f4eSBarry Smith
1842f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)P, MATIS, &ismatis));
1843f1580f4eSBarry Smith if (!data->is && !ismatis) {
1844db4a47b3SPierre Jolivet PetscErrorCode (*create)(DM, IS *, Mat *, PetscErrorCode (**)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void **) = nullptr;
1845db4a47b3SPierre Jolivet PetscErrorCode (*usetup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *) = nullptr;
1846db4a47b3SPierre Jolivet void *uctx = nullptr;
1847f1580f4eSBarry Smith
1848f1580f4eSBarry Smith /* first see if we can get the data from the DM */
1849f1580f4eSBarry Smith PetscCall(MatGetDM(P, &dm));
1850f1580f4eSBarry Smith if (!dm) PetscCall(MatGetDM(A, &dm));
1851f1580f4eSBarry Smith if (!dm) PetscCall(PCGetDM(pc, &dm));
1852907a3e9cSStefano Zampini if (dm) { /* this is the hook for DMPLEX for which the auxiliary Mat is the local Neumann matrix */
1853f1580f4eSBarry Smith PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", &create));
1854f1580f4eSBarry Smith if (create) {
1855f1580f4eSBarry Smith PetscCall((*create)(dm, &uis, &uaux, &usetup, &uctx));
1856c8ea6600SPierre 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 */
1857f1580f4eSBarry Smith }
1858f1580f4eSBarry Smith }
1859f1580f4eSBarry Smith if (!create) {
1860f1580f4eSBarry Smith if (!uis) {
1861f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_IS", (PetscObject *)&uis));
1862f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)uis));
1863f1580f4eSBarry Smith }
1864f1580f4eSBarry Smith if (!uaux) {
1865f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_Mat", (PetscObject *)&uaux));
1866f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)uaux));
1867f1580f4eSBarry Smith }
1868f1580f4eSBarry Smith /* look inside the Pmat instead of the PC, needed for MatSchurComplementComputeExplicitOperator() */
1869f1580f4eSBarry Smith if (!uis) {
1870f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)P, "_PCHPDDM_Neumann_IS", (PetscObject *)&uis));
1871f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)uis));
1872f1580f4eSBarry Smith }
1873f1580f4eSBarry Smith if (!uaux) {
1874f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)P, "_PCHPDDM_Neumann_Mat", (PetscObject *)&uaux));
1875f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)uaux));
1876f1580f4eSBarry Smith }
1877f1580f4eSBarry Smith }
1878f1580f4eSBarry Smith PetscCall(PCHPDDMSetAuxiliaryMat(pc, uis, uaux, usetup, uctx));
1879f1580f4eSBarry Smith PetscCall(MatDestroy(&uaux));
1880f1580f4eSBarry Smith PetscCall(ISDestroy(&uis));
1881f1580f4eSBarry Smith }
1882f1580f4eSBarry Smith
1883f1580f4eSBarry Smith if (!ismatis) {
1884f1580f4eSBarry Smith PetscCall(PCHPDDMSetUpNeumannOverlap_Private(pc));
18858dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetBool(((PetscObject)pc)->options, pcpre, "-pc_hpddm_block_splitting", &block, nullptr));
18868dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetInt(((PetscObject)pc)->options, pcpre, "-pc_hpddm_harmonic_overlap", &overlap, nullptr));
18875e642048SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)P, MATSCHURCOMPLEMENT, &flg));
188851ea4bc8SPierre Jolivet if (data->is || flg) {
18899bb5c669SPierre Jolivet if (block || overlap != -1) {
189002800ff6SPierre Jolivet PetscCall(ISDestroy(&data->is));
189102800ff6SPierre Jolivet PetscCall(MatDestroy(&data->aux));
189251ea4bc8SPierre Jolivet } else if (flg) {
189313044ca3SPierre Jolivet PCHPDDMSchurPreType type = PC_HPDDM_SCHUR_PRE_GENEO;
189413044ca3SPierre Jolivet
18958dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetEnum(((PetscObject)pc)->options, pcpre, "-pc_hpddm_schur_precondition", PCHPDDMSchurPreTypes, (PetscEnum *)&type, &flg));
189613044ca3SPierre Jolivet if (type == PC_HPDDM_SCHUR_PRE_LEAST_SQUARES) {
189713044ca3SPierre Jolivet PetscCall(ISDestroy(&data->is)); /* destroy any previously user-set objects since they will be set automatically */
189813044ca3SPierre Jolivet PetscCall(MatDestroy(&data->aux));
189913044ca3SPierre Jolivet } else if (type == PC_HPDDM_SCHUR_PRE_GENEO) {
190013044ca3SPierre Jolivet PetscContainer container = nullptr;
190113044ca3SPierre Jolivet
190213044ca3SPierre Jolivet PetscCall(PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Schur", (PetscObject *)&container));
190313044ca3SPierre Jolivet if (!container) { /* first call to PCSetUp() on the PC associated to the Schur complement */
190413044ca3SPierre Jolivet PC_HPDDM *data_00;
190513044ca3SPierre Jolivet KSP ksp, inner_ksp;
190613044ca3SPierre Jolivet PC pc_00;
1907cf67ef9dSPierre Jolivet Mat A11 = nullptr;
1908cf67ef9dSPierre Jolivet Vec d = nullptr;
19098a8e6071SPierre Jolivet PetscReal norm;
191051ea4bc8SPierre Jolivet const PetscInt *ranges;
191151ea4bc8SPierre Jolivet PetscMPIInt size;
191213044ca3SPierre Jolivet char *prefix;
191313044ca3SPierre Jolivet
191413044ca3SPierre Jolivet PetscCall(MatSchurComplementGetKSP(P, &ksp));
191513044ca3SPierre Jolivet PetscCall(KSPGetPC(ksp, &pc_00));
191613044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)pc_00, PCHPDDM, &flg));
1917fd310a01SPierre Jolivet PetscCheck(flg, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition %s and -%spc_type %s (!= %s)", pcpre ? pcpre : "", PCHPDDMSchurPreTypes[type], ((PetscObject)pc_00)->prefix ? ((PetscObject)pc_00)->prefix : "",
191813044ca3SPierre Jolivet ((PetscObject)pc_00)->type_name, PCHPDDM);
191913044ca3SPierre Jolivet data_00 = (PC_HPDDM *)pc_00->data;
1920fd310a01SPierre Jolivet PetscCheck(data_00->N == 2, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition %s and %" PetscInt_FMT " level%s instead of 2 for the A00 block -%s", pcpre ? pcpre : "", PCHPDDMSchurPreTypes[type],
1921fd310a01SPierre Jolivet data_00->N, data_00->N > 1 ? "s" : "", ((PetscObject)pc_00)->prefix);
192251ea4bc8SPierre Jolivet PetscCheck(data_00->levels[0]->pc, PetscObjectComm((PetscObject)P), PETSC_ERR_ORDER, "PC of the first block%s not setup yet", ((PetscObject)pc_00)->prefix ? std::string(std::string(" (") + ((PetscObject)pc_00)->prefix + std::string(")")).c_str() : "");
192313044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)data_00->levels[0]->pc, PCASM, &flg));
1924fd310a01SPierre Jolivet PetscCheck(flg, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition %s and -%spc_type %s (!= %s)", pcpre ? pcpre : "", PCHPDDMSchurPreTypes[type], ((PetscObject)data_00->levels[0]->pc)->prefix,
192513044ca3SPierre Jolivet ((PetscObject)data_00->levels[0]->pc)->type_name, PCASM);
192651ea4bc8SPierre Jolivet PetscCall(PetscNew(&ctx)); /* context to pass data around for the inner-most PC, which will be a proper PCHPDDM (or a dummy variable if the Schur complement is centralized on a single process) */
1927d16c0b94SPierre Jolivet PetscCall(MatSchurComplementGetSubMatrices(P, nullptr, nullptr, nullptr, nullptr, &A11));
192851ea4bc8SPierre Jolivet PetscCall(MatGetOwnershipRanges(A11, &ranges));
192951ea4bc8SPierre Jolivet PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A11), &size));
193051ea4bc8SPierre Jolivet flg = PetscBool(std::find_if(ranges, ranges + size + 1, [&](PetscInt v) { return v != ranges[0] && v != ranges[size]; }) == ranges + size + 1); /* are all local matrices but one of dimension 0 (centralized Schur complement)? */
193151ea4bc8SPierre Jolivet if (!flg) {
19325e642048SPierre Jolivet if (PetscDefined(USE_DEBUG) || !data->is) {
19335e642048SPierre Jolivet Mat A01, A10, B = nullptr, C = nullptr, *sub;
193413044ca3SPierre Jolivet
19355e642048SPierre Jolivet PetscCall(MatSchurComplementGetSubMatrices(P, &A, nullptr, &A01, &A10, nullptr));
19365e642048SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATTRANSPOSEVIRTUAL, &flg));
19375e642048SPierre Jolivet if (flg) {
19385e642048SPierre Jolivet PetscCall(MatTransposeGetMat(A10, &C));
19395e642048SPierre Jolivet PetscCall(MatTranspose(C, MAT_INITIAL_MATRIX, &B));
19405e642048SPierre Jolivet } else {
19415e642048SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATHERMITIANTRANSPOSEVIRTUAL, &flg));
19425e642048SPierre Jolivet if (flg) {
19435e642048SPierre Jolivet PetscCall(MatHermitianTransposeGetMat(A10, &C));
19445e642048SPierre Jolivet PetscCall(MatHermitianTranspose(C, MAT_INITIAL_MATRIX, &B));
19455e642048SPierre Jolivet }
19465e642048SPierre Jolivet }
1947811e8887SPierre Jolivet if (flg)
1948811e8887SPierre Jolivet PetscCall(MatShellGetScalingShifts(A10, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
19495e642048SPierre Jolivet if (!B) {
19505e642048SPierre Jolivet B = A10;
19515e642048SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)B));
19525e642048SPierre Jolivet } else if (!data->is) {
19535e642048SPierre Jolivet PetscCall(PetscObjectTypeCompareAny((PetscObject)A01, &flg, MATTRANSPOSEVIRTUAL, MATHERMITIANTRANSPOSEVIRTUAL, ""));
19545e642048SPierre Jolivet if (!flg) C = A01;
1955811e8887SPierre Jolivet else
1956811e8887SPierre Jolivet PetscCall(MatShellGetScalingShifts(A01, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (PetscScalar *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Vec *)MAT_SHELL_NOT_ALLOWED, (Mat *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED, (IS *)MAT_SHELL_NOT_ALLOWED));
19575e642048SPierre Jolivet }
195813044ca3SPierre Jolivet PetscCall(ISCreateStride(PETSC_COMM_SELF, B->rmap->N, 0, 1, &uis));
19595e642048SPierre Jolivet PetscCall(ISSetIdentity(uis));
19605e642048SPierre Jolivet if (!data->is) {
19615e642048SPierre Jolivet if (C) PetscCall(PetscObjectReference((PetscObject)C));
19625e642048SPierre Jolivet else PetscCall(MatTranspose(B, MAT_INITIAL_MATRIX, &C));
19635e642048SPierre Jolivet PetscCall(ISDuplicate(data_00->is, is));
19645e642048SPierre Jolivet PetscCall(MatIncreaseOverlap(A, 1, is, 1));
19655e642048SPierre Jolivet PetscCall(MatSetOption(C, MAT_SUBMAT_SINGLEIS, PETSC_TRUE));
19665e642048SPierre Jolivet PetscCall(MatCreateSubMatrices(C, 1, is, &uis, MAT_INITIAL_MATRIX, &sub));
19675e642048SPierre Jolivet PetscCall(MatDestroy(&C));
19685e642048SPierre Jolivet PetscCall(MatTranspose(sub[0], MAT_INITIAL_MATRIX, &C));
19695e642048SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &sub));
19705e642048SPierre Jolivet PetscCall(MatFindNonzeroRows(C, &data->is));
19719640b26aSPierre Jolivet PetscCheck(data->is, PetscObjectComm((PetscObject)C), PETSC_ERR_SUP, "No empty row, which likely means that some rows of A_10 are dense");
19725e642048SPierre Jolivet PetscCall(MatDestroy(&C));
19735e642048SPierre Jolivet PetscCall(ISDestroy(is));
1974d16c0b94SPierre Jolivet PetscCall(ISCreateStride(PetscObjectComm((PetscObject)data->is), A11->rmap->n, A11->rmap->rstart, 1, &loc));
1975d16c0b94SPierre Jolivet if (PetscDefined(USE_DEBUG)) PetscCall(PCHPDDMCheckInclusion_Private(pc, data->is, loc, PETSC_FALSE));
1976d16c0b94SPierre Jolivet PetscCall(ISExpand(data->is, loc, is));
1977d16c0b94SPierre Jolivet PetscCall(ISDestroy(&loc));
1978d16c0b94SPierre Jolivet PetscCall(ISDestroy(&data->is));
1979d16c0b94SPierre Jolivet data->is = is[0];
1980d16c0b94SPierre Jolivet is[0] = nullptr;
19815e642048SPierre Jolivet }
19825e642048SPierre Jolivet if (PetscDefined(USE_DEBUG)) {
19835e642048SPierre Jolivet PetscCall(PCHPDDMCheckSymmetry_Private(pc, A01, A10));
198413044ca3SPierre Jolivet PetscCall(MatCreateSubMatrices(B, 1, &uis, &data_00->is, MAT_INITIAL_MATRIX, &sub)); /* expensive check since all processes fetch all rows (but only some columns) of the constraint matrix */
198513044ca3SPierre Jolivet PetscCall(ISDestroy(&uis));
198613044ca3SPierre Jolivet PetscCall(ISDuplicate(data->is, &uis));
198713044ca3SPierre Jolivet PetscCall(ISSort(uis));
19885e642048SPierre Jolivet PetscCall(ISComplement(uis, 0, B->rmap->N, is));
198913044ca3SPierre Jolivet PetscCall(MatDuplicate(sub[0], MAT_COPY_VALUES, &C));
19905e642048SPierre Jolivet PetscCall(MatZeroRowsIS(C, is[0], 0.0, nullptr, nullptr));
19915e642048SPierre Jolivet PetscCall(ISDestroy(is));
199213044ca3SPierre Jolivet PetscCall(MatMultEqual(sub[0], C, 20, &flg));
199313044ca3SPierre Jolivet PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "The image of A_10 (R_i^p)^T from the local primal (e.g., velocity) space to the full dual (e.g., pressure) space is not restricted to the local dual space: A_10 (R_i^p)^T != R_i^d (R_i^d)^T A_10 (R_i^p)^T"); /* cf. eq. (9) of https://hal.science/hal-02343808v6/document */
199413044ca3SPierre Jolivet PetscCall(MatDestroy(&C));
199513044ca3SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &sub));
199613044ca3SPierre Jolivet }
19975e642048SPierre Jolivet PetscCall(ISDestroy(&uis));
19985e642048SPierre Jolivet PetscCall(MatDestroy(&B));
19995e642048SPierre Jolivet }
2000cf67ef9dSPierre Jolivet flg = PETSC_FALSE;
2001cf67ef9dSPierre Jolivet if (!data->aux) {
2002cf67ef9dSPierre Jolivet Mat D;
2003cf67ef9dSPierre Jolivet
2004cf67ef9dSPierre Jolivet PetscCall(MatCreateVecs(A11, &d, nullptr));
2005cf67ef9dSPierre Jolivet PetscCall(MatGetDiagonal(A11, d));
2006cf67ef9dSPierre Jolivet PetscCall(PetscObjectTypeCompareAny((PetscObject)A11, &flg, MATDIAGONAL, MATCONSTANTDIAGONAL, ""));
2007cf67ef9dSPierre Jolivet if (!flg) {
2008cf67ef9dSPierre Jolivet PetscCall(MatCreateDiagonal(d, &D));
2009cf67ef9dSPierre Jolivet PetscCall(MatMultEqual(A11, D, 20, &flg));
2010cf67ef9dSPierre Jolivet PetscCall(MatDestroy(&D));
2011cf67ef9dSPierre Jolivet }
2012cf67ef9dSPierre Jolivet if (flg) PetscCall(PetscInfo(pc, "A11 block is likely diagonal so the PC will build an auxiliary Mat (which was not initially provided by the user)\n"));
2013cf67ef9dSPierre Jolivet }
20148a8e6071SPierre Jolivet if ((PetscDefined(USE_DEBUG) || (data->Neumann != PETSC_BOOL3_TRUE && !flg)) && A11) {
2015cf67ef9dSPierre Jolivet PetscCall(MatNorm(A11, NORM_INFINITY, &norm));
20168a8e6071SPierre Jolivet if (data->Neumann != PETSC_BOOL3_TRUE && !flg) {
2017cf67ef9dSPierre Jolivet PetscCheck(norm < PETSC_MACHINE_EPSILON * static_cast<PetscReal>(10.0), PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition geneo and -%spc_hpddm_has_neumann != true with a nonzero or non-diagonal A11 block", pcpre ? pcpre : "", pcpre ? pcpre : "");
2018cf67ef9dSPierre Jolivet PetscCall(PetscInfo(pc, "A11 block is likely zero so the PC will build an auxiliary Mat (which was%s initially provided by the user)\n", data->aux ? "" : " not"));
2019cf67ef9dSPierre Jolivet PetscCall(MatDestroy(&data->aux));
2020cf67ef9dSPierre Jolivet flg = PETSC_TRUE;
2021cf67ef9dSPierre Jolivet }
20228a8e6071SPierre Jolivet }
2023cf67ef9dSPierre Jolivet if (!data->aux) { /* if A11 is near zero, e.g., Stokes equation, or diagonal, build an auxiliary (Neumann) Mat which is a (possibly slightly shifted) diagonal weighted by the inverse of the multiplicity */
202401e3c840SPierre Jolivet PetscSF scatter;
202513044ca3SPierre Jolivet const PetscScalar *read;
2026cf67ef9dSPierre Jolivet PetscScalar *write, *diagonal = nullptr;
202713044ca3SPierre Jolivet
202813044ca3SPierre Jolivet PetscCall(MatDestroy(&data->aux));
202913044ca3SPierre Jolivet PetscCall(ISGetLocalSize(data->is, &n));
2030cf67ef9dSPierre Jolivet PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)P), n, PETSC_DECIDE, &xin));
2031cf67ef9dSPierre Jolivet PetscCall(VecDuplicate(xin, &v));
2032cf67ef9dSPierre Jolivet PetscCall(VecScatterCreate(xin, data->is, v, nullptr, &scatter));
203313044ca3SPierre Jolivet PetscCall(VecSet(v, 1.0));
2034cf67ef9dSPierre Jolivet PetscCall(VecSet(xin, 1.0));
2035cf67ef9dSPierre Jolivet PetscCall(VecScatterBegin(scatter, v, xin, ADD_VALUES, SCATTER_REVERSE));
2036cf67ef9dSPierre Jolivet PetscCall(VecScatterEnd(scatter, v, xin, ADD_VALUES, SCATTER_REVERSE)); /* v has the multiplicity of all unknowns on the overlap */
2037cf67ef9dSPierre Jolivet PetscCall(PetscSFDestroy(&scatter));
2038cf67ef9dSPierre Jolivet if (d) {
2039cf67ef9dSPierre Jolivet PetscCall(VecScatterCreate(d, data->is, v, nullptr, &scatter));
2040cf67ef9dSPierre Jolivet PetscCall(VecScatterBegin(scatter, d, v, INSERT_VALUES, SCATTER_FORWARD));
2041cf67ef9dSPierre Jolivet PetscCall(VecScatterEnd(scatter, d, v, INSERT_VALUES, SCATTER_FORWARD));
2042cf67ef9dSPierre Jolivet PetscCall(PetscSFDestroy(&scatter));
2043cf67ef9dSPierre Jolivet PetscCall(VecDestroy(&d));
2044cf67ef9dSPierre Jolivet PetscCall(PetscMalloc1(n, &diagonal));
2045cf67ef9dSPierre Jolivet PetscCall(VecGetArrayRead(v, &read));
2046cf67ef9dSPierre Jolivet PetscCallCXX(std::copy_n(read, n, diagonal));
2047cf67ef9dSPierre Jolivet PetscCall(VecRestoreArrayRead(v, &read));
2048cf67ef9dSPierre Jolivet }
204913044ca3SPierre Jolivet PetscCall(VecDestroy(&v));
205013044ca3SPierre Jolivet PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &v));
2051cf67ef9dSPierre Jolivet PetscCall(VecGetArrayRead(xin, &read));
205213044ca3SPierre Jolivet PetscCall(VecGetArrayWrite(v, &write));
2053cf67ef9dSPierre Jolivet for (PetscInt i = 0; i < n; ++i) write[i] = (!diagonal || std::abs(diagonal[i]) < PETSC_MACHINE_EPSILON) ? PETSC_SMALL / (static_cast<PetscReal>(1000.0) * read[i]) : diagonal[i] / read[i];
2054cf67ef9dSPierre Jolivet PetscCall(PetscFree(diagonal));
2055cf67ef9dSPierre Jolivet PetscCall(VecRestoreArrayRead(xin, &read));
205613044ca3SPierre Jolivet PetscCall(VecRestoreArrayWrite(v, &write));
2057cf67ef9dSPierre Jolivet PetscCall(VecDestroy(&xin));
2058c3e1b152SPierre Jolivet PetscCall(MatCreateDiagonal(v, &data->aux));
205913044ca3SPierre Jolivet PetscCall(VecDestroy(&v));
206013044ca3SPierre Jolivet }
206113044ca3SPierre Jolivet uis = data->is;
206213044ca3SPierre Jolivet uaux = data->aux;
206313044ca3SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)uis));
206413044ca3SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)uaux));
206513044ca3SPierre Jolivet PetscCall(PetscStrallocpy(pcpre, &prefix));
206613044ca3SPierre Jolivet PetscCall(PCSetOptionsPrefix(pc, nullptr));
206713044ca3SPierre Jolivet PetscCall(PCSetType(pc, PCKSP)); /* replace the PC associated to the Schur complement by PCKSP */
206813044ca3SPierre Jolivet PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &inner_ksp)); /* new KSP that will be attached to the previously set PC */
206913044ca3SPierre Jolivet PetscCall(PetscObjectGetTabLevel((PetscObject)pc, &n));
207013044ca3SPierre Jolivet PetscCall(PetscObjectSetTabLevel((PetscObject)inner_ksp, n + 2));
207113044ca3SPierre Jolivet PetscCall(KSPSetOperators(inner_ksp, pc->mat, pc->pmat));
207213044ca3SPierre Jolivet PetscCall(KSPSetOptionsPrefix(inner_ksp, std::string(std::string(prefix) + "pc_hpddm_").c_str()));
207313044ca3SPierre Jolivet PetscCall(KSPSetSkipPCSetFromOptions(inner_ksp, PETSC_TRUE));
207413044ca3SPierre Jolivet PetscCall(KSPSetFromOptions(inner_ksp));
207513044ca3SPierre Jolivet PetscCall(KSPGetPC(inner_ksp, &inner));
207613044ca3SPierre Jolivet PetscCall(PCSetOptionsPrefix(inner, nullptr));
207713044ca3SPierre Jolivet PetscCall(PCSetType(inner, PCNONE)); /* no preconditioner since the action of M^-1 A or A M^-1 will be computed by the Amat */
207813044ca3SPierre Jolivet PetscCall(PCKSPSetKSP(pc, inner_ksp));
207913044ca3SPierre Jolivet std::get<0>(*ctx)[0] = pc_00; /* for coarse correction on the primal (e.g., velocity) space */
208013044ca3SPierre Jolivet PetscCall(PCCreate(PetscObjectComm((PetscObject)pc), &std::get<0>(*ctx)[1]));
208151ea4bc8SPierre Jolivet PetscCall(PCSetOptionsPrefix(pc, prefix)); /* both PC share the same prefix so that the outer PC can be reset with PCSetFromOptions() */
208213044ca3SPierre Jolivet PetscCall(PCSetOptionsPrefix(std::get<0>(*ctx)[1], prefix));
208313044ca3SPierre Jolivet PetscCall(PetscFree(prefix));
208413044ca3SPierre Jolivet PetscCall(PCSetOperators(std::get<0>(*ctx)[1], pc->mat, pc->pmat));
208513044ca3SPierre Jolivet PetscCall(PCSetType(std::get<0>(*ctx)[1], PCHPDDM));
208613044ca3SPierre Jolivet PetscCall(PCHPDDMSetAuxiliaryMat(std::get<0>(*ctx)[1], uis, uaux, nullptr, nullptr)); /* transfer ownership of the auxiliary inputs from the inner (PCKSP) to the inner-most (PCHPDDM) PC */
2087cf67ef9dSPierre Jolivet if (flg) static_cast<PC_HPDDM *>(std::get<0>(*ctx)[1]->data)->Neumann = PETSC_BOOL3_TRUE;
20888a8e6071SPierre Jolivet else if (PetscDefined(USE_DEBUG) && norm > PETSC_MACHINE_EPSILON * static_cast<PetscReal>(10.0)) {
20898a8e6071SPierre Jolivet /* no check when A11 is near zero */
20908a8e6071SPierre Jolivet PetscCall(MatCreateSubMatrices(A11, 1, &uis, &uis, MAT_INITIAL_MATRIX, &sub));
20918a8e6071SPierre Jolivet PetscCall(PCHPDDMCheckMatStructure_Private(pc, sub[0], uaux));
20928a8e6071SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &sub));
20938a8e6071SPierre Jolivet }
209413044ca3SPierre Jolivet PetscCall(PCSetFromOptions(std::get<0>(*ctx)[1]));
209513044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)uis));
209613044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)uaux));
209713044ca3SPierre Jolivet PetscCall(MatCreateShell(PetscObjectComm((PetscObject)pc), inner->mat->rmap->n, inner->mat->cmap->n, inner->mat->rmap->N, inner->mat->cmap->N, ctx, &S)); /* MatShell computing the action of M^-1 A or A M^-1 */
209857d50842SBarry Smith PetscCall(MatShellSetOperation(S, MATOP_MULT, (PetscErrorCodeFn *)MatMult_SchurCorrection));
209957d50842SBarry Smith PetscCall(MatShellSetOperation(S, MATOP_VIEW, (PetscErrorCodeFn *)MatView_SchurCorrection));
210057d50842SBarry Smith PetscCall(MatShellSetOperation(S, MATOP_DESTROY, (PetscErrorCodeFn *)MatDestroy_SchurCorrection));
210113044ca3SPierre Jolivet PetscCall(KSPGetPCSide(inner_ksp, &(std::get<2>(*ctx))));
210213044ca3SPierre Jolivet if (std::get<2>(*ctx) == PC_LEFT || std::get<2>(*ctx) == PC_SIDE_DEFAULT) {
210313044ca3SPierre Jolivet PetscCall(KSPSetPreSolve(inner_ksp, KSPPreSolve_SchurCorrection, ctx));
210413044ca3SPierre Jolivet } else { /* no support for PC_SYMMETRIC */
210513044ca3SPierre Jolivet PetscCheck(std::get<2>(*ctx) == PC_RIGHT, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "PCSide %s (!= %s or %s or %s)", PCSides[std::get<2>(*ctx)], PCSides[PC_SIDE_DEFAULT], PCSides[PC_LEFT], PCSides[PC_RIGHT]);
210613044ca3SPierre Jolivet }
210713044ca3SPierre Jolivet PetscCall(KSPSetPostSolve(inner_ksp, KSPPostSolve_SchurCorrection, ctx));
2108715c1178SPierre Jolivet PetscCall(PetscObjectContainerCompose((PetscObject)std::get<0>(*ctx)[1], "_PCHPDDM_Schur", ctx, nullptr));
210913044ca3SPierre Jolivet PetscCall(PCSetUp(std::get<0>(*ctx)[1]));
211013044ca3SPierre Jolivet PetscCall(KSPSetOperators(inner_ksp, S, S));
211113044ca3SPierre Jolivet PetscCall(MatCreateVecs(std::get<1>(*ctx)[0], std::get<3>(*ctx), std::get<3>(*ctx) + 1));
211213044ca3SPierre Jolivet PetscCall(VecDuplicate(std::get<3>(*ctx)[0], std::get<3>(*ctx) + 2));
211313044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)inner_ksp));
211413044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)S));
211551ea4bc8SPierre Jolivet } else {
211651ea4bc8SPierre Jolivet std::get<0>(*ctx)[0] = pc_00;
211751ea4bc8SPierre Jolivet PetscCall(PetscObjectContainerCompose((PetscObject)pc, "_PCHPDDM_Schur", ctx, nullptr));
211851ea4bc8SPierre Jolivet PetscCall(ISCreateStride(PetscObjectComm((PetscObject)data_00->is), A11->rmap->n, A11->rmap->rstart, 1, &data->is)); /* dummy variables in the case of a centralized Schur complement */
211951ea4bc8SPierre Jolivet PetscCall(MatGetDiagonalBlock(A11, &data->aux));
212051ea4bc8SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)data->aux));
212151ea4bc8SPierre Jolivet PetscCall(PCSetUp(pc));
212251ea4bc8SPierre Jolivet }
21230307214fSPierre Jolivet for (std::vector<Vec>::iterator it = initial.begin(); it != initial.end(); ++it) PetscCall(VecDestroy(&*it));
212413044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
212513044ca3SPierre Jolivet } else { /* second call to PCSetUp() on the PC associated to the Schur complement, retrieve previously set context */
21262a8381b2SBarry Smith PetscCall(PetscContainerGetPointer(container, &ctx));
212713044ca3SPierre Jolivet }
212813044ca3SPierre Jolivet }
212913044ca3SPierre Jolivet }
213013044ca3SPierre Jolivet }
2131f1580f4eSBarry Smith if (!data->is && data->N > 1) {
2132f1580f4eSBarry Smith char type[256] = {}; /* same size as in src/ksp/pc/interface/pcset.c */
2133811e8887SPierre Jolivet
2134f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompareAny((PetscObject)P, &flg, MATNORMAL, MATNORMALHERMITIAN, ""));
2135f1580f4eSBarry Smith if (flg || (A->rmap->N != A->cmap->N && P->rmap->N == P->cmap->N && P->rmap->N == A->cmap->N)) {
2136f1580f4eSBarry Smith Mat B;
2137811e8887SPierre Jolivet
2138f1580f4eSBarry Smith PetscCall(PCHPDDMSetAuxiliaryMatNormal_Private(pc, A, P, &B, pcpre));
2139f1580f4eSBarry Smith if (data->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED) data->correction = PC_HPDDM_COARSE_CORRECTION_BALANCED;
2140f1580f4eSBarry Smith PetscCall(MatDestroy(&B));
2141f1580f4eSBarry Smith } else {
2142f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)P, MATSCHURCOMPLEMENT, &flg));
2143f1580f4eSBarry Smith if (flg) {
2144f1580f4eSBarry Smith Mat A00, P00, A01, A10, A11, B, N;
214513044ca3SPierre Jolivet PCHPDDMSchurPreType type = PC_HPDDM_SCHUR_PRE_LEAST_SQUARES;
214613044ca3SPierre Jolivet
214713044ca3SPierre Jolivet PetscCall(MatSchurComplementGetSubMatrices(P, &A00, &P00, &A01, &A10, &A11));
21488dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetEnum(((PetscObject)pc)->options, pcpre, "-pc_hpddm_schur_precondition", PCHPDDMSchurPreTypes, (PetscEnum *)&type, &flg));
214913044ca3SPierre Jolivet if (type == PC_HPDDM_SCHUR_PRE_LEAST_SQUARES) {
2150281f8ce6SPierre Jolivet Mat B01;
21513df4cd7bSPierre Jolivet Vec diagonal = nullptr;
2152f1580f4eSBarry Smith const PetscScalar *array;
2153f1580f4eSBarry Smith MatSchurComplementAinvType type;
2154f1580f4eSBarry Smith
2155281f8ce6SPierre Jolivet PetscCall(PCHPDDMCheckSymmetry_Private(pc, A01, A10, &B01));
2156f1580f4eSBarry Smith if (A11) {
21573df4cd7bSPierre Jolivet PetscCall(MatCreateVecs(A11, &diagonal, nullptr));
21583df4cd7bSPierre Jolivet PetscCall(MatGetDiagonal(A11, diagonal));
2159f1580f4eSBarry Smith }
2160db4a47b3SPierre Jolivet PetscCall(MatCreateVecs(P00, &v, nullptr));
2161f1580f4eSBarry Smith PetscCall(MatSchurComplementGetAinvType(P, &type));
2162e5634b20SPierre Jolivet PetscCheck(type == MAT_SCHUR_COMPLEMENT_AINV_DIAG || type == MAT_SCHUR_COMPLEMENT_AINV_LUMP || type == MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG, PetscObjectComm((PetscObject)P), PETSC_ERR_SUP, "-%smat_schur_complement_ainv_type %s",
2163e5634b20SPierre Jolivet ((PetscObject)P)->prefix ? ((PetscObject)P)->prefix : "", MatSchurComplementAinvTypes[type]);
2164e5634b20SPierre Jolivet if (type != MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG) {
2165f1580f4eSBarry Smith if (type == MAT_SCHUR_COMPLEMENT_AINV_LUMP) {
2166f1580f4eSBarry Smith PetscCall(MatGetRowSum(P00, v));
2167f1580f4eSBarry Smith if (A00 == P00) PetscCall(PetscObjectReference((PetscObject)A00));
2168f1580f4eSBarry Smith PetscCall(MatDestroy(&P00));
2169f1580f4eSBarry Smith PetscCall(VecGetArrayRead(v, &array));
2170db4a47b3SPierre Jolivet PetscCall(MatCreateAIJ(PetscObjectComm((PetscObject)A00), A00->rmap->n, A00->cmap->n, A00->rmap->N, A00->cmap->N, 1, nullptr, 0, nullptr, &P00));
2171f1580f4eSBarry Smith PetscCall(MatSetOption(P00, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
2172f1580f4eSBarry Smith for (n = A00->rmap->rstart; n < A00->rmap->rend; ++n) PetscCall(MatSetValue(P00, n, n, array[n - A00->rmap->rstart], INSERT_VALUES));
2173f1580f4eSBarry Smith PetscCall(MatAssemblyBegin(P00, MAT_FINAL_ASSEMBLY));
2174f1580f4eSBarry Smith PetscCall(MatAssemblyEnd(P00, MAT_FINAL_ASSEMBLY));
2175f1580f4eSBarry Smith PetscCall(VecRestoreArrayRead(v, &array));
2176f1580f4eSBarry Smith PetscCall(MatSchurComplementUpdateSubMatrices(P, A00, P00, A01, A10, A11)); /* replace P00 by diag(sum of each row of P00) */
2177f1580f4eSBarry Smith PetscCall(MatDestroy(&P00));
2178f1580f4eSBarry Smith } else PetscCall(MatGetDiagonal(P00, v));
2179f1580f4eSBarry Smith PetscCall(VecReciprocal(v)); /* inv(diag(P00)) */
2180f1580f4eSBarry Smith PetscCall(VecSqrtAbs(v)); /* sqrt(inv(diag(P00))) */
2181f1580f4eSBarry Smith PetscCall(MatDuplicate(A01, MAT_COPY_VALUES, &B));
2182db4a47b3SPierre Jolivet PetscCall(MatDiagonalScale(B, v, nullptr));
2183281f8ce6SPierre Jolivet if (B01) PetscCall(MatDiagonalScale(B01, v, nullptr));
218458aab184SPierre Jolivet } else {
2185e5634b20SPierre Jolivet Mat D00;
2186e5634b20SPierre Jolivet MatType type;
2187e5634b20SPierre Jolivet
2188e5634b20SPierre Jolivet PetscCall(MatCreate(PetscObjectComm((PetscObject)A00), &D00));
2189e5634b20SPierre Jolivet PetscCall(MatSetType(D00, MATAIJ));
2190e5634b20SPierre Jolivet PetscCall(MatSetOptionsPrefix(D00, ((PetscObject)A00)->prefix));
2191e5634b20SPierre Jolivet PetscCall(MatAppendOptionsPrefix(D00, "block_diagonal_"));
2192e5634b20SPierre Jolivet PetscCall(MatSetFromOptions(D00)); /* for setting -mat_block_size dynamically */
2193e5634b20SPierre Jolivet PetscCall(MatConvert(A00, MATAIJ, MAT_INITIAL_MATRIX, &B)); /* not all MatTypes have a MatInvertBlockDiagonal() implementation, plus one may want to use a different block size than the one of A00 */
2194e5634b20SPierre Jolivet PetscCall(MatSetBlockSizesFromMats(B, D00, D00));
2195e5634b20SPierre Jolivet PetscCall(MatInvertBlockDiagonalMat(B, D00));
2196e5634b20SPierre Jolivet PetscCall(MatDestroy(&B));
2197e5634b20SPierre Jolivet PetscCall(MatGetType(A01, &type)); /* cache MatType */
2198e5634b20SPierre Jolivet PetscCall(MatConvert(A01, MATAIJ, MAT_INPLACE_MATRIX, &A01)); /* MatProduct is not versatile enough to fallback gracefully if no implementation found, so MatConvert() */
2199e5634b20SPierre Jolivet PetscCall(MatMatMult(D00, A01, MAT_INITIAL_MATRIX, PETSC_CURRENT, &B));
2200e5634b20SPierre Jolivet PetscCall(MatDestroy(&D00));
2201e5634b20SPierre Jolivet PetscCall(MatConvert(A01, type, MAT_INPLACE_MATRIX, &A01)); /* reset to previous MatType */
2202e5634b20SPierre Jolivet PetscCall(MatConvert(B, type, MAT_INPLACE_MATRIX, &B));
2203e5634b20SPierre Jolivet if (!B01) { /* symmetric case */
2204e5634b20SPierre Jolivet B01 = A01;
2205e5634b20SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)B01));
2206e5634b20SPierre Jolivet }
2207e5634b20SPierre Jolivet }
2208e5634b20SPierre Jolivet if (B01 && B01 != A01) PetscCall(MatSetBlockSizesFromMats(B01, A01, A01)); /* TODO: remove this line once Firedrake is fixed */
2209f1580f4eSBarry Smith PetscCall(VecDestroy(&v));
2210f1580f4eSBarry Smith PetscCall(MatCreateNormalHermitian(B, &N));
2211281f8ce6SPierre Jolivet PetscCall(PCHPDDMSetAuxiliaryMatNormal_Private(pc, B, N, &P, pcpre, &diagonal, B01));
2212f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)data->aux, MATSEQAIJ, &flg));
2213f1580f4eSBarry Smith if (!flg) {
2214f1580f4eSBarry Smith PetscCall(MatDestroy(&P));
2215f1580f4eSBarry Smith P = N;
2216f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)P));
2217feebddf4SPierre Jolivet }
22183df4cd7bSPierre Jolivet if (diagonal) {
2219e5634b20SPierre Jolivet PetscCall(MatSetOption(P, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_FALSE)); /* may have missing diagonal entries */
22203df4cd7bSPierre Jolivet PetscCall(MatDiagonalSet(P, diagonal, ADD_VALUES));
2221feebddf4SPierre Jolivet PetscCall(PCSetOperators(pc, P, P)); /* replace P by A01^T inv(diag(P00)) A01 - diag(P11) */
22223df4cd7bSPierre Jolivet PetscCall(VecDestroy(&diagonal));
2223281f8ce6SPierre Jolivet } else PetscCall(PCSetOperators(pc, B01 ? P : N, P)); /* replace P by A01^T inv(diag(P00)) A01 */
2224feebddf4SPierre Jolivet pc->ops->postsolve = PCPostSolve_SchurPreLeastSquares; /* PCFIELDSPLIT expect a KSP for (P11 - A10 inv(diag(P00)) A01) */
2225feebddf4SPierre Jolivet PetscCall(MatDestroy(&N)); /* but a PC for (A10 inv(diag(P00)) A10 - P11) is setup instead */
2226feebddf4SPierre Jolivet PetscCall(MatDestroy(&P)); /* so the sign of the solution must be flipped */
2227f1580f4eSBarry Smith PetscCall(MatDestroy(&B));
222813044ca3SPierre Jolivet } else
2229fd310a01SPierre Jolivet PetscCheck(type != PC_HPDDM_SCHUR_PRE_GENEO, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition %s without a prior call to PCHPDDMSetAuxiliaryMat() on the A11 block%s%s", pcpre ? pcpre : "", PCHPDDMSchurPreTypes[type], pcpre ? " -" : "", pcpre ? pcpre : "");
22300307214fSPierre Jolivet for (std::vector<Vec>::iterator it = initial.begin(); it != initial.end(); ++it) PetscCall(VecDestroy(&*it));
22313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
2232f1580f4eSBarry Smith } else {
22338dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetString(((PetscObject)pc)->options, pcpre, "-pc_hpddm_levels_1_st_pc_type", type, sizeof(type), nullptr));
2234f1580f4eSBarry Smith PetscCall(PetscStrcmp(type, PCMAT, &algebraic));
2235fc31f830SPierre Jolivet PetscCheck(!algebraic || !block, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_levels_1_st_pc_type mat and -%spc_hpddm_block_splitting", pcpre ? pcpre : "", pcpre ? pcpre : "");
22369bb5c669SPierre Jolivet if (overlap != -1) {
2237fc31f830SPierre Jolivet PetscCheck(!block && !algebraic, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_%s and -%spc_hpddm_harmonic_overlap", pcpre ? pcpre : "", block ? "block_splitting" : "levels_1_st_pc_type mat", pcpre ? pcpre : "");
2238fc31f830SPierre Jolivet PetscCheck(overlap >= 1, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_WRONG, "-%spc_hpddm_harmonic_overlap %" PetscInt_FMT " < 1", pcpre ? pcpre : "", overlap);
22399bb5c669SPierre Jolivet }
22409bb5c669SPierre Jolivet if (block || overlap != -1) algebraic = PETSC_TRUE;
2241f1580f4eSBarry Smith if (algebraic) {
2242f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, P->rmap->n, P->rmap->rstart, 1, &data->is));
2243f1580f4eSBarry Smith PetscCall(MatIncreaseOverlap(P, 1, &data->is, 1));
2244f1580f4eSBarry Smith PetscCall(ISSort(data->is));
22459bb5c669SPierre Jolivet } else
22469bb5c669SPierre Jolivet 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 and -%spc_hpddm_harmonic_overlap < 1\n", pcpre ? pcpre : "", pcpre ? pcpre : "", pcpre ? pcpre : ""));
2247f1580f4eSBarry Smith }
2248f1580f4eSBarry Smith }
2249f1580f4eSBarry Smith }
2250f1580f4eSBarry Smith }
2251398c7888SPierre Jolivet #if PetscDefined(USE_DEBUG)
2252398c7888SPierre Jolivet if (data->is) PetscCall(ISDuplicate(data->is, &dis));
2253398c7888SPierre Jolivet if (data->aux) PetscCall(MatDuplicate(data->aux, MAT_COPY_VALUES, &daux));
2254398c7888SPierre Jolivet #endif
2255f1580f4eSBarry Smith if (data->is || (ismatis && data->N > 1)) {
2256f1580f4eSBarry Smith if (ismatis) {
2257f1580f4eSBarry Smith std::initializer_list<std::string> list = {MATSEQBAIJ, MATSEQSBAIJ};
2258f1580f4eSBarry Smith PetscCall(MatISGetLocalMat(P, &N));
2259f1580f4eSBarry Smith std::initializer_list<std::string>::const_iterator it = std::find(list.begin(), list.end(), ((PetscObject)N)->type_name);
2260f1580f4eSBarry Smith PetscCall(MatISRestoreLocalMat(P, &N));
2261f1580f4eSBarry Smith switch (std::distance(list.begin(), it)) {
2262d71ae5a4SJacob Faibussowitsch case 0:
2263d71ae5a4SJacob Faibussowitsch PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &C));
2264d71ae5a4SJacob Faibussowitsch break;
2265f1580f4eSBarry Smith case 1:
2266f1580f4eSBarry Smith /* MatCreateSubMatrices() does not work with MATSBAIJ and unsorted ISes, so convert to MPIBAIJ */
2267f1580f4eSBarry Smith PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &C));
2268f1580f4eSBarry Smith break;
2269d71ae5a4SJacob Faibussowitsch default:
2270d71ae5a4SJacob Faibussowitsch PetscCall(MatConvert(P, MATMPIAIJ, MAT_INITIAL_MATRIX, &C));
2271f1580f4eSBarry Smith }
2272db4a47b3SPierre Jolivet PetscCall(MatISGetLocalToGlobalMapping(P, &l2g, nullptr));
2273f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)P));
2274f1580f4eSBarry Smith PetscCall(KSPSetOperators(data->levels[0]->ksp, A, C));
2275f1580f4eSBarry Smith std::swap(C, P);
2276f1580f4eSBarry Smith PetscCall(ISLocalToGlobalMappingGetSize(l2g, &n));
2277f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, n, 0, 1, &loc));
2278f1580f4eSBarry Smith PetscCall(ISLocalToGlobalMappingApplyIS(l2g, loc, &is[0]));
2279f1580f4eSBarry Smith PetscCall(ISDestroy(&loc));
2280f1580f4eSBarry Smith /* the auxiliary Mat is _not_ the local Neumann matrix */
2281f1580f4eSBarry Smith /* it is the local Neumann matrix augmented (with zeros) through MatIncreaseOverlap() */
2282c8ea6600SPierre Jolivet data->Neumann = PETSC_BOOL3_FALSE;
2283f1580f4eSBarry Smith structure = SAME_NONZERO_PATTERN;
2284f1580f4eSBarry Smith } else {
2285f1580f4eSBarry Smith is[0] = data->is;
228613044ca3SPierre Jolivet if (algebraic || ctx) subdomains = PETSC_TRUE;
22878dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetBool(((PetscObject)pc)->options, pcpre, "-pc_hpddm_define_subdomains", &subdomains, nullptr));
228813044ca3SPierre Jolivet if (ctx) PetscCheck(subdomains, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition geneo and -%spc_hpddm_define_subdomains false", pcpre, pcpre);
2289c8ea6600SPierre Jolivet if (PetscBool3ToBool(data->Neumann)) {
2290fc31f830SPierre Jolivet PetscCheck(!block, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_block_splitting and -%spc_hpddm_has_neumann", pcpre ? pcpre : "", pcpre ? pcpre : "");
2291fc31f830SPierre Jolivet PetscCheck(overlap == -1, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_harmonic_overlap %" PetscInt_FMT " and -%spc_hpddm_has_neumann", pcpre ? pcpre : "", overlap, pcpre ? pcpre : "");
2292fc31f830SPierre Jolivet PetscCheck(!algebraic, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_levels_1_st_pc_type mat and -%spc_hpddm_has_neumann", pcpre ? pcpre : "", pcpre ? pcpre : "");
2293f1580f4eSBarry Smith }
2294c8ea6600SPierre Jolivet if (PetscBool3ToBool(data->Neumann) || block) structure = SAME_NONZERO_PATTERN;
2295f1580f4eSBarry Smith PetscCall(ISCreateStride(PetscObjectComm((PetscObject)data->is), P->rmap->n, P->rmap->rstart, 1, &loc));
2296f1580f4eSBarry Smith }
2297f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_", pcpre ? pcpre : ""));
22988dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetEnum(((PetscObject)pc)->options, prefix, "-st_matstructure", MatStructures, (PetscEnum *)&structure, &flg)); /* if not user-provided, force its value when possible */
2299f1580f4eSBarry Smith if (!flg && structure == SAME_NONZERO_PATTERN) { /* cannot call STSetMatStructure() yet, insert the appropriate option in the database, parsed by STSetFromOptions() */
2300f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-%spc_hpddm_levels_1_st_matstructure", pcpre ? pcpre : ""));
23018dc3fbeeSPierre Jolivet PetscCall(PetscOptionsSetValue(((PetscObject)pc)->options, prefix, MatStructures[structure]));
2302f1580f4eSBarry Smith }
2303398c7888SPierre Jolivet flg = PETSC_FALSE;
2304b07dfdedSPierre Jolivet if (data->share) {
2305398c7888SPierre Jolivet data->share = PETSC_FALSE; /* will be reset to PETSC_TRUE if none of the conditions below are true */
2306398c7888SPierre 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 : ""));
2307398c7888SPierre Jolivet else if (data->deflation) PetscCall(PetscInfo(pc, "Nothing to share since PCHPDDMSetDeflationMat() has been called\n"));
2308398c7888SPierre Jolivet else if (ismatis) PetscCall(PetscInfo(pc, "Cannot share subdomain KSP between SLEPc and PETSc with a Pmat of type MATIS\n"));
2309b07dfdedSPierre Jolivet else if (!algebraic && structure != SAME_NONZERO_PATTERN)
2310398c7888SPierre 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]));
2311398c7888SPierre Jolivet else data->share = PETSC_TRUE;
2312*dc96be45SPierre Jolivet if (!data->share) {
2313*dc96be45SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-%spc_hpddm_levels_1_st_share_sub_ksp", pcpre ? pcpre : ""));
2314*dc96be45SPierre Jolivet PetscCall(PetscOptionsClearValue(((PetscObject)pc)->options, prefix));
2315*dc96be45SPierre Jolivet }
2316398c7888SPierre Jolivet }
2317398c7888SPierre Jolivet if (!ismatis) {
2318398c7888SPierre Jolivet if (data->share || (!PetscBool3ToBool(data->Neumann) && subdomains)) PetscCall(ISDuplicate(is[0], &unsorted));
2319398c7888SPierre Jolivet else unsorted = is[0];
2320398c7888SPierre Jolivet }
232151ea4bc8SPierre Jolivet if ((ctx || data->N > 1) && (data->aux || ismatis || algebraic)) {
2322f1580f4eSBarry Smith PetscCheck(loadedSym, PETSC_COMM_SELF, PETSC_ERR_PLIB, "HPDDM library not loaded, cannot use more than one level");
2323f1580f4eSBarry Smith PetscCall(MatSetOption(P, MAT_SUBMAT_SINGLEIS, PETSC_TRUE));
2324f1580f4eSBarry Smith if (ismatis) {
2325f1580f4eSBarry Smith /* needed by HPDDM (currently) so that the partition of unity is 0 on subdomain interfaces */
2326f1580f4eSBarry Smith PetscCall(MatIncreaseOverlap(P, 1, is, 1));
2327f1580f4eSBarry Smith PetscCall(ISDestroy(&data->is));
2328f1580f4eSBarry Smith data->is = is[0];
2329f1580f4eSBarry Smith } else {
2330d16c0b94SPierre Jolivet if (PetscDefined(USE_DEBUG)) PetscCall(PCHPDDMCheckInclusion_Private(pc, data->is, loc, PETSC_TRUE));
233151ea4bc8SPierre Jolivet if (!ctx && overlap == -1) PetscCall(PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", PCHPDDMAlgebraicAuxiliaryMat_Private));
23329bb5c669SPierre Jolivet if (!PetscBool3ToBool(data->Neumann) && (!algebraic || overlap != -1)) {
2333f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)P, MATMPISBAIJ, &flg));
2334f1580f4eSBarry Smith if (flg) {
2335f1580f4eSBarry Smith /* maybe better to ISSort(is[0]), MatCreateSubMatrices(), and then MatPermute() */
2336f1580f4eSBarry Smith /* but there is no MatPermute_SeqSBAIJ(), so as before, just use MATMPIBAIJ */
2337f1580f4eSBarry Smith PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &uaux));
2338f1580f4eSBarry Smith flg = PETSC_FALSE;
2339f1580f4eSBarry Smith }
2340f1580f4eSBarry Smith }
2341f1580f4eSBarry Smith }
23429bb5c669SPierre Jolivet if (algebraic && overlap == -1) {
2343f1580f4eSBarry Smith PetscUseMethod(pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", (Mat, IS *, Mat *[], PetscBool), (P, is, &sub, block));
2344f1580f4eSBarry Smith if (block) {
2345f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)sub[0], "_PCHPDDM_Neumann_Mat", (PetscObject *)&data->aux));
2346db4a47b3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Neumann_Mat", nullptr));
2347f1580f4eSBarry Smith }
23489bb5c669SPierre Jolivet } else if (!uaux || overlap != -1) {
234913044ca3SPierre Jolivet if (!ctx) {
2350c8ea6600SPierre Jolivet if (PetscBool3ToBool(data->Neumann)) sub = &data->aux;
23519bb5c669SPierre Jolivet else {
2352df21ef68SPierre Jolivet PetscBool flg;
23539bb5c669SPierre Jolivet if (overlap != -1) {
23549bb5c669SPierre Jolivet Harmonic h;
23559bb5c669SPierre Jolivet Mat A0, *a; /* with an SVD: [ A_00 A_01 ] */
23569bb5c669SPierre Jolivet IS ov[2], rows, cols, stride; /* [ A_10 A_11 A_12 ] */
235758b7e2c1SStefano Zampini const PetscInt *i[2], bs = P->cmap->bs; /* with a GEVP: [ A_00 A_01 ] */
235854bee67dSPierre Jolivet PetscInt n[2], location; /* [ A_10 A_11 A_12 ] */
23599bb5c669SPierre Jolivet std::vector<PetscInt> v[2]; /* [ A_21 A_22 ] */
23609bb5c669SPierre Jolivet
23612589ceddSPierre Jolivet do {
23629bb5c669SPierre Jolivet PetscCall(ISDuplicate(data->is, ov));
23639bb5c669SPierre Jolivet if (overlap > 1) PetscCall(MatIncreaseOverlap(P, 1, ov, overlap - 1));
23649bb5c669SPierre Jolivet PetscCall(ISDuplicate(ov[0], ov + 1));
23659bb5c669SPierre Jolivet PetscCall(MatIncreaseOverlap(P, 1, ov + 1, 1));
23662589ceddSPierre Jolivet PetscCall(ISGetLocalSize(ov[0], n));
23672589ceddSPierre Jolivet PetscCall(ISGetLocalSize(ov[1], n + 1));
23688fad524dSPierre Jolivet flg = PetscBool(n[0] == n[1] && n[0] != P->rmap->n);
23695440e5dcSBarry Smith PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &flg, 1, MPI_C_BOOL, MPI_LOR, PetscObjectComm((PetscObject)pc)));
23702589ceddSPierre Jolivet if (flg) {
23712589ceddSPierre Jolivet PetscCall(ISDestroy(ov));
23722589ceddSPierre Jolivet PetscCall(ISDestroy(ov + 1));
23732589ceddSPierre Jolivet PetscCheck(--overlap, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "No oversampling possible");
23742589ceddSPierre Jolivet PetscCall(PetscInfo(pc, "Supplied -%spc_hpddm_harmonic_overlap parameter is too large, it has been decreased to %" PetscInt_FMT "\n", pcpre ? pcpre : "", overlap));
23752589ceddSPierre Jolivet } else break;
23762589ceddSPierre Jolivet } while (1);
23779bb5c669SPierre Jolivet PetscCall(PetscNew(&h));
23789bb5c669SPierre Jolivet h->ksp = nullptr;
23799bb5c669SPierre Jolivet PetscCall(PetscCalloc1(2, &h->A));
23808dc3fbeeSPierre Jolivet PetscCall(PetscOptionsHasName(((PetscObject)pc)->options, prefix, "-eps_nev", &flg));
2381b5a302b3SPierre Jolivet if (!flg) {
23828dc3fbeeSPierre Jolivet PetscCall(PetscOptionsHasName(((PetscObject)pc)->options, prefix, "-svd_nsv", &flg));
23838dc3fbeeSPierre Jolivet if (!flg) PetscCall(PetscOptionsHasName(((PetscObject)pc)->options, prefix, "-svd_threshold_relative", &flg));
2384b5a302b3SPierre Jolivet } else flg = PETSC_FALSE;
23859bb5c669SPierre Jolivet PetscCall(ISSort(ov[0]));
23869bb5c669SPierre Jolivet if (!flg) PetscCall(ISSort(ov[1]));
238732603206SJames Wright PetscCall(PetscCalloc1(5, &h->is));
23889bb5c669SPierre Jolivet PetscCall(MatCreateSubMatrices(uaux ? uaux : P, 1, ov + !flg, ov + 1, MAT_INITIAL_MATRIX, &a)); /* submatrix from above, either square (!flg) or rectangular (flg) */
23892589ceddSPierre Jolivet for (PetscInt j = 0; j < 2; ++j) PetscCall(ISGetIndices(ov[j], i + j));
23909bb5c669SPierre Jolivet v[1].reserve((n[1] - n[0]) / bs);
23919bb5c669SPierre Jolivet for (PetscInt j = 0; j < n[1]; j += bs) { /* indices of the (2,2) block */
23929bb5c669SPierre Jolivet PetscCall(ISLocate(ov[0], i[1][j], &location));
23939bb5c669SPierre Jolivet if (location < 0) v[1].emplace_back(j / bs);
23949bb5c669SPierre Jolivet }
23959bb5c669SPierre Jolivet if (!flg) {
23969bb5c669SPierre Jolivet h->A[1] = a[0];
23979bb5c669SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)h->A[1]));
23989bb5c669SPierre Jolivet v[0].reserve((n[0] - P->rmap->n) / bs);
23999bb5c669SPierre Jolivet for (PetscInt j = 0; j < n[1]; j += bs) { /* row indices of the (1,2) block */
24009bb5c669SPierre Jolivet PetscCall(ISLocate(loc, i[1][j], &location));
24019bb5c669SPierre Jolivet if (location < 0) {
24029bb5c669SPierre Jolivet PetscCall(ISLocate(ov[0], i[1][j], &location));
24039bb5c669SPierre Jolivet if (location >= 0) v[0].emplace_back(j / bs);
24049bb5c669SPierre Jolivet }
24059bb5c669SPierre Jolivet }
24069bb5c669SPierre Jolivet PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, v[0].size(), v[0].data(), PETSC_USE_POINTER, &rows));
24079bb5c669SPierre Jolivet PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, v[1].size(), v[1].data(), PETSC_COPY_VALUES, h->is + 4));
24089bb5c669SPierre Jolivet PetscCall(MatCreateSubMatrix(a[0], rows, h->is[4], MAT_INITIAL_MATRIX, h->A)); /* A_12 submatrix from above */
24099bb5c669SPierre Jolivet PetscCall(ISDestroy(&rows));
24109bb5c669SPierre Jolivet if (uaux) PetscCall(MatConvert(a[0], MATSEQSBAIJ, MAT_INPLACE_MATRIX, a)); /* initial Pmat was MATSBAIJ, convert back to the same format since the rectangular A_12 submatrix has been created */
24119bb5c669SPierre Jolivet PetscCall(ISEmbed(ov[0], ov[1], PETSC_TRUE, &rows));
24129bb5c669SPierre Jolivet PetscCall(MatCreateSubMatrix(a[0], rows, cols = rows, MAT_INITIAL_MATRIX, &A0)); /* [ A_00 A_01 ; A_10 A_11 ] submatrix from above */
24139bb5c669SPierre Jolivet PetscCall(ISDestroy(&rows));
24149bb5c669SPierre Jolivet v[0].clear();
24159bb5c669SPierre Jolivet PetscCall(ISEmbed(loc, ov[1], PETSC_TRUE, h->is + 3));
24169bb5c669SPierre Jolivet PetscCall(ISEmbed(data->is, ov[1], PETSC_TRUE, h->is + 2));
24179bb5c669SPierre Jolivet }
24189bb5c669SPierre Jolivet v[0].reserve((n[0] - P->rmap->n) / bs);
24199bb5c669SPierre Jolivet for (PetscInt j = 0; j < n[0]; j += bs) {
24209bb5c669SPierre Jolivet PetscCall(ISLocate(loc, i[0][j], &location));
24219bb5c669SPierre Jolivet if (location < 0) v[0].emplace_back(j / bs);
24229bb5c669SPierre Jolivet }
24239bb5c669SPierre Jolivet PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, v[0].size(), v[0].data(), PETSC_USE_POINTER, &rows));
24249bb5c669SPierre Jolivet for (PetscInt j = 0; j < 2; ++j) PetscCall(ISRestoreIndices(ov[j], i + j));
24259bb5c669SPierre Jolivet if (flg) {
242601962aebSPierre Jolivet PetscCall(ISCreateStride(PETSC_COMM_SELF, a[0]->rmap->n, 0, 1, &stride));
24279bb5c669SPierre Jolivet PetscCall(ISEmbed(ov[0], ov[1], PETSC_TRUE, &cols));
242801962aebSPierre Jolivet PetscCall(MatCreateSubMatrix(a[0], stride, cols, MAT_INITIAL_MATRIX, &A0)); /* [ A_00 A_01 ; A_10 A_11 ] submatrix from above */
24299bb5c669SPierre Jolivet PetscCall(ISDestroy(&cols));
243001962aebSPierre Jolivet PetscCall(ISDestroy(&stride));
2431b0c98d1dSPierre Jolivet if (uaux) { /* initial Pmat was MATSBAIJ, convert back to the same format since this submatrix is square */
2432b0c98d1dSPierre Jolivet PetscCall(MatSetOption(A0, MAT_SYMMETRIC, PETSC_TRUE));
2433b0c98d1dSPierre Jolivet PetscCall(MatConvert(A0, MATSEQSBAIJ, MAT_INPLACE_MATRIX, &A0));
2434b0c98d1dSPierre Jolivet }
24359bb5c669SPierre Jolivet PetscCall(ISEmbed(loc, data->is, PETSC_TRUE, h->is + 2));
24369bb5c669SPierre Jolivet PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, v[1].size(), v[1].data(), PETSC_USE_POINTER, &cols));
24379bb5c669SPierre Jolivet PetscCall(MatCreateSubMatrix(a[0], rows, cols, MAT_INITIAL_MATRIX, h->A)); /* A_12 submatrix from above */
24389bb5c669SPierre Jolivet PetscCall(ISDestroy(&cols));
24399bb5c669SPierre Jolivet }
24409bb5c669SPierre Jolivet PetscCall(ISCreateStride(PETSC_COMM_SELF, A0->rmap->n, 0, 1, &stride));
24419bb5c669SPierre Jolivet PetscCall(ISEmbed(rows, stride, PETSC_TRUE, h->is));
24429bb5c669SPierre Jolivet PetscCall(ISDestroy(&stride));
24439bb5c669SPierre Jolivet PetscCall(ISDestroy(&rows));
24449bb5c669SPierre Jolivet PetscCall(ISEmbed(loc, ov[0], PETSC_TRUE, h->is + 1));
24459bb5c669SPierre Jolivet if (subdomains) {
24469bb5c669SPierre Jolivet if (!data->levels[0]->pc) {
24479bb5c669SPierre Jolivet PetscCall(PCCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->pc));
24489bb5c669SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_", pcpre ? pcpre : ""));
24499bb5c669SPierre Jolivet PetscCall(PCSetOptionsPrefix(data->levels[0]->pc, prefix));
24509bb5c669SPierre Jolivet PetscCall(PCSetOperators(data->levels[0]->pc, A, P));
24519bb5c669SPierre Jolivet }
24529bb5c669SPierre Jolivet PetscCall(PCSetType(data->levels[0]->pc, PCASM));
24539bb5c669SPierre Jolivet if (!data->levels[0]->pc->setupcalled) PetscCall(PCASMSetLocalSubdomains(data->levels[0]->pc, 1, ov + !flg, &loc));
2454b89bd804SBoris Martin PetscCall(PCSetModifySubMatrices(data->levels[0]->pc, pc->modifysubmatrices, pc->modifysubmatricesP));
24559bb5c669SPierre Jolivet PetscCall(PCHPDDMCommunicationAvoidingPCASM_Private(data->levels[0]->pc, flg ? A0 : a[0], PETSC_TRUE));
24569bb5c669SPierre Jolivet if (!flg) ++overlap;
24579bb5c669SPierre Jolivet if (data->share) {
24589bb5c669SPierre Jolivet PetscInt n = -1;
24599bb5c669SPierre Jolivet PetscTryMethod(data->levels[0]->pc, "PCASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (data->levels[0]->pc, &n, nullptr, &ksp));
24609bb5c669SPierre Jolivet PetscCheck(n == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of subdomain solver %" PetscInt_FMT " != 1", n);
24619bb5c669SPierre Jolivet if (flg) {
24629bb5c669SPierre Jolivet h->ksp = ksp[0];
24639bb5c669SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)h->ksp));
24649bb5c669SPierre Jolivet }
24659bb5c669SPierre Jolivet }
24669bb5c669SPierre Jolivet }
24679bb5c669SPierre Jolivet if (!h->ksp) {
24689bb5c669SPierre Jolivet PetscBool share = data->share;
24698dc3fbeeSPierre Jolivet
24709bb5c669SPierre Jolivet PetscCall(KSPCreate(PETSC_COMM_SELF, &h->ksp));
24719bb5c669SPierre Jolivet PetscCall(KSPSetType(h->ksp, KSPPREONLY));
24729bb5c669SPierre Jolivet PetscCall(KSPSetOperators(h->ksp, A0, A0));
24739bb5c669SPierre Jolivet do {
24749bb5c669SPierre Jolivet if (!data->share) {
24759bb5c669SPierre Jolivet share = PETSC_FALSE;
24769bb5c669SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_%s", pcpre ? pcpre : "", flg ? "svd_" : "eps_"));
24779bb5c669SPierre Jolivet PetscCall(KSPSetOptionsPrefix(h->ksp, prefix));
24789bb5c669SPierre Jolivet PetscCall(KSPSetFromOptions(h->ksp));
24799bb5c669SPierre Jolivet } else {
24809bb5c669SPierre Jolivet MatSolverType type;
24818dc3fbeeSPierre Jolivet
24828dc3fbeeSPierre Jolivet PetscCall(PetscObjectTypeCompareAny((PetscObject)ksp[0]->pc, &data->share, PCLU, PCCHOLESKY, ""));
24839bb5c669SPierre Jolivet if (data->share) {
24848dc3fbeeSPierre Jolivet PetscCall(PCFactorGetMatSolverType(ksp[0]->pc, &type));
24859bb5c669SPierre Jolivet if (!type) {
24868dc3fbeeSPierre Jolivet if (PetscDefined(HAVE_MUMPS)) PetscCall(PCFactorSetMatSolverType(ksp[0]->pc, MATSOLVERMUMPS));
24878dc3fbeeSPierre Jolivet else if (PetscDefined(HAVE_MKL_PARDISO)) PetscCall(PCFactorSetMatSolverType(ksp[0]->pc, MATSOLVERMKL_PARDISO));
24889bb5c669SPierre Jolivet else data->share = PETSC_FALSE;
24898dc3fbeeSPierre Jolivet if (data->share) PetscCall(PCSetFromOptions(ksp[0]->pc));
24909bb5c669SPierre Jolivet } else {
24919bb5c669SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &data->share));
24929bb5c669SPierre Jolivet if (!data->share) PetscCall(PetscStrcmp(type, MATSOLVERMKL_PARDISO, &data->share));
24939bb5c669SPierre Jolivet }
24949bb5c669SPierre Jolivet if (data->share) {
24959bb5c669SPierre Jolivet std::tuple<KSP, IS, Vec[2]> *p;
24968dc3fbeeSPierre Jolivet
24978dc3fbeeSPierre Jolivet PetscCall(PCFactorGetMatrix(ksp[0]->pc, &A));
24989bb5c669SPierre Jolivet PetscCall(MatFactorSetSchurIS(A, h->is[4]));
24999bb5c669SPierre Jolivet PetscCall(KSPSetUp(ksp[0]));
25009bb5c669SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_eps_shell_", pcpre ? pcpre : ""));
25019bb5c669SPierre Jolivet PetscCall(KSPSetOptionsPrefix(h->ksp, prefix));
25029bb5c669SPierre Jolivet PetscCall(KSPSetFromOptions(h->ksp));
25038dc3fbeeSPierre Jolivet PetscCall(PCSetType(h->ksp->pc, PCSHELL));
25049bb5c669SPierre Jolivet PetscCall(PetscNew(&p));
25059bb5c669SPierre Jolivet std::get<0>(*p) = ksp[0];
25069bb5c669SPierre Jolivet PetscCall(ISEmbed(ov[0], ov[1], PETSC_TRUE, &std::get<1>(*p)));
25079bb5c669SPierre Jolivet PetscCall(MatCreateVecs(A, std::get<2>(*p), std::get<2>(*p) + 1));
25088dc3fbeeSPierre Jolivet PetscCall(PCShellSetContext(h->ksp->pc, p));
25098dc3fbeeSPierre Jolivet PetscCall(PCShellSetApply(h->ksp->pc, PCApply_Schur));
25108dc3fbeeSPierre Jolivet PetscCall(PCShellSetApplyTranspose(h->ksp->pc, PCApply_Schur<Vec, true>));
25118dc3fbeeSPierre Jolivet PetscCall(PCShellSetMatApply(h->ksp->pc, PCApply_Schur<Mat>));
25128dc3fbeeSPierre Jolivet PetscCall(PCShellSetDestroy(h->ksp->pc, PCDestroy_Schur));
25139bb5c669SPierre Jolivet }
25149bb5c669SPierre Jolivet }
25159bb5c669SPierre Jolivet if (!data->share) PetscCall(PetscInfo(pc, "Cannot share subdomain KSP between SLEPc and PETSc since neither MUMPS nor MKL PARDISO is used\n"));
25169bb5c669SPierre Jolivet }
25179bb5c669SPierre Jolivet } while (!share != !data->share); /* if data->share is initially PETSC_TRUE, but then reset to PETSC_FALSE, then go back to the beginning of the do loop */
25189bb5c669SPierre Jolivet }
25199bb5c669SPierre Jolivet PetscCall(ISDestroy(ov));
25209bb5c669SPierre Jolivet PetscCall(ISDestroy(ov + 1));
25219bb5c669SPierre Jolivet if (overlap == 1 && subdomains && flg) {
25229bb5c669SPierre Jolivet *subA = A0;
25239bb5c669SPierre Jolivet sub = subA;
25249bb5c669SPierre Jolivet if (uaux) PetscCall(MatDestroy(&uaux));
25259bb5c669SPierre Jolivet } else PetscCall(MatDestroy(&A0));
25269bb5c669SPierre Jolivet PetscCall(MatCreateShell(PETSC_COMM_SELF, P->rmap->n, n[1] - n[0], P->rmap->n, n[1] - n[0], h, &data->aux));
2527bdcd51b8SPierre Jolivet PetscCall(KSPSetErrorIfNotConverged(h->ksp, PETSC_TRUE)); /* bail out as early as possible to avoid (apparently) unrelated error messages */
25289bb5c669SPierre Jolivet PetscCall(MatCreateVecs(h->ksp->pc->pmat, &h->v, nullptr));
252957d50842SBarry Smith PetscCall(MatShellSetOperation(data->aux, MATOP_MULT, (PetscErrorCodeFn *)MatMult_Harmonic));
253057d50842SBarry Smith PetscCall(MatShellSetOperation(data->aux, MATOP_MULT_TRANSPOSE, (PetscErrorCodeFn *)MatMultTranspose_Harmonic));
25319bb5c669SPierre Jolivet PetscCall(MatShellSetMatProductOperation(data->aux, MATPRODUCT_AB, nullptr, MatProduct_AB_Harmonic, nullptr, MATDENSE, MATDENSE));
2532f21e3f8aSPierre Jolivet PetscCall(MatShellSetMatProductOperation(data->aux, MATPRODUCT_AtB, nullptr, MatProduct_AtB_Harmonic, nullptr, MATDENSE, MATDENSE));
253357d50842SBarry Smith PetscCall(MatShellSetOperation(data->aux, MATOP_DESTROY, (PetscErrorCodeFn *)MatDestroy_Harmonic));
25349bb5c669SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &a));
25359bb5c669SPierre Jolivet }
2536df21ef68SPierre Jolivet if (overlap != 1 || !subdomains) {
2537df21ef68SPierre Jolivet PetscCall(MatCreateSubMatrices(uaux ? uaux : P, 1, is, is, MAT_INITIAL_MATRIX, &sub));
2538df21ef68SPierre Jolivet if (ismatis) {
2539df21ef68SPierre Jolivet PetscCall(MatISGetLocalMat(C, &N));
2540df21ef68SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)N, MATSEQSBAIJ, &flg));
2541df21ef68SPierre Jolivet if (flg) PetscCall(MatConvert(sub[0], MATSEQSBAIJ, MAT_INPLACE_MATRIX, sub));
2542df21ef68SPierre Jolivet PetscCall(MatISRestoreLocalMat(C, &N));
2543df21ef68SPierre Jolivet }
2544df21ef68SPierre Jolivet }
25459bb5c669SPierre Jolivet if (uaux) {
25469bb5c669SPierre Jolivet PetscCall(MatDestroy(&uaux));
25479bb5c669SPierre Jolivet PetscCall(MatConvert(sub[0], MATSEQSBAIJ, MAT_INPLACE_MATRIX, sub));
25489bb5c669SPierre Jolivet }
25499bb5c669SPierre Jolivet }
255013044ca3SPierre Jolivet }
255151ea4bc8SPierre Jolivet } else if (!ctx) {
2552f1580f4eSBarry Smith PetscCall(MatCreateSubMatrices(uaux, 1, is, is, MAT_INITIAL_MATRIX, &sub));
2553f1580f4eSBarry Smith PetscCall(MatDestroy(&uaux));
2554f1580f4eSBarry Smith PetscCall(MatConvert(sub[0], MATSEQSBAIJ, MAT_INPLACE_MATRIX, sub));
2555f1580f4eSBarry Smith }
255651ea4bc8SPierre Jolivet if (data->N > 1) {
2557f1580f4eSBarry Smith /* Vec holding the partition of unity */
2558f1580f4eSBarry Smith if (!data->levels[0]->D) {
2559f1580f4eSBarry Smith PetscCall(ISGetLocalSize(data->is, &n));
2560f1580f4eSBarry Smith PetscCall(VecCreateMPI(PETSC_COMM_SELF, n, PETSC_DETERMINE, &data->levels[0]->D));
2561f1580f4eSBarry Smith }
25629bb5c669SPierre Jolivet if (data->share && overlap == -1) {
2563f1580f4eSBarry Smith Mat D;
2564db4a47b3SPierre Jolivet IS perm = nullptr;
2565f1580f4eSBarry Smith PetscInt size = -1;
2566811e8887SPierre Jolivet
2567398c7888SPierre Jolivet if (!data->levels[0]->pc) {
2568398c7888SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_", pcpre ? pcpre : ""));
2569398c7888SPierre Jolivet PetscCall(PCCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->pc));
2570398c7888SPierre Jolivet PetscCall(PCSetOptionsPrefix(data->levels[0]->pc, prefix));
2571398c7888SPierre Jolivet PetscCall(PCSetOperators(data->levels[0]->pc, A, P));
2572398c7888SPierre Jolivet }
2573398c7888SPierre Jolivet PetscCall(PCSetType(data->levels[0]->pc, PCASM));
257413044ca3SPierre Jolivet if (!ctx) {
2575398c7888SPierre Jolivet if (!data->levels[0]->pc->setupcalled) {
2576398c7888SPierre Jolivet IS sorted; /* PCASM will sort the input IS, duplicate it to return an unmodified (PCHPDDM) input IS */
2577398c7888SPierre Jolivet PetscCall(ISDuplicate(is[0], &sorted));
2578398c7888SPierre Jolivet PetscCall(PCASMSetLocalSubdomains(data->levels[0]->pc, 1, &sorted, &loc));
2579398c7888SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)sorted));
2580398c7888SPierre Jolivet }
2581398c7888SPierre Jolivet PetscCall(PCSetFromOptions(data->levels[0]->pc));
2582b89bd804SBoris Martin PetscCall(PCSetModifySubMatrices(data->levels[0]->pc, pc->modifysubmatrices, pc->modifysubmatricesP));
2583398c7888SPierre Jolivet if (block) {
2584398c7888SPierre Jolivet PetscCall(PCHPDDMPermute_Private(unsorted, data->is, &uis, sub[0], &C, &perm));
2585398c7888SPierre Jolivet PetscCall(PCHPDDMCommunicationAvoidingPCASM_Private(data->levels[0]->pc, C, algebraic));
2586398c7888SPierre Jolivet } else PetscCall(PCSetUp(data->levels[0]->pc));
2587db4a47b3SPierre Jolivet PetscTryMethod(data->levels[0]->pc, "PCASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (data->levels[0]->pc, &size, nullptr, &ksp));
2588398c7888SPierre Jolivet if (size != 1) {
2589398c7888SPierre Jolivet data->share = PETSC_FALSE;
2590398c7888SPierre Jolivet PetscCheck(size == -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of subdomain solver %" PetscInt_FMT " != 1", size);
2591398c7888SPierre Jolivet PetscCall(PetscInfo(pc, "Cannot share subdomain KSP between SLEPc and PETSc since PCASMGetSubKSP() not found in fine-level PC\n"));
2592398c7888SPierre Jolivet PetscCall(ISDestroy(&unsorted));
2593398c7888SPierre Jolivet unsorted = is[0];
2594398c7888SPierre Jolivet } else {
2595811e8887SPierre Jolivet const char *matpre;
2596811e8887SPierre Jolivet PetscBool cmp[4];
2597811e8887SPierre Jolivet
259813044ca3SPierre Jolivet if (!block && !ctx) PetscCall(PCHPDDMPermute_Private(unsorted, data->is, &uis, PetscBool3ToBool(data->Neumann) ? sub[0] : data->aux, &C, &perm));
259951ea4bc8SPierre Jolivet if (perm) { /* unsorted input IS */
2600c8ea6600SPierre Jolivet if (!PetscBool3ToBool(data->Neumann) && !block) {
2601f1580f4eSBarry Smith PetscCall(MatPermute(sub[0], perm, perm, &D)); /* permute since PCASM will call ISSort() */
2602f1580f4eSBarry Smith PetscCall(MatHeaderReplace(sub[0], &D));
2603f1580f4eSBarry Smith }
2604f1580f4eSBarry Smith if (data->B) { /* see PCHPDDMSetRHSMat() */
2605f1580f4eSBarry Smith PetscCall(MatPermute(data->B, perm, perm, &D));
2606f1580f4eSBarry Smith PetscCall(MatHeaderReplace(data->B, &D));
2607f1580f4eSBarry Smith }
2608f1580f4eSBarry Smith PetscCall(ISDestroy(&perm));
260951ea4bc8SPierre Jolivet }
2610f1580f4eSBarry Smith PetscCall(KSPGetOperators(ksp[0], subA, subA + 1));
26110307214fSPierre Jolivet PetscCall(PetscObjectReference((PetscObject)subA[0]));
2612f1580f4eSBarry Smith PetscCall(MatDuplicate(subA[1], MAT_SHARE_NONZERO_PATTERN, &D));
2613f1580f4eSBarry Smith PetscCall(MatGetOptionsPrefix(subA[1], &matpre));
2614f1580f4eSBarry Smith PetscCall(MatSetOptionsPrefix(D, matpre));
2615f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)D, MATNORMAL, cmp));
2616f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)C, MATNORMAL, cmp + 1));
2617f1580f4eSBarry Smith if (!cmp[0]) PetscCall(PetscObjectTypeCompare((PetscObject)D, MATNORMALHERMITIAN, cmp + 2));
2618f1580f4eSBarry Smith else cmp[2] = PETSC_FALSE;
2619f1580f4eSBarry Smith if (!cmp[1]) PetscCall(PetscObjectTypeCompare((PetscObject)C, MATNORMALHERMITIAN, cmp + 3));
2620f1580f4eSBarry Smith else cmp[3] = PETSC_FALSE;
2621f1580f4eSBarry 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);
2622f1580f4eSBarry Smith if (!cmp[0] && !cmp[2]) {
26238a8e6071SPierre Jolivet if (!block) {
26248a8e6071SPierre Jolivet if (PetscDefined(USE_DEBUG)) PetscCall(PCHPDDMCheckMatStructure_Private(pc, D, C));
26258a8e6071SPierre Jolivet PetscCall(MatAXPY(D, 1.0, C, SUBSET_NONZERO_PATTERN));
26268a8e6071SPierre Jolivet } else {
2627421480d9SBarry Smith structure = DIFFERENT_NONZERO_PATTERN;
26285c7345deSPierre Jolivet PetscCall(MatAXPY(D, 1.0, data->aux, structure));
26295c7345deSPierre Jolivet }
2630f1580f4eSBarry Smith } else {
2631f1580f4eSBarry Smith Mat mat[2];
2632811e8887SPierre Jolivet
2633f1580f4eSBarry Smith if (cmp[0]) {
2634f1580f4eSBarry Smith PetscCall(MatNormalGetMat(D, mat));
2635f1580f4eSBarry Smith PetscCall(MatNormalGetMat(C, mat + 1));
2636f1580f4eSBarry Smith } else {
2637f1580f4eSBarry Smith PetscCall(MatNormalHermitianGetMat(D, mat));
2638f1580f4eSBarry Smith PetscCall(MatNormalHermitianGetMat(C, mat + 1));
2639f1580f4eSBarry Smith }
2640f1580f4eSBarry Smith PetscCall(MatAXPY(mat[0], 1.0, mat[1], SUBSET_NONZERO_PATTERN));
2641f1580f4eSBarry Smith }
2642f1580f4eSBarry Smith PetscCall(MatPropagateSymmetryOptions(C, D));
2643f1580f4eSBarry Smith PetscCall(MatDestroy(&C));
2644f1580f4eSBarry Smith C = D;
2645f1580f4eSBarry Smith /* swap pointers so that variables stay consistent throughout PCSetUp() */
2646f1580f4eSBarry Smith std::swap(C, data->aux);
2647f1580f4eSBarry Smith std::swap(uis, data->is);
2648f1580f4eSBarry Smith swap = PETSC_TRUE;
2649f1580f4eSBarry Smith }
2650f1580f4eSBarry Smith }
265113044ca3SPierre Jolivet }
265251ea4bc8SPierre Jolivet }
265313044ca3SPierre Jolivet if (ctx) {
265413044ca3SPierre Jolivet PC_HPDDM *data_00 = (PC_HPDDM *)std::get<0>(*ctx)[0]->data;
265513044ca3SPierre Jolivet PC s;
265600b491fbSPierre Jolivet Mat A00, P00, A01 = nullptr, A10, A11, N, b[4];
265707d8d47fSPierre Jolivet IS sorted, is[2], *is_00;
265813044ca3SPierre Jolivet MatSolverType type;
265913044ca3SPierre Jolivet std::pair<PC, Vec[2]> *p;
266013044ca3SPierre Jolivet
266107d8d47fSPierre Jolivet n = -1;
266207d8d47fSPierre Jolivet PetscTryMethod(data_00->levels[0]->pc, "PCASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (data_00->levels[0]->pc, &n, nullptr, &ksp));
266307d8d47fSPierre Jolivet PetscCheck(n == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of subdomain solver %" PetscInt_FMT " != 1", n);
266407d8d47fSPierre Jolivet PetscCall(KSPGetOperators(ksp[0], subA, subA + 1));
266507d8d47fSPierre Jolivet PetscCall(ISGetLocalSize(data_00->is, &n));
266607d8d47fSPierre Jolivet if (n != subA[0]->rmap->n || n != subA[0]->cmap->n) {
266707d8d47fSPierre Jolivet PetscCall(PCASMGetLocalSubdomains(data_00->levels[0]->pc, &n, &is_00, nullptr));
266807d8d47fSPierre Jolivet PetscCall(ISGetLocalSize(*is_00, &n));
266907d8d47fSPierre Jolivet PetscCheck(n == subA[0]->rmap->n && n == subA[0]->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition geneo and -%spc_hpddm_define_subdomains false", pcpre ? pcpre : "", ((PetscObject)pc)->prefix);
267007d8d47fSPierre Jolivet } else is_00 = &data_00->is;
267113044ca3SPierre Jolivet PetscCall(PCHPDDMPermute_Private(unsorted, data->is, &uis, data->aux, &C, nullptr)); /* permute since PCASM works with a sorted IS */
267213044ca3SPierre Jolivet std::swap(C, data->aux);
267313044ca3SPierre Jolivet std::swap(uis, data->is);
267413044ca3SPierre Jolivet swap = PETSC_TRUE;
267513044ca3SPierre Jolivet PetscCall(MatSchurComplementGetSubMatrices(P, &A00, &P00, std::get<1>(*ctx), &A10, &A11));
267613044ca3SPierre Jolivet std::get<1>(*ctx)[1] = A10;
267713044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATTRANSPOSEVIRTUAL, &flg));
267813044ca3SPierre Jolivet if (flg) PetscCall(MatTransposeGetMat(A10, &A01));
267913044ca3SPierre Jolivet else {
268013044ca3SPierre Jolivet PetscBool flg;
268113044ca3SPierre Jolivet
268213044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATHERMITIANTRANSPOSEVIRTUAL, &flg));
268313044ca3SPierre Jolivet if (flg) PetscCall(MatHermitianTransposeGetMat(A10, &A01));
268413044ca3SPierre Jolivet }
268507d8d47fSPierre Jolivet PetscCall(ISDuplicate(*is_00, &sorted)); /* during setup of the PC associated to the A00 block, this IS has already been sorted, but it's put back to its original state at the end of PCSetUp_HPDDM(), which may be unsorted */
268613044ca3SPierre Jolivet PetscCall(ISSort(sorted)); /* this is to avoid changing users inputs, but it requires a new call to ISSort() here */
268700b491fbSPierre Jolivet if (!A01) {
268800b491fbSPierre Jolivet PetscCall(MatSetOption(A10, MAT_SUBMAT_SINGLEIS, PETSC_TRUE));
268900b491fbSPierre Jolivet PetscCall(MatCreateSubMatrices(A10, 1, &data->is, &sorted, MAT_INITIAL_MATRIX, &sub));
269000b491fbSPierre Jolivet b[2] = sub[0];
269100b491fbSPierre Jolivet PetscCall(PetscObjectReference((PetscObject)sub[0]));
269200b491fbSPierre Jolivet PetscCall(MatDestroySubMatrices(1, &sub));
269300b491fbSPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)std::get<1>(*ctx)[0], MATTRANSPOSEVIRTUAL, &flg));
269400b491fbSPierre Jolivet A10 = nullptr;
269500b491fbSPierre Jolivet if (flg) PetscCall(MatTransposeGetMat(std::get<1>(*ctx)[0], &A10));
269613044ca3SPierre Jolivet else {
269700b491fbSPierre Jolivet PetscBool flg;
269800b491fbSPierre Jolivet
2699c71f06a7SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)std::get<1>(*ctx)[0], MATHERMITIANTRANSPOSEVIRTUAL, &flg));
270000b491fbSPierre Jolivet if (flg) PetscCall(MatHermitianTransposeGetMat(std::get<1>(*ctx)[0], &A10));
270113044ca3SPierre Jolivet }
2702811e8887SPierre Jolivet if (!A10) PetscCall(MatCreateSubMatrices(std::get<1>(*ctx)[0], 1, &sorted, &data->is, MAT_INITIAL_MATRIX, &sub));
2703811e8887SPierre Jolivet else {
2704811e8887SPierre Jolivet if (flg) PetscCall(MatCreateTranspose(b[2], b + 1));
2705811e8887SPierre Jolivet else PetscCall(MatCreateHermitianTranspose(b[2], b + 1));
270600b491fbSPierre Jolivet }
270700b491fbSPierre Jolivet } else {
270800b491fbSPierre Jolivet PetscCall(MatSetOption(A01, MAT_SUBMAT_SINGLEIS, PETSC_TRUE));
270900b491fbSPierre Jolivet PetscCall(MatCreateSubMatrices(A01, 1, &sorted, &data->is, MAT_INITIAL_MATRIX, &sub));
2710811e8887SPierre Jolivet if (flg) PetscCall(MatCreateTranspose(*sub, b + 2));
2711811e8887SPierre Jolivet else PetscCall(MatCreateHermitianTranspose(*sub, b + 2));
2712811e8887SPierre Jolivet }
2713811e8887SPierre Jolivet if (A01 || !A10) {
2714811e8887SPierre Jolivet b[1] = sub[0];
2715811e8887SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)sub[0]));
271600b491fbSPierre Jolivet }
271700b491fbSPierre Jolivet PetscCall(MatDestroySubMatrices(1, &sub));
271813044ca3SPierre Jolivet PetscCall(ISDestroy(&sorted));
271951ea4bc8SPierre Jolivet b[3] = data->aux;
272051ea4bc8SPierre Jolivet PetscCall(MatCreateSchurComplement(subA[0], subA[1], b[1], b[2], b[3], &S));
272113044ca3SPierre Jolivet PetscCall(MatSchurComplementSetKSP(S, ksp[0]));
272251ea4bc8SPierre Jolivet if (data->N != 1) {
272351ea4bc8SPierre Jolivet PetscCall(PCASMSetType(data->levels[0]->pc, PC_ASM_NONE)); /* "Neumann--Neumann" preconditioning with overlap and a Boolean partition of unity */
272451ea4bc8SPierre Jolivet PetscCall(PCASMSetLocalSubdomains(data->levels[0]->pc, 1, &data->is, &loc));
272551ea4bc8SPierre Jolivet PetscCall(PCSetFromOptions(data->levels[0]->pc)); /* action of eq. (15) of https://hal.science/hal-02343808v6/document (with a sign flip) */
272651ea4bc8SPierre Jolivet s = data->levels[0]->pc;
272751ea4bc8SPierre Jolivet } else {
272851ea4bc8SPierre Jolivet is[0] = data->is;
272951ea4bc8SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)is[0]));
273051ea4bc8SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)b[3]));
273151ea4bc8SPierre Jolivet PetscCall(PCSetType(pc, PCASM)); /* change the type of the current PC */
273251ea4bc8SPierre Jolivet data = nullptr; /* destroyed in the previous PCSetType(), so reset to NULL to avoid any faulty use */
273351ea4bc8SPierre Jolivet PetscCall(PCAppendOptionsPrefix(pc, "pc_hpddm_coarse_")); /* same prefix as when using PCHPDDM with a single level */
273451ea4bc8SPierre Jolivet PetscCall(PCASMSetLocalSubdomains(pc, 1, is, &loc));
273551ea4bc8SPierre Jolivet PetscCall(ISDestroy(is));
273651ea4bc8SPierre Jolivet PetscCall(ISDestroy(&loc));
273751ea4bc8SPierre Jolivet s = pc;
273851ea4bc8SPierre Jolivet }
273951ea4bc8SPierre Jolivet PetscCall(PCHPDDMCommunicationAvoidingPCASM_Private(s, S, PETSC_TRUE)); /* the subdomain Mat is already known and the input IS of PCASMSetLocalSubdomains() is already sorted */
274051ea4bc8SPierre Jolivet PetscTryMethod(s, "PCASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (s, &n, nullptr, &ksp));
274113044ca3SPierre Jolivet PetscCheck(n == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of subdomain solver %" PetscInt_FMT " != 1", n);
274213044ca3SPierre Jolivet PetscCall(KSPGetPC(ksp[0], &inner));
274313044ca3SPierre Jolivet PetscCall(PCSetType(inner, PCSHELL)); /* compute the action of the inverse of the local Schur complement with a PCSHELL */
274413044ca3SPierre Jolivet b[0] = subA[0];
274513044ca3SPierre Jolivet PetscCall(MatCreateNest(PETSC_COMM_SELF, 2, nullptr, 2, nullptr, b, &N)); /* instead of computing inv(A11 - A10 inv(A00) A01), compute inv([A00, A01; A10, A11]) followed by a partial solution associated to the A11 block */
274651ea4bc8SPierre Jolivet if (!data) PetscCall(PetscObjectDereference((PetscObject)b[3]));
274700b491fbSPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)b[1]));
274800b491fbSPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)b[2]));
274913044ca3SPierre Jolivet PetscCall(PCCreate(PETSC_COMM_SELF, &s));
275013044ca3SPierre Jolivet PetscCall(PCSetOptionsPrefix(s, ((PetscObject)inner)->prefix));
275113044ca3SPierre Jolivet PetscCall(PCSetOptionsPrefix(inner, nullptr));
275213044ca3SPierre Jolivet PetscCall(KSPSetSkipPCSetFromOptions(ksp[0], PETSC_TRUE));
275313044ca3SPierre Jolivet PetscCall(PCSetType(s, PCLU));
275451ea4bc8SPierre Jolivet if (PetscDefined(HAVE_MUMPS)) PetscCall(PCFactorSetMatSolverType(s, MATSOLVERMUMPS)); /* only MATSOLVERMUMPS handles MATNEST, so for the others, e.g., MATSOLVERPETSC or MATSOLVERMKL_PARDISO, convert to plain MATAIJ */
275513044ca3SPierre Jolivet PetscCall(PCSetFromOptions(s));
275613044ca3SPierre Jolivet PetscCall(PCFactorGetMatSolverType(s, &type));
275713044ca3SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &flg));
275851ea4bc8SPierre Jolivet PetscCall(MatGetLocalSize(A11, &n, nullptr));
275951ea4bc8SPierre Jolivet if (flg || n == 0) {
276013044ca3SPierre Jolivet PetscCall(PCSetOperators(s, N, N));
276151ea4bc8SPierre Jolivet if (n) {
276200b491fbSPierre Jolivet PetscCall(PCFactorGetMatrix(s, b));
276300b491fbSPierre Jolivet PetscCall(MatSetOptionsPrefix(*b, ((PetscObject)s)->prefix));
276424ddd604SPierre Jolivet n = -1;
27658dc3fbeeSPierre Jolivet PetscCall(PetscOptionsGetInt(((PetscObject)pc)->options, ((PetscObject)s)->prefix, "-mat_mumps_icntl_26", &n, nullptr));
2766f45b553cSPierre Jolivet if (n == 1) { /* allocates a square MatDense of size is[1]->map->n, so one */
2767f45b553cSPierre Jolivet PetscCall(MatNestGetISs(N, is, nullptr)); /* needs to be able to deactivate this path when dealing */
2768f45b553cSPierre Jolivet PetscCall(MatFactorSetSchurIS(*b, is[1])); /* with a large constraint space in order to avoid OOM */
2769f45b553cSPierre Jolivet }
277051ea4bc8SPierre Jolivet } else PetscCall(PCSetType(s, PCNONE)); /* empty local Schur complement (e.g., centralized on another process) */
277113044ca3SPierre Jolivet } else {
277200b491fbSPierre Jolivet PetscCall(MatConvert(N, MATAIJ, MAT_INITIAL_MATRIX, b));
277300b491fbSPierre Jolivet PetscCall(PCSetOperators(s, N, *b));
277400b491fbSPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)*b));
277551ea4bc8SPierre Jolivet PetscCall(PetscObjectTypeCompareAny((PetscObject)s, &flg, PCLU, PCCHOLESKY, PCILU, PCICC, PCQR, ""));
277651ea4bc8SPierre Jolivet if (flg) PetscCall(PCFactorGetMatrix(s, b)); /* MATSOLVERMKL_PARDISO cannot compute in PETSc (yet) a partial solution associated to the A11 block, only partial solution associated to the A00 block or full solution */
277713044ca3SPierre Jolivet }
277813044ca3SPierre Jolivet PetscCall(PetscNew(&p));
277913044ca3SPierre Jolivet p->first = s;
278051ea4bc8SPierre Jolivet if (n != 0) PetscCall(MatCreateVecs(*b, p->second, p->second + 1));
278151ea4bc8SPierre Jolivet else p->second[0] = p->second[1] = nullptr;
278213044ca3SPierre Jolivet PetscCall(PCShellSetContext(inner, p));
278313044ca3SPierre Jolivet PetscCall(PCShellSetApply(inner, PCApply_Nest));
278413044ca3SPierre Jolivet PetscCall(PCShellSetView(inner, PCView_Nest));
278513044ca3SPierre Jolivet PetscCall(PCShellSetDestroy(inner, PCDestroy_Nest));
278613044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)N));
278751ea4bc8SPierre Jolivet if (!data) {
278851ea4bc8SPierre Jolivet PetscCall(MatDestroy(&S));
278951ea4bc8SPierre Jolivet PetscCall(ISDestroy(&unsorted));
279051ea4bc8SPierre Jolivet PetscCall(MatDestroy(&C));
279151ea4bc8SPierre Jolivet PetscCall(ISDestroy(&uis));
279251ea4bc8SPierre Jolivet PetscCall(PetscFree(ctx));
279351ea4bc8SPierre Jolivet #if PetscDefined(USE_DEBUG)
279451ea4bc8SPierre Jolivet PetscCall(ISDestroy(&dis));
279551ea4bc8SPierre Jolivet PetscCall(MatDestroy(&daux));
279651ea4bc8SPierre Jolivet #endif
279751ea4bc8SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
279851ea4bc8SPierre Jolivet }
279913044ca3SPierre Jolivet }
2800f1580f4eSBarry Smith if (!data->levels[0]->scatter) {
2801db4a47b3SPierre Jolivet PetscCall(MatCreateVecs(P, &xin, nullptr));
2802f1580f4eSBarry Smith if (ismatis) PetscCall(MatDestroy(&P));
2803db4a47b3SPierre Jolivet PetscCall(VecScatterCreate(xin, data->is, data->levels[0]->D, nullptr, &data->levels[0]->scatter));
2804f1580f4eSBarry Smith PetscCall(VecDestroy(&xin));
2805f1580f4eSBarry Smith }
2806f1580f4eSBarry Smith if (data->levels[0]->P) {
2807f1580f4eSBarry Smith /* if the pattern is the same and PCSetUp() has previously succeeded, reuse HPDDM buffers and connectivity */
2808371d2eb7SMartin Diehl PetscCall(HPDDM::Schwarz<PetscScalar>::destroy(data->levels[0], !pc->setupcalled || pc->flag == DIFFERENT_NONZERO_PATTERN ? PETSC_TRUE : PETSC_FALSE));
2809f1580f4eSBarry Smith }
2810f1580f4eSBarry Smith if (!data->levels[0]->P) data->levels[0]->P = new HPDDM::Schwarz<PetscScalar>();
2811db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventBegin(PC_HPDDM_SetUp[0], data->levels[0]->ksp, nullptr, nullptr, nullptr));
2812db4a47b3SPierre Jolivet else PetscCall(PetscLogEventBegin(PC_HPDDM_Strc, data->levels[0]->ksp, nullptr, nullptr, nullptr));
2813f1580f4eSBarry Smith /* HPDDM internal data structure */
281413044ca3SPierre Jolivet PetscCall(data->levels[0]->P->structure(loc, data->is, !ctx ? sub[0] : nullptr, ismatis ? C : data->aux, data->levels));
2815db4a47b3SPierre Jolivet if (!data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_Strc, data->levels[0]->ksp, nullptr, nullptr, nullptr));
2816f1580f4eSBarry Smith /* matrix pencil of the generalized eigenvalue problem on the overlap (GenEO) */
281713044ca3SPierre Jolivet if (!ctx) {
28189bb5c669SPierre Jolivet if (data->deflation || overlap != -1) weighted = data->aux;
2819f1580f4eSBarry Smith else if (!data->B) {
2820e2b46ddfSPierre Jolivet PetscBool cmp;
2821811e8887SPierre Jolivet
2822f1580f4eSBarry Smith PetscCall(MatDuplicate(sub[0], MAT_COPY_VALUES, &weighted));
2823e2b46ddfSPierre Jolivet PetscCall(PetscObjectTypeCompareAny((PetscObject)weighted, &cmp, MATNORMAL, MATNORMALHERMITIAN, ""));
2824e2b46ddfSPierre Jolivet if (cmp) flg = PETSC_FALSE;
2825e2b46ddfSPierre Jolivet PetscCall(MatDiagonalScale(weighted, data->levels[0]->D, data->levels[0]->D));
28265b0bb9f2SPierre Jolivet /* neither MatDuplicate() nor MatDiagonalScale() handles the symmetry options, so propagate the options explicitly */
2827f1580f4eSBarry Smith /* only useful for -mat_type baij -pc_hpddm_levels_1_st_pc_type cholesky (no problem with MATAIJ or MATSBAIJ) */
2828f1580f4eSBarry Smith PetscCall(MatPropagateSymmetryOptions(sub[0], weighted));
28296d61d5b2SPierre Jolivet if (PetscDefined(USE_DEBUG) && PetscBool3ToBool(data->Neumann)) {
28306d61d5b2SPierre Jolivet Mat *sub, A[3];
28316d61d5b2SPierre Jolivet PetscReal norm[2];
28326d61d5b2SPierre Jolivet PetscBool flg;
28336d61d5b2SPierre Jolivet
28346d61d5b2SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)P, MATMPISBAIJ, &flg)); /* MatCreateSubMatrices() does not work with MATSBAIJ and unsorted ISes, so convert to MPIAIJ */
28356d61d5b2SPierre Jolivet if (flg) PetscCall(MatConvert(P, MATMPIAIJ, MAT_INITIAL_MATRIX, A));
28366d61d5b2SPierre Jolivet else {
28376d61d5b2SPierre Jolivet A[0] = P;
28386d61d5b2SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)P));
28396d61d5b2SPierre Jolivet }
28406d61d5b2SPierre Jolivet PetscCall(MatCreateSubMatrices(A[0], 1, &data->is, &data->is, MAT_INITIAL_MATRIX, &sub));
28416d61d5b2SPierre Jolivet PetscCall(MatDiagonalScale(sub[0], data->levels[0]->D, data->levels[0]->D));
28426d61d5b2SPierre Jolivet PetscCall(MatConvert(sub[0], MATSEQAIJ, MAT_INITIAL_MATRIX, A + 1)); /* too many corner cases to handle (MATNORMAL, MATNORMALHERMITIAN, MATBAIJ with different block sizes...), so just MatConvert() to MATSEQAIJ since this is just for debugging */
28436d61d5b2SPierre Jolivet PetscCall(MatConvert(weighted, MATSEQAIJ, MAT_INITIAL_MATRIX, A + 2));
28446d61d5b2SPierre Jolivet PetscCall(MatAXPY(A[1], -1.0, A[2], UNKNOWN_NONZERO_PATTERN));
28456d61d5b2SPierre Jolivet PetscCall(MatNorm(A[1], NORM_FROBENIUS, norm));
28466d61d5b2SPierre Jolivet if (norm[0]) {
28476d61d5b2SPierre Jolivet PetscCall(MatNorm(A[2], NORM_FROBENIUS, norm + 1));
28486d61d5b2SPierre Jolivet PetscCheck(PetscAbsReal(norm[0] / norm[1]) < PetscSqrtReal(PETSC_SMALL), PETSC_COMM_SELF, PETSC_ERR_USER_INPUT, "Auxiliary Mat is different from the (assembled) subdomain Mat for the interior unknowns, so it cannot be the Neumann matrix, remove -%spc_hpddm_has_neumann", pcpre ? pcpre : "");
28496d61d5b2SPierre Jolivet }
28506d61d5b2SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &sub));
28516d61d5b2SPierre Jolivet for (PetscInt i = 0; i < 3; ++i) PetscCall(MatDestroy(A + i));
28526d61d5b2SPierre Jolivet }
2853f1580f4eSBarry Smith } else weighted = data->B;
285413044ca3SPierre Jolivet } else weighted = nullptr;
2855f1580f4eSBarry Smith /* SLEPc is used inside the loaded symbol */
28569bb5c669SPierre Jolivet PetscCall((*loadedSym)(data->levels[0]->P, data->is, ismatis ? C : (algebraic && !block && overlap == -1 ? sub[0] : (!ctx ? data->aux : S)), weighted, data->B, initial, data->levels));
28579bb5c669SPierre Jolivet if (!ctx && data->share && overlap == -1) {
2858f1580f4eSBarry Smith Mat st[2];
28596d61d5b2SPierre Jolivet
2860f1580f4eSBarry Smith PetscCall(KSPGetOperators(ksp[0], st, st + 1));
28615c7345deSPierre Jolivet PetscCall(MatCopy(subA[0], st[0], structure));
2862f1580f4eSBarry Smith if (subA[1] != subA[0] || st[1] != st[0]) PetscCall(MatCopy(subA[1], st[1], SAME_NONZERO_PATTERN));
28630307214fSPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)subA[0]));
2864f1580f4eSBarry Smith }
2865db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_SetUp[0], data->levels[0]->ksp, nullptr, nullptr, nullptr));
2866f1580f4eSBarry Smith if (ismatis) PetscCall(MatISGetLocalMat(C, &N));
2867f1580f4eSBarry Smith else N = data->aux;
286813044ca3SPierre Jolivet if (!ctx) P = sub[0];
286913044ca3SPierre Jolivet else P = S;
2870f1580f4eSBarry Smith /* going through the grid hierarchy */
2871f1580f4eSBarry Smith for (n = 1; n < data->N; ++n) {
2872db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventBegin(PC_HPDDM_SetUp[n], data->levels[n]->ksp, nullptr, nullptr, nullptr));
2873f1580f4eSBarry Smith /* method composed in the loaded symbol since there, SLEPc is used as well */
2874f1580f4eSBarry 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));
2875db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_SetUp[n], data->levels[n]->ksp, nullptr, nullptr, nullptr));
2876f1580f4eSBarry Smith }
2877f1580f4eSBarry Smith /* reset to NULL to avoid any faulty use */
2878db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)data->levels[0]->ksp, "PCHPDDMSetUp_Private_C", nullptr));
2879db4a47b3SPierre Jolivet if (!ismatis) PetscCall(PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_C", nullptr));
2880f1580f4eSBarry Smith else PetscCall(PetscObjectDereference((PetscObject)C)); /* matching PetscObjectReference() above */
2881f1580f4eSBarry Smith for (n = 0; n < data->N - 1; ++n)
2882f1580f4eSBarry Smith if (data->levels[n]->P) {
2883f1580f4eSBarry Smith /* HPDDM internal work buffers */
2884f1580f4eSBarry Smith data->levels[n]->P->setBuffer();
2885f1580f4eSBarry Smith data->levels[n]->P->super::start();
2886f1580f4eSBarry Smith }
28879bb5c669SPierre Jolivet if (ismatis || !subdomains) PetscCall(PCHPDDMDestroySubMatrices_Private(PetscBool3ToBool(data->Neumann), PetscBool(algebraic && !block && overlap == -1), sub));
2888db4a47b3SPierre Jolivet if (ismatis) data->is = nullptr;
2889f1580f4eSBarry Smith for (n = 0; n < data->N - 1 + (reused > 0); ++n) {
2890f1580f4eSBarry Smith if (data->levels[n]->P) {
2891f1580f4eSBarry Smith PC spc;
2892f1580f4eSBarry Smith
2893f1580f4eSBarry Smith /* force the PC to be PCSHELL to do the coarse grid corrections */
2894f1580f4eSBarry Smith PetscCall(KSPSetSkipPCSetFromOptions(data->levels[n]->ksp, PETSC_TRUE));
2895f1580f4eSBarry Smith PetscCall(KSPGetPC(data->levels[n]->ksp, &spc));
2896f1580f4eSBarry Smith PetscCall(PCSetType(spc, PCSHELL));
2897f1580f4eSBarry Smith PetscCall(PCShellSetContext(spc, data->levels[n]));
2898f1580f4eSBarry Smith PetscCall(PCShellSetSetUp(spc, PCSetUp_HPDDMShell));
2899f1580f4eSBarry Smith PetscCall(PCShellSetApply(spc, PCApply_HPDDMShell));
2900f1580f4eSBarry Smith PetscCall(PCShellSetMatApply(spc, PCMatApply_HPDDMShell));
2901d4f06b61SRaphael Zanella PetscCall(PCShellSetApplyTranspose(spc, PCApplyTranspose_HPDDMShell));
29028cb7430dSRaphael Zanella PetscCall(PCShellSetMatApplyTranspose(spc, PCMatApplyTranspose_HPDDMShell));
290313044ca3SPierre Jolivet if (ctx && n == 0) {
290413044ca3SPierre Jolivet Mat Amat, Pmat;
290513044ca3SPierre Jolivet PetscInt m, M;
290601e3c840SPierre Jolivet std::tuple<Mat, PetscSF, Vec[2]> *ctx;
290713044ca3SPierre Jolivet
290813044ca3SPierre Jolivet PetscCall(KSPGetOperators(data->levels[n]->ksp, nullptr, &Pmat));
290913044ca3SPierre Jolivet PetscCall(MatGetLocalSize(Pmat, &m, nullptr));
291013044ca3SPierre Jolivet PetscCall(MatGetSize(Pmat, &M, nullptr));
291113044ca3SPierre Jolivet PetscCall(PetscNew(&ctx));
291213044ca3SPierre Jolivet std::get<0>(*ctx) = S;
291313044ca3SPierre Jolivet std::get<1>(*ctx) = data->levels[n]->scatter;
291413044ca3SPierre Jolivet PetscCall(MatCreateShell(PetscObjectComm((PetscObject)data->levels[n]->ksp), m, m, M, M, ctx, &Amat));
291557d50842SBarry Smith PetscCall(MatShellSetOperation(Amat, MATOP_MULT, (PetscErrorCodeFn *)MatMult_Schur<false>));
291657d50842SBarry Smith PetscCall(MatShellSetOperation(Amat, MATOP_MULT_TRANSPOSE, (PetscErrorCodeFn *)MatMult_Schur<true>));
291757d50842SBarry Smith PetscCall(MatShellSetOperation(Amat, MATOP_DESTROY, (PetscErrorCodeFn *)MatDestroy_Schur));
291813044ca3SPierre Jolivet PetscCall(MatCreateVecs(S, std::get<2>(*ctx), std::get<2>(*ctx) + 1));
291913044ca3SPierre Jolivet PetscCall(KSPSetOperators(data->levels[n]->ksp, Amat, Pmat));
292013044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)Amat));
292113044ca3SPierre Jolivet }
2922f1580f4eSBarry Smith PetscCall(PCShellSetDestroy(spc, PCDestroy_HPDDMShell));
2923f1580f4eSBarry Smith if (!data->levels[n]->pc) PetscCall(PCCreate(PetscObjectComm((PetscObject)data->levels[n]->ksp), &data->levels[n]->pc));
2924f1580f4eSBarry Smith if (n < reused) {
2925f1580f4eSBarry Smith PetscCall(PCSetReusePreconditioner(spc, PETSC_TRUE));
2926f1580f4eSBarry Smith PetscCall(PCSetReusePreconditioner(data->levels[n]->pc, PETSC_TRUE));
2927f1580f4eSBarry Smith }
2928f1580f4eSBarry Smith PetscCall(PCSetUp(spc));
2929f1580f4eSBarry Smith }
2930f1580f4eSBarry Smith }
293113044ca3SPierre Jolivet if (ctx) PetscCall(MatDestroy(&S));
29329bb5c669SPierre Jolivet if (overlap == -1) PetscCall(PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", nullptr));
2933f1580f4eSBarry Smith } else flg = reused ? PETSC_FALSE : PETSC_TRUE;
2934f1580f4eSBarry Smith if (!ismatis && subdomains) {
2935f1580f4eSBarry Smith if (flg) PetscCall(KSPGetPC(data->levels[0]->ksp, &inner));
2936f1580f4eSBarry Smith else inner = data->levels[0]->pc;
2937f1580f4eSBarry Smith if (inner) {
2938398c7888SPierre Jolivet if (!inner->setupcalled) PetscCall(PCSetType(inner, PCASM));
2939398c7888SPierre Jolivet PetscCall(PCSetFromOptions(inner));
2940b89bd804SBoris Martin PetscCall(PCSetModifySubMatrices(inner, pc->modifysubmatrices, pc->modifysubmatricesP));
2941398c7888SPierre Jolivet PetscCall(PetscStrcmp(((PetscObject)inner)->type_name, PCASM, &flg));
2942398c7888SPierre Jolivet if (flg) {
2943f1580f4eSBarry Smith if (!inner->setupcalled) { /* evaluates to PETSC_FALSE when -pc_hpddm_block_splitting */
2944398c7888SPierre Jolivet IS sorted; /* PCASM will sort the input IS, duplicate it to return an unmodified (PCHPDDM) input IS */
2945811e8887SPierre Jolivet
2946398c7888SPierre Jolivet PetscCall(ISDuplicate(is[0], &sorted));
2947398c7888SPierre Jolivet PetscCall(PCASMSetLocalSubdomains(inner, 1, &sorted, &loc));
2948398c7888SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)sorted));
2949398c7888SPierre Jolivet }
2950c8ea6600SPierre Jolivet if (!PetscBool3ToBool(data->Neumann) && data->N > 1) { /* subdomain matrices are already created for the eigenproblem, reuse them for the fine-level PC */
2951db4a47b3SPierre Jolivet PetscCall(PCHPDDMPermute_Private(*is, nullptr, nullptr, sub[0], &P, nullptr));
2952398c7888SPierre Jolivet PetscCall(PCHPDDMCommunicationAvoidingPCASM_Private(inner, P, algebraic));
2953398c7888SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)P));
2954f1580f4eSBarry Smith }
2955f1580f4eSBarry Smith }
2956f1580f4eSBarry Smith }
29579bb5c669SPierre Jolivet if (data->N > 1) {
29589bb5c669SPierre Jolivet if (overlap != 1) PetscCall(PCHPDDMDestroySubMatrices_Private(PetscBool3ToBool(data->Neumann), PetscBool(algebraic && !block && overlap == -1), sub));
29599bb5c669SPierre Jolivet if (overlap == 1) PetscCall(MatDestroy(subA));
29609bb5c669SPierre Jolivet }
2961f1580f4eSBarry Smith }
2962f1580f4eSBarry Smith PetscCall(ISDestroy(&loc));
2963f1580f4eSBarry Smith } else data->N = 1 + reused; /* enforce this value to 1 + reused if there is no way to build another level */
2964f1580f4eSBarry Smith if (requested != data->N + reused) {
2965f1580f4eSBarry 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,
2966f1580f4eSBarry Smith data->N, pcpre ? pcpre : ""));
2967b5a302b3SPierre Jolivet PetscCall(PetscInfo(pc, "It is best to tune parameters, e.g., a higher value for -%spc_hpddm_levels_%" PetscInt_FMT "_eps_threshold_absolute or a lower value for -%spc_hpddm_levels_%" PetscInt_FMT "_svd_threshold_relative, so that at least one local deflation vector will be selected\n", pcpre ? pcpre : "",
2968b5a302b3SPierre Jolivet data->N, pcpre ? pcpre : "", data->N));
2969f1580f4eSBarry Smith /* cannot use PCDestroy_HPDDMShell() because PCSHELL not set for unassembled levels */
2970f1580f4eSBarry Smith for (n = data->N - 1; n < requested - 1; ++n) {
2971f1580f4eSBarry Smith if (data->levels[n]->P) {
2972f1580f4eSBarry Smith PetscCall(HPDDM::Schwarz<PetscScalar>::destroy(data->levels[n], PETSC_TRUE));
2973f1580f4eSBarry Smith PetscCall(VecDestroyVecs(1, &data->levels[n]->v[0]));
2974f1580f4eSBarry Smith PetscCall(VecDestroyVecs(2, &data->levels[n]->v[1]));
2975f1580f4eSBarry Smith PetscCall(MatDestroy(data->levels[n]->V));
2976f1580f4eSBarry Smith PetscCall(MatDestroy(data->levels[n]->V + 1));
2977f1580f4eSBarry Smith PetscCall(MatDestroy(data->levels[n]->V + 2));
2978f1580f4eSBarry Smith PetscCall(VecDestroy(&data->levels[n]->D));
297901e3c840SPierre Jolivet PetscCall(PetscSFDestroy(&data->levels[n]->scatter));
2980f1580f4eSBarry Smith }
2981f1580f4eSBarry Smith }
2982f1580f4eSBarry Smith if (reused) {
2983f1580f4eSBarry Smith for (n = reused; n < PETSC_PCHPDDM_MAXLEVELS && data->levels[n]; ++n) {
2984f1580f4eSBarry Smith PetscCall(KSPDestroy(&data->levels[n]->ksp));
2985f1580f4eSBarry Smith PetscCall(PCDestroy(&data->levels[n]->pc));
2986f1580f4eSBarry Smith }
2987f1580f4eSBarry Smith }
2988b5a302b3SPierre Jolivet 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 or a lower value for -%spc_hpddm_levels_%" PetscInt_FMT "_svd_threshold_relative, 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,
2989b5a302b3SPierre Jolivet data->N, reused, data->N, pcpre ? pcpre : "", pcpre ? pcpre : "", data->N, pcpre ? pcpre : "", data->N);
2990f1580f4eSBarry Smith }
2991f1580f4eSBarry Smith /* these solvers are created after PCSetFromOptions() is called */
2992f1580f4eSBarry Smith if (pc->setfromoptionscalled) {
2993f1580f4eSBarry Smith for (n = 0; n < data->N; ++n) {
2994f1580f4eSBarry Smith if (data->levels[n]->ksp) PetscCall(KSPSetFromOptions(data->levels[n]->ksp));
2995f1580f4eSBarry Smith if (data->levels[n]->pc) PetscCall(PCSetFromOptions(data->levels[n]->pc));
2996f1580f4eSBarry Smith }
2997f1580f4eSBarry Smith pc->setfromoptionscalled = 0;
2998f1580f4eSBarry Smith }
2999f1580f4eSBarry Smith data->N += reused;
3000f1580f4eSBarry Smith if (data->share && swap) {
3001f1580f4eSBarry Smith /* swap back pointers so that variables follow the user-provided numbering */
3002f1580f4eSBarry Smith std::swap(C, data->aux);
3003f1580f4eSBarry Smith std::swap(uis, data->is);
3004f1580f4eSBarry Smith PetscCall(MatDestroy(&C));
3005f1580f4eSBarry Smith PetscCall(ISDestroy(&uis));
3006f1580f4eSBarry Smith }
3007398c7888SPierre Jolivet if (algebraic) PetscCall(MatDestroy(&data->aux));
300838fb34a1SPierre Jolivet if (unsorted && unsorted != is[0]) {
3009398c7888SPierre Jolivet PetscCall(ISCopy(unsorted, data->is));
3010398c7888SPierre Jolivet PetscCall(ISDestroy(&unsorted));
3011398c7888SPierre Jolivet }
3012398c7888SPierre Jolivet #if PetscDefined(USE_DEBUG)
3013398c7888SPierre 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);
3014398c7888SPierre Jolivet if (data->is) {
3015398c7888SPierre Jolivet PetscCall(ISEqualUnsorted(data->is, dis, &flg));
3016398c7888SPierre Jolivet PetscCall(ISDestroy(&dis));
3017398c7888SPierre Jolivet PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input IS and output IS are not equal");
3018398c7888SPierre Jolivet }
3019398c7888SPierre 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);
3020398c7888SPierre Jolivet if (data->aux) {
3021398c7888SPierre Jolivet PetscCall(MatMultEqual(data->aux, daux, 20, &flg));
3022398c7888SPierre Jolivet PetscCall(MatDestroy(&daux));
3023398c7888SPierre Jolivet PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input Mat and output Mat are not equal");
3024398c7888SPierre Jolivet }
3025398c7888SPierre Jolivet #endif
30263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3027f1580f4eSBarry Smith }
3028f1580f4eSBarry Smith
3029f1580f4eSBarry Smith /*@
3030f1580f4eSBarry Smith PCHPDDMSetCoarseCorrectionType - Sets the coarse correction type.
3031f1580f4eSBarry Smith
3032c3339decSBarry Smith Collective
3033f1580f4eSBarry Smith
3034f1580f4eSBarry Smith Input Parameters:
3035f1580f4eSBarry Smith + pc - preconditioner context
3036aa1539e9SPierre Jolivet - type - `PC_HPDDM_COARSE_CORRECTION_DEFLATED`, `PC_HPDDM_COARSE_CORRECTION_ADDITIVE`, `PC_HPDDM_COARSE_CORRECTION_BALANCED`, or `PC_HPDDM_COARSE_CORRECTION_NONE`
3037f1580f4eSBarry Smith
3038f1580f4eSBarry Smith Options Database Key:
3039aa1539e9SPierre Jolivet . -pc_hpddm_coarse_correction <deflated, additive, balanced, none> - type of coarse correction to apply
3040f1580f4eSBarry Smith
3041f1580f4eSBarry Smith Level: intermediate
3042f1580f4eSBarry Smith
3043562efe2eSBarry Smith .seealso: [](ch_ksp), `PCHPDDMGetCoarseCorrectionType()`, `PCHPDDM`, `PCHPDDMCoarseCorrectionType`
3044f1580f4eSBarry Smith @*/
PCHPDDMSetCoarseCorrectionType(PC pc,PCHPDDMCoarseCorrectionType type)3045d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMSetCoarseCorrectionType(PC pc, PCHPDDMCoarseCorrectionType type)
3046d71ae5a4SJacob Faibussowitsch {
3047f1580f4eSBarry Smith PetscFunctionBegin;
3048f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
3049f1580f4eSBarry Smith PetscValidLogicalCollectiveEnum(pc, type, 2);
3050f1580f4eSBarry Smith PetscTryMethod(pc, "PCHPDDMSetCoarseCorrectionType_C", (PC, PCHPDDMCoarseCorrectionType), (pc, type));
30513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3052f1580f4eSBarry Smith }
3053f1580f4eSBarry Smith
3054f1580f4eSBarry Smith /*@
3055f1580f4eSBarry Smith PCHPDDMGetCoarseCorrectionType - Gets the coarse correction type.
3056f1580f4eSBarry Smith
3057f1580f4eSBarry Smith Input Parameter:
3058f1580f4eSBarry Smith . pc - preconditioner context
3059f1580f4eSBarry Smith
3060f1580f4eSBarry Smith Output Parameter:
3061aa1539e9SPierre Jolivet . type - `PC_HPDDM_COARSE_CORRECTION_DEFLATED`, `PC_HPDDM_COARSE_CORRECTION_ADDITIVE`, `PC_HPDDM_COARSE_CORRECTION_BALANCED`, or `PC_HPDDM_COARSE_CORRECTION_NONE`
3062f1580f4eSBarry Smith
3063f1580f4eSBarry Smith Level: intermediate
3064f1580f4eSBarry Smith
3065562efe2eSBarry Smith .seealso: [](ch_ksp), `PCHPDDMSetCoarseCorrectionType()`, `PCHPDDM`, `PCHPDDMCoarseCorrectionType`
3066f1580f4eSBarry Smith @*/
PCHPDDMGetCoarseCorrectionType(PC pc,PCHPDDMCoarseCorrectionType * type)3067d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMGetCoarseCorrectionType(PC pc, PCHPDDMCoarseCorrectionType *type)
3068d71ae5a4SJacob Faibussowitsch {
3069f1580f4eSBarry Smith PetscFunctionBegin;
3070f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
3071f1580f4eSBarry Smith if (type) {
30724f572ea9SToby Isaac PetscAssertPointer(type, 2);
3073f1580f4eSBarry Smith PetscUseMethod(pc, "PCHPDDMGetCoarseCorrectionType_C", (PC, PCHPDDMCoarseCorrectionType *), (pc, type));
3074f1580f4eSBarry Smith }
30753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3076f1580f4eSBarry Smith }
3077f1580f4eSBarry Smith
PCHPDDMSetCoarseCorrectionType_HPDDM(PC pc,PCHPDDMCoarseCorrectionType type)3078d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetCoarseCorrectionType_HPDDM(PC pc, PCHPDDMCoarseCorrectionType type)
3079d71ae5a4SJacob Faibussowitsch {
3080f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
3081f1580f4eSBarry Smith
3082f1580f4eSBarry Smith PetscFunctionBegin;
3083f1580f4eSBarry Smith data->correction = type;
30843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3085f1580f4eSBarry Smith }
3086f1580f4eSBarry Smith
PCHPDDMGetCoarseCorrectionType_HPDDM(PC pc,PCHPDDMCoarseCorrectionType * type)3087d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMGetCoarseCorrectionType_HPDDM(PC pc, PCHPDDMCoarseCorrectionType *type)
3088d71ae5a4SJacob Faibussowitsch {
3089f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
3090f1580f4eSBarry Smith
3091f1580f4eSBarry Smith PetscFunctionBegin;
3092f1580f4eSBarry Smith *type = data->correction;
30933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3094f1580f4eSBarry Smith }
3095f1580f4eSBarry Smith
3096f1580f4eSBarry Smith /*@
3097e31fc274SPierre Jolivet PCHPDDMSetSTShareSubKSP - Sets whether the `KSP` in SLEPc `ST` and the fine-level subdomain solver should be shared.
3098e31fc274SPierre Jolivet
3099e31fc274SPierre Jolivet Input Parameters:
3100e31fc274SPierre Jolivet + pc - preconditioner context
3101e31fc274SPierre Jolivet - share - whether the `KSP` should be shared or not
3102e31fc274SPierre Jolivet
3103e31fc274SPierre Jolivet Note:
3104e31fc274SPierre Jolivet This is not the same as `PCSetReusePreconditioner()`. Given certain conditions (visible using -info), a symbolic factorization can be skipped
3105e31fc274SPierre Jolivet when using a subdomain `PCType` such as `PCLU` or `PCCHOLESKY`.
3106e31fc274SPierre Jolivet
3107e31fc274SPierre Jolivet Level: advanced
3108e31fc274SPierre Jolivet
3109562efe2eSBarry Smith .seealso: [](ch_ksp), `PCHPDDM`, `PCHPDDMGetSTShareSubKSP()`
3110e31fc274SPierre Jolivet @*/
PCHPDDMSetSTShareSubKSP(PC pc,PetscBool share)3111e31fc274SPierre Jolivet PetscErrorCode PCHPDDMSetSTShareSubKSP(PC pc, PetscBool share)
3112e31fc274SPierre Jolivet {
3113e31fc274SPierre Jolivet PetscFunctionBegin;
3114e31fc274SPierre Jolivet PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
3115e31fc274SPierre Jolivet PetscTryMethod(pc, "PCHPDDMSetSTShareSubKSP_C", (PC, PetscBool), (pc, share));
31163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3117e31fc274SPierre Jolivet }
3118e31fc274SPierre Jolivet
3119e31fc274SPierre Jolivet /*@
3120f1580f4eSBarry Smith PCHPDDMGetSTShareSubKSP - Gets whether the `KSP` in SLEPc `ST` and the fine-level subdomain solver is shared.
3121f1580f4eSBarry Smith
3122f1580f4eSBarry Smith Input Parameter:
3123f1580f4eSBarry Smith . pc - preconditioner context
3124f1580f4eSBarry Smith
3125f1580f4eSBarry Smith Output Parameter:
3126f1580f4eSBarry Smith . share - whether the `KSP` is shared or not
3127f1580f4eSBarry Smith
3128f1580f4eSBarry Smith Note:
3129f1580f4eSBarry 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
3130f1580f4eSBarry Smith when using a subdomain `PCType` such as `PCLU` or `PCCHOLESKY`.
3131f1580f4eSBarry Smith
3132f1580f4eSBarry Smith Level: advanced
3133f1580f4eSBarry Smith
3134562efe2eSBarry Smith .seealso: [](ch_ksp), `PCHPDDM`, `PCHPDDMSetSTShareSubKSP()`
3135f1580f4eSBarry Smith @*/
PCHPDDMGetSTShareSubKSP(PC pc,PetscBool * share)3136d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMGetSTShareSubKSP(PC pc, PetscBool *share)
3137d71ae5a4SJacob Faibussowitsch {
3138f1580f4eSBarry Smith PetscFunctionBegin;
3139f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
3140f1580f4eSBarry Smith if (share) {
31414f572ea9SToby Isaac PetscAssertPointer(share, 2);
3142f1580f4eSBarry Smith PetscUseMethod(pc, "PCHPDDMGetSTShareSubKSP_C", (PC, PetscBool *), (pc, share));
3143f1580f4eSBarry Smith }
31443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3145f1580f4eSBarry Smith }
3146f1580f4eSBarry Smith
PCHPDDMSetSTShareSubKSP_HPDDM(PC pc,PetscBool share)3147e31fc274SPierre Jolivet static PetscErrorCode PCHPDDMSetSTShareSubKSP_HPDDM(PC pc, PetscBool share)
3148e31fc274SPierre Jolivet {
3149e31fc274SPierre Jolivet PC_HPDDM *data = (PC_HPDDM *)pc->data;
3150e31fc274SPierre Jolivet
3151e31fc274SPierre Jolivet PetscFunctionBegin;
3152e31fc274SPierre Jolivet data->share = share;
31533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3154e31fc274SPierre Jolivet }
3155e31fc274SPierre Jolivet
PCHPDDMGetSTShareSubKSP_HPDDM(PC pc,PetscBool * share)3156d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMGetSTShareSubKSP_HPDDM(PC pc, PetscBool *share)
3157d71ae5a4SJacob Faibussowitsch {
3158f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data;
3159f1580f4eSBarry Smith
3160f1580f4eSBarry Smith PetscFunctionBegin;
3161f1580f4eSBarry Smith *share = data->share;
31623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3163f1580f4eSBarry Smith }
3164f1580f4eSBarry Smith
3165f1580f4eSBarry Smith /*@
3166f1580f4eSBarry Smith PCHPDDMSetDeflationMat - Sets the deflation space used to assemble a coarser operator.
3167f1580f4eSBarry Smith
3168f1580f4eSBarry Smith Input Parameters:
3169f1580f4eSBarry Smith + pc - preconditioner context
3170f1580f4eSBarry Smith . is - index set of the local deflation matrix
3171f1580f4eSBarry Smith - U - deflation sequential matrix stored as a `MATSEQDENSE`
3172f1580f4eSBarry Smith
3173f1580f4eSBarry Smith Level: advanced
3174f1580f4eSBarry Smith
3175562efe2eSBarry Smith .seealso: [](ch_ksp), `PCHPDDM`, `PCDeflationSetSpace()`, `PCMGSetRestriction()`
3176f1580f4eSBarry Smith @*/
PCHPDDMSetDeflationMat(PC pc,IS is,Mat U)3177d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMSetDeflationMat(PC pc, IS is, Mat U)
3178d71ae5a4SJacob Faibussowitsch {
3179f1580f4eSBarry Smith PetscFunctionBegin;
3180f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
3181f1580f4eSBarry Smith PetscValidHeaderSpecific(is, IS_CLASSID, 2);
3182f1580f4eSBarry Smith PetscValidHeaderSpecific(U, MAT_CLASSID, 3);
3183e31fc274SPierre Jolivet PetscTryMethod(pc, "PCHPDDMSetDeflationMat_C", (PC, IS, Mat), (pc, is, U));
31843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3185f1580f4eSBarry Smith }
3186f1580f4eSBarry Smith
PCHPDDMSetDeflationMat_HPDDM(PC pc,IS is,Mat U)3187d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetDeflationMat_HPDDM(PC pc, IS is, Mat U)
3188d71ae5a4SJacob Faibussowitsch {
3189f1580f4eSBarry Smith PetscFunctionBegin;
3190f1580f4eSBarry Smith PetscCall(PCHPDDMSetAuxiliaryMat_Private(pc, is, U, PETSC_TRUE));
31913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3192f1580f4eSBarry Smith }
3193f1580f4eSBarry Smith
HPDDMLoadDL_Private(PetscBool * found)3194d71ae5a4SJacob Faibussowitsch PetscErrorCode HPDDMLoadDL_Private(PetscBool *found)
3195d71ae5a4SJacob Faibussowitsch {
3196605ad303SPierre Jolivet PetscBool flg;
3197f1580f4eSBarry Smith char lib[PETSC_MAX_PATH_LEN], dlib[PETSC_MAX_PATH_LEN], dir[PETSC_MAX_PATH_LEN];
3198f1580f4eSBarry Smith
3199f1580f4eSBarry Smith PetscFunctionBegin;
32004f572ea9SToby Isaac PetscAssertPointer(found, 1);
3201c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(dir, "${PETSC_LIB_DIR}", sizeof(dir)));
3202db4a47b3SPierre Jolivet PetscCall(PetscOptionsGetString(nullptr, nullptr, "-hpddm_dir", dir, sizeof(dir), nullptr));
3203f1580f4eSBarry Smith PetscCall(PetscSNPrintf(lib, sizeof(lib), "%s/libhpddm_petsc", dir));
3204f1580f4eSBarry Smith PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found));
3205605ad303SPierre Jolivet #if defined(SLEPC_LIB_DIR) /* this variable is passed during SLEPc ./configure when PETSc has not been configured */
3206605ad303SPierre Jolivet if (!*found) { /* with --download-hpddm since slepcconf.h is not yet built (and thus can't be included) */
3207c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(dir, HPDDM_STR(SLEPC_LIB_DIR), sizeof(dir)));
3208f1580f4eSBarry Smith PetscCall(PetscSNPrintf(lib, sizeof(lib), "%s/libhpddm_petsc", dir));
3209f1580f4eSBarry Smith PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found));
3210f1580f4eSBarry Smith }
3211f1580f4eSBarry Smith #endif
3212605ad303SPierre Jolivet if (!*found) { /* probable options for this to evaluate to PETSC_TRUE: system inconsistency (libhpddm_petsc moved by user?) or PETSc configured without --download-slepc */
3213605ad303SPierre Jolivet PetscCall(PetscOptionsGetenv(PETSC_COMM_SELF, "SLEPC_DIR", dir, sizeof(dir), &flg));
3214605ad303SPierre Jolivet if (flg) { /* if both PETSc and SLEPc are configured with --download-hpddm but PETSc has been configured without --download-slepc, one must ensure that libslepc is loaded before libhpddm_petsc */
3215605ad303SPierre Jolivet PetscCall(PetscSNPrintf(lib, sizeof(lib), "%s/lib/libslepc", dir));
3216605ad303SPierre Jolivet PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found));
3217605ad303SPierre Jolivet PetscCheck(*found, PETSC_COMM_SELF, PETSC_ERR_PLIB, "%s not found but SLEPC_DIR=%s", lib, dir);
3218605ad303SPierre Jolivet PetscCall(PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, dlib));
3219605ad303SPierre Jolivet PetscCall(PetscSNPrintf(lib, sizeof(lib), "%s/lib/libhpddm_petsc", dir)); /* libhpddm_petsc is always in the same directory as libslepc */
3220605ad303SPierre Jolivet PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found));
3221605ad303SPierre Jolivet }
3222605ad303SPierre Jolivet }
3223f1580f4eSBarry Smith PetscCheck(*found, PETSC_COMM_SELF, PETSC_ERR_PLIB, "%s not found", lib);
3224f1580f4eSBarry Smith PetscCall(PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, dlib));
32253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3226f1580f4eSBarry Smith }
3227f1580f4eSBarry Smith
3228f1580f4eSBarry Smith /*MC
3229f1580f4eSBarry Smith PCHPDDM - Interface with the HPDDM library.
3230f1580f4eSBarry Smith
32311d27aa22SBarry Smith This `PC` may be used to build multilevel spectral domain decomposition methods based on the GenEO framework {cite}`spillane2011robust` {cite}`al2021multilevel`.
32321d27aa22SBarry Smith It may be viewed as an alternative to spectral
32331d27aa22SBarry Smith AMGe or `PCBDDC` with adaptive selection of constraints. The interface is explained in details in {cite}`jolivetromanzampini2020`
3234f1580f4eSBarry Smith
3235e7593814SPierre Jolivet The matrix used for building the preconditioner (Pmat) may be unassembled (`MATIS`), assembled (`MATAIJ`, `MATBAIJ`, or `MATSBAIJ`), hierarchical (`MATHTOOL`), `MATNORMAL`, `MATNORMALHERMITIAN`, or `MATSCHURCOMPLEMENT` (when `PCHPDDM` is used as part of an outer `PCFIELDSPLIT`).
3236f1580f4eSBarry Smith
3237f1580f4eSBarry Smith For multilevel preconditioning, when using an assembled or hierarchical Pmat, one must provide an auxiliary local `Mat` (unassembled local operator for GenEO) using
3238f1580f4eSBarry Smith `PCHPDDMSetAuxiliaryMat()`. Calling this routine is not needed when using a `MATIS` Pmat, assembly is done internally using `MatConvert()`.
3239f1580f4eSBarry Smith
3240f1580f4eSBarry Smith Options Database Keys:
3241f1580f4eSBarry Smith + -pc_hpddm_define_subdomains <true, default=false> - on the finest level, calls `PCASMSetLocalSubdomains()` with the `IS` supplied in `PCHPDDMSetAuxiliaryMat()`
3242f1580f4eSBarry Smith (not relevant with an unassembled Pmat)
3243f1580f4eSBarry Smith . -pc_hpddm_has_neumann <true, default=false> - on the finest level, informs the `PC` that the local Neumann matrix is supplied in `PCHPDDMSetAuxiliaryMat()`
3244f1580f4eSBarry Smith - -pc_hpddm_coarse_correction <type, default=deflated> - determines the `PCHPDDMCoarseCorrectionType` when calling `PCApply()`
3245f1580f4eSBarry Smith
324638bf2a8cSPierre Jolivet Options for subdomain solvers, subdomain eigensolvers (for computing deflation vectors), and the coarse solver can be set using the following options database prefixes.
3247f1580f4eSBarry Smith .vb
3248f1580f4eSBarry Smith -pc_hpddm_levels_%d_pc_
3249f1580f4eSBarry Smith -pc_hpddm_levels_%d_ksp_
3250f1580f4eSBarry Smith -pc_hpddm_levels_%d_eps_
3251f1580f4eSBarry Smith -pc_hpddm_levels_%d_p
32524ec2a359SPierre Jolivet -pc_hpddm_levels_%d_mat_type
3253f1580f4eSBarry Smith -pc_hpddm_coarse_
3254f1580f4eSBarry Smith -pc_hpddm_coarse_p
32554ec2a359SPierre Jolivet -pc_hpddm_coarse_mat_type
32562ce66baaSPierre Jolivet -pc_hpddm_coarse_mat_filter
3257f1580f4eSBarry Smith .ve
3258f1580f4eSBarry Smith
325938bf2a8cSPierre 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
3260f1580f4eSBarry Smith -pc_hpddm_coarse_p 2 -pc_hpddm_coarse_mat_type baij will use 10 deflation vectors per subdomain on the fine "level 1",
3261f1580f4eSBarry Smith aggregate the fine subdomains into 4 "level 2" subdomains, then use 10 deflation vectors per subdomain on "level 2",
32627eb095acSPierre Jolivet and assemble the coarse matrix (of dimension 4 x 10 = 40) on two processes as a `MATBAIJ` (default is `MATSBAIJ`).
3263f1580f4eSBarry Smith
3264b5a302b3SPierre Jolivet 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_absolute <val>. The default -pc_hpddm_coarse_p value is 1, meaning that the coarse operator is aggregated on a single process.
3265f1580f4eSBarry Smith
32661d27aa22SBarry Smith Level: intermediate
32671d27aa22SBarry Smith
32681d27aa22SBarry Smith Notes:
32691d27aa22SBarry Smith This preconditioner requires that PETSc is built with SLEPc (``--download-slepc``).
32701d27aa22SBarry Smith
32711d27aa22SBarry Smith By default, the underlying concurrent eigenproblems
32721d27aa22SBarry Smith are solved using SLEPc shift-and-invert spectral transformation. This is usually what gives the best performance for GenEO, cf.
32731d27aa22SBarry Smith {cite}`spillane2011robust` {cite}`jolivet2013scalabledd`. As
327438bf2a8cSPierre 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
327538bf2a8cSPierre 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
327638bf2a8cSPierre Jolivet SLEPc documentation since they are specific to `PCHPDDM`.
327738bf2a8cSPierre Jolivet .vb
327838bf2a8cSPierre Jolivet -pc_hpddm_levels_1_st_share_sub_ksp
3279b5a302b3SPierre Jolivet -pc_hpddm_levels_%d_eps_threshold_absolute
328038bf2a8cSPierre Jolivet -pc_hpddm_levels_1_eps_use_inertia
328138bf2a8cSPierre Jolivet .ve
328238bf2a8cSPierre Jolivet
328338bf2a8cSPierre Jolivet The first option from the list only applies to the fine-level eigensolver, see `PCHPDDMSetSTShareSubKSP()`. The second option from the list is
328438bf2a8cSPierre Jolivet used to filter eigenmodes retrieved after convergence of `EPSSolve()` at "level N" such that eigenvectors used to define a "level N+1" coarse
3285b5a302b3SPierre Jolivet correction are associated to eigenvalues whose magnitude are lower or equal than -pc_hpddm_levels_N_eps_threshold_absolute. When using an `EPS` which cannot
328638bf2a8cSPierre 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
328738bf2a8cSPierre 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
328838bf2a8cSPierre Jolivet to supply -pc_hpddm_levels_1_eps_nev. This last option also only applies to the fine-level (N = 1) eigensolver.
3289f1580f4eSBarry Smith
32901d27aa22SBarry Smith See also {cite}`dolean2015introduction`, {cite}`al2022robust`, {cite}`al2022robustpd`, and {cite}`nataf2022recent`
3291f1580f4eSBarry Smith
3292562efe2eSBarry Smith .seealso: [](ch_ksp), `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCHPDDMSetAuxiliaryMat()`, `MATIS`, `PCBDDC`, `PCDEFLATION`, `PCTELESCOPE`, `PCASM`,
3293e31fc274SPierre Jolivet `PCHPDDMSetCoarseCorrectionType()`, `PCHPDDMHasNeumannMat()`, `PCHPDDMSetRHSMat()`, `PCHPDDMSetDeflationMat()`, `PCHPDDMSetSTShareSubKSP()`,
3294e31fc274SPierre Jolivet `PCHPDDMGetSTShareSubKSP()`, `PCHPDDMGetCoarseCorrectionType()`, `PCHPDDMGetComplexities()`
3295f1580f4eSBarry Smith M*/
PCCreate_HPDDM(PC pc)3296d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_HPDDM(PC pc)
3297d71ae5a4SJacob Faibussowitsch {
3298f1580f4eSBarry Smith PC_HPDDM *data;
3299f1580f4eSBarry Smith PetscBool found;
3300f1580f4eSBarry Smith
3301f1580f4eSBarry Smith PetscFunctionBegin;
3302f1580f4eSBarry Smith if (!loadedSym) {
3303f1580f4eSBarry Smith PetscCall(HPDDMLoadDL_Private(&found));
3304db4a47b3SPierre Jolivet if (found) PetscCall(PetscDLLibrarySym(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, nullptr, "PCHPDDM_Internal", (void **)&loadedSym));
3305f1580f4eSBarry Smith }
3306f1580f4eSBarry Smith PetscCheck(loadedSym, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCHPDDM_Internal symbol not found in loaded libhpddm_petsc");
33074dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&data));
3308f1580f4eSBarry Smith pc->data = data;
3309c8ea6600SPierre Jolivet data->Neumann = PETSC_BOOL3_UNKNOWN;
3310f1580f4eSBarry Smith pc->ops->reset = PCReset_HPDDM;
3311f1580f4eSBarry Smith pc->ops->destroy = PCDestroy_HPDDM;
3312f1580f4eSBarry Smith pc->ops->setfromoptions = PCSetFromOptions_HPDDM;
3313f1580f4eSBarry Smith pc->ops->setup = PCSetUp_HPDDM;
3314d4f06b61SRaphael Zanella pc->ops->apply = PCApply_HPDDM<false>;
33158cb7430dSRaphael Zanella pc->ops->matapply = PCMatApply_HPDDM<false>;
3316d4f06b61SRaphael Zanella pc->ops->applytranspose = PCApply_HPDDM<true>;
33178cb7430dSRaphael Zanella pc->ops->matapplytranspose = PCMatApply_HPDDM<true>;
3318f1580f4eSBarry Smith pc->ops->view = PCView_HPDDM;
3319f1580f4eSBarry Smith pc->ops->presolve = PCPreSolve_HPDDM;
3320f1580f4eSBarry Smith
3321f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetAuxiliaryMat_C", PCHPDDMSetAuxiliaryMat_HPDDM));
3322f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMHasNeumannMat_C", PCHPDDMHasNeumannMat_HPDDM));
3323f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetRHSMat_C", PCHPDDMSetRHSMat_HPDDM));
3324f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetCoarseCorrectionType_C", PCHPDDMSetCoarseCorrectionType_HPDDM));
3325f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetCoarseCorrectionType_C", PCHPDDMGetCoarseCorrectionType_HPDDM));
3326e31fc274SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetSTShareSubKSP_C", PCHPDDMSetSTShareSubKSP_HPDDM));
3327f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetSTShareSubKSP_C", PCHPDDMGetSTShareSubKSP_HPDDM));
3328f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetDeflationMat_C", PCHPDDMSetDeflationMat_HPDDM));
33293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3330f1580f4eSBarry Smith }
3331f1580f4eSBarry Smith
3332f1580f4eSBarry Smith /*@C
3333f1580f4eSBarry Smith PCHPDDMInitializePackage - This function initializes everything in the `PCHPDDM` package. It is called from `PCInitializePackage()`.
3334f1580f4eSBarry Smith
3335f1580f4eSBarry Smith Level: developer
3336f1580f4eSBarry Smith
3337562efe2eSBarry Smith .seealso: [](ch_ksp), `PetscInitialize()`
3338f1580f4eSBarry Smith @*/
PCHPDDMInitializePackage(void)3339d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMInitializePackage(void)
3340d71ae5a4SJacob Faibussowitsch {
3341f1580f4eSBarry Smith char ename[32];
3342f1580f4eSBarry Smith
3343f1580f4eSBarry Smith PetscFunctionBegin;
33443ba16761SJacob Faibussowitsch if (PCHPDDMPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
3345f1580f4eSBarry Smith PCHPDDMPackageInitialized = PETSC_TRUE;
3346f1580f4eSBarry Smith PetscCall(PetscRegisterFinalize(PCHPDDMFinalizePackage));
3347f1580f4eSBarry Smith /* general events registered once during package initialization */
3348f1580f4eSBarry Smith /* some of these events are not triggered in libpetsc, */
3349f1580f4eSBarry Smith /* but rather directly in libhpddm_petsc, */
3350f1580f4eSBarry Smith /* which is in charge of performing the following operations */
3351f1580f4eSBarry Smith
3352f1580f4eSBarry Smith /* domain decomposition structure from Pmat sparsity pattern */
3353f1580f4eSBarry Smith PetscCall(PetscLogEventRegister("PCHPDDMStrc", PC_CLASSID, &PC_HPDDM_Strc));
3354f1580f4eSBarry Smith /* Galerkin product, redistribution, and setup (not triggered in libpetsc) */
3355f1580f4eSBarry Smith PetscCall(PetscLogEventRegister("PCHPDDMPtAP", PC_CLASSID, &PC_HPDDM_PtAP));
3356f1580f4eSBarry Smith /* Galerkin product with summation, redistribution, and setup (not triggered in libpetsc) */
3357f1580f4eSBarry Smith PetscCall(PetscLogEventRegister("PCHPDDMPtBP", PC_CLASSID, &PC_HPDDM_PtBP));
3358f1580f4eSBarry Smith /* next level construction using PtAP and PtBP (not triggered in libpetsc) */
3359f1580f4eSBarry Smith PetscCall(PetscLogEventRegister("PCHPDDMNext", PC_CLASSID, &PC_HPDDM_Next));
3360f1580f4eSBarry Smith static_assert(PETSC_PCHPDDM_MAXLEVELS <= 9, "PETSC_PCHPDDM_MAXLEVELS value is too high");
3361811e8887SPierre Jolivet for (PetscInt i = 1; i < PETSC_PCHPDDM_MAXLEVELS; ++i) {
3362f1580f4eSBarry Smith PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCHPDDMSetUp L%1" PetscInt_FMT, i));
3363f1580f4eSBarry Smith /* events during a PCSetUp() at level #i _except_ the assembly */
3364f1580f4eSBarry Smith /* of the Galerkin operator of the coarser level #(i + 1) */
3365f1580f4eSBarry Smith PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &PC_HPDDM_SetUp[i - 1]));
3366f1580f4eSBarry Smith PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCHPDDMSolve L%1" PetscInt_FMT, i));
3367f1580f4eSBarry Smith /* events during a PCApply() at level #i _except_ */
3368f1580f4eSBarry Smith /* the KSPSolve() of the coarser level #(i + 1) */
3369f1580f4eSBarry Smith PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &PC_HPDDM_Solve[i - 1]));
3370f1580f4eSBarry Smith }
33713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3372f1580f4eSBarry Smith }
3373f1580f4eSBarry Smith
3374f1580f4eSBarry Smith /*@C
3375f1580f4eSBarry Smith PCHPDDMFinalizePackage - This function frees everything from the `PCHPDDM` package. It is called from `PetscFinalize()`.
3376f1580f4eSBarry Smith
3377f1580f4eSBarry Smith Level: developer
3378f1580f4eSBarry Smith
3379562efe2eSBarry Smith .seealso: [](ch_ksp), `PetscFinalize()`
3380f1580f4eSBarry Smith @*/
PCHPDDMFinalizePackage(void)3381d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMFinalizePackage(void)
3382d71ae5a4SJacob Faibussowitsch {
3383f1580f4eSBarry Smith PetscFunctionBegin;
3384f1580f4eSBarry Smith PCHPDDMPackageInitialized = PETSC_FALSE;
33853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS);
3386f1580f4eSBarry Smith }
33879bb5c669SPierre Jolivet
MatMult_Harmonic(Mat A,Vec x,Vec y)33889bb5c669SPierre Jolivet static PetscErrorCode MatMult_Harmonic(Mat A, Vec x, Vec y)
33899bb5c669SPierre Jolivet {
33909bb5c669SPierre Jolivet Harmonic h; /* [ A_00 A_01 ], furthermore, A_00 = [ A_loc,loc A_loc,ovl ], thus, A_01 = [ ] */
33919bb5c669SPierre Jolivet /* [ A_10 A_11 A_12 ] [ A_ovl,loc A_ovl,ovl ] [ A_ovl,1 ] */
33929bb5c669SPierre Jolivet Vec sub; /* y = A x = R_loc R_0 [ A_00 A_01 ]^-1 R_loc = [ I_loc ] */
33939bb5c669SPierre Jolivet /* [ A_10 A_11 ] R_1^T A_12 x [ ] */
33949bb5c669SPierre Jolivet PetscFunctionBegin;
33959bb5c669SPierre Jolivet PetscCall(MatShellGetContext(A, &h));
33969bb5c669SPierre Jolivet PetscCall(VecSet(h->v, 0.0));
33979bb5c669SPierre Jolivet PetscCall(VecGetSubVector(h->v, h->is[0], &sub));
33989bb5c669SPierre Jolivet PetscCall(MatMult(h->A[0], x, sub));
33999bb5c669SPierre Jolivet PetscCall(VecRestoreSubVector(h->v, h->is[0], &sub));
34009bb5c669SPierre Jolivet PetscCall(KSPSolve(h->ksp, h->v, h->v));
34019bb5c669SPierre Jolivet PetscCall(VecISCopy(h->v, h->is[1], SCATTER_REVERSE, y));
34029bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
34039bb5c669SPierre Jolivet }
34049bb5c669SPierre Jolivet
MatMultTranspose_Harmonic(Mat A,Vec y,Vec x)34059bb5c669SPierre Jolivet static PetscErrorCode MatMultTranspose_Harmonic(Mat A, Vec y, Vec x)
34069bb5c669SPierre Jolivet {
34079bb5c669SPierre Jolivet Harmonic h; /* x = A^T y = [ A_00 A_01 ]^-T R_0^T R_loc^T y */
34089bb5c669SPierre Jolivet Vec sub; /* A_12^T R_1 [ A_10 A_11 ] */
34099bb5c669SPierre Jolivet
34109bb5c669SPierre Jolivet PetscFunctionBegin;
34119bb5c669SPierre Jolivet PetscCall(MatShellGetContext(A, &h));
34129bb5c669SPierre Jolivet PetscCall(VecSet(h->v, 0.0));
34139bb5c669SPierre Jolivet PetscCall(VecISCopy(h->v, h->is[1], SCATTER_FORWARD, y));
34149bb5c669SPierre Jolivet PetscCall(KSPSolveTranspose(h->ksp, h->v, h->v));
34159bb5c669SPierre Jolivet PetscCall(VecGetSubVector(h->v, h->is[0], &sub));
34169bb5c669SPierre Jolivet PetscCall(MatMultTranspose(h->A[0], sub, x));
34179bb5c669SPierre Jolivet PetscCall(VecRestoreSubVector(h->v, h->is[0], &sub));
34189bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
34199bb5c669SPierre Jolivet }
34209bb5c669SPierre Jolivet
MatProduct_AB_Harmonic(Mat S,Mat X,Mat Y,void *)34219bb5c669SPierre Jolivet static PetscErrorCode MatProduct_AB_Harmonic(Mat S, Mat X, Mat Y, void *)
34229bb5c669SPierre Jolivet {
34239bb5c669SPierre Jolivet Harmonic h;
34249bb5c669SPierre Jolivet Mat A, B;
34259bb5c669SPierre Jolivet Vec a, b;
34269bb5c669SPierre Jolivet
34279bb5c669SPierre Jolivet PetscFunctionBegin;
34289bb5c669SPierre Jolivet PetscCall(MatShellGetContext(S, &h));
3429fb842aefSJose E. Roman PetscCall(MatMatMult(h->A[0], X, MAT_INITIAL_MATRIX, PETSC_CURRENT, &A));
34309bb5c669SPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, h->ksp->pc->mat->rmap->n, A->cmap->n, nullptr, &B));
34319bb5c669SPierre Jolivet for (PetscInt i = 0; i < A->cmap->n; ++i) {
34329bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecRead(A, i, &a));
34339bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecWrite(B, i, &b));
34349bb5c669SPierre Jolivet PetscCall(VecISCopy(b, h->is[0], SCATTER_FORWARD, a));
34359bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecWrite(B, i, &b));
34369bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecRead(A, i, &a));
34379bb5c669SPierre Jolivet }
34389bb5c669SPierre Jolivet PetscCall(MatDestroy(&A));
34399bb5c669SPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, h->ksp->pc->mat->rmap->n, B->cmap->n, nullptr, &A));
34409bb5c669SPierre Jolivet PetscCall(KSPMatSolve(h->ksp, B, A));
34419bb5c669SPierre Jolivet PetscCall(MatDestroy(&B));
34429bb5c669SPierre Jolivet for (PetscInt i = 0; i < A->cmap->n; ++i) {
34439bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecRead(A, i, &a));
34449bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecWrite(Y, i, &b));
34459bb5c669SPierre Jolivet PetscCall(VecISCopy(a, h->is[1], SCATTER_REVERSE, b));
34469bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &b));
34479bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecRead(A, i, &a));
34489bb5c669SPierre Jolivet }
34499bb5c669SPierre Jolivet PetscCall(MatDestroy(&A));
34509bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
34519bb5c669SPierre Jolivet }
34529bb5c669SPierre Jolivet
MatProduct_AtB_Harmonic(Mat S,Mat Y,Mat X,void *)3453f21e3f8aSPierre Jolivet static PetscErrorCode MatProduct_AtB_Harmonic(Mat S, Mat Y, Mat X, void *)
3454f21e3f8aSPierre Jolivet {
3455f21e3f8aSPierre Jolivet Harmonic h;
3456f21e3f8aSPierre Jolivet Mat A, B;
3457f21e3f8aSPierre Jolivet Vec a, b;
3458f21e3f8aSPierre Jolivet
3459f21e3f8aSPierre Jolivet PetscFunctionBegin;
3460f21e3f8aSPierre Jolivet PetscCall(MatShellGetContext(S, &h));
3461f21e3f8aSPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, h->ksp->pc->mat->rmap->n, Y->cmap->n, nullptr, &A));
3462f21e3f8aSPierre Jolivet for (PetscInt i = 0; i < A->cmap->n; ++i) {
3463f21e3f8aSPierre Jolivet PetscCall(MatDenseGetColumnVecRead(Y, i, &b));
3464f21e3f8aSPierre Jolivet PetscCall(MatDenseGetColumnVecWrite(A, i, &a));
3465f21e3f8aSPierre Jolivet PetscCall(VecISCopy(a, h->is[1], SCATTER_FORWARD, b));
3466f21e3f8aSPierre Jolivet PetscCall(MatDenseRestoreColumnVecWrite(A, i, &a));
3467f21e3f8aSPierre Jolivet PetscCall(MatDenseRestoreColumnVecRead(Y, i, &b));
3468f21e3f8aSPierre Jolivet }
3469f21e3f8aSPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, h->ksp->pc->mat->rmap->n, A->cmap->n, nullptr, &B));
3470f21e3f8aSPierre Jolivet PetscCall(KSPMatSolveTranspose(h->ksp, A, B));
3471f21e3f8aSPierre Jolivet PetscCall(MatDestroy(&A));
3472f21e3f8aSPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, h->A[0]->rmap->n, B->cmap->n, nullptr, &A));
3473f21e3f8aSPierre Jolivet for (PetscInt i = 0; i < A->cmap->n; ++i) {
3474f21e3f8aSPierre Jolivet PetscCall(MatDenseGetColumnVecRead(B, i, &b));
3475f21e3f8aSPierre Jolivet PetscCall(MatDenseGetColumnVecWrite(A, i, &a));
3476f21e3f8aSPierre Jolivet PetscCall(VecISCopy(b, h->is[0], SCATTER_REVERSE, a));
3477f21e3f8aSPierre Jolivet PetscCall(MatDenseRestoreColumnVecWrite(A, i, &a));
3478f21e3f8aSPierre Jolivet PetscCall(MatDenseRestoreColumnVecRead(B, i, &b));
3479f21e3f8aSPierre Jolivet }
3480f21e3f8aSPierre Jolivet PetscCall(MatDestroy(&B));
3481f21e3f8aSPierre Jolivet PetscCall(MatTransposeMatMult(h->A[0], A, MAT_REUSE_MATRIX, PETSC_CURRENT, &X));
3482f21e3f8aSPierre Jolivet PetscCall(MatDestroy(&A));
3483f21e3f8aSPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
3484f21e3f8aSPierre Jolivet }
3485f21e3f8aSPierre Jolivet
MatDestroy_Harmonic(Mat A)34869bb5c669SPierre Jolivet static PetscErrorCode MatDestroy_Harmonic(Mat A)
34879bb5c669SPierre Jolivet {
34889bb5c669SPierre Jolivet Harmonic h;
34899bb5c669SPierre Jolivet
34909bb5c669SPierre Jolivet PetscFunctionBegin;
34919bb5c669SPierre Jolivet PetscCall(MatShellGetContext(A, &h));
349232603206SJames Wright for (PetscInt i = 0; i < 5; ++i) PetscCall(ISDestroy(h->is + i));
34939bb5c669SPierre Jolivet PetscCall(PetscFree(h->is));
34949bb5c669SPierre Jolivet PetscCall(VecDestroy(&h->v));
34959bb5c669SPierre Jolivet for (PetscInt i = 0; i < 2; ++i) PetscCall(MatDestroy(h->A + i));
34969bb5c669SPierre Jolivet PetscCall(PetscFree(h->A));
34979bb5c669SPierre Jolivet PetscCall(KSPDestroy(&h->ksp));
34989bb5c669SPierre Jolivet PetscCall(PetscFree(h));
34999bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS);
35009bb5c669SPierre Jolivet }
3501