113044ca3SPierre Jolivet #include <petsc/private/vecimpl.h> 2f1580f4eSBarry Smith #include <petsc/private/matimpl.h> 3f1580f4eSBarry Smith #include <petsc/private/petschpddm.h> /*I "petscpc.h" I*/ 4e905f78bSJacob Faibussowitsch #include <petsc/private/pcimpl.h> 5e905f78bSJacob Faibussowitsch #include <petsc/private/dmimpl.h> /* this must be included after petschpddm.h so that DM_MAX_WORK_VECTORS is not defined */ 6f1580f4eSBarry Smith /* otherwise, it is assumed that one is compiling libhpddm_petsc => circular dependency */ 7fbf9dbe5SBarry Smith #if PetscDefined(USE_FORTRAN_BINDINGS) 8f1580f4eSBarry Smith #include <petsc/private/fortranimpl.h> 9f1580f4eSBarry Smith #endif 10f1580f4eSBarry Smith 11db4a47b3SPierre Jolivet static PetscErrorCode (*loadedSym)(HPDDM::Schwarz<PetscScalar> *const, IS, Mat, Mat, Mat, std::vector<Vec>, PC_HPDDM_Level **const) = nullptr; 12f1580f4eSBarry Smith 13f1580f4eSBarry Smith static PetscBool PCHPDDMPackageInitialized = PETSC_FALSE; 14f1580f4eSBarry Smith 15f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_Strc; 16f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_PtAP; 17f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_PtBP; 18f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_Next; 19f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_SetUp[PETSC_PCHPDDM_MAXLEVELS]; 20f1580f4eSBarry Smith PetscLogEvent PC_HPDDM_Solve[PETSC_PCHPDDM_MAXLEVELS]; 21f1580f4eSBarry Smith 22db4a47b3SPierre Jolivet const char *const PCHPDDMCoarseCorrectionTypes[] = {"DEFLATED", "ADDITIVE", "BALANCED", "PCHPDDMCoarseCorrectionType", "PC_HPDDM_COARSE_CORRECTION_", nullptr}; 2313044ca3SPierre Jolivet const char *const PCHPDDMSchurPreTypes[] = {"LEAST_SQUARES", "GENEO", "PCHPDDMSchurPreType", "PC_HPDDM_SCHUR_PRE", nullptr}; 24f1580f4eSBarry Smith 25d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCReset_HPDDM(PC pc) 26d71ae5a4SJacob Faibussowitsch { 27f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 28f1580f4eSBarry Smith PetscInt i; 29f1580f4eSBarry Smith 30f1580f4eSBarry Smith PetscFunctionBegin; 31f1580f4eSBarry Smith if (data->levels) { 32f1580f4eSBarry Smith for (i = 0; i < PETSC_PCHPDDM_MAXLEVELS && data->levels[i]; ++i) { 33f1580f4eSBarry Smith PetscCall(KSPDestroy(&data->levels[i]->ksp)); 34f1580f4eSBarry Smith PetscCall(PCDestroy(&data->levels[i]->pc)); 35f1580f4eSBarry Smith PetscCall(PetscFree(data->levels[i])); 36f1580f4eSBarry Smith } 37f1580f4eSBarry Smith PetscCall(PetscFree(data->levels)); 38f1580f4eSBarry Smith } 39f1580f4eSBarry Smith 40f1580f4eSBarry Smith PetscCall(ISDestroy(&data->is)); 41f1580f4eSBarry Smith PetscCall(MatDestroy(&data->aux)); 42f1580f4eSBarry Smith PetscCall(MatDestroy(&data->B)); 43f1580f4eSBarry Smith PetscCall(VecDestroy(&data->normal)); 44f1580f4eSBarry Smith data->correction = PC_HPDDM_COARSE_CORRECTION_DEFLATED; 45c8ea6600SPierre Jolivet data->Neumann = PETSC_BOOL3_UNKNOWN; 46f1580f4eSBarry Smith data->deflation = PETSC_FALSE; 47db4a47b3SPierre Jolivet data->setup = nullptr; 48db4a47b3SPierre Jolivet data->setup_ctx = nullptr; 493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 50f1580f4eSBarry Smith } 51f1580f4eSBarry Smith 52d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_HPDDM(PC pc) 53d71ae5a4SJacob Faibussowitsch { 54f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 55f1580f4eSBarry Smith 56f1580f4eSBarry Smith PetscFunctionBegin; 57f1580f4eSBarry Smith PetscCall(PCReset_HPDDM(pc)); 58f1580f4eSBarry Smith PetscCall(PetscFree(data)); 59db4a47b3SPierre Jolivet PetscCall(PetscObjectChangeTypeName((PetscObject)pc, nullptr)); 60db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetAuxiliaryMat_C", nullptr)); 61db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMHasNeumannMat_C", nullptr)); 62db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetRHSMat_C", nullptr)); 63db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetCoarseCorrectionType_C", nullptr)); 64db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetCoarseCorrectionType_C", nullptr)); 65db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetSTShareSubKSP_C", nullptr)); 66db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetSTShareSubKSP_C", nullptr)); 67db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetDeflationMat_C", nullptr)); 6813044ca3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)pc, "_PCHPDDM_Schur", nullptr)); 693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 70f1580f4eSBarry Smith } 71f1580f4eSBarry Smith 72d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PCHPDDMSetAuxiliaryMat_Private(PC pc, IS is, Mat A, PetscBool deflation) 73d71ae5a4SJacob Faibussowitsch { 74f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 75f1580f4eSBarry Smith 76f1580f4eSBarry Smith PetscFunctionBegin; 77f1580f4eSBarry Smith if (is) { 78f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)is)); 79f1580f4eSBarry Smith if (data->is) { /* new overlap definition resets the PC */ 80f1580f4eSBarry Smith PetscCall(PCReset_HPDDM(pc)); 81f1580f4eSBarry Smith pc->setfromoptionscalled = 0; 82b853e353SPierre Jolivet pc->setupcalled = 0; 83f1580f4eSBarry Smith } 84f1580f4eSBarry Smith PetscCall(ISDestroy(&data->is)); 85f1580f4eSBarry Smith data->is = is; 86f1580f4eSBarry Smith } 87f1580f4eSBarry Smith if (A) { 88f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)A)); 89f1580f4eSBarry Smith PetscCall(MatDestroy(&data->aux)); 90f1580f4eSBarry Smith data->aux = A; 91f1580f4eSBarry Smith } 92f1580f4eSBarry Smith data->deflation = deflation; 933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 94f1580f4eSBarry Smith } 95f1580f4eSBarry Smith 963df4cd7bSPierre Jolivet static inline PetscErrorCode PCHPDDMSetAuxiliaryMatNormal_Private(PC pc, Mat A, Mat N, Mat *B, const char *pcpre, Vec *diagonal = nullptr) 97d71ae5a4SJacob Faibussowitsch { 98f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 99f1580f4eSBarry Smith Mat *splitting, *sub, aux; 1003df4cd7bSPierre Jolivet Vec d; 101f1580f4eSBarry Smith IS is, cols[2], rows; 102f1580f4eSBarry Smith PetscReal norm; 103f1580f4eSBarry Smith PetscBool flg; 104f1580f4eSBarry Smith char type[256] = {}; /* same size as in src/ksp/pc/interface/pcset.c */ 105f1580f4eSBarry Smith 106f1580f4eSBarry Smith PetscFunctionBegin; 107f1580f4eSBarry Smith PetscCall(MatConvert(N, MATAIJ, MAT_INITIAL_MATRIX, B)); 108f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, A->cmap->n, A->cmap->rstart, 1, cols)); 109f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, A->rmap->N, 0, 1, &rows)); 110f1580f4eSBarry Smith PetscCall(MatSetOption(A, MAT_SUBMAT_SINGLEIS, PETSC_TRUE)); 111f1580f4eSBarry Smith PetscCall(MatIncreaseOverlap(*B, 1, cols, 1)); 112f1580f4eSBarry Smith PetscCall(MatCreateSubMatrices(A, 1, &rows, cols, MAT_INITIAL_MATRIX, &splitting)); 113f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, A->cmap->n, A->cmap->rstart, 1, &is)); 114f1580f4eSBarry Smith PetscCall(ISEmbed(*cols, is, PETSC_TRUE, cols + 1)); 115f1580f4eSBarry Smith PetscCall(ISDestroy(&is)); 116f1580f4eSBarry Smith PetscCall(MatCreateSubMatrices(*splitting, 1, &rows, cols + 1, MAT_INITIAL_MATRIX, &sub)); 117f1580f4eSBarry Smith PetscCall(ISDestroy(cols + 1)); 118f1580f4eSBarry Smith PetscCall(MatFindZeroRows(*sub, &is)); 119f1580f4eSBarry Smith PetscCall(MatDestroySubMatrices(1, &sub)); 120f1580f4eSBarry Smith PetscCall(ISDestroy(&rows)); 121f1580f4eSBarry Smith PetscCall(MatSetOption(*splitting, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE)); 122db4a47b3SPierre Jolivet PetscCall(MatZeroRowsIS(*splitting, is, 0.0, nullptr, nullptr)); 123f1580f4eSBarry Smith PetscCall(ISDestroy(&is)); 124db4a47b3SPierre Jolivet PetscCall(PetscOptionsGetString(nullptr, pcpre, "-pc_hpddm_levels_1_sub_pc_type", type, sizeof(type), nullptr)); 125f1580f4eSBarry Smith PetscCall(PetscStrcmp(type, PCQR, &flg)); 126f1580f4eSBarry Smith if (!flg) { 127f1580f4eSBarry Smith Mat conjugate = *splitting; 128f1580f4eSBarry Smith if (PetscDefined(USE_COMPLEX)) { 129f1580f4eSBarry Smith PetscCall(MatDuplicate(*splitting, MAT_COPY_VALUES, &conjugate)); 130f1580f4eSBarry Smith PetscCall(MatConjugate(conjugate)); 131f1580f4eSBarry Smith } 132f1580f4eSBarry Smith PetscCall(MatTransposeMatMult(conjugate, *splitting, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &aux)); 133f1580f4eSBarry Smith if (PetscDefined(USE_COMPLEX)) PetscCall(MatDestroy(&conjugate)); 134f1580f4eSBarry Smith PetscCall(MatNorm(aux, NORM_FROBENIUS, &norm)); 135f1580f4eSBarry Smith PetscCall(MatSetOption(aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE)); 1363df4cd7bSPierre Jolivet if (diagonal) { 1373df4cd7bSPierre Jolivet PetscCall(VecNorm(*diagonal, NORM_INFINITY, &norm)); 1383df4cd7bSPierre Jolivet if (norm > PETSC_SMALL) { 1393df4cd7bSPierre Jolivet VecScatter scatter; 1403df4cd7bSPierre Jolivet PetscInt n; 1413df4cd7bSPierre Jolivet PetscCall(ISGetLocalSize(*cols, &n)); 1423df4cd7bSPierre Jolivet PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)pc), n, PETSC_DECIDE, &d)); 1433df4cd7bSPierre Jolivet PetscCall(VecScatterCreate(*diagonal, *cols, d, nullptr, &scatter)); 1443df4cd7bSPierre Jolivet PetscCall(VecScatterBegin(scatter, *diagonal, d, INSERT_VALUES, SCATTER_FORWARD)); 1453df4cd7bSPierre Jolivet PetscCall(VecScatterEnd(scatter, *diagonal, d, INSERT_VALUES, SCATTER_FORWARD)); 1463df4cd7bSPierre Jolivet PetscCall(VecScatterDestroy(&scatter)); 1473df4cd7bSPierre Jolivet PetscCall(MatScale(aux, -1.0)); 1483df4cd7bSPierre Jolivet PetscCall(MatDiagonalSet(aux, d, ADD_VALUES)); 1493df4cd7bSPierre Jolivet PetscCall(VecDestroy(&d)); 1503df4cd7bSPierre Jolivet } else PetscCall(VecDestroy(diagonal)); 1513df4cd7bSPierre Jolivet } 1523df4cd7bSPierre Jolivet if (!diagonal) PetscCall(MatShift(aux, PETSC_SMALL * norm)); 153f1580f4eSBarry Smith } else { 154f1580f4eSBarry Smith PetscBool flg; 1553df4cd7bSPierre Jolivet if (diagonal) { 1563df4cd7bSPierre Jolivet PetscCall(VecNorm(*diagonal, NORM_INFINITY, &norm)); 1573df4cd7bSPierre Jolivet PetscCheck(norm < PETSC_SMALL, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Nonzero diagonal A11 block"); 1583df4cd7bSPierre Jolivet PetscCall(VecDestroy(diagonal)); 1593df4cd7bSPierre Jolivet } 160f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)N, MATNORMAL, &flg)); 161f1580f4eSBarry Smith if (flg) PetscCall(MatCreateNormal(*splitting, &aux)); 162f1580f4eSBarry Smith else PetscCall(MatCreateNormalHermitian(*splitting, &aux)); 163f1580f4eSBarry Smith } 164f1580f4eSBarry Smith PetscCall(MatDestroySubMatrices(1, &splitting)); 165db4a47b3SPierre Jolivet PetscCall(PCHPDDMSetAuxiliaryMat(pc, *cols, aux, nullptr, nullptr)); 166c8ea6600SPierre Jolivet data->Neumann = PETSC_BOOL3_TRUE; 167f1580f4eSBarry Smith PetscCall(ISDestroy(cols)); 168f1580f4eSBarry Smith PetscCall(MatDestroy(&aux)); 1693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 170f1580f4eSBarry Smith } 171f1580f4eSBarry Smith 172d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetAuxiliaryMat_HPDDM(PC pc, IS is, Mat A, PetscErrorCode (*setup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void *setup_ctx) 173d71ae5a4SJacob Faibussowitsch { 174f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 175f1580f4eSBarry Smith 176f1580f4eSBarry Smith PetscFunctionBegin; 177f1580f4eSBarry Smith PetscCall(PCHPDDMSetAuxiliaryMat_Private(pc, is, A, PETSC_FALSE)); 178f1580f4eSBarry Smith if (setup) { 179f1580f4eSBarry Smith data->setup = setup; 180f1580f4eSBarry Smith data->setup_ctx = setup_ctx; 181f1580f4eSBarry Smith } 1823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 183f1580f4eSBarry Smith } 184f1580f4eSBarry Smith 18570009435SPierre Jolivet /*@C 18604c3f3b8SBarry Smith PCHPDDMSetAuxiliaryMat - Sets the auxiliary matrix used by `PCHPDDM` for the concurrent GenEO problems at the finest level. 187f1580f4eSBarry Smith 188f1580f4eSBarry Smith Input Parameters: 189f1580f4eSBarry Smith + pc - preconditioner context 190f1580f4eSBarry Smith . is - index set of the local auxiliary, e.g., Neumann, matrix 191f1580f4eSBarry Smith . A - auxiliary sequential matrix 19204c3f3b8SBarry Smith . setup - function for generating the auxiliary matrix entries, may be `NULL` 19304c3f3b8SBarry Smith - ctx - context for `setup`, may be `NULL` 19404c3f3b8SBarry Smith 19504c3f3b8SBarry Smith Calling sequence of `setup`: 19604c3f3b8SBarry Smith + J - matrix whose values are to be set 19704c3f3b8SBarry Smith . t - time 19804c3f3b8SBarry Smith . X - linearization point 19904c3f3b8SBarry Smith . X_t - time-derivative of the linearization point 20004c3f3b8SBarry Smith . s - step 20104c3f3b8SBarry Smith . ovl - index set of the local auxiliary, e.g., Neumann, matrix 20204c3f3b8SBarry Smith - ctx - context for `setup`, may be `NULL` 203f1580f4eSBarry Smith 204f1580f4eSBarry Smith Level: intermediate 205f1580f4eSBarry Smith 20604c3f3b8SBarry Smith Note: 20704c3f3b8SBarry Smith As an example, in a finite element context with nonoverlapping subdomains plus (overlapping) ghost elements, this could be the unassembled (Neumann) 20804c3f3b8SBarry Smith local overlapping operator. As opposed to the assembled (Dirichlet) local overlapping operator obtained by summing neighborhood contributions 20904c3f3b8SBarry Smith at the interface of ghost elements. 21004c3f3b8SBarry Smith 21170009435SPierre Jolivet Fortran Notes: 21204c3f3b8SBarry Smith Only `PETSC_NULL_FUNCTION` is supported for `setup` and `ctx` is never accessed 21370009435SPierre Jolivet 214f1580f4eSBarry Smith .seealso: `PCHPDDM`, `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCHPDDMSetRHSMat()`, `MATIS` 215f1580f4eSBarry Smith @*/ 21604c3f3b8SBarry Smith PetscErrorCode PCHPDDMSetAuxiliaryMat(PC pc, IS is, Mat A, PetscErrorCode (*setup)(Mat J, PetscReal t, Vec X, Vec X_t, PetscReal s, IS ovl, void *ctx), void *ctx) 217d71ae5a4SJacob Faibussowitsch { 218f1580f4eSBarry Smith PetscFunctionBegin; 219f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 220f1580f4eSBarry Smith if (is) PetscValidHeaderSpecific(is, IS_CLASSID, 2); 221f1580f4eSBarry Smith if (A) PetscValidHeaderSpecific(A, MAT_CLASSID, 3); 22204c3f3b8SBarry Smith PetscTryMethod(pc, "PCHPDDMSetAuxiliaryMat_C", (PC, IS, Mat, PetscErrorCode(*)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void *), (pc, is, A, setup, ctx)); 2233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 224f1580f4eSBarry Smith } 225f1580f4eSBarry Smith 226d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMHasNeumannMat_HPDDM(PC pc, PetscBool has) 227d71ae5a4SJacob Faibussowitsch { 228f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 229f1580f4eSBarry Smith 230f1580f4eSBarry Smith PetscFunctionBegin; 231c8ea6600SPierre Jolivet data->Neumann = PetscBoolToBool3(has); 2323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 233f1580f4eSBarry Smith } 234f1580f4eSBarry Smith 235f1580f4eSBarry Smith /*@ 236f1580f4eSBarry Smith PCHPDDMHasNeumannMat - Informs `PCHPDDM` that the `Mat` passed to `PCHPDDMSetAuxiliaryMat()` is the local Neumann matrix. 237f1580f4eSBarry Smith 238f1580f4eSBarry Smith Input Parameters: 239f1580f4eSBarry Smith + pc - preconditioner context 240f1580f4eSBarry Smith - has - Boolean value 241f1580f4eSBarry Smith 242f1580f4eSBarry Smith Level: intermediate 243f1580f4eSBarry Smith 244f1580f4eSBarry Smith Notes: 2457eb095acSPierre Jolivet This may be used to bypass a call to `MatCreateSubMatrices()` and to `MatConvert()` for `MATSBAIJ` matrices. 246f1580f4eSBarry Smith 247f1580f4eSBarry Smith If a `DMCreateNeumannOverlap()` implementation is available in the `DM` attached to the Pmat, or the Amat, or the `PC`, the flag is internally set to `PETSC_TRUE`. Its default value is otherwise `PETSC_FALSE`. 248f1580f4eSBarry Smith 249f1580f4eSBarry Smith .seealso: `PCHPDDM`, `PCHPDDMSetAuxiliaryMat()` 250f1580f4eSBarry Smith @*/ 251d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMHasNeumannMat(PC pc, PetscBool has) 252d71ae5a4SJacob Faibussowitsch { 253f1580f4eSBarry Smith PetscFunctionBegin; 254f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 255f1580f4eSBarry Smith PetscTryMethod(pc, "PCHPDDMHasNeumannMat_C", (PC, PetscBool), (pc, has)); 2563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 257f1580f4eSBarry Smith } 258f1580f4eSBarry Smith 259d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetRHSMat_HPDDM(PC pc, Mat B) 260d71ae5a4SJacob Faibussowitsch { 261f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 262f1580f4eSBarry Smith 263f1580f4eSBarry Smith PetscFunctionBegin; 264f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)B)); 265f1580f4eSBarry Smith PetscCall(MatDestroy(&data->B)); 266f1580f4eSBarry Smith data->B = B; 2673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 268f1580f4eSBarry Smith } 269f1580f4eSBarry Smith 270f1580f4eSBarry Smith /*@ 27104c3f3b8SBarry Smith PCHPDDMSetRHSMat - Sets the right-hand side matrix used by `PCHPDDM` for the concurrent GenEO problems at the finest level. 272f1580f4eSBarry Smith 273f1580f4eSBarry Smith Input Parameters: 274f1580f4eSBarry Smith + pc - preconditioner context 275f1580f4eSBarry Smith - B - right-hand side sequential matrix 276f1580f4eSBarry Smith 277f1580f4eSBarry Smith Level: advanced 278f1580f4eSBarry Smith 27904c3f3b8SBarry Smith Note: 28004c3f3b8SBarry Smith Must be used in conjunction with `PCHPDDMSetAuxiliaryMat`(N), so that Nv = lambda Bv is solved using `EPSSetOperators`(N, B). 28104c3f3b8SBarry 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. 28204c3f3b8SBarry Smith 283f1580f4eSBarry Smith .seealso: `PCHPDDMSetAuxiliaryMat()`, `PCHPDDM` 284f1580f4eSBarry Smith @*/ 285d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMSetRHSMat(PC pc, Mat B) 286d71ae5a4SJacob Faibussowitsch { 287f1580f4eSBarry Smith PetscFunctionBegin; 288f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 289f1580f4eSBarry Smith if (B) { 290f1580f4eSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 2); 291f1580f4eSBarry Smith PetscTryMethod(pc, "PCHPDDMSetRHSMat_C", (PC, Mat), (pc, B)); 292f1580f4eSBarry Smith } 2933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 294f1580f4eSBarry Smith } 295f1580f4eSBarry Smith 296d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetFromOptions_HPDDM(PC pc, PetscOptionItems *PetscOptionsObject) 297d71ae5a4SJacob Faibussowitsch { 298f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 299f1580f4eSBarry Smith PC_HPDDM_Level **levels = data->levels; 300f1580f4eSBarry Smith char prefix[256]; 301f1580f4eSBarry Smith int i = 1; 302f1580f4eSBarry Smith PetscMPIInt size, previous; 303*9bb5c669SPierre Jolivet PetscInt n, overlap = 1; 304f1580f4eSBarry Smith PCHPDDMCoarseCorrectionType type; 305c8ea6600SPierre Jolivet PetscBool flg = PETSC_TRUE, set; 306f1580f4eSBarry Smith 307f1580f4eSBarry Smith PetscFunctionBegin; 308f1580f4eSBarry Smith if (!data->levels) { 309f1580f4eSBarry Smith PetscCall(PetscCalloc1(PETSC_PCHPDDM_MAXLEVELS, &levels)); 310f1580f4eSBarry Smith data->levels = levels; 311f1580f4eSBarry Smith } 312f1580f4eSBarry Smith PetscOptionsHeadBegin(PetscOptionsObject, "PCHPDDM options"); 313*9bb5c669SPierre Jolivet PetscCall(PetscOptionsBoundedInt("-pc_hpddm_harmonic_overlap", "Overlap prior to computing local harmonic extensions", "PCHPDDM", overlap, &overlap, &set, 1)); 314*9bb5c669SPierre Jolivet if (!set) overlap = -1; 315f1580f4eSBarry Smith PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size)); 316f1580f4eSBarry Smith previous = size; 317f1580f4eSBarry Smith while (i < PETSC_PCHPDDM_MAXLEVELS) { 318f1580f4eSBarry Smith PetscInt p = 1; 319f1580f4eSBarry Smith 3204dfa11a4SJacob Faibussowitsch if (!data->levels[i - 1]) PetscCall(PetscNew(data->levels + i - 1)); 321f1580f4eSBarry Smith data->levels[i - 1]->parent = data; 322f1580f4eSBarry Smith /* if the previous level has a single process, it is not possible to coarsen further */ 323f1580f4eSBarry Smith if (previous == 1 || !flg) break; 324f1580f4eSBarry Smith data->levels[i - 1]->nu = 0; 325f1580f4eSBarry Smith data->levels[i - 1]->threshold = -1.0; 326f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_nev", i)); 327db4a47b3SPierre 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)); 328f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold", i)); 329db4a47b3SPierre Jolivet PetscCall(PetscOptionsReal(prefix, "Local threshold for selecting deflation vectors returned by SLEPc", "PCHPDDM", data->levels[i - 1]->threshold, &data->levels[i - 1]->threshold, nullptr)); 330f1580f4eSBarry Smith if (i == 1) { 331*9bb5c669SPierre Jolivet PetscCheck(overlap == -1 || PetscAbsReal(data->levels[i - 1]->threshold + PetscReal(1.0)) < PETSC_MACHINE_EPSILON, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot supply both -pc_hpddm_levels_1_eps_threshold and -pc_hpddm_harmonic_overlap"); 332*9bb5c669SPierre Jolivet if (overlap != -1) { 333*9bb5c669SPierre Jolivet PetscInt nsv = 0; 334*9bb5c669SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_svd_nsv", i)); 335*9bb5c669SPierre Jolivet PetscCall(PetscOptionsBoundedInt(prefix, "Local number of deflation vectors computed by SLEPc", "SVDSetDimensions", nsv, &nsv, nullptr, 0)); 336*9bb5c669SPierre Jolivet PetscCheck(bool(data->levels[0]->nu) != bool(nsv), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot supply %s -pc_hpddm_levels_1_eps_nev %s -pc_hpddm_levels_1_svd_nsv", nsv ? "both" : "neither", nsv ? "and" : "nor"); 337*9bb5c669SPierre Jolivet if (nsv) { 338*9bb5c669SPierre Jolivet data->levels[0]->nu = nsv; 339*9bb5c669SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_svd_relative_threshold", i)); 340*9bb5c669SPierre Jolivet } else PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_relative_threshold", i)); 341*9bb5c669SPierre Jolivet PetscCall(PetscOptionsReal(prefix, "Local relative threshold for selecting deflation vectors returned by SLEPc", "PCHPDDM", data->levels[0]->threshold, &data->levels[0]->threshold, nullptr)); 342*9bb5c669SPierre Jolivet } 343f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_1_st_share_sub_ksp")); 344db4a47b3SPierre Jolivet PetscCall(PetscOptionsBool(prefix, "Shared KSP between SLEPc ST and the fine-level subdomain solver", "PCHPDDMSetSTShareSubKSP", PETSC_FALSE, &data->share, nullptr)); 345f1580f4eSBarry Smith } 346f1580f4eSBarry Smith /* if there is no prescribed coarsening, just break out of the loop */ 3470594bca0SPierre Jolivet if (data->levels[i - 1]->threshold <= PetscReal() && data->levels[i - 1]->nu <= 0 && !(data->deflation && i == 1)) break; 348f1580f4eSBarry Smith else { 349f1580f4eSBarry Smith ++i; 350f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_nev", i)); 351f1580f4eSBarry Smith PetscCall(PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg)); 352f1580f4eSBarry Smith if (!flg) { 353f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_eps_threshold", i)); 354f1580f4eSBarry Smith PetscCall(PetscOptionsHasName(PetscOptionsObject->options, PetscOptionsObject->prefix, prefix, &flg)); 355f1580f4eSBarry Smith } 356f1580f4eSBarry Smith if (flg) { 357f1580f4eSBarry Smith /* if there are coarsening options for the next level, then register it */ 358f1580f4eSBarry Smith /* otherwise, don't to avoid having both options levels_N_p and coarse_p */ 359f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_levels_%d_p", i)); 360f1580f4eSBarry 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))); 361f1580f4eSBarry Smith previous = p; 362f1580f4eSBarry Smith } 363f1580f4eSBarry Smith } 364f1580f4eSBarry Smith } 365f1580f4eSBarry Smith data->N = i; 366f1580f4eSBarry Smith n = 1; 367f1580f4eSBarry Smith if (i > 1) { 368f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_coarse_p")); 369db4a47b3SPierre Jolivet PetscCall(PetscOptionsRangeInt(prefix, "Number of processes used to assemble the coarsest operator", "PCHPDDM", n, &n, nullptr, 1, PetscMax(1, previous / 2))); 37002800ff6SPierre Jolivet #if PetscDefined(HAVE_MUMPS) 371f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "pc_hpddm_coarse_")); 372db4a47b3SPierre Jolivet PetscCall(PetscOptionsHasName(nullptr, prefix, "-mat_mumps_use_omp_threads", &flg)); 373f1580f4eSBarry Smith if (flg) { 374f1580f4eSBarry Smith char type[64]; /* same size as in src/ksp/pc/impls/factor/factimpl.c */ 375c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(type, n > 1 && PetscDefined(HAVE_MUMPS) ? MATSOLVERMUMPS : MATSOLVERPETSC, sizeof(type))); /* default solver for a MatMPIAIJ or a MatSeqAIJ */ 376db4a47b3SPierre Jolivet PetscCall(PetscOptionsGetString(nullptr, prefix, "-pc_factor_mat_solver_type", type, sizeof(type), nullptr)); 3773ce573a3SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &flg)); 378f1580f4eSBarry 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); 379f1580f4eSBarry Smith size = n; 380f1580f4eSBarry Smith n = -1; 381db4a47b3SPierre Jolivet PetscCall(PetscOptionsGetInt(nullptr, prefix, "-mat_mumps_use_omp_threads", &n, nullptr)); 382f1580f4eSBarry Smith PetscCheck(n >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Need to specify a positive integer for -%smat_mumps_use_omp_threads", prefix); 383f1580f4eSBarry 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" : ""); 384f1580f4eSBarry Smith } 385f1580f4eSBarry Smith #endif 386f1580f4eSBarry Smith PetscCall(PetscOptionsEnum("-pc_hpddm_coarse_correction", "Type of coarse correction applied each iteration", "PCHPDDMSetCoarseCorrectionType", PCHPDDMCoarseCorrectionTypes, (PetscEnum)data->correction, (PetscEnum *)&type, &flg)); 387f1580f4eSBarry Smith if (flg) PetscCall(PCHPDDMSetCoarseCorrectionType(pc, type)); 388f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_has_neumann")); 389c8ea6600SPierre Jolivet PetscCall(PetscOptionsBool(prefix, "Is the auxiliary Mat the local Neumann matrix?", "PCHPDDMHasNeumannMat", PetscBool3ToBool(data->Neumann), &flg, &set)); 390c8ea6600SPierre Jolivet if (set) data->Neumann = PetscBoolToBool3(flg); 391f1580f4eSBarry Smith data->log_separate = PETSC_FALSE; 392f1580f4eSBarry Smith if (PetscDefined(USE_LOG)) { 393f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-pc_hpddm_log_separate")); 394db4a47b3SPierre Jolivet PetscCall(PetscOptionsBool(prefix, "Log events level by level instead of inside PCSetUp()/KSPSolve()", nullptr, data->log_separate, &data->log_separate, nullptr)); 395f1580f4eSBarry Smith } 396f1580f4eSBarry Smith } 397f1580f4eSBarry Smith PetscOptionsHeadEnd(); 398f1580f4eSBarry Smith while (i < PETSC_PCHPDDM_MAXLEVELS && data->levels[i]) PetscCall(PetscFree(data->levels[i++])); 3993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 400f1580f4eSBarry Smith } 401f1580f4eSBarry Smith 402d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_HPDDM(PC pc, Vec x, Vec y) 403d71ae5a4SJacob Faibussowitsch { 404f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 405f1580f4eSBarry Smith 406f1580f4eSBarry Smith PetscFunctionBegin; 407f1580f4eSBarry Smith PetscCall(PetscCitationsRegister(HPDDMCitation, &HPDDMCite)); 408f1580f4eSBarry Smith PetscCheck(data->levels[0]->ksp, PETSC_COMM_SELF, PETSC_ERR_PLIB, "No KSP attached to PCHPDDM"); 409db4a47b3SPierre 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 */ 410f1580f4eSBarry Smith PetscCall(KSPSolve(data->levels[0]->ksp, x, y)); 411db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_Solve[0], data->levels[0]->ksp, nullptr, nullptr, nullptr)); 4123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 413f1580f4eSBarry Smith } 414f1580f4eSBarry Smith 415d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_HPDDM(PC pc, Mat X, Mat Y) 416d71ae5a4SJacob Faibussowitsch { 417f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 418f1580f4eSBarry Smith 419f1580f4eSBarry Smith PetscFunctionBegin; 420f1580f4eSBarry Smith PetscCall(PetscCitationsRegister(HPDDMCitation, &HPDDMCite)); 421f1580f4eSBarry Smith PetscCheck(data->levels[0]->ksp, PETSC_COMM_SELF, PETSC_ERR_PLIB, "No KSP attached to PCHPDDM"); 422f1580f4eSBarry Smith PetscCall(KSPMatSolve(data->levels[0]->ksp, X, Y)); 4233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 424f1580f4eSBarry Smith } 425f1580f4eSBarry Smith 42604c3f3b8SBarry Smith // PetscClangLinter pragma disable: -fdoc-internal-linkage 427f1580f4eSBarry Smith /*@C 428f1580f4eSBarry Smith PCHPDDMGetComplexities - Computes the grid and operator complexities. 429f1580f4eSBarry Smith 430f1580f4eSBarry Smith Input Parameter: 431f1580f4eSBarry Smith . pc - preconditioner context 432f1580f4eSBarry Smith 433f1580f4eSBarry Smith Output Parameters: 434f1580f4eSBarry Smith + gc - grid complexity = sum_i(m_i) / m_1 435f1580f4eSBarry Smith - oc - operator complexity = sum_i(nnz_i) / nnz_1 436f1580f4eSBarry Smith 437f1580f4eSBarry Smith Level: advanced 438f1580f4eSBarry Smith 439f1580f4eSBarry Smith .seealso: `PCMGGetGridComplexity()`, `PCHPDDM`, `PCHYPRE`, `PCGAMG` 440f1580f4eSBarry Smith @*/ 441d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMGetComplexities(PC pc, PetscReal *gc, PetscReal *oc) 442d71ae5a4SJacob Faibussowitsch { 443f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 444f1580f4eSBarry Smith MatInfo info; 445f1580f4eSBarry Smith PetscInt n, m; 446f1580f4eSBarry Smith PetscLogDouble accumulate[2]{}, nnz1 = 1.0, m1 = 1.0; 447f1580f4eSBarry Smith 448f1580f4eSBarry Smith PetscFunctionBegin; 449f1580f4eSBarry Smith for (n = 0, *gc = 0, *oc = 0; n < data->N; ++n) { 450f1580f4eSBarry Smith if (data->levels[n]->ksp) { 45113044ca3SPierre Jolivet Mat P, A = nullptr; 45213044ca3SPierre Jolivet PetscBool flg = PETSC_FALSE; 45313044ca3SPierre Jolivet 454db4a47b3SPierre Jolivet PetscCall(KSPGetOperators(data->levels[n]->ksp, nullptr, &P)); 455db4a47b3SPierre Jolivet PetscCall(MatGetSize(P, &m, nullptr)); 456f1580f4eSBarry Smith accumulate[0] += m; 457f1580f4eSBarry Smith if (n == 0) { 458f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompareAny((PetscObject)P, &flg, MATNORMAL, MATNORMALHERMITIAN, "")); 459f1580f4eSBarry Smith if (flg) { 460f1580f4eSBarry Smith PetscCall(MatConvert(P, MATAIJ, MAT_INITIAL_MATRIX, &A)); 461f1580f4eSBarry Smith P = A; 46213044ca3SPierre Jolivet } else { 46313044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)P, MATSCHURCOMPLEMENT, &flg)); 46413044ca3SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)P)); 465f1580f4eSBarry Smith } 46613044ca3SPierre Jolivet } 46713044ca3SPierre Jolivet if (!A && flg) accumulate[1] += m * m; /* assumption that a MATSCHURCOMPLEMENT is dense if stored explicitly */ 46813044ca3SPierre Jolivet else if (P->ops->getinfo) { 469f1580f4eSBarry Smith PetscCall(MatGetInfo(P, MAT_GLOBAL_SUM, &info)); 470f1580f4eSBarry Smith accumulate[1] += info.nz_used; 471f1580f4eSBarry Smith } 472f1580f4eSBarry Smith if (n == 0) { 473f1580f4eSBarry Smith m1 = m; 47413044ca3SPierre Jolivet if (!A && flg) nnz1 = m * m; 47513044ca3SPierre Jolivet else if (P->ops->getinfo) nnz1 = info.nz_used; 476f1580f4eSBarry Smith PetscCall(MatDestroy(&P)); 477f1580f4eSBarry Smith } 478f1580f4eSBarry Smith } 479f1580f4eSBarry Smith } 480f1580f4eSBarry Smith *gc = static_cast<PetscReal>(accumulate[0] / m1); 481f1580f4eSBarry Smith *oc = static_cast<PetscReal>(accumulate[1] / nnz1); 4823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 483f1580f4eSBarry Smith } 484f1580f4eSBarry Smith 485d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_HPDDM(PC pc, PetscViewer viewer) 486d71ae5a4SJacob Faibussowitsch { 487f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 488f1580f4eSBarry Smith PetscViewer subviewer; 489aa21023fSPierre Jolivet PetscViewerFormat format; 490f1580f4eSBarry Smith PetscSubcomm subcomm; 491f1580f4eSBarry Smith PetscReal oc, gc; 492f1580f4eSBarry Smith PetscInt i, tabs; 493f1580f4eSBarry Smith PetscMPIInt size, color, rank; 494aa21023fSPierre Jolivet PetscBool flg; 495aa21023fSPierre Jolivet const char *name; 496f1580f4eSBarry Smith 497f1580f4eSBarry Smith PetscFunctionBegin; 498aa21023fSPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &flg)); 499aa21023fSPierre Jolivet if (flg) { 500f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "level%s: %" PetscInt_FMT "\n", data->N > 1 ? "s" : "", data->N)); 501f1580f4eSBarry Smith PetscCall(PCHPDDMGetComplexities(pc, &gc, &oc)); 502f1580f4eSBarry Smith if (data->N > 1) { 503f1580f4eSBarry Smith if (!data->deflation) { 504c8ea6600SPierre Jolivet PetscCall(PetscViewerASCIIPrintf(viewer, "Neumann matrix attached? %s\n", PetscBools[PetscBool3ToBool(data->Neumann)])); 505f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "shared subdomain KSP between SLEPc and PETSc? %s\n", PetscBools[data->share])); 506f1580f4eSBarry Smith } else PetscCall(PetscViewerASCIIPrintf(viewer, "user-supplied deflation matrix\n")); 507f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "coarse correction: %s\n", PCHPDDMCoarseCorrectionTypes[data->correction])); 508f1580f4eSBarry 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" : "")); 509f1580f4eSBarry Smith PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 510f1580f4eSBarry Smith PetscCall(PetscViewerASCIISetTab(viewer, 0)); 511f1580f4eSBarry Smith for (i = 1; i < data->N; ++i) { 512f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, data->levels[i - 1]->nu)); 5130594bca0SPierre Jolivet if (data->levels[i - 1]->threshold > static_cast<PetscReal>(-0.1)) PetscCall(PetscViewerASCIIPrintf(viewer, " (%g)", (double)data->levels[i - 1]->threshold)); 514f1580f4eSBarry Smith } 515f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "\n")); 516f1580f4eSBarry Smith PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 517f1580f4eSBarry Smith } 518f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "grid and operator complexities: %g %g\n", (double)gc, (double)oc)); 519f1580f4eSBarry Smith if (data->levels[0]->ksp) { 520f1580f4eSBarry Smith PetscCall(KSPView(data->levels[0]->ksp, viewer)); 521f1580f4eSBarry Smith if (data->levels[0]->pc) PetscCall(PCView(data->levels[0]->pc, viewer)); 522f1580f4eSBarry Smith for (i = 1; i < data->N; ++i) { 523f1580f4eSBarry Smith if (data->levels[i]->ksp) color = 1; 524f1580f4eSBarry Smith else color = 0; 525f1580f4eSBarry Smith PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size)); 526f1580f4eSBarry Smith PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank)); 527f1580f4eSBarry Smith PetscCall(PetscSubcommCreate(PetscObjectComm((PetscObject)pc), &subcomm)); 528f1580f4eSBarry Smith PetscCall(PetscSubcommSetNumber(subcomm, PetscMin(size, 2))); 529f1580f4eSBarry Smith PetscCall(PetscSubcommSetTypeGeneral(subcomm, color, rank)); 530f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPushTab(viewer)); 531f1580f4eSBarry Smith PetscCall(PetscViewerGetSubViewer(viewer, PetscSubcommChild(subcomm), &subviewer)); 532f1580f4eSBarry Smith if (color == 1) { 533f1580f4eSBarry Smith PetscCall(KSPView(data->levels[i]->ksp, subviewer)); 534f1580f4eSBarry Smith if (data->levels[i]->pc) PetscCall(PCView(data->levels[i]->pc, subviewer)); 535f1580f4eSBarry Smith PetscCall(PetscViewerFlush(subviewer)); 536f1580f4eSBarry Smith } 537f1580f4eSBarry Smith PetscCall(PetscViewerRestoreSubViewer(viewer, PetscSubcommChild(subcomm), &subviewer)); 538f1580f4eSBarry Smith PetscCall(PetscViewerASCIIPopTab(viewer)); 539f1580f4eSBarry Smith PetscCall(PetscSubcommDestroy(&subcomm)); 540f1580f4eSBarry Smith PetscCall(PetscViewerFlush(viewer)); 541f1580f4eSBarry Smith } 542f1580f4eSBarry Smith } 543aa21023fSPierre Jolivet PetscCall(PetscViewerGetFormat(viewer, &format)); 544aa21023fSPierre Jolivet if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 545aa21023fSPierre Jolivet PetscCall(PetscViewerFileGetName(viewer, &name)); 546aa21023fSPierre Jolivet if (name) { 547aa21023fSPierre Jolivet IS is; 548aa21023fSPierre Jolivet PetscInt sizes[4] = {pc->mat->rmap->n, pc->mat->cmap->n, pc->mat->rmap->N, pc->mat->cmap->N}; 549aa21023fSPierre Jolivet char *tmp; 550aa21023fSPierre Jolivet std::string prefix, suffix; 551aa21023fSPierre Jolivet size_t pos; 552aa21023fSPierre Jolivet 553aa21023fSPierre Jolivet PetscCall(PetscStrstr(name, ".", &tmp)); 554aa21023fSPierre Jolivet if (tmp) { 555aa21023fSPierre Jolivet pos = std::distance(const_cast<char *>(name), tmp); 556aa21023fSPierre Jolivet prefix = std::string(name, pos); 557aa21023fSPierre Jolivet suffix = std::string(name + pos + 1); 558aa21023fSPierre Jolivet } else prefix = name; 559aa21023fSPierre Jolivet if (data->aux) { 560aa21023fSPierre Jolivet PetscCall(PetscViewerBinaryOpen(PETSC_COMM_SELF, std::string(prefix + "_aux_" + std::to_string(rank) + "_" + std::to_string(size) + (tmp ? ("." + suffix) : "")).c_str(), FILE_MODE_WRITE, &subviewer)); 561aa21023fSPierre Jolivet PetscCall(MatView(data->aux, subviewer)); 562aa21023fSPierre Jolivet PetscCall(PetscViewerDestroy(&subviewer)); 563aa21023fSPierre Jolivet } 564aa21023fSPierre Jolivet if (data->is) { 565aa21023fSPierre Jolivet PetscCall(PetscViewerBinaryOpen(PETSC_COMM_SELF, std::string(prefix + "_is_" + std::to_string(rank) + "_" + std::to_string(size) + (tmp ? ("." + suffix) : "")).c_str(), FILE_MODE_WRITE, &subviewer)); 566aa21023fSPierre Jolivet PetscCall(ISView(data->is, subviewer)); 567aa21023fSPierre Jolivet PetscCall(PetscViewerDestroy(&subviewer)); 568aa21023fSPierre Jolivet } 569aa21023fSPierre Jolivet PetscCall(ISCreateGeneral(PETSC_COMM_SELF, PETSC_STATIC_ARRAY_LENGTH(sizes), sizes, PETSC_USE_POINTER, &is)); 570aa21023fSPierre Jolivet PetscCall(PetscViewerBinaryOpen(PETSC_COMM_SELF, std::string(prefix + "_sizes_" + std::to_string(rank) + "_" + std::to_string(size) + (tmp ? ("." + suffix) : "")).c_str(), FILE_MODE_WRITE, &subviewer)); 571aa21023fSPierre Jolivet PetscCall(ISView(is, subviewer)); 572aa21023fSPierre Jolivet PetscCall(PetscViewerDestroy(&subviewer)); 573aa21023fSPierre Jolivet PetscCall(ISDestroy(&is)); 574aa21023fSPierre Jolivet } 575aa21023fSPierre Jolivet } 576f1580f4eSBarry Smith } 5773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 578f1580f4eSBarry Smith } 579f1580f4eSBarry Smith 580d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPreSolve_HPDDM(PC pc, KSP ksp, Vec, Vec) 581d71ae5a4SJacob Faibussowitsch { 582f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 583f1580f4eSBarry Smith PetscBool flg; 584f1580f4eSBarry Smith Mat A; 585f1580f4eSBarry Smith 586f1580f4eSBarry Smith PetscFunctionBegin; 587f1580f4eSBarry Smith if (ksp) { 588f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)ksp, KSPLSQR, &flg)); 589f1580f4eSBarry Smith if (flg && !data->normal) { 590db4a47b3SPierre Jolivet PetscCall(KSPGetOperators(ksp, &A, nullptr)); 591db4a47b3SPierre Jolivet PetscCall(MatCreateVecs(A, nullptr, &data->normal)); /* temporary Vec used in PCApply_HPDDMShell() for coarse grid corrections */ 592f1580f4eSBarry Smith } 593f1580f4eSBarry Smith } 5943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 595f1580f4eSBarry Smith } 596f1580f4eSBarry Smith 597d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_HPDDMShell(PC pc) 598d71ae5a4SJacob Faibussowitsch { 599f1580f4eSBarry Smith PC_HPDDM_Level *ctx; 600f1580f4eSBarry Smith Mat A, P; 601f1580f4eSBarry Smith Vec x; 602f1580f4eSBarry Smith const char *pcpre; 603f1580f4eSBarry Smith 604f1580f4eSBarry Smith PetscFunctionBegin; 605f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx)); 606f1580f4eSBarry Smith PetscCall(KSPGetOptionsPrefix(ctx->ksp, &pcpre)); 607f1580f4eSBarry Smith PetscCall(KSPGetOperators(ctx->ksp, &A, &P)); 608f1580f4eSBarry Smith /* smoother */ 609f1580f4eSBarry Smith PetscCall(PCSetOptionsPrefix(ctx->pc, pcpre)); 610f1580f4eSBarry Smith PetscCall(PCSetOperators(ctx->pc, A, P)); 611f1580f4eSBarry Smith if (!ctx->v[0]) { 612f1580f4eSBarry Smith PetscCall(VecDuplicateVecs(ctx->D, 1, &ctx->v[0])); 613f1580f4eSBarry Smith if (!std::is_same<PetscScalar, PetscReal>::value) PetscCall(VecDestroy(&ctx->D)); 614db4a47b3SPierre Jolivet PetscCall(MatCreateVecs(A, &x, nullptr)); 615f1580f4eSBarry Smith PetscCall(VecDuplicateVecs(x, 2, &ctx->v[1])); 616f1580f4eSBarry Smith PetscCall(VecDestroy(&x)); 617f1580f4eSBarry Smith } 618f1580f4eSBarry Smith std::fill_n(ctx->V, 3, nullptr); 6193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 620f1580f4eSBarry Smith } 621f1580f4eSBarry Smith 622f1580f4eSBarry Smith template <class Type, typename std::enable_if<std::is_same<Type, Vec>::value>::type * = nullptr> 623d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PCHPDDMDeflate_Private(PC pc, Type x, Type y) 624d71ae5a4SJacob Faibussowitsch { 625f1580f4eSBarry Smith PC_HPDDM_Level *ctx; 626f1580f4eSBarry Smith PetscScalar *out; 627f1580f4eSBarry Smith 628f1580f4eSBarry Smith PetscFunctionBegin; 629f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx)); 630f1580f4eSBarry Smith /* going from PETSc to HPDDM numbering */ 631f1580f4eSBarry Smith PetscCall(VecScatterBegin(ctx->scatter, x, ctx->v[0][0], INSERT_VALUES, SCATTER_FORWARD)); 632f1580f4eSBarry Smith PetscCall(VecScatterEnd(ctx->scatter, x, ctx->v[0][0], INSERT_VALUES, SCATTER_FORWARD)); 633f1580f4eSBarry Smith PetscCall(VecGetArrayWrite(ctx->v[0][0], &out)); 634db4a47b3SPierre Jolivet ctx->P->deflation<false>(nullptr, out, 1); /* y = Q x */ 635f1580f4eSBarry Smith PetscCall(VecRestoreArrayWrite(ctx->v[0][0], &out)); 636f1580f4eSBarry Smith /* going from HPDDM to PETSc numbering */ 637f1580f4eSBarry Smith PetscCall(VecScatterBegin(ctx->scatter, ctx->v[0][0], y, INSERT_VALUES, SCATTER_REVERSE)); 638f1580f4eSBarry Smith PetscCall(VecScatterEnd(ctx->scatter, ctx->v[0][0], y, INSERT_VALUES, SCATTER_REVERSE)); 6393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 640f1580f4eSBarry Smith } 641f1580f4eSBarry Smith 642f1580f4eSBarry Smith template <class Type, typename std::enable_if<std::is_same<Type, Mat>::value>::type * = nullptr> 643d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PCHPDDMDeflate_Private(PC pc, Type X, Type Y) 644d71ae5a4SJacob Faibussowitsch { 645f1580f4eSBarry Smith PC_HPDDM_Level *ctx; 646f1580f4eSBarry Smith Vec vX, vY, vC; 647f1580f4eSBarry Smith PetscScalar *out; 648f1580f4eSBarry Smith PetscInt i, N; 649f1580f4eSBarry Smith 650f1580f4eSBarry Smith PetscFunctionBegin; 651f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx)); 652db4a47b3SPierre Jolivet PetscCall(MatGetSize(X, nullptr, &N)); 653f1580f4eSBarry Smith /* going from PETSc to HPDDM numbering */ 654f1580f4eSBarry Smith for (i = 0; i < N; ++i) { 655f1580f4eSBarry Smith PetscCall(MatDenseGetColumnVecRead(X, i, &vX)); 656f1580f4eSBarry Smith PetscCall(MatDenseGetColumnVecWrite(ctx->V[0], i, &vC)); 657f1580f4eSBarry Smith PetscCall(VecScatterBegin(ctx->scatter, vX, vC, INSERT_VALUES, SCATTER_FORWARD)); 658f1580f4eSBarry Smith PetscCall(VecScatterEnd(ctx->scatter, vX, vC, INSERT_VALUES, SCATTER_FORWARD)); 659f1580f4eSBarry Smith PetscCall(MatDenseRestoreColumnVecWrite(ctx->V[0], i, &vC)); 660f1580f4eSBarry Smith PetscCall(MatDenseRestoreColumnVecRead(X, i, &vX)); 661f1580f4eSBarry Smith } 662f1580f4eSBarry Smith PetscCall(MatDenseGetArrayWrite(ctx->V[0], &out)); 663db4a47b3SPierre Jolivet ctx->P->deflation<false>(nullptr, out, N); /* Y = Q X */ 664f1580f4eSBarry Smith PetscCall(MatDenseRestoreArrayWrite(ctx->V[0], &out)); 665f1580f4eSBarry Smith /* going from HPDDM to PETSc numbering */ 666f1580f4eSBarry Smith for (i = 0; i < N; ++i) { 667f1580f4eSBarry Smith PetscCall(MatDenseGetColumnVecRead(ctx->V[0], i, &vC)); 668f1580f4eSBarry Smith PetscCall(MatDenseGetColumnVecWrite(Y, i, &vY)); 669f1580f4eSBarry Smith PetscCall(VecScatterBegin(ctx->scatter, vC, vY, INSERT_VALUES, SCATTER_REVERSE)); 670f1580f4eSBarry Smith PetscCall(VecScatterEnd(ctx->scatter, vC, vY, INSERT_VALUES, SCATTER_REVERSE)); 671f1580f4eSBarry Smith PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &vY)); 672f1580f4eSBarry Smith PetscCall(MatDenseRestoreColumnVecRead(ctx->V[0], i, &vC)); 673f1580f4eSBarry Smith } 6743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 675f1580f4eSBarry Smith } 676f1580f4eSBarry Smith 677f1580f4eSBarry Smith /* 678f1580f4eSBarry Smith PCApply_HPDDMShell - Applies a (2) deflated, (1) additive, or (3) balanced coarse correction. In what follows, E = Z Pmat Z^T and Q = Z^T E^-1 Z. 679f1580f4eSBarry Smith 680f1580f4eSBarry Smith .vb 681f1580f4eSBarry Smith (1) y = Pmat^-1 x + Q x, 682f1580f4eSBarry Smith (2) y = Pmat^-1 (I - Amat Q) x + Q x (default), 683f1580f4eSBarry Smith (3) y = (I - Q Amat^T) Pmat^-1 (I - Amat Q) x + Q x. 684f1580f4eSBarry Smith .ve 685f1580f4eSBarry Smith 686f1580f4eSBarry Smith Input Parameters: 687f1580f4eSBarry Smith + pc - preconditioner context 688f1580f4eSBarry Smith - x - input vector 689f1580f4eSBarry Smith 690f1580f4eSBarry Smith Output Parameter: 691f1580f4eSBarry Smith . y - output vector 692f1580f4eSBarry Smith 693f1580f4eSBarry Smith Notes: 694f1580f4eSBarry 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. 695f1580f4eSBarry 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. 696f1580f4eSBarry 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. 697f1580f4eSBarry Smith 698f1580f4eSBarry Smith Level: advanced 699f1580f4eSBarry Smith 700f1580f4eSBarry Smith Developer Note: 701f1580f4eSBarry 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 702f1580f4eSBarry Smith to trigger it. Likely the manual page is `PCHPDDM` 703f1580f4eSBarry Smith 704f1580f4eSBarry Smith .seealso: `PCHPDDM`, `PCHPDDMCoarseCorrectionType` 705f1580f4eSBarry Smith */ 706d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_HPDDMShell(PC pc, Vec x, Vec y) 707d71ae5a4SJacob Faibussowitsch { 708f1580f4eSBarry Smith PC_HPDDM_Level *ctx; 709f1580f4eSBarry Smith Mat A; 710f1580f4eSBarry Smith 711f1580f4eSBarry Smith PetscFunctionBegin; 712f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx)); 713f1580f4eSBarry Smith PetscCheck(ctx->P, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object"); 714db4a47b3SPierre Jolivet PetscCall(KSPGetOperators(ctx->ksp, &A, nullptr)); 715f1580f4eSBarry Smith PetscCall(PCHPDDMDeflate_Private(pc, x, y)); /* y = Q x */ 716f1580f4eSBarry Smith if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) { 717f1580f4eSBarry Smith if (!ctx->parent->normal || ctx != ctx->parent->levels[0]) PetscCall(MatMult(A, y, ctx->v[1][0])); /* y = A Q x */ 718f1580f4eSBarry Smith else { /* KSPLSQR and finest level */ PetscCall(MatMult(A, y, ctx->parent->normal)); /* y = A Q x */ 719f1580f4eSBarry Smith PetscCall(MatMultHermitianTranspose(A, ctx->parent->normal, ctx->v[1][0])); /* y = A^T A Q x */ 720f1580f4eSBarry Smith } 721f1580f4eSBarry Smith PetscCall(VecWAXPY(ctx->v[1][1], -1.0, ctx->v[1][0], x)); /* y = (I - A Q) x */ 722f1580f4eSBarry Smith PetscCall(PCApply(ctx->pc, ctx->v[1][1], ctx->v[1][0])); /* y = M^-1 (I - A Q) x */ 723f1580f4eSBarry Smith if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) { 724f1580f4eSBarry 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 */ 725f1580f4eSBarry Smith else { 726f1580f4eSBarry Smith PetscCall(MatMult(A, ctx->v[1][0], ctx->parent->normal)); 727f1580f4eSBarry Smith PetscCall(MatMultHermitianTranspose(A, ctx->parent->normal, ctx->v[1][1])); /* z = A^T A y */ 728f1580f4eSBarry Smith } 729f1580f4eSBarry Smith PetscCall(PCHPDDMDeflate_Private(pc, ctx->v[1][1], ctx->v[1][1])); 730f1580f4eSBarry Smith PetscCall(VecAXPBYPCZ(y, -1.0, 1.0, 1.0, ctx->v[1][1], ctx->v[1][0])); /* y = (I - Q A^T) y + Q x */ 731f1580f4eSBarry Smith } else PetscCall(VecAXPY(y, 1.0, ctx->v[1][0])); /* y = Q M^-1 (I - A Q) x + Q x */ 732f1580f4eSBarry Smith } else { 733f1580f4eSBarry 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); 734f1580f4eSBarry Smith PetscCall(PCApply(ctx->pc, x, ctx->v[1][0])); 735f1580f4eSBarry Smith PetscCall(VecAXPY(y, 1.0, ctx->v[1][0])); /* y = M^-1 x + Q x */ 736f1580f4eSBarry Smith } 7373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 738f1580f4eSBarry Smith } 739f1580f4eSBarry Smith 740f1580f4eSBarry Smith /* 741f1580f4eSBarry Smith PCMatApply_HPDDMShell - Variant of PCApply_HPDDMShell() for blocks of vectors. 742f1580f4eSBarry Smith 743f1580f4eSBarry Smith Input Parameters: 744f1580f4eSBarry Smith + pc - preconditioner context 745f1580f4eSBarry Smith - X - block of input vectors 746f1580f4eSBarry Smith 747f1580f4eSBarry Smith Output Parameter: 748f1580f4eSBarry Smith . Y - block of output vectors 749f1580f4eSBarry Smith 750f1580f4eSBarry Smith Level: advanced 751f1580f4eSBarry Smith 752f1580f4eSBarry Smith .seealso: `PCHPDDM`, `PCApply_HPDDMShell()`, `PCHPDDMCoarseCorrectionType` 753f1580f4eSBarry Smith */ 754d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_HPDDMShell(PC pc, Mat X, Mat Y) 755d71ae5a4SJacob Faibussowitsch { 756f1580f4eSBarry Smith PC_HPDDM_Level *ctx; 757f1580f4eSBarry Smith Mat A, *ptr; 758db4a47b3SPierre Jolivet PetscContainer container = nullptr; 759f1580f4eSBarry Smith PetscScalar *array; 760f1580f4eSBarry Smith PetscInt m, M, N, prev = 0; 761f1580f4eSBarry Smith PetscBool reset = PETSC_FALSE; 762f1580f4eSBarry Smith 763f1580f4eSBarry Smith PetscFunctionBegin; 764f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx)); 765f1580f4eSBarry Smith PetscCheck(ctx->P, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCSHELL from PCHPDDM called with no HPDDM object"); 766db4a47b3SPierre Jolivet PetscCall(MatGetSize(X, nullptr, &N)); 767db4a47b3SPierre Jolivet PetscCall(KSPGetOperators(ctx->ksp, &A, nullptr)); 768f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)A, "_HPDDM_MatProduct", (PetscObject *)&container)); 769f1580f4eSBarry Smith if (container) { /* MatProduct container already attached */ 770f1580f4eSBarry Smith PetscCall(PetscContainerGetPointer(container, (void **)&ptr)); 771f1580f4eSBarry Smith if (ptr[1] != ctx->V[2]) /* Mat has changed or may have been set first in KSPHPDDM */ 772f1580f4eSBarry Smith for (m = 0; m < 2; ++m) { 773f1580f4eSBarry Smith PetscCall(MatDestroy(ctx->V + m + 1)); 774f1580f4eSBarry Smith ctx->V[m + 1] = ptr[m]; 775f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)ctx->V[m + 1])); 776f1580f4eSBarry Smith } 777f1580f4eSBarry Smith } 778db4a47b3SPierre Jolivet if (ctx->V[1]) PetscCall(MatGetSize(ctx->V[1], nullptr, &prev)); 779f1580f4eSBarry Smith if (N != prev || !ctx->V[0]) { 780f1580f4eSBarry Smith PetscCall(MatDestroy(ctx->V)); 781f1580f4eSBarry Smith PetscCall(VecGetLocalSize(ctx->v[0][0], &m)); 782db4a47b3SPierre Jolivet PetscCall(MatCreateDense(PetscObjectComm((PetscObject)pc), m, PETSC_DECIDE, PETSC_DECIDE, N, nullptr, ctx->V)); 783f1580f4eSBarry Smith if (N != prev) { 784f1580f4eSBarry Smith PetscCall(MatDestroy(ctx->V + 1)); 785f1580f4eSBarry Smith PetscCall(MatDestroy(ctx->V + 2)); 786db4a47b3SPierre Jolivet PetscCall(MatGetLocalSize(X, &m, nullptr)); 787db4a47b3SPierre Jolivet PetscCall(MatGetSize(X, &M, nullptr)); 788f1580f4eSBarry Smith if (ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) PetscCall(MatDenseGetArrayWrite(ctx->V[0], &array)); 789db4a47b3SPierre Jolivet else array = nullptr; 790f1580f4eSBarry Smith PetscCall(MatCreateDense(PetscObjectComm((PetscObject)pc), m, PETSC_DECIDE, M, N, array, ctx->V + 1)); 791f1580f4eSBarry Smith if (ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) PetscCall(MatDenseRestoreArrayWrite(ctx->V[0], &array)); 792db4a47b3SPierre Jolivet PetscCall(MatCreateDense(PetscObjectComm((PetscObject)pc), m, PETSC_DECIDE, M, N, nullptr, ctx->V + 2)); 793db4a47b3SPierre Jolivet PetscCall(MatProductCreateWithMat(A, Y, nullptr, ctx->V[1])); 794f1580f4eSBarry Smith PetscCall(MatProductSetType(ctx->V[1], MATPRODUCT_AB)); 795f1580f4eSBarry Smith PetscCall(MatProductSetFromOptions(ctx->V[1])); 796f1580f4eSBarry Smith PetscCall(MatProductSymbolic(ctx->V[1])); 797f1580f4eSBarry Smith if (!container) { /* no MatProduct container attached, create one to be queried in KSPHPDDM or at the next call to PCMatApply() */ 798f1580f4eSBarry Smith PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)A), &container)); 799f1580f4eSBarry Smith PetscCall(PetscObjectCompose((PetscObject)A, "_HPDDM_MatProduct", (PetscObject)container)); 800f1580f4eSBarry Smith } 801f1580f4eSBarry Smith PetscCall(PetscContainerSetPointer(container, ctx->V + 1)); /* need to compose B and D from MatProductCreateWithMath(A, B, NULL, D), which are stored in the contiguous array ctx->V */ 802f1580f4eSBarry Smith } 803f1580f4eSBarry Smith if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) { 804db4a47b3SPierre Jolivet PetscCall(MatProductCreateWithMat(A, ctx->V[1], nullptr, ctx->V[2])); 805f1580f4eSBarry Smith PetscCall(MatProductSetType(ctx->V[2], MATPRODUCT_AtB)); 806f1580f4eSBarry Smith PetscCall(MatProductSetFromOptions(ctx->V[2])); 807f1580f4eSBarry Smith PetscCall(MatProductSymbolic(ctx->V[2])); 808f1580f4eSBarry Smith } 809f1580f4eSBarry Smith ctx->P->start(N); 810f1580f4eSBarry Smith } 811f1580f4eSBarry Smith if (N == prev || container) { /* when MatProduct container is attached, always need to MatProductReplaceMats() since KSPHPDDM may have replaced the Mat as well */ 812db4a47b3SPierre Jolivet PetscCall(MatProductReplaceMats(nullptr, Y, nullptr, ctx->V[1])); 813f1580f4eSBarry Smith if (container && ctx->parent->correction != PC_HPDDM_COARSE_CORRECTION_BALANCED) { 814f1580f4eSBarry Smith PetscCall(MatDenseGetArrayWrite(ctx->V[0], &array)); 815f1580f4eSBarry Smith PetscCall(MatDensePlaceArray(ctx->V[1], array)); 816f1580f4eSBarry Smith PetscCall(MatDenseRestoreArrayWrite(ctx->V[0], &array)); 817f1580f4eSBarry Smith reset = PETSC_TRUE; 818f1580f4eSBarry Smith } 819f1580f4eSBarry Smith } 820f1580f4eSBarry Smith PetscCall(PCHPDDMDeflate_Private(pc, X, Y)); 821f1580f4eSBarry Smith if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED || ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) { 822f1580f4eSBarry Smith PetscCall(MatProductNumeric(ctx->V[1])); 823f1580f4eSBarry Smith PetscCall(MatCopy(ctx->V[1], ctx->V[2], SAME_NONZERO_PATTERN)); 824f1580f4eSBarry Smith PetscCall(MatAXPY(ctx->V[2], -1.0, X, SAME_NONZERO_PATTERN)); 825f1580f4eSBarry Smith PetscCall(PCMatApply(ctx->pc, ctx->V[2], ctx->V[1])); 826f1580f4eSBarry Smith if (ctx->parent->correction == PC_HPDDM_COARSE_CORRECTION_BALANCED) { 827f1580f4eSBarry Smith PetscCall(MatProductNumeric(ctx->V[2])); 828f1580f4eSBarry Smith PetscCall(PCHPDDMDeflate_Private(pc, ctx->V[2], ctx->V[2])); 829f1580f4eSBarry Smith PetscCall(MatAXPY(ctx->V[1], -1.0, ctx->V[2], SAME_NONZERO_PATTERN)); 830f1580f4eSBarry Smith } 831f1580f4eSBarry Smith PetscCall(MatAXPY(Y, -1.0, ctx->V[1], SAME_NONZERO_PATTERN)); 832f1580f4eSBarry Smith } else { 833f1580f4eSBarry 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); 834f1580f4eSBarry Smith PetscCall(PCMatApply(ctx->pc, X, ctx->V[1])); 835f1580f4eSBarry Smith PetscCall(MatAXPY(Y, 1.0, ctx->V[1], SAME_NONZERO_PATTERN)); 836f1580f4eSBarry Smith } 837f1580f4eSBarry Smith if (reset) PetscCall(MatDenseResetArray(ctx->V[1])); 8383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 839f1580f4eSBarry Smith } 840f1580f4eSBarry Smith 841d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_HPDDMShell(PC pc) 842d71ae5a4SJacob Faibussowitsch { 843f1580f4eSBarry Smith PC_HPDDM_Level *ctx; 844f1580f4eSBarry Smith PetscContainer container; 845f1580f4eSBarry Smith 846f1580f4eSBarry Smith PetscFunctionBegin; 847f1580f4eSBarry Smith PetscCall(PCShellGetContext(pc, &ctx)); 848f1580f4eSBarry Smith PetscCall(HPDDM::Schwarz<PetscScalar>::destroy(ctx, PETSC_TRUE)); 849f1580f4eSBarry Smith PetscCall(VecDestroyVecs(1, &ctx->v[0])); 850f1580f4eSBarry Smith PetscCall(VecDestroyVecs(2, &ctx->v[1])); 851f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)(ctx->pc)->mat, "_HPDDM_MatProduct", (PetscObject *)&container)); 852f1580f4eSBarry Smith PetscCall(PetscContainerDestroy(&container)); 853db4a47b3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)(ctx->pc)->mat, "_HPDDM_MatProduct", nullptr)); 854f1580f4eSBarry Smith PetscCall(MatDestroy(ctx->V)); 855f1580f4eSBarry Smith PetscCall(MatDestroy(ctx->V + 1)); 856f1580f4eSBarry Smith PetscCall(MatDestroy(ctx->V + 2)); 857f1580f4eSBarry Smith PetscCall(VecDestroy(&ctx->D)); 858f1580f4eSBarry Smith PetscCall(VecScatterDestroy(&ctx->scatter)); 859f1580f4eSBarry Smith PetscCall(PCDestroy(&ctx->pc)); 8603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 861f1580f4eSBarry Smith } 862f1580f4eSBarry Smith 863*9bb5c669SPierre Jolivet template <class Type, bool T = false, typename std::enable_if<std::is_same<Type, Vec>::value>::type * = nullptr> 864*9bb5c669SPierre Jolivet static inline PetscErrorCode PCApply_Schur_Private(std::tuple<KSP, IS, Vec[2]> *p, PC factor, Type x, Type y) 865*9bb5c669SPierre Jolivet { 866*9bb5c669SPierre Jolivet PetscFunctionBegin; 867*9bb5c669SPierre Jolivet PetscCall(VecISCopy(std::get<2>(*p)[0], std::get<1>(*p), SCATTER_FORWARD, x)); 868*9bb5c669SPierre Jolivet if (!T) PetscCall(PCApply(factor, std::get<2>(*p)[0], std::get<2>(*p)[1])); 869*9bb5c669SPierre Jolivet else PetscCall(PCApplyTranspose(factor, std::get<2>(*p)[0], std::get<2>(*p)[1])); 870*9bb5c669SPierre Jolivet PetscCall(VecISCopy(std::get<2>(*p)[1], std::get<1>(*p), SCATTER_REVERSE, y)); 871*9bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 872*9bb5c669SPierre Jolivet } 873*9bb5c669SPierre Jolivet 874*9bb5c669SPierre Jolivet template <class Type, bool = false, typename std::enable_if<std::is_same<Type, Mat>::value>::type * = nullptr> 875*9bb5c669SPierre Jolivet static inline PetscErrorCode PCApply_Schur_Private(std::tuple<KSP, IS, Vec[2]> *p, PC factor, Type X, Type Y) 876*9bb5c669SPierre Jolivet { 877*9bb5c669SPierre Jolivet Mat B[2]; 878*9bb5c669SPierre Jolivet Vec x, y; 879*9bb5c669SPierre Jolivet 880*9bb5c669SPierre Jolivet PetscFunctionBegin; 881*9bb5c669SPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, factor->mat->rmap->n, X->cmap->n, nullptr, B)); 882*9bb5c669SPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, factor->mat->rmap->n, X->cmap->n, nullptr, B + 1)); 883*9bb5c669SPierre Jolivet for (PetscInt i = 0; i < X->cmap->n; ++i) { 884*9bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecRead(X, i, &x)); 885*9bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecWrite(B[0], i, &y)); 886*9bb5c669SPierre Jolivet PetscCall(VecISCopy(y, std::get<1>(*p), SCATTER_FORWARD, x)); 887*9bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecWrite(B[0], i, &y)); 888*9bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecRead(X, i, &x)); 889*9bb5c669SPierre Jolivet } 890*9bb5c669SPierre Jolivet PetscCall(PCMatApply(factor, B[0], B[1])); 891*9bb5c669SPierre Jolivet PetscCall(MatDestroy(B)); 892*9bb5c669SPierre Jolivet for (PetscInt i = 0; i < X->cmap->n; ++i) { 893*9bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecRead(B[1], i, &x)); 894*9bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecWrite(Y, i, &y)); 895*9bb5c669SPierre Jolivet PetscCall(VecISCopy(x, std::get<1>(*p), SCATTER_REVERSE, y)); 896*9bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &y)); 897*9bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecRead(B[1], i, &x)); 898*9bb5c669SPierre Jolivet } 899*9bb5c669SPierre Jolivet PetscCall(MatDestroy(B + 1)); 900*9bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 901*9bb5c669SPierre Jolivet } 902*9bb5c669SPierre Jolivet 903*9bb5c669SPierre Jolivet template <class Type = Vec, bool T = false> 904*9bb5c669SPierre Jolivet static PetscErrorCode PCApply_Schur(PC pc, Type x, Type y) 905*9bb5c669SPierre Jolivet { 906*9bb5c669SPierre Jolivet PC factor; 907*9bb5c669SPierre Jolivet Mat A; 908*9bb5c669SPierre Jolivet MatSolverType type; 909*9bb5c669SPierre Jolivet PetscBool flg; 910*9bb5c669SPierre Jolivet std::tuple<KSP, IS, Vec[2]> *p; 911*9bb5c669SPierre Jolivet 912*9bb5c669SPierre Jolivet PetscFunctionBegin; 913*9bb5c669SPierre Jolivet PetscCall(PCShellGetContext(pc, &p)); 914*9bb5c669SPierre Jolivet PetscCall(KSPGetPC(std::get<0>(*p), &factor)); 915*9bb5c669SPierre Jolivet PetscCall(PCFactorGetMatSolverType(factor, &type)); 916*9bb5c669SPierre Jolivet PetscCall(PCFactorGetMatrix(factor, &A)); 917*9bb5c669SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &flg)); 918*9bb5c669SPierre Jolivet if (flg) { 919*9bb5c669SPierre Jolivet PetscCheck(PetscDefined(HAVE_MUMPS), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent MatSolverType"); 920*9bb5c669SPierre Jolivet #if PetscDefined(HAVE_MUMPS) 921*9bb5c669SPierre Jolivet PetscCall(MatMumpsSetIcntl(A, 26, 0)); 922*9bb5c669SPierre Jolivet #endif 923*9bb5c669SPierre Jolivet } else { 924*9bb5c669SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMKL_PARDISO, &flg)); 925*9bb5c669SPierre Jolivet PetscCheck(flg && PetscDefined(HAVE_MKL_PARDISO), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent MatSolverType"); 926*9bb5c669SPierre Jolivet flg = PETSC_FALSE; 927*9bb5c669SPierre Jolivet #if PetscDefined(HAVE_MKL_PARDISO) 928*9bb5c669SPierre Jolivet PetscCall(MatMkl_PardisoSetCntl(A, 70, 1)); 929*9bb5c669SPierre Jolivet #endif 930*9bb5c669SPierre Jolivet } 931*9bb5c669SPierre Jolivet PetscCall(PCApply_Schur_Private<Type, T>(p, factor, x, y)); 932*9bb5c669SPierre Jolivet if (flg) { 933*9bb5c669SPierre Jolivet #if PetscDefined(HAVE_MUMPS) 934*9bb5c669SPierre Jolivet PetscCall(MatMumpsSetIcntl(A, 26, -1)); 935*9bb5c669SPierre Jolivet #endif 936*9bb5c669SPierre Jolivet } else { 937*9bb5c669SPierre Jolivet #if PetscDefined(HAVE_MKL_PARDISO) 938*9bb5c669SPierre Jolivet PetscCall(MatMkl_PardisoSetCntl(A, 70, 0)); 939*9bb5c669SPierre Jolivet #endif 940*9bb5c669SPierre Jolivet } 941*9bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 942*9bb5c669SPierre Jolivet } 943*9bb5c669SPierre Jolivet 944*9bb5c669SPierre Jolivet static PetscErrorCode PCDestroy_Schur(PC pc) 945*9bb5c669SPierre Jolivet { 946*9bb5c669SPierre Jolivet std::tuple<KSP, IS, Vec[2]> *p; 947*9bb5c669SPierre Jolivet 948*9bb5c669SPierre Jolivet PetscFunctionBegin; 949*9bb5c669SPierre Jolivet PetscCall(PCShellGetContext(pc, &p)); 950*9bb5c669SPierre Jolivet PetscCall(ISDestroy(&std::get<1>(*p))); 951*9bb5c669SPierre Jolivet PetscCall(VecDestroy(std::get<2>(*p))); 952*9bb5c669SPierre Jolivet PetscCall(VecDestroy(std::get<2>(*p) + 1)); 953*9bb5c669SPierre Jolivet PetscCall(PetscFree(p)); 954*9bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 955*9bb5c669SPierre Jolivet } 956*9bb5c669SPierre Jolivet 957d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSolve_Private(const PC_HPDDM_Level *ctx, PetscScalar *rhs, const unsigned short &mu) 958d71ae5a4SJacob Faibussowitsch { 959f1580f4eSBarry Smith Mat B, X; 960f1580f4eSBarry Smith PetscInt n, N, j = 0; 961f1580f4eSBarry Smith 962f1580f4eSBarry Smith PetscFunctionBegin; 963db4a47b3SPierre Jolivet PetscCall(KSPGetOperators(ctx->ksp, &B, nullptr)); 964db4a47b3SPierre Jolivet PetscCall(MatGetLocalSize(B, &n, nullptr)); 965db4a47b3SPierre Jolivet PetscCall(MatGetSize(B, &N, nullptr)); 966f1580f4eSBarry Smith if (ctx->parent->log_separate) { 967f1580f4eSBarry Smith j = std::distance(ctx->parent->levels, std::find(ctx->parent->levels, ctx->parent->levels + ctx->parent->N, ctx)); 968db4a47b3SPierre Jolivet PetscCall(PetscLogEventBegin(PC_HPDDM_Solve[j], ctx->ksp, nullptr, nullptr, nullptr)); 969f1580f4eSBarry Smith } 970f1580f4eSBarry Smith if (mu == 1) { 971f1580f4eSBarry Smith if (!ctx->ksp->vec_rhs) { 972db4a47b3SPierre Jolivet PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)ctx->ksp), 1, n, N, nullptr, &ctx->ksp->vec_rhs)); 973f1580f4eSBarry Smith PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)ctx->ksp), n, N, &ctx->ksp->vec_sol)); 974f1580f4eSBarry Smith } 975f1580f4eSBarry Smith PetscCall(VecPlaceArray(ctx->ksp->vec_rhs, rhs)); 976db4a47b3SPierre Jolivet PetscCall(KSPSolve(ctx->ksp, nullptr, nullptr)); 977f1580f4eSBarry Smith PetscCall(VecCopy(ctx->ksp->vec_sol, ctx->ksp->vec_rhs)); 978f1580f4eSBarry Smith PetscCall(VecResetArray(ctx->ksp->vec_rhs)); 979f1580f4eSBarry Smith } else { 980f1580f4eSBarry Smith PetscCall(MatCreateDense(PetscObjectComm((PetscObject)ctx->ksp), n, PETSC_DECIDE, N, mu, rhs, &B)); 981db4a47b3SPierre Jolivet PetscCall(MatCreateDense(PetscObjectComm((PetscObject)ctx->ksp), n, PETSC_DECIDE, N, mu, nullptr, &X)); 982f1580f4eSBarry Smith PetscCall(KSPMatSolve(ctx->ksp, B, X)); 983f1580f4eSBarry Smith PetscCall(MatCopy(X, B, SAME_NONZERO_PATTERN)); 984f1580f4eSBarry Smith PetscCall(MatDestroy(&X)); 985f1580f4eSBarry Smith PetscCall(MatDestroy(&B)); 986f1580f4eSBarry Smith } 987db4a47b3SPierre Jolivet if (ctx->parent->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_Solve[j], ctx->ksp, nullptr, nullptr, nullptr)); 9883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 989f1580f4eSBarry Smith } 990f1580f4eSBarry Smith 991d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetUpNeumannOverlap_Private(PC pc) 992d71ae5a4SJacob Faibussowitsch { 993f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 994f1580f4eSBarry Smith 995f1580f4eSBarry Smith PetscFunctionBegin; 996f1580f4eSBarry Smith if (data->setup) { 997f1580f4eSBarry Smith Mat P; 998db4a47b3SPierre Jolivet Vec x, xt = nullptr; 999f1580f4eSBarry Smith PetscReal t = 0.0, s = 0.0; 1000f1580f4eSBarry Smith 1001db4a47b3SPierre Jolivet PetscCall(PCGetOperators(pc, nullptr, &P)); 1002f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)P, "__SNES_latest_X", (PetscObject *)&x)); 1003f1580f4eSBarry Smith PetscCallBack("PCHPDDM Neumann callback", (*data->setup)(data->aux, t, x, xt, s, data->is, data->setup_ctx)); 1004f1580f4eSBarry Smith } 10053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1006f1580f4eSBarry Smith } 1007f1580f4eSBarry Smith 1008d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMCreateSubMatrices_Private(Mat mat, PetscInt n, const IS *, const IS *, MatReuse scall, Mat *submat[]) 1009d71ae5a4SJacob Faibussowitsch { 1010f1580f4eSBarry Smith Mat A; 101113044ca3SPierre Jolivet PetscBool flg; 1012f1580f4eSBarry Smith 1013f1580f4eSBarry Smith PetscFunctionBegin; 1014f1580f4eSBarry Smith PetscCheck(n == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatCreateSubMatrices() called to extract %" PetscInt_FMT " submatrices, which is different than 1", n); 1015f1580f4eSBarry Smith /* previously composed Mat */ 1016f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)mat, "_PCHPDDM_SubMatrices", (PetscObject *)&A)); 1017f1580f4eSBarry Smith PetscCheck(A, PETSC_COMM_SELF, PETSC_ERR_PLIB, "SubMatrices not found in Mat"); 101813044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A, MATSCHURCOMPLEMENT, &flg)); /* MATSCHURCOMPLEMENT has neither a MatDuplicate() nor a MatCopy() implementation */ 1019f1580f4eSBarry Smith if (scall == MAT_INITIAL_MATRIX) { 10206f2c871aSStefano Zampini PetscCall(PetscCalloc1(2, submat)); /* allocate an extra Mat to avoid errors in MatDestroySubMatrices_Dummy() */ 102113044ca3SPierre Jolivet if (!flg) PetscCall(MatDuplicate(A, MAT_COPY_VALUES, *submat)); 102213044ca3SPierre Jolivet } else if (!flg) PetscCall(MatCopy(A, (*submat)[0], SAME_NONZERO_PATTERN)); 102313044ca3SPierre Jolivet if (flg) { 102413044ca3SPierre Jolivet (*submat)[0] = A; 102513044ca3SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)A)); 102613044ca3SPierre Jolivet } 10273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1028f1580f4eSBarry Smith } 1029f1580f4eSBarry Smith 1030d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMCommunicationAvoidingPCASM_Private(PC pc, Mat C, PetscBool sorted) 1031d71ae5a4SJacob Faibussowitsch { 1032f1580f4eSBarry Smith void (*op)(void); 1033f1580f4eSBarry Smith 1034f1580f4eSBarry Smith PetscFunctionBegin; 1035f1580f4eSBarry Smith /* previously-composed Mat */ 1036f1580f4eSBarry Smith PetscCall(PetscObjectCompose((PetscObject)pc->pmat, "_PCHPDDM_SubMatrices", (PetscObject)C)); 1037f1580f4eSBarry Smith PetscCall(MatGetOperation(pc->pmat, MATOP_CREATE_SUBMATRICES, &op)); 1038f1580f4eSBarry Smith /* trick suggested by Barry https://lists.mcs.anl.gov/pipermail/petsc-dev/2020-January/025491.html */ 1039f1580f4eSBarry Smith PetscCall(MatSetOperation(pc->pmat, MATOP_CREATE_SUBMATRICES, (void (*)(void))PCHPDDMCreateSubMatrices_Private)); 1040f1580f4eSBarry Smith if (sorted) PetscCall(PCASMSetSortIndices(pc, PETSC_FALSE)); /* everything is already sorted */ 1041f1580f4eSBarry Smith PetscCall(PCSetFromOptions(pc)); /* otherwise -pc_hpddm_levels_1_pc_asm_sub_mat_type is not used */ 1042f1580f4eSBarry Smith PetscCall(PCSetUp(pc)); 1043f1580f4eSBarry Smith /* reset MatCreateSubMatrices() */ 1044f1580f4eSBarry Smith PetscCall(MatSetOperation(pc->pmat, MATOP_CREATE_SUBMATRICES, op)); 1045db4a47b3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)pc->pmat, "_PCHPDDM_SubMatrices", nullptr)); 10463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1047f1580f4eSBarry Smith } 1048f1580f4eSBarry Smith 1049d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMPermute_Private(IS is, IS in_is, IS *out_is, Mat in_C, Mat *out_C, IS *p) 1050d71ae5a4SJacob Faibussowitsch { 1051f1580f4eSBarry Smith IS perm; 1052f1580f4eSBarry Smith const PetscInt *ptr; 1053b07dfdedSPierre Jolivet PetscInt *concatenate, size, n, bs; 1054f1580f4eSBarry Smith std::map<PetscInt, PetscInt> order; 1055f1580f4eSBarry Smith PetscBool sorted; 1056f1580f4eSBarry Smith 1057f1580f4eSBarry Smith PetscFunctionBegin; 1058f1580f4eSBarry Smith PetscCall(ISSorted(is, &sorted)); 1059f1580f4eSBarry Smith if (!sorted) { 1060f1580f4eSBarry Smith PetscCall(ISGetLocalSize(is, &size)); 1061f1580f4eSBarry Smith PetscCall(ISGetIndices(is, &ptr)); 1062b07dfdedSPierre Jolivet PetscCall(ISGetBlockSize(is, &bs)); 1063f1580f4eSBarry Smith /* MatCreateSubMatrices(), called by PCASM, follows the global numbering of Pmat */ 1064b07dfdedSPierre Jolivet for (n = 0; n < size; n += bs) order.insert(std::make_pair(ptr[n] / bs, n / bs)); 1065f1580f4eSBarry Smith PetscCall(ISRestoreIndices(is, &ptr)); 1066b07dfdedSPierre Jolivet size /= bs; 1067f1580f4eSBarry Smith if (out_C) { 1068f1580f4eSBarry Smith PetscCall(PetscMalloc1(size, &concatenate)); 1069f1580f4eSBarry Smith for (const std::pair<const PetscInt, PetscInt> &i : order) *concatenate++ = i.second; 1070f1580f4eSBarry Smith concatenate -= size; 1071b07dfdedSPierre Jolivet PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)in_C), bs, size, concatenate, PETSC_OWN_POINTER, &perm)); 1072f1580f4eSBarry Smith PetscCall(ISSetPermutation(perm)); 1073f1580f4eSBarry Smith /* permute user-provided Mat so that it matches with MatCreateSubMatrices() numbering */ 1074f1580f4eSBarry Smith PetscCall(MatPermute(in_C, perm, perm, out_C)); 1075f1580f4eSBarry Smith if (p) *p = perm; 1076f1580f4eSBarry Smith else PetscCall(ISDestroy(&perm)); /* no need to save the permutation */ 1077f1580f4eSBarry Smith } 1078f1580f4eSBarry Smith if (out_is) { 1079f1580f4eSBarry Smith PetscCall(PetscMalloc1(size, &concatenate)); 1080f1580f4eSBarry Smith for (const std::pair<const PetscInt, PetscInt> &i : order) *concatenate++ = i.first; 1081f1580f4eSBarry Smith concatenate -= size; 1082f1580f4eSBarry Smith /* permute user-provided IS so that it matches with MatCreateSubMatrices() numbering */ 1083b07dfdedSPierre Jolivet PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)in_is), bs, size, concatenate, PETSC_OWN_POINTER, out_is)); 1084f1580f4eSBarry Smith } 1085f1580f4eSBarry Smith } else { /* input IS is sorted, nothing to permute, simply duplicate inputs when needed */ 1086f1580f4eSBarry Smith if (out_C) PetscCall(MatDuplicate(in_C, MAT_COPY_VALUES, out_C)); 1087f1580f4eSBarry Smith if (out_is) PetscCall(ISDuplicate(in_is, out_is)); 1088f1580f4eSBarry Smith } 10893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1090f1580f4eSBarry Smith } 1091f1580f4eSBarry Smith 10925e642048SPierre Jolivet static PetscErrorCode PCHPDDMCheckSymmetry_Private(PC pc, Mat A01, Mat A10) 109313044ca3SPierre Jolivet { 109413044ca3SPierre Jolivet Mat T, U = nullptr, B = nullptr; 109513044ca3SPierre Jolivet IS z; 109613044ca3SPierre Jolivet PetscBool flg[2]; 109713044ca3SPierre Jolivet 109813044ca3SPierre Jolivet PetscFunctionBegin; 109913044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATTRANSPOSEVIRTUAL, flg)); 110013044ca3SPierre Jolivet if (flg[0]) PetscCall(MatTransposeGetMat(A10, &U)); 110113044ca3SPierre Jolivet else { 110213044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATHERMITIANTRANSPOSEVIRTUAL, flg + 1)); 110313044ca3SPierre Jolivet if (flg[1]) PetscCall(MatHermitianTransposeGetMat(A10, &U)); 110413044ca3SPierre Jolivet } 110513044ca3SPierre Jolivet if (U) PetscCall(MatDuplicate(U, MAT_COPY_VALUES, &T)); 110613044ca3SPierre Jolivet else PetscCall(MatHermitianTranspose(A10, MAT_INITIAL_MATRIX, &T)); 110713044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A01, MATTRANSPOSEVIRTUAL, flg)); 110813044ca3SPierre Jolivet if (flg[0]) { 110913044ca3SPierre Jolivet PetscCall(MatTransposeGetMat(A01, &A01)); 111013044ca3SPierre Jolivet PetscCall(MatTranspose(A01, MAT_INITIAL_MATRIX, &B)); 111113044ca3SPierre Jolivet A01 = B; 111213044ca3SPierre Jolivet } else { 111313044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A01, MATHERMITIANTRANSPOSEVIRTUAL, flg)); 111413044ca3SPierre Jolivet if (flg[0]) { 111513044ca3SPierre Jolivet PetscCall(MatHermitianTransposeGetMat(A01, &A01)); 111613044ca3SPierre Jolivet PetscCall(MatHermitianTranspose(A01, MAT_INITIAL_MATRIX, &B)); 111713044ca3SPierre Jolivet A01 = B; 111813044ca3SPierre Jolivet } 111913044ca3SPierre Jolivet } 112013044ca3SPierre Jolivet PetscCall(PetscLayoutCompare(T->rmap, A01->rmap, flg)); 112113044ca3SPierre Jolivet if (flg[0]) { 112213044ca3SPierre Jolivet PetscCall(PetscLayoutCompare(T->cmap, A01->cmap, flg)); 112313044ca3SPierre Jolivet if (flg[0]) { 112413044ca3SPierre Jolivet PetscCall(MatFindZeroRows(A01, &z)); /* for essential boundary conditions, some implementations will */ 112513044ca3SPierre Jolivet if (z) { /* zero rows in [P00 A01] except for the diagonal of P00 */ 112613044ca3SPierre Jolivet PetscCall(MatSetOption(T, MAT_NO_OFF_PROC_ZERO_ROWS, PETSC_TRUE)); 112713044ca3SPierre Jolivet PetscCall(MatZeroRowsIS(T, z, 0.0, nullptr, nullptr)); /* corresponding zero rows from A01 */ 112813044ca3SPierre Jolivet PetscCall(ISDestroy(&z)); 112913044ca3SPierre Jolivet } 113013044ca3SPierre Jolivet PetscCall(MatMultEqual(A01, T, 20, flg)); 113113044ca3SPierre Jolivet PetscCheck(flg[0], PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "A01 != A10^T"); 113213044ca3SPierre Jolivet } else PetscCall(PetscInfo(pc, "A01 and A10^T have non-congruent column layouts, cannot test for equality\n")); 113313044ca3SPierre Jolivet } 113413044ca3SPierre Jolivet PetscCall(MatDestroy(&B)); 113513044ca3SPierre Jolivet PetscCall(MatDestroy(&T)); 113613044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 113713044ca3SPierre Jolivet } 113813044ca3SPierre Jolivet 1139d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMDestroySubMatrices_Private(PetscBool flg, PetscBool algebraic, Mat *sub) 1140d71ae5a4SJacob Faibussowitsch { 1141f1580f4eSBarry Smith IS is; 1142f1580f4eSBarry Smith 1143f1580f4eSBarry Smith PetscFunctionBegin; 1144f1580f4eSBarry Smith if (!flg) { 1145f1580f4eSBarry Smith if (algebraic) { 1146f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)sub[0], "_PCHPDDM_Embed", (PetscObject *)&is)); 1147f1580f4eSBarry Smith PetscCall(ISDestroy(&is)); 1148db4a47b3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Embed", nullptr)); 1149db4a47b3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Compact", nullptr)); 1150f1580f4eSBarry Smith } 1151f1580f4eSBarry Smith PetscCall(MatDestroySubMatrices(algebraic ? 2 : 1, &sub)); 1152f1580f4eSBarry Smith } 11533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1154f1580f4eSBarry Smith } 1155f1580f4eSBarry Smith 1156d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMAlgebraicAuxiliaryMat_Private(Mat P, IS *is, Mat *sub[], PetscBool block) 1157d71ae5a4SJacob Faibussowitsch { 1158f1580f4eSBarry Smith IS icol[3], irow[2]; 1159f1580f4eSBarry Smith Mat *M, Q; 1160f1580f4eSBarry Smith PetscReal *ptr; 1161f1580f4eSBarry Smith PetscInt *idx, p = 0, n, bs = PetscAbs(P->cmap->bs); 1162f1580f4eSBarry Smith PetscBool flg; 1163f1580f4eSBarry Smith 1164f1580f4eSBarry Smith PetscFunctionBegin; 1165f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, P->cmap->N, 0, 1, icol + 2)); 1166f1580f4eSBarry Smith PetscCall(ISSetBlockSize(icol[2], bs)); 1167f1580f4eSBarry Smith PetscCall(ISSetIdentity(icol[2])); 1168f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)P, MATMPISBAIJ, &flg)); 1169f1580f4eSBarry Smith if (flg) { 1170f1580f4eSBarry Smith /* MatCreateSubMatrices() does not handle MATMPISBAIJ properly when iscol != isrow, so convert first to MATMPIBAIJ */ 1171f1580f4eSBarry Smith PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &Q)); 1172f1580f4eSBarry Smith std::swap(P, Q); 1173f1580f4eSBarry Smith } 1174f1580f4eSBarry Smith PetscCall(MatCreateSubMatrices(P, 1, is, icol + 2, MAT_INITIAL_MATRIX, &M)); 1175f1580f4eSBarry Smith if (flg) { 1176f1580f4eSBarry Smith std::swap(P, Q); 1177f1580f4eSBarry Smith PetscCall(MatDestroy(&Q)); 1178f1580f4eSBarry Smith } 1179f1580f4eSBarry Smith PetscCall(ISDestroy(icol + 2)); 1180f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, M[0]->rmap->N, 0, 1, irow)); 1181f1580f4eSBarry Smith PetscCall(ISSetBlockSize(irow[0], bs)); 1182f1580f4eSBarry Smith PetscCall(ISSetIdentity(irow[0])); 1183f1580f4eSBarry Smith if (!block) { 1184b07dfdedSPierre Jolivet PetscCall(PetscMalloc2(P->cmap->N, &ptr, P->cmap->N / bs, &idx)); 1185f1580f4eSBarry Smith PetscCall(MatGetColumnNorms(M[0], NORM_INFINITY, ptr)); 1186f1580f4eSBarry Smith /* check for nonzero columns so that M[0] may be expressed in compact form */ 1187b07dfdedSPierre Jolivet for (n = 0; n < P->cmap->N; n += bs) { 1188b07dfdedSPierre Jolivet if (std::find_if(ptr + n, ptr + n + bs, [](PetscReal v) { return v > PETSC_MACHINE_EPSILON; }) != ptr + n + bs) idx[p++] = n / bs; 1189f1580f4eSBarry Smith } 1190b07dfdedSPierre Jolivet PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, p, idx, PETSC_USE_POINTER, icol + 1)); 1191f1580f4eSBarry Smith PetscCall(ISSetInfo(icol[1], IS_SORTED, IS_GLOBAL, PETSC_TRUE, PETSC_TRUE)); 1192f1580f4eSBarry Smith PetscCall(ISEmbed(*is, icol[1], PETSC_FALSE, icol + 2)); 1193f1580f4eSBarry Smith irow[1] = irow[0]; 1194f1580f4eSBarry 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 */ 1195f1580f4eSBarry Smith icol[0] = is[0]; 1196f1580f4eSBarry Smith PetscCall(MatCreateSubMatrices(M[0], 2, irow, icol, MAT_INITIAL_MATRIX, sub)); 1197f1580f4eSBarry Smith PetscCall(ISDestroy(icol + 1)); 1198f1580f4eSBarry Smith PetscCall(PetscFree2(ptr, idx)); 1199f1580f4eSBarry Smith /* IS used to go back and forth between the augmented and the original local linear system, see eq. (3.4) of [2022b] */ 1200f1580f4eSBarry Smith PetscCall(PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Embed", (PetscObject)icol[2])); 1201f1580f4eSBarry Smith /* Mat used in eq. (3.1) of [2022b] */ 1202f1580f4eSBarry Smith PetscCall(PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Compact", (PetscObject)(*sub)[1])); 1203f1580f4eSBarry Smith } else { 1204f1580f4eSBarry Smith Mat aux; 1205f1580f4eSBarry Smith PetscCall(MatSetOption(M[0], MAT_SUBMAT_SINGLEIS, PETSC_TRUE)); 1206f1580f4eSBarry Smith /* diagonal block of the overlapping rows */ 1207f1580f4eSBarry Smith PetscCall(MatCreateSubMatrices(M[0], 1, irow, is, MAT_INITIAL_MATRIX, sub)); 1208f1580f4eSBarry Smith PetscCall(MatDuplicate((*sub)[0], MAT_COPY_VALUES, &aux)); 1209f1580f4eSBarry Smith PetscCall(MatSetOption(aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE)); 1210f1580f4eSBarry Smith if (bs == 1) { /* scalar case */ 1211f1580f4eSBarry Smith Vec sum[2]; 1212f1580f4eSBarry Smith PetscCall(MatCreateVecs(aux, sum, sum + 1)); 1213f1580f4eSBarry Smith PetscCall(MatGetRowSum(M[0], sum[0])); 1214f1580f4eSBarry Smith PetscCall(MatGetRowSum(aux, sum[1])); 1215f1580f4eSBarry Smith /* off-diagonal block row sum (full rows - diagonal block rows) */ 1216f1580f4eSBarry Smith PetscCall(VecAXPY(sum[0], -1.0, sum[1])); 1217f1580f4eSBarry Smith /* subdomain matrix plus off-diagonal block row sum */ 1218f1580f4eSBarry Smith PetscCall(MatDiagonalSet(aux, sum[0], ADD_VALUES)); 1219f1580f4eSBarry Smith PetscCall(VecDestroy(sum)); 1220f1580f4eSBarry Smith PetscCall(VecDestroy(sum + 1)); 1221f1580f4eSBarry Smith } else { /* vectorial case */ 1222f1580f4eSBarry Smith /* TODO: missing MatGetValuesBlocked(), so the code below is */ 1223f1580f4eSBarry Smith /* an extension of the scalar case for when bs > 1, but it could */ 1224f1580f4eSBarry Smith /* be more efficient by avoiding all these MatMatMult() */ 1225f1580f4eSBarry Smith Mat sum[2], ones; 1226f1580f4eSBarry Smith PetscScalar *ptr; 1227f1580f4eSBarry Smith PetscCall(PetscCalloc1(M[0]->cmap->n * bs, &ptr)); 1228f1580f4eSBarry Smith PetscCall(MatCreateDense(PETSC_COMM_SELF, M[0]->cmap->n, bs, M[0]->cmap->n, bs, ptr, &ones)); 1229f1580f4eSBarry Smith for (n = 0; n < M[0]->cmap->n; n += bs) { 1230f1580f4eSBarry Smith for (p = 0; p < bs; ++p) ptr[n + p * (M[0]->cmap->n + 1)] = 1.0; 1231f1580f4eSBarry Smith } 1232f1580f4eSBarry Smith PetscCall(MatMatMult(M[0], ones, MAT_INITIAL_MATRIX, PETSC_DEFAULT, sum)); 1233f1580f4eSBarry Smith PetscCall(MatDestroy(&ones)); 1234f1580f4eSBarry Smith PetscCall(MatCreateDense(PETSC_COMM_SELF, aux->cmap->n, bs, aux->cmap->n, bs, ptr, &ones)); 1235f1580f4eSBarry Smith PetscCall(MatDenseSetLDA(ones, M[0]->cmap->n)); 1236f1580f4eSBarry Smith PetscCall(MatMatMult(aux, ones, MAT_INITIAL_MATRIX, PETSC_DEFAULT, sum + 1)); 1237f1580f4eSBarry Smith PetscCall(MatDestroy(&ones)); 1238f1580f4eSBarry Smith PetscCall(PetscFree(ptr)); 1239f1580f4eSBarry Smith /* off-diagonal block row sum (full rows - diagonal block rows) */ 1240f1580f4eSBarry Smith PetscCall(MatAXPY(sum[0], -1.0, sum[1], SAME_NONZERO_PATTERN)); 1241f1580f4eSBarry Smith PetscCall(MatDestroy(sum + 1)); 1242f1580f4eSBarry Smith /* re-order values to be consistent with MatSetValuesBlocked() */ 1243f1580f4eSBarry Smith /* equivalent to MatTranspose() which does not truly handle */ 1244f1580f4eSBarry Smith /* MAT_INPLACE_MATRIX in the rectangular case, as it calls PetscMalloc() */ 1245f1580f4eSBarry Smith PetscCall(MatDenseGetArrayWrite(sum[0], &ptr)); 1246f1580f4eSBarry Smith HPDDM::Wrapper<PetscScalar>::imatcopy<'T'>(bs, sum[0]->rmap->n, ptr, sum[0]->rmap->n, bs); 1247f1580f4eSBarry Smith /* subdomain matrix plus off-diagonal block row sum */ 1248f1580f4eSBarry Smith for (n = 0; n < aux->cmap->n / bs; ++n) PetscCall(MatSetValuesBlocked(aux, 1, &n, 1, &n, ptr + n * bs * bs, ADD_VALUES)); 1249f1580f4eSBarry Smith PetscCall(MatAssemblyBegin(aux, MAT_FINAL_ASSEMBLY)); 1250f1580f4eSBarry Smith PetscCall(MatAssemblyEnd(aux, MAT_FINAL_ASSEMBLY)); 1251f1580f4eSBarry Smith PetscCall(MatDenseRestoreArrayWrite(sum[0], &ptr)); 1252f1580f4eSBarry Smith PetscCall(MatDestroy(sum)); 1253f1580f4eSBarry Smith } 1254f1580f4eSBarry Smith PetscCall(MatSetOption(aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE)); 1255f1580f4eSBarry Smith /* left-hand side of GenEO, with the same sparsity pattern as PCASM subdomain solvers */ 1256f1580f4eSBarry Smith PetscCall(PetscObjectCompose((PetscObject)(*sub)[0], "_PCHPDDM_Neumann_Mat", (PetscObject)aux)); 1257f1580f4eSBarry Smith } 1258f1580f4eSBarry Smith PetscCall(ISDestroy(irow)); 1259f1580f4eSBarry Smith PetscCall(MatDestroySubMatrices(1, &M)); 12603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1261f1580f4eSBarry Smith } 1262f1580f4eSBarry Smith 126313044ca3SPierre Jolivet static PetscErrorCode PCApply_Nest(PC pc, Vec x, Vec y) 126413044ca3SPierre Jolivet { 126513044ca3SPierre Jolivet Mat A; 126613044ca3SPierre Jolivet MatSolverType type; 126713044ca3SPierre Jolivet IS is[2]; 126813044ca3SPierre Jolivet PetscBool flg; 126913044ca3SPierre Jolivet std::pair<PC, Vec[2]> *p; 127013044ca3SPierre Jolivet 127113044ca3SPierre Jolivet PetscFunctionBegin; 127213044ca3SPierre Jolivet PetscCall(PCShellGetContext(pc, &p)); 127313044ca3SPierre Jolivet PetscCall(PCGetOperators(p->first, &A, nullptr)); 127413044ca3SPierre Jolivet PetscCall(MatNestGetISs(A, is, nullptr)); 127513044ca3SPierre Jolivet PetscCall(PCFactorGetMatSolverType(p->first, &type)); 127613044ca3SPierre Jolivet PetscCall(PCFactorGetMatrix(p->first, &A)); 127713044ca3SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &flg)); 127813044ca3SPierre Jolivet if (flg) { 127913044ca3SPierre Jolivet #if PetscDefined(HAVE_MUMPS) 128013044ca3SPierre Jolivet PetscCall(MatMumpsSetIcntl(A, 26, 1)); /* reduction/condensation phase followed by Schur complement solve */ 128113044ca3SPierre Jolivet #endif 128213044ca3SPierre Jolivet } 128313044ca3SPierre Jolivet PetscCall(VecISCopy(p->second[0], is[1], SCATTER_FORWARD, x)); /* assign the RHS associated to the Schur complement */ 128413044ca3SPierre Jolivet PetscCall(PCApply(p->first, p->second[0], p->second[1])); 128513044ca3SPierre Jolivet PetscCall(VecISCopy(p->second[1], is[1], SCATTER_REVERSE, y)); /* retrieve the partial solution associated to the Schur complement */ 128613044ca3SPierre Jolivet if (flg) { 128713044ca3SPierre Jolivet #if PetscDefined(HAVE_MUMPS) 128813044ca3SPierre Jolivet PetscCall(MatMumpsSetIcntl(A, 26, -1)); /* default ICNTL(26) value in PETSc */ 128913044ca3SPierre Jolivet #endif 129013044ca3SPierre Jolivet } 129113044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 129213044ca3SPierre Jolivet } 129313044ca3SPierre Jolivet 129413044ca3SPierre Jolivet static PetscErrorCode PCView_Nest(PC pc, PetscViewer viewer) 129513044ca3SPierre Jolivet { 129613044ca3SPierre Jolivet std::pair<PC, Vec[2]> *p; 129713044ca3SPierre Jolivet 129813044ca3SPierre Jolivet PetscFunctionBegin; 129913044ca3SPierre Jolivet PetscCall(PCShellGetContext(pc, &p)); 130013044ca3SPierre Jolivet PetscCall(PCView(p->first, viewer)); 130113044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 130213044ca3SPierre Jolivet } 130313044ca3SPierre Jolivet 130413044ca3SPierre Jolivet static PetscErrorCode PCDestroy_Nest(PC pc) 130513044ca3SPierre Jolivet { 130613044ca3SPierre Jolivet std::pair<PC, Vec[2]> *p; 130713044ca3SPierre Jolivet 130813044ca3SPierre Jolivet PetscFunctionBegin; 130913044ca3SPierre Jolivet PetscCall(PCShellGetContext(pc, &p)); 131013044ca3SPierre Jolivet PetscCall(VecDestroy(p->second)); 131113044ca3SPierre Jolivet PetscCall(VecDestroy(p->second + 1)); 131213044ca3SPierre Jolivet PetscCall(PCDestroy(&p->first)); 131313044ca3SPierre Jolivet PetscCall(PetscFree(p)); 131413044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 131513044ca3SPierre Jolivet } 131613044ca3SPierre Jolivet 131713044ca3SPierre Jolivet template <bool T = false> 131813044ca3SPierre Jolivet static PetscErrorCode MatMult_Schur(Mat A, Vec x, Vec y) 131913044ca3SPierre Jolivet { 132013044ca3SPierre Jolivet std::tuple<Mat, VecScatter, Vec[2]> *ctx; 132113044ca3SPierre Jolivet 132213044ca3SPierre Jolivet PetscFunctionBegin; 132313044ca3SPierre Jolivet PetscCall(MatShellGetContext(A, &ctx)); 132413044ca3SPierre Jolivet PetscCall(VecScatterBegin(std::get<1>(*ctx), x, std::get<2>(*ctx)[0], INSERT_VALUES, SCATTER_FORWARD)); /* local Vec with overlap */ 132513044ca3SPierre Jolivet PetscCall(VecScatterEnd(std::get<1>(*ctx), x, std::get<2>(*ctx)[0], INSERT_VALUES, SCATTER_FORWARD)); 132613044ca3SPierre Jolivet if (!T) PetscCall(MatMult(std::get<0>(*ctx), std::get<2>(*ctx)[0], std::get<2>(*ctx)[1])); /* local Schur complement */ 132713044ca3SPierre Jolivet else PetscCall(MatMultTranspose(std::get<0>(*ctx), std::get<2>(*ctx)[0], std::get<2>(*ctx)[1])); 132813044ca3SPierre Jolivet PetscCall(VecSet(y, 0.0)); 132913044ca3SPierre 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 */ 133013044ca3SPierre Jolivet PetscCall(VecScatterEnd(std::get<1>(*ctx), std::get<2>(*ctx)[1], y, ADD_VALUES, SCATTER_REVERSE)); 133113044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 133213044ca3SPierre Jolivet } 133313044ca3SPierre Jolivet 133413044ca3SPierre Jolivet static PetscErrorCode MatDestroy_Schur(Mat A) 133513044ca3SPierre Jolivet { 133613044ca3SPierre Jolivet std::tuple<Mat, VecScatter, Vec[2]> *ctx; 133713044ca3SPierre Jolivet 133813044ca3SPierre Jolivet PetscFunctionBegin; 133913044ca3SPierre Jolivet PetscCall(MatShellGetContext(A, &ctx)); 134013044ca3SPierre Jolivet PetscCall(VecDestroy(std::get<2>(*ctx))); 134113044ca3SPierre Jolivet PetscCall(VecDestroy(std::get<2>(*ctx) + 1)); 134213044ca3SPierre Jolivet PetscCall(PetscFree(ctx)); 134313044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 134413044ca3SPierre Jolivet } 134513044ca3SPierre Jolivet 134613044ca3SPierre Jolivet static PetscErrorCode MatMult_SchurCorrection(Mat A, Vec x, Vec y) 134713044ca3SPierre Jolivet { 134813044ca3SPierre Jolivet PC pc; 134913044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx; 135013044ca3SPierre Jolivet 135113044ca3SPierre Jolivet PetscFunctionBegin; 135213044ca3SPierre Jolivet PetscCall(MatShellGetContext(A, &ctx)); 135313044ca3SPierre Jolivet pc = ((PC_HPDDM *)std::get<0>(*ctx)[0]->data)->levels[0]->ksp->pc; 135413044ca3SPierre 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 */ 135513044ca3SPierre Jolivet PetscCall(MatMult(std::get<1>(*ctx)[0], x, std::get<3>(*ctx)[1])); /* A_01 x */ 135613044ca3SPierre Jolivet PetscCall(PCHPDDMDeflate_Private(pc, std::get<3>(*ctx)[1], std::get<3>(*ctx)[1])); /* Q_0 A_01 x */ 135713044ca3SPierre 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 */ 135813044ca3SPierre 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 */ 135913044ca3SPierre Jolivet } else { 136013044ca3SPierre Jolivet PetscCall(PCApply(std::get<0>(*ctx)[1], x, std::get<3>(*ctx)[0])); /* M_S^-1 x */ 136113044ca3SPierre Jolivet PetscCall(MatMult(std::get<1>(*ctx)[0], std::get<3>(*ctx)[0], std::get<3>(*ctx)[1])); /* A_01 M_S^-1 x */ 136213044ca3SPierre Jolivet PetscCall(PCHPDDMDeflate_Private(pc, std::get<3>(*ctx)[1], std::get<3>(*ctx)[1])); /* Q_0 A_01 M_S^-1 x */ 136313044ca3SPierre 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 */ 136413044ca3SPierre Jolivet } 136513044ca3SPierre Jolivet PetscCall(VecAXPY(y, -1.0, x)); /* y -= x, preconditioned eq. (24) of https://hal.science/hal-02343808v6/document (with a sign flip) */ 136613044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 136713044ca3SPierre Jolivet } 136813044ca3SPierre Jolivet 136913044ca3SPierre Jolivet static PetscErrorCode MatView_SchurCorrection(Mat A, PetscViewer viewer) 137013044ca3SPierre Jolivet { 137113044ca3SPierre Jolivet PetscBool ascii; 137213044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx; 137313044ca3SPierre Jolivet 137413044ca3SPierre Jolivet PetscFunctionBegin; 137513044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &ascii)); 137613044ca3SPierre Jolivet if (ascii) { 137713044ca3SPierre Jolivet PetscCall(MatShellGetContext(A, &ctx)); 137813044ca3SPierre 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)")); 137913044ca3SPierre Jolivet PetscCall(PCView(std::get<0>(*ctx)[1], viewer)); /* no need to PCView(Q_0) since it will be done by PCFIELDSPLIT */ 138013044ca3SPierre Jolivet } 138113044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 138213044ca3SPierre Jolivet } 138313044ca3SPierre Jolivet 138413044ca3SPierre Jolivet static PetscErrorCode MatDestroy_SchurCorrection(Mat A) 138513044ca3SPierre Jolivet { 138613044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx; 138713044ca3SPierre Jolivet 138813044ca3SPierre Jolivet PetscFunctionBegin; 138913044ca3SPierre Jolivet PetscCall(MatShellGetContext(A, &ctx)); 139013044ca3SPierre Jolivet PetscCall(VecDestroy(std::get<3>(*ctx))); 139113044ca3SPierre Jolivet PetscCall(VecDestroy(std::get<3>(*ctx) + 1)); 139213044ca3SPierre Jolivet PetscCall(VecDestroy(std::get<3>(*ctx) + 2)); 139313044ca3SPierre Jolivet PetscCall(PCDestroy(std::get<0>(*ctx) + 1)); 139413044ca3SPierre Jolivet PetscCall(PetscFree(ctx)); 139513044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 139613044ca3SPierre Jolivet } 139713044ca3SPierre Jolivet 139813044ca3SPierre Jolivet static PetscErrorCode KSPPreSolve_SchurCorrection(KSP, Vec b, Vec, void *context) 139913044ca3SPierre Jolivet { 140013044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx = reinterpret_cast<std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *>(context); 140113044ca3SPierre Jolivet 1402a6b3e571SPierre Jolivet PetscFunctionBegin; 140313044ca3SPierre Jolivet if (std::get<2>(*ctx) == PC_LEFT || std::get<2>(*ctx) == PC_SIDE_DEFAULT) { 140413044ca3SPierre Jolivet PetscCall(PCApply(std::get<0>(*ctx)[1], b, std::get<3>(*ctx)[2])); 140513044ca3SPierre 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 */ 140613044ca3SPierre Jolivet } 140713044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 140813044ca3SPierre Jolivet } 140913044ca3SPierre Jolivet 141013044ca3SPierre Jolivet static PetscErrorCode KSPPostSolve_SchurCorrection(KSP, Vec b, Vec x, void *context) 141113044ca3SPierre Jolivet { 141213044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx = reinterpret_cast<std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *>(context); 141313044ca3SPierre Jolivet 1414a6b3e571SPierre Jolivet PetscFunctionBegin; 141513044ca3SPierre 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 */ 141613044ca3SPierre Jolivet else { 141713044ca3SPierre Jolivet PetscCall(PCApply(std::get<0>(*ctx)[1], x, std::get<3>(*ctx)[2])); 141813044ca3SPierre Jolivet PetscCall(VecCopy(std::get<3>(*ctx)[2], x)); /* replace x by M^-1 x */ 141913044ca3SPierre Jolivet } 142013044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 142113044ca3SPierre Jolivet } 142213044ca3SPierre Jolivet 1423*9bb5c669SPierre Jolivet static PetscErrorCode MatMult_Harmonic(Mat, Vec, Vec); 1424*9bb5c669SPierre Jolivet static PetscErrorCode MatMultTranspose_Harmonic(Mat, Vec, Vec); 1425*9bb5c669SPierre Jolivet static PetscErrorCode MatProduct_AB_Harmonic(Mat, Mat, Mat, void *); 1426*9bb5c669SPierre Jolivet static PetscErrorCode MatDestroy_Harmonic(Mat); 1427*9bb5c669SPierre Jolivet 1428d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_HPDDM(PC pc) 1429d71ae5a4SJacob Faibussowitsch { 1430f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 1431f1580f4eSBarry Smith PC inner; 1432f1580f4eSBarry Smith KSP *ksp; 143313044ca3SPierre Jolivet Mat *sub, A, P, N, C = nullptr, uaux = nullptr, weighted, subA[2], S; 1434f1580f4eSBarry Smith Vec xin, v; 1435f1580f4eSBarry Smith std::vector<Vec> initial; 1436db4a47b3SPierre Jolivet IS is[1], loc, uis = data->is, unsorted = nullptr; 1437f1580f4eSBarry Smith ISLocalToGlobalMapping l2g; 1438f1580f4eSBarry Smith char prefix[256]; 1439f1580f4eSBarry Smith const char *pcpre; 1440f1580f4eSBarry Smith const PetscScalar *const *ev; 1441*9bb5c669SPierre Jolivet PetscInt n, requested = data->N, reused = 0, overlap = -1; 1442f1580f4eSBarry Smith MatStructure structure = UNKNOWN_NONZERO_PATTERN; 1443f1580f4eSBarry Smith PetscBool subdomains = PETSC_FALSE, flg = PETSC_FALSE, ismatis, swap = PETSC_FALSE, algebraic = PETSC_FALSE, block = PETSC_FALSE; 1444f1580f4eSBarry Smith DM dm; 144513044ca3SPierre Jolivet std::tuple<PC[2], Mat[2], PCSide, Vec[3]> *ctx = nullptr; 1446398c7888SPierre Jolivet #if PetscDefined(USE_DEBUG) 1447db4a47b3SPierre Jolivet IS dis = nullptr; 1448db4a47b3SPierre Jolivet Mat daux = nullptr; 1449398c7888SPierre Jolivet #endif 1450f1580f4eSBarry Smith 1451f1580f4eSBarry Smith PetscFunctionBegin; 1452f1580f4eSBarry Smith PetscCheck(data->levels && data->levels[0], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not a single level allocated"); 1453f1580f4eSBarry Smith PetscCall(PCGetOptionsPrefix(pc, &pcpre)); 1454f1580f4eSBarry Smith PetscCall(PCGetOperators(pc, &A, &P)); 1455f1580f4eSBarry Smith if (!data->levels[0]->ksp) { 1456f1580f4eSBarry Smith PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->ksp)); 14573821be0aSBarry Smith PetscCall(KSPSetNestLevel(data->levels[0]->ksp, pc->kspnestlevel)); 1458f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_%s_", pcpre ? pcpre : "", data->N > 1 ? "levels_1" : "coarse")); 1459f1580f4eSBarry Smith PetscCall(KSPSetOptionsPrefix(data->levels[0]->ksp, prefix)); 1460f1580f4eSBarry Smith PetscCall(KSPSetType(data->levels[0]->ksp, KSPPREONLY)); 1461f1580f4eSBarry Smith } else if (data->levels[0]->ksp->pc && data->levels[0]->ksp->pc->setupcalled == 1 && data->levels[0]->ksp->pc->reusepreconditioner) { 1462f1580f4eSBarry Smith /* if the fine-level PCSHELL exists, its setup has succeeded, and one wants to reuse it, */ 1463f1580f4eSBarry Smith /* then just propagate the appropriate flag to the coarser levels */ 1464f1580f4eSBarry Smith for (n = 0; n < PETSC_PCHPDDM_MAXLEVELS && data->levels[n]; ++n) { 1465f1580f4eSBarry Smith /* the following KSP and PC may be NULL for some processes, hence the check */ 1466f1580f4eSBarry Smith if (data->levels[n]->ksp) PetscCall(KSPSetReusePreconditioner(data->levels[n]->ksp, PETSC_TRUE)); 1467f1580f4eSBarry Smith if (data->levels[n]->pc) PetscCall(PCSetReusePreconditioner(data->levels[n]->pc, PETSC_TRUE)); 1468f1580f4eSBarry Smith } 1469f1580f4eSBarry Smith /* early bail out because there is nothing to do */ 14703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1471f1580f4eSBarry Smith } else { 1472f1580f4eSBarry Smith /* reset coarser levels */ 1473f1580f4eSBarry Smith for (n = 1; n < PETSC_PCHPDDM_MAXLEVELS && data->levels[n]; ++n) { 1474f1580f4eSBarry Smith if (data->levels[n]->ksp && data->levels[n]->ksp->pc && data->levels[n]->ksp->pc->setupcalled == 1 && data->levels[n]->ksp->pc->reusepreconditioner && n < data->N) { 1475f1580f4eSBarry Smith reused = data->N - n; 1476f1580f4eSBarry Smith break; 1477f1580f4eSBarry Smith } 1478f1580f4eSBarry Smith PetscCall(KSPDestroy(&data->levels[n]->ksp)); 1479f1580f4eSBarry Smith PetscCall(PCDestroy(&data->levels[n]->pc)); 1480f1580f4eSBarry Smith } 1481f1580f4eSBarry Smith /* check if some coarser levels are being reused */ 1482f1580f4eSBarry Smith PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &reused, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)pc))); 1483f1580f4eSBarry Smith const int *addr = data->levels[0]->P ? data->levels[0]->P->getAddrLocal() : &HPDDM::i__0; 1484f1580f4eSBarry Smith 1485f1580f4eSBarry Smith if (addr != &HPDDM::i__0 && reused != data->N - 1) { 1486f1580f4eSBarry Smith /* reuse previously computed eigenvectors */ 1487f1580f4eSBarry Smith ev = data->levels[0]->P->getVectors(); 1488f1580f4eSBarry Smith if (ev) { 1489f1580f4eSBarry Smith initial.reserve(*addr); 1490f1580f4eSBarry Smith PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, data->levels[0]->P->getDof(), ev[0], &xin)); 1491f1580f4eSBarry Smith for (n = 0; n < *addr; ++n) { 1492f1580f4eSBarry Smith PetscCall(VecDuplicate(xin, &v)); 1493f1580f4eSBarry Smith PetscCall(VecPlaceArray(xin, ev[n])); 1494f1580f4eSBarry Smith PetscCall(VecCopy(xin, v)); 1495f1580f4eSBarry Smith initial.emplace_back(v); 1496f1580f4eSBarry Smith PetscCall(VecResetArray(xin)); 1497f1580f4eSBarry Smith } 1498f1580f4eSBarry Smith PetscCall(VecDestroy(&xin)); 1499f1580f4eSBarry Smith } 1500f1580f4eSBarry Smith } 1501f1580f4eSBarry Smith } 1502f1580f4eSBarry Smith data->N -= reused; 1503f1580f4eSBarry Smith PetscCall(KSPSetOperators(data->levels[0]->ksp, A, P)); 1504f1580f4eSBarry Smith 1505f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)P, MATIS, &ismatis)); 1506f1580f4eSBarry Smith if (!data->is && !ismatis) { 1507db4a47b3SPierre Jolivet PetscErrorCode (*create)(DM, IS *, Mat *, PetscErrorCode (**)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void **) = nullptr; 1508db4a47b3SPierre Jolivet PetscErrorCode (*usetup)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *) = nullptr; 1509db4a47b3SPierre Jolivet void *uctx = nullptr; 1510f1580f4eSBarry Smith 1511f1580f4eSBarry Smith /* first see if we can get the data from the DM */ 1512f1580f4eSBarry Smith PetscCall(MatGetDM(P, &dm)); 1513f1580f4eSBarry Smith if (!dm) PetscCall(MatGetDM(A, &dm)); 1514f1580f4eSBarry Smith if (!dm) PetscCall(PCGetDM(pc, &dm)); 1515f1580f4eSBarry Smith if (dm) { /* this is the hook for DMPLEX and DMDA for which the auxiliary Mat is the local Neumann matrix */ 1516f1580f4eSBarry Smith PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", &create)); 1517f1580f4eSBarry Smith if (create) { 1518f1580f4eSBarry Smith PetscCall((*create)(dm, &uis, &uaux, &usetup, &uctx)); 1519c8ea6600SPierre 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 */ 1520f1580f4eSBarry Smith } 1521f1580f4eSBarry Smith } 1522f1580f4eSBarry Smith if (!create) { 1523f1580f4eSBarry Smith if (!uis) { 1524f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_IS", (PetscObject *)&uis)); 1525f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)uis)); 1526f1580f4eSBarry Smith } 1527f1580f4eSBarry Smith if (!uaux) { 1528f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Neumann_Mat", (PetscObject *)&uaux)); 1529f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)uaux)); 1530f1580f4eSBarry Smith } 1531f1580f4eSBarry Smith /* look inside the Pmat instead of the PC, needed for MatSchurComplementComputeExplicitOperator() */ 1532f1580f4eSBarry Smith if (!uis) { 1533f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)P, "_PCHPDDM_Neumann_IS", (PetscObject *)&uis)); 1534f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)uis)); 1535f1580f4eSBarry Smith } 1536f1580f4eSBarry Smith if (!uaux) { 1537f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)P, "_PCHPDDM_Neumann_Mat", (PetscObject *)&uaux)); 1538f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)uaux)); 1539f1580f4eSBarry Smith } 1540f1580f4eSBarry Smith } 1541f1580f4eSBarry Smith PetscCall(PCHPDDMSetAuxiliaryMat(pc, uis, uaux, usetup, uctx)); 1542f1580f4eSBarry Smith PetscCall(MatDestroy(&uaux)); 1543f1580f4eSBarry Smith PetscCall(ISDestroy(&uis)); 1544f1580f4eSBarry Smith } 1545f1580f4eSBarry Smith 1546f1580f4eSBarry Smith if (!ismatis) { 1547f1580f4eSBarry Smith PetscCall(PCHPDDMSetUpNeumannOverlap_Private(pc)); 1548db4a47b3SPierre Jolivet PetscCall(PetscOptionsGetBool(nullptr, pcpre, "-pc_hpddm_block_splitting", &block, nullptr)); 1549*9bb5c669SPierre Jolivet PetscCall(PetscOptionsGetInt(nullptr, pcpre, "-pc_hpddm_harmonic_overlap", &overlap, nullptr)); 15505e642048SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)P, MATSCHURCOMPLEMENT, &flg)); 15515e642048SPierre Jolivet if (data->is || (data->N > 1 && flg)) { 1552*9bb5c669SPierre Jolivet if (block || overlap != -1) { 155302800ff6SPierre Jolivet PetscCall(ISDestroy(&data->is)); 155402800ff6SPierre Jolivet PetscCall(MatDestroy(&data->aux)); 15555e642048SPierre Jolivet } else if (data->N > 1 && flg) { 155613044ca3SPierre Jolivet PCHPDDMSchurPreType type = PC_HPDDM_SCHUR_PRE_GENEO; 155713044ca3SPierre Jolivet 155813044ca3SPierre Jolivet PetscCall(PetscOptionsGetEnum(nullptr, pcpre, "-pc_hpddm_schur_precondition", PCHPDDMSchurPreTypes, (PetscEnum *)&type, &flg)); 155913044ca3SPierre Jolivet if (type == PC_HPDDM_SCHUR_PRE_LEAST_SQUARES) { 156013044ca3SPierre Jolivet PetscCall(ISDestroy(&data->is)); /* destroy any previously user-set objects since they will be set automatically */ 156113044ca3SPierre Jolivet PetscCall(MatDestroy(&data->aux)); 156213044ca3SPierre Jolivet } else if (type == PC_HPDDM_SCHUR_PRE_GENEO) { 156313044ca3SPierre Jolivet PetscContainer container = nullptr; 156413044ca3SPierre Jolivet 156513044ca3SPierre Jolivet PetscCall(PetscObjectQuery((PetscObject)pc, "_PCHPDDM_Schur", (PetscObject *)&container)); 156613044ca3SPierre Jolivet if (!container) { /* first call to PCSetUp() on the PC associated to the Schur complement */ 156713044ca3SPierre Jolivet PC_HPDDM *data_00; 156813044ca3SPierre Jolivet KSP ksp, inner_ksp; 156913044ca3SPierre Jolivet PC pc_00; 157013044ca3SPierre Jolivet char *prefix; 157113044ca3SPierre Jolivet PetscReal norm; 157213044ca3SPierre Jolivet 157313044ca3SPierre Jolivet PetscCall(MatSchurComplementGetKSP(P, &ksp)); 157413044ca3SPierre Jolivet PetscCall(KSPGetPC(ksp, &pc_00)); 157513044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)pc_00, PCHPDDM, &flg)); 157613044ca3SPierre Jolivet PetscCheck(flg, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition geneo and -%spc_type %s (!= %s)", pcpre ? pcpre : "", ((PetscObject)pc_00)->prefix ? ((PetscObject)pc_00)->prefix : "", 157713044ca3SPierre Jolivet ((PetscObject)pc_00)->type_name, PCHPDDM); 157813044ca3SPierre Jolivet data_00 = (PC_HPDDM *)pc_00->data; 157913044ca3SPierre Jolivet PetscCheck(data_00->N == 2, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition geneo and %" PetscInt_FMT " level%s instead of 2 for the A00 block -%s", pcpre ? pcpre : "", data_00->N, data_00->N > 1 ? "s" : "", 158013044ca3SPierre Jolivet ((PetscObject)pc_00)->prefix); 158113044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)data_00->levels[0]->pc, PCASM, &flg)); 158213044ca3SPierre Jolivet PetscCheck(flg, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition geneo and -%spc_type %s (!= %s)", pcpre ? pcpre : "", ((PetscObject)data_00->levels[0]->pc)->prefix, 158313044ca3SPierre Jolivet ((PetscObject)data_00->levels[0]->pc)->type_name, PCASM); 158413044ca3SPierre Jolivet PetscCheck(data->Neumann == PETSC_BOOL3_TRUE, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition geneo and -%spc_hpddm_has_neumann != true", pcpre ? pcpre : "", pcpre ? pcpre : ""); 15855e642048SPierre Jolivet if (PetscDefined(USE_DEBUG) || !data->is) { 15865e642048SPierre Jolivet Mat A01, A10, B = nullptr, C = nullptr, *sub; 158713044ca3SPierre Jolivet 15885e642048SPierre Jolivet PetscCall(MatSchurComplementGetSubMatrices(P, &A, nullptr, &A01, &A10, nullptr)); 15895e642048SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATTRANSPOSEVIRTUAL, &flg)); 15905e642048SPierre Jolivet if (flg) { 15915e642048SPierre Jolivet PetscCall(MatTransposeGetMat(A10, &C)); 15925e642048SPierre Jolivet PetscCall(MatTranspose(C, MAT_INITIAL_MATRIX, &B)); 15935e642048SPierre Jolivet } else { 15945e642048SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATHERMITIANTRANSPOSEVIRTUAL, &flg)); 15955e642048SPierre Jolivet if (flg) { 15965e642048SPierre Jolivet PetscCall(MatHermitianTransposeGetMat(A10, &C)); 15975e642048SPierre Jolivet PetscCall(MatHermitianTranspose(C, MAT_INITIAL_MATRIX, &B)); 15985e642048SPierre Jolivet } 15995e642048SPierre Jolivet } 16005e642048SPierre Jolivet if (!B) { 16015e642048SPierre Jolivet B = A10; 16025e642048SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)B)); 16035e642048SPierre Jolivet } else if (!data->is) { 16045e642048SPierre Jolivet PetscCall(PetscObjectTypeCompareAny((PetscObject)A01, &flg, MATTRANSPOSEVIRTUAL, MATHERMITIANTRANSPOSEVIRTUAL, "")); 16055e642048SPierre Jolivet if (!flg) C = A01; 16065e642048SPierre Jolivet } 160713044ca3SPierre Jolivet PetscCall(ISCreateStride(PETSC_COMM_SELF, B->rmap->N, 0, 1, &uis)); 16085e642048SPierre Jolivet PetscCall(ISSetIdentity(uis)); 16095e642048SPierre Jolivet if (!data->is) { 16105e642048SPierre Jolivet if (C) PetscCall(PetscObjectReference((PetscObject)C)); 16115e642048SPierre Jolivet else PetscCall(MatTranspose(B, MAT_INITIAL_MATRIX, &C)); 16125e642048SPierre Jolivet PetscCall(ISDuplicate(data_00->is, is)); 16135e642048SPierre Jolivet PetscCall(MatIncreaseOverlap(A, 1, is, 1)); 16145e642048SPierre Jolivet PetscCall(MatSetOption(C, MAT_SUBMAT_SINGLEIS, PETSC_TRUE)); 16155e642048SPierre Jolivet PetscCall(MatCreateSubMatrices(C, 1, is, &uis, MAT_INITIAL_MATRIX, &sub)); 16165e642048SPierre Jolivet PetscCall(MatDestroy(&C)); 16175e642048SPierre Jolivet PetscCall(MatTranspose(sub[0], MAT_INITIAL_MATRIX, &C)); 16185e642048SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &sub)); 16195e642048SPierre Jolivet PetscCall(MatFindNonzeroRows(C, &data->is)); 16205e642048SPierre Jolivet PetscCall(MatDestroy(&C)); 16215e642048SPierre Jolivet PetscCall(ISDestroy(is)); 16225e642048SPierre Jolivet } 16235e642048SPierre Jolivet if (PetscDefined(USE_DEBUG)) { 16245e642048SPierre Jolivet PetscCall(PCHPDDMCheckSymmetry_Private(pc, A01, A10)); 162513044ca3SPierre 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 */ 162613044ca3SPierre Jolivet PetscCall(ISDestroy(&uis)); 162713044ca3SPierre Jolivet PetscCall(ISDuplicate(data->is, &uis)); 162813044ca3SPierre Jolivet PetscCall(ISSort(uis)); 16295e642048SPierre Jolivet PetscCall(ISComplement(uis, 0, B->rmap->N, is)); 163013044ca3SPierre Jolivet PetscCall(MatDuplicate(sub[0], MAT_COPY_VALUES, &C)); 16315e642048SPierre Jolivet PetscCall(MatZeroRowsIS(C, is[0], 0.0, nullptr, nullptr)); 16325e642048SPierre Jolivet PetscCall(ISDestroy(is)); 163313044ca3SPierre Jolivet PetscCall(MatMultEqual(sub[0], C, 20, &flg)); 163413044ca3SPierre 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 */ 163513044ca3SPierre Jolivet PetscCall(MatDestroy(&C)); 163613044ca3SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &sub)); 163713044ca3SPierre Jolivet } 16385e642048SPierre Jolivet PetscCall(ISDestroy(&uis)); 16395e642048SPierre Jolivet PetscCall(MatDestroy(&B)); 16405e642048SPierre Jolivet } 164113044ca3SPierre Jolivet if (data->aux) PetscCall(MatNorm(data->aux, NORM_FROBENIUS, &norm)); 164213044ca3SPierre Jolivet else norm = 0.0; 164313044ca3SPierre Jolivet PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &norm, 1, MPIU_REAL, MPI_MAX, PetscObjectComm((PetscObject)P))); 16440594bca0SPierre Jolivet if (norm < PETSC_MACHINE_EPSILON * static_cast<PetscReal>(10.0)) { /* if A11 is near zero, e.g., Stokes equation, build a diagonal auxiliary (Neumann) Mat which is just a small diagonal weighted by the inverse of the multiplicity */ 164513044ca3SPierre Jolivet VecScatter scatter; 164613044ca3SPierre Jolivet Vec x; 164713044ca3SPierre Jolivet const PetscScalar *read; 164813044ca3SPierre Jolivet PetscScalar *write; 164913044ca3SPierre Jolivet 165013044ca3SPierre Jolivet PetscCall(MatDestroy(&data->aux)); 165113044ca3SPierre Jolivet PetscCall(ISGetLocalSize(data->is, &n)); 165213044ca3SPierre Jolivet PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)P), n, PETSC_DECIDE, &x)); 165313044ca3SPierre Jolivet PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)P), n, PETSC_DECIDE, &v)); 165413044ca3SPierre Jolivet PetscCall(VecScatterCreate(x, data->is, v, nullptr, &scatter)); 165513044ca3SPierre Jolivet PetscCall(VecSet(v, 1.0)); 165613044ca3SPierre Jolivet PetscCall(VecSet(x, 1.0)); 165713044ca3SPierre Jolivet PetscCall(VecScatterBegin(scatter, v, x, ADD_VALUES, SCATTER_REVERSE)); 165813044ca3SPierre Jolivet PetscCall(VecScatterEnd(scatter, v, x, ADD_VALUES, SCATTER_REVERSE)); /* v has the multiplicity of all unknowns on the overlap */ 165913044ca3SPierre Jolivet PetscCall(VecScatterDestroy(&scatter)); 166013044ca3SPierre Jolivet PetscCall(VecDestroy(&v)); 166113044ca3SPierre Jolivet PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &v)); 166213044ca3SPierre Jolivet PetscCall(VecGetArrayRead(x, &read)); 166313044ca3SPierre Jolivet PetscCall(VecGetArrayWrite(v, &write)); 16640594bca0SPierre Jolivet PetscCallCXX(std::transform(read, read + n, write, [](const PetscScalar &m) { return PETSC_SMALL / (static_cast<PetscReal>(1000.0) * m); })); 166513044ca3SPierre Jolivet PetscCall(VecRestoreArrayRead(x, &read)); 166613044ca3SPierre Jolivet PetscCall(VecRestoreArrayWrite(v, &write)); 166713044ca3SPierre Jolivet PetscCall(VecDestroy(&x)); 166813044ca3SPierre Jolivet PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF, n, n, 1, nullptr, &data->aux)); 166913044ca3SPierre Jolivet PetscCall(MatSetOption(data->aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE)); 167013044ca3SPierre Jolivet PetscCall(MatDiagonalSet(data->aux, v, ADD_VALUES)); 167113044ca3SPierre Jolivet PetscCall(MatSetOption(data->aux, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE)); 167213044ca3SPierre Jolivet PetscCall(VecDestroy(&v)); 167313044ca3SPierre Jolivet } 167413044ca3SPierre Jolivet uis = data->is; 167513044ca3SPierre Jolivet uaux = data->aux; 167613044ca3SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)uis)); 167713044ca3SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)uaux)); 167813044ca3SPierre Jolivet PetscCall(PetscStrallocpy(pcpre, &prefix)); 167913044ca3SPierre Jolivet PetscCall(PCSetOptionsPrefix(pc, nullptr)); 168013044ca3SPierre Jolivet PetscCall(PCSetType(pc, PCKSP)); /* replace the PC associated to the Schur complement by PCKSP */ 168113044ca3SPierre Jolivet PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &inner_ksp)); /* new KSP that will be attached to the previously set PC */ 168213044ca3SPierre Jolivet PetscCall(PetscObjectGetTabLevel((PetscObject)pc, &n)); 168313044ca3SPierre Jolivet PetscCall(PetscObjectSetTabLevel((PetscObject)inner_ksp, n + 2)); 168413044ca3SPierre Jolivet PetscCall(KSPSetOperators(inner_ksp, pc->mat, pc->pmat)); 168513044ca3SPierre Jolivet PetscCall(KSPSetOptionsPrefix(inner_ksp, std::string(std::string(prefix) + "pc_hpddm_").c_str())); 168613044ca3SPierre Jolivet PetscCall(KSPSetSkipPCSetFromOptions(inner_ksp, PETSC_TRUE)); 168713044ca3SPierre Jolivet PetscCall(KSPSetFromOptions(inner_ksp)); 168813044ca3SPierre Jolivet PetscCall(KSPGetPC(inner_ksp, &inner)); 168913044ca3SPierre Jolivet PetscCall(PCSetOptionsPrefix(inner, nullptr)); 169013044ca3SPierre Jolivet PetscCall(PCSetType(inner, PCNONE)); /* no preconditioner since the action of M^-1 A or A M^-1 will be computed by the Amat */ 169113044ca3SPierre Jolivet PetscCall(PCKSPSetKSP(pc, inner_ksp)); 169213044ca3SPierre Jolivet PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)pc), &container)); 169313044ca3SPierre Jolivet PetscCall(PetscNew(&ctx)); /* context to pass data around for the inner-most PC, which will be a proper PCHPDDM */ 169413044ca3SPierre Jolivet PetscCall(PetscContainerSetPointer(container, ctx)); 169513044ca3SPierre Jolivet std::get<0>(*ctx)[0] = pc_00; /* for coarse correction on the primal (e.g., velocity) space */ 169613044ca3SPierre Jolivet PetscCall(PCCreate(PetscObjectComm((PetscObject)pc), &std::get<0>(*ctx)[1])); 169713044ca3SPierre Jolivet PetscCall(PCSetOptionsPrefix(std::get<0>(*ctx)[1], prefix)); 169813044ca3SPierre Jolivet PetscCall(PetscFree(prefix)); 169913044ca3SPierre Jolivet PetscCall(PCSetOperators(std::get<0>(*ctx)[1], pc->mat, pc->pmat)); 170013044ca3SPierre Jolivet PetscCall(PCSetType(std::get<0>(*ctx)[1], PCHPDDM)); 170113044ca3SPierre 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 */ 170213044ca3SPierre Jolivet PetscCall(PCSetFromOptions(std::get<0>(*ctx)[1])); 170313044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)uis)); 170413044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)uaux)); 170513044ca3SPierre 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 */ 170613044ca3SPierre Jolivet PetscCall(MatShellSetOperation(S, MATOP_MULT, (void (*)(void))MatMult_SchurCorrection)); 170713044ca3SPierre Jolivet PetscCall(MatShellSetOperation(S, MATOP_VIEW, (void (*)(void))MatView_SchurCorrection)); 170813044ca3SPierre Jolivet PetscCall(MatShellSetOperation(S, MATOP_DESTROY, (void (*)(void))MatDestroy_SchurCorrection)); 170913044ca3SPierre Jolivet PetscCall(KSPGetPCSide(inner_ksp, &(std::get<2>(*ctx)))); 171013044ca3SPierre Jolivet if (std::get<2>(*ctx) == PC_LEFT || std::get<2>(*ctx) == PC_SIDE_DEFAULT) { 171113044ca3SPierre Jolivet PetscCall(KSPSetPreSolve(inner_ksp, KSPPreSolve_SchurCorrection, ctx)); 171213044ca3SPierre Jolivet } else { /* no support for PC_SYMMETRIC */ 171313044ca3SPierre 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]); 171413044ca3SPierre Jolivet } 171513044ca3SPierre Jolivet PetscCall(KSPSetPostSolve(inner_ksp, KSPPostSolve_SchurCorrection, ctx)); 171613044ca3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)(std::get<0>(*ctx)[1]), "_PCHPDDM_Schur", (PetscObject)container)); 171713044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)container)); 171813044ca3SPierre Jolivet PetscCall(PCSetUp(std::get<0>(*ctx)[1])); 171913044ca3SPierre Jolivet PetscCall(PCSetUp(pc)); 172013044ca3SPierre Jolivet PetscCall(KSPSetOperators(inner_ksp, S, S)); 172113044ca3SPierre Jolivet PetscCall(MatCreateVecs(std::get<1>(*ctx)[0], std::get<3>(*ctx), std::get<3>(*ctx) + 1)); 172213044ca3SPierre Jolivet PetscCall(VecDuplicate(std::get<3>(*ctx)[0], std::get<3>(*ctx) + 2)); 172313044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)inner_ksp)); 172413044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)S)); 172513044ca3SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 172613044ca3SPierre Jolivet } else { /* second call to PCSetUp() on the PC associated to the Schur complement, retrieve previously set context */ 172713044ca3SPierre Jolivet PetscCall(PetscContainerGetPointer(container, (void **)&ctx)); 172813044ca3SPierre Jolivet } 172913044ca3SPierre Jolivet } 173013044ca3SPierre Jolivet } 173113044ca3SPierre Jolivet } 1732f1580f4eSBarry Smith if (!data->is && data->N > 1) { 1733f1580f4eSBarry Smith char type[256] = {}; /* same size as in src/ksp/pc/interface/pcset.c */ 1734f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompareAny((PetscObject)P, &flg, MATNORMAL, MATNORMALHERMITIAN, "")); 1735f1580f4eSBarry Smith if (flg || (A->rmap->N != A->cmap->N && P->rmap->N == P->cmap->N && P->rmap->N == A->cmap->N)) { 1736f1580f4eSBarry Smith Mat B; 1737f1580f4eSBarry Smith PetscCall(PCHPDDMSetAuxiliaryMatNormal_Private(pc, A, P, &B, pcpre)); 1738f1580f4eSBarry Smith if (data->correction == PC_HPDDM_COARSE_CORRECTION_DEFLATED) data->correction = PC_HPDDM_COARSE_CORRECTION_BALANCED; 1739f1580f4eSBarry Smith PetscCall(MatDestroy(&B)); 1740f1580f4eSBarry Smith } else { 1741f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)P, MATSCHURCOMPLEMENT, &flg)); 1742f1580f4eSBarry Smith if (flg) { 1743f1580f4eSBarry Smith Mat A00, P00, A01, A10, A11, B, N; 174413044ca3SPierre Jolivet PCHPDDMSchurPreType type = PC_HPDDM_SCHUR_PRE_LEAST_SQUARES; 174513044ca3SPierre Jolivet 174613044ca3SPierre Jolivet PetscCall(MatSchurComplementGetSubMatrices(P, &A00, &P00, &A01, &A10, &A11)); 174713044ca3SPierre Jolivet if (PetscDefined(USE_DEBUG)) PetscCall(PCHPDDMCheckSymmetry_Private(pc, A01, A10)); 174813044ca3SPierre Jolivet PetscCall(PetscOptionsGetEnum(nullptr, pcpre, "-pc_hpddm_schur_precondition", PCHPDDMSchurPreTypes, (PetscEnum *)&type, &flg)); 174913044ca3SPierre Jolivet if (type == PC_HPDDM_SCHUR_PRE_LEAST_SQUARES) { 17503df4cd7bSPierre Jolivet Vec diagonal = nullptr; 1751f1580f4eSBarry Smith const PetscScalar *array; 1752f1580f4eSBarry Smith MatSchurComplementAinvType type; 1753f1580f4eSBarry Smith 1754f1580f4eSBarry Smith if (A11) { 17553df4cd7bSPierre Jolivet PetscCall(MatCreateVecs(A11, &diagonal, nullptr)); 17563df4cd7bSPierre Jolivet PetscCall(MatGetDiagonal(A11, diagonal)); 1757f1580f4eSBarry Smith } 1758db4a47b3SPierre Jolivet PetscCall(MatCreateVecs(P00, &v, nullptr)); 1759f1580f4eSBarry Smith PetscCall(MatSchurComplementGetAinvType(P, &type)); 1760f1580f4eSBarry Smith PetscCheck(type == MAT_SCHUR_COMPLEMENT_AINV_DIAG || type == MAT_SCHUR_COMPLEMENT_AINV_LUMP, PetscObjectComm((PetscObject)P), PETSC_ERR_SUP, "-%smat_schur_complement_ainv_type %s", ((PetscObject)P)->prefix ? ((PetscObject)P)->prefix : "", MatSchurComplementAinvTypes[type]); 1761f1580f4eSBarry Smith if (type == MAT_SCHUR_COMPLEMENT_AINV_LUMP) { 1762f1580f4eSBarry Smith PetscCall(MatGetRowSum(P00, v)); 1763f1580f4eSBarry Smith if (A00 == P00) PetscCall(PetscObjectReference((PetscObject)A00)); 1764f1580f4eSBarry Smith PetscCall(MatDestroy(&P00)); 1765f1580f4eSBarry Smith PetscCall(VecGetArrayRead(v, &array)); 1766db4a47b3SPierre Jolivet PetscCall(MatCreateAIJ(PetscObjectComm((PetscObject)A00), A00->rmap->n, A00->cmap->n, A00->rmap->N, A00->cmap->N, 1, nullptr, 0, nullptr, &P00)); 1767f1580f4eSBarry Smith PetscCall(MatSetOption(P00, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE)); 1768f1580f4eSBarry Smith for (n = A00->rmap->rstart; n < A00->rmap->rend; ++n) PetscCall(MatSetValue(P00, n, n, array[n - A00->rmap->rstart], INSERT_VALUES)); 1769f1580f4eSBarry Smith PetscCall(MatAssemblyBegin(P00, MAT_FINAL_ASSEMBLY)); 1770f1580f4eSBarry Smith PetscCall(MatAssemblyEnd(P00, MAT_FINAL_ASSEMBLY)); 1771f1580f4eSBarry Smith PetscCall(VecRestoreArrayRead(v, &array)); 1772f1580f4eSBarry Smith PetscCall(MatSchurComplementUpdateSubMatrices(P, A00, P00, A01, A10, A11)); /* replace P00 by diag(sum of each row of P00) */ 1773f1580f4eSBarry Smith PetscCall(MatDestroy(&P00)); 1774f1580f4eSBarry Smith } else PetscCall(MatGetDiagonal(P00, v)); 1775f1580f4eSBarry Smith PetscCall(VecReciprocal(v)); /* inv(diag(P00)) */ 1776f1580f4eSBarry Smith PetscCall(VecSqrtAbs(v)); /* sqrt(inv(diag(P00))) */ 1777f1580f4eSBarry Smith PetscCall(MatDuplicate(A01, MAT_COPY_VALUES, &B)); 1778db4a47b3SPierre Jolivet PetscCall(MatDiagonalScale(B, v, nullptr)); 1779f1580f4eSBarry Smith PetscCall(VecDestroy(&v)); 1780f1580f4eSBarry Smith PetscCall(MatCreateNormalHermitian(B, &N)); 17813df4cd7bSPierre Jolivet PetscCall(PCHPDDMSetAuxiliaryMatNormal_Private(pc, B, N, &P, pcpre, &diagonal)); 1782f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)data->aux, MATSEQAIJ, &flg)); 1783f1580f4eSBarry Smith if (!flg) { 1784f1580f4eSBarry Smith PetscCall(MatDestroy(&P)); 1785f1580f4eSBarry Smith P = N; 1786f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)P)); 1787f1580f4eSBarry Smith } else PetscCall(MatScale(P, -1.0)); 17883df4cd7bSPierre Jolivet if (diagonal) { 17893df4cd7bSPierre Jolivet PetscCall(MatDiagonalSet(P, diagonal, ADD_VALUES)); 1790aa21023fSPierre Jolivet PetscCall(PCSetOperators(pc, P, P)); /* replace P by diag(P11) - A01^T inv(diag(P00)) A01 */ 17913df4cd7bSPierre Jolivet PetscCall(VecDestroy(&diagonal)); 17923df4cd7bSPierre Jolivet } else { 1793f1580f4eSBarry Smith PetscCall(MatScale(N, -1.0)); 1794aa21023fSPierre Jolivet PetscCall(PCSetOperators(pc, N, P)); /* replace P by - A01^T inv(diag(P00)) A01 */ 17953df4cd7bSPierre Jolivet } 1796f1580f4eSBarry Smith PetscCall(MatDestroy(&N)); 1797f1580f4eSBarry Smith PetscCall(MatDestroy(&P)); 1798f1580f4eSBarry Smith PetscCall(MatDestroy(&B)); 179913044ca3SPierre Jolivet } else 180013044ca3SPierre Jolivet PetscCheck(type != PC_HPDDM_SCHUR_PRE_GENEO, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-%spc_hpddm_schur_precondition geneo without a prior call to PCHPDDMSetAuxiliaryMat() on the A11 block%s%s", pcpre ? pcpre : "", pcpre ? " -" : "", pcpre ? pcpre : ""); 18013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1802f1580f4eSBarry Smith } else { 1803db4a47b3SPierre Jolivet PetscCall(PetscOptionsGetString(nullptr, pcpre, "-pc_hpddm_levels_1_st_pc_type", type, sizeof(type), nullptr)); 1804f1580f4eSBarry Smith PetscCall(PetscStrcmp(type, PCMAT, &algebraic)); 1805b07dfdedSPierre Jolivet PetscCheck(!algebraic || !block, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-pc_hpddm_levels_1_st_pc_type mat and -pc_hpddm_block_splitting"); 1806*9bb5c669SPierre Jolivet if (overlap != -1) { 1807*9bb5c669SPierre Jolivet PetscCheck(!block && !algebraic, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-pc_hpddm_%s and -pc_hpddm_harmonic_overlap", block ? "block_splitting" : "levels_1_st_pc_type mat"); 1808*9bb5c669SPierre Jolivet PetscCheck(overlap >= 1, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_WRONG, "-pc_hpddm_harmonic_overlap %" PetscInt_FMT " < 1", overlap); 1809*9bb5c669SPierre Jolivet } 1810*9bb5c669SPierre Jolivet if (block || overlap != -1) algebraic = PETSC_TRUE; 1811f1580f4eSBarry Smith if (algebraic) { 1812f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, P->rmap->n, P->rmap->rstart, 1, &data->is)); 1813f1580f4eSBarry Smith PetscCall(MatIncreaseOverlap(P, 1, &data->is, 1)); 1814f1580f4eSBarry Smith PetscCall(ISSort(data->is)); 1815*9bb5c669SPierre Jolivet } else 1816*9bb5c669SPierre 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 : "")); 1817f1580f4eSBarry Smith } 1818f1580f4eSBarry Smith } 1819f1580f4eSBarry Smith } 1820f1580f4eSBarry Smith } 1821398c7888SPierre Jolivet #if PetscDefined(USE_DEBUG) 1822398c7888SPierre Jolivet if (data->is) PetscCall(ISDuplicate(data->is, &dis)); 1823398c7888SPierre Jolivet if (data->aux) PetscCall(MatDuplicate(data->aux, MAT_COPY_VALUES, &daux)); 1824398c7888SPierre Jolivet #endif 1825f1580f4eSBarry Smith if (data->is || (ismatis && data->N > 1)) { 1826f1580f4eSBarry Smith if (ismatis) { 1827f1580f4eSBarry Smith std::initializer_list<std::string> list = {MATSEQBAIJ, MATSEQSBAIJ}; 1828f1580f4eSBarry Smith PetscCall(MatISGetLocalMat(P, &N)); 1829f1580f4eSBarry Smith std::initializer_list<std::string>::const_iterator it = std::find(list.begin(), list.end(), ((PetscObject)N)->type_name); 1830f1580f4eSBarry Smith PetscCall(MatISRestoreLocalMat(P, &N)); 1831f1580f4eSBarry Smith switch (std::distance(list.begin(), it)) { 1832d71ae5a4SJacob Faibussowitsch case 0: 1833d71ae5a4SJacob Faibussowitsch PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &C)); 1834d71ae5a4SJacob Faibussowitsch break; 1835f1580f4eSBarry Smith case 1: 1836f1580f4eSBarry Smith /* MatCreateSubMatrices() does not work with MATSBAIJ and unsorted ISes, so convert to MPIBAIJ */ 1837f1580f4eSBarry Smith PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &C)); 1838f1580f4eSBarry Smith PetscCall(MatSetOption(C, MAT_SYMMETRIC, PETSC_TRUE)); 1839f1580f4eSBarry Smith break; 1840d71ae5a4SJacob Faibussowitsch default: 1841d71ae5a4SJacob Faibussowitsch PetscCall(MatConvert(P, MATMPIAIJ, MAT_INITIAL_MATRIX, &C)); 1842f1580f4eSBarry Smith } 1843db4a47b3SPierre Jolivet PetscCall(MatISGetLocalToGlobalMapping(P, &l2g, nullptr)); 1844f1580f4eSBarry Smith PetscCall(PetscObjectReference((PetscObject)P)); 1845f1580f4eSBarry Smith PetscCall(KSPSetOperators(data->levels[0]->ksp, A, C)); 1846f1580f4eSBarry Smith std::swap(C, P); 1847f1580f4eSBarry Smith PetscCall(ISLocalToGlobalMappingGetSize(l2g, &n)); 1848f1580f4eSBarry Smith PetscCall(ISCreateStride(PETSC_COMM_SELF, n, 0, 1, &loc)); 1849f1580f4eSBarry Smith PetscCall(ISLocalToGlobalMappingApplyIS(l2g, loc, &is[0])); 1850f1580f4eSBarry Smith PetscCall(ISDestroy(&loc)); 1851f1580f4eSBarry Smith /* the auxiliary Mat is _not_ the local Neumann matrix */ 1852f1580f4eSBarry Smith /* it is the local Neumann matrix augmented (with zeros) through MatIncreaseOverlap() */ 1853c8ea6600SPierre Jolivet data->Neumann = PETSC_BOOL3_FALSE; 1854f1580f4eSBarry Smith structure = SAME_NONZERO_PATTERN; 1855f1580f4eSBarry Smith } else { 1856f1580f4eSBarry Smith is[0] = data->is; 185713044ca3SPierre Jolivet if (algebraic || ctx) subdomains = PETSC_TRUE; 1858db4a47b3SPierre Jolivet PetscCall(PetscOptionsGetBool(nullptr, pcpre, "-pc_hpddm_define_subdomains", &subdomains, nullptr)); 185913044ca3SPierre 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); 1860c8ea6600SPierre Jolivet if (PetscBool3ToBool(data->Neumann)) { 1861b07dfdedSPierre Jolivet PetscCheck(!block, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-pc_hpddm_block_splitting and -pc_hpddm_has_neumann"); 1862*9bb5c669SPierre Jolivet PetscCheck(overlap == -1, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-pc_hpddm_harmonic_overlap %" PetscInt_FMT " and -pc_hpddm_has_neumann", overlap); 1863b07dfdedSPierre Jolivet PetscCheck(!algebraic, PetscObjectComm((PetscObject)P), PETSC_ERR_ARG_INCOMP, "-pc_hpddm_levels_1_st_pc_type mat and -pc_hpddm_has_neumann"); 1864f1580f4eSBarry Smith } 1865c8ea6600SPierre Jolivet if (PetscBool3ToBool(data->Neumann) || block) structure = SAME_NONZERO_PATTERN; 1866f1580f4eSBarry Smith PetscCall(ISCreateStride(PetscObjectComm((PetscObject)data->is), P->rmap->n, P->rmap->rstart, 1, &loc)); 1867f1580f4eSBarry Smith } 1868f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_", pcpre ? pcpre : "")); 1869db4a47b3SPierre Jolivet PetscCall(PetscOptionsGetEnum(nullptr, prefix, "-st_matstructure", MatStructures, (PetscEnum *)&structure, &flg)); /* if not user-provided, force its value when possible */ 1870f1580f4eSBarry Smith if (!flg && structure == SAME_NONZERO_PATTERN) { /* cannot call STSetMatStructure() yet, insert the appropriate option in the database, parsed by STSetFromOptions() */ 1871f1580f4eSBarry Smith PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "-%spc_hpddm_levels_1_st_matstructure", pcpre ? pcpre : "")); 1872db4a47b3SPierre Jolivet PetscCall(PetscOptionsSetValue(nullptr, prefix, MatStructures[structure])); 1873f1580f4eSBarry Smith } 1874398c7888SPierre Jolivet flg = PETSC_FALSE; 1875b07dfdedSPierre Jolivet if (data->share) { 1876398c7888SPierre Jolivet data->share = PETSC_FALSE; /* will be reset to PETSC_TRUE if none of the conditions below are true */ 1877398c7888SPierre 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 : "")); 1878398c7888SPierre Jolivet else if (data->deflation) PetscCall(PetscInfo(pc, "Nothing to share since PCHPDDMSetDeflationMat() has been called\n")); 1879398c7888SPierre Jolivet else if (ismatis) PetscCall(PetscInfo(pc, "Cannot share subdomain KSP between SLEPc and PETSc with a Pmat of type MATIS\n")); 1880b07dfdedSPierre Jolivet else if (!algebraic && structure != SAME_NONZERO_PATTERN) 1881398c7888SPierre 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])); 1882398c7888SPierre Jolivet else data->share = PETSC_TRUE; 1883398c7888SPierre Jolivet } 1884398c7888SPierre Jolivet if (!ismatis) { 1885398c7888SPierre Jolivet if (data->share || (!PetscBool3ToBool(data->Neumann) && subdomains)) PetscCall(ISDuplicate(is[0], &unsorted)); 1886398c7888SPierre Jolivet else unsorted = is[0]; 1887398c7888SPierre Jolivet } 1888f1580f4eSBarry Smith if (data->N > 1 && (data->aux || ismatis || algebraic)) { 1889f1580f4eSBarry Smith PetscCheck(loadedSym, PETSC_COMM_SELF, PETSC_ERR_PLIB, "HPDDM library not loaded, cannot use more than one level"); 1890f1580f4eSBarry Smith PetscCall(MatSetOption(P, MAT_SUBMAT_SINGLEIS, PETSC_TRUE)); 1891f1580f4eSBarry Smith if (ismatis) { 1892f1580f4eSBarry Smith /* needed by HPDDM (currently) so that the partition of unity is 0 on subdomain interfaces */ 1893f1580f4eSBarry Smith PetscCall(MatIncreaseOverlap(P, 1, is, 1)); 1894f1580f4eSBarry Smith PetscCall(ISDestroy(&data->is)); 1895f1580f4eSBarry Smith data->is = is[0]; 1896f1580f4eSBarry Smith } else { 1897f1580f4eSBarry Smith if (PetscDefined(USE_DEBUG)) { 1898f1580f4eSBarry Smith PetscBool equal; 1899f1580f4eSBarry Smith IS intersect; 1900f1580f4eSBarry Smith 1901f1580f4eSBarry Smith PetscCall(ISIntersect(data->is, loc, &intersect)); 1902f1580f4eSBarry Smith PetscCall(ISEqualUnsorted(loc, intersect, &equal)); 1903f1580f4eSBarry Smith PetscCall(ISDestroy(&intersect)); 1904f1580f4eSBarry Smith PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "IS of the auxiliary Mat does not include all local rows of A"); 1905f1580f4eSBarry Smith } 1906*9bb5c669SPierre Jolivet if (overlap == -1) PetscCall(PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", PCHPDDMAlgebraicAuxiliaryMat_Private)); 1907*9bb5c669SPierre Jolivet if (!PetscBool3ToBool(data->Neumann) && (!algebraic || overlap != -1)) { 1908f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)P, MATMPISBAIJ, &flg)); 1909f1580f4eSBarry Smith if (flg) { 1910f1580f4eSBarry Smith /* maybe better to ISSort(is[0]), MatCreateSubMatrices(), and then MatPermute() */ 1911f1580f4eSBarry Smith /* but there is no MatPermute_SeqSBAIJ(), so as before, just use MATMPIBAIJ */ 1912f1580f4eSBarry Smith PetscCall(MatConvert(P, MATMPIBAIJ, MAT_INITIAL_MATRIX, &uaux)); 1913f1580f4eSBarry Smith flg = PETSC_FALSE; 1914f1580f4eSBarry Smith } 1915f1580f4eSBarry Smith } 1916f1580f4eSBarry Smith } 1917*9bb5c669SPierre Jolivet if (algebraic && overlap == -1) { 1918f1580f4eSBarry Smith PetscUseMethod(pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", (Mat, IS *, Mat *[], PetscBool), (P, is, &sub, block)); 1919f1580f4eSBarry Smith if (block) { 1920f1580f4eSBarry Smith PetscCall(PetscObjectQuery((PetscObject)sub[0], "_PCHPDDM_Neumann_Mat", (PetscObject *)&data->aux)); 1921db4a47b3SPierre Jolivet PetscCall(PetscObjectCompose((PetscObject)sub[0], "_PCHPDDM_Neumann_Mat", nullptr)); 1922f1580f4eSBarry Smith } 1923*9bb5c669SPierre Jolivet } else if (!uaux || overlap != -1) { 192413044ca3SPierre Jolivet if (!ctx) { 1925c8ea6600SPierre Jolivet if (PetscBool3ToBool(data->Neumann)) sub = &data->aux; 1926*9bb5c669SPierre Jolivet else { 1927*9bb5c669SPierre Jolivet if (overlap != -1) { 1928*9bb5c669SPierre Jolivet Harmonic h; 1929*9bb5c669SPierre Jolivet Mat A0, *a; /* with an SVD: [ A_00 A_01 ] */ 1930*9bb5c669SPierre Jolivet IS ov[2], rows, cols, stride; /* [ A_10 A_11 A_12 ] */ 1931*9bb5c669SPierre Jolivet const PetscInt *i[2], bs = PetscAbs(P->cmap->bs); /* with a GEVP: [ A_00 A_01 ] */ 1932*9bb5c669SPierre Jolivet PetscInt n[2]; /* [ A_10 A_11 A_12 ] */ 1933*9bb5c669SPierre Jolivet std::vector<PetscInt> v[2]; /* [ A_21 A_22 ] */ 1934*9bb5c669SPierre Jolivet PetscBool flg; 1935*9bb5c669SPierre Jolivet 1936*9bb5c669SPierre Jolivet PetscCall(ISDuplicate(data->is, ov)); 1937*9bb5c669SPierre Jolivet if (overlap > 1) PetscCall(MatIncreaseOverlap(P, 1, ov, overlap - 1)); 1938*9bb5c669SPierre Jolivet PetscCall(ISDuplicate(ov[0], ov + 1)); 1939*9bb5c669SPierre Jolivet PetscCall(MatIncreaseOverlap(P, 1, ov + 1, 1)); 1940*9bb5c669SPierre Jolivet PetscCall(PetscNew(&h)); 1941*9bb5c669SPierre Jolivet h->ksp = nullptr; 1942*9bb5c669SPierre Jolivet PetscCall(PetscCalloc1(2, &h->A)); 1943*9bb5c669SPierre Jolivet PetscCall(PetscOptionsHasName(nullptr, pcpre, "-svd_nsv", &flg)); 1944*9bb5c669SPierre Jolivet if (!flg) PetscCall(PetscOptionsHasName(nullptr, prefix, "-svd_relative_threshold", &flg)); 1945*9bb5c669SPierre Jolivet PetscCall(ISSort(ov[0])); 1946*9bb5c669SPierre Jolivet if (!flg) PetscCall(ISSort(ov[1])); 1947*9bb5c669SPierre Jolivet PetscCall(PetscMalloc1(!flg ? 5 : 3, &h->is)); 1948*9bb5c669SPierre Jolivet PetscCall(MatCreateSubMatrices(uaux ? uaux : P, 1, ov + !flg, ov + 1, MAT_INITIAL_MATRIX, &a)); /* submatrix from above, either square (!flg) or rectangular (flg) */ 1949*9bb5c669SPierre Jolivet for (PetscInt j = 0; j < 2; ++j) { 1950*9bb5c669SPierre Jolivet PetscCall(ISGetIndices(ov[j], i + j)); 1951*9bb5c669SPierre Jolivet PetscCall(ISGetLocalSize(ov[j], n + j)); 1952*9bb5c669SPierre Jolivet } 1953*9bb5c669SPierre Jolivet v[1].reserve((n[1] - n[0]) / bs); 1954*9bb5c669SPierre Jolivet for (PetscInt j = 0; j < n[1]; j += bs) { /* indices of the (2,2) block */ 1955*9bb5c669SPierre Jolivet PetscInt location; 1956*9bb5c669SPierre Jolivet PetscCall(ISLocate(ov[0], i[1][j], &location)); 1957*9bb5c669SPierre Jolivet if (location < 0) v[1].emplace_back(j / bs); 1958*9bb5c669SPierre Jolivet } 1959*9bb5c669SPierre Jolivet if (!flg) { 1960*9bb5c669SPierre Jolivet h->A[1] = a[0]; 1961*9bb5c669SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)h->A[1])); 1962*9bb5c669SPierre Jolivet v[0].reserve((n[0] - P->rmap->n) / bs); 1963*9bb5c669SPierre Jolivet for (PetscInt j = 0; j < n[1]; j += bs) { /* row indices of the (1,2) block */ 1964*9bb5c669SPierre Jolivet PetscInt location; 1965*9bb5c669SPierre Jolivet PetscCall(ISLocate(loc, i[1][j], &location)); 1966*9bb5c669SPierre Jolivet if (location < 0) { 1967*9bb5c669SPierre Jolivet PetscCall(ISLocate(ov[0], i[1][j], &location)); 1968*9bb5c669SPierre Jolivet if (location >= 0) v[0].emplace_back(j / bs); 1969*9bb5c669SPierre Jolivet } 1970*9bb5c669SPierre Jolivet } 1971*9bb5c669SPierre Jolivet PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, v[0].size(), v[0].data(), PETSC_USE_POINTER, &rows)); 1972*9bb5c669SPierre Jolivet PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, v[1].size(), v[1].data(), PETSC_COPY_VALUES, h->is + 4)); 1973*9bb5c669SPierre Jolivet PetscCall(MatCreateSubMatrix(a[0], rows, h->is[4], MAT_INITIAL_MATRIX, h->A)); /* A_12 submatrix from above */ 1974*9bb5c669SPierre Jolivet PetscCall(ISDestroy(&rows)); 1975*9bb5c669SPierre 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 */ 1976*9bb5c669SPierre Jolivet PetscCall(ISEmbed(ov[0], ov[1], PETSC_TRUE, &rows)); 1977*9bb5c669SPierre Jolivet PetscCall(MatCreateSubMatrix(a[0], rows, cols = rows, MAT_INITIAL_MATRIX, &A0)); /* [ A_00 A_01 ; A_10 A_11 ] submatrix from above */ 1978*9bb5c669SPierre Jolivet PetscCall(ISDestroy(&rows)); 1979*9bb5c669SPierre Jolivet v[0].clear(); 1980*9bb5c669SPierre Jolivet PetscCall(ISEmbed(loc, ov[1], PETSC_TRUE, h->is + 3)); 1981*9bb5c669SPierre Jolivet PetscCall(ISEmbed(data->is, ov[1], PETSC_TRUE, h->is + 2)); 1982*9bb5c669SPierre Jolivet } 1983*9bb5c669SPierre Jolivet v[0].reserve((n[0] - P->rmap->n) / bs); 1984*9bb5c669SPierre Jolivet for (PetscInt j = 0; j < n[0]; j += bs) { 1985*9bb5c669SPierre Jolivet PetscInt location; 1986*9bb5c669SPierre Jolivet PetscCall(ISLocate(loc, i[0][j], &location)); 1987*9bb5c669SPierre Jolivet if (location < 0) v[0].emplace_back(j / bs); 1988*9bb5c669SPierre Jolivet } 1989*9bb5c669SPierre Jolivet PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, v[0].size(), v[0].data(), PETSC_USE_POINTER, &rows)); 1990*9bb5c669SPierre Jolivet for (PetscInt j = 0; j < 2; ++j) PetscCall(ISRestoreIndices(ov[j], i + j)); 1991*9bb5c669SPierre Jolivet if (flg) { 1992*9bb5c669SPierre Jolivet IS is; 1993*9bb5c669SPierre Jolivet PetscCall(ISCreateStride(PETSC_COMM_SELF, a[0]->rmap->n, 0, 1, &is)); 1994*9bb5c669SPierre Jolivet PetscCall(ISEmbed(ov[0], ov[1], PETSC_TRUE, &cols)); 1995*9bb5c669SPierre Jolivet PetscCall(MatCreateSubMatrix(a[0], is, cols, MAT_INITIAL_MATRIX, &A0)); /* [ A_00 A_01 ; A_10 A_11 ] submatrix from above */ 1996*9bb5c669SPierre Jolivet PetscCall(ISDestroy(&cols)); 1997*9bb5c669SPierre Jolivet PetscCall(ISDestroy(&is)); 1998*9bb5c669SPierre Jolivet if (uaux) PetscCall(MatConvert(A0, MATSEQSBAIJ, MAT_INPLACE_MATRIX, &A0)); /* initial Pmat was MATSBAIJ, convert back to the same format since this submatrix is square */ 1999*9bb5c669SPierre Jolivet PetscCall(ISEmbed(loc, data->is, PETSC_TRUE, h->is + 2)); 2000*9bb5c669SPierre Jolivet PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, v[1].size(), v[1].data(), PETSC_USE_POINTER, &cols)); 2001*9bb5c669SPierre Jolivet PetscCall(MatCreateSubMatrix(a[0], rows, cols, MAT_INITIAL_MATRIX, h->A)); /* A_12 submatrix from above */ 2002*9bb5c669SPierre Jolivet PetscCall(ISDestroy(&cols)); 2003*9bb5c669SPierre Jolivet } 2004*9bb5c669SPierre Jolivet PetscCall(ISCreateStride(PETSC_COMM_SELF, A0->rmap->n, 0, 1, &stride)); 2005*9bb5c669SPierre Jolivet PetscCall(ISEmbed(rows, stride, PETSC_TRUE, h->is)); 2006*9bb5c669SPierre Jolivet PetscCall(ISDestroy(&stride)); 2007*9bb5c669SPierre Jolivet PetscCall(ISDestroy(&rows)); 2008*9bb5c669SPierre Jolivet PetscCall(ISEmbed(loc, ov[0], PETSC_TRUE, h->is + 1)); 2009*9bb5c669SPierre Jolivet if (subdomains) { 2010*9bb5c669SPierre Jolivet if (!data->levels[0]->pc) { 2011*9bb5c669SPierre Jolivet PetscCall(PCCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->pc)); 2012*9bb5c669SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_", pcpre ? pcpre : "")); 2013*9bb5c669SPierre Jolivet PetscCall(PCSetOptionsPrefix(data->levels[0]->pc, prefix)); 2014*9bb5c669SPierre Jolivet PetscCall(PCSetOperators(data->levels[0]->pc, A, P)); 2015*9bb5c669SPierre Jolivet } 2016*9bb5c669SPierre Jolivet PetscCall(PCSetType(data->levels[0]->pc, PCASM)); 2017*9bb5c669SPierre Jolivet if (!data->levels[0]->pc->setupcalled) PetscCall(PCASMSetLocalSubdomains(data->levels[0]->pc, 1, ov + !flg, &loc)); 2018*9bb5c669SPierre Jolivet PetscCall(PCHPDDMCommunicationAvoidingPCASM_Private(data->levels[0]->pc, flg ? A0 : a[0], PETSC_TRUE)); 2019*9bb5c669SPierre Jolivet if (!flg) ++overlap; 2020*9bb5c669SPierre Jolivet if (data->share) { 2021*9bb5c669SPierre Jolivet PetscInt n = -1; 2022*9bb5c669SPierre Jolivet PetscTryMethod(data->levels[0]->pc, "PCASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (data->levels[0]->pc, &n, nullptr, &ksp)); 2023*9bb5c669SPierre Jolivet PetscCheck(n == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of subdomain solver %" PetscInt_FMT " != 1", n); 2024*9bb5c669SPierre Jolivet if (flg) { 2025*9bb5c669SPierre Jolivet h->ksp = ksp[0]; 2026*9bb5c669SPierre Jolivet PetscCall(PetscObjectReference((PetscObject)h->ksp)); 2027*9bb5c669SPierre Jolivet } 2028*9bb5c669SPierre Jolivet } 2029*9bb5c669SPierre Jolivet } 2030*9bb5c669SPierre Jolivet if (!h->ksp) { 2031*9bb5c669SPierre Jolivet PetscBool share = data->share; 2032*9bb5c669SPierre Jolivet PetscCall(KSPCreate(PETSC_COMM_SELF, &h->ksp)); 2033*9bb5c669SPierre Jolivet PetscCall(KSPSetType(h->ksp, KSPPREONLY)); 2034*9bb5c669SPierre Jolivet PetscCall(KSPSetOperators(h->ksp, A0, A0)); 2035*9bb5c669SPierre Jolivet do { 2036*9bb5c669SPierre Jolivet if (!data->share) { 2037*9bb5c669SPierre Jolivet share = PETSC_FALSE; 2038*9bb5c669SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_%s", pcpre ? pcpre : "", flg ? "svd_" : "eps_")); 2039*9bb5c669SPierre Jolivet PetscCall(KSPSetOptionsPrefix(h->ksp, prefix)); 2040*9bb5c669SPierre Jolivet PetscCall(KSPSetFromOptions(h->ksp)); 2041*9bb5c669SPierre Jolivet } else { 2042*9bb5c669SPierre Jolivet MatSolverType type; 2043*9bb5c669SPierre Jolivet PetscCall(KSPGetPC(ksp[0], &pc)); 2044*9bb5c669SPierre Jolivet PetscCall(PetscObjectTypeCompareAny((PetscObject)pc, &data->share, PCLU, PCCHOLESKY, "")); 2045*9bb5c669SPierre Jolivet if (data->share) { 2046*9bb5c669SPierre Jolivet PetscCall(PCFactorGetMatSolverType(pc, &type)); 2047*9bb5c669SPierre Jolivet if (!type) { 2048*9bb5c669SPierre Jolivet if (PetscDefined(HAVE_MUMPS)) PetscCall(PCFactorSetMatSolverType(pc, MATSOLVERMUMPS)); 2049*9bb5c669SPierre Jolivet else if (PetscDefined(HAVE_MKL_PARDISO)) PetscCall(PCFactorSetMatSolverType(pc, MATSOLVERMKL_PARDISO)); 2050*9bb5c669SPierre Jolivet else data->share = PETSC_FALSE; 2051*9bb5c669SPierre Jolivet if (data->share) PetscCall(PCSetFromOptions(pc)); 2052*9bb5c669SPierre Jolivet } else { 2053*9bb5c669SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &data->share)); 2054*9bb5c669SPierre Jolivet if (!data->share) PetscCall(PetscStrcmp(type, MATSOLVERMKL_PARDISO, &data->share)); 2055*9bb5c669SPierre Jolivet } 2056*9bb5c669SPierre Jolivet if (data->share) { 2057*9bb5c669SPierre Jolivet std::tuple<KSP, IS, Vec[2]> *p; 2058*9bb5c669SPierre Jolivet PetscCall(PCFactorGetMatrix(pc, &A)); 2059*9bb5c669SPierre Jolivet PetscCall(MatFactorSetSchurIS(A, h->is[4])); 2060*9bb5c669SPierre Jolivet PetscCall(KSPSetUp(ksp[0])); 2061*9bb5c669SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_eps_shell_", pcpre ? pcpre : "")); 2062*9bb5c669SPierre Jolivet PetscCall(KSPSetOptionsPrefix(h->ksp, prefix)); 2063*9bb5c669SPierre Jolivet PetscCall(KSPSetFromOptions(h->ksp)); 2064*9bb5c669SPierre Jolivet PetscCall(KSPGetPC(h->ksp, &pc)); 2065*9bb5c669SPierre Jolivet PetscCall(PCSetType(pc, PCSHELL)); 2066*9bb5c669SPierre Jolivet PetscCall(PetscNew(&p)); 2067*9bb5c669SPierre Jolivet std::get<0>(*p) = ksp[0]; 2068*9bb5c669SPierre Jolivet PetscCall(ISEmbed(ov[0], ov[1], PETSC_TRUE, &std::get<1>(*p))); 2069*9bb5c669SPierre Jolivet PetscCall(MatCreateVecs(A, std::get<2>(*p), std::get<2>(*p) + 1)); 2070*9bb5c669SPierre Jolivet PetscCall(PCShellSetContext(pc, p)); 2071*9bb5c669SPierre Jolivet PetscCall(PCShellSetApply(pc, PCApply_Schur)); 2072*9bb5c669SPierre Jolivet PetscCall(PCShellSetApplyTranspose(pc, PCApply_Schur<Vec, true>)); 2073*9bb5c669SPierre Jolivet PetscCall(PCShellSetMatApply(pc, PCApply_Schur<Mat>)); 2074*9bb5c669SPierre Jolivet PetscCall(PCShellSetDestroy(pc, PCDestroy_Schur)); 2075*9bb5c669SPierre Jolivet } 2076*9bb5c669SPierre Jolivet } 2077*9bb5c669SPierre Jolivet if (!data->share) PetscCall(PetscInfo(pc, "Cannot share subdomain KSP between SLEPc and PETSc since neither MUMPS nor MKL PARDISO is used\n")); 2078*9bb5c669SPierre Jolivet } 2079*9bb5c669SPierre 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 */ 2080*9bb5c669SPierre Jolivet } 2081*9bb5c669SPierre Jolivet PetscCall(ISDestroy(ov)); 2082*9bb5c669SPierre Jolivet PetscCall(ISDestroy(ov + 1)); 2083*9bb5c669SPierre Jolivet if (overlap == 1 && subdomains && flg) { 2084*9bb5c669SPierre Jolivet *subA = A0; 2085*9bb5c669SPierre Jolivet sub = subA; 2086*9bb5c669SPierre Jolivet if (uaux) PetscCall(MatDestroy(&uaux)); 2087*9bb5c669SPierre Jolivet } else PetscCall(MatDestroy(&A0)); 2088*9bb5c669SPierre Jolivet PetscCall(MatCreateShell(PETSC_COMM_SELF, P->rmap->n, n[1] - n[0], P->rmap->n, n[1] - n[0], h, &data->aux)); 2089*9bb5c669SPierre Jolivet PetscCall(MatCreateVecs(h->ksp->pc->pmat, &h->v, nullptr)); 2090*9bb5c669SPierre Jolivet PetscCall(MatShellSetOperation(data->aux, MATOP_MULT, (void (*)(void))MatMult_Harmonic)); 2091*9bb5c669SPierre Jolivet PetscCall(MatShellSetOperation(data->aux, MATOP_MULT_TRANSPOSE, (void (*)(void))MatMultTranspose_Harmonic)); 2092*9bb5c669SPierre Jolivet PetscCall(MatShellSetMatProductOperation(data->aux, MATPRODUCT_AB, nullptr, MatProduct_AB_Harmonic, nullptr, MATDENSE, MATDENSE)); 2093*9bb5c669SPierre Jolivet PetscCall(MatShellSetOperation(data->aux, MATOP_DESTROY, (void (*)(void))MatDestroy_Harmonic)); 2094*9bb5c669SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &a)); 2095*9bb5c669SPierre Jolivet } 2096*9bb5c669SPierre Jolivet if (overlap != 1 || !subdomains) PetscCall(MatCreateSubMatrices(uaux ? uaux : P, 1, is, is, MAT_INITIAL_MATRIX, &sub)); 2097*9bb5c669SPierre Jolivet if (uaux) { 2098*9bb5c669SPierre Jolivet PetscCall(MatDestroy(&uaux)); 2099*9bb5c669SPierre Jolivet PetscCall(MatConvert(sub[0], MATSEQSBAIJ, MAT_INPLACE_MATRIX, sub)); 2100*9bb5c669SPierre Jolivet } 2101*9bb5c669SPierre Jolivet } 210213044ca3SPierre Jolivet } 2103f1580f4eSBarry Smith } else { 2104f1580f4eSBarry Smith PetscCall(MatCreateSubMatrices(uaux, 1, is, is, MAT_INITIAL_MATRIX, &sub)); 2105f1580f4eSBarry Smith PetscCall(MatDestroy(&uaux)); 2106f1580f4eSBarry Smith PetscCall(MatConvert(sub[0], MATSEQSBAIJ, MAT_INPLACE_MATRIX, sub)); 2107f1580f4eSBarry Smith } 2108f1580f4eSBarry Smith /* Vec holding the partition of unity */ 2109f1580f4eSBarry Smith if (!data->levels[0]->D) { 2110f1580f4eSBarry Smith PetscCall(ISGetLocalSize(data->is, &n)); 2111f1580f4eSBarry Smith PetscCall(VecCreateMPI(PETSC_COMM_SELF, n, PETSC_DETERMINE, &data->levels[0]->D)); 2112f1580f4eSBarry Smith } 2113*9bb5c669SPierre Jolivet if (data->share && overlap == -1) { 2114f1580f4eSBarry Smith Mat D; 2115db4a47b3SPierre Jolivet IS perm = nullptr; 2116f1580f4eSBarry Smith PetscInt size = -1; 2117398c7888SPierre Jolivet if (!data->levels[0]->pc) { 2118398c7888SPierre Jolivet PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_hpddm_levels_1_", pcpre ? pcpre : "")); 2119398c7888SPierre Jolivet PetscCall(PCCreate(PetscObjectComm((PetscObject)pc), &data->levels[0]->pc)); 2120398c7888SPierre Jolivet PetscCall(PCSetOptionsPrefix(data->levels[0]->pc, prefix)); 2121398c7888SPierre Jolivet PetscCall(PCSetOperators(data->levels[0]->pc, A, P)); 2122398c7888SPierre Jolivet } 2123398c7888SPierre Jolivet PetscCall(PCSetType(data->levels[0]->pc, PCASM)); 212413044ca3SPierre Jolivet if (!ctx) { 2125398c7888SPierre Jolivet if (!data->levels[0]->pc->setupcalled) { 2126398c7888SPierre Jolivet IS sorted; /* PCASM will sort the input IS, duplicate it to return an unmodified (PCHPDDM) input IS */ 2127398c7888SPierre Jolivet PetscCall(ISDuplicate(is[0], &sorted)); 2128398c7888SPierre Jolivet PetscCall(PCASMSetLocalSubdomains(data->levels[0]->pc, 1, &sorted, &loc)); 2129398c7888SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)sorted)); 2130398c7888SPierre Jolivet } 2131398c7888SPierre Jolivet PetscCall(PCSetFromOptions(data->levels[0]->pc)); 2132398c7888SPierre Jolivet if (block) { 2133398c7888SPierre Jolivet PetscCall(PCHPDDMPermute_Private(unsorted, data->is, &uis, sub[0], &C, &perm)); 2134398c7888SPierre Jolivet PetscCall(PCHPDDMCommunicationAvoidingPCASM_Private(data->levels[0]->pc, C, algebraic)); 2135398c7888SPierre Jolivet } else PetscCall(PCSetUp(data->levels[0]->pc)); 2136db4a47b3SPierre Jolivet PetscTryMethod(data->levels[0]->pc, "PCASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (data->levels[0]->pc, &size, nullptr, &ksp)); 2137398c7888SPierre Jolivet if (size != 1) { 2138398c7888SPierre Jolivet data->share = PETSC_FALSE; 2139398c7888SPierre Jolivet PetscCheck(size == -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of subdomain solver %" PetscInt_FMT " != 1", size); 2140398c7888SPierre Jolivet PetscCall(PetscInfo(pc, "Cannot share subdomain KSP between SLEPc and PETSc since PCASMGetSubKSP() not found in fine-level PC\n")); 2141398c7888SPierre Jolivet PetscCall(ISDestroy(&unsorted)); 2142398c7888SPierre Jolivet unsorted = is[0]; 2143398c7888SPierre Jolivet } else { 214413044ca3SPierre Jolivet if (!block && !ctx) PetscCall(PCHPDDMPermute_Private(unsorted, data->is, &uis, PetscBool3ToBool(data->Neumann) ? sub[0] : data->aux, &C, &perm)); 2145c8ea6600SPierre Jolivet if (!PetscBool3ToBool(data->Neumann) && !block) { 2146f1580f4eSBarry Smith PetscCall(MatPermute(sub[0], perm, perm, &D)); /* permute since PCASM will call ISSort() */ 2147f1580f4eSBarry Smith PetscCall(MatHeaderReplace(sub[0], &D)); 2148f1580f4eSBarry Smith } 2149f1580f4eSBarry Smith if (data->B) { /* see PCHPDDMSetRHSMat() */ 2150f1580f4eSBarry Smith PetscCall(MatPermute(data->B, perm, perm, &D)); 2151f1580f4eSBarry Smith PetscCall(MatHeaderReplace(data->B, &D)); 2152f1580f4eSBarry Smith } 2153f1580f4eSBarry Smith PetscCall(ISDestroy(&perm)); 2154f1580f4eSBarry Smith const char *matpre; 2155f1580f4eSBarry Smith PetscBool cmp[4]; 2156f1580f4eSBarry Smith PetscCall(KSPGetOperators(ksp[0], subA, subA + 1)); 2157f1580f4eSBarry Smith PetscCall(MatDuplicate(subA[1], MAT_SHARE_NONZERO_PATTERN, &D)); 2158f1580f4eSBarry Smith PetscCall(MatGetOptionsPrefix(subA[1], &matpre)); 2159f1580f4eSBarry Smith PetscCall(MatSetOptionsPrefix(D, matpre)); 2160f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)D, MATNORMAL, cmp)); 2161f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)C, MATNORMAL, cmp + 1)); 2162f1580f4eSBarry Smith if (!cmp[0]) PetscCall(PetscObjectTypeCompare((PetscObject)D, MATNORMALHERMITIAN, cmp + 2)); 2163f1580f4eSBarry Smith else cmp[2] = PETSC_FALSE; 2164f1580f4eSBarry Smith if (!cmp[1]) PetscCall(PetscObjectTypeCompare((PetscObject)C, MATNORMALHERMITIAN, cmp + 3)); 2165f1580f4eSBarry Smith else cmp[3] = PETSC_FALSE; 2166f1580f4eSBarry 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); 2167f1580f4eSBarry Smith if (!cmp[0] && !cmp[2]) { 2168f1580f4eSBarry Smith if (!block) PetscCall(MatAXPY(D, 1.0, C, SUBSET_NONZERO_PATTERN)); 21695c7345deSPierre Jolivet else { 2170db4a47b3SPierre Jolivet PetscCall(MatMissingDiagonal(D, cmp, nullptr)); 21715c7345deSPierre Jolivet if (cmp[0]) structure = DIFFERENT_NONZERO_PATTERN; /* data->aux has no missing diagonal entry */ 21725c7345deSPierre Jolivet PetscCall(MatAXPY(D, 1.0, data->aux, structure)); 21735c7345deSPierre Jolivet } 2174f1580f4eSBarry Smith } else { 2175f1580f4eSBarry Smith Mat mat[2]; 2176f1580f4eSBarry Smith if (cmp[0]) { 2177f1580f4eSBarry Smith PetscCall(MatNormalGetMat(D, mat)); 2178f1580f4eSBarry Smith PetscCall(MatNormalGetMat(C, mat + 1)); 2179f1580f4eSBarry Smith } else { 2180f1580f4eSBarry Smith PetscCall(MatNormalHermitianGetMat(D, mat)); 2181f1580f4eSBarry Smith PetscCall(MatNormalHermitianGetMat(C, mat + 1)); 2182f1580f4eSBarry Smith } 2183f1580f4eSBarry Smith PetscCall(MatAXPY(mat[0], 1.0, mat[1], SUBSET_NONZERO_PATTERN)); 2184f1580f4eSBarry Smith } 2185f1580f4eSBarry Smith PetscCall(MatPropagateSymmetryOptions(C, D)); 2186f1580f4eSBarry Smith PetscCall(MatDestroy(&C)); 2187f1580f4eSBarry Smith C = D; 2188f1580f4eSBarry Smith /* swap pointers so that variables stay consistent throughout PCSetUp() */ 2189f1580f4eSBarry Smith std::swap(C, data->aux); 2190f1580f4eSBarry Smith std::swap(uis, data->is); 2191f1580f4eSBarry Smith swap = PETSC_TRUE; 2192f1580f4eSBarry Smith } 2193f1580f4eSBarry Smith } 219413044ca3SPierre Jolivet } 219513044ca3SPierre Jolivet if (ctx) { 219613044ca3SPierre Jolivet PC_HPDDM *data_00 = (PC_HPDDM *)std::get<0>(*ctx)[0]->data; 219713044ca3SPierre Jolivet PC s; 219813044ca3SPierre Jolivet Mat A00, P00, A01 = nullptr, A10, A11, *B, T, N, b[4]; 219913044ca3SPierre Jolivet IS sorted, is[2]; 220013044ca3SPierre Jolivet MatSolverType type; 220113044ca3SPierre Jolivet std::pair<PC, Vec[2]> *p; 220213044ca3SPierre Jolivet 220313044ca3SPierre Jolivet PetscCall(PCHPDDMPermute_Private(unsorted, data->is, &uis, data->aux, &C, nullptr)); /* permute since PCASM works with a sorted IS */ 220413044ca3SPierre Jolivet std::swap(C, data->aux); 220513044ca3SPierre Jolivet std::swap(uis, data->is); 220613044ca3SPierre Jolivet swap = PETSC_TRUE; 220713044ca3SPierre Jolivet PetscCall(PCASMSetType(data->levels[0]->pc, PC_ASM_NONE)); /* "Neumann--Neumann" preconditioning with overlap and a Boolean partition of unity */ 220813044ca3SPierre Jolivet PetscCall(PCASMSetLocalSubdomains(data->levels[0]->pc, 1, &data->is, &loc)); 220913044ca3SPierre Jolivet PetscCall(PCSetFromOptions(data->levels[0]->pc)); /* action of eq. (15) of https://hal.science/hal-02343808v6/document (with a sign flip) */ 221013044ca3SPierre Jolivet PetscCall(MatSchurComplementGetSubMatrices(P, &A00, &P00, std::get<1>(*ctx), &A10, &A11)); 221113044ca3SPierre Jolivet std::get<1>(*ctx)[1] = A10; 221213044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATTRANSPOSEVIRTUAL, &flg)); 221313044ca3SPierre Jolivet if (flg) PetscCall(MatTransposeGetMat(A10, &A01)); 221413044ca3SPierre Jolivet else { 221513044ca3SPierre Jolivet PetscBool flg; 221613044ca3SPierre Jolivet 221713044ca3SPierre Jolivet PetscCall(PetscObjectTypeCompare((PetscObject)A10, MATHERMITIANTRANSPOSEVIRTUAL, &flg)); 221813044ca3SPierre Jolivet if (flg) PetscCall(MatHermitianTransposeGetMat(A10, &A01)); 221913044ca3SPierre Jolivet } 222013044ca3SPierre Jolivet PetscCall(ISDuplicate(data_00->is, &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 */ 222113044ca3SPierre Jolivet PetscCall(ISSort(sorted)); /* this is to avoid changing users inputs, but it requires a new call to ISSort() here */ 222213044ca3SPierre Jolivet if (!A01) PetscCall(MatCreateSubMatrices(A10, 1, &data->is, &sorted, MAT_INITIAL_MATRIX, &B)); 222313044ca3SPierre Jolivet else { 222413044ca3SPierre Jolivet PetscCall(MatCreateSubMatrices(A01, 1, &sorted, &data->is, MAT_INITIAL_MATRIX, &B)); 222513044ca3SPierre Jolivet PetscCall(PetscMalloc1(1, &sub)); 222613044ca3SPierre Jolivet if (flg) PetscCall(MatTranspose(*B, MAT_INITIAL_MATRIX, sub)); 222713044ca3SPierre Jolivet else PetscCall(MatHermitianTranspose(*B, MAT_INITIAL_MATRIX, sub)); 222813044ca3SPierre Jolivet PetscCall(MatDestroySubMatrices(1, &B)); 222913044ca3SPierre Jolivet B = sub; 223013044ca3SPierre Jolivet } 223113044ca3SPierre Jolivet PetscCall(ISDestroy(&sorted)); 223213044ca3SPierre Jolivet n = -1; 223313044ca3SPierre Jolivet PetscTryMethod(data_00->levels[0]->pc, "PCASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (data_00->levels[0]->pc, &n, nullptr, &ksp)); 223413044ca3SPierre Jolivet PetscCheck(n == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of subdomain solver %" PetscInt_FMT " != 1", n); 223513044ca3SPierre Jolivet PetscCall(KSPGetOperators(ksp[0], subA, subA + 1)); 223613044ca3SPierre Jolivet PetscCall(ISGetLocalSize(data_00->is, &n)); 223713044ca3SPierre 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); 223813044ca3SPierre Jolivet if (flg) PetscCall(MatTranspose(*B, MAT_INITIAL_MATRIX, &T)); 223913044ca3SPierre Jolivet else PetscCall(MatHermitianTranspose(*B, MAT_INITIAL_MATRIX, &T)); 224013044ca3SPierre Jolivet PetscCall(MatCreateSchurComplement(subA[0], subA[1], T, *B, data->aux, &S)); 224113044ca3SPierre Jolivet PetscCall(MatSchurComplementSetKSP(S, ksp[0])); 224213044ca3SPierre Jolivet PetscCall(PCHPDDMCommunicationAvoidingPCASM_Private(data->levels[0]->pc, S, PETSC_TRUE)); /* the subdomain Mat is already known and the input IS of PCASMSetLocalSubdomains() is already sorted */ 224313044ca3SPierre Jolivet PetscTryMethod(data->levels[0]->pc, "PCASMGetSubKSP_C", (PC, PetscInt *, PetscInt *, KSP **), (data->levels[0]->pc, &n, nullptr, &ksp)); 224413044ca3SPierre Jolivet PetscCheck(n == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of subdomain solver %" PetscInt_FMT " != 1", n); 224513044ca3SPierre Jolivet PetscCall(KSPGetPC(ksp[0], &inner)); 224613044ca3SPierre Jolivet PetscCall(PCSetType(inner, PCSHELL)); /* compute the action of the inverse of the local Schur complement with a PCSHELL */ 224713044ca3SPierre Jolivet b[0] = subA[0]; 224813044ca3SPierre Jolivet b[1] = T; 224913044ca3SPierre Jolivet b[2] = *B; 225013044ca3SPierre Jolivet b[3] = data->aux; 225113044ca3SPierre 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 */ 225213044ca3SPierre Jolivet if (!A01) PetscCall(MatDestroySubMatrices(1, &B)); 225313044ca3SPierre Jolivet else { 225413044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)*B)); 225513044ca3SPierre Jolivet PetscCall(PetscFree(B)); 225613044ca3SPierre Jolivet } 225713044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)T)); 225813044ca3SPierre Jolivet PetscCall(PCCreate(PETSC_COMM_SELF, &s)); 225913044ca3SPierre Jolivet PetscCall(PCSetOptionsPrefix(s, ((PetscObject)inner)->prefix)); 226013044ca3SPierre Jolivet PetscCall(PCSetOptionsPrefix(inner, nullptr)); 226113044ca3SPierre Jolivet PetscCall(KSPSetSkipPCSetFromOptions(ksp[0], PETSC_TRUE)); 226213044ca3SPierre Jolivet PetscCall(PCSetType(s, PCLU)); 226313044ca3SPierre Jolivet if (PetscDefined(HAVE_MUMPS)) { /* only MATSOLVERMUMPS handles MATNEST, so for the others, e.g., MATSOLVERPETSC or MATSOLVERMKL_PARDISO, convert to plain MATAIJ */ 226413044ca3SPierre Jolivet PetscCall(PCFactorSetMatSolverType(s, MATSOLVERMUMPS)); 226513044ca3SPierre Jolivet } 226613044ca3SPierre Jolivet PetscCall(PCSetFromOptions(s)); 226713044ca3SPierre Jolivet PetscCall(PCFactorGetMatSolverType(s, &type)); 226813044ca3SPierre Jolivet PetscCall(PetscStrcmp(type, MATSOLVERMUMPS, &flg)); 226913044ca3SPierre Jolivet if (flg) { 227013044ca3SPierre Jolivet PetscCall(PCSetOperators(s, N, N)); 227113044ca3SPierre Jolivet PetscCall(PCFactorGetMatrix(s, &T)); 227213044ca3SPierre Jolivet PetscCall(MatSetOptionsPrefix(T, ((PetscObject)s)->prefix)); 227313044ca3SPierre Jolivet PetscCall(MatNestGetISs(N, is, nullptr)); 227413044ca3SPierre Jolivet PetscCall(MatFactorSetSchurIS(T, is[1])); 227513044ca3SPierre Jolivet } else { 227613044ca3SPierre Jolivet PetscCall(MatConvert(N, MATAIJ, MAT_INITIAL_MATRIX, &T)); 227713044ca3SPierre Jolivet PetscCall(PCSetOperators(s, N, T)); 227813044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)T)); 227913044ca3SPierre Jolivet PetscCall(PCFactorGetMatrix(s, &T)); /* 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 */ 228013044ca3SPierre Jolivet } 228113044ca3SPierre Jolivet PetscCall(PetscNew(&p)); 228213044ca3SPierre Jolivet p->first = s; 228313044ca3SPierre Jolivet PetscCall(MatCreateVecs(T, p->second, p->second + 1)); 228413044ca3SPierre Jolivet PetscCall(PCShellSetContext(inner, p)); 228513044ca3SPierre Jolivet PetscCall(PCShellSetApply(inner, PCApply_Nest)); 228613044ca3SPierre Jolivet PetscCall(PCShellSetView(inner, PCView_Nest)); 228713044ca3SPierre Jolivet PetscCall(PCShellSetDestroy(inner, PCDestroy_Nest)); 228813044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)N)); 228913044ca3SPierre Jolivet } 2290f1580f4eSBarry Smith if (!data->levels[0]->scatter) { 2291db4a47b3SPierre Jolivet PetscCall(MatCreateVecs(P, &xin, nullptr)); 2292f1580f4eSBarry Smith if (ismatis) PetscCall(MatDestroy(&P)); 2293db4a47b3SPierre Jolivet PetscCall(VecScatterCreate(xin, data->is, data->levels[0]->D, nullptr, &data->levels[0]->scatter)); 2294f1580f4eSBarry Smith PetscCall(VecDestroy(&xin)); 2295f1580f4eSBarry Smith } 2296f1580f4eSBarry Smith if (data->levels[0]->P) { 2297f1580f4eSBarry Smith /* if the pattern is the same and PCSetUp() has previously succeeded, reuse HPDDM buffers and connectivity */ 2298f1580f4eSBarry Smith PetscCall(HPDDM::Schwarz<PetscScalar>::destroy(data->levels[0], pc->setupcalled < 1 || pc->flag == DIFFERENT_NONZERO_PATTERN ? PETSC_TRUE : PETSC_FALSE)); 2299f1580f4eSBarry Smith } 2300f1580f4eSBarry Smith if (!data->levels[0]->P) data->levels[0]->P = new HPDDM::Schwarz<PetscScalar>(); 2301db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventBegin(PC_HPDDM_SetUp[0], data->levels[0]->ksp, nullptr, nullptr, nullptr)); 2302db4a47b3SPierre Jolivet else PetscCall(PetscLogEventBegin(PC_HPDDM_Strc, data->levels[0]->ksp, nullptr, nullptr, nullptr)); 2303f1580f4eSBarry Smith /* HPDDM internal data structure */ 230413044ca3SPierre Jolivet PetscCall(data->levels[0]->P->structure(loc, data->is, !ctx ? sub[0] : nullptr, ismatis ? C : data->aux, data->levels)); 2305db4a47b3SPierre Jolivet if (!data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_Strc, data->levels[0]->ksp, nullptr, nullptr, nullptr)); 2306f1580f4eSBarry Smith /* matrix pencil of the generalized eigenvalue problem on the overlap (GenEO) */ 230713044ca3SPierre Jolivet if (!ctx) { 2308*9bb5c669SPierre Jolivet if (data->deflation || overlap != -1) weighted = data->aux; 2309f1580f4eSBarry Smith else if (!data->B) { 2310f1580f4eSBarry Smith PetscBool cmp[2]; 2311f1580f4eSBarry Smith PetscCall(MatDuplicate(sub[0], MAT_COPY_VALUES, &weighted)); 2312f1580f4eSBarry Smith PetscCall(PetscObjectTypeCompare((PetscObject)weighted, MATNORMAL, cmp)); 2313f1580f4eSBarry Smith if (!cmp[0]) PetscCall(PetscObjectTypeCompare((PetscObject)weighted, MATNORMALHERMITIAN, cmp + 1)); 2314f1580f4eSBarry Smith else cmp[1] = PETSC_FALSE; 2315f1580f4eSBarry Smith if (!cmp[0] && !cmp[1]) PetscCall(MatDiagonalScale(weighted, data->levels[0]->D, data->levels[0]->D)); 2316f1580f4eSBarry Smith else { /* MATNORMAL applies MatDiagonalScale() in a matrix-free fashion, not what is needed since this won't be passed to SLEPc during the eigensolve */ 2317f1580f4eSBarry Smith if (cmp[0]) PetscCall(MatNormalGetMat(weighted, &data->B)); 2318f1580f4eSBarry Smith else PetscCall(MatNormalHermitianGetMat(weighted, &data->B)); 2319db4a47b3SPierre Jolivet PetscCall(MatDiagonalScale(data->B, nullptr, data->levels[0]->D)); 2320db4a47b3SPierre Jolivet data->B = nullptr; 2321f1580f4eSBarry Smith flg = PETSC_FALSE; 2322f1580f4eSBarry Smith } 2323f1580f4eSBarry Smith /* neither MatDuplicate() nor MatDiagonaleScale() handles the symmetry options, so propagate the options explicitly */ 2324f1580f4eSBarry Smith /* only useful for -mat_type baij -pc_hpddm_levels_1_st_pc_type cholesky (no problem with MATAIJ or MATSBAIJ) */ 2325f1580f4eSBarry Smith PetscCall(MatPropagateSymmetryOptions(sub[0], weighted)); 2326f1580f4eSBarry Smith } else weighted = data->B; 232713044ca3SPierre Jolivet } else weighted = nullptr; 2328f1580f4eSBarry Smith /* SLEPc is used inside the loaded symbol */ 2329*9bb5c669SPierre 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)); 2330*9bb5c669SPierre Jolivet if (!ctx && data->share && overlap == -1) { 2331f1580f4eSBarry Smith Mat st[2]; 2332f1580f4eSBarry Smith PetscCall(KSPGetOperators(ksp[0], st, st + 1)); 23335c7345deSPierre Jolivet PetscCall(MatCopy(subA[0], st[0], structure)); 2334f1580f4eSBarry Smith if (subA[1] != subA[0] || st[1] != st[0]) PetscCall(MatCopy(subA[1], st[1], SAME_NONZERO_PATTERN)); 2335f1580f4eSBarry Smith } 2336db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_SetUp[0], data->levels[0]->ksp, nullptr, nullptr, nullptr)); 2337f1580f4eSBarry Smith if (ismatis) PetscCall(MatISGetLocalMat(C, &N)); 2338f1580f4eSBarry Smith else N = data->aux; 233913044ca3SPierre Jolivet if (!ctx) P = sub[0]; 234013044ca3SPierre Jolivet else P = S; 2341f1580f4eSBarry Smith /* going through the grid hierarchy */ 2342f1580f4eSBarry Smith for (n = 1; n < data->N; ++n) { 2343db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventBegin(PC_HPDDM_SetUp[n], data->levels[n]->ksp, nullptr, nullptr, nullptr)); 2344f1580f4eSBarry Smith /* method composed in the loaded symbol since there, SLEPc is used as well */ 2345f1580f4eSBarry 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)); 2346db4a47b3SPierre Jolivet if (data->log_separate) PetscCall(PetscLogEventEnd(PC_HPDDM_SetUp[n], data->levels[n]->ksp, nullptr, nullptr, nullptr)); 2347f1580f4eSBarry Smith } 2348f1580f4eSBarry Smith /* reset to NULL to avoid any faulty use */ 2349db4a47b3SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)data->levels[0]->ksp, "PCHPDDMSetUp_Private_C", nullptr)); 2350db4a47b3SPierre Jolivet if (!ismatis) PetscCall(PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_C", nullptr)); 2351f1580f4eSBarry Smith else PetscCall(PetscObjectDereference((PetscObject)C)); /* matching PetscObjectReference() above */ 2352f1580f4eSBarry Smith for (n = 0; n < data->N - 1; ++n) 2353f1580f4eSBarry Smith if (data->levels[n]->P) { 2354f1580f4eSBarry Smith /* HPDDM internal work buffers */ 2355f1580f4eSBarry Smith data->levels[n]->P->setBuffer(); 2356f1580f4eSBarry Smith data->levels[n]->P->super::start(); 2357f1580f4eSBarry Smith } 2358*9bb5c669SPierre Jolivet if (ismatis || !subdomains) PetscCall(PCHPDDMDestroySubMatrices_Private(PetscBool3ToBool(data->Neumann), PetscBool(algebraic && !block && overlap == -1), sub)); 2359db4a47b3SPierre Jolivet if (ismatis) data->is = nullptr; 2360f1580f4eSBarry Smith for (n = 0; n < data->N - 1 + (reused > 0); ++n) { 2361f1580f4eSBarry Smith if (data->levels[n]->P) { 2362f1580f4eSBarry Smith PC spc; 2363f1580f4eSBarry Smith 2364f1580f4eSBarry Smith /* force the PC to be PCSHELL to do the coarse grid corrections */ 2365f1580f4eSBarry Smith PetscCall(KSPSetSkipPCSetFromOptions(data->levels[n]->ksp, PETSC_TRUE)); 2366f1580f4eSBarry Smith PetscCall(KSPGetPC(data->levels[n]->ksp, &spc)); 2367f1580f4eSBarry Smith PetscCall(PCSetType(spc, PCSHELL)); 2368f1580f4eSBarry Smith PetscCall(PCShellSetContext(spc, data->levels[n])); 2369f1580f4eSBarry Smith PetscCall(PCShellSetSetUp(spc, PCSetUp_HPDDMShell)); 2370f1580f4eSBarry Smith PetscCall(PCShellSetApply(spc, PCApply_HPDDMShell)); 2371f1580f4eSBarry Smith PetscCall(PCShellSetMatApply(spc, PCMatApply_HPDDMShell)); 237213044ca3SPierre Jolivet if (ctx && n == 0) { 237313044ca3SPierre Jolivet Mat Amat, Pmat; 237413044ca3SPierre Jolivet PetscInt m, M; 237513044ca3SPierre Jolivet std::tuple<Mat, VecScatter, Vec[2]> *ctx; 237613044ca3SPierre Jolivet 237713044ca3SPierre Jolivet PetscCall(KSPGetOperators(data->levels[n]->ksp, nullptr, &Pmat)); 237813044ca3SPierre Jolivet PetscCall(MatGetLocalSize(Pmat, &m, nullptr)); 237913044ca3SPierre Jolivet PetscCall(MatGetSize(Pmat, &M, nullptr)); 238013044ca3SPierre Jolivet PetscCall(PetscNew(&ctx)); 238113044ca3SPierre Jolivet std::get<0>(*ctx) = S; 238213044ca3SPierre Jolivet std::get<1>(*ctx) = data->levels[n]->scatter; 238313044ca3SPierre Jolivet PetscCall(MatCreateShell(PetscObjectComm((PetscObject)data->levels[n]->ksp), m, m, M, M, ctx, &Amat)); 238413044ca3SPierre Jolivet PetscCall(MatShellSetOperation(Amat, MATOP_MULT, (void (*)(void))MatMult_Schur<false>)); 238513044ca3SPierre Jolivet PetscCall(MatShellSetOperation(Amat, MATOP_MULT_TRANSPOSE, (void (*)(void))MatMult_Schur<true>)); 238613044ca3SPierre Jolivet PetscCall(MatShellSetOperation(Amat, MATOP_DESTROY, (void (*)(void))MatDestroy_Schur)); 238713044ca3SPierre Jolivet PetscCall(MatCreateVecs(S, std::get<2>(*ctx), std::get<2>(*ctx) + 1)); 238813044ca3SPierre Jolivet PetscCall(KSPSetOperators(data->levels[n]->ksp, Amat, Pmat)); 238913044ca3SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)Amat)); 239013044ca3SPierre Jolivet } 2391f1580f4eSBarry Smith PetscCall(PCShellSetDestroy(spc, PCDestroy_HPDDMShell)); 2392f1580f4eSBarry Smith if (!data->levels[n]->pc) PetscCall(PCCreate(PetscObjectComm((PetscObject)data->levels[n]->ksp), &data->levels[n]->pc)); 2393f1580f4eSBarry Smith if (n < reused) { 2394f1580f4eSBarry Smith PetscCall(PCSetReusePreconditioner(spc, PETSC_TRUE)); 2395f1580f4eSBarry Smith PetscCall(PCSetReusePreconditioner(data->levels[n]->pc, PETSC_TRUE)); 2396f1580f4eSBarry Smith } 2397f1580f4eSBarry Smith PetscCall(PCSetUp(spc)); 2398f1580f4eSBarry Smith } 2399f1580f4eSBarry Smith } 240013044ca3SPierre Jolivet if (ctx) PetscCall(MatDestroy(&S)); 2401*9bb5c669SPierre Jolivet if (overlap == -1) PetscCall(PetscObjectComposeFunction((PetscObject)pc->pmat, "PCHPDDMAlgebraicAuxiliaryMat_Private_C", nullptr)); 2402f1580f4eSBarry Smith } else flg = reused ? PETSC_FALSE : PETSC_TRUE; 2403f1580f4eSBarry Smith if (!ismatis && subdomains) { 2404f1580f4eSBarry Smith if (flg) PetscCall(KSPGetPC(data->levels[0]->ksp, &inner)); 2405f1580f4eSBarry Smith else inner = data->levels[0]->pc; 2406f1580f4eSBarry Smith if (inner) { 2407398c7888SPierre Jolivet if (!inner->setupcalled) PetscCall(PCSetType(inner, PCASM)); 2408398c7888SPierre Jolivet PetscCall(PCSetFromOptions(inner)); 2409398c7888SPierre Jolivet PetscCall(PetscStrcmp(((PetscObject)inner)->type_name, PCASM, &flg)); 2410398c7888SPierre Jolivet if (flg) { 2411f1580f4eSBarry Smith if (!inner->setupcalled) { /* evaluates to PETSC_FALSE when -pc_hpddm_block_splitting */ 2412398c7888SPierre Jolivet IS sorted; /* PCASM will sort the input IS, duplicate it to return an unmodified (PCHPDDM) input IS */ 2413398c7888SPierre Jolivet PetscCall(ISDuplicate(is[0], &sorted)); 2414398c7888SPierre Jolivet PetscCall(PCASMSetLocalSubdomains(inner, 1, &sorted, &loc)); 2415398c7888SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)sorted)); 2416398c7888SPierre Jolivet } 2417c8ea6600SPierre Jolivet if (!PetscBool3ToBool(data->Neumann) && data->N > 1) { /* subdomain matrices are already created for the eigenproblem, reuse them for the fine-level PC */ 2418db4a47b3SPierre Jolivet PetscCall(PCHPDDMPermute_Private(*is, nullptr, nullptr, sub[0], &P, nullptr)); 2419398c7888SPierre Jolivet PetscCall(PCHPDDMCommunicationAvoidingPCASM_Private(inner, P, algebraic)); 2420398c7888SPierre Jolivet PetscCall(PetscObjectDereference((PetscObject)P)); 2421f1580f4eSBarry Smith } 2422f1580f4eSBarry Smith } 2423f1580f4eSBarry Smith } 2424*9bb5c669SPierre Jolivet if (data->N > 1) { 2425*9bb5c669SPierre Jolivet if (overlap != 1) PetscCall(PCHPDDMDestroySubMatrices_Private(PetscBool3ToBool(data->Neumann), PetscBool(algebraic && !block && overlap == -1), sub)); 2426*9bb5c669SPierre Jolivet if (overlap == 1) PetscCall(MatDestroy(subA)); 2427*9bb5c669SPierre Jolivet } 2428f1580f4eSBarry Smith } 2429f1580f4eSBarry Smith PetscCall(ISDestroy(&loc)); 2430f1580f4eSBarry Smith } else data->N = 1 + reused; /* enforce this value to 1 + reused if there is no way to build another level */ 2431f1580f4eSBarry Smith if (requested != data->N + reused) { 2432f1580f4eSBarry 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, 2433f1580f4eSBarry Smith data->N, pcpre ? pcpre : "")); 2434f1580f4eSBarry Smith PetscCall(PetscInfo(pc, "It is best to tune parameters, e.g., a higher value for -%spc_hpddm_levels_%" PetscInt_FMT "_eps_threshold so that at least one local deflation vector will be selected\n", pcpre ? pcpre : "", data->N)); 2435f1580f4eSBarry Smith /* cannot use PCDestroy_HPDDMShell() because PCSHELL not set for unassembled levels */ 2436f1580f4eSBarry Smith for (n = data->N - 1; n < requested - 1; ++n) { 2437f1580f4eSBarry Smith if (data->levels[n]->P) { 2438f1580f4eSBarry Smith PetscCall(HPDDM::Schwarz<PetscScalar>::destroy(data->levels[n], PETSC_TRUE)); 2439f1580f4eSBarry Smith PetscCall(VecDestroyVecs(1, &data->levels[n]->v[0])); 2440f1580f4eSBarry Smith PetscCall(VecDestroyVecs(2, &data->levels[n]->v[1])); 2441f1580f4eSBarry Smith PetscCall(MatDestroy(data->levels[n]->V)); 2442f1580f4eSBarry Smith PetscCall(MatDestroy(data->levels[n]->V + 1)); 2443f1580f4eSBarry Smith PetscCall(MatDestroy(data->levels[n]->V + 2)); 2444f1580f4eSBarry Smith PetscCall(VecDestroy(&data->levels[n]->D)); 2445f1580f4eSBarry Smith PetscCall(VecScatterDestroy(&data->levels[n]->scatter)); 2446f1580f4eSBarry Smith } 2447f1580f4eSBarry Smith } 2448f1580f4eSBarry Smith if (reused) { 2449f1580f4eSBarry Smith for (n = reused; n < PETSC_PCHPDDM_MAXLEVELS && data->levels[n]; ++n) { 2450f1580f4eSBarry Smith PetscCall(KSPDestroy(&data->levels[n]->ksp)); 2451f1580f4eSBarry Smith PetscCall(PCDestroy(&data->levels[n]->pc)); 2452f1580f4eSBarry Smith } 2453f1580f4eSBarry Smith } 2454f1580f4eSBarry Smith PetscCheck(!PetscDefined(USE_DEBUG), PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "%" PetscInt_FMT " levels requested, only %" PetscInt_FMT " built + %" PetscInt_FMT " reused. Options for level(s) > %" PetscInt_FMT ", including -%spc_hpddm_coarse_ will not be taken into account. It is best to tune parameters, e.g., a higher value for -%spc_hpddm_levels_%" PetscInt_FMT "_eps_threshold so that at least one local deflation vector will be selected. If you don't want this to error out, compile --with-debugging=0", requested, 2455f1580f4eSBarry Smith data->N, reused, data->N, pcpre ? pcpre : "", pcpre ? pcpre : "", data->N); 2456f1580f4eSBarry Smith } 2457f1580f4eSBarry Smith /* these solvers are created after PCSetFromOptions() is called */ 2458f1580f4eSBarry Smith if (pc->setfromoptionscalled) { 2459f1580f4eSBarry Smith for (n = 0; n < data->N; ++n) { 2460f1580f4eSBarry Smith if (data->levels[n]->ksp) PetscCall(KSPSetFromOptions(data->levels[n]->ksp)); 2461f1580f4eSBarry Smith if (data->levels[n]->pc) PetscCall(PCSetFromOptions(data->levels[n]->pc)); 2462f1580f4eSBarry Smith } 2463f1580f4eSBarry Smith pc->setfromoptionscalled = 0; 2464f1580f4eSBarry Smith } 2465f1580f4eSBarry Smith data->N += reused; 2466f1580f4eSBarry Smith if (data->share && swap) { 2467f1580f4eSBarry Smith /* swap back pointers so that variables follow the user-provided numbering */ 2468f1580f4eSBarry Smith std::swap(C, data->aux); 2469f1580f4eSBarry Smith std::swap(uis, data->is); 2470f1580f4eSBarry Smith PetscCall(MatDestroy(&C)); 2471f1580f4eSBarry Smith PetscCall(ISDestroy(&uis)); 2472f1580f4eSBarry Smith } 2473398c7888SPierre Jolivet if (algebraic) PetscCall(MatDestroy(&data->aux)); 247438fb34a1SPierre Jolivet if (unsorted && unsorted != is[0]) { 2475398c7888SPierre Jolivet PetscCall(ISCopy(unsorted, data->is)); 2476398c7888SPierre Jolivet PetscCall(ISDestroy(&unsorted)); 2477398c7888SPierre Jolivet } 2478398c7888SPierre Jolivet #if PetscDefined(USE_DEBUG) 2479398c7888SPierre 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); 2480398c7888SPierre Jolivet if (data->is) { 2481398c7888SPierre Jolivet PetscCall(ISEqualUnsorted(data->is, dis, &flg)); 2482398c7888SPierre Jolivet PetscCall(ISDestroy(&dis)); 2483398c7888SPierre Jolivet PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input IS and output IS are not equal"); 2484398c7888SPierre Jolivet } 2485398c7888SPierre 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); 2486398c7888SPierre Jolivet if (data->aux) { 2487398c7888SPierre Jolivet PetscCall(MatMultEqual(data->aux, daux, 20, &flg)); 2488398c7888SPierre Jolivet PetscCall(MatDestroy(&daux)); 2489398c7888SPierre Jolivet PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input Mat and output Mat are not equal"); 2490398c7888SPierre Jolivet } 2491398c7888SPierre Jolivet #endif 24923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2493f1580f4eSBarry Smith } 2494f1580f4eSBarry Smith 2495f1580f4eSBarry Smith /*@ 2496f1580f4eSBarry Smith PCHPDDMSetCoarseCorrectionType - Sets the coarse correction type. 2497f1580f4eSBarry Smith 2498c3339decSBarry Smith Collective 2499f1580f4eSBarry Smith 2500f1580f4eSBarry Smith Input Parameters: 2501f1580f4eSBarry Smith + pc - preconditioner context 2502f1580f4eSBarry Smith - type - `PC_HPDDM_COARSE_CORRECTION_DEFLATED`, `PC_HPDDM_COARSE_CORRECTION_ADDITIVE`, or `PC_HPDDM_COARSE_CORRECTION_BALANCED` 2503f1580f4eSBarry Smith 2504f1580f4eSBarry Smith Options Database Key: 2505f1580f4eSBarry Smith . -pc_hpddm_coarse_correction <deflated, additive, balanced> - type of coarse correction to apply 2506f1580f4eSBarry Smith 2507f1580f4eSBarry Smith Level: intermediate 2508f1580f4eSBarry Smith 2509f1580f4eSBarry Smith .seealso: `PCHPDDMGetCoarseCorrectionType()`, `PCHPDDM`, `PCHPDDMCoarseCorrectionType` 2510f1580f4eSBarry Smith @*/ 2511d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMSetCoarseCorrectionType(PC pc, PCHPDDMCoarseCorrectionType type) 2512d71ae5a4SJacob Faibussowitsch { 2513f1580f4eSBarry Smith PetscFunctionBegin; 2514f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 2515f1580f4eSBarry Smith PetscValidLogicalCollectiveEnum(pc, type, 2); 2516f1580f4eSBarry Smith PetscTryMethod(pc, "PCHPDDMSetCoarseCorrectionType_C", (PC, PCHPDDMCoarseCorrectionType), (pc, type)); 25173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2518f1580f4eSBarry Smith } 2519f1580f4eSBarry Smith 2520f1580f4eSBarry Smith /*@ 2521f1580f4eSBarry Smith PCHPDDMGetCoarseCorrectionType - Gets the coarse correction type. 2522f1580f4eSBarry Smith 2523f1580f4eSBarry Smith Input Parameter: 2524f1580f4eSBarry Smith . pc - preconditioner context 2525f1580f4eSBarry Smith 2526f1580f4eSBarry Smith Output Parameter: 2527f1580f4eSBarry Smith . type - `PC_HPDDM_COARSE_CORRECTION_DEFLATED`, `PC_HPDDM_COARSE_CORRECTION_ADDITIVE`, or `PC_HPDDM_COARSE_CORRECTION_BALANCED` 2528f1580f4eSBarry Smith 2529f1580f4eSBarry Smith Level: intermediate 2530f1580f4eSBarry Smith 2531f1580f4eSBarry Smith .seealso: `PCHPDDMSetCoarseCorrectionType()`, `PCHPDDM`, `PCHPDDMCoarseCorrectionType` 2532f1580f4eSBarry Smith @*/ 2533d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMGetCoarseCorrectionType(PC pc, PCHPDDMCoarseCorrectionType *type) 2534d71ae5a4SJacob Faibussowitsch { 2535f1580f4eSBarry Smith PetscFunctionBegin; 2536f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 2537f1580f4eSBarry Smith if (type) { 25384f572ea9SToby Isaac PetscAssertPointer(type, 2); 2539f1580f4eSBarry Smith PetscUseMethod(pc, "PCHPDDMGetCoarseCorrectionType_C", (PC, PCHPDDMCoarseCorrectionType *), (pc, type)); 2540f1580f4eSBarry Smith } 25413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2542f1580f4eSBarry Smith } 2543f1580f4eSBarry Smith 2544d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetCoarseCorrectionType_HPDDM(PC pc, PCHPDDMCoarseCorrectionType type) 2545d71ae5a4SJacob Faibussowitsch { 2546f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 2547f1580f4eSBarry Smith 2548f1580f4eSBarry Smith PetscFunctionBegin; 2549f1580f4eSBarry Smith PetscCheck(type >= 0 && type <= 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PCHPDDMCoarseCorrectionType %d", type); 2550f1580f4eSBarry Smith data->correction = type; 25513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2552f1580f4eSBarry Smith } 2553f1580f4eSBarry Smith 2554d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMGetCoarseCorrectionType_HPDDM(PC pc, PCHPDDMCoarseCorrectionType *type) 2555d71ae5a4SJacob Faibussowitsch { 2556f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 2557f1580f4eSBarry Smith 2558f1580f4eSBarry Smith PetscFunctionBegin; 2559f1580f4eSBarry Smith *type = data->correction; 25603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2561f1580f4eSBarry Smith } 2562f1580f4eSBarry Smith 2563f1580f4eSBarry Smith /*@ 2564e31fc274SPierre Jolivet PCHPDDMSetSTShareSubKSP - Sets whether the `KSP` in SLEPc `ST` and the fine-level subdomain solver should be shared. 2565e31fc274SPierre Jolivet 2566e31fc274SPierre Jolivet Input Parameters: 2567e31fc274SPierre Jolivet + pc - preconditioner context 2568e31fc274SPierre Jolivet - share - whether the `KSP` should be shared or not 2569e31fc274SPierre Jolivet 2570e31fc274SPierre Jolivet Note: 2571e31fc274SPierre Jolivet This is not the same as `PCSetReusePreconditioner()`. Given certain conditions (visible using -info), a symbolic factorization can be skipped 2572e31fc274SPierre Jolivet when using a subdomain `PCType` such as `PCLU` or `PCCHOLESKY`. 2573e31fc274SPierre Jolivet 2574e31fc274SPierre Jolivet Level: advanced 2575e31fc274SPierre Jolivet 2576e31fc274SPierre Jolivet .seealso: `PCHPDDM`, `PCHPDDMGetSTShareSubKSP()` 2577e31fc274SPierre Jolivet @*/ 2578e31fc274SPierre Jolivet PetscErrorCode PCHPDDMSetSTShareSubKSP(PC pc, PetscBool share) 2579e31fc274SPierre Jolivet { 2580e31fc274SPierre Jolivet PetscFunctionBegin; 2581e31fc274SPierre Jolivet PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 2582e31fc274SPierre Jolivet PetscTryMethod(pc, "PCHPDDMSetSTShareSubKSP_C", (PC, PetscBool), (pc, share)); 25833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2584e31fc274SPierre Jolivet } 2585e31fc274SPierre Jolivet 2586e31fc274SPierre Jolivet /*@ 2587f1580f4eSBarry Smith PCHPDDMGetSTShareSubKSP - Gets whether the `KSP` in SLEPc `ST` and the fine-level subdomain solver is shared. 2588f1580f4eSBarry Smith 2589f1580f4eSBarry Smith Input Parameter: 2590f1580f4eSBarry Smith . pc - preconditioner context 2591f1580f4eSBarry Smith 2592f1580f4eSBarry Smith Output Parameter: 2593f1580f4eSBarry Smith . share - whether the `KSP` is shared or not 2594f1580f4eSBarry Smith 2595f1580f4eSBarry Smith Note: 2596f1580f4eSBarry 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 2597f1580f4eSBarry Smith when using a subdomain `PCType` such as `PCLU` or `PCCHOLESKY`. 2598f1580f4eSBarry Smith 2599f1580f4eSBarry Smith Level: advanced 2600f1580f4eSBarry Smith 2601e31fc274SPierre Jolivet .seealso: `PCHPDDM`, `PCHPDDMSetSTShareSubKSP()` 2602f1580f4eSBarry Smith @*/ 2603d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMGetSTShareSubKSP(PC pc, PetscBool *share) 2604d71ae5a4SJacob Faibussowitsch { 2605f1580f4eSBarry Smith PetscFunctionBegin; 2606f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 2607f1580f4eSBarry Smith if (share) { 26084f572ea9SToby Isaac PetscAssertPointer(share, 2); 2609f1580f4eSBarry Smith PetscUseMethod(pc, "PCHPDDMGetSTShareSubKSP_C", (PC, PetscBool *), (pc, share)); 2610f1580f4eSBarry Smith } 26113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2612f1580f4eSBarry Smith } 2613f1580f4eSBarry Smith 2614e31fc274SPierre Jolivet static PetscErrorCode PCHPDDMSetSTShareSubKSP_HPDDM(PC pc, PetscBool share) 2615e31fc274SPierre Jolivet { 2616e31fc274SPierre Jolivet PC_HPDDM *data = (PC_HPDDM *)pc->data; 2617e31fc274SPierre Jolivet 2618e31fc274SPierre Jolivet PetscFunctionBegin; 2619e31fc274SPierre Jolivet data->share = share; 26203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2621e31fc274SPierre Jolivet } 2622e31fc274SPierre Jolivet 2623d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMGetSTShareSubKSP_HPDDM(PC pc, PetscBool *share) 2624d71ae5a4SJacob Faibussowitsch { 2625f1580f4eSBarry Smith PC_HPDDM *data = (PC_HPDDM *)pc->data; 2626f1580f4eSBarry Smith 2627f1580f4eSBarry Smith PetscFunctionBegin; 2628f1580f4eSBarry Smith *share = data->share; 26293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2630f1580f4eSBarry Smith } 2631f1580f4eSBarry Smith 2632f1580f4eSBarry Smith /*@ 2633f1580f4eSBarry Smith PCHPDDMSetDeflationMat - Sets the deflation space used to assemble a coarser operator. 2634f1580f4eSBarry Smith 2635f1580f4eSBarry Smith Input Parameters: 2636f1580f4eSBarry Smith + pc - preconditioner context 2637f1580f4eSBarry Smith . is - index set of the local deflation matrix 2638f1580f4eSBarry Smith - U - deflation sequential matrix stored as a `MATSEQDENSE` 2639f1580f4eSBarry Smith 2640f1580f4eSBarry Smith Level: advanced 2641f1580f4eSBarry Smith 2642f1580f4eSBarry Smith .seealso: `PCHPDDM`, `PCDeflationSetSpace()`, `PCMGSetRestriction()` 2643f1580f4eSBarry Smith @*/ 2644d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMSetDeflationMat(PC pc, IS is, Mat U) 2645d71ae5a4SJacob Faibussowitsch { 2646f1580f4eSBarry Smith PetscFunctionBegin; 2647f1580f4eSBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 2648f1580f4eSBarry Smith PetscValidHeaderSpecific(is, IS_CLASSID, 2); 2649f1580f4eSBarry Smith PetscValidHeaderSpecific(U, MAT_CLASSID, 3); 2650e31fc274SPierre Jolivet PetscTryMethod(pc, "PCHPDDMSetDeflationMat_C", (PC, IS, Mat), (pc, is, U)); 26513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2652f1580f4eSBarry Smith } 2653f1580f4eSBarry Smith 2654d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCHPDDMSetDeflationMat_HPDDM(PC pc, IS is, Mat U) 2655d71ae5a4SJacob Faibussowitsch { 2656f1580f4eSBarry Smith PetscFunctionBegin; 2657f1580f4eSBarry Smith PetscCall(PCHPDDMSetAuxiliaryMat_Private(pc, is, U, PETSC_TRUE)); 26583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2659f1580f4eSBarry Smith } 2660f1580f4eSBarry Smith 2661d71ae5a4SJacob Faibussowitsch PetscErrorCode HPDDMLoadDL_Private(PetscBool *found) 2662d71ae5a4SJacob Faibussowitsch { 2663605ad303SPierre Jolivet PetscBool flg; 2664f1580f4eSBarry Smith char lib[PETSC_MAX_PATH_LEN], dlib[PETSC_MAX_PATH_LEN], dir[PETSC_MAX_PATH_LEN]; 2665f1580f4eSBarry Smith 2666f1580f4eSBarry Smith PetscFunctionBegin; 26674f572ea9SToby Isaac PetscAssertPointer(found, 1); 2668c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(dir, "${PETSC_LIB_DIR}", sizeof(dir))); 2669db4a47b3SPierre Jolivet PetscCall(PetscOptionsGetString(nullptr, nullptr, "-hpddm_dir", dir, sizeof(dir), nullptr)); 2670f1580f4eSBarry Smith PetscCall(PetscSNPrintf(lib, sizeof(lib), "%s/libhpddm_petsc", dir)); 2671f1580f4eSBarry Smith PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found)); 2672605ad303SPierre Jolivet #if defined(SLEPC_LIB_DIR) /* this variable is passed during SLEPc ./configure when PETSc has not been configured */ 2673605ad303SPierre Jolivet if (!*found) { /* with --download-hpddm since slepcconf.h is not yet built (and thus can't be included) */ 2674c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(dir, HPDDM_STR(SLEPC_LIB_DIR), sizeof(dir))); 2675f1580f4eSBarry Smith PetscCall(PetscSNPrintf(lib, sizeof(lib), "%s/libhpddm_petsc", dir)); 2676f1580f4eSBarry Smith PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found)); 2677f1580f4eSBarry Smith } 2678f1580f4eSBarry Smith #endif 2679605ad303SPierre 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 */ 2680605ad303SPierre Jolivet PetscCall(PetscOptionsGetenv(PETSC_COMM_SELF, "SLEPC_DIR", dir, sizeof(dir), &flg)); 2681605ad303SPierre 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 */ 2682605ad303SPierre Jolivet PetscCall(PetscSNPrintf(lib, sizeof(lib), "%s/lib/libslepc", dir)); 2683605ad303SPierre Jolivet PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found)); 2684605ad303SPierre Jolivet PetscCheck(*found, PETSC_COMM_SELF, PETSC_ERR_PLIB, "%s not found but SLEPC_DIR=%s", lib, dir); 2685605ad303SPierre Jolivet PetscCall(PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, dlib)); 2686605ad303SPierre Jolivet PetscCall(PetscSNPrintf(lib, sizeof(lib), "%s/lib/libhpddm_petsc", dir)); /* libhpddm_petsc is always in the same directory as libslepc */ 2687605ad303SPierre Jolivet PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_SELF, lib, dlib, 1024, found)); 2688605ad303SPierre Jolivet } 2689605ad303SPierre Jolivet } 2690f1580f4eSBarry Smith PetscCheck(*found, PETSC_COMM_SELF, PETSC_ERR_PLIB, "%s not found", lib); 2691f1580f4eSBarry Smith PetscCall(PetscDLLibraryAppend(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, dlib)); 26923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2693f1580f4eSBarry Smith } 2694f1580f4eSBarry Smith 2695f1580f4eSBarry Smith /*MC 2696f1580f4eSBarry Smith PCHPDDM - Interface with the HPDDM library. 2697f1580f4eSBarry Smith 2698f1580f4eSBarry Smith This `PC` may be used to build multilevel spectral domain decomposition methods based on the GenEO framework [2011, 2019]. It may be viewed as an alternative to spectral 2699f1580f4eSBarry Smith AMGe or `PCBDDC` with adaptive selection of constraints. The interface is explained in details in [2021] (see references below) 2700f1580f4eSBarry Smith 27017eb095acSPierre Jolivet The matrix to be preconditioned (Pmat) may be unassembled (`MATIS`), assembled (`MATAIJ`, `MATBAIJ`, or `MATSBAIJ`), hierarchical (`MATHTOOL`), or `MATNORMAL`. 2702f1580f4eSBarry Smith 2703f1580f4eSBarry Smith For multilevel preconditioning, when using an assembled or hierarchical Pmat, one must provide an auxiliary local `Mat` (unassembled local operator for GenEO) using 2704f1580f4eSBarry Smith `PCHPDDMSetAuxiliaryMat()`. Calling this routine is not needed when using a `MATIS` Pmat, assembly is done internally using `MatConvert()`. 2705f1580f4eSBarry Smith 2706f1580f4eSBarry Smith Options Database Keys: 2707f1580f4eSBarry Smith + -pc_hpddm_define_subdomains <true, default=false> - on the finest level, calls `PCASMSetLocalSubdomains()` with the `IS` supplied in `PCHPDDMSetAuxiliaryMat()` 2708f1580f4eSBarry Smith (not relevant with an unassembled Pmat) 2709f1580f4eSBarry Smith . -pc_hpddm_has_neumann <true, default=false> - on the finest level, informs the `PC` that the local Neumann matrix is supplied in `PCHPDDMSetAuxiliaryMat()` 2710f1580f4eSBarry Smith - -pc_hpddm_coarse_correction <type, default=deflated> - determines the `PCHPDDMCoarseCorrectionType` when calling `PCApply()` 2711f1580f4eSBarry Smith 271238bf2a8cSPierre Jolivet Options for subdomain solvers, subdomain eigensolvers (for computing deflation vectors), and the coarse solver can be set using the following options database prefixes. 2713f1580f4eSBarry Smith .vb 2714f1580f4eSBarry Smith -pc_hpddm_levels_%d_pc_ 2715f1580f4eSBarry Smith -pc_hpddm_levels_%d_ksp_ 2716f1580f4eSBarry Smith -pc_hpddm_levels_%d_eps_ 2717f1580f4eSBarry Smith -pc_hpddm_levels_%d_p 27184ec2a359SPierre Jolivet -pc_hpddm_levels_%d_mat_type 2719f1580f4eSBarry Smith -pc_hpddm_coarse_ 2720f1580f4eSBarry Smith -pc_hpddm_coarse_p 27214ec2a359SPierre Jolivet -pc_hpddm_coarse_mat_type 27222ce66baaSPierre Jolivet -pc_hpddm_coarse_mat_filter 2723f1580f4eSBarry Smith .ve 2724f1580f4eSBarry Smith 272538bf2a8cSPierre 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 2726f1580f4eSBarry Smith -pc_hpddm_coarse_p 2 -pc_hpddm_coarse_mat_type baij will use 10 deflation vectors per subdomain on the fine "level 1", 2727f1580f4eSBarry Smith aggregate the fine subdomains into 4 "level 2" subdomains, then use 10 deflation vectors per subdomain on "level 2", 27287eb095acSPierre Jolivet and assemble the coarse matrix (of dimension 4 x 10 = 40) on two processes as a `MATBAIJ` (default is `MATSBAIJ`). 2729f1580f4eSBarry Smith 2730f1580f4eSBarry Smith In order to activate a "level N+1" coarse correction, it is mandatory to call -pc_hpddm_levels_N_eps_nev <nu> or -pc_hpddm_levels_N_eps_threshold <val>. The default -pc_hpddm_coarse_p value is 1, meaning that the coarse operator is aggregated on a single process. 2731f1580f4eSBarry Smith 273238bf2a8cSPierre Jolivet This preconditioner requires that you build PETSc with SLEPc (``--download-slepc``). By default, the underlying concurrent eigenproblems 273338bf2a8cSPierre Jolivet are solved using SLEPc shift-and-invert spectral transformation. This is usually what gives the best performance for GenEO, cf. [2011, 2013]. As 273438bf2a8cSPierre 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 273538bf2a8cSPierre 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 273638bf2a8cSPierre Jolivet SLEPc documentation since they are specific to `PCHPDDM`. 273738bf2a8cSPierre Jolivet .vb 273838bf2a8cSPierre Jolivet -pc_hpddm_levels_1_st_share_sub_ksp 273938bf2a8cSPierre Jolivet -pc_hpddm_levels_%d_eps_threshold 274038bf2a8cSPierre Jolivet -pc_hpddm_levels_1_eps_use_inertia 274138bf2a8cSPierre Jolivet .ve 274238bf2a8cSPierre Jolivet 274338bf2a8cSPierre Jolivet The first option from the list only applies to the fine-level eigensolver, see `PCHPDDMSetSTShareSubKSP()`. The second option from the list is 274438bf2a8cSPierre Jolivet used to filter eigenmodes retrieved after convergence of `EPSSolve()` at "level N" such that eigenvectors used to define a "level N+1" coarse 274538bf2a8cSPierre Jolivet correction are associated to eigenvalues whose magnitude are lower or equal than -pc_hpddm_levels_N_eps_threshold. When using an `EPS` which cannot 274638bf2a8cSPierre 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 274738bf2a8cSPierre 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 274838bf2a8cSPierre Jolivet to supply -pc_hpddm_levels_1_eps_nev. This last option also only applies to the fine-level (N = 1) eigensolver. 2749f1580f4eSBarry Smith 2750f1580f4eSBarry Smith References: 2751f1580f4eSBarry Smith + 2011 - A robust two-level domain decomposition preconditioner for systems of PDEs. Spillane, Dolean, Hauret, Nataf, Pechstein, and Scheichl. Comptes Rendus Mathematique. 2752f1580f4eSBarry Smith . 2013 - Scalable domain decomposition preconditioners for heterogeneous elliptic problems. Jolivet, Hecht, Nataf, and Prud'homme. SC13. 2753f1580f4eSBarry Smith . 2015 - An introduction to domain decomposition methods: algorithms, theory, and parallel implementation. Dolean, Jolivet, and Nataf. SIAM. 2754f1580f4eSBarry Smith . 2019 - A multilevel Schwarz preconditioner based on a hierarchy of robust coarse spaces. Al Daas, Grigori, Jolivet, and Tournier. SIAM Journal on Scientific Computing. 2755f1580f4eSBarry Smith . 2021 - KSPHPDDM and PCHPDDM: extending PETSc with advanced Krylov methods and robust multilevel overlapping Schwarz preconditioners. Jolivet, Roman, and Zampini. Computer & Mathematics with Applications. 2756f1580f4eSBarry Smith . 2022a - A robust algebraic domain decomposition preconditioner for sparse normal equations. Al Daas, Jolivet, and Scott. SIAM Journal on Scientific Computing. 275713044ca3SPierre Jolivet . 2022b - A robust algebraic multilevel domain decomposition preconditioner for sparse symmetric positive definite matrices. Al Daas and Jolivet. 275813044ca3SPierre Jolivet - 2023 - Recent advances in domain decomposition methods for large-scale saddle point problems. Nataf and Tournier. Comptes Rendus Mecanique. 2759f1580f4eSBarry Smith 2760f1580f4eSBarry Smith Level: intermediate 2761f1580f4eSBarry Smith 2762f1580f4eSBarry Smith .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCHPDDMSetAuxiliaryMat()`, `MATIS`, `PCBDDC`, `PCDEFLATION`, `PCTELESCOPE`, `PCASM`, 2763e31fc274SPierre Jolivet `PCHPDDMSetCoarseCorrectionType()`, `PCHPDDMHasNeumannMat()`, `PCHPDDMSetRHSMat()`, `PCHPDDMSetDeflationMat()`, `PCHPDDMSetSTShareSubKSP()`, 2764e31fc274SPierre Jolivet `PCHPDDMGetSTShareSubKSP()`, `PCHPDDMGetCoarseCorrectionType()`, `PCHPDDMGetComplexities()` 2765f1580f4eSBarry Smith M*/ 2766d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_HPDDM(PC pc) 2767d71ae5a4SJacob Faibussowitsch { 2768f1580f4eSBarry Smith PC_HPDDM *data; 2769f1580f4eSBarry Smith PetscBool found; 2770f1580f4eSBarry Smith 2771f1580f4eSBarry Smith PetscFunctionBegin; 2772f1580f4eSBarry Smith if (!loadedSym) { 2773f1580f4eSBarry Smith PetscCall(HPDDMLoadDL_Private(&found)); 2774db4a47b3SPierre Jolivet if (found) PetscCall(PetscDLLibrarySym(PETSC_COMM_SELF, &PetscDLLibrariesLoaded, nullptr, "PCHPDDM_Internal", (void **)&loadedSym)); 2775f1580f4eSBarry Smith } 2776f1580f4eSBarry Smith PetscCheck(loadedSym, PETSC_COMM_SELF, PETSC_ERR_PLIB, "PCHPDDM_Internal symbol not found in loaded libhpddm_petsc"); 27774dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&data)); 2778f1580f4eSBarry Smith pc->data = data; 2779c8ea6600SPierre Jolivet data->Neumann = PETSC_BOOL3_UNKNOWN; 2780f1580f4eSBarry Smith pc->ops->reset = PCReset_HPDDM; 2781f1580f4eSBarry Smith pc->ops->destroy = PCDestroy_HPDDM; 2782f1580f4eSBarry Smith pc->ops->setfromoptions = PCSetFromOptions_HPDDM; 2783f1580f4eSBarry Smith pc->ops->setup = PCSetUp_HPDDM; 2784f1580f4eSBarry Smith pc->ops->apply = PCApply_HPDDM; 2785f1580f4eSBarry Smith pc->ops->matapply = PCMatApply_HPDDM; 2786f1580f4eSBarry Smith pc->ops->view = PCView_HPDDM; 2787f1580f4eSBarry Smith pc->ops->presolve = PCPreSolve_HPDDM; 2788f1580f4eSBarry Smith 2789f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetAuxiliaryMat_C", PCHPDDMSetAuxiliaryMat_HPDDM)); 2790f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMHasNeumannMat_C", PCHPDDMHasNeumannMat_HPDDM)); 2791f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetRHSMat_C", PCHPDDMSetRHSMat_HPDDM)); 2792f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetCoarseCorrectionType_C", PCHPDDMSetCoarseCorrectionType_HPDDM)); 2793f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetCoarseCorrectionType_C", PCHPDDMGetCoarseCorrectionType_HPDDM)); 2794e31fc274SPierre Jolivet PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetSTShareSubKSP_C", PCHPDDMSetSTShareSubKSP_HPDDM)); 2795f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMGetSTShareSubKSP_C", PCHPDDMGetSTShareSubKSP_HPDDM)); 2796f1580f4eSBarry Smith PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHPDDMSetDeflationMat_C", PCHPDDMSetDeflationMat_HPDDM)); 27973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2798f1580f4eSBarry Smith } 2799f1580f4eSBarry Smith 2800f1580f4eSBarry Smith /*@C 2801f1580f4eSBarry Smith PCHPDDMInitializePackage - This function initializes everything in the `PCHPDDM` package. It is called from `PCInitializePackage()`. 2802f1580f4eSBarry Smith 2803f1580f4eSBarry Smith Level: developer 2804f1580f4eSBarry Smith 2805f1580f4eSBarry Smith .seealso: `PetscInitialize()` 2806f1580f4eSBarry Smith @*/ 2807d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMInitializePackage(void) 2808d71ae5a4SJacob Faibussowitsch { 2809f1580f4eSBarry Smith char ename[32]; 2810f1580f4eSBarry Smith PetscInt i; 2811f1580f4eSBarry Smith 2812f1580f4eSBarry Smith PetscFunctionBegin; 28133ba16761SJacob Faibussowitsch if (PCHPDDMPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS); 2814f1580f4eSBarry Smith PCHPDDMPackageInitialized = PETSC_TRUE; 2815f1580f4eSBarry Smith PetscCall(PetscRegisterFinalize(PCHPDDMFinalizePackage)); 2816f1580f4eSBarry Smith /* general events registered once during package initialization */ 2817f1580f4eSBarry Smith /* some of these events are not triggered in libpetsc, */ 2818f1580f4eSBarry Smith /* but rather directly in libhpddm_petsc, */ 2819f1580f4eSBarry Smith /* which is in charge of performing the following operations */ 2820f1580f4eSBarry Smith 2821f1580f4eSBarry Smith /* domain decomposition structure from Pmat sparsity pattern */ 2822f1580f4eSBarry Smith PetscCall(PetscLogEventRegister("PCHPDDMStrc", PC_CLASSID, &PC_HPDDM_Strc)); 2823f1580f4eSBarry Smith /* Galerkin product, redistribution, and setup (not triggered in libpetsc) */ 2824f1580f4eSBarry Smith PetscCall(PetscLogEventRegister("PCHPDDMPtAP", PC_CLASSID, &PC_HPDDM_PtAP)); 2825f1580f4eSBarry Smith /* Galerkin product with summation, redistribution, and setup (not triggered in libpetsc) */ 2826f1580f4eSBarry Smith PetscCall(PetscLogEventRegister("PCHPDDMPtBP", PC_CLASSID, &PC_HPDDM_PtBP)); 2827f1580f4eSBarry Smith /* next level construction using PtAP and PtBP (not triggered in libpetsc) */ 2828f1580f4eSBarry Smith PetscCall(PetscLogEventRegister("PCHPDDMNext", PC_CLASSID, &PC_HPDDM_Next)); 2829f1580f4eSBarry Smith static_assert(PETSC_PCHPDDM_MAXLEVELS <= 9, "PETSC_PCHPDDM_MAXLEVELS value is too high"); 2830f1580f4eSBarry Smith for (i = 1; i < PETSC_PCHPDDM_MAXLEVELS; ++i) { 2831f1580f4eSBarry Smith PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCHPDDMSetUp L%1" PetscInt_FMT, i)); 2832f1580f4eSBarry Smith /* events during a PCSetUp() at level #i _except_ the assembly */ 2833f1580f4eSBarry Smith /* of the Galerkin operator of the coarser level #(i + 1) */ 2834f1580f4eSBarry Smith PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &PC_HPDDM_SetUp[i - 1])); 2835f1580f4eSBarry Smith PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCHPDDMSolve L%1" PetscInt_FMT, i)); 2836f1580f4eSBarry Smith /* events during a PCApply() at level #i _except_ */ 2837f1580f4eSBarry Smith /* the KSPSolve() of the coarser level #(i + 1) */ 2838f1580f4eSBarry Smith PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &PC_HPDDM_Solve[i - 1])); 2839f1580f4eSBarry Smith } 28403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2841f1580f4eSBarry Smith } 2842f1580f4eSBarry Smith 2843f1580f4eSBarry Smith /*@C 2844f1580f4eSBarry Smith PCHPDDMFinalizePackage - This function frees everything from the `PCHPDDM` package. It is called from `PetscFinalize()`. 2845f1580f4eSBarry Smith 2846f1580f4eSBarry Smith Level: developer 2847f1580f4eSBarry Smith 2848f1580f4eSBarry Smith .seealso: `PetscFinalize()` 2849f1580f4eSBarry Smith @*/ 2850d71ae5a4SJacob Faibussowitsch PetscErrorCode PCHPDDMFinalizePackage(void) 2851d71ae5a4SJacob Faibussowitsch { 2852f1580f4eSBarry Smith PetscFunctionBegin; 2853f1580f4eSBarry Smith PCHPDDMPackageInitialized = PETSC_FALSE; 28543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2855f1580f4eSBarry Smith } 2856*9bb5c669SPierre Jolivet 2857*9bb5c669SPierre Jolivet static PetscErrorCode MatMult_Harmonic(Mat A, Vec x, Vec y) 2858*9bb5c669SPierre Jolivet { 2859*9bb5c669SPierre Jolivet Harmonic h; /* [ A_00 A_01 ], furthermore, A_00 = [ A_loc,loc A_loc,ovl ], thus, A_01 = [ ] */ 2860*9bb5c669SPierre Jolivet /* [ A_10 A_11 A_12 ] [ A_ovl,loc A_ovl,ovl ] [ A_ovl,1 ] */ 2861*9bb5c669SPierre Jolivet Vec sub; /* y = A x = R_loc R_0 [ A_00 A_01 ]^-1 R_loc = [ I_loc ] */ 2862*9bb5c669SPierre Jolivet /* [ A_10 A_11 ] R_1^T A_12 x [ ] */ 2863*9bb5c669SPierre Jolivet PetscFunctionBegin; 2864*9bb5c669SPierre Jolivet PetscCall(MatShellGetContext(A, &h)); 2865*9bb5c669SPierre Jolivet PetscCall(VecSet(h->v, 0.0)); 2866*9bb5c669SPierre Jolivet PetscCall(VecGetSubVector(h->v, h->is[0], &sub)); 2867*9bb5c669SPierre Jolivet PetscCall(MatMult(h->A[0], x, sub)); 2868*9bb5c669SPierre Jolivet PetscCall(VecRestoreSubVector(h->v, h->is[0], &sub)); 2869*9bb5c669SPierre Jolivet PetscCall(KSPSolve(h->ksp, h->v, h->v)); 2870*9bb5c669SPierre Jolivet PetscCall(VecISCopy(h->v, h->is[1], SCATTER_REVERSE, y)); 2871*9bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 2872*9bb5c669SPierre Jolivet } 2873*9bb5c669SPierre Jolivet 2874*9bb5c669SPierre Jolivet static PetscErrorCode MatMultTranspose_Harmonic(Mat A, Vec y, Vec x) 2875*9bb5c669SPierre Jolivet { 2876*9bb5c669SPierre Jolivet Harmonic h; /* x = A^T y = [ A_00 A_01 ]^-T R_0^T R_loc^T y */ 2877*9bb5c669SPierre Jolivet Vec sub; /* A_12^T R_1 [ A_10 A_11 ] */ 2878*9bb5c669SPierre Jolivet 2879*9bb5c669SPierre Jolivet PetscFunctionBegin; 2880*9bb5c669SPierre Jolivet PetscCall(MatShellGetContext(A, &h)); 2881*9bb5c669SPierre Jolivet PetscCall(VecSet(h->v, 0.0)); 2882*9bb5c669SPierre Jolivet PetscCall(VecISCopy(h->v, h->is[1], SCATTER_FORWARD, y)); 2883*9bb5c669SPierre Jolivet PetscCall(KSPSolveTranspose(h->ksp, h->v, h->v)); 2884*9bb5c669SPierre Jolivet PetscCall(VecGetSubVector(h->v, h->is[0], &sub)); 2885*9bb5c669SPierre Jolivet PetscCall(MatMultTranspose(h->A[0], sub, x)); 2886*9bb5c669SPierre Jolivet PetscCall(VecRestoreSubVector(h->v, h->is[0], &sub)); 2887*9bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 2888*9bb5c669SPierre Jolivet } 2889*9bb5c669SPierre Jolivet 2890*9bb5c669SPierre Jolivet static PetscErrorCode MatProduct_AB_Harmonic(Mat S, Mat X, Mat Y, void *) 2891*9bb5c669SPierre Jolivet { 2892*9bb5c669SPierre Jolivet Harmonic h; 2893*9bb5c669SPierre Jolivet Mat A, B; 2894*9bb5c669SPierre Jolivet Vec a, b; 2895*9bb5c669SPierre Jolivet 2896*9bb5c669SPierre Jolivet PetscFunctionBegin; 2897*9bb5c669SPierre Jolivet PetscCall(MatShellGetContext(S, &h)); 2898*9bb5c669SPierre Jolivet PetscCall(MatMatMult(h->A[0], X, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &A)); 2899*9bb5c669SPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, h->ksp->pc->mat->rmap->n, A->cmap->n, nullptr, &B)); 2900*9bb5c669SPierre Jolivet for (PetscInt i = 0; i < A->cmap->n; ++i) { 2901*9bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecRead(A, i, &a)); 2902*9bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecWrite(B, i, &b)); 2903*9bb5c669SPierre Jolivet PetscCall(VecISCopy(b, h->is[0], SCATTER_FORWARD, a)); 2904*9bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecWrite(B, i, &b)); 2905*9bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecRead(A, i, &a)); 2906*9bb5c669SPierre Jolivet } 2907*9bb5c669SPierre Jolivet PetscCall(MatDestroy(&A)); 2908*9bb5c669SPierre Jolivet PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, h->ksp->pc->mat->rmap->n, B->cmap->n, nullptr, &A)); 2909*9bb5c669SPierre Jolivet PetscCall(KSPMatSolve(h->ksp, B, A)); 2910*9bb5c669SPierre Jolivet PetscCall(MatDestroy(&B)); 2911*9bb5c669SPierre Jolivet for (PetscInt i = 0; i < A->cmap->n; ++i) { 2912*9bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecRead(A, i, &a)); 2913*9bb5c669SPierre Jolivet PetscCall(MatDenseGetColumnVecWrite(Y, i, &b)); 2914*9bb5c669SPierre Jolivet PetscCall(VecISCopy(a, h->is[1], SCATTER_REVERSE, b)); 2915*9bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecWrite(Y, i, &b)); 2916*9bb5c669SPierre Jolivet PetscCall(MatDenseRestoreColumnVecRead(A, i, &a)); 2917*9bb5c669SPierre Jolivet } 2918*9bb5c669SPierre Jolivet PetscCall(MatDestroy(&A)); 2919*9bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 2920*9bb5c669SPierre Jolivet } 2921*9bb5c669SPierre Jolivet 2922*9bb5c669SPierre Jolivet static PetscErrorCode MatDestroy_Harmonic(Mat A) 2923*9bb5c669SPierre Jolivet { 2924*9bb5c669SPierre Jolivet Harmonic h; 2925*9bb5c669SPierre Jolivet 2926*9bb5c669SPierre Jolivet PetscFunctionBegin; 2927*9bb5c669SPierre Jolivet PetscCall(MatShellGetContext(A, &h)); 2928*9bb5c669SPierre Jolivet for (PetscInt i = 0; i < (h->A[1] ? 5 : 3); ++i) PetscCall(ISDestroy(h->is + i)); 2929*9bb5c669SPierre Jolivet PetscCall(PetscFree(h->is)); 2930*9bb5c669SPierre Jolivet PetscCall(VecDestroy(&h->v)); 2931*9bb5c669SPierre Jolivet for (PetscInt i = 0; i < 2; ++i) PetscCall(MatDestroy(h->A + i)); 2932*9bb5c669SPierre Jolivet PetscCall(PetscFree(h->A)); 2933*9bb5c669SPierre Jolivet PetscCall(KSPDestroy(&h->ksp)); 2934*9bb5c669SPierre Jolivet PetscCall(PetscFree(h)); 2935*9bb5c669SPierre Jolivet PetscFunctionReturn(PETSC_SUCCESS); 2936*9bb5c669SPierre Jolivet } 2937