xref: /honee/src/petsc_ops.c (revision 919335501e27975b45841c41b857fe4fd673e2e8)
1f7325489SJames Wright // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors.
2f7325489SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3f7325489SJames Wright //
4f7325489SJames Wright // SPDX-License-Identifier: BSD-2-Clause
5f7325489SJames Wright //
6f7325489SJames Wright // This file is part of CEED:  http://github.com/ceed
7f7325489SJames Wright 
8f7325489SJames Wright #include "../include/petsc_ops.h"
9f7325489SJames Wright 
10f7325489SJames Wright #include <ceed.h>
11f7325489SJames Wright #include <petscdm.h>
12f7325489SJames Wright 
13f7325489SJames Wright #include "../navierstokes.h"
14f7325489SJames Wright 
1529afcdd1SJames Wright // @brief Get information about a DM's local vector
1629afcdd1SJames Wright PetscErrorCode DMGetLocalVectorInfo(DM dm, PetscInt *local_size, PetscInt *global_size, VecType *vec_type) {
1729afcdd1SJames Wright   Vec V_loc;
1829afcdd1SJames Wright 
1929afcdd1SJames Wright   PetscFunctionBeginUser;
2029afcdd1SJames Wright   PetscCall(DMGetLocalVector(dm, &V_loc));
2129afcdd1SJames Wright   if (local_size) PetscCall(VecGetLocalSize(V_loc, local_size));
2229afcdd1SJames Wright   if (global_size) PetscCall(VecGetSize(V_loc, global_size));
2329afcdd1SJames Wright   if (vec_type) PetscCall(VecGetType(V_loc, vec_type));
2429afcdd1SJames Wright   PetscCall(DMRestoreLocalVector(dm, &V_loc));
2529afcdd1SJames Wright   PetscFunctionReturn(0);
2629afcdd1SJames Wright }
2729afcdd1SJames Wright 
2829afcdd1SJames Wright // @brief Get information about a DM's global vector
2929afcdd1SJames Wright PetscErrorCode DMGetGlobalVectorInfo(DM dm, PetscInt *local_size, PetscInt *global_size, VecType *vec_type) {
3029afcdd1SJames Wright   Vec V;
3129afcdd1SJames Wright 
3229afcdd1SJames Wright   PetscFunctionBeginUser;
3329afcdd1SJames Wright   PetscCall(DMGetGlobalVector(dm, &V));
3429afcdd1SJames Wright   if (local_size) PetscCall(VecGetLocalSize(V, local_size));
3529afcdd1SJames Wright   if (global_size) PetscCall(VecGetSize(V, global_size));
3629afcdd1SJames Wright   if (vec_type) PetscCall(VecGetType(V, vec_type));
3729afcdd1SJames Wright   PetscCall(DMRestoreGlobalVector(dm, &V));
3829afcdd1SJames Wright   PetscFunctionReturn(0);
3929afcdd1SJames Wright }
4029afcdd1SJames Wright 
41fbcfc4fcSJames Wright /**
42fbcfc4fcSJames Wright  * @brief Create OperatorApplyContext struct for applying FEM operator in a PETSc context
43fbcfc4fcSJames Wright  *
44fbcfc4fcSJames Wright  * All passed in objects are reference copied and may be destroyed if desired (with the exception of `CEED_VECTOR_NONE`).
45fbcfc4fcSJames Wright  * Resulting context should be destroyed with `OperatorApplyContextDestroy()`.
46fbcfc4fcSJames Wright  *
47fbcfc4fcSJames Wright  * @param[in]  dm_x     `DM` associated with the operator active input. May be `NULL`
48fbcfc4fcSJames Wright  * @param[in]  dm_y     `DM` associated with the operator active output. May be `NULL`
49fbcfc4fcSJames Wright  * @param[in]  ceed     `Ceed` object
50fbcfc4fcSJames Wright  * @param[in]  op_apply `CeedOperator` representing the local action of the FEM operator
51fbcfc4fcSJames Wright  * @param[in]  x_ceed   `CeedVector` for operator active input. May be `CEED_VECTOR_NONE` or `NULL`. If `NULL`, `CeedVector` will be automatically
52fbcfc4fcSJames Wright  *                      generated.
53fbcfc4fcSJames Wright  * @param[in]  y_ceed   `CeedVector` for operator active output. May be `CEED_VECTOR_NONE` or `NULL`. If `NULL`, `CeedVector` will be automatically
54fbcfc4fcSJames Wright  *                      generated.
55fbcfc4fcSJames Wright  * @param[in]  X_loc    Local `Vec` for operator active input. If `NULL`, vector will be obtained if needed at ApplyCeedOperator time.
56fbcfc4fcSJames Wright  * @param[in]  Y_loc    Local `Vec` for operator active output. If `NULL`, vector will be obtained if needed at ApplyCeedOperator time.
57fbcfc4fcSJames Wright  * @param[out] ctx      Struct containing all data necessary for applying the operator
58fbcfc4fcSJames Wright  */
59f7325489SJames Wright PetscErrorCode OperatorApplyContextCreate(DM dm_x, DM dm_y, Ceed ceed, CeedOperator op_apply, CeedVector x_ceed, CeedVector y_ceed, Vec X_loc,
60fbcfc4fcSJames Wright                                           Vec Y_loc, OperatorApplyContext *ctx) {
61f7325489SJames Wright   CeedSize x_size, y_size;
62fbcfc4fcSJames Wright 
63fbcfc4fcSJames Wright   PetscFunctionBeginUser;
64f7325489SJames Wright   CeedOperatorGetActiveVectorLengths(op_apply, &x_size, &y_size);
65fbcfc4fcSJames Wright   {  // Verify sizes
66fbcfc4fcSJames Wright     PetscInt X_size, Y_size;
67f7325489SJames Wright     if (X_loc) {
68f7325489SJames Wright       PetscCall(VecGetLocalSize(X_loc, &X_size));
69f7325489SJames Wright       PetscCheck(X_size == x_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ,
70f7325489SJames Wright                  "X_loc (%" PetscInt_FMT ") not correct size for CeedOperator active input size (%" CeedSize_FMT ")", X_size, x_size);
71f7325489SJames Wright     }
72f7325489SJames Wright     if (Y_loc) {
73f7325489SJames Wright       PetscCall(VecGetLocalSize(Y_loc, &Y_size));
74f7325489SJames Wright       PetscCheck(Y_size == y_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ,
75f7325489SJames Wright                  "Y_loc (%" PetscInt_FMT ") not correct size for CeedOperator active output size (%" CeedSize_FMT ")", Y_size, y_size);
76f7325489SJames Wright     }
77f7325489SJames Wright   }
78f7325489SJames Wright 
79fbcfc4fcSJames Wright   PetscCall(PetscNew(ctx));
80f7325489SJames Wright 
81f7325489SJames Wright   // Copy PETSc Objects
82f7325489SJames Wright   if (dm_x) PetscCall(PetscObjectReference((PetscObject)dm_x));
83fbcfc4fcSJames Wright   (*ctx)->dm_x = dm_x;
84f7325489SJames Wright   if (dm_y) PetscCall(PetscObjectReference((PetscObject)dm_y));
85fbcfc4fcSJames Wright   (*ctx)->dm_y = dm_y;
86f7325489SJames Wright 
87f7325489SJames Wright   if (X_loc) PetscCall(PetscObjectReference((PetscObject)X_loc));
88fbcfc4fcSJames Wright   (*ctx)->X_loc = X_loc;
89f7325489SJames Wright   if (Y_loc) PetscCall(PetscObjectReference((PetscObject)Y_loc));
90fbcfc4fcSJames Wright   (*ctx)->Y_loc = Y_loc;
91f7325489SJames Wright 
92f7325489SJames Wright   // Copy libCEED objects
93fbcfc4fcSJames Wright   if (x_ceed) CeedVectorReferenceCopy(x_ceed, &(*ctx)->x_ceed);
94fbcfc4fcSJames Wright   else CeedVectorCreate(ceed, x_size, &(*ctx)->x_ceed);
95fbcfc4fcSJames Wright 
96fbcfc4fcSJames Wright   if (y_ceed) CeedVectorReferenceCopy(y_ceed, &(*ctx)->y_ceed);
97fbcfc4fcSJames Wright   else CeedVectorCreate(ceed, y_size, &(*ctx)->y_ceed);
98fbcfc4fcSJames Wright 
99fbcfc4fcSJames Wright   CeedOperatorReferenceCopy(op_apply, &(*ctx)->op);
100fbcfc4fcSJames Wright   CeedReferenceCopy(ceed, &(*ctx)->ceed);
101f7325489SJames Wright 
102f7325489SJames Wright   PetscFunctionReturn(0);
103f7325489SJames Wright }
104f7325489SJames Wright 
105fbcfc4fcSJames Wright /**
106fbcfc4fcSJames Wright  * @brief Destroy OperatorApplyContext struct
107fbcfc4fcSJames Wright  *
108fbcfc4fcSJames Wright  * @param[in,out] ctx Context to destroy
109fbcfc4fcSJames Wright  */
110fbcfc4fcSJames Wright PetscErrorCode OperatorApplyContextDestroy(OperatorApplyContext ctx) {
111f7325489SJames Wright   PetscFunctionBeginUser;
112f7325489SJames Wright 
113fbcfc4fcSJames Wright   if (!ctx) PetscFunctionReturn(0);
114f7325489SJames Wright 
115f7325489SJames Wright   // Destroy PETSc Objects
116fbcfc4fcSJames Wright   PetscCall(DMDestroy(&ctx->dm_x));
117fbcfc4fcSJames Wright   PetscCall(DMDestroy(&ctx->dm_y));
118fbcfc4fcSJames Wright   PetscCall(VecDestroy(&ctx->X_loc));
119fbcfc4fcSJames Wright   PetscCall(VecDestroy(&ctx->Y_loc));
120f7325489SJames Wright 
121f7325489SJames Wright   // Destroy libCEED Objects
122fbcfc4fcSJames Wright   CeedVectorDestroy(&ctx->x_ceed);
123fbcfc4fcSJames Wright   CeedVectorDestroy(&ctx->y_ceed);
124fbcfc4fcSJames Wright   CeedOperatorDestroy(&ctx->op);
125fbcfc4fcSJames Wright   CeedDestroy(&ctx->ceed);
126f7325489SJames Wright 
127fbcfc4fcSJames Wright   PetscCall(PetscFree(ctx));
128f7325489SJames Wright 
129f7325489SJames Wright   PetscFunctionReturn(0);
130f7325489SJames Wright }
131f7325489SJames Wright 
132f7325489SJames Wright /**
133f7325489SJames Wright   @brief Transfer array from PETSc Vec to CeedVector
134f7325489SJames Wright 
135f7325489SJames Wright   @param[in]   X_petsc   PETSc Vec
136f7325489SJames Wright   @param[out]  mem_type  PETSc MemType
137f7325489SJames Wright   @param[out]  x_ceed    CeedVector
138f7325489SJames Wright 
139f7325489SJames Wright   @return An error code: 0 - success, otherwise - failure
140f7325489SJames Wright **/
141f7325489SJames Wright PetscErrorCode VecP2C(Vec X_petsc, PetscMemType *mem_type, CeedVector x_ceed) {
142f7325489SJames Wright   PetscScalar *x;
143f7325489SJames Wright   PetscInt     X_size;
144f7325489SJames Wright   CeedSize     x_size;
145f7325489SJames Wright 
146f7325489SJames Wright   PetscFunctionBeginUser;
147f7325489SJames Wright   PetscCall(VecGetLocalSize(X_petsc, &X_size));
148f7325489SJames Wright   CeedVectorGetLength(x_ceed, &x_size);
149f7325489SJames Wright   PetscCheck(X_size == x_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, "X_petsc (%" PetscInt_FMT ") and x_ceed (%" CeedSize_FMT ") must be same size",
150f7325489SJames Wright              X_size, x_size);
151f7325489SJames Wright 
152f7325489SJames Wright   PetscCall(VecGetArrayAndMemType(X_petsc, &x, mem_type));
153f7325489SJames Wright   CeedVectorSetArray(x_ceed, MemTypeP2C(*mem_type), CEED_USE_POINTER, x);
154f7325489SJames Wright 
155f7325489SJames Wright   PetscFunctionReturn(0);
156f7325489SJames Wright }
157f7325489SJames Wright 
158f7325489SJames Wright /**
159f7325489SJames Wright   @brief Transfer array from CeedVector to PETSc Vec
160f7325489SJames Wright 
161f7325489SJames Wright   @param[in]   x_ceed    CeedVector
162f7325489SJames Wright   @param[in]   mem_type  PETSc MemType
163f7325489SJames Wright   @param[out]  X_petsc   PETSc Vec
164f7325489SJames Wright 
165f7325489SJames Wright   @return An error code: 0 - success, otherwise - failure
166f7325489SJames Wright **/
167f7325489SJames Wright PetscErrorCode VecC2P(CeedVector x_ceed, PetscMemType mem_type, Vec X_petsc) {
168f7325489SJames Wright   PetscScalar *x;
169f7325489SJames Wright   PetscInt     X_size;
170f7325489SJames Wright   CeedSize     x_size;
171f7325489SJames Wright 
172f7325489SJames Wright   PetscFunctionBeginUser;
173f7325489SJames Wright   PetscCall(VecGetLocalSize(X_petsc, &X_size));
174f7325489SJames Wright   CeedVectorGetLength(x_ceed, &x_size);
175f7325489SJames Wright   PetscCheck(X_size == x_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, "X_petsc (%" PetscInt_FMT ") and x_ceed (%" CeedSize_FMT ") must be same size",
176f7325489SJames Wright              X_size, x_size);
177f7325489SJames Wright 
178f7325489SJames Wright   CeedVectorTakeArray(x_ceed, MemTypeP2C(mem_type), &x);
179f7325489SJames Wright   PetscCall(VecRestoreArrayAndMemType(X_petsc, &x));
180f7325489SJames Wright 
181f7325489SJames Wright   PetscFunctionReturn(0);
182f7325489SJames Wright }
183f7325489SJames Wright 
184f7325489SJames Wright /**
185f7325489SJames Wright   @brief Transfer read-only array from PETSc Vec to CeedVector
186f7325489SJames Wright 
187f7325489SJames Wright   @param[in]   X_petsc   PETSc Vec
188f7325489SJames Wright   @param[out]  mem_type  PETSc MemType
189f7325489SJames Wright   @param[out]  x_ceed    CeedVector
190f7325489SJames Wright 
191f7325489SJames Wright   @return An error code: 0 - success, otherwise - failure
192f7325489SJames Wright **/
193f7325489SJames Wright PetscErrorCode VecReadP2C(Vec X_petsc, PetscMemType *mem_type, CeedVector x_ceed) {
194f7325489SJames Wright   PetscScalar *x;
195f7325489SJames Wright   PetscInt     X_size;
196f7325489SJames Wright   CeedSize     x_size;
197f7325489SJames Wright 
198f7325489SJames Wright   PetscFunctionBeginUser;
199f7325489SJames Wright   PetscCall(VecGetLocalSize(X_petsc, &X_size));
200f7325489SJames Wright   CeedVectorGetLength(x_ceed, &x_size);
201f7325489SJames Wright   PetscCheck(X_size == x_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, "X_petsc (%" PetscInt_FMT ") and x_ceed (%" CeedSize_FMT ") must be same size",
202f7325489SJames Wright              X_size, x_size);
203f7325489SJames Wright 
204f7325489SJames Wright   PetscCall(VecGetArrayReadAndMemType(X_petsc, (const PetscScalar **)&x, mem_type));
205f7325489SJames Wright   CeedVectorSetArray(x_ceed, MemTypeP2C(*mem_type), CEED_USE_POINTER, x);
206f7325489SJames Wright 
207f7325489SJames Wright   PetscFunctionReturn(0);
208f7325489SJames Wright }
209f7325489SJames Wright 
210f7325489SJames Wright /**
211f7325489SJames Wright   @brief Transfer read-only array from CeedVector to PETSc Vec
212f7325489SJames Wright 
213f7325489SJames Wright   @param[in]   x_ceed    CeedVector
214f7325489SJames Wright   @param[in]   mem_type  PETSc MemType
215f7325489SJames Wright   @param[out]  X_petsc   PETSc Vec
216f7325489SJames Wright 
217f7325489SJames Wright   @return An error code: 0 - success, otherwise - failure
218f7325489SJames Wright **/
219f7325489SJames Wright PetscErrorCode VecReadC2P(CeedVector x_ceed, PetscMemType mem_type, Vec X_petsc) {
220f7325489SJames Wright   PetscScalar *x;
221f7325489SJames Wright   PetscInt     X_size;
222f7325489SJames Wright   CeedSize     x_size;
223f7325489SJames Wright 
224f7325489SJames Wright   PetscFunctionBeginUser;
225f7325489SJames Wright   PetscCall(VecGetLocalSize(X_petsc, &X_size));
226f7325489SJames Wright   CeedVectorGetLength(x_ceed, &x_size);
227f7325489SJames Wright   PetscCheck(X_size == x_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, "X_petsc (%" PetscInt_FMT ") and x_ceed (%" CeedSize_FMT ") must be same size",
228f7325489SJames Wright              X_size, x_size);
229f7325489SJames Wright 
230f7325489SJames Wright   CeedVectorTakeArray(x_ceed, MemTypeP2C(mem_type), &x);
231f7325489SJames Wright   PetscCall(VecRestoreArrayReadAndMemType(X_petsc, (const PetscScalar **)&x));
232f7325489SJames Wright 
233f7325489SJames Wright   PetscFunctionReturn(0);
234f7325489SJames Wright }
235f7325489SJames Wright 
236f7325489SJames Wright /**
237f7325489SJames Wright   @brief Copy PETSc Vec data into CeedVector
238f7325489SJames Wright 
239f7325489SJames Wright   @param[in]   X_petsc PETSc Vec
240f7325489SJames Wright   @param[out]  x_ceed  CeedVector
241f7325489SJames Wright 
242f7325489SJames Wright   @return An error code: 0 - success, otherwise - failure
243f7325489SJames Wright **/
244f7325489SJames Wright PetscErrorCode VecCopyP2C(Vec X_petsc, CeedVector x_ceed) {
245f7325489SJames Wright   PetscScalar *x;
246f7325489SJames Wright   PetscMemType mem_type;
247f7325489SJames Wright   PetscInt     X_size;
248f7325489SJames Wright   CeedSize     x_size;
249f7325489SJames Wright 
250f7325489SJames Wright   PetscFunctionBeginUser;
251f7325489SJames Wright   PetscCall(VecGetLocalSize(X_petsc, &X_size));
252f7325489SJames Wright   CeedVectorGetLength(x_ceed, &x_size);
253f7325489SJames Wright   PetscCheck(X_size == x_size, PETSC_COMM_WORLD, PETSC_ERR_ARG_SIZ, "X_petsc (%" PetscInt_FMT ") and x_ceed (%" CeedSize_FMT ") must be same size",
254f7325489SJames Wright              X_size, x_size);
255f7325489SJames Wright 
256f7325489SJames Wright   PetscCall(VecGetArrayReadAndMemType(X_petsc, (const PetscScalar **)&x, &mem_type));
257f7325489SJames Wright   CeedVectorSetArray(x_ceed, MemTypeP2C(mem_type), CEED_COPY_VALUES, x);
258f7325489SJames Wright   PetscCall(VecRestoreArrayReadAndMemType(X_petsc, (const PetscScalar **)&x));
259f7325489SJames Wright 
260f7325489SJames Wright   PetscFunctionReturn(0);
261f7325489SJames Wright }
262f7325489SJames Wright 
2636cbc221eSJames Wright //@brief Return VecType for the given DM
2646cbc221eSJames Wright VecType DMReturnVecType(DM dm) {
2656cbc221eSJames Wright   VecType vec_type;
2666cbc221eSJames Wright   DMGetVecType(dm, &vec_type);
2676cbc221eSJames Wright   return vec_type;
2686cbc221eSJames Wright }
2696cbc221eSJames Wright 
2706cbc221eSJames Wright /**
2716cbc221eSJames Wright  * @brief Create local PETSc Vecs for CeedOperator's active input/outputs
2726cbc221eSJames Wright  *
2736cbc221eSJames Wright  * This is primarily used for when the active input/ouput vector does not correspond to a `DM` object, and thus `DMCreateLocalVector` or
2746cbc221eSJames Wright  * `DMGetLocalVector` are not applicable.
2756cbc221eSJames Wright  * For example, if statitics are being store at quadrature points, a `DM`-created `Vec` will not have the
2766cbc221eSJames Wright  * correct size.
2776cbc221eSJames Wright  *
2786cbc221eSJames Wright  * @param[in]  dm     DM overwhich the Vecs would be used
2796cbc221eSJames Wright  * @param[in]  op     Operator to make the Vecs for
2806cbc221eSJames Wright  * @param[out] input  Vec for CeedOperator active input
2816cbc221eSJames Wright  * @param[out] output Vec for CeedOperator active output
2826cbc221eSJames Wright  */
2836cbc221eSJames Wright PetscErrorCode CeedOperatorCreateLocalVecs(CeedOperator op, VecType vec_type, MPI_Comm comm, Vec *input, Vec *output) {
2846cbc221eSJames Wright   CeedSize input_size, output_size;
2856cbc221eSJames Wright 
2866cbc221eSJames Wright   PetscFunctionBeginUser;
2876cbc221eSJames Wright   CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size);
2886cbc221eSJames Wright   if (input) {
2896cbc221eSJames Wright     PetscCall(VecCreate(comm, input));
2906cbc221eSJames Wright     PetscCall(VecSetType(*input, vec_type));
2916cbc221eSJames Wright     PetscCall(VecSetSizes(*input, input_size, input_size));
2926cbc221eSJames Wright   }
2936cbc221eSJames Wright   if (output) {
2946cbc221eSJames Wright     PetscCall(VecCreate(comm, output));
2956cbc221eSJames Wright     PetscCall(VecSetType(*output, vec_type));
2966cbc221eSJames Wright     PetscCall(VecSetSizes(*output, output_size, output_size));
2976cbc221eSJames Wright   }
2986cbc221eSJames Wright 
2996cbc221eSJames Wright   PetscFunctionReturn(0);
3006cbc221eSJames Wright }
3016cbc221eSJames Wright 
302f7325489SJames Wright /**
303f7325489SJames Wright  * @brief Apply FEM Operator defined by `OperatorApplyContext` to various input and output vectors
304f7325489SJames Wright  *
305f7325489SJames Wright  * @param X             Input global `Vec`, maybe `NULL`
306f7325489SJames Wright  * @param X_loc         Input local `Vec`, maybe `NULL`
307f7325489SJames Wright  * @param x_ceed        Input `CeedVector`, maybe `CEED_VECTOR_NONE`
308f7325489SJames Wright  * @param y_ceed        Output `CeedVector`, maybe `CEED_VECTOR_NONE`
309f7325489SJames Wright  * @param Y_loc         Output local `Vec`, maybe `NULL`
310f7325489SJames Wright  * @param Y             Output global `Vec`, maybe `NULL`
311f7325489SJames Wright  * @param ctx           Context for the operator apply
312f7325489SJames Wright  * @param use_apply_add Whether to use `CeedOperatorApply` or `CeedOperatorApplyAdd`
313f7325489SJames Wright  */
314f7325489SJames Wright PetscErrorCode ApplyCeedOperator_Core(Vec X, Vec X_loc, CeedVector x_ceed, CeedVector y_ceed, Vec Y_loc, Vec Y, OperatorApplyContext ctx,
315f7325489SJames Wright                                       bool use_apply_add) {
316f7325489SJames Wright   PetscMemType x_mem_type, y_mem_type;
317f7325489SJames Wright 
318f7325489SJames Wright   PetscFunctionBeginUser;
319f7325489SJames Wright   if (X) PetscCall(DMGlobalToLocal(ctx->dm_x, X, INSERT_VALUES, X_loc));
320f7325489SJames Wright   if (X_loc) PetscCall(VecReadP2C(X_loc, &x_mem_type, x_ceed));
321f7325489SJames Wright 
322f7325489SJames Wright   if (Y_loc) PetscCall(VecP2C(Y_loc, &y_mem_type, y_ceed));
323f7325489SJames Wright 
324f7325489SJames Wright   if (use_apply_add) CeedOperatorApplyAdd(ctx->op, x_ceed, y_ceed, CEED_REQUEST_IMMEDIATE);
325f7325489SJames Wright   else CeedOperatorApply(ctx->op, x_ceed, y_ceed, CEED_REQUEST_IMMEDIATE);
326f7325489SJames Wright 
327f7325489SJames Wright   if (X_loc) PetscCall(VecReadC2P(ctx->x_ceed, x_mem_type, X_loc));
328f7325489SJames Wright 
329f7325489SJames Wright   if (Y_loc) PetscCall(VecC2P(ctx->y_ceed, y_mem_type, Y_loc));
330f7325489SJames Wright   if (Y) PetscCall(DMLocalToGlobal(ctx->dm_y, Y_loc, ADD_VALUES, Y));
331f7325489SJames Wright 
332f7325489SJames Wright   PetscFunctionReturn(0);
333f7325489SJames Wright };
334f7325489SJames Wright 
335f7325489SJames Wright PetscErrorCode ApplyCeedOperatorGlobalToGlobal(Vec X, Vec Y, OperatorApplyContext ctx) {
336f7325489SJames Wright   Vec X_loc = ctx->X_loc, Y_loc = ctx->Y_loc;
337f7325489SJames Wright 
338f7325489SJames Wright   PetscFunctionBeginUser;
339f7325489SJames Wright   PetscCall(VecZeroEntries(Y));
340f7325489SJames Wright 
341f7325489SJames Wright   // Get local vectors (if needed)
342f7325489SJames Wright   if (!ctx->X_loc) PetscCall(DMGetLocalVector(ctx->dm_x, &X_loc));
343f7325489SJames Wright   if (!ctx->Y_loc) PetscCall(DMGetLocalVector(ctx->dm_y, &Y_loc));
344f7325489SJames Wright 
345f7325489SJames Wright   PetscCall(ApplyCeedOperator_Core(X, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, Y, ctx, false));
346f7325489SJames Wright 
347f7325489SJames Wright   // Restore local vector (if needed)
348f7325489SJames Wright   if (!ctx->X_loc) PetscCall(DMRestoreLocalVector(ctx->dm_x, &X_loc));
349f7325489SJames Wright   if (!ctx->Y_loc) PetscCall(DMRestoreLocalVector(ctx->dm_y, &Y_loc));
350f7325489SJames Wright 
351f7325489SJames Wright   PetscFunctionReturn(0);
352f7325489SJames Wright }
353f7325489SJames Wright 
354f7325489SJames Wright PetscErrorCode ApplyCeedOperatorLocalToGlobal(Vec X_loc, Vec Y, OperatorApplyContext ctx) {
355f7325489SJames Wright   Vec Y_loc = ctx->Y_loc;
356f7325489SJames Wright 
357f7325489SJames Wright   PetscFunctionBeginUser;
358f7325489SJames Wright   PetscCall(VecZeroEntries(Y));
359f7325489SJames Wright 
360f7325489SJames Wright   // Get local vectors (if needed)
361f7325489SJames Wright   if (!ctx->Y_loc) PetscCall(DMGetLocalVector(ctx->dm_y, &Y_loc));
362f7325489SJames Wright 
363f7325489SJames Wright   PetscCall(ApplyCeedOperator_Core(NULL, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, Y, ctx, false));
364f7325489SJames Wright 
365f7325489SJames Wright   // Restore local vectors (if needed)
366f7325489SJames Wright   if (!ctx->Y_loc) PetscCall(DMRestoreLocalVector(ctx->dm_y, &Y_loc));
367f7325489SJames Wright 
368f7325489SJames Wright   PetscFunctionReturn(0);
369f7325489SJames Wright }
370f7325489SJames Wright 
371f7325489SJames Wright PetscErrorCode ApplyCeedOperatorGlobalToLocal(Vec X, Vec Y_loc, OperatorApplyContext ctx) {
372f7325489SJames Wright   Vec X_loc = ctx->X_loc;
373f7325489SJames Wright 
374f7325489SJames Wright   PetscFunctionBeginUser;
375f7325489SJames Wright   // Get local vectors (if needed)
376f7325489SJames Wright   if (!ctx->X_loc) PetscCall(DMGetLocalVector(ctx->dm_x, &X_loc));
377f7325489SJames Wright 
378f7325489SJames Wright   PetscCall(ApplyCeedOperator_Core(X, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, NULL, ctx, false));
379f7325489SJames Wright 
380f7325489SJames Wright   // Restore local vector (if needed)
381f7325489SJames Wright   if (!ctx->X_loc) PetscCall(DMRestoreLocalVector(ctx->dm_x, &X_loc));
382f7325489SJames Wright 
383f7325489SJames Wright   PetscFunctionReturn(0);
384f7325489SJames Wright }
385f7325489SJames Wright 
3865ce09d65SJames Wright PetscErrorCode ApplyCeedOperatorLocalToLocal(Vec X_loc, Vec Y_loc, OperatorApplyContext ctx) {
3875ce09d65SJames Wright   PetscFunctionBeginUser;
3885ce09d65SJames Wright   PetscCall(ApplyCeedOperator_Core(NULL, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, NULL, ctx, false));
3895ce09d65SJames Wright   PetscFunctionReturn(0);
3905ce09d65SJames Wright }
3915ce09d65SJames Wright 
392f7325489SJames Wright PetscErrorCode ApplyAddCeedOperatorLocalToLocal(Vec X_loc, Vec Y_loc, OperatorApplyContext ctx) {
393f7325489SJames Wright   PetscFunctionBeginUser;
394f7325489SJames Wright   PetscCall(ApplyCeedOperator_Core(NULL, X_loc, ctx->x_ceed, ctx->y_ceed, Y_loc, NULL, ctx, true));
395f7325489SJames Wright   PetscFunctionReturn(0);
396f7325489SJames Wright }
397f7325489SJames Wright 
398f7325489SJames Wright // -----------------------------------------------------------------------------
399f7325489SJames Wright // Wraps the libCEED operator for a MatShell
400f7325489SJames Wright // -----------------------------------------------------------------------------
401f7325489SJames Wright PetscErrorCode MatMult_Ceed(Mat A, Vec X, Vec Y) {
402f7325489SJames Wright   OperatorApplyContext ctx;
403f7325489SJames Wright   PetscFunctionBeginUser;
404f7325489SJames Wright 
405f7325489SJames Wright   PetscCall(MatShellGetContext(A, &ctx));
406f7325489SJames Wright   PetscCall(ApplyCeedOperatorGlobalToGlobal(X, Y, ctx));
407f7325489SJames Wright 
408f7325489SJames Wright   PetscFunctionReturn(0);
409f7325489SJames Wright };
410f7325489SJames Wright 
411f7325489SJames Wright // -----------------------------------------------------------------------------
412f7325489SJames Wright // Returns the computed diagonal of the operator
413f7325489SJames Wright // -----------------------------------------------------------------------------
414f7325489SJames Wright PetscErrorCode MatGetDiag_Ceed(Mat A, Vec D) {
415f7325489SJames Wright   OperatorApplyContext ctx;
416f7325489SJames Wright   Vec                  Y_loc;
417f7325489SJames Wright   PetscMemType         mem_type;
418f7325489SJames Wright   PetscFunctionBeginUser;
419f7325489SJames Wright 
420f7325489SJames Wright   PetscCall(MatShellGetContext(A, &ctx));
421f7325489SJames Wright   if (ctx->Y_loc) Y_loc = ctx->Y_loc;
422f7325489SJames Wright   else PetscCall(DMGetLocalVector(ctx->dm_y, &Y_loc));
423f7325489SJames Wright 
424f7325489SJames Wright   // -- Place PETSc vector in libCEED vector
425f7325489SJames Wright   PetscCall(VecP2C(Y_loc, &mem_type, ctx->y_ceed));
426f7325489SJames Wright 
427f7325489SJames Wright   // -- Compute Diagonal
428f7325489SJames Wright   CeedOperatorLinearAssembleDiagonal(ctx->op, ctx->y_ceed, CEED_REQUEST_IMMEDIATE);
429f7325489SJames Wright 
430f7325489SJames Wright   // -- Local-to-Global
431f7325489SJames Wright   PetscCall(VecC2P(ctx->y_ceed, mem_type, Y_loc));
432f7325489SJames Wright   PetscCall(VecZeroEntries(D));
433f7325489SJames Wright   PetscCall(DMLocalToGlobal(ctx->dm_y, Y_loc, ADD_VALUES, D));
434f7325489SJames Wright 
435f7325489SJames Wright   if (!ctx->Y_loc) PetscCall(DMRestoreLocalVector(ctx->dm_y, &Y_loc));
436f7325489SJames Wright   PetscFunctionReturn(0);
437f7325489SJames Wright };
438f7325489SJames Wright 
439bb1c79bdSJames Wright /**
440bb1c79bdSJames Wright  * @brief Create PETSc MatShell object for the corresponding OperatorApplyContext
441bb1c79bdSJames Wright  *
442bb1c79bdSJames Wright  * @param[in]  ctx Context that does the action of the operator
443bb1c79bdSJames Wright  * @param[out] mat MatShell for the operator
444bb1c79bdSJames Wright  */
445bb1c79bdSJames Wright PetscErrorCode CreateMatShell_Ceed(OperatorApplyContext ctx, Mat *mat) {
446bb1c79bdSJames Wright   MPI_Comm comm_x = PetscObjectComm((PetscObject)(ctx->dm_x));
447bb1c79bdSJames Wright   MPI_Comm comm_y = PetscObjectComm((PetscObject)(ctx->dm_y));
448bb1c79bdSJames Wright   PetscInt X_loc_size, X_size, Y_size, Y_loc_size;
449bb1c79bdSJames Wright   VecType  X_vec_type, Y_vec_type;
450bb1c79bdSJames Wright 
451bb1c79bdSJames Wright   PetscFunctionBeginUser;
452*91933550SJames Wright   PetscCheck(comm_x == comm_y, PETSC_COMM_WORLD, PETSC_ERR_ARG_NOTSAMECOMM, "Input and output DM must have the same comm");
453bb1c79bdSJames Wright 
454bb1c79bdSJames Wright   PetscCall(DMGetGlobalVectorInfo(ctx->dm_x, &X_loc_size, &X_size, &X_vec_type));
455bb1c79bdSJames Wright   PetscCall(DMGetGlobalVectorInfo(ctx->dm_y, &Y_loc_size, &Y_size, &Y_vec_type));
456bb1c79bdSJames Wright 
457bb1c79bdSJames Wright   PetscCall(MatCreateShell(comm_x, Y_loc_size, X_loc_size, Y_size, X_size, ctx, mat));
458bb1c79bdSJames Wright   PetscCall(MatShellSetContextDestroy(*mat, (PetscErrorCode(*)(void *))OperatorApplyContextDestroy));
459bb1c79bdSJames Wright   PetscCall(MatShellSetOperation(*mat, MATOP_MULT, (void (*)(void))MatMult_Ceed));
460bb1c79bdSJames Wright   PetscCall(MatShellSetOperation(*mat, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag_Ceed));
461bb1c79bdSJames Wright 
462bb1c79bdSJames Wright   PetscCheck(X_vec_type == Y_vec_type, PETSC_COMM_WORLD, PETSC_ERR_ARG_NOTSAMETYPE, "Vec_type of ctx->dm_x (%s) and ctx->dm_y (%s) must be the same",
463bb1c79bdSJames Wright              X_vec_type, Y_vec_type);
464bb1c79bdSJames Wright   PetscCall(MatShellSetVecType(*mat, X_vec_type));
465bb1c79bdSJames Wright 
466bb1c79bdSJames Wright   PetscFunctionReturn(0);
467bb1c79bdSJames Wright }
468bb1c79bdSJames Wright 
469f7325489SJames Wright // -----------------------------------------------------------------------------
470