xref: /libCEED/rust/libceed-sys/c-src/backends/blocked/ceed-blocked-operator.c (revision 81670346db790fd69581584ebefdf7924e18afee)
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:
268*81670346SSebastian 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));
276*81670346SSebastian 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));
312*81670346SSebastian Grimberg         CeedCallBackend(CeedVectorSetArray(impl->e_vecs_out[i], CEED_MEM_HOST, CEED_USE_POINTER,
313*81670346SSebastian 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));
396*81670346SSebastian Grimberg         CeedCallBackend(
397*81670346SSebastian 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;
440ad70ee2cSJeremy L Thompson   CeedSize              q_size;
441d1d35e2fSjeremylt   CeedInt               Q, num_input_fields, num_output_fields, num_elem, size;
442ad70ee2cSJeremy L Thompson   const CeedInt         block_size = 8;
443ad70ee2cSJeremy L Thompson   CeedScalar           *l_vec_array;
444ad70ee2cSJeremy L Thompson   CeedScalar           *e_data_full[2 * CEED_FIELD_MAX] = {0};
445ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
446ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
447ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
448ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
449ad70ee2cSJeremy L Thompson 
450ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
451ad70ee2cSJeremy L Thompson   CeedInt             num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
452ad70ee2cSJeremy L Thompson   CeedVector         *active_in  = impl->qf_active_in;
453ad70ee2cSJeremy L Thompson   CeedVector          l_vec      = impl->qf_l_vec;
454ad70ee2cSJeremy L Thompson   CeedElemRestriction block_rstr = impl->qf_block_rstr;
455ad70ee2cSJeremy L Thompson 
4562b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
4572b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
4582b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4592b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
4602b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4612b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
462ad70ee2cSJeremy L Thompson   const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size);
4631d102b48SJeremy L Thompson 
4641d102b48SJeremy L Thompson   // Setup
4652b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Blocked(op));
4661d102b48SJeremy L Thompson 
467506b1a0cSSebastian Grimberg   // Check for restriction only operator
468506b1a0cSSebastian Grimberg   CeedCheck(!impl->is_identity_rstr_op, ceed, CEED_ERROR_BACKEND, "Assembling restriction only operators is not supported");
46916911fdaSjeremylt 
4701d102b48SJeremy L Thompson   // Input Evecs and Restriction
4712b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data_full, impl, request));
4721d102b48SJeremy L Thompson 
4731d102b48SJeremy L Thompson   // Count number of active input fields
474bb219a0fSJeremy L Thompson   if (!num_active_in) {
475d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_input_fields; i++) {
476ad70ee2cSJeremy L Thompson       CeedScalar *q_vec_array;
477ad70ee2cSJeremy L Thompson       CeedVector  vec;
478ad70ee2cSJeremy L Thompson 
4791d102b48SJeremy L Thompson       // Get input vector
4802b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4811d102b48SJeremy L Thompson       // Check if active input
4821d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
4832b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
4842b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
485ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_HOST, &q_vec_array));
4862b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_in));
4871d102b48SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
488ad70ee2cSJeremy L Thompson           q_size = (CeedSize)Q * block_size;
4892b730f8bSJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_in[num_active_in + field]));
490ad70ee2cSJeremy L Thompson           CeedCallBackend(
491ad70ee2cSJeremy L Thompson               CeedVectorSetArray(active_in[num_active_in + field], CEED_MEM_HOST, CEED_USE_POINTER, &q_vec_array[field * Q * block_size]));
4921d102b48SJeremy L Thompson         }
493d1d35e2fSjeremylt         num_active_in += size;
494ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
4951d102b48SJeremy L Thompson       }
4961d102b48SJeremy L Thompson     }
4974fc1f125SJeremy L Thompson     impl->num_active_in = num_active_in;
498bb219a0fSJeremy L Thompson     impl->qf_active_in  = active_in;
499bb219a0fSJeremy L Thompson   }
5001d102b48SJeremy L Thompson 
5011d102b48SJeremy L Thompson   // Count number of active output fields
502bb219a0fSJeremy L Thompson   if (!num_active_out) {
503d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_output_fields; i++) {
504ad70ee2cSJeremy L Thompson       CeedVector vec;
505ad70ee2cSJeremy L Thompson 
5061d102b48SJeremy L Thompson       // Get output vector
5072b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5081d102b48SJeremy L Thompson       // Check if active output
5091d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
5102b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
511d1d35e2fSjeremylt         num_active_out += size;
5121d102b48SJeremy L Thompson       }
5131d102b48SJeremy L Thompson     }
5144fc1f125SJeremy L Thompson     impl->num_active_out = num_active_out;
515bb219a0fSJeremy L Thompson   }
5161d102b48SJeremy L Thompson 
5171d102b48SJeremy L Thompson   // Check sizes
5186574a04fSJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5191d102b48SJeremy L Thompson 
520d1d35e2fSjeremylt   // Setup Lvec
5214fc1f125SJeremy L Thompson   if (!l_vec) {
522ad70ee2cSJeremy L Thompson     const CeedSize l_size = (CeedSize)num_blocks * block_size * Q * num_active_in * num_active_out;
523ad70ee2cSJeremy L Thompson 
5242b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, &l_vec));
5254fc1f125SJeremy L Thompson     impl->qf_l_vec = l_vec;
526bb219a0fSJeremy L Thompson   }
527ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(l_vec, CEED_MEM_HOST, &l_vec_array));
528ad70ee2cSJeremy L Thompson 
529ad70ee2cSJeremy L Thompson   // Setup block restriction
530ad70ee2cSJeremy L Thompson   if (!block_rstr) {
531ad70ee2cSJeremy L Thompson     const CeedInt strides[3] = {1, Q, num_active_in * num_active_out * Q};
532ad70ee2cSJeremy L Thompson 
533ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateBlockedStrided(ceed, num_elem, Q, block_size, num_active_in * num_active_out,
534ad70ee2cSJeremy L Thompson                                                             num_active_in * num_active_out * num_elem * Q, strides, &block_rstr));
535ad70ee2cSJeremy L Thompson     impl->qf_block_rstr = block_rstr;
536ad70ee2cSJeremy L Thompson   }
5371d102b48SJeremy L Thompson 
53870a7ffb3SJeremy L Thompson   // Build objects if needed
53970a7ffb3SJeremy L Thompson   if (build_objects) {
540ad70ee2cSJeremy L Thompson     const CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
541ad70ee2cSJeremy L Thompson     const CeedInt  strides[3] = {1, Q, num_active_in * num_active_out * Q};
542ad70ee2cSJeremy L Thompson 
54370a7ffb3SJeremy L Thompson     // Create output restriction
5442b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed, num_elem, Q, num_active_in * num_active_out, num_active_in * num_active_out * num_elem * Q,
5452b730f8bSJeremy L Thompson                                                      strides, rstr));
5461d102b48SJeremy L Thompson     // Create assembled vector
5472b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, assembled));
54870a7ffb3SJeremy L Thompson   }
5491d102b48SJeremy L Thompson 
5501d102b48SJeremy L Thompson   // Loop through elements
551ad70ee2cSJeremy L Thompson   for (CeedInt e = 0; e < num_blocks * block_size; e += block_size) {
5521d102b48SJeremy L Thompson     // Input basis apply
553ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, true, e_data_full, impl));
5541d102b48SJeremy L Thompson 
5551d102b48SJeremy L Thompson     // Assemble QFunction
556d1d35e2fSjeremylt     for (CeedInt in = 0; in < num_active_in; in++) {
5571d102b48SJeremy L Thompson       // Set Inputs
5582b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_in[in], 1.0));
559d1d35e2fSjeremylt       if (num_active_in > 1) {
5602b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(active_in[(in + num_active_in - 1) % num_active_in], 0.0));
56142ea3801Sjeremylt       }
562506b1a0cSSebastian Grimberg       if (!impl->is_identity_qf) {
5631d102b48SJeremy L Thompson         // Set Outputs
564d1d35e2fSjeremylt         for (CeedInt out = 0; out < num_output_fields; out++) {
565ad70ee2cSJeremy L Thompson           CeedVector vec;
566ad70ee2cSJeremy L Thompson 
5671d102b48SJeremy L Thompson           // Get output vector
5682b730f8bSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5691d102b48SJeremy L Thompson           // Check if active output
5701d102b48SJeremy L Thompson           if (vec == CEED_VECTOR_ACTIVE) {
571ad70ee2cSJeremy L Thompson             CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_HOST, CEED_USE_POINTER, l_vec_array));
5722b730f8bSJeremy L Thompson             CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
573ad70ee2cSJeremy L Thompson             l_vec_array += size * Q * block_size;  // Advance the pointer by the size of the output
5741d102b48SJeremy L Thompson           }
5751d102b48SJeremy L Thompson         }
5761d102b48SJeremy L Thompson         // Apply QFunction
577ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
578506b1a0cSSebastian Grimberg       } else {
579506b1a0cSSebastian Grimberg         const CeedScalar *q_vec_array;
580506b1a0cSSebastian Grimberg 
581506b1a0cSSebastian Grimberg         // Copy Identity Outputs
582506b1a0cSSebastian Grimberg         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[0], &size));
583506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorGetArrayRead(impl->q_vecs_out[0], CEED_MEM_HOST, &q_vec_array));
584506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < size * Q * block_size; i++) l_vec_array[i] = q_vec_array[i];
585506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorRestoreArrayRead(impl->q_vecs_out[0], &q_vec_array));
586506b1a0cSSebastian Grimberg         l_vec_array += size * Q * block_size;
587506b1a0cSSebastian Grimberg       }
5884a2e7687Sjeremylt     }
5894a2e7687Sjeremylt   }
5904a2e7687Sjeremylt 
5911d102b48SJeremy L Thompson   // Un-set output Qvecs to prevent accidental overwrite of Assembled
592506b1a0cSSebastian Grimberg   if (!impl->is_identity_qf) {
593d1d35e2fSjeremylt     for (CeedInt out = 0; out < num_output_fields; out++) {
594ad70ee2cSJeremy L Thompson       CeedVector vec;
595ad70ee2cSJeremy L Thompson 
5961d102b48SJeremy L Thompson       // Get output vector
5972b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5981d102b48SJeremy L Thompson       // Check if active output
5991d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
600506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_HOST, NULL));
601506b1a0cSSebastian Grimberg       }
6021d102b48SJeremy L Thompson     }
6031d102b48SJeremy L Thompson   }
6041d102b48SJeremy L Thompson 
6051d102b48SJeremy L Thompson   // Restore input arrays
6062b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, true, e_data_full, impl));
6071d102b48SJeremy L Thompson 
6081d102b48SJeremy L Thompson   // Output blocked restriction
609ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(l_vec, &l_vec_array));
6102b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
611ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(block_rstr, CEED_TRANSPOSE, l_vec, *assembled, request));
612e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6134a2e7687Sjeremylt }
6144a2e7687Sjeremylt 
615f10650afSjeremylt //------------------------------------------------------------------------------
61670a7ffb3SJeremy L Thompson // Assemble Linear QFunction
61770a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6182b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Blocked(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
6192b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, true, assembled, rstr, request);
62070a7ffb3SJeremy L Thompson }
62170a7ffb3SJeremy L Thompson 
62270a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
62370a7ffb3SJeremy L Thompson // Update Assembled Linear QFunction
62470a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6252b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Blocked(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
6262b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, false, &assembled, &rstr, request);
62770a7ffb3SJeremy L Thompson }
62870a7ffb3SJeremy L Thompson 
62970a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
630f10650afSjeremylt // Operator Destroy
631f10650afSjeremylt //------------------------------------------------------------------------------
632f10650afSjeremylt static int CeedOperatorDestroy_Blocked(CeedOperator op) {
633f10650afSjeremylt   CeedOperator_Blocked *impl;
634ad70ee2cSJeremy L Thompson 
6352b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
636f10650afSjeremylt 
6374fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
638edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->block_rstr[i]));
6392b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_full[i]));
640f10650afSjeremylt   }
641edb2538eSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->block_rstr));
6422b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_full));
6432b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
644f10650afSjeremylt 
6454fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
6462b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_in[i]));
6472b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
648f10650afSjeremylt   }
6492b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_in));
6502b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
651f10650afSjeremylt 
6524fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
6532b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_out[i]));
6542b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
655f10650afSjeremylt   }
6562b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_out));
6572b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
658f10650afSjeremylt 
659bb219a0fSJeremy L Thompson   // QFunction assembly data
6604fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
6612b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
662bb219a0fSJeremy L Thompson   }
6632b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
6642b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->qf_l_vec));
665ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&impl->qf_block_rstr));
666bb219a0fSJeremy L Thompson 
6672b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
668e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
669f10650afSjeremylt }
670f10650afSjeremylt 
671f10650afSjeremylt //------------------------------------------------------------------------------
672f10650afSjeremylt // Operator Create
673f10650afSjeremylt //------------------------------------------------------------------------------
6744a2e7687Sjeremylt int CeedOperatorCreate_Blocked(CeedOperator op) {
675fe2413ffSjeremylt   Ceed                  ceed;
6764ce2993fSjeremylt   CeedOperator_Blocked *impl;
6774a2e7687Sjeremylt 
678ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
6792b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
6802b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
6812b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Blocked));
6822b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Blocked));
6832b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Blocked));
6842b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Blocked));
685e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6864a2e7687Sjeremylt }
6872a86cc9dSSebastian Grimberg 
688f10650afSjeremylt //------------------------------------------------------------------------------
689