xref: /libCEED/rust/libceed-sys/c-src/backends/blocked/ceed-blocked-operator.c (revision c7b67790f45ae71043f73ea7aa7684f20189bfc2)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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 //------------------------------------------------------------------------------
198130dc29SSebastian Grimberg static int CeedOperatorSetupFields_Blocked(CeedQFunction qf, CeedOperator op, bool is_input, const CeedInt block_size,
208130dc29SSebastian Grimberg                                            CeedElemRestriction *block_rstr, CeedVector *e_vecs_full, CeedVector *e_vecs, CeedVector *q_vecs,
218130dc29SSebastian Grimberg                                            CeedInt start_e, CeedInt num_fields, CeedInt Q) {
22aedaa0e5Sjeremylt   Ceed                ceed;
23ad70ee2cSJeremy L Thompson   CeedSize            e_size, q_size;
24ad70ee2cSJeremy L Thompson   CeedInt             num_comp, size, P;
25d1d35e2fSjeremylt   CeedQFunctionField *qf_fields;
26ad70ee2cSJeremy L Thompson   CeedOperatorField  *op_fields;
27ad70ee2cSJeremy L Thompson 
28e910d748SJeremy L Thompson   {
29e910d748SJeremy L Thompson     Ceed ceed_parent;
30e910d748SJeremy L Thompson 
31ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
32e910d748SJeremy L Thompson     CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
33e910d748SJeremy L Thompson     if (ceed_parent) ceed = ceed_parent;
34e910d748SJeremy L Thompson   }
354fc1f125SJeremy L Thompson   if (is_input) {
362b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
372b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
384fc1f125SJeremy L Thompson   } else {
392b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
402b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
41fe2413ffSjeremylt   }
424a2e7687Sjeremylt 
434a2e7687Sjeremylt   // Loop over fields
444fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
45d1d35e2fSjeremylt     CeedEvalMode eval_mode;
46ad70ee2cSJeremy L Thompson     CeedBasis    basis;
47ad70ee2cSJeremy L Thompson 
482b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
49d1d35e2fSjeremylt     if (eval_mode != CEED_EVAL_WEIGHT) {
5020a93772SSebastian Grimberg       Ceed                ceed_rstr;
51e79b91d9SJeremy L Thompson       CeedSize            l_size;
52e79b91d9SJeremy L Thompson       CeedInt             num_elem, elem_size, comp_stride;
53fcbe8c06SSebastian Grimberg       CeedRestrictionType rstr_type;
54ad70ee2cSJeremy L Thompson       CeedElemRestriction rstr;
55ad70ee2cSJeremy L Thompson 
56ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr));
57ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed_rstr));
58ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem));
59ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size));
60ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
61ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
62ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetCompStride(rstr, &comp_stride));
63ad70ee2cSJeremy L Thompson 
64ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetType(rstr, &rstr_type));
65fcbe8c06SSebastian Grimberg       switch (rstr_type) {
6661a27d74SSebastian Grimberg         case CEED_RESTRICTION_STANDARD: {
670305e208SSebastian Grimberg           const CeedInt *offsets = NULL;
68ad70ee2cSJeremy L Thompson 
69ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
70ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionCreateBlocked(ceed_rstr, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, CEED_MEM_HOST,
71edb2538eSJeremy L Thompson                                                            CEED_COPY_VALUES, offsets, &block_rstr[i + start_e]));
72ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
73fcbe8c06SSebastian Grimberg         } break;
74fcbe8c06SSebastian Grimberg         case CEED_RESTRICTION_ORIENTED: {
75fcbe8c06SSebastian Grimberg           const bool    *orients = NULL;
76ad70ee2cSJeremy L Thompson           const CeedInt *offsets = NULL;
77ad70ee2cSJeremy L Thompson 
78ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
79ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetOrientations(rstr, CEED_MEM_HOST, &orients));
80ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionCreateBlockedOriented(ceed_rstr, num_elem, elem_size, block_size, num_comp, comp_stride, l_size,
81edb2538eSJeremy L Thompson                                                                    CEED_MEM_HOST, CEED_COPY_VALUES, offsets, orients, &block_rstr[i + start_e]));
82ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
83ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr, &orients));
84fcbe8c06SSebastian Grimberg         } break;
85fcbe8c06SSebastian Grimberg         case CEED_RESTRICTION_CURL_ORIENTED: {
860c73c039SSebastian Grimberg           const CeedInt8 *curl_orients = NULL;
87ad70ee2cSJeremy L Thompson           const CeedInt  *offsets      = NULL;
88ad70ee2cSJeremy L Thompson 
89ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
90ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr, CEED_MEM_HOST, &curl_orients));
91ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionCreateBlockedCurlOriented(ceed_rstr, num_elem, elem_size, block_size, num_comp, comp_stride, l_size,
92fcbe8c06SSebastian Grimberg                                                                        CEED_MEM_HOST, CEED_COPY_VALUES, offsets, curl_orients,
93edb2538eSJeremy L Thompson                                                                        &block_rstr[i + start_e]));
94ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
95ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr, &curl_orients));
96fcbe8c06SSebastian Grimberg         } break;
97fcbe8c06SSebastian Grimberg         case CEED_RESTRICTION_STRIDED: {
983ac43b2cSJeremy L Thompson           CeedInt strides[3];
99ad70ee2cSJeremy L Thompson 
10056c48462SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetStrides(rstr, strides));
101ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionCreateBlockedStrided(ceed_rstr, num_elem, elem_size, block_size, num_comp, l_size, strides,
102edb2538eSJeremy L Thompson                                                                   &block_rstr[i + start_e]));
103fcbe8c06SSebastian Grimberg         } break;
1042c7e7413SJeremy L Thompson         case CEED_RESTRICTION_POINTS:
1052c7e7413SJeremy L Thompson           // Empty case - won't occur
1062c7e7413SJeremy L Thompson           break;
10777d1c127SSebastian Grimberg       }
108edb2538eSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionCreateVector(block_rstr[i + start_e], NULL, &e_vecs_full[i + start_e]));
1094a2e7687Sjeremylt     }
1104a2e7687Sjeremylt 
111d1d35e2fSjeremylt     switch (eval_mode) {
1124a2e7687Sjeremylt       case CEED_EVAL_NONE:
1132b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
114ad70ee2cSJeremy L Thompson         q_size = (CeedSize)Q * size * block_size;
1152b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
116aedaa0e5Sjeremylt         break;
117aedaa0e5Sjeremylt       case CEED_EVAL_INTERP:
1184a2e7687Sjeremylt       case CEED_EVAL_GRAD:
119a915a514Srezgarshakeri       case CEED_EVAL_DIV:
120c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
1212b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
1222b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
1232b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetNumNodes(basis, &P));
1242b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
125ad70ee2cSJeremy L Thompson         e_size = (CeedSize)P * num_comp * block_size;
1262b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, e_size, &e_vecs[i]));
127ad70ee2cSJeremy L Thompson         q_size = (CeedSize)Q * size * block_size;
1282b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1294a2e7687Sjeremylt         break;
1304a2e7687Sjeremylt       case CEED_EVAL_WEIGHT:  // Only on input fields
1312b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
132ad70ee2cSJeremy L Thompson         q_size = (CeedSize)Q * block_size;
1332b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
134ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, block_size, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
1354a2e7687Sjeremylt         break;
1364a2e7687Sjeremylt     }
1374a2e7687Sjeremylt   }
138e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1394a2e7687Sjeremylt }
1404a2e7687Sjeremylt 
141f10650afSjeremylt //------------------------------------------------------------------------------
142f10650afSjeremylt // Setup Operator
143f10650afSjeremylt //------------------------------------------------------------------------------
1444a2e7687Sjeremylt static int CeedOperatorSetup_Blocked(CeedOperator op) {
1458c1105f8SJeremy L Thompson   bool                  is_setup_done;
146ad70ee2cSJeremy L Thompson   Ceed                  ceed;
147ad70ee2cSJeremy L Thompson   CeedInt               Q, num_input_fields, num_output_fields;
1488130dc29SSebastian Grimberg   const CeedInt         block_size = 8;
149ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
150ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
151ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
152ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
153ad70ee2cSJeremy L Thompson 
1542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
1558c1105f8SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
156ad70ee2cSJeremy L Thompson 
1572b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1582b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1592b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
1612b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionIsIdentity(qf, &impl->is_identity_qf));
1622b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1632b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1644a2e7687Sjeremylt 
1654a2e7687Sjeremylt   // Allocate
166edb2538eSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->block_rstr));
1672b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs_full));
1684a2e7687Sjeremylt 
1692b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
1702b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->e_vecs_in));
1712b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->e_vecs_out));
1722b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
1732b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
1744a2e7687Sjeremylt 
1754fc1f125SJeremy L Thompson   impl->num_inputs  = num_input_fields;
1764fc1f125SJeremy L Thompson   impl->num_outputs = num_output_fields;
177aedaa0e5Sjeremylt 
1784a2e7687Sjeremylt   // Set up infield and outfield pointer arrays
1794a2e7687Sjeremylt   // Infields
1808130dc29SSebastian Grimberg   CeedCallBackend(CeedOperatorSetupFields_Blocked(qf, op, true, block_size, impl->block_rstr, impl->e_vecs_full, impl->e_vecs_in, impl->q_vecs_in, 0,
1818130dc29SSebastian Grimberg                                                   num_input_fields, Q));
1824a2e7687Sjeremylt   // Outfields
1838130dc29SSebastian Grimberg   CeedCallBackend(CeedOperatorSetupFields_Blocked(qf, op, false, block_size, impl->block_rstr, impl->e_vecs_full, impl->e_vecs_out, impl->q_vecs_out,
1842b730f8bSJeremy L Thompson                                                   num_input_fields, num_output_fields, Q));
185aedaa0e5Sjeremylt 
18616911fdaSjeremylt   // Identity QFunctions
1870b454692Sjeremylt   if (impl->is_identity_qf) {
188d1d35e2fSjeremylt     CeedEvalMode        in_mode, out_mode;
189d1d35e2fSjeremylt     CeedQFunctionField *in_fields, *out_fields;
190ad70ee2cSJeremy L Thompson 
1912b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &in_fields, NULL, &out_fields));
1922b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(in_fields[0], &in_mode));
1932b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(out_fields[0], &out_mode));
194d1d35e2fSjeremylt 
1950b454692Sjeremylt     if (in_mode == CEED_EVAL_NONE && out_mode == CEED_EVAL_NONE) {
196edb2538eSJeremy L Thompson       impl->is_identity_rstr_op = true;
1970b454692Sjeremylt     } else {
198db002c03SJeremy L Thompson       CeedCallBackend(CeedVectorReferenceCopy(impl->q_vecs_in[0], &impl->q_vecs_out[0]));
19916911fdaSjeremylt     }
20016911fdaSjeremylt   }
20116911fdaSjeremylt 
2022b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
203e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2044a2e7687Sjeremylt }
2054a2e7687Sjeremylt 
206f10650afSjeremylt //------------------------------------------------------------------------------
207f10650afSjeremylt // Setup Operator Inputs
208f10650afSjeremylt //------------------------------------------------------------------------------
2092b730f8bSJeremy L Thompson static inline int CeedOperatorSetupInputs_Blocked(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
2104fc1f125SJeremy L Thompson                                                   CeedVector in_vec, bool skip_active, CeedScalar *e_data_full[2 * CEED_FIELD_MAX],
211a0162de9SJeremy L Thompson                                                   CeedOperator_Blocked *impl, CeedRequest *request) {
212ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
213ad70ee2cSJeremy L Thompson     uint64_t     state;
214d1d35e2fSjeremylt     CeedEvalMode eval_mode;
215d1bcdac9Sjeremylt     CeedVector   vec;
2164a2e7687Sjeremylt 
2171d102b48SJeremy L Thompson     // Get input vector
2182b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2191d102b48SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2202b730f8bSJeremy L Thompson       if (skip_active) continue;
2212b730f8bSJeremy L Thompson       else vec = in_vec;
2221d102b48SJeremy L Thompson     }
2231d102b48SJeremy L Thompson 
2242b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
225d1d35e2fSjeremylt     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2264a2e7687Sjeremylt     } else {
2274a2e7687Sjeremylt       // Restrict
2282b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetState(vec, &state));
2294fc1f125SJeremy L Thompson       if (state != impl->input_states[i] || vec == in_vec) {
230edb2538eSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[i], CEED_NOTRANSPOSE, vec, impl->e_vecs_full[i], request));
2314fc1f125SJeremy L Thompson         impl->input_states[i] = state;
23216c359e6Sjeremylt       }
2334a2e7687Sjeremylt       // Get evec
2342b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs_full[i], CEED_MEM_HOST, (const CeedScalar **)&e_data_full[i]));
2354a2e7687Sjeremylt     }
2364a2e7687Sjeremylt   }
237e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2384a2e7687Sjeremylt }
2394a2e7687Sjeremylt 
240f10650afSjeremylt //------------------------------------------------------------------------------
241f10650afSjeremylt // Input Basis Action
242f10650afSjeremylt //------------------------------------------------------------------------------
2432b730f8bSJeremy L Thompson static inline int CeedOperatorInputBasis_Blocked(CeedInt e, CeedInt Q, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
244ad70ee2cSJeremy L Thompson                                                  CeedInt num_input_fields, CeedInt block_size, bool skip_active,
2454fc1f125SJeremy L Thompson                                                  CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
246ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
247a915a514Srezgarshakeri     CeedInt             elem_size, size, num_comp;
248d1d35e2fSjeremylt     CeedEvalMode        eval_mode;
249edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2501d102b48SJeremy L Thompson     CeedBasis           basis;
2511d102b48SJeremy L Thompson 
2521d102b48SJeremy L Thompson     // Skip active input
253d1d35e2fSjeremylt     if (skip_active) {
2541d102b48SJeremy L Thompson       CeedVector vec;
255ad70ee2cSJeremy L Thompson 
2562b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2572b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2581d102b48SJeremy L Thompson     }
2591d102b48SJeremy L Thompson 
260d1d35e2fSjeremylt     // Get elem_size, eval_mode, size
261edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
262edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
2632b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
2642b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
2654a2e7687Sjeremylt     // Basis action
266d1d35e2fSjeremylt     switch (eval_mode) {
2674a2e7687Sjeremylt       case CEED_EVAL_NONE:
26881670346SSebastian Grimberg         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i][(CeedSize)e * Q * size]));
2694a2e7687Sjeremylt         break;
2704a2e7687Sjeremylt       case CEED_EVAL_INTERP:
2714a2e7687Sjeremylt       case CEED_EVAL_GRAD:
272a915a514Srezgarshakeri       case CEED_EVAL_DIV:
273c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
274a915a514Srezgarshakeri         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
275a915a514Srezgarshakeri         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
27681670346SSebastian Grimberg         CeedCallBackend(CeedVectorSetArray(impl->e_vecs_in[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i][(CeedSize)e * elem_size * num_comp]));
277ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, block_size, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs_in[i], impl->q_vecs_in[i]));
278a915a514Srezgarshakeri         break;
2794a2e7687Sjeremylt       case CEED_EVAL_WEIGHT:
2804a2e7687Sjeremylt         break;  // No action
2814a2e7687Sjeremylt     }
28289c6efa4Sjeremylt   }
283e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
28489c6efa4Sjeremylt }
2854a2e7687Sjeremylt 
286f10650afSjeremylt //------------------------------------------------------------------------------
287f10650afSjeremylt // Output Basis Action
288f10650afSjeremylt //------------------------------------------------------------------------------
2892b730f8bSJeremy L Thompson static inline int CeedOperatorOutputBasis_Blocked(CeedInt e, CeedInt Q, CeedQFunctionField *qf_output_fields, CeedOperatorField *op_output_fields,
290ad70ee2cSJeremy L Thompson                                                   CeedInt block_size, CeedInt num_input_fields, CeedInt num_output_fields, CeedOperator op,
2912b730f8bSJeremy L Thompson                                                   CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
292ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
293a915a514Srezgarshakeri     CeedInt             elem_size, num_comp;
294d1d35e2fSjeremylt     CeedEvalMode        eval_mode;
295edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2961d102b48SJeremy L Thompson     CeedBasis           basis;
2971d102b48SJeremy L Thompson 
298d1d35e2fSjeremylt     // Get elem_size, eval_mode, size
299edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
300edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
3012b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
3024a2e7687Sjeremylt     // Basis action
303d1d35e2fSjeremylt     switch (eval_mode) {
3044a2e7687Sjeremylt       case CEED_EVAL_NONE:
3054a2e7687Sjeremylt         break;  // No action
3064a2e7687Sjeremylt       case CEED_EVAL_INTERP:
3074a2e7687Sjeremylt       case CEED_EVAL_GRAD:
308a915a514Srezgarshakeri       case CEED_EVAL_DIV:
309c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
310a915a514Srezgarshakeri         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
311a915a514Srezgarshakeri         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
31281670346SSebastian Grimberg         CeedCallBackend(CeedVectorSetArray(impl->e_vecs_out[i], CEED_MEM_HOST, CEED_USE_POINTER,
31381670346SSebastian Grimberg                                            &e_data_full[i + num_input_fields][(CeedSize)e * elem_size * num_comp]));
314ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, block_size, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs_out[i]));
315a915a514Srezgarshakeri         break;
316c042f62fSJeremy L Thompson       // LCOV_EXCL_START
317bbfacfcdSjeremylt       case CEED_EVAL_WEIGHT: {
3186e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
319bbfacfcdSjeremylt         // LCOV_EXCL_STOP
3204a2e7687Sjeremylt       }
32189c6efa4Sjeremylt     }
32289c6efa4Sjeremylt   }
323e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3241d102b48SJeremy L Thompson }
3251d102b48SJeremy L Thompson 
326f10650afSjeremylt //------------------------------------------------------------------------------
327f10650afSjeremylt // Restore Input Vectors
328f10650afSjeremylt //------------------------------------------------------------------------------
3292b730f8bSJeremy L Thompson static inline int CeedOperatorRestoreInputs_Blocked(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
3302b730f8bSJeremy L Thompson                                                     bool skip_active, CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
331ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
332d1d35e2fSjeremylt     CeedEvalMode eval_mode;
3331d102b48SJeremy L Thompson 
3341d102b48SJeremy L Thompson     // Skip active inputs
335d1d35e2fSjeremylt     if (skip_active) {
3361d102b48SJeremy L Thompson       CeedVector vec;
337ad70ee2cSJeremy L Thompson 
3382b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3392b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3401d102b48SJeremy L Thompson     }
3412b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
342d1d35e2fSjeremylt     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3431d102b48SJeremy L Thompson     } else {
3442b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs_full[i], (const CeedScalar **)&e_data_full[i]));
3451d102b48SJeremy L Thompson     }
3461d102b48SJeremy L Thompson   }
347e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3481d102b48SJeremy L Thompson }
3491d102b48SJeremy L Thompson 
350f10650afSjeremylt //------------------------------------------------------------------------------
351f10650afSjeremylt // Operator Apply
352f10650afSjeremylt //------------------------------------------------------------------------------
3532b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Blocked(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
354d1d35e2fSjeremylt   CeedInt               Q, num_input_fields, num_output_fields, num_elem, size;
355ad70ee2cSJeremy L Thompson   const CeedInt         block_size = 8;
356ad70ee2cSJeremy L Thompson   CeedEvalMode          eval_mode;
357ad70ee2cSJeremy L Thompson   CeedScalar           *e_data_full[2 * CEED_FIELD_MAX] = {0};
358ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
359ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
360ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
361ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
362ad70ee2cSJeremy L Thompson 
363ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3642b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
3652b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
3662b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3672b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
3682b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
369ad70ee2cSJeremy L Thompson   const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size);
3701d102b48SJeremy L Thompson 
3711d102b48SJeremy L Thompson   // Setup
3722b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Blocked(op));
3731d102b48SJeremy L Thompson 
3740b454692Sjeremylt   // Restriction only operator
375edb2538eSJeremy L Thompson   if (impl->is_identity_rstr_op) {
376edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[0], CEED_NOTRANSPOSE, in_vec, impl->e_vecs_full[0], request));
377edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[1], CEED_TRANSPOSE, impl->e_vecs_full[0], out_vec, request));
3780b454692Sjeremylt     return CEED_ERROR_SUCCESS;
3790b454692Sjeremylt   }
3800b454692Sjeremylt 
3811d102b48SJeremy L Thompson   // Input Evecs and Restriction
3822b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data_full, impl, request));
3831d102b48SJeremy L Thompson 
3841d102b48SJeremy L Thompson   // Output Evecs
385d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_output_fields; i++) {
3862b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs_full[i + impl->num_inputs], CEED_MEM_HOST, &e_data_full[i + num_input_fields]));
3871d102b48SJeremy L Thompson   }
3881d102b48SJeremy L Thompson 
3891d102b48SJeremy L Thompson   // Loop through elements
390ad70ee2cSJeremy L Thompson   for (CeedInt e = 0; e < num_blocks * block_size; e += block_size) {
3911d102b48SJeremy L Thompson     // Output pointers
392d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_output_fields; i++) {
3932b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
394d1d35e2fSjeremylt       if (eval_mode == CEED_EVAL_NONE) {
3952b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
39681670346SSebastian Grimberg         CeedCallBackend(
39781670346SSebastian Grimberg             CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i + num_input_fields][(CeedSize)e * Q * size]));
3981d102b48SJeremy L Thompson       }
3991d102b48SJeremy L Thompson     }
4001d102b48SJeremy L Thompson 
40116911fdaSjeremylt     // Input basis apply
402ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, false, e_data_full, impl));
40316911fdaSjeremylt 
4041d102b48SJeremy L Thompson     // Q function
4050b454692Sjeremylt     if (!impl->is_identity_qf) {
406ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
40716911fdaSjeremylt     }
4081d102b48SJeremy L Thompson 
4091d102b48SJeremy L Thompson     // Output basis apply
410ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorOutputBasis_Blocked(e, Q, qf_output_fields, op_output_fields, block_size, num_input_fields, num_output_fields, op,
4112b730f8bSJeremy L Thompson                                                     e_data_full, impl));
4121d102b48SJeremy L Thompson   }
41389c6efa4Sjeremylt 
41489c6efa4Sjeremylt   // Output restriction
415d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_output_fields; i++) {
416ad70ee2cSJeremy L Thompson     CeedVector vec;
417ad70ee2cSJeremy L Thompson 
41889c6efa4Sjeremylt     // Restore evec
4192b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs_full[i + impl->num_inputs], &e_data_full[i + num_input_fields]));
420d1bcdac9Sjeremylt     // Get output vector
4212b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
42289c6efa4Sjeremylt     // Active
4232b730f8bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4244a2e7687Sjeremylt     // Restrict
4252b730f8bSJeremy L Thompson     CeedCallBackend(
426edb2538eSJeremy L Thompson         CeedElemRestrictionApply(impl->block_rstr[i + impl->num_inputs], CEED_TRANSPOSE, impl->e_vecs_full[i + impl->num_inputs], vec, request));
4274a2e7687Sjeremylt   }
4284a2e7687Sjeremylt 
4294a2e7687Sjeremylt   // Restore input arrays
4302b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, false, e_data_full, impl));
431e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4321d102b48SJeremy L Thompson }
4331d102b48SJeremy L Thompson 
434f10650afSjeremylt //------------------------------------------------------------------------------
43570a7ffb3SJeremy L Thompson // Core code for assembling linear QFunction
436f10650afSjeremylt //------------------------------------------------------------------------------
4372b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Blocked(CeedOperator op, bool build_objects, CeedVector *assembled,
438a0162de9SJeremy L Thompson                                                                   CeedElemRestriction *rstr, CeedRequest *request) {
439ad70ee2cSJeremy L Thompson   Ceed                  ceed;
440*c7b67790SJeremy L Thompson   CeedInt               num_active_in, num_active_out, Q, num_input_fields, num_output_fields, num_elem;
441ad70ee2cSJeremy L Thompson   const CeedInt         block_size = 8;
442ad70ee2cSJeremy L Thompson   CeedScalar           *l_vec_array;
443ad70ee2cSJeremy L Thompson   CeedScalar           *e_data_full[2 * CEED_FIELD_MAX] = {0};
444ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
445ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
446ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
447ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
448ad70ee2cSJeremy L Thompson 
449ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
450*c7b67790SJeremy L Thompson   num_active_in                  = impl->num_active_in;
451*c7b67790SJeremy L Thompson   num_active_out                 = impl->num_active_in;
452ad70ee2cSJeremy L Thompson   CeedVector          l_vec      = impl->qf_l_vec;
453ad70ee2cSJeremy L Thompson   CeedElemRestriction block_rstr = impl->qf_block_rstr;
454ad70ee2cSJeremy L Thompson 
4552b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
4562b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
4572b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4582b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
4592b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
461ad70ee2cSJeremy L Thompson   const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size);
4621d102b48SJeremy L Thompson 
4631d102b48SJeremy L Thompson   // Setup
4642b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Blocked(op));
4651d102b48SJeremy L Thompson 
466506b1a0cSSebastian Grimberg   // Check for restriction only operator
467506b1a0cSSebastian Grimberg   CeedCheck(!impl->is_identity_rstr_op, ceed, CEED_ERROR_BACKEND, "Assembling restriction only operators is not supported");
46816911fdaSjeremylt 
4691d102b48SJeremy L Thompson   // Input Evecs and Restriction
4702b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data_full, impl, request));
4711d102b48SJeremy L Thompson 
4721d102b48SJeremy L Thompson   // Count number of active input fields
473*c7b67790SJeremy L Thompson   if (num_active_in == 0) {
474d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_input_fields; i++) {
475*c7b67790SJeremy L Thompson       CeedInt    field_size;
476ad70ee2cSJeremy L Thompson       CeedVector vec;
477ad70ee2cSJeremy L Thompson 
4781d102b48SJeremy L Thompson       // Get input vector
4792b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4801d102b48SJeremy L Thompson       // Check if active input
4811d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
482*c7b67790SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &field_size));
4832b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
484*c7b67790SJeremy L Thompson         num_active_in += field_size;
4851d102b48SJeremy L Thompson       }
4861d102b48SJeremy L Thompson     }
487*c7b67790SJeremy L Thompson     CeedCheck(num_active_in > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
4884fc1f125SJeremy L Thompson     impl->num_active_in = num_active_in;
489bb219a0fSJeremy L Thompson   }
4901d102b48SJeremy L Thompson 
4911d102b48SJeremy L Thompson   // Count number of active output fields
492*c7b67790SJeremy L Thompson   if (num_active_out == 0) {
493d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_output_fields; i++) {
494*c7b67790SJeremy L Thompson       CeedInt    field_size;
495ad70ee2cSJeremy L Thompson       CeedVector vec;
496ad70ee2cSJeremy L Thompson 
4971d102b48SJeremy L Thompson       // Get output vector
4982b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4991d102b48SJeremy L Thompson       // Check if active output
5001d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
501*c7b67790SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &field_size));
502*c7b67790SJeremy L Thompson         num_active_out += field_size;
5031d102b48SJeremy L Thompson       }
5041d102b48SJeremy L Thompson     }
505*c7b67790SJeremy L Thompson     CeedCheck(num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5064fc1f125SJeremy L Thompson     impl->num_active_out = num_active_out;
507bb219a0fSJeremy L Thompson   }
5081d102b48SJeremy L Thompson 
509d1d35e2fSjeremylt   // Setup Lvec
5104fc1f125SJeremy L Thompson   if (!l_vec) {
511ad70ee2cSJeremy L Thompson     const CeedSize l_size = (CeedSize)num_blocks * block_size * Q * num_active_in * num_active_out;
512ad70ee2cSJeremy L Thompson 
5132b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, &l_vec));
5144fc1f125SJeremy L Thompson     impl->qf_l_vec = l_vec;
515bb219a0fSJeremy L Thompson   }
516ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(l_vec, CEED_MEM_HOST, &l_vec_array));
517ad70ee2cSJeremy L Thompson 
518ad70ee2cSJeremy L Thompson   // Setup block restriction
519ad70ee2cSJeremy L Thompson   if (!block_rstr) {
520ad70ee2cSJeremy L Thompson     const CeedInt strides[3] = {1, Q, num_active_in * num_active_out * Q};
521ad70ee2cSJeremy L Thompson 
522ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateBlockedStrided(ceed, num_elem, Q, block_size, num_active_in * num_active_out,
523ad70ee2cSJeremy L Thompson                                                             num_active_in * num_active_out * num_elem * Q, strides, &block_rstr));
524ad70ee2cSJeremy L Thompson     impl->qf_block_rstr = block_rstr;
525ad70ee2cSJeremy L Thompson   }
5261d102b48SJeremy L Thompson 
52770a7ffb3SJeremy L Thompson   // Build objects if needed
52870a7ffb3SJeremy L Thompson   if (build_objects) {
529ad70ee2cSJeremy L Thompson     const CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
530ad70ee2cSJeremy L Thompson     const CeedInt  strides[3] = {1, Q, num_active_in * num_active_out * Q};
531ad70ee2cSJeremy L Thompson 
53270a7ffb3SJeremy L Thompson     // Create output restriction
5332b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed, num_elem, Q, num_active_in * num_active_out, num_active_in * num_active_out * num_elem * Q,
5342b730f8bSJeremy L Thompson                                                      strides, rstr));
5351d102b48SJeremy L Thompson     // Create assembled vector
5362b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, assembled));
53770a7ffb3SJeremy L Thompson   }
5381d102b48SJeremy L Thompson 
5391d102b48SJeremy L Thompson   // Loop through elements
540ad70ee2cSJeremy L Thompson   for (CeedInt e = 0; e < num_blocks * block_size; e += block_size) {
5411d102b48SJeremy L Thompson     // Input basis apply
542ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, true, e_data_full, impl));
5431d102b48SJeremy L Thompson 
5441d102b48SJeremy L Thompson     // Assemble QFunction
545*c7b67790SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
546*c7b67790SJeremy L Thompson       CeedInt    field_size;
547*c7b67790SJeremy L Thompson       CeedVector vec;
548*c7b67790SJeremy L Thompson 
549*c7b67790SJeremy L Thompson       // Get input vector
550*c7b67790SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
551*c7b67790SJeremy L Thompson       // Check if active input
552*c7b67790SJeremy L Thompson       if (vec != CEED_VECTOR_ACTIVE) continue;
553*c7b67790SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &field_size));
554*c7b67790SJeremy L Thompson       for (CeedInt field = 0; field < field_size; field++) {
555*c7b67790SJeremy L Thompson         // Set current portion of input to 1.0
556*c7b67790SJeremy L Thompson         {
557*c7b67790SJeremy L Thompson           CeedScalar *array;
558*c7b67790SJeremy L Thompson 
559*c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_HOST, &array));
560*c7b67790SJeremy L Thompson           for (CeedInt j = 0; j < Q * block_size; j++) array[field * Q * block_size + j] = 1.0;
561*c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &array));
56242ea3801Sjeremylt         }
563*c7b67790SJeremy L Thompson 
564506b1a0cSSebastian Grimberg         if (!impl->is_identity_qf) {
5651d102b48SJeremy L Thompson           // Set Outputs
566d1d35e2fSjeremylt           for (CeedInt out = 0; out < num_output_fields; out++) {
567*c7b67790SJeremy L Thompson             CeedInt    field_size;
568ad70ee2cSJeremy L Thompson             CeedVector vec;
569ad70ee2cSJeremy L Thompson 
5701d102b48SJeremy L Thompson             // Get output vector
5712b730f8bSJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5721d102b48SJeremy L Thompson             // Check if active output
5731d102b48SJeremy L Thompson             if (vec == CEED_VECTOR_ACTIVE) {
574ad70ee2cSJeremy L Thompson               CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_HOST, CEED_USE_POINTER, l_vec_array));
575*c7b67790SJeremy L Thompson               CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &field_size));
576*c7b67790SJeremy L Thompson               l_vec_array += field_size * Q * block_size;  // Advance the pointer by the size of the output
5771d102b48SJeremy L Thompson             }
5781d102b48SJeremy L Thompson           }
5791d102b48SJeremy L Thompson           // Apply QFunction
580ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
581506b1a0cSSebastian Grimberg         } else {
582*c7b67790SJeremy L Thompson           CeedInt           field_size;
583*c7b67790SJeremy L Thompson           const CeedScalar *array;
584506b1a0cSSebastian Grimberg 
585506b1a0cSSebastian Grimberg           // Copy Identity Outputs
586*c7b67790SJeremy L Thompson           CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[0], &field_size));
587*c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayRead(impl->q_vecs_out[0], CEED_MEM_HOST, &array));
588*c7b67790SJeremy L Thompson           for (CeedInt j = 0; j < field_size * Q * block_size; j++) l_vec_array[j] = array[j];
589*c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorRestoreArrayRead(impl->q_vecs_out[0], &array));
590*c7b67790SJeremy L Thompson           l_vec_array += field_size * Q * block_size;
591*c7b67790SJeremy L Thompson         }
592*c7b67790SJeremy L Thompson         // Reset input to 0.0
593*c7b67790SJeremy L Thompson         {
594*c7b67790SJeremy L Thompson           CeedScalar *array;
595*c7b67790SJeremy L Thompson 
596*c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_HOST, &array));
597*c7b67790SJeremy L Thompson           for (CeedInt j = 0; j < Q * block_size; j++) array[field * Q * block_size + j] = 0.0;
598*c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &array));
599*c7b67790SJeremy L Thompson         }
600506b1a0cSSebastian Grimberg       }
6014a2e7687Sjeremylt     }
6024a2e7687Sjeremylt   }
6034a2e7687Sjeremylt 
6041d102b48SJeremy L Thompson   // Un-set output Qvecs to prevent accidental overwrite of Assembled
605506b1a0cSSebastian Grimberg   if (!impl->is_identity_qf) {
606d1d35e2fSjeremylt     for (CeedInt out = 0; out < num_output_fields; out++) {
607ad70ee2cSJeremy L Thompson       CeedVector vec;
608ad70ee2cSJeremy L Thompson 
6091d102b48SJeremy L Thompson       // Get output vector
6102b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
6111d102b48SJeremy L Thompson       // Check if active output
6121d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
613506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_HOST, NULL));
614506b1a0cSSebastian Grimberg       }
6151d102b48SJeremy L Thompson     }
6161d102b48SJeremy L Thompson   }
6171d102b48SJeremy L Thompson 
6181d102b48SJeremy L Thompson   // Restore input arrays
6192b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, true, e_data_full, impl));
6201d102b48SJeremy L Thompson 
6211d102b48SJeremy L Thompson   // Output blocked restriction
622ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(l_vec, &l_vec_array));
6232b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
624ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(block_rstr, CEED_TRANSPOSE, l_vec, *assembled, request));
625e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6264a2e7687Sjeremylt }
6274a2e7687Sjeremylt 
628f10650afSjeremylt //------------------------------------------------------------------------------
62970a7ffb3SJeremy L Thompson // Assemble Linear QFunction
63070a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6312b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Blocked(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
6322b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, true, assembled, rstr, request);
63370a7ffb3SJeremy L Thompson }
63470a7ffb3SJeremy L Thompson 
63570a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
63670a7ffb3SJeremy L Thompson // Update Assembled Linear QFunction
63770a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6382b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Blocked(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
6392b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, false, &assembled, &rstr, request);
64070a7ffb3SJeremy L Thompson }
64170a7ffb3SJeremy L Thompson 
64270a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
643f10650afSjeremylt // Operator Destroy
644f10650afSjeremylt //------------------------------------------------------------------------------
645f10650afSjeremylt static int CeedOperatorDestroy_Blocked(CeedOperator op) {
646f10650afSjeremylt   CeedOperator_Blocked *impl;
647ad70ee2cSJeremy L Thompson 
6482b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
649f10650afSjeremylt 
6504fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
651edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->block_rstr[i]));
6522b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_full[i]));
653f10650afSjeremylt   }
654edb2538eSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->block_rstr));
6552b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_full));
6562b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
657f10650afSjeremylt 
6584fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
6592b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_in[i]));
6602b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
661f10650afSjeremylt   }
6622b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_in));
6632b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
664f10650afSjeremylt 
6654fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
6662b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_out[i]));
6672b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
668f10650afSjeremylt   }
6692b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_out));
6702b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
671f10650afSjeremylt 
672bb219a0fSJeremy L Thompson   // QFunction assembly data
6732b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->qf_l_vec));
674ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&impl->qf_block_rstr));
675bb219a0fSJeremy L Thompson 
6762b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
677e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
678f10650afSjeremylt }
679f10650afSjeremylt 
680f10650afSjeremylt //------------------------------------------------------------------------------
681f10650afSjeremylt // Operator Create
682f10650afSjeremylt //------------------------------------------------------------------------------
6834a2e7687Sjeremylt int CeedOperatorCreate_Blocked(CeedOperator op) {
684fe2413ffSjeremylt   Ceed                  ceed;
6854ce2993fSjeremylt   CeedOperator_Blocked *impl;
6864a2e7687Sjeremylt 
687ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
6882b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
6892b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
6902b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Blocked));
6912b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Blocked));
6922b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Blocked));
6932b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Blocked));
694e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6954a2e7687Sjeremylt }
6962a86cc9dSSebastian Grimberg 
697f10650afSjeremylt //------------------------------------------------------------------------------
698