1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3 4 #include <petsc_ops.h> 5 6 #include <ceed.h> 7 #include <petscdm.h> 8 9 #include <navierstokes.h> 10 11 // @brief Get information about a DM's local vector 12 PetscErrorCode DMGetLocalVectorInfo(DM dm, PetscInt *local_size, PetscInt *global_size, VecType *vec_type) { 13 Vec V_loc; 14 15 PetscFunctionBeginUser; 16 PetscCall(DMGetLocalVector(dm, &V_loc)); 17 if (local_size) PetscCall(VecGetLocalSize(V_loc, local_size)); 18 if (global_size) PetscCall(VecGetSize(V_loc, global_size)); 19 if (vec_type) PetscCall(VecGetType(V_loc, vec_type)); 20 PetscCall(DMRestoreLocalVector(dm, &V_loc)); 21 PetscFunctionReturn(PETSC_SUCCESS); 22 } 23 24 // @brief Get information about a DM's global vector 25 PetscErrorCode DMGetGlobalVectorInfo(DM dm, PetscInt *local_size, PetscInt *global_size, VecType *vec_type) { 26 Vec V; 27 28 PetscFunctionBeginUser; 29 PetscCall(DMGetGlobalVector(dm, &V)); 30 if (local_size) PetscCall(VecGetLocalSize(V, local_size)); 31 if (global_size) PetscCall(VecGetSize(V, global_size)); 32 if (vec_type) PetscCall(VecGetType(V, vec_type)); 33 PetscCall(DMRestoreGlobalVector(dm, &V)); 34 PetscFunctionReturn(PETSC_SUCCESS); 35 } 36 37 /** 38 * @brief Create OperatorApplyContext struct for applying FEM operator in a PETSc context 39 * 40 * All passed in objects are reference copied and may be destroyed if desired (with the exception of `CEED_VECTOR_NONE`). 41 * Resulting context should be destroyed with `OperatorApplyContextDestroy()`. 42 * 43 * @param[in] dm_x `DM` associated with the operator active input. May be `NULL` 44 * @param[in] dm_y `DM` associated with the operator active output. May be `NULL` 45 * @param[in] ceed `Ceed` object 46 * @param[in] op_apply `CeedOperator` representing the local action of the FEM operator 47 * @param[in] x_ceed `CeedVector` for operator active input. May be `CEED_VECTOR_NONE` or `NULL`. If `NULL`, `CeedVector` will be automatically 48 * generated. 49 * @param[in] y_ceed `CeedVector` for operator active output. May be `CEED_VECTOR_NONE` or `NULL`. If `NULL`, `CeedVector` will be automatically 50 * generated. 51 * @param[in] X_loc Local `Vec` for operator active input. If `NULL`, vector will be obtained if needed at ApplyCeedOperator time. 52 * @param[in] Y_loc Local `Vec` for operator active output. If `NULL`, vector will be obtained if needed at ApplyCeedOperator time. 53 * @param[out] ctx Struct containing all data necessary for applying the operator 54 */ 55 PetscErrorCode OperatorApplyContextCreate(DM dm_x, DM dm_y, Ceed ceed, CeedOperator op_apply, CeedVector x_ceed, CeedVector y_ceed, Vec X_loc, 56 Vec Y_loc, OperatorApplyContext *ctx) { 57 CeedSize x_size, y_size; 58 59 PetscFunctionBeginUser; 60 PetscCallCeed(ceed, CeedOperatorGetActiveVectorLengths(op_apply, &x_size, &y_size)); 61 { // Verify sizes 62 PetscInt X_size, Y_size, dm_X_size, dm_Y_size; 63 CeedSize x_ceed_size, y_ceed_size; 64 if (dm_x) PetscCall(DMGetLocalVectorInfo(dm_x, &dm_X_size, NULL, NULL)); 65 if (dm_y) PetscCall(DMGetLocalVectorInfo(dm_y, &dm_Y_size, NULL, NULL)); 66 if (X_loc) { 67 PetscCall(VecGetLocalSize(X_loc, &X_size)); 68 PetscCheck(X_size == x_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, 69 "X_loc (%" PetscInt_FMT ") not correct size for CeedOperator active input size (%" CeedSize_FMT ")", X_size, x_size); 70 if (dm_x) 71 PetscCheck(X_size == dm_X_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, 72 "X_loc size (%" PetscInt_FMT ") does not match dm_x local vector size (%" PetscInt_FMT ")", X_size, dm_X_size); 73 } 74 if (Y_loc) { 75 PetscCall(VecGetLocalSize(Y_loc, &Y_size)); 76 PetscCheck(Y_size == y_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, 77 "Y_loc (%" PetscInt_FMT ") not correct size for CeedOperator active output size (%" CeedSize_FMT ")", Y_size, y_size); 78 if (dm_y) 79 PetscCheck(Y_size == dm_Y_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, 80 "Y_loc size (%" PetscInt_FMT ") does not match dm_y local vector size (%" PetscInt_FMT ")", Y_size, dm_Y_size); 81 } 82 if (x_ceed && x_ceed != CEED_VECTOR_NONE) { 83 PetscCallCeed(ceed, CeedVectorGetLength(x_ceed, &x_ceed_size)); 84 PetscCheck(x_size >= 0 ? x_ceed_size == x_size : true, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, 85 "x_ceed (%" CeedSize_FMT ") not correct size for CeedOperator active input size (%" CeedSize_FMT ")", x_ceed_size, x_size); 86 if (dm_x) 87 PetscCheck(x_ceed_size == dm_X_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, 88 "x_ceed size (%" CeedSize_FMT ") does not match dm_x local vector size (%" PetscInt_FMT ")", x_ceed_size, dm_X_size); 89 } 90 if (y_ceed && y_ceed != CEED_VECTOR_NONE) { 91 PetscCallCeed(ceed, CeedVectorGetLength(y_ceed, &y_ceed_size)); 92 PetscCheck(y_ceed_size == y_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, 93 "y_ceed (%" CeedSize_FMT ") not correct size for CeedOperator active input size (%" CeedSize_FMT ")", y_ceed_size, y_size); 94 if (dm_y) 95 PetscCheck(y_ceed_size == dm_Y_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, 96 "y_ceed size (%" CeedSize_FMT ") does not match dm_y local vector size (%" PetscInt_FMT ")", y_ceed_size, dm_Y_size); 97 } 98 } 99 100 PetscCall(PetscNew(ctx)); 101 102 // Copy PETSc Objects 103 if (dm_x) PetscCall(PetscObjectReference((PetscObject)dm_x)); 104 (*ctx)->dm_x = dm_x; 105 if (dm_y) PetscCall(PetscObjectReference((PetscObject)dm_y)); 106 (*ctx)->dm_y = dm_y; 107 108 if (X_loc) PetscCall(PetscObjectReference((PetscObject)X_loc)); 109 (*ctx)->X_loc = X_loc; 110 if (Y_loc) PetscCall(PetscObjectReference((PetscObject)Y_loc)); 111 (*ctx)->Y_loc = Y_loc; 112 113 // Copy libCEED objects 114 if (x_ceed) PetscCallCeed(ceed, CeedVectorReferenceCopy(x_ceed, &(*ctx)->x_ceed)); 115 else PetscCallCeed(ceed, CeedVectorCreate(ceed, x_size, &(*ctx)->x_ceed)); 116 117 if (y_ceed) PetscCallCeed(ceed, CeedVectorReferenceCopy(y_ceed, &(*ctx)->y_ceed)); 118 else PetscCallCeed(ceed, CeedVectorCreate(ceed, y_size, &(*ctx)->y_ceed)); 119 120 PetscCallCeed(ceed, CeedOperatorReferenceCopy(op_apply, &(*ctx)->op)); 121 PetscCallCeed(ceed, CeedReferenceCopy(ceed, &(*ctx)->ceed)); 122 PetscFunctionReturn(PETSC_SUCCESS); 123 } 124 125 /** 126 * @brief Destroy OperatorApplyContext struct 127 * 128 * @param[in,out] ctx Context to destroy 129 */ 130 PetscErrorCode OperatorApplyContextDestroy(OperatorApplyContext ctx) { 131 PetscFunctionBeginUser; 132 if (!ctx) PetscFunctionReturn(PETSC_SUCCESS); 133 Ceed ceed = ctx->ceed; 134 135 // Destroy PETSc Objects 136 PetscCall(DMDestroy(&ctx->dm_x)); 137 PetscCall(DMDestroy(&ctx->dm_y)); 138 PetscCall(VecDestroy(&ctx->X_loc)); 139 PetscCall(VecDestroy(&ctx->Y_loc)); 140 141 // Destroy libCEED Objects 142 PetscCallCeed(ceed, CeedVectorDestroy(&ctx->x_ceed)); 143 PetscCallCeed(ceed, CeedVectorDestroy(&ctx->y_ceed)); 144 PetscCallCeed(ceed, CeedOperatorDestroy(&ctx->op)); 145 PetscCallCeed(ceed, CeedDestroy(&ctx->ceed)); 146 147 PetscCall(PetscFree(ctx)); 148 PetscFunctionReturn(PETSC_SUCCESS); 149 } 150 151 //@brief Return VecType for the given DM 152 VecType DMReturnVecType(DM dm) { 153 VecType vec_type; 154 PetscInt ierr = DMGetVecType(dm, &vec_type); 155 return ierr ? NULL : vec_type; 156 } 157 158 /** 159 * @brief Create local PETSc Vecs for CeedOperator's active input/outputs 160 * 161 * This is primarily used for when the active input/ouput vector does not correspond to a `DM` object, and thus `DMCreateLocalVector` or 162 * `DMGetLocalVector` are not applicable. 163 * For example, if statitics are being store at quadrature points, a `DM`-created `Vec` will not have the 164 * correct size. 165 * 166 * @param[in] op Operator to make the Vecs for 167 * @param[in] vec_type `VecType` for the new Vecs 168 * @param[in] comm `MPI_Comm` for the new Vecs 169 * @param[out] input Vec for CeedOperator active input 170 * @param[out] output Vec for CeedOperator active output 171 */ 172 PetscErrorCode CeedOperatorCreateLocalVecs(CeedOperator op, VecType vec_type, MPI_Comm comm, Vec *input, Vec *output) { 173 CeedSize input_size, output_size; 174 Ceed ceed; 175 int comm_size; 176 177 PetscFunctionBeginUser; 178 PetscCall(CeedOperatorGetCeed(op, &ceed)); 179 PetscCallMPI(MPI_Comm_size(comm, &comm_size)); 180 PetscCheck(comm_size == 1, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, "MPI_Comm must be of size 1, recieved comm of size %d", comm_size); 181 PetscCallCeed(ceed, CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 182 if (input) { 183 PetscCall(VecCreate(comm, input)); 184 PetscCall(VecSetType(*input, vec_type)); 185 PetscCall(VecSetSizes(*input, input_size, input_size)); 186 } 187 if (output) { 188 PetscCall(VecCreate(comm, output)); 189 PetscCall(VecSetType(*output, vec_type)); 190 PetscCall(VecSetSizes(*output, output_size, output_size)); 191 } 192 PetscFunctionReturn(PETSC_SUCCESS); 193 } 194 195 /** 196 * @brief Apply FEM Operator defined by `OperatorApplyContext` to various input and output vectors 197 * 198 * @param[in] X Input global `Vec`, maybe `NULL` 199 * @param[in] X_loc Input local `Vec`, maybe `NULL` 200 * @param[in] x_ceed Input `CeedVector`, maybe `CEED_VECTOR_NONE` 201 * @param[in,out] y_ceed Output `CeedVector`, maybe `CEED_VECTOR_NONE` 202 * @param[in,out] Y_loc Output local `Vec`, maybe `NULL` 203 * @param[in,out] Y Output global `Vec`, maybe `NULL` 204 * @param[in] ctx Context for the operator apply 205 * @param[in] use_apply_add Whether to use `CeedOperatorApply` or `CeedOperatorApplyAdd` 206 */ 207 PetscErrorCode ApplyCeedOperator_Core(Vec X, Vec X_loc, CeedVector x_ceed, CeedVector y_ceed, Vec Y_loc, Vec Y, OperatorApplyContext ctx, 208 bool use_apply_add) { 209 PetscMemType x_mem_type, y_mem_type; 210 Ceed ceed = ctx->ceed; 211 212 PetscFunctionBeginUser; 213 if (X) PetscCall(DMGlobalToLocal(ctx->dm_x, X, INSERT_VALUES, X_loc)); 214 if (X_loc) PetscCall(VecReadPetscToCeed(X_loc, &x_mem_type, x_ceed)); 215 216 if (Y_loc) PetscCall(VecPetscToCeed(Y_loc, &y_mem_type, y_ceed)); 217 218 PetscCall(PetscLogEventBegin(FLUIDS_CeedOperatorApply, X, Y, 0, 0)); 219 PetscCall(PetscLogGpuTimeBegin()); 220 if (use_apply_add) PetscCallCeed(ceed, CeedOperatorApplyAdd(ctx->op, x_ceed, y_ceed, CEED_REQUEST_IMMEDIATE)); 221 else PetscCallCeed(ceed, CeedOperatorApply(ctx->op, x_ceed, y_ceed, CEED_REQUEST_IMMEDIATE)); 222 PetscCall(PetscLogGpuTimeEnd()); 223 PetscCall(PetscLogEventEnd(FLUIDS_CeedOperatorApply, X, Y, 0, 0)); 224 225 if (X_loc) PetscCall(VecReadCeedToPetsc(ctx->x_ceed, x_mem_type, X_loc)); 226 227 if (Y_loc) PetscCall(VecCeedToPetsc(ctx->y_ceed, y_mem_type, Y_loc)); 228 if (Y) PetscCall(DMLocalToGlobal(ctx->dm_y, Y_loc, ADD_VALUES, Y)); 229 PetscFunctionReturn(PETSC_SUCCESS); 230 }; 231 232 PetscErrorCode ApplyCeedOperatorGlobalToGlobal(Vec X, Vec Y, OperatorApplyContext ctx) { 233 Vec X_loc = ctx->X_loc, Y_loc = ctx->Y_loc; 234 235 PetscFunctionBeginUser; 236 PetscCall(VecZeroEntries(Y)); 237 238 // Get local vectors (if needed) 239 if (!ctx->X_loc) PetscCall(DMGetLocalVector(ctx->dm_x, &X_loc)); 240 if (!ctx->Y_loc) PetscCall(DMGetLocalVector(ctx->dm_y, &Y_loc)); 241 242 PetscCall(ApplyCeedOperator_Core(X, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, Y, ctx, false)); 243 244 // Restore local vector (if needed) 245 if (!ctx->X_loc) PetscCall(DMRestoreLocalVector(ctx->dm_x, &X_loc)); 246 if (!ctx->Y_loc) PetscCall(DMRestoreLocalVector(ctx->dm_y, &Y_loc)); 247 PetscFunctionReturn(PETSC_SUCCESS); 248 } 249 250 PetscErrorCode ApplyCeedOperatorLocalToGlobal(Vec X_loc, Vec Y, OperatorApplyContext ctx) { 251 Vec Y_loc = ctx->Y_loc; 252 253 PetscFunctionBeginUser; 254 PetscCall(VecZeroEntries(Y)); 255 256 // Get local vectors (if needed) 257 if (!ctx->Y_loc) PetscCall(DMGetLocalVector(ctx->dm_y, &Y_loc)); 258 259 PetscCall(ApplyCeedOperator_Core(NULL, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, Y, ctx, false)); 260 261 // Restore local vectors (if needed) 262 if (!ctx->Y_loc) PetscCall(DMRestoreLocalVector(ctx->dm_y, &Y_loc)); 263 PetscFunctionReturn(PETSC_SUCCESS); 264 } 265 266 PetscErrorCode ApplyCeedOperatorGlobalToLocal(Vec X, Vec Y_loc, OperatorApplyContext ctx) { 267 Vec X_loc = ctx->X_loc; 268 269 PetscFunctionBeginUser; 270 // Get local vectors (if needed) 271 if (!ctx->X_loc) PetscCall(DMGetLocalVector(ctx->dm_x, &X_loc)); 272 273 PetscCall(ApplyCeedOperator_Core(X, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, NULL, ctx, false)); 274 275 // Restore local vector (if needed) 276 if (!ctx->X_loc) PetscCall(DMRestoreLocalVector(ctx->dm_x, &X_loc)); 277 PetscFunctionReturn(PETSC_SUCCESS); 278 } 279 280 PetscErrorCode ApplyCeedOperatorLocalToLocal(Vec X_loc, Vec Y_loc, OperatorApplyContext ctx) { 281 PetscFunctionBeginUser; 282 PetscCall(ApplyCeedOperator_Core(NULL, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, NULL, ctx, false)); 283 PetscFunctionReturn(PETSC_SUCCESS); 284 } 285 286 PetscErrorCode ApplyAddCeedOperatorLocalToLocal(Vec X_loc, Vec Y_loc, OperatorApplyContext ctx) { 287 PetscFunctionBeginUser; 288 PetscCall(ApplyCeedOperator_Core(NULL, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, NULL, ctx, true)); 289 PetscFunctionReturn(PETSC_SUCCESS); 290 } 291 292 /** 293 * @brief Return Mats for KSP solve 294 * 295 * Uses command-line flag with `ksp`'s prefix to determine if mat_ceed should be used directly or whether it should be assembled. 296 * If `Amat` is to be assembled, then `Pmat` is equal to `Amat`. 297 * 298 * If `Amat` uses `mat_ceed`, then `Pmat` is either assembled or uses `mat_ceed` based on the preconditioner choice in `ksp`. 299 * 300 * @param[in] ksp `KSP` object for used for solving 301 * @param[in] mat_ceed `MATCEED` for the linear operator 302 * @param[in] assemble Whether to assemble `Amat` and `Pmat` if they are not `mat_ceed` 303 * @param[out] Amat `Mat` to be used for the solver `Amat` 304 * @param[out] Pmat `Mat` to be used for the solver `Pmat` 305 */ 306 PetscErrorCode CreateSolveOperatorsFromMatCeed(KSP ksp, Mat mat_ceed, PetscBool assemble, Mat *Amat, Mat *Pmat) { 307 PetscBool use_matceed_pmat, assemble_amat = PETSC_FALSE; 308 309 PetscFunctionBeginUser; 310 { // Determine if Amat should be MATCEED or assembled 311 const char *ksp_prefix = NULL; 312 313 PetscCall(KSPGetOptionsPrefix(ksp, &ksp_prefix)); 314 PetscOptionsBegin(PetscObjectComm((PetscObject)mat_ceed), ksp_prefix, "", NULL); 315 PetscCall(PetscOptionsBool("-matceed_assemble_amat", "Assemble the A matrix for KSP solve", NULL, assemble_amat, &assemble_amat, NULL)); 316 PetscOptionsEnd(); 317 } 318 319 if (assemble_amat) { 320 PetscCall(MatCeedCreateMatCOO(mat_ceed, Amat)); 321 if (assemble) PetscCall(MatCeedAssembleCOO(mat_ceed, *Amat)); 322 323 PetscCall(PetscObjectReference((PetscObject)*Amat)); 324 *Pmat = *Amat; 325 PetscFunctionReturn(PETSC_SUCCESS); 326 } else { 327 PetscCall(PetscObjectReference((PetscObject)mat_ceed)); 328 *Amat = mat_ceed; 329 } 330 331 { // Determine if Pmat should be MATCEED or assembled 332 PC pc; 333 PCType pc_type; 334 335 PetscCall(KSPGetPC(ksp, &pc)); 336 PetscCall(PCGetType(pc, &pc_type)); 337 PetscCall(PetscStrcmpAny(pc_type, &use_matceed_pmat, PCNONE, PCJACOBI, PCVPBJACOBI, PCPBJACOBI, "")); 338 } 339 340 if (use_matceed_pmat) { 341 PetscCall(PetscObjectReference((PetscObject)mat_ceed)); 342 *Pmat = mat_ceed; 343 } else { 344 PetscCall(MatCeedCreateMatCOO(mat_ceed, Pmat)); 345 if (assemble) PetscCall(MatCeedAssembleCOO(mat_ceed, *Pmat)); 346 } 347 PetscFunctionReturn(PETSC_SUCCESS); 348 } 349 350 /** 351 * @brief Runs KSPSetFromOptions and sets Operators based on mat_ceed 352 * 353 * See CreateSolveOperatorsFromMatCeed for details on how the KSPSolve operators are set. 354 * 355 * @param[in] ksp `KSP` of the solve 356 * @param[in] mat_ceed `MatCeed` linear operator to solve for 357 */ 358 PetscErrorCode KSPSetFromOptions_WithMatCeed(KSP ksp, Mat mat_ceed) { 359 Mat Amat, Pmat; 360 361 PetscFunctionBeginUser; 362 PetscCall(KSPSetFromOptions(ksp)); 363 PetscCall(CreateSolveOperatorsFromMatCeed(ksp, mat_ceed, PETSC_TRUE, &Amat, &Pmat)); 364 PetscCall(KSPSetOperators(ksp, Amat, Pmat)); 365 PetscCall(MatDestroy(&Amat)); 366 PetscCall(MatDestroy(&Pmat)); 367 PetscFunctionReturn(PETSC_SUCCESS); 368 } 369 // ----------------------------------------------------------------------------- 370