xref: /libCEED/rust/libceed-sys/c-src/backends/blocked/ceed-blocked-operator.c (revision 6e536b992ff6bc401c55631f1bc4464446496b52)
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 //------------------------------------------------------------------------------
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:
2682b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i][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));
276a915a514Srezgarshakeri         CeedCallBackend(CeedVectorSetArray(impl->e_vecs_in[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i][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));
312a915a514Srezgarshakeri         CeedCallBackend(
313a915a514Srezgarshakeri             CeedVectorSetArray(impl->e_vecs_out[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i + num_input_fields][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: {
318*6e536b99SJeremy 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));
3962b730f8bSJeremy 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]));
3971d102b48SJeremy L Thompson       }
3981d102b48SJeremy L Thompson     }
3991d102b48SJeremy L Thompson 
40016911fdaSjeremylt     // Input basis apply
401ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, false, e_data_full, impl));
40216911fdaSjeremylt 
4031d102b48SJeremy L Thompson     // Q function
4040b454692Sjeremylt     if (!impl->is_identity_qf) {
405ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
40616911fdaSjeremylt     }
4071d102b48SJeremy L Thompson 
4081d102b48SJeremy L Thompson     // Output basis apply
409ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorOutputBasis_Blocked(e, Q, qf_output_fields, op_output_fields, block_size, num_input_fields, num_output_fields, op,
4102b730f8bSJeremy L Thompson                                                     e_data_full, impl));
4111d102b48SJeremy L Thompson   }
41289c6efa4Sjeremylt 
41389c6efa4Sjeremylt   // Output restriction
414d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_output_fields; i++) {
415ad70ee2cSJeremy L Thompson     CeedVector vec;
416ad70ee2cSJeremy L Thompson 
41789c6efa4Sjeremylt     // Restore evec
4182b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs_full[i + impl->num_inputs], &e_data_full[i + num_input_fields]));
419d1bcdac9Sjeremylt     // Get output vector
4202b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
42189c6efa4Sjeremylt     // Active
4222b730f8bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4234a2e7687Sjeremylt     // Restrict
4242b730f8bSJeremy L Thompson     CeedCallBackend(
425edb2538eSJeremy L Thompson         CeedElemRestrictionApply(impl->block_rstr[i + impl->num_inputs], CEED_TRANSPOSE, impl->e_vecs_full[i + impl->num_inputs], vec, request));
4264a2e7687Sjeremylt   }
4274a2e7687Sjeremylt 
4284a2e7687Sjeremylt   // Restore input arrays
4292b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, false, e_data_full, impl));
430e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4311d102b48SJeremy L Thompson }
4321d102b48SJeremy L Thompson 
433f10650afSjeremylt //------------------------------------------------------------------------------
43470a7ffb3SJeremy L Thompson // Core code for assembling linear QFunction
435f10650afSjeremylt //------------------------------------------------------------------------------
4362b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Blocked(CeedOperator op, bool build_objects, CeedVector *assembled,
437a0162de9SJeremy L Thompson                                                                   CeedElemRestriction *rstr, CeedRequest *request) {
438ad70ee2cSJeremy L Thompson   Ceed                  ceed;
439ad70ee2cSJeremy L Thompson   CeedSize              q_size;
440d1d35e2fSjeremylt   CeedInt               Q, num_input_fields, num_output_fields, num_elem, size;
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));
450ad70ee2cSJeremy L Thompson   CeedInt             num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
451ad70ee2cSJeremy L Thompson   CeedVector         *active_in  = impl->qf_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
473bb219a0fSJeremy L Thompson   if (!num_active_in) {
474d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_input_fields; i++) {
475ad70ee2cSJeremy L Thompson       CeedScalar *q_vec_array;
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) {
4822b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
4832b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
484ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_HOST, &q_vec_array));
4852b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_in));
4861d102b48SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
487ad70ee2cSJeremy L Thompson           q_size = (CeedSize)Q * block_size;
4882b730f8bSJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_in[num_active_in + field]));
489ad70ee2cSJeremy L Thompson           CeedCallBackend(
490ad70ee2cSJeremy L Thompson               CeedVectorSetArray(active_in[num_active_in + field], CEED_MEM_HOST, CEED_USE_POINTER, &q_vec_array[field * Q * block_size]));
4911d102b48SJeremy L Thompson         }
492d1d35e2fSjeremylt         num_active_in += size;
493ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
4941d102b48SJeremy L Thompson       }
4951d102b48SJeremy L Thompson     }
4964fc1f125SJeremy L Thompson     impl->num_active_in = num_active_in;
497bb219a0fSJeremy L Thompson     impl->qf_active_in  = active_in;
498bb219a0fSJeremy L Thompson   }
4991d102b48SJeremy L Thompson 
5001d102b48SJeremy L Thompson   // Count number of active output fields
501bb219a0fSJeremy L Thompson   if (!num_active_out) {
502d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_output_fields; i++) {
503ad70ee2cSJeremy L Thompson       CeedVector vec;
504ad70ee2cSJeremy L Thompson 
5051d102b48SJeremy L Thompson       // Get output vector
5062b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5071d102b48SJeremy L Thompson       // Check if active output
5081d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
5092b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
510d1d35e2fSjeremylt         num_active_out += size;
5111d102b48SJeremy L Thompson       }
5121d102b48SJeremy L Thompson     }
5134fc1f125SJeremy L Thompson     impl->num_active_out = num_active_out;
514bb219a0fSJeremy L Thompson   }
5151d102b48SJeremy L Thompson 
5161d102b48SJeremy L Thompson   // Check sizes
5176574a04fSJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5181d102b48SJeremy L Thompson 
519d1d35e2fSjeremylt   // Setup Lvec
5204fc1f125SJeremy L Thompson   if (!l_vec) {
521ad70ee2cSJeremy L Thompson     const CeedSize l_size = (CeedSize)num_blocks * block_size * Q * num_active_in * num_active_out;
522ad70ee2cSJeremy L Thompson 
5232b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, &l_vec));
5244fc1f125SJeremy L Thompson     impl->qf_l_vec = l_vec;
525bb219a0fSJeremy L Thompson   }
526ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(l_vec, CEED_MEM_HOST, &l_vec_array));
527ad70ee2cSJeremy L Thompson 
528ad70ee2cSJeremy L Thompson   // Setup block restriction
529ad70ee2cSJeremy L Thompson   if (!block_rstr) {
530ad70ee2cSJeremy L Thompson     const CeedInt strides[3] = {1, Q, num_active_in * num_active_out * Q};
531ad70ee2cSJeremy L Thompson 
532ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateBlockedStrided(ceed, num_elem, Q, block_size, num_active_in * num_active_out,
533ad70ee2cSJeremy L Thompson                                                             num_active_in * num_active_out * num_elem * Q, strides, &block_rstr));
534ad70ee2cSJeremy L Thompson     impl->qf_block_rstr = block_rstr;
535ad70ee2cSJeremy L Thompson   }
5361d102b48SJeremy L Thompson 
53770a7ffb3SJeremy L Thompson   // Build objects if needed
53870a7ffb3SJeremy L Thompson   if (build_objects) {
539ad70ee2cSJeremy L Thompson     const CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
540ad70ee2cSJeremy L Thompson     const CeedInt  strides[3] = {1, Q, num_active_in * num_active_out * Q};
541ad70ee2cSJeremy L Thompson 
54270a7ffb3SJeremy L Thompson     // Create output restriction
5432b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed, num_elem, Q, num_active_in * num_active_out, num_active_in * num_active_out * num_elem * Q,
5442b730f8bSJeremy L Thompson                                                      strides, rstr));
5451d102b48SJeremy L Thompson     // Create assembled vector
5462b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, assembled));
54770a7ffb3SJeremy L Thompson   }
5481d102b48SJeremy L Thompson 
5491d102b48SJeremy L Thompson   // Loop through elements
550ad70ee2cSJeremy L Thompson   for (CeedInt e = 0; e < num_blocks * block_size; e += block_size) {
5511d102b48SJeremy L Thompson     // Input basis apply
552ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, true, e_data_full, impl));
5531d102b48SJeremy L Thompson 
5541d102b48SJeremy L Thompson     // Assemble QFunction
555d1d35e2fSjeremylt     for (CeedInt in = 0; in < num_active_in; in++) {
5561d102b48SJeremy L Thompson       // Set Inputs
5572b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_in[in], 1.0));
558d1d35e2fSjeremylt       if (num_active_in > 1) {
5592b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(active_in[(in + num_active_in - 1) % num_active_in], 0.0));
56042ea3801Sjeremylt       }
561506b1a0cSSebastian Grimberg       if (!impl->is_identity_qf) {
5621d102b48SJeremy L Thompson         // Set Outputs
563d1d35e2fSjeremylt         for (CeedInt out = 0; out < num_output_fields; out++) {
564ad70ee2cSJeremy L Thompson           CeedVector vec;
565ad70ee2cSJeremy L Thompson 
5661d102b48SJeremy L Thompson           // Get output vector
5672b730f8bSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5681d102b48SJeremy L Thompson           // Check if active output
5691d102b48SJeremy L Thompson           if (vec == CEED_VECTOR_ACTIVE) {
570ad70ee2cSJeremy L Thompson             CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_HOST, CEED_USE_POINTER, l_vec_array));
5712b730f8bSJeremy L Thompson             CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
572ad70ee2cSJeremy L Thompson             l_vec_array += size * Q * block_size;  // Advance the pointer by the size of the output
5731d102b48SJeremy L Thompson           }
5741d102b48SJeremy L Thompson         }
5751d102b48SJeremy L Thompson         // Apply QFunction
576ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
577506b1a0cSSebastian Grimberg       } else {
578506b1a0cSSebastian Grimberg         const CeedScalar *q_vec_array;
579506b1a0cSSebastian Grimberg 
580506b1a0cSSebastian Grimberg         // Copy Identity Outputs
581506b1a0cSSebastian Grimberg         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[0], &size));
582506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorGetArrayRead(impl->q_vecs_out[0], CEED_MEM_HOST, &q_vec_array));
583506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < size * Q * block_size; i++) l_vec_array[i] = q_vec_array[i];
584506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorRestoreArrayRead(impl->q_vecs_out[0], &q_vec_array));
585506b1a0cSSebastian Grimberg         l_vec_array += size * Q * block_size;
586506b1a0cSSebastian Grimberg       }
5874a2e7687Sjeremylt     }
5884a2e7687Sjeremylt   }
5894a2e7687Sjeremylt 
5901d102b48SJeremy L Thompson   // Un-set output Qvecs to prevent accidental overwrite of Assembled
591506b1a0cSSebastian Grimberg   if (!impl->is_identity_qf) {
592d1d35e2fSjeremylt     for (CeedInt out = 0; out < num_output_fields; out++) {
593ad70ee2cSJeremy L Thompson       CeedVector vec;
594ad70ee2cSJeremy L Thompson 
5951d102b48SJeremy L Thompson       // Get output vector
5962b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5971d102b48SJeremy L Thompson       // Check if active output
5981d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
599506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_HOST, NULL));
600506b1a0cSSebastian Grimberg       }
6011d102b48SJeremy L Thompson     }
6021d102b48SJeremy L Thompson   }
6031d102b48SJeremy L Thompson 
6041d102b48SJeremy L Thompson   // Restore input arrays
6052b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, true, e_data_full, impl));
6061d102b48SJeremy L Thompson 
6071d102b48SJeremy L Thompson   // Output blocked restriction
608ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(l_vec, &l_vec_array));
6092b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
610ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(block_rstr, CEED_TRANSPOSE, l_vec, *assembled, request));
611e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6124a2e7687Sjeremylt }
6134a2e7687Sjeremylt 
614f10650afSjeremylt //------------------------------------------------------------------------------
61570a7ffb3SJeremy L Thompson // Assemble Linear QFunction
61670a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6172b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Blocked(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
6182b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, true, assembled, rstr, request);
61970a7ffb3SJeremy L Thompson }
62070a7ffb3SJeremy L Thompson 
62170a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
62270a7ffb3SJeremy L Thompson // Update Assembled Linear QFunction
62370a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6242b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Blocked(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
6252b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, false, &assembled, &rstr, request);
62670a7ffb3SJeremy L Thompson }
62770a7ffb3SJeremy L Thompson 
62870a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
629f10650afSjeremylt // Operator Destroy
630f10650afSjeremylt //------------------------------------------------------------------------------
631f10650afSjeremylt static int CeedOperatorDestroy_Blocked(CeedOperator op) {
632f10650afSjeremylt   CeedOperator_Blocked *impl;
633ad70ee2cSJeremy L Thompson 
6342b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
635f10650afSjeremylt 
6364fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
637edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->block_rstr[i]));
6382b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_full[i]));
639f10650afSjeremylt   }
640edb2538eSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->block_rstr));
6412b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_full));
6422b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
643f10650afSjeremylt 
6444fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
6452b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_in[i]));
6462b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
647f10650afSjeremylt   }
6482b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_in));
6492b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
650f10650afSjeremylt 
6514fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
6522b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_out[i]));
6532b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
654f10650afSjeremylt   }
6552b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_out));
6562b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
657f10650afSjeremylt 
658bb219a0fSJeremy L Thompson   // QFunction assembly data
6594fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
6602b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
661bb219a0fSJeremy L Thompson   }
6622b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
6632b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->qf_l_vec));
664ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&impl->qf_block_rstr));
665bb219a0fSJeremy L Thompson 
6662b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
667e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
668f10650afSjeremylt }
669f10650afSjeremylt 
670f10650afSjeremylt //------------------------------------------------------------------------------
671f10650afSjeremylt // Operator Create
672f10650afSjeremylt //------------------------------------------------------------------------------
6734a2e7687Sjeremylt int CeedOperatorCreate_Blocked(CeedOperator op) {
674fe2413ffSjeremylt   Ceed                  ceed;
6754ce2993fSjeremylt   CeedOperator_Blocked *impl;
6764a2e7687Sjeremylt 
677ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
6782b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
6792b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
6802b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Blocked));
6812b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Blocked));
6822b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Blocked));
6832b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Blocked));
684e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6854a2e7687Sjeremylt }
6862a86cc9dSSebastian Grimberg 
687f10650afSjeremylt //------------------------------------------------------------------------------
688