xref: /libCEED/rust/libceed-sys/c-src/backends/blocked/ceed-blocked-operator.c (revision 2c7e74134068de5221afb7e7255d8033016bfd12)
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.
34a2e7687Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
54a2e7687Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
74a2e7687Sjeremylt 
849aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d576824SJeremy L Thompson #include <stdbool.h>
113d576824SJeremy L Thompson #include <stddef.h>
123d576824SJeremy L Thompson #include <stdint.h>
132b730f8bSJeremy L Thompson 
144a2e7687Sjeremylt #include "ceed-blocked.h"
154a2e7687Sjeremylt 
16f10650afSjeremylt //------------------------------------------------------------------------------
17f10650afSjeremylt // Setup Input/Output Fields
18f10650afSjeremylt //------------------------------------------------------------------------------
19edb2538eSJeremy L Thompson static int CeedOperatorSetupFields_Blocked(CeedQFunction qf, CeedOperator op, bool is_input, CeedElemRestriction *block_rstr, CeedVector *e_vecs_full,
20edb2538eSJeremy L Thompson                                            CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e, CeedInt num_fields, CeedInt Q) {
21aedaa0e5Sjeremylt   Ceed                ceed;
22ad70ee2cSJeremy L Thompson   CeedSize            e_size, q_size;
23ad70ee2cSJeremy L Thompson   CeedInt             num_comp, size, P;
24ad70ee2cSJeremy L Thompson   const CeedInt       block_size = 8;
25d1d35e2fSjeremylt   CeedQFunctionField *qf_fields;
26ad70ee2cSJeremy L Thompson   CeedOperatorField  *op_fields;
27ad70ee2cSJeremy L Thompson 
28ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
29ad70ee2cSJeremy L Thompson 
304fc1f125SJeremy L Thompson   if (is_input) {
312b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
322b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
334fc1f125SJeremy L Thompson   } else {
342b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
352b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
36fe2413ffSjeremylt   }
374a2e7687Sjeremylt 
384a2e7687Sjeremylt   // Loop over fields
394fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
40d1d35e2fSjeremylt     CeedEvalMode eval_mode;
41ad70ee2cSJeremy L Thompson     CeedBasis    basis;
42ad70ee2cSJeremy L Thompson 
432b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
444a2e7687Sjeremylt 
45d1d35e2fSjeremylt     if (eval_mode != CEED_EVAL_WEIGHT) {
4620a93772SSebastian Grimberg       Ceed                ceed_rstr;
47e79b91d9SJeremy L Thompson       CeedSize            l_size;
48e79b91d9SJeremy L Thompson       CeedInt             num_elem, elem_size, comp_stride;
49fcbe8c06SSebastian Grimberg       CeedRestrictionType rstr_type;
50ad70ee2cSJeremy L Thompson       CeedElemRestriction rstr;
51ad70ee2cSJeremy L Thompson 
52ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
53ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed_rstr));
54ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
55ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
56ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
57ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
58ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
59ad70ee2cSJeremy L Thompson 
60ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
61fcbe8c06SSebastian Grimberg       switch (rstr_type) {
6261a27d74SSebastian Grimberg         case CEED_RESTRICTION_STANDARD: {
630305e208SSebastian Grimberg           const CeedInt *offsets = NULL;
64ad70ee2cSJeremy L Thompson 
65ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
66ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionCreateBlocked(ceed_rstr, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, CEED_MEM_HOST,
67edb2538eSJeremy L Thompson                                                            CEED_COPY_VALUES, offsets, &block_rstr[i + start_e]));
68ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
69fcbe8c06SSebastian Grimberg         } break;
70fcbe8c06SSebastian Grimberg         case CEED_RESTRICTION_ORIENTED: {
71fcbe8c06SSebastian Grimberg           const bool    *orients = NULL;
72ad70ee2cSJeremy L Thompson           const CeedInt *offsets = NULL;
73ad70ee2cSJeremy L Thompson 
74ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
75ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetOrientations(rstr, CEED_MEM_HOST, &orients));
76ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionCreateBlockedOriented(ceed_rstr, num_elem, elem_size, block_size, num_comp, comp_stride, l_size,
77edb2538eSJeremy L Thompson                                                                    CEED_MEM_HOST, CEED_COPY_VALUES, offsets, orients, &block_rstr[i + start_e]));
78ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
79ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr, &orients));
80fcbe8c06SSebastian Grimberg         } break;
81fcbe8c06SSebastian Grimberg         case CEED_RESTRICTION_CURL_ORIENTED: {
820c73c039SSebastian Grimberg           const CeedInt8 *curl_orients = NULL;
83ad70ee2cSJeremy L Thompson           const CeedInt  *offsets      = NULL;
84ad70ee2cSJeremy L Thompson 
85ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
86ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr, CEED_MEM_HOST, &curl_orients));
87ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionCreateBlockedCurlOriented(ceed_rstr, num_elem, elem_size, block_size, num_comp, comp_stride, l_size,
88fcbe8c06SSebastian Grimberg                                                                        CEED_MEM_HOST, CEED_COPY_VALUES, offsets, curl_orients,
89edb2538eSJeremy L Thompson                                                                        &block_rstr[i + start_e]));
90ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
91ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr, &curl_orients));
92fcbe8c06SSebastian Grimberg         } break;
93fcbe8c06SSebastian Grimberg         case CEED_RESTRICTION_STRIDED: {
943ac43b2cSJeremy L Thompson           CeedInt strides[3];
95ad70ee2cSJeremy L Thompson 
96ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetStrides(rstr, &strides));
97ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionCreateBlockedStrided(ceed_rstr, num_elem, elem_size, block_size, num_comp, l_size, strides,
98edb2538eSJeremy L Thompson                                                                   &block_rstr[i + start_e]));
99fcbe8c06SSebastian Grimberg         } break;
100*2c7e7413SJeremy L Thompson         case CEED_RESTRICTION_POINTS:
101*2c7e7413SJeremy L Thompson           // Empty case - won't occur
102*2c7e7413SJeremy L Thompson           break;
10377d1c127SSebastian Grimberg       }
104edb2538eSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionCreateVector(block_rstr[i + start_e], NULL, &e_vecs_full[i + start_e]));
1054a2e7687Sjeremylt     }
1064a2e7687Sjeremylt 
107d1d35e2fSjeremylt     switch (eval_mode) {
1084a2e7687Sjeremylt       case CEED_EVAL_NONE:
1092b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
110ad70ee2cSJeremy L Thompson         q_size = (CeedSize)Q * size * block_size;
1112b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
112aedaa0e5Sjeremylt         break;
113aedaa0e5Sjeremylt       case CEED_EVAL_INTERP:
1144a2e7687Sjeremylt       case CEED_EVAL_GRAD:
115a915a514Srezgarshakeri       case CEED_EVAL_DIV:
116c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
1172b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
1182b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
1192b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetNumNodes(basis, &P));
1202b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
121ad70ee2cSJeremy L Thompson         e_size = (CeedSize)P * num_comp * block_size;
1222b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, e_size, &e_vecs[i]));
123ad70ee2cSJeremy L Thompson         q_size = (CeedSize)Q * size * block_size;
1242b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1254a2e7687Sjeremylt         break;
1264a2e7687Sjeremylt       case CEED_EVAL_WEIGHT:  // Only on input fields
1272b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
128ad70ee2cSJeremy L Thompson         q_size = (CeedSize)Q * block_size;
1292b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
130ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, block_size, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
1314a2e7687Sjeremylt         break;
1324a2e7687Sjeremylt     }
1334a2e7687Sjeremylt   }
134e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1354a2e7687Sjeremylt }
1364a2e7687Sjeremylt 
137f10650afSjeremylt //------------------------------------------------------------------------------
138f10650afSjeremylt // Setup Operator
139f10650afSjeremylt //------------------------------------------------------------------------------
1404a2e7687Sjeremylt static int CeedOperatorSetup_Blocked(CeedOperator op) {
1418c1105f8SJeremy L Thompson   bool                  is_setup_done;
142ad70ee2cSJeremy L Thompson   Ceed                  ceed;
143ad70ee2cSJeremy L Thompson   CeedInt               Q, num_input_fields, num_output_fields;
144ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
145ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
146ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
147ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
148ad70ee2cSJeremy L Thompson 
1492b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
1508c1105f8SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
151ad70ee2cSJeremy L Thompson 
1522b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1532b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1552b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
1562b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionIsIdentity(qf, &impl->is_identity_qf));
1572b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1582b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1594a2e7687Sjeremylt 
1604a2e7687Sjeremylt   // Allocate
161edb2538eSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->block_rstr));
1622b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs_full));
1634a2e7687Sjeremylt 
1642b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
1652b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->e_vecs_in));
1662b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->e_vecs_out));
1672b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
1682b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
1694a2e7687Sjeremylt 
1704fc1f125SJeremy L Thompson   impl->num_inputs  = num_input_fields;
1714fc1f125SJeremy L Thompson   impl->num_outputs = num_output_fields;
172aedaa0e5Sjeremylt 
1734a2e7687Sjeremylt   // Set up infield and outfield pointer arrays
1744a2e7687Sjeremylt   // Infields
1752b730f8bSJeremy L Thompson   CeedCallBackend(
176edb2538eSJeremy L Thompson       CeedOperatorSetupFields_Blocked(qf, op, true, impl->block_rstr, impl->e_vecs_full, impl->e_vecs_in, impl->q_vecs_in, 0, num_input_fields, Q));
1774a2e7687Sjeremylt   // Outfields
178edb2538eSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Blocked(qf, op, false, impl->block_rstr, impl->e_vecs_full, impl->e_vecs_out, impl->q_vecs_out,
1792b730f8bSJeremy L Thompson                                                   num_input_fields, num_output_fields, Q));
180aedaa0e5Sjeremylt 
18116911fdaSjeremylt   // Identity QFunctions
1820b454692Sjeremylt   if (impl->is_identity_qf) {
183d1d35e2fSjeremylt     CeedEvalMode        in_mode, out_mode;
184d1d35e2fSjeremylt     CeedQFunctionField *in_fields, *out_fields;
185ad70ee2cSJeremy L Thompson 
1862b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &in_fields, NULL, &out_fields));
1872b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(in_fields[0], &in_mode));
1882b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(out_fields[0], &out_mode));
189d1d35e2fSjeremylt 
1900b454692Sjeremylt     if (in_mode == CEED_EVAL_NONE && out_mode == CEED_EVAL_NONE) {
191edb2538eSJeremy L Thompson       impl->is_identity_rstr_op = true;
1920b454692Sjeremylt     } else {
193db002c03SJeremy L Thompson       CeedCallBackend(CeedVectorReferenceCopy(impl->q_vecs_in[0], &impl->q_vecs_out[0]));
19416911fdaSjeremylt     }
19516911fdaSjeremylt   }
19616911fdaSjeremylt 
1972b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
198e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1994a2e7687Sjeremylt }
2004a2e7687Sjeremylt 
201f10650afSjeremylt //------------------------------------------------------------------------------
202f10650afSjeremylt // Setup Operator Inputs
203f10650afSjeremylt //------------------------------------------------------------------------------
2042b730f8bSJeremy L Thompson static inline int CeedOperatorSetupInputs_Blocked(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
2054fc1f125SJeremy L Thompson                                                   CeedVector in_vec, bool skip_active, CeedScalar *e_data_full[2 * CEED_FIELD_MAX],
206a0162de9SJeremy L Thompson                                                   CeedOperator_Blocked *impl, CeedRequest *request) {
207ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
208ad70ee2cSJeremy L Thompson     uint64_t     state;
209d1d35e2fSjeremylt     CeedEvalMode eval_mode;
210d1bcdac9Sjeremylt     CeedVector   vec;
2114a2e7687Sjeremylt 
2121d102b48SJeremy L Thompson     // Get input vector
2132b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2141d102b48SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2152b730f8bSJeremy L Thompson       if (skip_active) continue;
2162b730f8bSJeremy L Thompson       else vec = in_vec;
2171d102b48SJeremy L Thompson     }
2181d102b48SJeremy L Thompson 
2192b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
220d1d35e2fSjeremylt     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2214a2e7687Sjeremylt     } else {
2224a2e7687Sjeremylt       // Restrict
2232b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetState(vec, &state));
2244fc1f125SJeremy L Thompson       if (state != impl->input_states[i] || vec == in_vec) {
225edb2538eSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[i], CEED_NOTRANSPOSE, vec, impl->e_vecs_full[i], request));
2264fc1f125SJeremy L Thompson         impl->input_states[i] = state;
22716c359e6Sjeremylt       }
2284a2e7687Sjeremylt       // Get evec
2292b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs_full[i], CEED_MEM_HOST, (const CeedScalar **)&e_data_full[i]));
2304a2e7687Sjeremylt     }
2314a2e7687Sjeremylt   }
232e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2334a2e7687Sjeremylt }
2344a2e7687Sjeremylt 
235f10650afSjeremylt //------------------------------------------------------------------------------
236f10650afSjeremylt // Input Basis Action
237f10650afSjeremylt //------------------------------------------------------------------------------
2382b730f8bSJeremy L Thompson static inline int CeedOperatorInputBasis_Blocked(CeedInt e, CeedInt Q, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
239ad70ee2cSJeremy L Thompson                                                  CeedInt num_input_fields, CeedInt block_size, bool skip_active,
2404fc1f125SJeremy L Thompson                                                  CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
241ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
242a915a514Srezgarshakeri     CeedInt             elem_size, size, num_comp;
243d1d35e2fSjeremylt     CeedEvalMode        eval_mode;
244edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2451d102b48SJeremy L Thompson     CeedBasis           basis;
2461d102b48SJeremy L Thompson 
2471d102b48SJeremy L Thompson     // Skip active input
248d1d35e2fSjeremylt     if (skip_active) {
2491d102b48SJeremy L Thompson       CeedVector vec;
250ad70ee2cSJeremy L Thompson 
2512b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2522b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2531d102b48SJeremy L Thompson     }
2541d102b48SJeremy L Thompson 
255d1d35e2fSjeremylt     // Get elem_size, eval_mode, size
256edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
257edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
2582b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
2592b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
2604a2e7687Sjeremylt     // Basis action
261d1d35e2fSjeremylt     switch (eval_mode) {
2624a2e7687Sjeremylt       case CEED_EVAL_NONE:
2632b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i][e * Q * size]));
2644a2e7687Sjeremylt         break;
2654a2e7687Sjeremylt       case CEED_EVAL_INTERP:
2664a2e7687Sjeremylt       case CEED_EVAL_GRAD:
267a915a514Srezgarshakeri       case CEED_EVAL_DIV:
268c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
269a915a514Srezgarshakeri         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
270a915a514Srezgarshakeri         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
271a915a514Srezgarshakeri         CeedCallBackend(CeedVectorSetArray(impl->e_vecs_in[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i][e * elem_size * num_comp]));
272ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, block_size, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs_in[i], impl->q_vecs_in[i]));
273a915a514Srezgarshakeri         break;
2744a2e7687Sjeremylt       case CEED_EVAL_WEIGHT:
2754a2e7687Sjeremylt         break;  // No action
2764a2e7687Sjeremylt     }
27789c6efa4Sjeremylt   }
278e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
27989c6efa4Sjeremylt }
2804a2e7687Sjeremylt 
281f10650afSjeremylt //------------------------------------------------------------------------------
282f10650afSjeremylt // Output Basis Action
283f10650afSjeremylt //------------------------------------------------------------------------------
2842b730f8bSJeremy L Thompson static inline int CeedOperatorOutputBasis_Blocked(CeedInt e, CeedInt Q, CeedQFunctionField *qf_output_fields, CeedOperatorField *op_output_fields,
285ad70ee2cSJeremy L Thompson                                                   CeedInt block_size, CeedInt num_input_fields, CeedInt num_output_fields, CeedOperator op,
2862b730f8bSJeremy L Thompson                                                   CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
287ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
288a915a514Srezgarshakeri     CeedInt             elem_size, num_comp;
289d1d35e2fSjeremylt     CeedEvalMode        eval_mode;
290edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2911d102b48SJeremy L Thompson     CeedBasis           basis;
2921d102b48SJeremy L Thompson 
293d1d35e2fSjeremylt     // Get elem_size, eval_mode, size
294edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
295edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
2962b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
2974a2e7687Sjeremylt     // Basis action
298d1d35e2fSjeremylt     switch (eval_mode) {
2994a2e7687Sjeremylt       case CEED_EVAL_NONE:
3004a2e7687Sjeremylt         break;  // No action
3014a2e7687Sjeremylt       case CEED_EVAL_INTERP:
3024a2e7687Sjeremylt       case CEED_EVAL_GRAD:
303a915a514Srezgarshakeri       case CEED_EVAL_DIV:
304c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
305a915a514Srezgarshakeri         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
306a915a514Srezgarshakeri         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
307a915a514Srezgarshakeri         CeedCallBackend(
308a915a514Srezgarshakeri             CeedVectorSetArray(impl->e_vecs_out[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i + num_input_fields][e * elem_size * num_comp]));
309ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, block_size, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs_out[i]));
310a915a514Srezgarshakeri         break;
311c042f62fSJeremy L Thompson       // LCOV_EXCL_START
312bbfacfcdSjeremylt       case CEED_EVAL_WEIGHT: {
3134ce2993fSjeremylt         Ceed ceed;
3142b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
3152b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
316bbfacfcdSjeremylt         // LCOV_EXCL_STOP
3174a2e7687Sjeremylt       }
31889c6efa4Sjeremylt     }
31989c6efa4Sjeremylt   }
320e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3211d102b48SJeremy L Thompson }
3221d102b48SJeremy L Thompson 
323f10650afSjeremylt //------------------------------------------------------------------------------
324f10650afSjeremylt // Restore Input Vectors
325f10650afSjeremylt //------------------------------------------------------------------------------
3262b730f8bSJeremy L Thompson static inline int CeedOperatorRestoreInputs_Blocked(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
3272b730f8bSJeremy L Thompson                                                     bool skip_active, CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
328ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
329d1d35e2fSjeremylt     CeedEvalMode eval_mode;
3301d102b48SJeremy L Thompson 
3311d102b48SJeremy L Thompson     // Skip active inputs
332d1d35e2fSjeremylt     if (skip_active) {
3331d102b48SJeremy L Thompson       CeedVector vec;
334ad70ee2cSJeremy L Thompson 
3352b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3362b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3371d102b48SJeremy L Thompson     }
3382b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
339d1d35e2fSjeremylt     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3401d102b48SJeremy L Thompson     } else {
3412b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs_full[i], (const CeedScalar **)&e_data_full[i]));
3421d102b48SJeremy L Thompson     }
3431d102b48SJeremy L Thompson   }
344e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3451d102b48SJeremy L Thompson }
3461d102b48SJeremy L Thompson 
347f10650afSjeremylt //------------------------------------------------------------------------------
348f10650afSjeremylt // Operator Apply
349f10650afSjeremylt //------------------------------------------------------------------------------
3502b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Blocked(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
351d1d35e2fSjeremylt   CeedInt               Q, num_input_fields, num_output_fields, num_elem, size;
352ad70ee2cSJeremy L Thompson   const CeedInt         block_size = 8;
353ad70ee2cSJeremy L Thompson   CeedEvalMode          eval_mode;
354ad70ee2cSJeremy L Thompson   CeedScalar           *e_data_full[2 * CEED_FIELD_MAX] = {0};
355ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
356ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
357ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
358ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
359ad70ee2cSJeremy L Thompson 
360ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3612b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
3622b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
3632b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3642b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
3652b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
366ad70ee2cSJeremy L Thompson   const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size);
3671d102b48SJeremy L Thompson 
3681d102b48SJeremy L Thompson   // Setup
3692b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Blocked(op));
3701d102b48SJeremy L Thompson 
3710b454692Sjeremylt   // Restriction only operator
372edb2538eSJeremy L Thompson   if (impl->is_identity_rstr_op) {
373edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[0], CEED_NOTRANSPOSE, in_vec, impl->e_vecs_full[0], request));
374edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[1], CEED_TRANSPOSE, impl->e_vecs_full[0], out_vec, request));
3750b454692Sjeremylt     return CEED_ERROR_SUCCESS;
3760b454692Sjeremylt   }
3770b454692Sjeremylt 
3781d102b48SJeremy L Thompson   // Input Evecs and Restriction
3792b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data_full, impl, request));
3801d102b48SJeremy L Thompson 
3811d102b48SJeremy L Thompson   // Output Evecs
382d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_output_fields; i++) {
3832b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs_full[i + impl->num_inputs], CEED_MEM_HOST, &e_data_full[i + num_input_fields]));
3841d102b48SJeremy L Thompson   }
3851d102b48SJeremy L Thompson 
3861d102b48SJeremy L Thompson   // Loop through elements
387ad70ee2cSJeremy L Thompson   for (CeedInt e = 0; e < num_blocks * block_size; e += block_size) {
3881d102b48SJeremy L Thompson     // Output pointers
389d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_output_fields; i++) {
3902b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
391d1d35e2fSjeremylt       if (eval_mode == CEED_EVAL_NONE) {
3922b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
3932b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i + num_input_fields][e * Q * size]));
3941d102b48SJeremy L Thompson       }
3951d102b48SJeremy L Thompson     }
3961d102b48SJeremy L Thompson 
39716911fdaSjeremylt     // Input basis apply
398ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, false, e_data_full, impl));
39916911fdaSjeremylt 
4001d102b48SJeremy L Thompson     // Q function
4010b454692Sjeremylt     if (!impl->is_identity_qf) {
402ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
40316911fdaSjeremylt     }
4041d102b48SJeremy L Thompson 
4051d102b48SJeremy L Thompson     // Output basis apply
406ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorOutputBasis_Blocked(e, Q, qf_output_fields, op_output_fields, block_size, num_input_fields, num_output_fields, op,
4072b730f8bSJeremy L Thompson                                                     e_data_full, impl));
4081d102b48SJeremy L Thompson   }
40989c6efa4Sjeremylt 
41089c6efa4Sjeremylt   // Output restriction
411d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_output_fields; i++) {
412ad70ee2cSJeremy L Thompson     CeedVector vec;
413ad70ee2cSJeremy L Thompson 
41489c6efa4Sjeremylt     // Restore evec
4152b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs_full[i + impl->num_inputs], &e_data_full[i + num_input_fields]));
416d1bcdac9Sjeremylt     // Get output vector
4172b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
41889c6efa4Sjeremylt     // Active
4192b730f8bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4204a2e7687Sjeremylt     // Restrict
4212b730f8bSJeremy L Thompson     CeedCallBackend(
422edb2538eSJeremy L Thompson         CeedElemRestrictionApply(impl->block_rstr[i + impl->num_inputs], CEED_TRANSPOSE, impl->e_vecs_full[i + impl->num_inputs], vec, request));
4234a2e7687Sjeremylt   }
4244a2e7687Sjeremylt 
4254a2e7687Sjeremylt   // Restore input arrays
4262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, false, e_data_full, impl));
427e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4281d102b48SJeremy L Thompson }
4291d102b48SJeremy L Thompson 
430f10650afSjeremylt //------------------------------------------------------------------------------
43170a7ffb3SJeremy L Thompson // Core code for assembling linear QFunction
432f10650afSjeremylt //------------------------------------------------------------------------------
4332b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Blocked(CeedOperator op, bool build_objects, CeedVector *assembled,
434a0162de9SJeremy L Thompson                                                                   CeedElemRestriction *rstr, CeedRequest *request) {
435ad70ee2cSJeremy L Thompson   Ceed                  ceed;
436ad70ee2cSJeremy L Thompson   CeedSize              q_size;
437d1d35e2fSjeremylt   CeedInt               Q, num_input_fields, num_output_fields, num_elem, size;
438ad70ee2cSJeremy L Thompson   const CeedInt         block_size = 8;
439ad70ee2cSJeremy L Thompson   CeedScalar           *l_vec_array;
440ad70ee2cSJeremy L Thompson   CeedScalar           *e_data_full[2 * CEED_FIELD_MAX] = {0};
441ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
442ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
443ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
444ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
445ad70ee2cSJeremy L Thompson 
446ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
447ad70ee2cSJeremy L Thompson   CeedInt             num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
448ad70ee2cSJeremy L Thompson   CeedVector         *active_in  = impl->qf_active_in;
449ad70ee2cSJeremy L Thompson   CeedVector          l_vec      = impl->qf_l_vec;
450ad70ee2cSJeremy L Thompson   CeedElemRestriction block_rstr = impl->qf_block_rstr;
451ad70ee2cSJeremy L Thompson 
4522b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
4532b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
4542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4552b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
4562b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4572b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
458ad70ee2cSJeremy L Thompson   const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size);
4591d102b48SJeremy L Thompson 
4601d102b48SJeremy L Thompson   // Setup
4612b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Blocked(op));
4621d102b48SJeremy L Thompson 
46316911fdaSjeremylt   // Check for identity
4646574a04fSJeremy L Thompson   CeedCheck(!impl->is_identity_qf, ceed, CEED_ERROR_BACKEND, "Assembling identity QFunctions not supported");
46516911fdaSjeremylt 
4661d102b48SJeremy L Thompson   // Input Evecs and Restriction
4672b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data_full, impl, request));
4681d102b48SJeremy L Thompson 
4691d102b48SJeremy L Thompson   // Count number of active input fields
470bb219a0fSJeremy L Thompson   if (!num_active_in) {
471d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_input_fields; i++) {
472ad70ee2cSJeremy L Thompson       CeedScalar *q_vec_array;
473ad70ee2cSJeremy L Thompson       CeedVector  vec;
474ad70ee2cSJeremy L Thompson 
4751d102b48SJeremy L Thompson       // Get input vector
4762b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4771d102b48SJeremy L Thompson       // Check if active input
4781d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
4792b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
4802b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
481ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_HOST, &q_vec_array));
4822b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_in));
4831d102b48SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
484ad70ee2cSJeremy L Thompson           q_size = (CeedSize)Q * block_size;
4852b730f8bSJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_in[num_active_in + field]));
486ad70ee2cSJeremy L Thompson           CeedCallBackend(
487ad70ee2cSJeremy L Thompson               CeedVectorSetArray(active_in[num_active_in + field], CEED_MEM_HOST, CEED_USE_POINTER, &q_vec_array[field * Q * block_size]));
4881d102b48SJeremy L Thompson         }
489d1d35e2fSjeremylt         num_active_in += size;
490ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
4911d102b48SJeremy L Thompson       }
4921d102b48SJeremy L Thompson     }
4934fc1f125SJeremy L Thompson     impl->num_active_in = num_active_in;
494bb219a0fSJeremy L Thompson     impl->qf_active_in  = active_in;
495bb219a0fSJeremy L Thompson   }
4961d102b48SJeremy L Thompson 
4971d102b48SJeremy L Thompson   // Count number of active output fields
498bb219a0fSJeremy L Thompson   if (!num_active_out) {
499d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_output_fields; i++) {
500ad70ee2cSJeremy L Thompson       CeedVector vec;
501ad70ee2cSJeremy L Thompson 
5021d102b48SJeremy L Thompson       // Get output vector
5032b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5041d102b48SJeremy L Thompson       // Check if active output
5051d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
5062b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
507d1d35e2fSjeremylt         num_active_out += size;
5081d102b48SJeremy L Thompson       }
5091d102b48SJeremy L Thompson     }
5104fc1f125SJeremy L Thompson     impl->num_active_out = num_active_out;
511bb219a0fSJeremy L Thompson   }
5121d102b48SJeremy L Thompson 
5131d102b48SJeremy L Thompson   // Check sizes
5146574a04fSJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5151d102b48SJeremy L Thompson 
516d1d35e2fSjeremylt   // Setup Lvec
5174fc1f125SJeremy L Thompson   if (!l_vec) {
518ad70ee2cSJeremy L Thompson     const CeedSize l_size = (CeedSize)num_blocks * block_size * Q * num_active_in * num_active_out;
519ad70ee2cSJeremy L Thompson 
5202b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, &l_vec));
5214fc1f125SJeremy L Thompson     impl->qf_l_vec = l_vec;
522bb219a0fSJeremy L Thompson   }
523ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(l_vec, CEED_MEM_HOST, &l_vec_array));
524ad70ee2cSJeremy L Thompson 
525ad70ee2cSJeremy L Thompson   // Setup block restriction
526ad70ee2cSJeremy L Thompson   if (!block_rstr) {
527ad70ee2cSJeremy L Thompson     const CeedInt strides[3] = {1, Q, num_active_in * num_active_out * Q};
528ad70ee2cSJeremy L Thompson 
529ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateBlockedStrided(ceed, num_elem, Q, block_size, num_active_in * num_active_out,
530ad70ee2cSJeremy L Thompson                                                             num_active_in * num_active_out * num_elem * Q, strides, &block_rstr));
531ad70ee2cSJeremy L Thompson     impl->qf_block_rstr = block_rstr;
532ad70ee2cSJeremy L Thompson   }
5331d102b48SJeremy L Thompson 
53470a7ffb3SJeremy L Thompson   // Build objects if needed
53570a7ffb3SJeremy L Thompson   if (build_objects) {
536ad70ee2cSJeremy L Thompson     const CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
537ad70ee2cSJeremy L Thompson     const CeedInt  strides[3] = {1, Q, num_active_in * num_active_out * Q};
538ad70ee2cSJeremy L Thompson 
53970a7ffb3SJeremy L Thompson     // Create output restriction
5402b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed, num_elem, Q, num_active_in * num_active_out, num_active_in * num_active_out * num_elem * Q,
5412b730f8bSJeremy L Thompson                                                      strides, rstr));
5421d102b48SJeremy L Thompson     // Create assembled vector
5432b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, assembled));
54470a7ffb3SJeremy L Thompson   }
5451d102b48SJeremy L Thompson 
5461d102b48SJeremy L Thompson   // Loop through elements
547ad70ee2cSJeremy L Thompson   for (CeedInt e = 0; e < num_blocks * block_size; e += block_size) {
5481d102b48SJeremy L Thompson     // Input basis apply
549ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, true, e_data_full, impl));
5501d102b48SJeremy L Thompson 
5511d102b48SJeremy L Thompson     // Assemble QFunction
552d1d35e2fSjeremylt     for (CeedInt in = 0; in < num_active_in; in++) {
5531d102b48SJeremy L Thompson       // Set Inputs
5542b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_in[in], 1.0));
555d1d35e2fSjeremylt       if (num_active_in > 1) {
5562b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(active_in[(in + num_active_in - 1) % num_active_in], 0.0));
55742ea3801Sjeremylt       }
5581d102b48SJeremy L Thompson       // Set Outputs
559d1d35e2fSjeremylt       for (CeedInt out = 0; out < num_output_fields; out++) {
560ad70ee2cSJeremy L Thompson         CeedVector vec;
561ad70ee2cSJeremy L Thompson 
5621d102b48SJeremy L Thompson         // Get output vector
5632b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5641d102b48SJeremy L Thompson         // Check if active output
5651d102b48SJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
566ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_HOST, CEED_USE_POINTER, l_vec_array));
5672b730f8bSJeremy L Thompson           CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
568ad70ee2cSJeremy L Thompson           l_vec_array += size * Q * block_size;  // Advance the pointer by the size of the output
5691d102b48SJeremy L Thompson         }
5701d102b48SJeremy L Thompson       }
5711d102b48SJeremy L Thompson       // Apply QFunction
572ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
5734a2e7687Sjeremylt     }
5744a2e7687Sjeremylt   }
5754a2e7687Sjeremylt 
5761d102b48SJeremy L Thompson   // Un-set output Qvecs to prevent accidental overwrite of Assembled
577d1d35e2fSjeremylt   for (CeedInt out = 0; out < num_output_fields; out++) {
578ad70ee2cSJeremy L Thompson     CeedVector vec;
579ad70ee2cSJeremy L Thompson 
5801d102b48SJeremy L Thompson     // Get output vector
5812b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5821d102b48SJeremy L Thompson     // Check if active output
5831d102b48SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
5842b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_HOST, CEED_COPY_VALUES, NULL));
5851d102b48SJeremy L Thompson     }
5861d102b48SJeremy L Thompson   }
5871d102b48SJeremy L Thompson 
5881d102b48SJeremy L Thompson   // Restore input arrays
5892b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, true, e_data_full, impl));
5901d102b48SJeremy L Thompson 
5911d102b48SJeremy L Thompson   // Output blocked restriction
592ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(l_vec, &l_vec_array));
5932b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
594ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(block_rstr, CEED_TRANSPOSE, l_vec, *assembled, request));
595e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5964a2e7687Sjeremylt }
5974a2e7687Sjeremylt 
598f10650afSjeremylt //------------------------------------------------------------------------------
59970a7ffb3SJeremy L Thompson // Assemble Linear QFunction
60070a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6012b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Blocked(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
6022b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, true, assembled, rstr, request);
60370a7ffb3SJeremy L Thompson }
60470a7ffb3SJeremy L Thompson 
60570a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
60670a7ffb3SJeremy L Thompson // Update Assembled Linear QFunction
60770a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6082b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Blocked(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
6092b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, false, &assembled, &rstr, request);
61070a7ffb3SJeremy L Thompson }
61170a7ffb3SJeremy L Thompson 
61270a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
613f10650afSjeremylt // Operator Destroy
614f10650afSjeremylt //------------------------------------------------------------------------------
615f10650afSjeremylt static int CeedOperatorDestroy_Blocked(CeedOperator op) {
616f10650afSjeremylt   CeedOperator_Blocked *impl;
617ad70ee2cSJeremy L Thompson 
6182b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
619f10650afSjeremylt 
6204fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
621edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->block_rstr[i]));
6222b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_full[i]));
623f10650afSjeremylt   }
624edb2538eSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->block_rstr));
6252b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_full));
6262b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
627f10650afSjeremylt 
6284fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
6292b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_in[i]));
6302b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
631f10650afSjeremylt   }
6322b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_in));
6332b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
634f10650afSjeremylt 
6354fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
6362b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_out[i]));
6372b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
638f10650afSjeremylt   }
6392b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_out));
6402b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
641f10650afSjeremylt 
642bb219a0fSJeremy L Thompson   // QFunction assembly data
6434fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
6442b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
645bb219a0fSJeremy L Thompson   }
6462b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
6472b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->qf_l_vec));
648ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&impl->qf_block_rstr));
649bb219a0fSJeremy L Thompson 
6502b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
651e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
652f10650afSjeremylt }
653f10650afSjeremylt 
654f10650afSjeremylt //------------------------------------------------------------------------------
655f10650afSjeremylt // Operator Create
656f10650afSjeremylt //------------------------------------------------------------------------------
6574a2e7687Sjeremylt int CeedOperatorCreate_Blocked(CeedOperator op) {
658fe2413ffSjeremylt   Ceed                  ceed;
6594ce2993fSjeremylt   CeedOperator_Blocked *impl;
6604a2e7687Sjeremylt 
661ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
6622b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
6632b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
6642b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Blocked));
6652b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Blocked));
6662b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Blocked));
6672b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Blocked));
668e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6694a2e7687Sjeremylt }
6702a86cc9dSSebastian Grimberg 
671f10650afSjeremylt //------------------------------------------------------------------------------
672