Actual source code: hpddm.cxx

  1: #define HPDDM_MIXED_PRECISION 1
  2: #include <petsc/private/petschpddm.h>

  4: const char *const KSPHPDDMTypes[]          = {KSPGMRES, "bgmres", KSPCG, "bcg", "gcrodr", "bgcrodr", "bfbcg", KSPPREONLY};
  5: const char *const KSPHPDDMPrecisionTypes[] = {"HALF", "SINGLE", "DOUBLE", "QUADRUPLE", "KSPHPDDMPrecisionType", "KSP_HPDDM_PRECISION_", nullptr};
  6: const char *const HPDDMOrthogonalization[] = {"cgs", "mgs"};
  7: const char *const HPDDMQR[]                = {"cholqr", "cgs", "mgs"};
  8: const char *const HPDDMVariant[]           = {"left", "right", "flexible"};
  9: const char *const HPDDMRecycleTarget[]     = {"SM", "LM", "SR", "LR", "SI", "LI"};
 10: const char *const HPDDMRecycleStrategy[]   = {"A", "B"};

 12: PetscBool  HPDDMCite       = PETSC_FALSE;
 13: const char HPDDMCitation[] = "@article{jolivet2020petsc,\n"
 14:                              "  Author = {Jolivet, Pierre and Roman, Jose E. and Zampini, Stefano},\n"
 15:                              "  Title = {{KSPHPDDM} and {PCHPDDM}: Extending {PETSc} with Robust Overlapping {Schwarz} Preconditioners and Advanced {Krylov} Methods},\n"
 16:                              "  Year = {2021},\n"
 17:                              "  Publisher = {Elsevier},\n"
 18:                              "  Journal = {Computer \\& Mathematics with Applications},\n"
 19:                              "  Volume = {84},\n"
 20:                              "  Pages = {277--295},\n"
 21:                              "  Url = {https://github.com/prj-/jolivet2020petsc}\n"
 22:                              "}\n";

 24: #if PetscDefined(HAVE_SLEPC) && PetscDefined(HAVE_DYNAMIC_LIBRARIES) && PetscDefined(USE_SHARED_LIBRARIES)
 25: static PetscBool loadedDL = PETSC_FALSE;
 26: #endif

 28: static PetscErrorCode KSPSetFromOptions_HPDDM(KSP ksp, PetscOptionItems *PetscOptionsObject)
 29: {
 30:   KSP_HPDDM  *data = (KSP_HPDDM *)ksp->data;
 31:   PetscInt    i, j;
 32:   PetscMPIInt size;

 34:   PetscFunctionBegin;
 35:   PetscOptionsHeadBegin(PetscOptionsObject, "KSPHPDDM options, cf. https://github.com/hpddm/hpddm");
 36:   i = (data->cntl[0] == static_cast<char>(PETSC_DECIDE) ? HPDDM_KRYLOV_METHOD_GMRES : data->cntl[0]);
 37:   PetscCall(PetscOptionsEList("-ksp_hpddm_type", "Type of Krylov method", "KSPHPDDMGetType", KSPHPDDMTypes, PETSC_STATIC_ARRAY_LENGTH(KSPHPDDMTypes), KSPHPDDMTypes[HPDDM_KRYLOV_METHOD_GMRES], &i, nullptr));
 38:   if (i == PETSC_STATIC_ARRAY_LENGTH(KSPHPDDMTypes) - 1) i = HPDDM_KRYLOV_METHOD_NONE; /* need to shift the value since HPDDM_KRYLOV_METHOD_RICHARDSON is not registered in PETSc */
 39:   data->cntl[0] = i;
 40:   PetscCall(PetscOptionsEnum("-ksp_hpddm_precision", "Precision in which Krylov bases are stored", "KSPHPDDM", KSPHPDDMPrecisionTypes, (PetscEnum)data->precision, (PetscEnum *)&data->precision, nullptr));
 41:   PetscCheck(data->precision != KSP_HPDDM_PRECISION_QUADRUPLE || PetscDefined(HAVE_REAL___FLOAT128), PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP_SYS, "Unsupported %s precision", KSPHPDDMPrecisionTypes[data->precision]);
 42:   PetscCheck(std::abs(data->precision - PETSC_KSPHPDDM_DEFAULT_PRECISION) <= 1, PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "Unhandled mixed %s and %s precisions", KSPHPDDMPrecisionTypes[data->precision], KSPHPDDMPrecisionTypes[PETSC_KSPHPDDM_DEFAULT_PRECISION]);
 43:   if (data->cntl[0] != HPDDM_KRYLOV_METHOD_NONE) {
 44:     if (data->cntl[0] != HPDDM_KRYLOV_METHOD_BCG && data->cntl[0] != HPDDM_KRYLOV_METHOD_BFBCG) {
 45:       i = (data->cntl[1] == static_cast<char>(PETSC_DECIDE) ? HPDDM_VARIANT_LEFT : data->cntl[1]);
 46:       if (ksp->pc_side_set == PC_SIDE_DEFAULT)
 47:         PetscCall(PetscOptionsEList("-ksp_hpddm_variant", "Left, right, or variable preconditioning", "KSPHPDDM", HPDDMVariant, PETSC_STATIC_ARRAY_LENGTH(HPDDMVariant), HPDDMVariant[HPDDM_VARIANT_LEFT], &i, nullptr));
 48:       else if (ksp->pc_side_set == PC_RIGHT) i = HPDDM_VARIANT_RIGHT;
 49:       data->cntl[1] = i;
 50:       if (i > 0) PetscCall(KSPSetPCSide(ksp, PC_RIGHT));
 51:     }
 52:     if (data->cntl[0] == HPDDM_KRYLOV_METHOD_BGMRES || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGCRODR || data->cntl[0] == HPDDM_KRYLOV_METHOD_BFBCG) {
 53:       data->rcntl[0] = (PetscAbsReal(data->rcntl[0] - static_cast<PetscReal>(PETSC_DECIDE)) < PETSC_SMALL ? -1.0 : data->rcntl[0]);
 54:       PetscCall(PetscOptionsReal("-ksp_hpddm_deflation_tol", "Tolerance when deflating right-hand sides inside block methods", "KSPHPDDM", data->rcntl[0], data->rcntl, nullptr));
 55:       i = (data->scntl[data->cntl[0] != HPDDM_KRYLOV_METHOD_BFBCG] == static_cast<unsigned short>(PETSC_DECIDE) ? 1 : PetscMax(1, data->scntl[data->cntl[0] != HPDDM_KRYLOV_METHOD_BFBCG]));
 56:       PetscCall(PetscOptionsRangeInt("-ksp_hpddm_enlarge_krylov_subspace", "Split the initial right-hand side into multiple vectors", "KSPHPDDM", i, &i, nullptr, 1, std::numeric_limits<unsigned short>::max() - 1));
 57:       data->scntl[data->cntl[0] != HPDDM_KRYLOV_METHOD_BFBCG] = i;
 58:     } else data->scntl[data->cntl[0] != HPDDM_KRYLOV_METHOD_BCG] = 0;
 59:     if (data->cntl[0] == HPDDM_KRYLOV_METHOD_GMRES || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGMRES || data->cntl[0] == HPDDM_KRYLOV_METHOD_GCRODR || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGCRODR) {
 60:       i = (data->cntl[2] == static_cast<char>(PETSC_DECIDE) ? HPDDM_ORTHOGONALIZATION_CGS : data->cntl[2] & 3);
 61:       PetscCall(PetscOptionsEList("-ksp_hpddm_orthogonalization", "Classical (faster) or Modified (more robust) Gram--Schmidt process", "KSPHPDDM", HPDDMOrthogonalization, PETSC_STATIC_ARRAY_LENGTH(HPDDMOrthogonalization), HPDDMOrthogonalization[HPDDM_ORTHOGONALIZATION_CGS], &i, nullptr));
 62:       j = (data->cntl[2] == static_cast<char>(PETSC_DECIDE) ? HPDDM_QR_CHOLQR : ((data->cntl[2] >> 2) & 7));
 63:       PetscCall(PetscOptionsEList("-ksp_hpddm_qr", "Distributed QR factorizations computed with Cholesky QR, Classical or Modified Gram--Schmidt process", "KSPHPDDM", HPDDMQR, PETSC_STATIC_ARRAY_LENGTH(HPDDMQR), HPDDMQR[HPDDM_QR_CHOLQR], &j, nullptr));
 64:       data->cntl[2] = static_cast<char>(i) + (static_cast<char>(j) << 2);
 65:       i             = (data->scntl[0] == static_cast<unsigned short>(PETSC_DECIDE) ? PetscMin(30, ksp->max_it) : data->scntl[0]);
 66:       PetscCall(PetscOptionsRangeInt("-ksp_gmres_restart", "Maximum number of Arnoldi vectors generated per cycle", "KSPHPDDM", i, &i, nullptr, PetscMin(1, ksp->max_it), PetscMin(ksp->max_it, std::numeric_limits<unsigned short>::max() - 1)));
 67:       data->scntl[0] = i;
 68:     }
 69:     if (data->cntl[0] == HPDDM_KRYLOV_METHOD_BCG || data->cntl[0] == HPDDM_KRYLOV_METHOD_BFBCG) {
 70:       j = (data->cntl[1] == static_cast<char>(PETSC_DECIDE) ? HPDDM_QR_CHOLQR : data->cntl[1]);
 71:       PetscCall(PetscOptionsEList("-ksp_hpddm_qr", "Distributed QR factorizations computed with Cholesky QR, Classical or Modified Gram--Schmidt process", "KSPHPDDM", HPDDMQR, PETSC_STATIC_ARRAY_LENGTH(HPDDMQR), HPDDMQR[HPDDM_QR_CHOLQR], &j, nullptr));
 72:       data->cntl[1] = j;
 73:     }
 74:     if (data->cntl[0] == HPDDM_KRYLOV_METHOD_GCRODR || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGCRODR) {
 75:       i = (data->icntl[0] == static_cast<int>(PETSC_DECIDE) ? PetscMin(20, data->scntl[0] - 1) : data->icntl[0]);
 76:       PetscCall(PetscOptionsRangeInt("-ksp_hpddm_recycle", "Number of harmonic Ritz vectors to compute", "KSPHPDDM", i, &i, nullptr, 1, data->scntl[0] - 1));
 77:       data->icntl[0] = i;
 78:       if (!PetscDefined(HAVE_SLEPC) || !PetscDefined(USE_SHARED_LIBRARIES) || data->cntl[0] == HPDDM_KRYLOV_METHOD_GCRODR) {
 79:         i = (data->cntl[3] == static_cast<char>(PETSC_DECIDE) ? HPDDM_RECYCLE_TARGET_SM : data->cntl[3]);
 80:         PetscCall(PetscOptionsEList("-ksp_hpddm_recycle_target", "Criterion to select harmonic Ritz vectors", "KSPHPDDM", HPDDMRecycleTarget, PETSC_STATIC_ARRAY_LENGTH(HPDDMRecycleTarget), HPDDMRecycleTarget[HPDDM_RECYCLE_TARGET_SM], &i, nullptr));
 81:         data->cntl[3] = i;
 82:       } else {
 83:         PetscCheck(data->precision == PETSC_KSPHPDDM_DEFAULT_PRECISION, PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_INCOMP, "Cannot use SLEPc with a different precision than PETSc for harmonic Ritz eigensolves");
 84:         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)ksp), &size));
 85:         i = (data->cntl[3] == static_cast<char>(PETSC_DECIDE) ? 1 : data->cntl[3]);
 86:         PetscCall(PetscOptionsRangeInt("-ksp_hpddm_recycle_redistribute", "Number of processes used to solve eigenvalue problems when recycling in BGCRODR", "KSPHPDDM", i, &i, nullptr, 1, PetscMin(size, 192)));
 87:         data->cntl[3] = i;
 88:       }
 89:       i = (data->cntl[4] == static_cast<char>(PETSC_DECIDE) ? HPDDM_RECYCLE_STRATEGY_A : data->cntl[4]);
 90:       PetscCall(PetscOptionsEList("-ksp_hpddm_recycle_strategy", "Generalized eigenvalue problem to solve for recycling", "KSPHPDDM", HPDDMRecycleStrategy, PETSC_STATIC_ARRAY_LENGTH(HPDDMRecycleStrategy), HPDDMRecycleStrategy[HPDDM_RECYCLE_STRATEGY_A], &i, nullptr));
 91:       data->cntl[4] = i;
 92:     }
 93:   } else {
 94:     data->cntl[0]  = HPDDM_KRYLOV_METHOD_NONE;
 95:     data->scntl[1] = 1;
 96:   }
 97:   PetscCheck(ksp->nmax >= std::numeric_limits<int>::min() && ksp->nmax <= std::numeric_limits<int>::max(), PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_OUTOFRANGE, "KSPMatSolve() block size %" PetscInt_FMT " not representable by an integer, which is not handled by KSPHPDDM",
 98:              ksp->nmax);
 99:   data->icntl[1] = static_cast<int>(ksp->nmax);
100:   PetscOptionsHeadEnd();
101:   PetscFunctionReturn(PETSC_SUCCESS);
102: }

104: static PetscErrorCode KSPView_HPDDM(KSP ksp, PetscViewer viewer)
105: {
106:   KSP_HPDDM            *data  = (KSP_HPDDM *)ksp->data;
107:   HPDDM::PETScOperator *op    = data->op;
108:   const PetscScalar    *array = op ? op->storage() : nullptr;
109:   PetscBool             ascii;

111:   PetscFunctionBegin;
112:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &ascii));
113:   if (op && ascii) {
114:     PetscCall(PetscViewerASCIIPrintf(viewer, "HPDDM type: %s%s\n", KSPHPDDMTypes[std::min(static_cast<PetscInt>(data->cntl[0]), static_cast<PetscInt>(PETSC_STATIC_ARRAY_LENGTH(KSPHPDDMTypes) - 1))], data->cntl[1] == HPDDM_VARIANT_FLEXIBLE ? " (with support for variable preconditioning)" : ""));
115:     PetscCall(PetscViewerASCIIPrintf(viewer, "precision: %s\n", KSPHPDDMPrecisionTypes[data->precision]));
116:     if (data->cntl[0] == HPDDM_KRYLOV_METHOD_BGMRES || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGCRODR || data->cntl[0] == HPDDM_KRYLOV_METHOD_BFBCG) {
117:       if (PetscAbsReal(data->rcntl[0] - static_cast<PetscReal>(PETSC_DECIDE)) < PETSC_SMALL) PetscCall(PetscViewerASCIIPrintf(viewer, "no deflation at restarts\n"));
118:       else PetscCall(PetscViewerASCIIPrintf(viewer, "deflation tolerance: %g\n", static_cast<double>(data->rcntl[0])));
119:     }
120:     if (data->cntl[0] == HPDDM_KRYLOV_METHOD_GCRODR || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGCRODR) {
121:       PetscCall(PetscViewerASCIIPrintf(viewer, "deflation subspace attached? %s\n", PetscBools[array ? PETSC_TRUE : PETSC_FALSE]));
122:       if (!PetscDefined(HAVE_SLEPC) || !PetscDefined(USE_SHARED_LIBRARIES) || data->cntl[0] == HPDDM_KRYLOV_METHOD_GCRODR) PetscCall(PetscViewerASCIIPrintf(viewer, "deflation target: %s\n", HPDDMRecycleTarget[static_cast<PetscInt>(data->cntl[3])]));
123:       else PetscCall(PetscViewerASCIIPrintf(viewer, "redistribution size: %d\n", static_cast<PetscMPIInt>(data->cntl[3])));
124:     }
125:     if (data->icntl[1] != static_cast<int>(PETSC_DECIDE)) PetscCall(PetscViewerASCIIPrintf(viewer, "  block size is %d\n", data->icntl[1]));
126:   }
127:   PetscFunctionReturn(PETSC_SUCCESS);
128: }

130: static PetscErrorCode KSPSetUp_HPDDM(KSP ksp)
131: {
132:   KSP_HPDDM *data = (KSP_HPDDM *)ksp->data;
133:   Mat        A;
134:   PetscInt   n, bs;
135:   PetscBool  match;

137:   PetscFunctionBegin;
138:   PetscCall(KSPGetOperators(ksp, &A, nullptr));
139:   PetscCall(MatGetLocalSize(A, &n, nullptr));
140:   PetscCall(MatGetBlockSize(A, &bs));
141:   PetscCall(PetscObjectTypeCompareAny((PetscObject)A, &match, MATSEQKAIJ, MATMPIKAIJ, ""));
142:   if (match) n /= bs;
143:   data->op = new HPDDM::PETScOperator(ksp, n);
144:   if (PetscUnlikely(!ksp->setfromoptionscalled || data->cntl[0] == static_cast<char>(PETSC_DECIDE))) { /* what follows is basically a copy/paste of KSPSetFromOptions_HPDDM, with no call to PetscOptions() */
145:     PetscCall(PetscInfo(ksp, "KSPSetFromOptions() not called or uninitialized internal structure, hardwiring default KSPHPDDM options\n"));
146:     if (data->cntl[0] == static_cast<char>(PETSC_DECIDE)) data->cntl[0] = 0; /* GMRES by default */
147:     if (data->cntl[0] != HPDDM_KRYLOV_METHOD_NONE) {                         /* following options do not matter with PREONLY */
148:       if (data->cntl[0] != HPDDM_KRYLOV_METHOD_BCG && data->cntl[0] != HPDDM_KRYLOV_METHOD_BFBCG) {
149:         data->cntl[1] = HPDDM_VARIANT_LEFT; /* left preconditioning by default */
150:         if (ksp->pc_side_set == PC_RIGHT) data->cntl[1] = HPDDM_VARIANT_RIGHT;
151:         if (data->cntl[1] > 0) PetscCall(KSPSetPCSide(ksp, PC_RIGHT));
152:       }
153:       if (data->cntl[0] == HPDDM_KRYLOV_METHOD_BGMRES || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGCRODR || data->cntl[0] == HPDDM_KRYLOV_METHOD_BFBCG) {
154:         data->rcntl[0]                                          = -1.0; /* no deflation by default */
155:         data->scntl[data->cntl[0] != HPDDM_KRYLOV_METHOD_BFBCG] = 1;    /* Krylov subspace not enlarged by default */
156:       } else data->scntl[data->cntl[0] != HPDDM_KRYLOV_METHOD_BCG] = 0;
157:       if (data->cntl[0] == HPDDM_KRYLOV_METHOD_GMRES || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGMRES || data->cntl[0] == HPDDM_KRYLOV_METHOD_GCRODR || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGCRODR) {
158:         data->cntl[2]  = static_cast<char>(HPDDM_ORTHOGONALIZATION_CGS) + (static_cast<char>(HPDDM_QR_CHOLQR) << 2); /* CGS and CholQR by default */
159:         data->scntl[0] = PetscMin(30, ksp->max_it);                                                                  /* restart parameter of 30 by default */
160:       }
161:       if (data->cntl[0] == HPDDM_KRYLOV_METHOD_BCG || data->cntl[0] == HPDDM_KRYLOV_METHOD_BFBCG) { data->cntl[1] = HPDDM_QR_CHOLQR; /* CholQR by default */ }
162:       if (data->cntl[0] == HPDDM_KRYLOV_METHOD_GCRODR || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGCRODR) {
163:         data->icntl[0] = PetscMin(20, data->scntl[0] - 1); /* recycled subspace of size 20 by default */
164:         if (!PetscDefined(HAVE_SLEPC) || !PetscDefined(USE_SHARED_LIBRARIES) || data->cntl[0] == HPDDM_KRYLOV_METHOD_GCRODR) {
165:           data->cntl[3] = HPDDM_RECYCLE_TARGET_SM; /* default recycling target */
166:         } else {
167:           data->cntl[3] = 1; /* redistribution parameter of 1 by default */
168:         }
169:         data->cntl[4] = HPDDM_RECYCLE_STRATEGY_A; /* default recycling strategy */
170:       }
171:     } else data->scntl[1] = 1;
172:   }
173:   PetscCheck(ksp->nmax >= std::numeric_limits<int>::min() && ksp->nmax <= std::numeric_limits<int>::max(), PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_OUTOFRANGE, "KSPMatSolve() block size %" PetscInt_FMT " not representable by an integer, which is not handled by KSPHPDDM",
174:              ksp->nmax);
175:   data->icntl[1] = static_cast<int>(ksp->nmax);
176:   PetscFunctionReturn(PETSC_SUCCESS);
177: }

179: static inline PetscErrorCode KSPReset_HPDDM_Private(KSP ksp)
180: {
181:   KSP_HPDDM *data = (KSP_HPDDM *)ksp->data;

183:   PetscFunctionBegin;
184:   /* cast PETSC_DECIDE into the appropriate types to avoid compiler warnings */
185:   std::fill_n(data->rcntl, PETSC_STATIC_ARRAY_LENGTH(data->rcntl), static_cast<PetscReal>(PETSC_DECIDE));
186:   std::fill_n(data->icntl, PETSC_STATIC_ARRAY_LENGTH(data->icntl), static_cast<int>(PETSC_DECIDE));
187:   std::fill_n(data->scntl, PETSC_STATIC_ARRAY_LENGTH(data->scntl), static_cast<unsigned short>(PETSC_DECIDE));
188:   std::fill_n(data->cntl, PETSC_STATIC_ARRAY_LENGTH(data->cntl), static_cast<char>(PETSC_DECIDE));
189:   data->precision = PETSC_KSPHPDDM_DEFAULT_PRECISION;
190:   PetscFunctionReturn(PETSC_SUCCESS);
191: }

193: static PetscErrorCode KSPReset_HPDDM(KSP ksp)
194: {
195:   KSP_HPDDM *data = (KSP_HPDDM *)ksp->data;

197:   PetscFunctionBegin;
198:   delete data->op;
199:   data->op = nullptr;
200:   PetscCall(KSPReset_HPDDM_Private(ksp));
201:   PetscFunctionReturn(PETSC_SUCCESS);
202: }

204: static PetscErrorCode KSPDestroy_HPDDM(KSP ksp)
205: {
206:   PetscFunctionBegin;
207:   PetscCall(KSPReset_HPDDM(ksp));
208:   PetscCall(KSPDestroyDefault(ksp));
209:   PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPHPDDMSetDeflationMat_C", nullptr));
210:   PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPHPDDMGetDeflationMat_C", nullptr));
211:   PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPHPDDMSetType_C", nullptr));
212:   PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPHPDDMGetType_C", nullptr));
213:   PetscFunctionReturn(PETSC_SUCCESS);
214: }

216: template <PetscMemType type = PETSC_MEMTYPE_HOST>
217: static inline PetscErrorCode KSPSolve_HPDDM_Private(KSP ksp, const PetscScalar *b, PetscScalar *x, PetscInt n)
218: {
219:   KSP_HPDDM              *data = (KSP_HPDDM *)ksp->data;
220:   KSPConvergedDefaultCtx *ctx  = (KSPConvergedDefaultCtx *)ksp->cnvP;
221:   const PetscInt          N    = data->op->getDof() * n;
222:   PetscBool               flg;
223: #if !PetscDefined(USE_REAL_DOUBLE) || PetscDefined(HAVE_F2CBLASLAPACK___FLOAT128_BINDINGS)
224:   HPDDM::upscaled_type<PetscScalar> *high[2];
225: #endif
226: #if !PetscDefined(USE_REAL_SINGLE) || PetscDefined(HAVE_F2CBLASLAPACK___FP16_BINDINGS)
227:   typedef HPDDM::downscaled_type<PetscReal> PetscDownscaledReal PETSC_ATTRIBUTE_MAY_ALIAS;
228:   #if !PetscDefined(USE_COMPLEX)
229:   PetscDownscaledReal *low[2];
230:   #else
231:   typedef PetscReal PetscAliasedReal   PETSC_ATTRIBUTE_MAY_ALIAS;
232:   HPDDM::downscaled_type<PetscScalar> *low[2];
233:   PetscAliasedReal                    *x_r;
234:   PetscDownscaledReal                 *low_r;
235:   #endif
236: #endif
237: #if PetscDefined(HAVE_CUDA)
238:   Mat     A;
239:   VecType vtype;
240: #endif

242:   PetscFunctionBegin;
243: #if PetscDefined(HAVE_CUDA)
244:   PetscCall(KSPGetOperators(ksp, &A, nullptr));
245:   PetscCall(MatGetVecType(A, &vtype));
246:   std::initializer_list<std::string>                 list = {VECCUDA, VECSEQCUDA, VECMPICUDA};
247:   std::initializer_list<std::string>::const_iterator it   = std::find(list.begin(), list.end(), std::string(vtype));
248:   PetscCheck(type != PETSC_MEMTYPE_HOST || it == list.end(), PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "MatGetVecType() must return a Vec with the same PetscMemType as the right-hand side and solution, PetscMemType(%s) != %s", vtype, PetscMemTypeToString(type));
249: #endif
250:   PetscCall(PCGetDiagonalScale(ksp->pc, &flg));
251:   PetscCheck(!flg, PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "Krylov method %s does not support diagonal scaling", ((PetscObject)ksp)->type_name);
252:   if (n > 1) {
253:     if (ksp->converged == KSPConvergedDefault) {
254:       PetscCheck(!ctx->mininitialrtol, PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "Krylov method %s does not support KSPConvergedDefaultSetUMIRNorm()", ((PetscObject)ksp)->type_name);
255:       if (!ctx->initialrtol) {
256:         PetscCall(PetscInfo(ksp, "Forcing KSPConvergedDefaultSetUIRNorm() since KSPConvergedDefault() cannot handle multiple norms\n"));
257:         ctx->initialrtol = PETSC_TRUE;
258:       }
259:     } else PetscCall(PetscInfo(ksp, "Using a special \"converged\" callback, be careful, it is used in KSPHPDDM to track blocks of residuals\n"));
260:   }
261:   /* initial guess is always nonzero with recycling methods if there is a deflation subspace available */
262:   if ((data->cntl[0] == HPDDM_KRYLOV_METHOD_GCRODR || data->cntl[0] == HPDDM_KRYLOV_METHOD_BGCRODR) && data->op->storage()) ksp->guess_zero = PETSC_FALSE;
263:   ksp->its    = 0;
264:   ksp->reason = KSP_CONVERGED_ITERATING;
265:   if (data->precision > PETSC_KSPHPDDM_DEFAULT_PRECISION) { /* Krylov basis stored in higher precision than PetscScalar */
266: #if !PetscDefined(USE_REAL_DOUBLE) || PetscDefined(HAVE_F2CBLASLAPACK___FLOAT128_BINDINGS)
267:     if (type == PETSC_MEMTYPE_HOST) {
268:       PetscCall(PetscMalloc2(N, high, N, high + 1));
269:       HPDDM::copy_n(b, N, high[0]);
270:       HPDDM::copy_n(x, N, high[1]);
271:       PetscCall(HPDDM::IterativeMethod::solve(*data->op, high[0], high[1], n, PetscObjectComm((PetscObject)ksp)));
272:       HPDDM::copy_n(high[1], N, x);
273:       PetscCall(PetscFree2(high[0], high[1]));
274:     } else {
275:       PetscCheck(PetscDefined(HAVE_CUDA) && PetscDefined(USE_REAL_SINGLE), PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "CUDA in PETSc has no support for precisions other than single or double");
276:   #if PetscDefined(HAVE_CUDA)
277:     #if PetscDefined(HAVE_HPDDM)
278:       PetscCall(KSPSolve_HPDDM_CUDA_Private(data, b, x, n, PetscObjectComm((PetscObject)ksp)));
279:     #else
280:       SETERRQ(PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "No CUDA support with --download-hpddm from SLEPc");
281:     #endif
282:   #endif
283:     }
284: #else
285:     PetscCheck(data->precision != KSP_HPDDM_PRECISION_QUADRUPLE, PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "Reconfigure with --download-f2cblaslapack --with-f2cblaslapack-float128-bindings");
286: #endif
287:   } else if (data->precision < PETSC_KSPHPDDM_DEFAULT_PRECISION) { /* Krylov basis stored in lower precision than PetscScalar */
288: #if !PetscDefined(USE_REAL_SINGLE) || PetscDefined(HAVE_F2CBLASLAPACK___FP16_BINDINGS)
289:     if (type == PETSC_MEMTYPE_HOST) {
290:       PetscCall(PetscMalloc1(N, low));
291:   #if !PetscDefined(USE_COMPLEX)
292:       low[1] = reinterpret_cast<PetscDownscaledReal *>(x);
293:   #else
294:       low[1] = reinterpret_cast<HPDDM::downscaled_type<PetscScalar> *>(x);
295:   #endif
296:       std::copy_n(b, N, low[0]);
297:       for (PetscInt i = 0; i < N; ++i) low[1][i] = x[i];
298:       PetscCall(HPDDM::IterativeMethod::solve(*data->op, low[0], low[1], n, PetscObjectComm((PetscObject)ksp)));
299:   #if !PetscDefined(USE_COMPLEX)
300:       for (PetscInt i = N; i-- > 0;) x[i] = static_cast<PetscScalar>(low[1][i]);
301:   #else
302:       x_r = reinterpret_cast<PetscAliasedReal *>(x), low_r = reinterpret_cast<PetscDownscaledReal *>(x_r);
303:       for (PetscInt i = 2 * N; i-- > 0;) x_r[i] = static_cast<PetscReal>(low_r[i]);
304:   #endif
305:       PetscCall(PetscFree(low[0]));
306:     } else {
307:       PetscCheck(PetscDefined(HAVE_CUDA) && PetscDefined(USE_REAL_DOUBLE), PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "CUDA in PETSc has no support for precisions other than single or double");
308:   #if PetscDefined(HAVE_CUDA)
309:     #if PetscDefined(HAVE_HPDDM)
310:       PetscCall(KSPSolve_HPDDM_CUDA_Private(data, b, x, n, PetscObjectComm((PetscObject)ksp)));
311:     #else
312:       SETERRQ(PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "No CUDA support with --download-hpddm from SLEPc");
313:     #endif
314:   #endif
315:     }
316: #else
317:     PetscCheck(data->precision != KSP_HPDDM_PRECISION_HALF, PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "Reconfigure with --download-f2cblaslapack --with-f2cblaslapack-fp16-bindings");
318: #endif
319:   } else { /* Krylov basis stored in the same precision as PetscScalar */
320:     if (type == PETSC_MEMTYPE_HOST) PetscCall(HPDDM::IterativeMethod::solve(*data->op, b, x, n, PetscObjectComm((PetscObject)ksp)));
321:     else {
322:       PetscCheck(PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE), PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "CUDA in PETSc has no support for precisions other than single or double");
323: #if PetscDefined(HAVE_CUDA)
324:   #if PetscDefined(HAVE_HPDDM)
325:       PetscCall(KSPSolve_HPDDM_CUDA_Private(data, b, x, n, PetscObjectComm((PetscObject)ksp)));
326:   #else
327:       SETERRQ(PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "No CUDA support with --download-hpddm from SLEPc");
328:   #endif
329: #endif
330:     }
331:   }
332:   if (!ksp->reason) { /* KSPConvergedDefault() is still returning 0 (= KSP_CONVERGED_ITERATING) */
333:     if (ksp->its >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
334:     else ksp->reason = KSP_CONVERGED_RTOL; /* early exit by HPDDM, which only happens on breakdowns or convergence */
335:   }
336:   ksp->its = PetscMin(ksp->its, ksp->max_it);
337:   PetscFunctionReturn(PETSC_SUCCESS);
338: }

340: static PetscErrorCode KSPSolve_HPDDM(KSP ksp)
341: {
342:   KSP_HPDDM         *data = (KSP_HPDDM *)ksp->data;
343:   Mat                A, B;
344:   PetscScalar       *x, *bt = nullptr, **ptr;
345:   const PetscScalar *b;
346:   PetscInt           i, j, n;
347:   PetscBool          flg;
348:   PetscMemType       type[2];

350:   PetscFunctionBegin;
351:   PetscCall(PetscCitationsRegister(HPDDMCitation, &HPDDMCite));
352:   PetscCall(KSPGetOperators(ksp, &A, nullptr));
353:   PetscCall(PetscObjectTypeCompareAny((PetscObject)A, &flg, MATSEQKAIJ, MATMPIKAIJ, ""));
354:   PetscCall(VecGetArrayWriteAndMemType(ksp->vec_sol, &x, type));
355:   PetscCall(VecGetArrayReadAndMemType(ksp->vec_rhs, &b, type + 1));
356:   PetscCheck(type[0] == type[1], PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "Right-hand side and solution vectors must have the same PetscMemType, %s != %s", PetscMemTypeToString(type[0]), PetscMemTypeToString(type[1]));
357:   if (!flg) {
358:     if (PetscMemTypeCUDA(type[0])) PetscCall(KSPSolve_HPDDM_Private<PETSC_MEMTYPE_CUDA>(ksp, b, x, 1));
359:     else {
360:       PetscCheck(PetscMemTypeHost(type[0]), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "PetscMemType (%s) is neither PETSC_MEMTYPE_HOST nor PETSC_MEMTYPE_CUDA", PetscMemTypeToString(type[0]));
361:       PetscCall(KSPSolve_HPDDM_Private(ksp, b, x, 1));
362:     }
363:   } else {
364:     PetscCheck(PetscMemTypeHost(type[0]), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "PetscMemType (%s) is not PETSC_MEMTYPE_HOST", PetscMemTypeToString(type[0]));
365:     PetscCall(MatKAIJGetScaledIdentity(A, &flg));
366:     PetscCall(MatKAIJGetAIJ(A, &B));
367:     PetscCall(MatGetBlockSize(A, &n));
368:     PetscCall(MatGetLocalSize(B, &i, nullptr));
369:     j = data->op->getDof();
370:     if (!flg) i *= n; /* S and T are not scaled identities, cannot use block methods */
371:     if (i != j) {     /* switching between block and standard methods */
372:       delete data->op;
373:       data->op = new HPDDM::PETScOperator(ksp, i);
374:     }
375:     if (flg && n > 1) {
376:       PetscCall(PetscMalloc1(i * n, &bt));
377:       /* from row- to column-major to be consistent with HPDDM */
378:       HPDDM::Wrapper<PetscScalar>::omatcopy<'T'>(i, n, b, n, bt, i);
379:       ptr = const_cast<PetscScalar **>(&b);
380:       std::swap(*ptr, bt);
381:       HPDDM::Wrapper<PetscScalar>::imatcopy<'T'>(i, n, x, n, i);
382:     }
383:     PetscCall(KSPSolve_HPDDM_Private(ksp, b, x, flg ? n : 1));
384:     if (flg && n > 1) {
385:       std::swap(*ptr, bt);
386:       PetscCall(PetscFree(bt));
387:       /* from column- to row-major to be consistent with MatKAIJ format */
388:       HPDDM::Wrapper<PetscScalar>::imatcopy<'T'>(n, i, x, i, n);
389:     }
390:   }
391:   PetscCall(VecRestoreArrayReadAndMemType(ksp->vec_rhs, &b));
392:   PetscCall(VecRestoreArrayWriteAndMemType(ksp->vec_sol, &x));
393:   PetscFunctionReturn(PETSC_SUCCESS);
394: }

396: /*@
397:   KSPHPDDMSetDeflationMat - Sets the deflation space used by Krylov methods in `KSPHPDDM` with recycling. This space is viewed as a set of vectors stored in
398:   a `MATDENSE` (column major).

400:   Input Parameters:
401: + ksp - iterative context
402: - U   - deflation space to be used during `KSPSolve()`

404:   Level: intermediate

406: .seealso: [](ch_ksp), `KSPHPDDM`, `KSPCreate()`, `KSPType`, `KSPHPDDMGetDeflationMat()`
407: @*/
408: PetscErrorCode KSPHPDDMSetDeflationMat(KSP ksp, Mat U)
409: {
410:   PetscFunctionBegin;
413:   PetscCheckSameComm(ksp, 1, U, 2);
414:   PetscUseMethod(ksp, "KSPHPDDMSetDeflationMat_C", (KSP, Mat), (ksp, U));
415:   PetscFunctionReturn(PETSC_SUCCESS);
416: }

418: /*@
419:   KSPHPDDMGetDeflationMat - Gets the deflation space computed by Krylov methods in `KSPHPDDM`  with recycling or `NULL` if `KSPSolve()` has not been called yet.

421:   Input Parameter:
422: . ksp - iterative context

424:   Output Parameter:
425: . U - deflation space generated during `KSPSolve()`

427:   Level: intermediate

429:   Note:
430:   This space is viewed as a set of vectors stored in a `MATDENSE` (column major). It is the responsibility of the user to free the returned `Mat`.

432: .seealso: [](ch_ksp), `KSPHPDDM`, `KSPCreate()`, `KSPType`, `KSPHPDDMSetDeflationMat()`
433: @*/
434: PetscErrorCode KSPHPDDMGetDeflationMat(KSP ksp, Mat *U)
435: {
436:   PetscFunctionBegin;
438:   if (U) {
439:     PetscAssertPointer(U, 2);
440:     PetscUseMethod(ksp, "KSPHPDDMGetDeflationMat_C", (KSP, Mat *), (ksp, U));
441:   }
442:   PetscFunctionReturn(PETSC_SUCCESS);
443: }

445: static PetscErrorCode KSPHPDDMSetDeflationMat_HPDDM(KSP ksp, Mat U)
446: {
447:   KSP_HPDDM            *data = (KSP_HPDDM *)ksp->data;
448:   HPDDM::PETScOperator *op   = data->op;
449:   Mat                   A;
450:   const PetscScalar    *array;
451:   PetscScalar          *copy;
452:   PetscInt              m1, M1, m2, M2, n2, N2, ldu;
453:   PetscBool             match;

455:   PetscFunctionBegin;
456:   if (!op) {
457:     PetscCall(KSPSetUp(ksp));
458:     op = data->op;
459:   }
460:   PetscCheck(data->precision == PETSC_KSPHPDDM_DEFAULT_PRECISION, PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "%s != %s", KSPHPDDMPrecisionTypes[data->precision], KSPHPDDMPrecisionTypes[PETSC_KSPHPDDM_DEFAULT_PRECISION]);
461:   PetscCall(KSPGetOperators(ksp, &A, nullptr));
462:   PetscCall(MatGetLocalSize(A, &m1, nullptr));
463:   PetscCall(MatGetLocalSize(U, &m2, &n2));
464:   PetscCall(MatGetSize(A, &M1, nullptr));
465:   PetscCall(MatGetSize(U, &M2, &N2));
466:   PetscCheck(m1 == m2 && M1 == M2, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot use a deflation space with (m2,M2) = (%" PetscInt_FMT ",%" PetscInt_FMT ") for a linear system with (m1,M1) = (%" PetscInt_FMT ",%" PetscInt_FMT ")", m2, M2, m1, M1);
467:   PetscCall(PetscObjectTypeCompareAny((PetscObject)U, &match, MATSEQDENSE, MATMPIDENSE, ""));
468:   PetscCheck(match, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Provided deflation space not stored in a dense Mat");
469:   PetscCall(MatDenseGetArrayRead(U, &array));
470:   copy = op->allocate(m2, 1, N2);
471:   PetscCheck(copy, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Memory allocation error");
472:   PetscCall(MatDenseGetLDA(U, &ldu));
473:   HPDDM::Wrapper<PetscScalar>::omatcopy<'N'>(N2, m2, array, ldu, copy, m2);
474:   PetscCall(MatDenseRestoreArrayRead(U, &array));
475:   PetscFunctionReturn(PETSC_SUCCESS);
476: }

478: static PetscErrorCode KSPHPDDMGetDeflationMat_HPDDM(KSP ksp, Mat *U)
479: {
480:   KSP_HPDDM            *data = (KSP_HPDDM *)ksp->data;
481:   HPDDM::PETScOperator *op   = data->op;
482:   Mat                   A;
483:   const PetscScalar    *array;
484:   PetscScalar          *copy;
485:   PetscInt              m1, M1, N2;

487:   PetscFunctionBegin;
488:   if (!op) {
489:     PetscCall(KSPSetUp(ksp));
490:     op = data->op;
491:   }
492:   PetscCheck(data->precision == PETSC_KSPHPDDM_DEFAULT_PRECISION, PetscObjectComm((PetscObject)ksp), PETSC_ERR_SUP, "%s != %s", KSPHPDDMPrecisionTypes[data->precision], KSPHPDDMPrecisionTypes[PETSC_KSPHPDDM_DEFAULT_PRECISION]);
493:   array = op->storage();
494:   N2    = op->k().first * op->k().second;
495:   if (!array) *U = nullptr;
496:   else {
497:     PetscCall(KSPGetOperators(ksp, &A, nullptr));
498:     PetscCall(MatGetLocalSize(A, &m1, nullptr));
499:     PetscCall(MatGetSize(A, &M1, nullptr));
500:     PetscCall(MatCreateDense(PetscObjectComm((PetscObject)ksp), m1, PETSC_DECIDE, M1, N2, nullptr, U));
501:     PetscCall(MatDenseGetArrayWrite(*U, &copy));
502:     PetscCall(PetscArraycpy(copy, array, m1 * N2));
503:     PetscCall(MatDenseRestoreArrayWrite(*U, &copy));
504:   }
505:   PetscFunctionReturn(PETSC_SUCCESS);
506: }

508: static PetscErrorCode KSPMatSolve_HPDDM(KSP ksp, Mat B, Mat X)
509: {
510:   KSP_HPDDM         *data = (KSP_HPDDM *)ksp->data;
511:   Mat                A;
512:   const PetscScalar *b;
513:   PetscScalar       *x;
514:   PetscInt           n, lda;
515:   PetscMemType       type[2];

517:   PetscFunctionBegin;
518:   PetscCall(PetscCitationsRegister(HPDDMCitation, &HPDDMCite));
519:   if (!data->op) PetscCall(KSPSetUp(ksp));
520:   PetscCall(KSPGetOperators(ksp, &A, nullptr));
521:   PetscCall(MatGetLocalSize(B, &n, nullptr));
522:   PetscCall(MatDenseGetLDA(B, &lda));
523:   PetscCheck(n == lda, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Unhandled leading dimension lda = %" PetscInt_FMT " with n = %" PetscInt_FMT, lda, n);
524:   PetscCall(MatGetLocalSize(A, &n, nullptr));
525:   PetscCall(MatDenseGetLDA(X, &lda));
526:   PetscCheck(n == lda, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Unhandled leading dimension lda = %" PetscInt_FMT " with n = %" PetscInt_FMT, lda, n);
527:   PetscCall(MatGetSize(X, nullptr, &n));
528:   PetscCall(MatDenseGetArrayWriteAndMemType(X, &x, type));
529:   PetscCall(MatDenseGetArrayReadAndMemType(B, &b, type + 1));
530:   PetscCheck(type[0] == type[1], PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "Right-hand side and solution matrices must have the same PetscMemType, %s != %s", PetscMemTypeToString(type[0]), PetscMemTypeToString(type[1]));
531:   if (PetscMemTypeCUDA(type[0])) PetscCall(KSPSolve_HPDDM_Private<PETSC_MEMTYPE_CUDA>(ksp, b, x, n));
532:   else {
533:     PetscCheck(PetscMemTypeHost(type[0]), PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "PetscMemType (%s) is neither PETSC_MEMTYPE_HOST nor PETSC_MEMTYPE_CUDA", PetscMemTypeToString(type[0]));
534:     PetscCall(KSPSolve_HPDDM_Private(ksp, b, x, n));
535:   }
536:   PetscCall(MatDenseRestoreArrayReadAndMemType(B, &b));
537:   PetscCall(MatDenseRestoreArrayWriteAndMemType(X, &x));
538:   PetscFunctionReturn(PETSC_SUCCESS);
539: }

541: /*@
542:   KSPHPDDMSetType - Sets the type of Krylov method used in `KSPHPDDM`.

544:   Collective

546:   Input Parameters:
547: + ksp  - iterative context
548: - type - any of gmres, bgmres, cg, bcg, gcrodr, bgcrodr, bfbcg, or preonly

550:   Level: intermediate

552:   Notes:
553:   Unlike `KSPReset()`, this function does not destroy any deflation space attached to the `KSP`.

555:   As an example, in the following sequence\:
556: .vb
557:      KSPHPDDMSetType(ksp, KSPGCRODR);
558:      KSPSolve(ksp, b, x);
559:      KSPHPDDMSetType(ksp, KSPGMRES);
560:      KSPHPDDMSetType(ksp, KSPGCRODR);
561:      KSPSolve(ksp, b, x);
562: .ve
563:   the recycled space is reused in the second `KSPSolve()`.

565: .seealso: [](ch_ksp), `KSPCreate()`, `KSPType`, `KSPHPDDMType`, `KSPHPDDMGetType()`
566: @*/
567: PetscErrorCode KSPHPDDMSetType(KSP ksp, KSPHPDDMType type)
568: {
569:   PetscFunctionBegin;
572:   PetscUseMethod(ksp, "KSPHPDDMSetType_C", (KSP, KSPHPDDMType), (ksp, type));
573:   PetscFunctionReturn(PETSC_SUCCESS);
574: }

576: /*@
577:   KSPHPDDMGetType - Gets the type of Krylov method used in `KSPHPDDM`.

579:   Input Parameter:
580: . ksp - iterative context

582:   Output Parameter:
583: . type - any of gmres, bgmres, cg, bcg, gcrodr, bgcrodr, bfbcg, or preonly

585:   Level: intermediate

587: .seealso: [](ch_ksp), `KSPCreate()`, `KSPType`, `KSPHPDDMType`, `KSPHPDDMSetType()`
588: @*/
589: PetscErrorCode KSPHPDDMGetType(KSP ksp, KSPHPDDMType *type)
590: {
591:   PetscFunctionBegin;
593:   if (type) {
594:     PetscAssertPointer(type, 2);
595:     PetscUseMethod(ksp, "KSPHPDDMGetType_C", (KSP, KSPHPDDMType *), (ksp, type));
596:   }
597:   PetscFunctionReturn(PETSC_SUCCESS);
598: }

600: static PetscErrorCode KSPHPDDMSetType_HPDDM(KSP ksp, KSPHPDDMType type)
601: {
602:   KSP_HPDDM *data = (KSP_HPDDM *)ksp->data;
603:   PetscInt   i;
604:   PetscBool  flg = PETSC_FALSE;

606:   PetscFunctionBegin;
607:   for (i = 0; i < static_cast<PetscInt>(PETSC_STATIC_ARRAY_LENGTH(KSPHPDDMTypes)); ++i) {
608:     PetscCall(PetscStrcmp(KSPHPDDMTypes[type], KSPHPDDMTypes[i], &flg));
609:     if (flg) break;
610:   }
611:   PetscCheck(i != PETSC_STATIC_ARRAY_LENGTH(KSPHPDDMTypes), PetscObjectComm((PetscObject)ksp), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown KSPHPDDMType %d", type);
612:   if (data->cntl[0] != static_cast<char>(PETSC_DECIDE) && data->cntl[0] != i) PetscCall(KSPReset_HPDDM_Private(ksp));
613:   data->cntl[0] = i;
614:   PetscFunctionReturn(PETSC_SUCCESS);
615: }

617: static PetscErrorCode KSPHPDDMGetType_HPDDM(KSP ksp, KSPHPDDMType *type)
618: {
619:   KSP_HPDDM *data = (KSP_HPDDM *)ksp->data;

621:   PetscFunctionBegin;
622:   PetscCheck(data->cntl[0] != static_cast<char>(PETSC_DECIDE), PETSC_COMM_SELF, PETSC_ERR_ORDER, "KSPHPDDMType not set yet");
623:   /* need to shift by -1 for HPDDM_KRYLOV_METHOD_NONE */
624:   *type = static_cast<KSPHPDDMType>(PetscMin(data->cntl[0], static_cast<char>(PETSC_STATIC_ARRAY_LENGTH(KSPHPDDMTypes) - 1)));
625:   PetscFunctionReturn(PETSC_SUCCESS);
626: }

628: /*MC
629:    KSPHPDDM - Interface with the HPDDM library. This `KSP` may be used to further select methods that are currently not implemented natively in PETSc, e.g.,
630:    GCRODR {cite}`parks2006recycling`, a recycled Krylov method which is similar to `KSPLGMRES`, see {cite}`jolivet2016block` for a comparison.
631:    ex75.c shows how to reproduce the results
632:    from the aforementioned paper {cite}`parks2006recycling`. A chronological bibliography of relevant publications linked with `KSP` available in HPDDM through `KSPHPDDM`,
633:    and not available directly in PETSc, may be found below. The interface is explained in details in {cite}`jolivetromanzampini2020`.
634:    See also {cite}`o1980block`, {cite}`ji2017breakdown` and {cite}`calandra2013modified`

636:    Options Database Keys:
637: +   -ksp_gmres_restart <restart, default=30>                  - see `KSPGMRES`
638: .   -ksp_hpddm_type <type, default=gmres>                     - any of gmres, bgmres, cg, bcg, gcrodr, bgcrodr, bfbcg, or preonly, see `KSPHPDDMType`
639: .   -ksp_hpddm_precision <value, default=same as PetscScalar> - any of half, single, double or quadruple, see `KSPHPDDMPrecision`
640: .   -ksp_hpddm_deflation_tol <eps, default=\-1.0>             - tolerance when deflating right-hand sides inside block methods (no deflation by default,
641:                                                               only relevant with block methods)
642: .   -ksp_hpddm_enlarge_krylov_subspace <p, default=1>         - split the initial right-hand side into multiple vectors (only relevant with nonblock methods)
643: .   -ksp_hpddm_orthogonalization <type, default=cgs>          - any of cgs or mgs, see KSPGMRES
644: .   -ksp_hpddm_qr <type, default=cholqr>                      - distributed QR factorizations with any of cholqr, cgs, or mgs (only relevant with block methods)
645: .   -ksp_hpddm_variant <type, default=left>                   - any of left, right, or flexible (this option is superseded by `KSPSetPCSide()`)
646: .   -ksp_hpddm_recycle <n, default=0>                         - number of harmonic Ritz vectors to compute (only relevant with GCRODR or BGCRODR)
647: .   -ksp_hpddm_recycle_target <type, default=SM>              - criterion to select harmonic Ritz vectors using either SM, LM, SR, LR, SI, or LI
648:                                                               (only relevant with GCRODR or BGCRODR). For BGCRODR, if PETSc is compiled with SLEPc,
649:                                                               this option is not relevant, since SLEPc is used instead. Options are set with the prefix -ksp_hpddm_recycle_eps_
650: .   -ksp_hpddm_recycle_strategy <type, default=A>             - generalized eigenvalue problem A or B to solve for recycling (only relevant with flexible GCRODR or BGCRODR)
651: -   -ksp_hpddm_recycle_symmetric <true, default=false>        - symmetric generalized eigenproblems in BGCRODR, useful to switch to distributed solvers like
652:                                                               `EPSELEMENTAL` or `EPSSCALAPACK` (only relevant when PETSc is compiled with SLEPc)

654:    Level: intermediate

656: .seealso: [](ch_ksp), [](sec_flexibleksp), `KSPCreate()`, `KSPSetType()`, `KSPType`, `KSP`, `KSPGMRES`, `KSPCG`, `KSPLGMRES`, `KSPDGMRES`
657: M*/

659: PETSC_EXTERN PetscErrorCode KSPCreate_HPDDM(KSP ksp)
660: {
661:   KSP_HPDDM  *data;
662:   PetscInt    i;
663:   const char *common[] = {KSPGMRES, KSPCG, KSPPREONLY};
664:   PetscBool   flg      = PETSC_FALSE;

666:   PetscFunctionBegin;
667:   PetscCall(PetscNew(&data));
668:   ksp->data = (void *)data;
669:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_PRECONDITIONED, PC_LEFT, 2));
670:   PetscCall(KSPSetSupportedNorm(ksp, KSP_NORM_UNPRECONDITIONED, PC_RIGHT, 1));
671:   ksp->ops->solve          = KSPSolve_HPDDM;
672:   ksp->ops->matsolve       = KSPMatSolve_HPDDM;
673:   ksp->ops->setup          = KSPSetUp_HPDDM;
674:   ksp->ops->setfromoptions = KSPSetFromOptions_HPDDM;
675:   ksp->ops->destroy        = KSPDestroy_HPDDM;
676:   ksp->ops->view           = KSPView_HPDDM;
677:   ksp->ops->reset          = KSPReset_HPDDM;
678:   PetscCall(KSPReset_HPDDM_Private(ksp));
679:   for (i = 0; i < static_cast<PetscInt>(PETSC_STATIC_ARRAY_LENGTH(common)); ++i) {
680:     PetscCall(PetscStrcmp(((PetscObject)ksp)->type_name, common[i], &flg));
681:     if (flg) break;
682:   }
683:   if (!i) data->cntl[0] = HPDDM_KRYLOV_METHOD_GMRES;
684:   else if (i == 1) data->cntl[0] = HPDDM_KRYLOV_METHOD_CG;
685:   else if (i == 2) data->cntl[0] = HPDDM_KRYLOV_METHOD_NONE;
686:   if (data->cntl[0] != static_cast<char>(PETSC_DECIDE)) PetscCall(PetscInfo(ksp, "Using the previously set KSPType %s\n", common[i]));
687:   PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPHPDDMSetDeflationMat_C", KSPHPDDMSetDeflationMat_HPDDM));
688:   PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPHPDDMGetDeflationMat_C", KSPHPDDMGetDeflationMat_HPDDM));
689:   PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPHPDDMSetType_C", KSPHPDDMSetType_HPDDM));
690:   PetscCall(PetscObjectComposeFunction((PetscObject)ksp, "KSPHPDDMGetType_C", KSPHPDDMGetType_HPDDM));
691: #if PetscDefined(HAVE_SLEPC) && PetscDefined(HAVE_DYNAMIC_LIBRARIES) && PetscDefined(USE_SHARED_LIBRARIES)
692:   if (!loadedDL) PetscCall(HPDDMLoadDL_Private(&loadedDL));
693: #endif
694:   data->precision = PETSC_KSPHPDDM_DEFAULT_PRECISION;
695:   PetscFunctionReturn(PETSC_SUCCESS);
696: }