xref: /libCEED/examples/petsc/bpsraw.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3cb32e2e7SValeria Barra //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5cb32e2e7SValeria Barra //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7cb32e2e7SValeria Barra 
8cb32e2e7SValeria Barra //                        libCEED + PETSc Example: CEED BPs
9cb32e2e7SValeria Barra //
10cb32e2e7SValeria Barra // This example demonstrates a simple usage of libCEED with PETSc to solve the
11cb32e2e7SValeria Barra // CEED BP benchmark problems, see http://ceed.exascaleproject.org/bps.
12cb32e2e7SValeria Barra //
13cb32e2e7SValeria Barra // The code is intentionally "raw", using only low-level communication
14cb32e2e7SValeria Barra // primitives.
15cb32e2e7SValeria Barra //
16cb32e2e7SValeria Barra // Build with:
17cb32e2e7SValeria Barra //
18cb32e2e7SValeria Barra //     make bpsraw [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>]
19cb32e2e7SValeria Barra //
20cb32e2e7SValeria Barra // Sample runs:
21cb32e2e7SValeria Barra //
2232d2ee49SValeria Barra //     ./bpsraw -problem bp1
2328688798Sjeremylt //     ./bpsraw -problem bp2
2428688798Sjeremylt //     ./bpsraw -problem bp3
2528688798Sjeremylt //     ./bpsraw -problem bp4
2628688798Sjeremylt //     ./bpsraw -problem bp5 -ceed /cpu/self
2728688798Sjeremylt //     ./bpsraw -problem bp6 -ceed /gpu/cuda
28cb32e2e7SValeria Barra //
299b072555Sjeremylt //TESTARGS -ceed {ceed_resource} -test -problem bp2 -degree 5 -q_extra 1 -ksp_max_it_clip 15,15
30cb32e2e7SValeria Barra 
31cb32e2e7SValeria Barra /// @file
32cb32e2e7SValeria Barra /// CEED BPs example using PETSc
33cb32e2e7SValeria Barra /// See bps.c for an implementation using DMPlex unstructured grids.
34cb32e2e7SValeria Barra const char help[] = "Solve CEED BPs using PETSc\n";
35cb32e2e7SValeria Barra 
363d576824SJeremy L Thompson #include <ceed.h>
373d576824SJeremy L Thompson #include <petscksp.h>
383d576824SJeremy L Thompson #include <petscsys.h>
39cb32e2e7SValeria Barra #include <stdbool.h>
40cb32e2e7SValeria Barra #include <string.h>
41*2b730f8bSJeremy L Thompson 
42cb32e2e7SValeria Barra #include "qfunctions/bps/bp1.h"
43cb32e2e7SValeria Barra #include "qfunctions/bps/bp2.h"
44cb32e2e7SValeria Barra #include "qfunctions/bps/bp3.h"
45cb32e2e7SValeria Barra #include "qfunctions/bps/bp4.h"
463d576824SJeremy L Thompson #include "qfunctions/bps/common.h"
47cb32e2e7SValeria Barra 
489396343dSjeremylt #if PETSC_VERSION_LT(3, 12, 0)
499396343dSjeremylt #ifdef PETSC_HAVE_CUDA
509396343dSjeremylt #include <petsccuda.h>
519396343dSjeremylt // Note: With PETSc prior to version 3.12.0, providing the source path to
529396343dSjeremylt //       include 'cublas_v2.h' will be needed to use 'petsccuda.h'.
539396343dSjeremylt #endif
549396343dSjeremylt #endif
559396343dSjeremylt 
56*2b730f8bSJeremy L Thompson static CeedMemType MemTypeP2C(PetscMemType mem_type) { return PetscMemTypeDevice(mem_type) ? CEED_MEM_DEVICE : CEED_MEM_HOST; }
57b68a8d79SJed Brown 
58cb32e2e7SValeria Barra static void Split3(PetscInt size, PetscInt m[3], bool reverse) {
599b072555Sjeremylt   for (PetscInt d = 0, size_left = size; d < 3; d++) {
609b072555Sjeremylt     PetscInt try = (PetscInt)PetscCeilReal(PetscPowReal(size_left, 1. / (3 - d)));
619b072555Sjeremylt     while (try * (size_left / try) != size_left) try++;
62cb32e2e7SValeria Barra     m[reverse ? 2 - d : d] = try;
639b072555Sjeremylt     size_left /= try;
64cb32e2e7SValeria Barra   }
65cb32e2e7SValeria Barra }
66cb32e2e7SValeria Barra 
67*2b730f8bSJeremy L Thompson static PetscInt Max3(const PetscInt a[3]) { return PetscMax(a[0], PetscMax(a[1], a[2])); }
68*2b730f8bSJeremy L Thompson static PetscInt Min3(const PetscInt a[3]) { return PetscMin(a[0], PetscMin(a[1], a[2])); }
69*2b730f8bSJeremy L Thompson static void     GlobalNodes(const PetscInt p[3], const PetscInt i_rank[3], PetscInt degree, const PetscInt mesh_elem[3], PetscInt m_nodes[3]) {
70*2b730f8bSJeremy L Thompson       for (int d = 0; d < 3; d++) m_nodes[d] = degree * mesh_elem[d] + (i_rank[d] == p[d] - 1);
71cb32e2e7SValeria Barra }
72*2b730f8bSJeremy L Thompson static PetscInt GlobalStart(const PetscInt p[3], const PetscInt i_rank[3], PetscInt degree, const PetscInt mesh_elem[3]) {
73cb32e2e7SValeria Barra   PetscInt start = 0;
74cb32e2e7SValeria Barra   // Dumb brute-force is easier to read
75cb32e2e7SValeria Barra   for (PetscInt i = 0; i < p[0]; i++) {
76cb32e2e7SValeria Barra     for (PetscInt j = 0; j < p[1]; j++) {
77cb32e2e7SValeria Barra       for (PetscInt k = 0; k < p[2]; k++) {
789b072555Sjeremylt         PetscInt m_nodes[3], ijk_rank[] = {i, j, k};
799b072555Sjeremylt         if (i == i_rank[0] && j == i_rank[1] && k == i_rank[2]) return start;
809b072555Sjeremylt         GlobalNodes(p, ijk_rank, degree, mesh_elem, m_nodes);
819b072555Sjeremylt         start += m_nodes[0] * m_nodes[1] * m_nodes[2];
82cb32e2e7SValeria Barra       }
83cb32e2e7SValeria Barra     }
84cb32e2e7SValeria Barra   }
85cb32e2e7SValeria Barra   return -1;
86cb32e2e7SValeria Barra }
87*2b730f8bSJeremy L Thompson static PetscErrorCode CreateRestriction(Ceed ceed, const CeedInt mesh_elem[3], CeedInt P, CeedInt num_comp, CeedElemRestriction *elem_restr) {
889b072555Sjeremylt   const PetscInt num_elem = mesh_elem[0] * mesh_elem[1] * mesh_elem[2];
899b072555Sjeremylt   PetscInt       m_nodes[3], *idx, *idx_p;
90cb32e2e7SValeria Barra 
915dfaedb8SJed Brown   PetscFunctionBeginUser;
92cb32e2e7SValeria Barra   // Get indicies
939b072555Sjeremylt   for (int d = 0; d < 3; d++) m_nodes[d] = mesh_elem[d] * (P - 1) + 1;
949b072555Sjeremylt   idx_p = idx = malloc(num_elem * P * P * P * sizeof idx[0]);
95*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < mesh_elem[0]; i++) {
96*2b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < mesh_elem[1]; j++) {
97*2b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < mesh_elem[2]; k++, idx_p += P * P * P) {
98*2b730f8bSJeremy L Thompson         for (CeedInt ii = 0; ii < P; ii++) {
99*2b730f8bSJeremy L Thompson           for (CeedInt jj = 0; jj < P; jj++) {
100cb32e2e7SValeria Barra             for (CeedInt kk = 0; kk < P; kk++) {
101cb32e2e7SValeria Barra               if (0) {  // This is the C-style (i,j,k) ordering that I prefer
102*2b730f8bSJeremy L Thompson                 idx_p[(ii * P + jj) * P + kk] = num_comp * (((i * (P - 1) + ii) * m_nodes[1] + (j * (P - 1) + jj)) * m_nodes[2] + (k * (P - 1) + kk));
103cb32e2e7SValeria Barra               } else {  // (k,j,i) ordering for consistency with MFEM example
104*2b730f8bSJeremy L Thompson                 idx_p[ii + P * (jj + P * kk)] = num_comp * (((i * (P - 1) + ii) * m_nodes[1] + (j * (P - 1) + jj)) * m_nodes[2] + (k * (P - 1) + kk));
105*2b730f8bSJeremy L Thompson               }
106*2b730f8bSJeremy L Thompson             }
107*2b730f8bSJeremy L Thompson           }
108*2b730f8bSJeremy L Thompson         }
109*2b730f8bSJeremy L Thompson       }
110cb32e2e7SValeria Barra     }
111cb32e2e7SValeria Barra   }
112cb32e2e7SValeria Barra 
113cb32e2e7SValeria Barra   // Setup CEED restriction
114*2b730f8bSJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, P * P * P, num_comp, 1, m_nodes[0] * m_nodes[1] * m_nodes[2] * num_comp, CEED_MEM_HOST, CEED_OWN_POINTER,
115*2b730f8bSJeremy L Thompson                             idx, elem_restr);
116cb32e2e7SValeria Barra 
117cb32e2e7SValeria Barra   PetscFunctionReturn(0);
118cb32e2e7SValeria Barra }
119cb32e2e7SValeria Barra 
120cb32e2e7SValeria Barra // Data for PETSc
121d4d45553Srezgarshakeri typedef struct OperatorApplyContext_ *OperatorApplyContext;
122d4d45553Srezgarshakeri struct OperatorApplyContext_ {
123cb32e2e7SValeria Barra   MPI_Comm     comm;
1249b072555Sjeremylt   VecScatter   l_to_g;    // Scatter for all entries
1259b072555Sjeremylt   VecScatter   l_to_g_0;  // Skip Dirichlet values
1269b072555Sjeremylt   VecScatter   g_to_g_D;  // global-to-global; only Dirichlet values
1279b072555Sjeremylt   Vec          X_loc, Y_loc;
1289b072555Sjeremylt   CeedVector   x_ceed, y_ceed;
129cb32e2e7SValeria Barra   CeedOperator op;
1309b072555Sjeremylt   CeedVector   q_data;
131cb32e2e7SValeria Barra   Ceed         ceed;
132cb32e2e7SValeria Barra };
133cb32e2e7SValeria Barra 
134cb32e2e7SValeria Barra // BP Options
135*2b730f8bSJeremy L Thompson typedef enum { CEED_BP1 = 0, CEED_BP2 = 1, CEED_BP3 = 2, CEED_BP4 = 3, CEED_BP5 = 4, CEED_BP6 = 5 } BPType;
136*2b730f8bSJeremy L Thompson static const char *const bp_types[] = {"bp1", "bp2", "bp3", "bp4", "bp5", "bp6", "BPType", "CEED_BP", 0};
137cb32e2e7SValeria Barra 
138cb32e2e7SValeria Barra // BP specific data
139cb32e2e7SValeria Barra typedef struct {
1409b072555Sjeremylt   CeedInt           num_comp_u, q_data_size, q_extra;
1419b072555Sjeremylt   CeedQFunctionUser setup_geo, setup_rhs, apply, error;
1429b072555Sjeremylt   const char       *setup_geo_loc, *setup_rhs_loc, *apply_loc, *error_loc;
1439b072555Sjeremylt   CeedEvalMode      in_mode, out_mode;
1449b072555Sjeremylt   CeedQuadMode      q_mode;
1459b072555Sjeremylt } BPData;
146cb32e2e7SValeria Barra 
1479b072555Sjeremylt BPData bp_options[6] = {
148*2b730f8bSJeremy L Thompson     [CEED_BP1] = {.num_comp_u    = 1,
1499b072555Sjeremylt                   .q_data_size   = 1,
1509b072555Sjeremylt                   .q_extra       = 1,
1519b072555Sjeremylt                   .setup_geo     = SetupMassGeo,
1529b072555Sjeremylt                   .setup_rhs     = SetupMassRhs,
153cb32e2e7SValeria Barra                   .apply         = Mass,
154cb32e2e7SValeria Barra                   .error         = Error,
1559b072555Sjeremylt                   .setup_geo_loc = SetupMassGeo_loc,
1569b072555Sjeremylt                   .setup_rhs_loc = SetupMassRhs_loc,
1579b072555Sjeremylt                   .apply_loc     = Mass_loc,
1589b072555Sjeremylt                   .error_loc     = Error_loc,
1599b072555Sjeremylt                   .in_mode       = CEED_EVAL_INTERP,
1609b072555Sjeremylt                   .out_mode      = CEED_EVAL_INTERP,
161*2b730f8bSJeremy L Thompson                   .q_mode        = CEED_GAUSS        },
162*2b730f8bSJeremy L Thompson     [CEED_BP2] = {.num_comp_u    = 3,
1639b072555Sjeremylt                   .q_data_size   = 1,
1649b072555Sjeremylt                   .q_extra       = 1,
1659b072555Sjeremylt                   .setup_geo     = SetupMassGeo,
1669b072555Sjeremylt                   .setup_rhs     = SetupMassRhs3,
167cb32e2e7SValeria Barra                   .apply         = Mass3,
168cb32e2e7SValeria Barra                   .error         = Error3,
1699b072555Sjeremylt                   .setup_geo_loc = SetupMassGeo_loc,
1709b072555Sjeremylt                   .setup_rhs_loc = SetupMassRhs3_loc,
1719b072555Sjeremylt                   .apply_loc     = Mass3_loc,
1729b072555Sjeremylt                   .error_loc     = Error3_loc,
1739b072555Sjeremylt                   .in_mode       = CEED_EVAL_INTERP,
1749b072555Sjeremylt                   .out_mode      = CEED_EVAL_INTERP,
175*2b730f8bSJeremy L Thompson                   .q_mode        = CEED_GAUSS        },
176*2b730f8bSJeremy L Thompson     [CEED_BP3] = {.num_comp_u    = 1,
1779b072555Sjeremylt                   .q_data_size   = 7,
1789b072555Sjeremylt                   .q_extra       = 1,
1799b072555Sjeremylt                   .setup_geo     = SetupDiffGeo,
1809b072555Sjeremylt                   .setup_rhs     = SetupDiffRhs,
181cb32e2e7SValeria Barra                   .apply         = Diff,
182cb32e2e7SValeria Barra                   .error         = Error,
1839b072555Sjeremylt                   .setup_geo_loc = SetupDiffGeo_loc,
1849b072555Sjeremylt                   .setup_rhs_loc = SetupDiffRhs_loc,
1859b072555Sjeremylt                   .apply_loc     = Diff_loc,
1869b072555Sjeremylt                   .error_loc     = Error_loc,
1879b072555Sjeremylt                   .in_mode       = CEED_EVAL_GRAD,
1889b072555Sjeremylt                   .out_mode      = CEED_EVAL_GRAD,
189*2b730f8bSJeremy L Thompson                   .q_mode        = CEED_GAUSS        },
190*2b730f8bSJeremy L Thompson     [CEED_BP4] = {.num_comp_u    = 3,
1919b072555Sjeremylt                   .q_data_size   = 7,
1929b072555Sjeremylt                   .q_extra       = 1,
1939b072555Sjeremylt                   .setup_geo     = SetupDiffGeo,
1949b072555Sjeremylt                   .setup_rhs     = SetupDiffRhs3,
195cb32e2e7SValeria Barra                   .apply         = Diff3,
196cb32e2e7SValeria Barra                   .error         = Error3,
1979b072555Sjeremylt                   .setup_geo_loc = SetupDiffGeo_loc,
1989b072555Sjeremylt                   .setup_rhs_loc = SetupDiffRhs3_loc,
1999b072555Sjeremylt                   .apply_loc     = Diff3_loc,
2009b072555Sjeremylt                   .error_loc     = Error3_loc,
2019b072555Sjeremylt                   .in_mode       = CEED_EVAL_GRAD,
2029b072555Sjeremylt                   .out_mode      = CEED_EVAL_GRAD,
203*2b730f8bSJeremy L Thompson                   .q_mode        = CEED_GAUSS        },
204*2b730f8bSJeremy L Thompson     [CEED_BP5] = {.num_comp_u    = 1,
2059b072555Sjeremylt                   .q_data_size   = 7,
2069b072555Sjeremylt                   .q_extra       = 0,
2079b072555Sjeremylt                   .setup_geo     = SetupDiffGeo,
2089b072555Sjeremylt                   .setup_rhs     = SetupDiffRhs,
209cb32e2e7SValeria Barra                   .apply         = Diff,
210cb32e2e7SValeria Barra                   .error         = Error,
2119b072555Sjeremylt                   .setup_geo_loc = SetupDiffGeo_loc,
2129b072555Sjeremylt                   .setup_rhs_loc = SetupDiffRhs_loc,
2139b072555Sjeremylt                   .apply_loc     = Diff_loc,
2149b072555Sjeremylt                   .error_loc     = Error_loc,
2159b072555Sjeremylt                   .in_mode       = CEED_EVAL_GRAD,
2169b072555Sjeremylt                   .out_mode      = CEED_EVAL_GRAD,
217*2b730f8bSJeremy L Thompson                   .q_mode        = CEED_GAUSS_LOBATTO},
218*2b730f8bSJeremy L Thompson     [CEED_BP6] = {.num_comp_u    = 3,
2199b072555Sjeremylt                   .q_data_size   = 7,
2209b072555Sjeremylt                   .q_extra       = 0,
2219b072555Sjeremylt                   .setup_geo     = SetupDiffGeo,
2229b072555Sjeremylt                   .setup_rhs     = SetupDiffRhs3,
223cb32e2e7SValeria Barra                   .apply         = Diff3,
224cb32e2e7SValeria Barra                   .error         = Error3,
2259b072555Sjeremylt                   .setup_geo_loc = SetupDiffGeo_loc,
2269b072555Sjeremylt                   .setup_rhs_loc = SetupDiffRhs3_loc,
2279b072555Sjeremylt                   .apply_loc     = Diff3_loc,
2289b072555Sjeremylt                   .error_loc     = Error3_loc,
2299b072555Sjeremylt                   .in_mode       = CEED_EVAL_GRAD,
2309b072555Sjeremylt                   .out_mode      = CEED_EVAL_GRAD,
231*2b730f8bSJeremy L Thompson                   .q_mode        = CEED_GAUSS_LOBATTO}
232cb32e2e7SValeria Barra };
233cb32e2e7SValeria Barra 
234cb32e2e7SValeria Barra // This function uses libCEED to compute the action of the mass matrix
235cb32e2e7SValeria Barra static PetscErrorCode MatMult_Mass(Mat A, Vec X, Vec Y) {
236d4d45553Srezgarshakeri   OperatorApplyContext op_apply_ctx;
237cb32e2e7SValeria Barra   PetscScalar         *x, *y;
2389b072555Sjeremylt   PetscMemType         x_mem_type, y_mem_type;
239cb32e2e7SValeria Barra 
240cb32e2e7SValeria Barra   PetscFunctionBeginUser;
2419396343dSjeremylt 
242*2b730f8bSJeremy L Thompson   PetscCall(MatShellGetContext(A, &op_apply_ctx));
2439396343dSjeremylt 
2449396343dSjeremylt   // Global-to-local
245*2b730f8bSJeremy L Thompson   PetscCall(VecScatterBegin(op_apply_ctx->l_to_g, X, op_apply_ctx->X_loc, INSERT_VALUES, SCATTER_REVERSE));
246*2b730f8bSJeremy L Thompson   PetscCall(VecScatterEnd(op_apply_ctx->l_to_g, X, op_apply_ctx->X_loc, INSERT_VALUES, SCATTER_REVERSE));
247cb32e2e7SValeria Barra 
2489396343dSjeremylt   // Setup libCEED vectors
249*2b730f8bSJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(op_apply_ctx->X_loc, (const PetscScalar **)&x, &x_mem_type));
250*2b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(op_apply_ctx->Y_loc, &y, &y_mem_type));
251*2b730f8bSJeremy L Thompson   CeedVectorSetArray(op_apply_ctx->x_ceed, MemTypeP2C(x_mem_type), CEED_USE_POINTER, x);
252*2b730f8bSJeremy L Thompson   CeedVectorSetArray(op_apply_ctx->y_ceed, MemTypeP2C(y_mem_type), CEED_USE_POINTER, y);
253cb32e2e7SValeria Barra 
2549396343dSjeremylt   // Apply libCEED operator
255*2b730f8bSJeremy L Thompson   CeedOperatorApply(op_apply_ctx->op, op_apply_ctx->x_ceed, op_apply_ctx->y_ceed, CEED_REQUEST_IMMEDIATE);
256cb32e2e7SValeria Barra 
2579396343dSjeremylt   // Restore PETSc vectors
258d4d45553Srezgarshakeri   CeedVectorTakeArray(op_apply_ctx->x_ceed, MemTypeP2C(x_mem_type), NULL);
259d4d45553Srezgarshakeri   CeedVectorTakeArray(op_apply_ctx->y_ceed, MemTypeP2C(y_mem_type), NULL);
260*2b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(op_apply_ctx->X_loc, (const PetscScalar **)&x));
261*2b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(op_apply_ctx->Y_loc, &y));
262cb32e2e7SValeria Barra 
2639396343dSjeremylt   // Local-to-global
264cb32e2e7SValeria Barra   if (Y) {
265*2b730f8bSJeremy L Thompson     PetscCall(VecZeroEntries(Y));
266*2b730f8bSJeremy L Thompson     PetscCall(VecScatterBegin(op_apply_ctx->l_to_g, op_apply_ctx->Y_loc, Y, ADD_VALUES, SCATTER_FORWARD));
267*2b730f8bSJeremy L Thompson     PetscCall(VecScatterEnd(op_apply_ctx->l_to_g, op_apply_ctx->Y_loc, Y, ADD_VALUES, SCATTER_FORWARD));
268cb32e2e7SValeria Barra   }
269cb32e2e7SValeria Barra   PetscFunctionReturn(0);
270cb32e2e7SValeria Barra }
271cb32e2e7SValeria Barra 
272cb32e2e7SValeria Barra // This function uses libCEED to compute the action of the Laplacian with
273cb32e2e7SValeria Barra // Dirichlet boundary conditions
274cb32e2e7SValeria Barra static PetscErrorCode MatMult_Diff(Mat A, Vec X, Vec Y) {
275d4d45553Srezgarshakeri   OperatorApplyContext op_apply_ctx;
276cb32e2e7SValeria Barra   PetscScalar         *x, *y;
2779b072555Sjeremylt   PetscMemType         x_mem_type, y_mem_type;
278cb32e2e7SValeria Barra 
279cb32e2e7SValeria Barra   PetscFunctionBeginUser;
2809396343dSjeremylt 
281*2b730f8bSJeremy L Thompson   PetscCall(MatShellGetContext(A, &op_apply_ctx));
282cb32e2e7SValeria Barra 
283cb32e2e7SValeria Barra   // Global-to-local
284*2b730f8bSJeremy L Thompson   PetscCall(VecScatterBegin(op_apply_ctx->l_to_g_0, X, op_apply_ctx->X_loc, INSERT_VALUES, SCATTER_REVERSE));
285*2b730f8bSJeremy L Thompson   PetscCall(VecScatterEnd(op_apply_ctx->l_to_g_0, X, op_apply_ctx->X_loc, INSERT_VALUES, SCATTER_REVERSE));
286cb32e2e7SValeria Barra 
2879396343dSjeremylt   // Setup libCEED vectors
288*2b730f8bSJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(op_apply_ctx->X_loc, (const PetscScalar **)&x, &x_mem_type));
289*2b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(op_apply_ctx->Y_loc, &y, &y_mem_type));
290*2b730f8bSJeremy L Thompson   CeedVectorSetArray(op_apply_ctx->x_ceed, MemTypeP2C(x_mem_type), CEED_USE_POINTER, x);
291*2b730f8bSJeremy L Thompson   CeedVectorSetArray(op_apply_ctx->y_ceed, MemTypeP2C(y_mem_type), CEED_USE_POINTER, y);
292cb32e2e7SValeria Barra 
2939396343dSjeremylt   // Apply libCEED operator
294*2b730f8bSJeremy L Thompson   CeedOperatorApply(op_apply_ctx->op, op_apply_ctx->x_ceed, op_apply_ctx->y_ceed, CEED_REQUEST_IMMEDIATE);
295cb32e2e7SValeria Barra 
296cb32e2e7SValeria Barra   // Restore PETSc vectors
297d4d45553Srezgarshakeri   CeedVectorTakeArray(op_apply_ctx->x_ceed, MemTypeP2C(x_mem_type), NULL);
298d4d45553Srezgarshakeri   CeedVectorTakeArray(op_apply_ctx->y_ceed, MemTypeP2C(y_mem_type), NULL);
299*2b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(op_apply_ctx->X_loc, (const PetscScalar **)&x));
300*2b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(op_apply_ctx->Y_loc, &y));
301cb32e2e7SValeria Barra 
302cb32e2e7SValeria Barra   // Local-to-global
303*2b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(Y));
304*2b730f8bSJeremy L Thompson   PetscCall(VecScatterBegin(op_apply_ctx->g_to_g_D, X, Y, INSERT_VALUES, SCATTER_FORWARD));
305*2b730f8bSJeremy L Thompson   PetscCall(VecScatterEnd(op_apply_ctx->g_to_g_D, X, Y, INSERT_VALUES, SCATTER_FORWARD));
306*2b730f8bSJeremy L Thompson   PetscCall(VecScatterBegin(op_apply_ctx->l_to_g_0, op_apply_ctx->Y_loc, Y, ADD_VALUES, SCATTER_FORWARD));
307*2b730f8bSJeremy L Thompson   PetscCall(VecScatterEnd(op_apply_ctx->l_to_g_0, op_apply_ctx->Y_loc, Y, ADD_VALUES, SCATTER_FORWARD));
308cb32e2e7SValeria Barra 
309cb32e2e7SValeria Barra   PetscFunctionReturn(0);
310cb32e2e7SValeria Barra }
311cb32e2e7SValeria Barra 
312cb32e2e7SValeria Barra // This function calculates the error in the final solution
313*2b730f8bSJeremy L Thompson static PetscErrorCode ComputeErrorMax(OperatorApplyContext op_apply_ctx, CeedOperator op_error, Vec X, CeedVector target, PetscReal *max_error) {
314cb32e2e7SValeria Barra   PetscScalar *x;
3159b072555Sjeremylt   PetscMemType mem_type;
316cb32e2e7SValeria Barra   CeedVector   collocated_error;
3171f9221feSJeremy L Thompson   CeedSize     length;
318cb32e2e7SValeria Barra 
319cb32e2e7SValeria Barra   PetscFunctionBeginUser;
3209396343dSjeremylt 
321cb32e2e7SValeria Barra   CeedVectorGetLength(target, &length);
322d4d45553Srezgarshakeri   CeedVectorCreate(op_apply_ctx->ceed, length, &collocated_error);
323cb32e2e7SValeria Barra 
324cb32e2e7SValeria Barra   // Global-to-local
325*2b730f8bSJeremy L Thompson   PetscCall(VecScatterBegin(op_apply_ctx->l_to_g, X, op_apply_ctx->X_loc, INSERT_VALUES, SCATTER_REVERSE));
326*2b730f8bSJeremy L Thompson   PetscCall(VecScatterEnd(op_apply_ctx->l_to_g, X, op_apply_ctx->X_loc, INSERT_VALUES, SCATTER_REVERSE));
3279396343dSjeremylt 
3289396343dSjeremylt   // Setup libCEED vector
329*2b730f8bSJeremy L Thompson   PetscCall(VecGetArrayReadAndMemType(op_apply_ctx->X_loc, (const PetscScalar **)&x, &mem_type));
330*2b730f8bSJeremy L Thompson   CeedVectorSetArray(op_apply_ctx->x_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, x);
331cb32e2e7SValeria Barra 
3329396343dSjeremylt   // Apply libCEED operator
333*2b730f8bSJeremy L Thompson   CeedOperatorApply(op_error, op_apply_ctx->x_ceed, collocated_error, CEED_REQUEST_IMMEDIATE);
334cb32e2e7SValeria Barra 
335cb32e2e7SValeria Barra   // Restore PETSc vector
336d4d45553Srezgarshakeri   CeedVectorTakeArray(op_apply_ctx->x_ceed, MemTypeP2C(mem_type), NULL);
337*2b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayReadAndMemType(op_apply_ctx->X_loc, (const PetscScalar **)&x));
338cb32e2e7SValeria Barra 
339cb32e2e7SValeria Barra   // Reduce max error
3409b072555Sjeremylt   *max_error = 0;
341cb32e2e7SValeria Barra   const CeedScalar *e;
342cb32e2e7SValeria Barra   CeedVectorGetArrayRead(collocated_error, CEED_MEM_HOST, &e);
343cb32e2e7SValeria Barra   for (CeedInt i = 0; i < length; i++) {
3449b072555Sjeremylt     *max_error = PetscMax(*max_error, PetscAbsScalar(e[i]));
345cb32e2e7SValeria Barra   }
346cb32e2e7SValeria Barra   CeedVectorRestoreArrayRead(collocated_error, &e);
347*2b730f8bSJeremy L Thompson   PetscCall(MPI_Allreduce(MPI_IN_PLACE, max_error, 1, MPIU_REAL, MPIU_MAX, op_apply_ctx->comm));
348cb32e2e7SValeria Barra 
349cb32e2e7SValeria Barra   // Cleanup
350cb32e2e7SValeria Barra   CeedVectorDestroy(&collocated_error);
351cb32e2e7SValeria Barra 
352cb32e2e7SValeria Barra   PetscFunctionReturn(0);
353cb32e2e7SValeria Barra }
354cb32e2e7SValeria Barra 
355cb32e2e7SValeria Barra int main(int argc, char **argv) {
356cb32e2e7SValeria Barra   MPI_Comm comm;
3579b072555Sjeremylt   char     ceed_resource[PETSC_MAX_PATH_LEN] = "/cpu/self";
358cb32e2e7SValeria Barra   double   my_rt_start, my_rt, rt_min, rt_max;
359*2b730f8bSJeremy L Thompson   PetscInt degree, q_extra, local_nodes, local_elem, mesh_elem[3], m_nodes[3], p[3], i_rank[3], l_nodes[3], l_size, num_comp_u = 1,
360*2b730f8bSJeremy L Thompson                                                                                                                     ksp_max_it_clip[2];
361cb32e2e7SValeria Barra   PetscScalar         *r;
362cb32e2e7SValeria Barra   PetscBool            test_mode, benchmark_mode, write_solution;
363cb32e2e7SValeria Barra   PetscMPIInt          size, rank;
3649b072555Sjeremylt   PetscLogStage        solve_stage;
3659b072555Sjeremylt   Vec                  X, X_loc, rhs, rhs_loc;
366cb32e2e7SValeria Barra   Mat                  mat;
367cb32e2e7SValeria Barra   KSP                  ksp;
3689b072555Sjeremylt   VecScatter           l_to_g, l_to_g_0, g_to_g_D;
3699b072555Sjeremylt   PetscMemType         mem_type;
370d4d45553Srezgarshakeri   OperatorApplyContext op_apply_ctx;
371cb32e2e7SValeria Barra   Ceed                 ceed;
3729b072555Sjeremylt   CeedBasis            basis_x, basis_u;
3739b072555Sjeremylt   CeedElemRestriction  elem_restr_x, elem_restr_u, elem_restr_u_i, elem_restr_qd_i;
3749b072555Sjeremylt   CeedQFunction        qf_setup_geo, qf_setup_rhs, qf_apply, qf_error;
3759b072555Sjeremylt   CeedOperator         op_setup_geo, op_setup_rhs, op_apply, op_error;
3769b072555Sjeremylt   CeedVector           x_coord, q_data, rhs_ceed, target;
377cb32e2e7SValeria Barra   CeedInt              P, Q;
3789b072555Sjeremylt   const CeedInt        dim = 3, num_comp_x = 3;
3799b072555Sjeremylt   BPType               bp_choice;
380cb32e2e7SValeria Barra 
381*2b730f8bSJeremy L Thompson   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
382cb32e2e7SValeria Barra   comm = PETSC_COMM_WORLD;
38332d2ee49SValeria Barra 
38432d2ee49SValeria Barra   // Read command line options
38567490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "CEED BPs in PETSc", NULL);
3869b072555Sjeremylt   bp_choice = CEED_BP1;
387*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-problem", "CEED benchmark problem to solve", NULL, bp_types, (PetscEnum)bp_choice, (PetscEnum *)&bp_choice, NULL));
3889b072555Sjeremylt   num_comp_u = bp_options[bp_choice].num_comp_u;
389cb32e2e7SValeria Barra   test_mode  = PETSC_FALSE;
390*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-test", "Testing mode (do not print unless error is large)", NULL, test_mode, &test_mode, NULL));
391cb32e2e7SValeria Barra   benchmark_mode = PETSC_FALSE;
392*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-benchmark", "Benchmarking mode (prints benchmark statistics)", NULL, benchmark_mode, &benchmark_mode, NULL));
393cb32e2e7SValeria Barra   write_solution = PETSC_FALSE;
394*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-write_solution", "Write solution for visualization", NULL, write_solution, &write_solution, NULL));
395cb32e2e7SValeria Barra   degree = test_mode ? 3 : 1;
396*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-degree", "Polynomial degree of tensor product basis", NULL, degree, &degree, NULL));
3979b072555Sjeremylt   q_extra = bp_options[bp_choice].q_extra;
398*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points", NULL, q_extra, &q_extra, NULL));
399*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsString("-ceed", "CEED resource specifier", NULL, ceed_resource, ceed_resource, sizeof(ceed_resource), NULL));
4009b072555Sjeremylt   local_nodes = 1000;
401*2b730f8bSJeremy L Thompson   PetscCall(PetscOptionsInt("-local", "Target number of locally owned nodes per process", NULL, local_nodes, &local_nodes, NULL));
4022fbc6e41SJeremy L Thompson   PetscInt two       = 2;
4032fbc6e41SJeremy L Thompson   ksp_max_it_clip[0] = 5;
4042fbc6e41SJeremy L Thompson   ksp_max_it_clip[1] = 20;
405*2b730f8bSJeremy L Thompson   PetscCall(
406*2b730f8bSJeremy L Thompson       PetscOptionsIntArray("-ksp_max_it_clip", "Min and max number of iterations to use during benchmarking", NULL, ksp_max_it_clip, &two, NULL));
40767490bc6SJeremy L Thompson   PetscOptionsEnd();
408cb32e2e7SValeria Barra   P = degree + 1;
4099b072555Sjeremylt   Q = P + q_extra;
410cb32e2e7SValeria Barra 
4119396343dSjeremylt   // Set up libCEED
4129b072555Sjeremylt   CeedInit(ceed_resource, &ceed);
4139b072555Sjeremylt   CeedMemType mem_type_backend;
4149b072555Sjeremylt   CeedGetPreferredMemType(ceed, &mem_type_backend);
4159396343dSjeremylt 
4169b072555Sjeremylt   VecType default_vec_type = NULL, vec_type;
4179b072555Sjeremylt   switch (mem_type_backend) {
418*2b730f8bSJeremy L Thompson     case CEED_MEM_HOST:
419*2b730f8bSJeremy L Thompson       default_vec_type = VECSTANDARD;
420*2b730f8bSJeremy L Thompson       break;
421b68a8d79SJed Brown     case CEED_MEM_DEVICE: {
422b68a8d79SJed Brown       const char *resolved;
423b68a8d79SJed Brown       CeedGetResource(ceed, &resolved);
4249b072555Sjeremylt       if (strstr(resolved, "/gpu/cuda")) default_vec_type = VECCUDA;
425*2b730f8bSJeremy L Thompson       else if (strstr(resolved, "/gpu/hip/occa")) default_vec_type = VECSTANDARD;  // https://github.com/CEED/libCEED/issues/678
4269b072555Sjeremylt       else if (strstr(resolved, "/gpu/hip")) default_vec_type = VECHIP;
4279b072555Sjeremylt       else default_vec_type = VECSTANDARD;
428b68a8d79SJed Brown     }
429b68a8d79SJed Brown   }
4309396343dSjeremylt 
431cb32e2e7SValeria Barra   // Determine size of process grid
432*2b730f8bSJeremy L Thompson   PetscCall(MPI_Comm_size(comm, &size));
433cb32e2e7SValeria Barra   Split3(size, p, false);
434cb32e2e7SValeria Barra 
4359b072555Sjeremylt   // Find a nicely composite number of elements no less than local_nodes
436*2b730f8bSJeremy L Thompson   for (local_elem = PetscMax(1, local_nodes / (degree * degree * degree));; local_elem++) {
4379b072555Sjeremylt     Split3(local_elem, mesh_elem, true);
4389b072555Sjeremylt     if (Max3(mesh_elem) / Min3(mesh_elem) <= 2) break;
439cb32e2e7SValeria Barra   }
440cb32e2e7SValeria Barra 
441cb32e2e7SValeria Barra   // Find my location in the process grid
442*2b730f8bSJeremy L Thompson   PetscCall(MPI_Comm_rank(comm, &rank));
4439b072555Sjeremylt   for (int d = 0, rank_left = rank; d < dim; d++) {
444cb32e2e7SValeria Barra     const int pstride[3] = {p[1] * p[2], p[2], 1};
4459b072555Sjeremylt     i_rank[d]            = rank_left / pstride[d];
4469b072555Sjeremylt     rank_left -= i_rank[d] * pstride[d];
447cb32e2e7SValeria Barra   }
448cb32e2e7SValeria Barra 
4499b072555Sjeremylt   GlobalNodes(p, i_rank, degree, mesh_elem, m_nodes);
450cb32e2e7SValeria Barra 
451cb32e2e7SValeria Barra   // Setup global vector
452*2b730f8bSJeremy L Thompson   PetscCall(VecCreate(comm, &X));
453*2b730f8bSJeremy L Thompson   PetscCall(VecSetType(X, default_vec_type));
454*2b730f8bSJeremy L Thompson   PetscCall(VecSetSizes(X, m_nodes[0] * m_nodes[1] * m_nodes[2] * num_comp_u, PETSC_DECIDE));
455*2b730f8bSJeremy L Thompson   PetscCall(VecSetFromOptions(X));
456*2b730f8bSJeremy L Thompson   PetscCall(VecSetUp(X));
457cb32e2e7SValeria Barra 
458cb32e2e7SValeria Barra   // Set up libCEED
4599b072555Sjeremylt   CeedInit(ceed_resource, &ceed);
460cb32e2e7SValeria Barra 
461cb32e2e7SValeria Barra   // Print summary
462cb32e2e7SValeria Barra   CeedInt gsize;
463*2b730f8bSJeremy L Thompson   PetscCall(VecGetSize(X, &gsize));
464dc7d240cSValeria Barra   if (!test_mode) {
4659b072555Sjeremylt     const char *used_resource;
4669b072555Sjeremylt     CeedGetResource(ceed, &used_resource);
4679396343dSjeremylt 
468*2b730f8bSJeremy L Thompson     PetscCall(VecGetType(X, &vec_type));
4699396343dSjeremylt 
470*2b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm,
471990fdeb6SJeremy L Thompson                           "\n-- CEED Benchmark Problem %" CeedInt_FMT " -- libCEED + PETSc --\n"
4729396343dSjeremylt                           "  PETSc:\n"
4739396343dSjeremylt                           "    PETSc Vec Type                     : %s\n"
474cb32e2e7SValeria Barra                           "  libCEED:\n"
475cb32e2e7SValeria Barra                           "    libCEED Backend                    : %s\n"
4769396343dSjeremylt                           "    libCEED Backend MemType            : %s\n"
477cb32e2e7SValeria Barra                           "  Mesh:\n"
478751eb813Srezgarshakeri                           "    Solution Order (P)                 : %" CeedInt_FMT "\n"
479751eb813Srezgarshakeri                           "    Quadrature  Order (Q)              : %" CeedInt_FMT "\n"
48008140895SJed Brown                           "    Global nodes                       : %" PetscInt_FMT "\n"
481*2b730f8bSJeremy L Thompson                           "    Process Decomposition              : %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n"
482*2b730f8bSJeremy L Thompson                           "    Local Elements                     : %" PetscInt_FMT " = %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n"
483*2b730f8bSJeremy L Thompson                           "    Owned nodes                        : %" PetscInt_FMT " = %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n"
48408140895SJed Brown                           "    DoF per node                       : %" PetscInt_FMT "\n",
485*2b730f8bSJeremy L Thompson                           bp_choice + 1, vec_type, used_resource, CeedMemTypes[mem_type_backend], P, Q, gsize / num_comp_u, p[0], p[1], p[2],
486*2b730f8bSJeremy L Thompson                           local_elem, mesh_elem[0], mesh_elem[1], mesh_elem[2], m_nodes[0] * m_nodes[1] * m_nodes[2], m_nodes[0], m_nodes[1],
487*2b730f8bSJeremy L Thompson                           m_nodes[2], num_comp_u));
488cb32e2e7SValeria Barra   }
489cb32e2e7SValeria Barra 
490cb32e2e7SValeria Barra   {
4919b072555Sjeremylt     l_size = 1;
492cb32e2e7SValeria Barra     for (int d = 0; d < dim; d++) {
4939b072555Sjeremylt       l_nodes[d] = mesh_elem[d] * degree + 1;
4949b072555Sjeremylt       l_size *= l_nodes[d];
495cb32e2e7SValeria Barra     }
496*2b730f8bSJeremy L Thompson     PetscCall(VecCreate(PETSC_COMM_SELF, &X_loc));
497*2b730f8bSJeremy L Thompson     PetscCall(VecSetType(X_loc, default_vec_type));
498*2b730f8bSJeremy L Thompson     PetscCall(VecSetSizes(X_loc, l_size * num_comp_u, PETSC_DECIDE));
499*2b730f8bSJeremy L Thompson     PetscCall(VecSetFromOptions(X_loc));
500*2b730f8bSJeremy L Thompson     PetscCall(VecSetUp(X_loc));
501cb32e2e7SValeria Barra 
502cb32e2e7SValeria Barra     // Create local-to-global scatter
5039b072555Sjeremylt     PetscInt *l_to_g_ind, *l_to_g_ind_0, *loc_ind, l_0_count;
5049b072555Sjeremylt     IS        l_to_g_is, l_to_g_is_0, loc_is;
5059b072555Sjeremylt     PetscInt  g_start[2][2][2], g_m_nodes[2][2][2][dim];
506cb32e2e7SValeria Barra 
507cb32e2e7SValeria Barra     for (int i = 0; i < 2; i++) {
508cb32e2e7SValeria Barra       for (int j = 0; j < 2; j++) {
509cb32e2e7SValeria Barra         for (int k = 0; k < 2; k++) {
5109b072555Sjeremylt           PetscInt ijk_rank[3] = {i_rank[0] + i, i_rank[1] + j, i_rank[2] + k};
5119b072555Sjeremylt           g_start[i][j][k]     = GlobalStart(p, ijk_rank, degree, mesh_elem);
5129b072555Sjeremylt           GlobalNodes(p, ijk_rank, degree, mesh_elem, g_m_nodes[i][j][k]);
513cb32e2e7SValeria Barra         }
514cb32e2e7SValeria Barra       }
515cb32e2e7SValeria Barra     }
516cb32e2e7SValeria Barra 
517*2b730f8bSJeremy L Thompson     PetscCall(PetscMalloc1(l_size, &l_to_g_ind));
518*2b730f8bSJeremy L Thompson     PetscCall(PetscMalloc1(l_size, &l_to_g_ind_0));
519*2b730f8bSJeremy L Thompson     PetscCall(PetscMalloc1(l_size, &loc_ind));
5209b072555Sjeremylt     l_0_count = 0;
521*2b730f8bSJeremy L Thompson     for (PetscInt i = 0, ir, ii; ir = i >= m_nodes[0], ii = i - ir * m_nodes[0], i < l_nodes[0]; i++) {
522*2b730f8bSJeremy L Thompson       for (PetscInt j = 0, jr, jj; jr = j >= m_nodes[1], jj = j - jr * m_nodes[1], j < l_nodes[1]; j++) {
523*2b730f8bSJeremy L Thompson         for (PetscInt k = 0, kr, kk; kr = k >= m_nodes[2], kk = k - kr * m_nodes[2], k < l_nodes[2]; k++) {
5249b072555Sjeremylt           PetscInt here    = (i * l_nodes[1] + j) * l_nodes[2] + k;
525*2b730f8bSJeremy L Thompson           l_to_g_ind[here] = g_start[ir][jr][kr] + (ii * g_m_nodes[ir][jr][kr][1] + jj) * g_m_nodes[ir][jr][kr][2] + kk;
526*2b730f8bSJeremy L Thompson           if ((i_rank[0] == 0 && i == 0) || (i_rank[1] == 0 && j == 0) || (i_rank[2] == 0 && k == 0) ||
527*2b730f8bSJeremy L Thompson               (i_rank[0] + 1 == p[0] && i + 1 == l_nodes[0]) || (i_rank[1] + 1 == p[1] && j + 1 == l_nodes[1]) ||
528*2b730f8bSJeremy L Thompson               (i_rank[2] + 1 == p[2] && k + 1 == l_nodes[2]))
529cb32e2e7SValeria Barra             continue;
5309b072555Sjeremylt           l_to_g_ind_0[l_0_count] = l_to_g_ind[here];
5319b072555Sjeremylt           loc_ind[l_0_count++]    = here;
532cb32e2e7SValeria Barra         }
533*2b730f8bSJeremy L Thompson       }
534*2b730f8bSJeremy L Thompson     }
535*2b730f8bSJeremy L Thompson     PetscCall(ISCreateBlock(comm, num_comp_u, l_size, l_to_g_ind, PETSC_OWN_POINTER, &l_to_g_is));
536*2b730f8bSJeremy L Thompson     PetscCall(VecScatterCreate(X_loc, NULL, X, l_to_g_is, &l_to_g));
537*2b730f8bSJeremy L Thompson     PetscCall(ISCreateBlock(comm, num_comp_u, l_0_count, l_to_g_ind_0, PETSC_OWN_POINTER, &l_to_g_is_0));
538*2b730f8bSJeremy L Thompson     PetscCall(ISCreateBlock(comm, num_comp_u, l_0_count, loc_ind, PETSC_OWN_POINTER, &loc_is));
539*2b730f8bSJeremy L Thompson     PetscCall(VecScatterCreate(X_loc, loc_is, X, l_to_g_is_0, &l_to_g_0));
540cb32e2e7SValeria Barra     {
541cb32e2e7SValeria Barra       // Create global-to-global scatter for Dirichlet values (everything not in
5429b072555Sjeremylt       // l_to_g_is_0, which is the range of l_to_g_0)
5439b072555Sjeremylt       PetscInt           x_start, x_end, *ind_D, count_D = 0;
5449b072555Sjeremylt       IS                 is_D;
545cb32e2e7SValeria Barra       const PetscScalar *x;
546*2b730f8bSJeremy L Thompson       PetscCall(VecZeroEntries(X_loc));
547*2b730f8bSJeremy L Thompson       PetscCall(VecSet(X, 1.0));
548*2b730f8bSJeremy L Thompson       PetscCall(VecScatterBegin(l_to_g_0, X_loc, X, INSERT_VALUES, SCATTER_FORWARD));
549*2b730f8bSJeremy L Thompson       PetscCall(VecScatterEnd(l_to_g_0, X_loc, X, INSERT_VALUES, SCATTER_FORWARD));
550*2b730f8bSJeremy L Thompson       PetscCall(VecGetOwnershipRange(X, &x_start, &x_end));
551*2b730f8bSJeremy L Thompson       PetscCall(PetscMalloc1(x_end - x_start, &ind_D));
552*2b730f8bSJeremy L Thompson       PetscCall(VecGetArrayRead(X, &x));
5539b072555Sjeremylt       for (PetscInt i = 0; i < x_end - x_start; i++) {
5549b072555Sjeremylt         if (x[i] == 1.) ind_D[count_D++] = x_start + i;
555cb32e2e7SValeria Barra       }
556*2b730f8bSJeremy L Thompson       PetscCall(VecRestoreArrayRead(X, &x));
557*2b730f8bSJeremy L Thompson       PetscCall(ISCreateGeneral(comm, count_D, ind_D, PETSC_COPY_VALUES, &is_D));
558*2b730f8bSJeremy L Thompson       PetscCall(PetscFree(ind_D));
559*2b730f8bSJeremy L Thompson       PetscCall(VecScatterCreate(X, is_D, X, is_D, &g_to_g_D));
560*2b730f8bSJeremy L Thompson       PetscCall(ISDestroy(&is_D));
561cb32e2e7SValeria Barra     }
562*2b730f8bSJeremy L Thompson     PetscCall(ISDestroy(&l_to_g_is));
563*2b730f8bSJeremy L Thompson     PetscCall(ISDestroy(&l_to_g_is_0));
564*2b730f8bSJeremy L Thompson     PetscCall(ISDestroy(&loc_is));
565cb32e2e7SValeria Barra   }
566cb32e2e7SValeria Barra 
567cb32e2e7SValeria Barra   // CEED bases
568*2b730f8bSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_u, P, Q, bp_options[bp_choice].q_mode, &basis_u);
569*2b730f8bSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_x, 2, Q, bp_options[bp_choice].q_mode, &basis_x);
570cb32e2e7SValeria Barra 
571cb32e2e7SValeria Barra   // CEED restrictions
5729b072555Sjeremylt   CreateRestriction(ceed, mesh_elem, P, num_comp_u, &elem_restr_u);
5739b072555Sjeremylt   CreateRestriction(ceed, mesh_elem, 2, dim, &elem_restr_x);
5749b072555Sjeremylt   CeedInt num_elem = mesh_elem[0] * mesh_elem[1] * mesh_elem[2];
575*2b730f8bSJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, Q * Q * Q, num_comp_u, num_comp_u * num_elem * Q * Q * Q, CEED_STRIDES_BACKEND, &elem_restr_u_i);
576*2b730f8bSJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, Q * Q * Q, bp_options[bp_choice].q_data_size,
577*2b730f8bSJeremy L Thompson                                    bp_options[bp_choice].q_data_size * num_elem * Q * Q * Q, CEED_STRIDES_BACKEND, &elem_restr_qd_i);
578cb32e2e7SValeria Barra   {
5799b072555Sjeremylt     CeedScalar *x_loc;
580*2b730f8bSJeremy L Thompson     CeedInt     shape[3] = {mesh_elem[0] + 1, mesh_elem[1] + 1, mesh_elem[2] + 1}, len = shape[0] * shape[1] * shape[2];
5819b072555Sjeremylt     x_loc = malloc(len * num_comp_x * sizeof x_loc[0]);
582cb32e2e7SValeria Barra     for (CeedInt i = 0; i < shape[0]; i++) {
583cb32e2e7SValeria Barra       for (CeedInt j = 0; j < shape[1]; j++) {
584cb32e2e7SValeria Barra         for (CeedInt k = 0; k < shape[2]; k++) {
585*2b730f8bSJeremy L Thompson           x_loc[dim * ((i * shape[1] + j) * shape[2] + k) + 0] = 1. * (i_rank[0] * mesh_elem[0] + i) / (p[0] * mesh_elem[0]);
586*2b730f8bSJeremy L Thompson           x_loc[dim * ((i * shape[1] + j) * shape[2] + k) + 1] = 1. * (i_rank[1] * mesh_elem[1] + j) / (p[1] * mesh_elem[1]);
587*2b730f8bSJeremy L Thompson           x_loc[dim * ((i * shape[1] + j) * shape[2] + k) + 2] = 1. * (i_rank[2] * mesh_elem[2] + k) / (p[2] * mesh_elem[2]);
588cb32e2e7SValeria Barra         }
589cb32e2e7SValeria Barra       }
590cb32e2e7SValeria Barra     }
5919b072555Sjeremylt     CeedVectorCreate(ceed, len * num_comp_x, &x_coord);
5929b072555Sjeremylt     CeedVectorSetArray(x_coord, CEED_MEM_HOST, CEED_OWN_POINTER, x_loc);
593cb32e2e7SValeria Barra   }
594cb32e2e7SValeria Barra 
5959b072555Sjeremylt   // Create the QFunction that builds the operator quadrature data
596*2b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, bp_options[bp_choice].setup_geo, bp_options[bp_choice].setup_geo_loc, &qf_setup_geo);
5979b072555Sjeremylt   CeedQFunctionAddInput(qf_setup_geo, "x", num_comp_x, CEED_EVAL_INTERP);
5989b072555Sjeremylt   CeedQFunctionAddInput(qf_setup_geo, "dx", num_comp_x * dim, CEED_EVAL_GRAD);
5999b072555Sjeremylt   CeedQFunctionAddInput(qf_setup_geo, "weight", 1, CEED_EVAL_WEIGHT);
600*2b730f8bSJeremy L Thompson   CeedQFunctionAddOutput(qf_setup_geo, "q_data", bp_options[bp_choice].q_data_size, CEED_EVAL_NONE);
601cb32e2e7SValeria Barra 
6029b072555Sjeremylt   // Create the QFunction that sets up the RHS and true solution
603*2b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, bp_options[bp_choice].setup_rhs, bp_options[bp_choice].setup_rhs_loc, &qf_setup_rhs);
6049b072555Sjeremylt   CeedQFunctionAddInput(qf_setup_rhs, "x", num_comp_x, CEED_EVAL_INTERP);
605*2b730f8bSJeremy L Thompson   CeedQFunctionAddInput(qf_setup_rhs, "q_data", bp_options[bp_choice].q_data_size, CEED_EVAL_NONE);
6069b072555Sjeremylt   CeedQFunctionAddOutput(qf_setup_rhs, "true_soln", num_comp_u, CEED_EVAL_NONE);
6079b072555Sjeremylt   CeedQFunctionAddOutput(qf_setup_rhs, "rhs", num_comp_u, CEED_EVAL_INTERP);
608cb32e2e7SValeria Barra 
609cb32e2e7SValeria Barra   // Set up PDE operator
610*2b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, bp_options[bp_choice].apply, bp_options[bp_choice].apply_loc, &qf_apply);
611cb32e2e7SValeria Barra   // Add inputs and outputs
6129b072555Sjeremylt   CeedInt in_scale  = bp_options[bp_choice].in_mode == CEED_EVAL_GRAD ? 3 : 1;
6139b072555Sjeremylt   CeedInt out_scale = bp_options[bp_choice].out_mode == CEED_EVAL_GRAD ? 3 : 1;
614*2b730f8bSJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "u", num_comp_u * in_scale, bp_options[bp_choice].in_mode);
615*2b730f8bSJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "q_data", bp_options[bp_choice].q_data_size, CEED_EVAL_NONE);
616*2b730f8bSJeremy L Thompson   CeedQFunctionAddOutput(qf_apply, "v", num_comp_u * out_scale, bp_options[bp_choice].out_mode);
617cb32e2e7SValeria Barra 
618cb32e2e7SValeria Barra   // Create the error qfunction
619*2b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, bp_options[bp_choice].error, bp_options[bp_choice].error_loc, &qf_error);
6209b072555Sjeremylt   CeedQFunctionAddInput(qf_error, "u", num_comp_u, CEED_EVAL_INTERP);
6219b072555Sjeremylt   CeedQFunctionAddInput(qf_error, "true_soln", num_comp_u, CEED_EVAL_NONE);
622*2b730f8bSJeremy L Thompson   CeedQFunctionAddInput(qf_error, "qdata", bp_options[bp_choice].q_data_size, CEED_EVAL_NONE);
6239b072555Sjeremylt   CeedQFunctionAddOutput(qf_error, "error", num_comp_u, CEED_EVAL_NONE);
624cb32e2e7SValeria Barra 
625cb32e2e7SValeria Barra   // Create the persistent vectors that will be needed in setup
6269b072555Sjeremylt   CeedInt num_qpts;
6279b072555Sjeremylt   CeedBasisGetNumQuadraturePoints(basis_u, &num_qpts);
628*2b730f8bSJeremy L Thompson   CeedVectorCreate(ceed, bp_options[bp_choice].q_data_size * num_elem * num_qpts, &q_data);
6299b072555Sjeremylt   CeedVectorCreate(ceed, num_elem * num_qpts * num_comp_u, &target);
6309b072555Sjeremylt   CeedVectorCreate(ceed, l_size * num_comp_u, &rhs_ceed);
631cb32e2e7SValeria Barra 
632cb32e2e7SValeria Barra   // Create the operator that builds the quadrature data for the ceed operator
633*2b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_geo, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_geo);
634*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_geo, "x", elem_restr_x, basis_x, CEED_VECTOR_ACTIVE);
635*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_geo, "dx", elem_restr_x, basis_x, CEED_VECTOR_ACTIVE);
636*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_geo, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
637*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_geo, "q_data", elem_restr_qd_i, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
638cb32e2e7SValeria Barra 
639cb32e2e7SValeria Barra   // Create the operator that builds the RHS and true solution
640*2b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_rhs, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_rhs);
641*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_rhs, "x", elem_restr_x, basis_x, CEED_VECTOR_ACTIVE);
642*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_rhs, "q_data", elem_restr_qd_i, CEED_BASIS_COLLOCATED, q_data);
643*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_rhs, "true_soln", elem_restr_u_i, CEED_BASIS_COLLOCATED, target);
644*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_rhs, "rhs", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE);
645cb32e2e7SValeria Barra 
646cb32e2e7SValeria Barra   // Create the mass or diff operator
647*2b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_apply, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_apply);
6489b072555Sjeremylt   CeedOperatorSetField(op_apply, "u", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE);
649*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_apply, "q_data", elem_restr_qd_i, CEED_BASIS_COLLOCATED, q_data);
6509b072555Sjeremylt   CeedOperatorSetField(op_apply, "v", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE);
651cb32e2e7SValeria Barra 
652cb32e2e7SValeria Barra   // Create the error operator
653*2b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_error, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_error);
6549b072555Sjeremylt   CeedOperatorSetField(op_error, "u", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE);
655*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "true_soln", elem_restr_u_i, CEED_BASIS_COLLOCATED, target);
656*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "qdata", elem_restr_qd_i, CEED_BASIS_COLLOCATED, q_data);
657*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_error, "error", elem_restr_u_i, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
658cb32e2e7SValeria Barra 
659cb32e2e7SValeria Barra   // Set up Mat
660*2b730f8bSJeremy L Thompson   PetscCall(PetscMalloc1(1, &op_apply_ctx));
661d4d45553Srezgarshakeri   op_apply_ctx->comm   = comm;
662d4d45553Srezgarshakeri   op_apply_ctx->l_to_g = l_to_g;
6639b072555Sjeremylt   if (bp_choice != CEED_BP1 && bp_choice != CEED_BP2) {
664d4d45553Srezgarshakeri     op_apply_ctx->l_to_g_0 = l_to_g_0;
665d4d45553Srezgarshakeri     op_apply_ctx->g_to_g_D = g_to_g_D;
666cb32e2e7SValeria Barra   }
667d4d45553Srezgarshakeri   op_apply_ctx->X_loc = X_loc;
668*2b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(X_loc, &op_apply_ctx->Y_loc));
669d4d45553Srezgarshakeri   CeedVectorCreate(ceed, l_size * num_comp_u, &op_apply_ctx->x_ceed);
670d4d45553Srezgarshakeri   CeedVectorCreate(ceed, l_size * num_comp_u, &op_apply_ctx->y_ceed);
671d4d45553Srezgarshakeri   op_apply_ctx->op     = op_apply;
672d4d45553Srezgarshakeri   op_apply_ctx->q_data = q_data;
673d4d45553Srezgarshakeri   op_apply_ctx->ceed   = ceed;
674cb32e2e7SValeria Barra 
675*2b730f8bSJeremy L Thompson   PetscCall(MatCreateShell(comm, m_nodes[0] * m_nodes[1] * m_nodes[2] * num_comp_u, m_nodes[0] * m_nodes[1] * m_nodes[2] * num_comp_u, PETSC_DECIDE,
676*2b730f8bSJeremy L Thompson                            PETSC_DECIDE, op_apply_ctx, &mat));
6779b072555Sjeremylt   if (bp_choice == CEED_BP1 || bp_choice == CEED_BP2) {
678*2b730f8bSJeremy L Thompson     PetscCall(MatShellSetOperation(mat, MATOP_MULT, (void (*)(void))MatMult_Mass));
679cb32e2e7SValeria Barra   } else {
680*2b730f8bSJeremy L Thompson     PetscCall(MatShellSetOperation(mat, MATOP_MULT, (void (*)(void))MatMult_Diff));
681cb32e2e7SValeria Barra   }
682*2b730f8bSJeremy L Thompson   PetscCall(VecGetType(X, &vec_type));
683*2b730f8bSJeremy L Thompson   PetscCall(MatShellSetVecType(mat, vec_type));
684cb32e2e7SValeria Barra 
685cb32e2e7SValeria Barra   // Get RHS vector
686*2b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(X, &rhs));
687*2b730f8bSJeremy L Thompson   PetscCall(VecDuplicate(X_loc, &rhs_loc));
688*2b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(rhs_loc));
689*2b730f8bSJeremy L Thompson   PetscCall(VecGetArrayAndMemType(rhs_loc, &r, &mem_type));
6909b072555Sjeremylt   CeedVectorSetArray(rhs_ceed, MemTypeP2C(mem_type), CEED_USE_POINTER, r);
691cb32e2e7SValeria Barra 
6929b072555Sjeremylt   // Setup q_data, rhs, and target
6939b072555Sjeremylt   CeedOperatorApply(op_setup_geo, x_coord, q_data, CEED_REQUEST_IMMEDIATE);
6949b072555Sjeremylt   CeedOperatorApply(op_setup_rhs, x_coord, rhs_ceed, CEED_REQUEST_IMMEDIATE);
6959b072555Sjeremylt   CeedVectorDestroy(&x_coord);
696cb32e2e7SValeria Barra 
697cb32e2e7SValeria Barra   // Gather RHS
698*2b730f8bSJeremy L Thompson   PetscCall(CeedVectorTakeArray(rhs_ceed, MemTypeP2C(mem_type), NULL));
699*2b730f8bSJeremy L Thompson   PetscCall(VecRestoreArrayAndMemType(rhs_loc, &r));
700*2b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(rhs));
701*2b730f8bSJeremy L Thompson   PetscCall(VecScatterBegin(l_to_g, rhs_loc, rhs, ADD_VALUES, SCATTER_FORWARD));
702*2b730f8bSJeremy L Thompson   PetscCall(VecScatterEnd(l_to_g, rhs_loc, rhs, ADD_VALUES, SCATTER_FORWARD));
7039b072555Sjeremylt   CeedVectorDestroy(&rhs_ceed);
704cb32e2e7SValeria Barra 
705*2b730f8bSJeremy L Thompson   PetscCall(KSPCreate(comm, &ksp));
706cb32e2e7SValeria Barra   {
707cb32e2e7SValeria Barra     PC pc;
708*2b730f8bSJeremy L Thompson     PetscCall(KSPGetPC(ksp, &pc));
7099b072555Sjeremylt     if (bp_choice == CEED_BP1 || bp_choice == CEED_BP2) {
710*2b730f8bSJeremy L Thompson       PetscCall(PCSetType(pc, PCJACOBI));
711*2b730f8bSJeremy L Thompson       PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM));
712cb32e2e7SValeria Barra     } else {
713*2b730f8bSJeremy L Thompson       PetscCall(PCSetType(pc, PCNONE));
714cb32e2e7SValeria Barra     }
715*2b730f8bSJeremy L Thompson     PetscCall(KSPSetType(ksp, KSPCG));
716*2b730f8bSJeremy L Thompson     PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL));
717*2b730f8bSJeremy L Thompson     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
718cb32e2e7SValeria Barra   }
719*2b730f8bSJeremy L Thompson   PetscCall(KSPSetOperators(ksp, mat, mat));
720da9108adSvaleriabarra   // First run's performance log is not considered for benchmarking purposes
721*2b730f8bSJeremy L Thompson   PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 1));
722cb32e2e7SValeria Barra   my_rt_start = MPI_Wtime();
723*2b730f8bSJeremy L Thompson   PetscCall(KSPSolve(ksp, rhs, X));
724cb32e2e7SValeria Barra   my_rt = MPI_Wtime() - my_rt_start;
725*2b730f8bSJeremy L Thompson   PetscCall(MPI_Allreduce(MPI_IN_PLACE, &my_rt, 1, MPI_DOUBLE, MPI_MIN, comm));
726cb32e2e7SValeria Barra   // Set maxits based on first iteration timing
727cb32e2e7SValeria Barra   if (my_rt > 0.02) {
728*2b730f8bSJeremy L Thompson     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, ksp_max_it_clip[0]));
729cb32e2e7SValeria Barra   } else {
730*2b730f8bSJeremy L Thompson     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, ksp_max_it_clip[1]));
731cb32e2e7SValeria Barra   }
732*2b730f8bSJeremy L Thompson   PetscCall(KSPSetFromOptions(ksp));
73309a940d7Sjeremylt 
734cb32e2e7SValeria Barra   // Timed solve
735*2b730f8bSJeremy L Thompson   PetscCall(VecZeroEntries(X));
736*2b730f8bSJeremy L Thompson   PetscCall(PetscBarrier((PetscObject)ksp));
73709a940d7Sjeremylt 
73809a940d7Sjeremylt   // -- Performance logging
739*2b730f8bSJeremy L Thompson   PetscCall(PetscLogStageRegister("Solve Stage", &solve_stage));
740*2b730f8bSJeremy L Thompson   PetscCall(PetscLogStagePush(solve_stage));
74109a940d7Sjeremylt 
74209a940d7Sjeremylt   // -- Solve
743cb32e2e7SValeria Barra   my_rt_start = MPI_Wtime();
744*2b730f8bSJeremy L Thompson   PetscCall(KSPSolve(ksp, rhs, X));
745cb32e2e7SValeria Barra   my_rt = MPI_Wtime() - my_rt_start;
74609a940d7Sjeremylt 
74709a940d7Sjeremylt   // -- Performance logging
748*2b730f8bSJeremy L Thompson   PetscCall(PetscLogStagePop());
74909a940d7Sjeremylt 
7508e87e98bSJed Brown   // Output results
751cb32e2e7SValeria Barra   {
7529b072555Sjeremylt     KSPType            ksp_type;
753cb32e2e7SValeria Barra     KSPConvergedReason reason;
754cb32e2e7SValeria Barra     PetscReal          rnorm;
755cb32e2e7SValeria Barra     PetscInt           its;
756*2b730f8bSJeremy L Thompson     PetscCall(KSPGetType(ksp, &ksp_type));
757*2b730f8bSJeremy L Thompson     PetscCall(KSPGetConvergedReason(ksp, &reason));
758*2b730f8bSJeremy L Thompson     PetscCall(KSPGetIterationNumber(ksp, &its));
759*2b730f8bSJeremy L Thompson     PetscCall(KSPGetResidualNorm(ksp, &rnorm));
760a22c4fb5SJed Brown     if (!test_mode || reason < 0 || rnorm > 1e-8) {
761*2b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm,
762cb32e2e7SValeria Barra                             "  KSP:\n"
763cb32e2e7SValeria Barra                             "    KSP Type                           : %s\n"
764cb32e2e7SValeria Barra                             "    KSP Convergence                    : %s\n"
76508140895SJed Brown                             "    Total KSP Iterations               : %" PetscInt_FMT "\n"
766cb32e2e7SValeria Barra                             "    Final rnorm                        : %e\n",
767*2b730f8bSJeremy L Thompson                             ksp_type, KSPConvergedReasons[reason], its, (double)rnorm));
768cb32e2e7SValeria Barra     }
7698e87e98bSJed Brown     if (!test_mode) {
770*2b730f8bSJeremy L Thompson       PetscCall(PetscPrintf(comm, "  Performance:\n"));
7718e87e98bSJed Brown     }
7728e87e98bSJed Brown     {
7739b072555Sjeremylt       PetscReal max_error;
774*2b730f8bSJeremy L Thompson       PetscCall(ComputeErrorMax(op_apply_ctx, op_error, X, target, &max_error));
7758e87e98bSJed Brown       PetscReal tol = 5e-2;
7769b072555Sjeremylt       if (!test_mode || max_error > tol) {
777*2b730f8bSJeremy L Thompson         PetscCall(MPI_Allreduce(&my_rt, &rt_min, 1, MPI_DOUBLE, MPI_MIN, comm));
778*2b730f8bSJeremy L Thompson         PetscCall(MPI_Allreduce(&my_rt, &rt_max, 1, MPI_DOUBLE, MPI_MAX, comm));
779*2b730f8bSJeremy L Thompson         PetscCall(PetscPrintf(comm,
7808e87e98bSJed Brown                               "    Pointwise Error (max)              : %e\n"
7818e87e98bSJed Brown                               "    CG Solve Time                      : %g (%g) sec\n",
782*2b730f8bSJeremy L Thompson                               (double)max_error, rt_max, rt_min));
783cb32e2e7SValeria Barra       }
784cb32e2e7SValeria Barra     }
7858d0bb2bbSvaleriabarra     if (!test_mode) {
786*2b730f8bSJeremy L Thompson       PetscCall(
787*2b730f8bSJeremy L Thompson           PetscPrintf(comm, "    DoFs/Sec in CG                     : %g (%g) million\n", 1e-6 * gsize * its / rt_max, 1e-6 * gsize * its / rt_min));
788cb32e2e7SValeria Barra     }
789cb32e2e7SValeria Barra   }
790cb32e2e7SValeria Barra 
791cb32e2e7SValeria Barra   if (write_solution) {
7929b072555Sjeremylt     PetscViewer vtk_viewer_soln;
793cb32e2e7SValeria Barra 
794*2b730f8bSJeremy L Thompson     PetscCall(PetscViewerCreate(comm, &vtk_viewer_soln));
795*2b730f8bSJeremy L Thompson     PetscCall(PetscViewerSetType(vtk_viewer_soln, PETSCVIEWERVTK));
796*2b730f8bSJeremy L Thompson     PetscCall(PetscViewerFileSetName(vtk_viewer_soln, "solution.vtu"));
797*2b730f8bSJeremy L Thompson     PetscCall(VecView(X, vtk_viewer_soln));
798*2b730f8bSJeremy L Thompson     PetscCall(PetscViewerDestroy(&vtk_viewer_soln));
799cb32e2e7SValeria Barra   }
800cb32e2e7SValeria Barra 
801*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&rhs));
802*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&rhs_loc));
803*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&X));
804*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&op_apply_ctx->X_loc));
805*2b730f8bSJeremy L Thompson   PetscCall(VecDestroy(&op_apply_ctx->Y_loc));
806*2b730f8bSJeremy L Thompson   PetscCall(VecScatterDestroy(&l_to_g));
807*2b730f8bSJeremy L Thompson   PetscCall(VecScatterDestroy(&l_to_g_0));
808*2b730f8bSJeremy L Thompson   PetscCall(VecScatterDestroy(&g_to_g_D));
809*2b730f8bSJeremy L Thompson   PetscCall(MatDestroy(&mat));
810*2b730f8bSJeremy L Thompson   PetscCall(KSPDestroy(&ksp));
811cb32e2e7SValeria Barra 
812d4d45553Srezgarshakeri   CeedVectorDestroy(&op_apply_ctx->x_ceed);
813d4d45553Srezgarshakeri   CeedVectorDestroy(&op_apply_ctx->y_ceed);
814d4d45553Srezgarshakeri   CeedVectorDestroy(&op_apply_ctx->q_data);
815cb32e2e7SValeria Barra   CeedVectorDestroy(&target);
8169b072555Sjeremylt   CeedOperatorDestroy(&op_setup_geo);
8179b072555Sjeremylt   CeedOperatorDestroy(&op_setup_rhs);
8189b072555Sjeremylt   CeedOperatorDestroy(&op_apply);
8199b072555Sjeremylt   CeedOperatorDestroy(&op_error);
8209b072555Sjeremylt   CeedElemRestrictionDestroy(&elem_restr_u);
8219b072555Sjeremylt   CeedElemRestrictionDestroy(&elem_restr_x);
8229b072555Sjeremylt   CeedElemRestrictionDestroy(&elem_restr_u_i);
8239b072555Sjeremylt   CeedElemRestrictionDestroy(&elem_restr_qd_i);
8249b072555Sjeremylt   CeedQFunctionDestroy(&qf_setup_geo);
8259b072555Sjeremylt   CeedQFunctionDestroy(&qf_setup_rhs);
8269b072555Sjeremylt   CeedQFunctionDestroy(&qf_apply);
8279b072555Sjeremylt   CeedQFunctionDestroy(&qf_error);
8289b072555Sjeremylt   CeedBasisDestroy(&basis_u);
8299b072555Sjeremylt   CeedBasisDestroy(&basis_x);
830cb32e2e7SValeria Barra   CeedDestroy(&ceed);
831*2b730f8bSJeremy L Thompson   PetscCall(PetscFree(op_apply_ctx));
832cb32e2e7SValeria Barra   return PetscFinalize();
833cb32e2e7SValeria Barra }
834