xref: /libCEED/rust/libceed-sys/c-src/backends/blocked/ceed-blocked-operator.c (revision f8a0df597ca176fee6b07766b6124704acaa0050)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
34a2e7687Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
54a2e7687Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
74a2e7687Sjeremylt 
849aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d576824SJeremy L Thompson #include <stdbool.h>
113d576824SJeremy L Thompson #include <stddef.h>
123d576824SJeremy L Thompson #include <stdint.h>
132b730f8bSJeremy L Thompson 
144a2e7687Sjeremylt #include "ceed-blocked.h"
154a2e7687Sjeremylt 
16f10650afSjeremylt //------------------------------------------------------------------------------
17f10650afSjeremylt // Setup Input/Output Fields
18f10650afSjeremylt //------------------------------------------------------------------------------
19*f8a0df59SJeremy L Thompson static int CeedOperatorSetupFields_Blocked(CeedQFunction qf, CeedOperator op, bool is_input, bool *skip_rstr, CeedInt *e_data_out_indices,
20*f8a0df59SJeremy L Thompson                                            bool *apply_add_basis, const CeedInt block_size, CeedElemRestriction *block_rstr, CeedVector *e_vecs_full,
21*f8a0df59SJeremy L Thompson                                            CeedVector *e_vecs, CeedVector *q_vecs, 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   }
138*f8a0df59SJeremy L Thompson   // Drop duplicate restrictions
1393aab95c0SJeremy L Thompson   if (is_input) {
1403aab95c0SJeremy L Thompson     for (CeedInt i = 0; i < num_fields; i++) {
1413aab95c0SJeremy L Thompson       CeedVector          vec_i;
1423aab95c0SJeremy L Thompson       CeedElemRestriction rstr_i;
1433aab95c0SJeremy L Thompson 
1443aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
1453aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
1463aab95c0SJeremy L Thompson       for (CeedInt j = i + 1; j < num_fields; j++) {
1473aab95c0SJeremy L Thompson         CeedVector          vec_j;
1483aab95c0SJeremy L Thompson         CeedElemRestriction rstr_j;
1493aab95c0SJeremy L Thompson 
1503aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
1513aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
1523aab95c0SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
1533aab95c0SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i], &e_vecs[j]));
154*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs_full[i + start_e], &e_vecs_full[j + start_e]));
1553aab95c0SJeremy L Thompson           skip_rstr[j] = true;
1563aab95c0SJeremy L Thompson         }
1573aab95c0SJeremy L Thompson       }
1583aab95c0SJeremy L Thompson     }
159*f8a0df59SJeremy L Thompson   } else {
160*f8a0df59SJeremy L Thompson     for (CeedInt i = num_fields - 1; i >= 0; i--) {
161*f8a0df59SJeremy L Thompson       CeedVector          vec_i;
162*f8a0df59SJeremy L Thompson       CeedElemRestriction rstr_i;
163*f8a0df59SJeremy L Thompson 
164*f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
165*f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
166*f8a0df59SJeremy L Thompson       for (CeedInt j = i - 1; j >= 0; j--) {
167*f8a0df59SJeremy L Thompson         CeedVector          vec_j;
168*f8a0df59SJeremy L Thompson         CeedElemRestriction rstr_j;
169*f8a0df59SJeremy L Thompson 
170*f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
171*f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
172*f8a0df59SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
173*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i], &e_vecs[j]));
174*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs_full[i + start_e], &e_vecs_full[j + start_e]));
175*f8a0df59SJeremy L Thompson           skip_rstr[j]          = true;
176*f8a0df59SJeremy L Thompson           apply_add_basis[i]    = true;
177*f8a0df59SJeremy L Thompson           e_data_out_indices[j] = i;
178*f8a0df59SJeremy L Thompson         }
179*f8a0df59SJeremy L Thompson       }
180*f8a0df59SJeremy L Thompson     }
1813aab95c0SJeremy L Thompson   }
182e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1834a2e7687Sjeremylt }
1844a2e7687Sjeremylt 
185f10650afSjeremylt //------------------------------------------------------------------------------
186f10650afSjeremylt // Setup Operator
187f10650afSjeremylt //------------------------------------------------------------------------------
1884a2e7687Sjeremylt static int CeedOperatorSetup_Blocked(CeedOperator op) {
1898c1105f8SJeremy L Thompson   bool                  is_setup_done;
190ad70ee2cSJeremy L Thompson   Ceed                  ceed;
191ad70ee2cSJeremy L Thompson   CeedInt               Q, num_input_fields, num_output_fields;
1928130dc29SSebastian Grimberg   const CeedInt         block_size = 8;
193ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
194ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
195ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
196ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
197ad70ee2cSJeremy L Thompson 
1982b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
1998c1105f8SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
200ad70ee2cSJeremy L Thompson 
2012b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
2022b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
2032b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
2042b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
2052b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionIsIdentity(qf, &impl->is_identity_qf));
2062b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
2072b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2084a2e7687Sjeremylt 
2094a2e7687Sjeremylt   // Allocate
210edb2538eSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->block_rstr));
2112b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs_full));
2124a2e7687Sjeremylt 
2133aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
214*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
215*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->e_data_out_indices));
216*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
2172b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
2182b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->e_vecs_in));
2192b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->e_vecs_out));
2202b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
2212b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
2224a2e7687Sjeremylt 
2234fc1f125SJeremy L Thompson   impl->num_inputs  = num_input_fields;
2244fc1f125SJeremy L Thompson   impl->num_outputs = num_output_fields;
225aedaa0e5Sjeremylt 
2264a2e7687Sjeremylt   // Set up infield and outfield pointer arrays
2274a2e7687Sjeremylt   // Infields
228*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Blocked(qf, op, true, impl->skip_rstr_in, NULL, NULL, block_size, impl->block_rstr, impl->e_vecs_full,
229*f8a0df59SJeremy L Thompson                                                   impl->e_vecs_in, impl->q_vecs_in, 0, num_input_fields, Q));
2304a2e7687Sjeremylt   // Outfields
231*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Blocked(qf, op, false, impl->skip_rstr_out, impl->e_data_out_indices, impl->apply_add_basis_out, block_size,
232*f8a0df59SJeremy L Thompson                                                   impl->block_rstr, impl->e_vecs_full, impl->e_vecs_out, impl->q_vecs_out, num_input_fields,
233*f8a0df59SJeremy L Thompson                                                   num_output_fields, Q));
234aedaa0e5Sjeremylt 
23516911fdaSjeremylt   // Identity QFunctions
2360b454692Sjeremylt   if (impl->is_identity_qf) {
237d1d35e2fSjeremylt     CeedEvalMode        in_mode, out_mode;
238d1d35e2fSjeremylt     CeedQFunctionField *in_fields, *out_fields;
239ad70ee2cSJeremy L Thompson 
2402b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &in_fields, NULL, &out_fields));
2412b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(in_fields[0], &in_mode));
2422b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(out_fields[0], &out_mode));
243d1d35e2fSjeremylt 
2440b454692Sjeremylt     if (in_mode == CEED_EVAL_NONE && out_mode == CEED_EVAL_NONE) {
245edb2538eSJeremy L Thompson       impl->is_identity_rstr_op = true;
2460b454692Sjeremylt     } else {
247db002c03SJeremy L Thompson       CeedCallBackend(CeedVectorReferenceCopy(impl->q_vecs_in[0], &impl->q_vecs_out[0]));
24816911fdaSjeremylt     }
24916911fdaSjeremylt   }
25016911fdaSjeremylt 
2512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
252e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2534a2e7687Sjeremylt }
2544a2e7687Sjeremylt 
255f10650afSjeremylt //------------------------------------------------------------------------------
256f10650afSjeremylt // Setup Operator Inputs
257f10650afSjeremylt //------------------------------------------------------------------------------
2582b730f8bSJeremy L Thompson static inline int CeedOperatorSetupInputs_Blocked(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
2594fc1f125SJeremy L Thompson                                                   CeedVector in_vec, bool skip_active, CeedScalar *e_data_full[2 * CEED_FIELD_MAX],
260a0162de9SJeremy L Thompson                                                   CeedOperator_Blocked *impl, CeedRequest *request) {
261ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
262ad70ee2cSJeremy L Thompson     uint64_t     state;
263d1d35e2fSjeremylt     CeedEvalMode eval_mode;
264d1bcdac9Sjeremylt     CeedVector   vec;
2654a2e7687Sjeremylt 
2661d102b48SJeremy L Thompson     // Get input vector
2672b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2681d102b48SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2692b730f8bSJeremy L Thompson       if (skip_active) continue;
2702b730f8bSJeremy L Thompson       else vec = in_vec;
2711d102b48SJeremy L Thompson     }
2721d102b48SJeremy L Thompson 
2732b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
274d1d35e2fSjeremylt     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2754a2e7687Sjeremylt     } else {
2764a2e7687Sjeremylt       // Restrict
2772b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetState(vec, &state));
2783aab95c0SJeremy L Thompson       if ((state != impl->input_states[i] || vec == in_vec) && !impl->skip_rstr_in[i]) {
279edb2538eSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[i], CEED_NOTRANSPOSE, vec, impl->e_vecs_full[i], request));
28016c359e6Sjeremylt       }
2813aab95c0SJeremy L Thompson       impl->input_states[i] = state;
2824a2e7687Sjeremylt       // Get evec
2832b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs_full[i], CEED_MEM_HOST, (const CeedScalar **)&e_data_full[i]));
2844a2e7687Sjeremylt     }
2854a2e7687Sjeremylt   }
286e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2874a2e7687Sjeremylt }
2884a2e7687Sjeremylt 
289f10650afSjeremylt //------------------------------------------------------------------------------
290f10650afSjeremylt // Input Basis Action
291f10650afSjeremylt //------------------------------------------------------------------------------
2922b730f8bSJeremy L Thompson static inline int CeedOperatorInputBasis_Blocked(CeedInt e, CeedInt Q, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
293ad70ee2cSJeremy L Thompson                                                  CeedInt num_input_fields, CeedInt block_size, bool skip_active,
2944fc1f125SJeremy L Thompson                                                  CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
295ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
296a915a514Srezgarshakeri     CeedInt             elem_size, size, num_comp;
297d1d35e2fSjeremylt     CeedEvalMode        eval_mode;
298edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2991d102b48SJeremy L Thompson     CeedBasis           basis;
3001d102b48SJeremy L Thompson 
3011d102b48SJeremy L Thompson     // Skip active input
302d1d35e2fSjeremylt     if (skip_active) {
3031d102b48SJeremy L Thompson       CeedVector vec;
304ad70ee2cSJeremy L Thompson 
3052b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3062b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3071d102b48SJeremy L Thompson     }
3081d102b48SJeremy L Thompson 
309d1d35e2fSjeremylt     // Get elem_size, eval_mode, size
310edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
311edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
3122b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
3132b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3144a2e7687Sjeremylt     // Basis action
315d1d35e2fSjeremylt     switch (eval_mode) {
3164a2e7687Sjeremylt       case CEED_EVAL_NONE:
31781670346SSebastian Grimberg         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i][(CeedSize)e * Q * size]));
3184a2e7687Sjeremylt         break;
3194a2e7687Sjeremylt       case CEED_EVAL_INTERP:
3204a2e7687Sjeremylt       case CEED_EVAL_GRAD:
321a915a514Srezgarshakeri       case CEED_EVAL_DIV:
322c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
323a915a514Srezgarshakeri         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
324a915a514Srezgarshakeri         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
32581670346SSebastian Grimberg         CeedCallBackend(CeedVectorSetArray(impl->e_vecs_in[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i][(CeedSize)e * elem_size * num_comp]));
326ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, block_size, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs_in[i], impl->q_vecs_in[i]));
327a915a514Srezgarshakeri         break;
3284a2e7687Sjeremylt       case CEED_EVAL_WEIGHT:
3294a2e7687Sjeremylt         break;  // No action
3304a2e7687Sjeremylt     }
33189c6efa4Sjeremylt   }
332e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
33389c6efa4Sjeremylt }
3344a2e7687Sjeremylt 
335f10650afSjeremylt //------------------------------------------------------------------------------
336f10650afSjeremylt // Output Basis Action
337f10650afSjeremylt //------------------------------------------------------------------------------
3382b730f8bSJeremy L Thompson static inline int CeedOperatorOutputBasis_Blocked(CeedInt e, CeedInt Q, CeedQFunctionField *qf_output_fields, CeedOperatorField *op_output_fields,
339*f8a0df59SJeremy L Thompson                                                   CeedInt block_size, CeedInt num_input_fields, CeedInt num_output_fields, bool *apply_add_basis,
340*f8a0df59SJeremy L Thompson                                                   CeedOperator op, CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
341ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
342a915a514Srezgarshakeri     CeedInt             elem_size, num_comp;
343d1d35e2fSjeremylt     CeedEvalMode        eval_mode;
344edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
3451d102b48SJeremy L Thompson     CeedBasis           basis;
3461d102b48SJeremy L Thompson 
347d1d35e2fSjeremylt     // Get elem_size, eval_mode, size
348edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
349edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
3502b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
3514a2e7687Sjeremylt     // Basis action
352d1d35e2fSjeremylt     switch (eval_mode) {
3534a2e7687Sjeremylt       case CEED_EVAL_NONE:
3544a2e7687Sjeremylt         break;  // No action
3554a2e7687Sjeremylt       case CEED_EVAL_INTERP:
3564a2e7687Sjeremylt       case CEED_EVAL_GRAD:
357a915a514Srezgarshakeri       case CEED_EVAL_DIV:
358c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
359a915a514Srezgarshakeri         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
360a915a514Srezgarshakeri         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
36181670346SSebastian Grimberg         CeedCallBackend(CeedVectorSetArray(impl->e_vecs_out[i], CEED_MEM_HOST, CEED_USE_POINTER,
36281670346SSebastian Grimberg                                            &e_data_full[i + num_input_fields][(CeedSize)e * elem_size * num_comp]));
363*f8a0df59SJeremy L Thompson         if (apply_add_basis[i]) {
364*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAdd(basis, block_size, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs_out[i]));
365*f8a0df59SJeremy L Thompson         } else {
366ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedBasisApply(basis, block_size, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs_out[i]));
367*f8a0df59SJeremy L Thompson         }
368a915a514Srezgarshakeri         break;
369c042f62fSJeremy L Thompson       // LCOV_EXCL_START
370bbfacfcdSjeremylt       case CEED_EVAL_WEIGHT: {
3716e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
372bbfacfcdSjeremylt         // LCOV_EXCL_STOP
3734a2e7687Sjeremylt       }
37489c6efa4Sjeremylt     }
37589c6efa4Sjeremylt   }
376e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3771d102b48SJeremy L Thompson }
3781d102b48SJeremy L Thompson 
379f10650afSjeremylt //------------------------------------------------------------------------------
380f10650afSjeremylt // Restore Input Vectors
381f10650afSjeremylt //------------------------------------------------------------------------------
3822b730f8bSJeremy L Thompson static inline int CeedOperatorRestoreInputs_Blocked(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
3832b730f8bSJeremy L Thompson                                                     bool skip_active, CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
384ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
385d1d35e2fSjeremylt     CeedEvalMode eval_mode;
3861d102b48SJeremy L Thompson 
3871d102b48SJeremy L Thompson     // Skip active inputs
388d1d35e2fSjeremylt     if (skip_active) {
3891d102b48SJeremy L Thompson       CeedVector vec;
390ad70ee2cSJeremy L Thompson 
3912b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3922b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3931d102b48SJeremy L Thompson     }
3942b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
395d1d35e2fSjeremylt     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3961d102b48SJeremy L Thompson     } else {
3972b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs_full[i], (const CeedScalar **)&e_data_full[i]));
3981d102b48SJeremy L Thompson     }
3991d102b48SJeremy L Thompson   }
400e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4011d102b48SJeremy L Thompson }
4021d102b48SJeremy L Thompson 
403f10650afSjeremylt //------------------------------------------------------------------------------
404f10650afSjeremylt // Operator Apply
405f10650afSjeremylt //------------------------------------------------------------------------------
4062b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Blocked(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
407d1d35e2fSjeremylt   CeedInt               Q, num_input_fields, num_output_fields, num_elem, size;
408ad70ee2cSJeremy L Thompson   const CeedInt         block_size = 8;
409ad70ee2cSJeremy L Thompson   CeedEvalMode          eval_mode;
410ad70ee2cSJeremy L Thompson   CeedScalar           *e_data_full[2 * CEED_FIELD_MAX] = {0};
411ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
412ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
413ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
414ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
415ad70ee2cSJeremy L Thompson 
416ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4172b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
4182b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
4192b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4202b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
4212b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
422ad70ee2cSJeremy L Thompson   const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size);
4231d102b48SJeremy L Thompson 
4241d102b48SJeremy L Thompson   // Setup
4252b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Blocked(op));
4261d102b48SJeremy L Thompson 
4270b454692Sjeremylt   // Restriction only operator
428edb2538eSJeremy L Thompson   if (impl->is_identity_rstr_op) {
429edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[0], CEED_NOTRANSPOSE, in_vec, impl->e_vecs_full[0], request));
430edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[1], CEED_TRANSPOSE, impl->e_vecs_full[0], out_vec, request));
4310b454692Sjeremylt     return CEED_ERROR_SUCCESS;
4320b454692Sjeremylt   }
4330b454692Sjeremylt 
4341d102b48SJeremy L Thompson   // Input Evecs and Restriction
4352b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data_full, impl, request));
4361d102b48SJeremy L Thompson 
4371d102b48SJeremy L Thompson   // Output Evecs
438*f8a0df59SJeremy L Thompson   for (CeedInt i = num_output_fields - 1; i >= 0; i--) {
439*f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) {
440*f8a0df59SJeremy L Thompson       e_data_full[i + num_input_fields] = e_data_full[impl->e_data_out_indices[i] + num_input_fields];
441*f8a0df59SJeremy L Thompson     } else {
4422b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs_full[i + impl->num_inputs], CEED_MEM_HOST, &e_data_full[i + num_input_fields]));
4431d102b48SJeremy L Thompson     }
444*f8a0df59SJeremy L Thompson   }
4451d102b48SJeremy L Thompson 
4461d102b48SJeremy L Thompson   // Loop through elements
447ad70ee2cSJeremy L Thompson   for (CeedInt e = 0; e < num_blocks * block_size; e += block_size) {
4481d102b48SJeremy L Thompson     // Output pointers
449d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_output_fields; i++) {
4502b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
451d1d35e2fSjeremylt       if (eval_mode == CEED_EVAL_NONE) {
4522b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
45381670346SSebastian Grimberg         CeedCallBackend(
45481670346SSebastian Grimberg             CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i + num_input_fields][(CeedSize)e * Q * size]));
4551d102b48SJeremy L Thompson       }
4561d102b48SJeremy L Thompson     }
4571d102b48SJeremy L Thompson 
45816911fdaSjeremylt     // Input basis apply
459ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, false, e_data_full, impl));
46016911fdaSjeremylt 
4611d102b48SJeremy L Thompson     // Q function
4620b454692Sjeremylt     if (!impl->is_identity_qf) {
463ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
46416911fdaSjeremylt     }
4651d102b48SJeremy L Thompson 
4661d102b48SJeremy L Thompson     // Output basis apply
467*f8a0df59SJeremy L Thompson     CeedCallBackend(CeedOperatorOutputBasis_Blocked(e, Q, qf_output_fields, op_output_fields, block_size, num_input_fields, num_output_fields,
468*f8a0df59SJeremy L Thompson                                                     impl->apply_add_basis_out, op, e_data_full, impl));
4691d102b48SJeremy L Thompson   }
47089c6efa4Sjeremylt 
47189c6efa4Sjeremylt   // Output restriction
472d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_output_fields; i++) {
473ad70ee2cSJeremy L Thompson     CeedVector vec;
474ad70ee2cSJeremy L Thompson 
475*f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
47689c6efa4Sjeremylt     // Restore evec
4772b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs_full[i + impl->num_inputs], &e_data_full[i + num_input_fields]));
478d1bcdac9Sjeremylt     // Get output vector
4792b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
48089c6efa4Sjeremylt     // Active
4812b730f8bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4824a2e7687Sjeremylt     // Restrict
4832b730f8bSJeremy L Thompson     CeedCallBackend(
484edb2538eSJeremy L Thompson         CeedElemRestrictionApply(impl->block_rstr[i + impl->num_inputs], CEED_TRANSPOSE, impl->e_vecs_full[i + impl->num_inputs], vec, request));
4854a2e7687Sjeremylt   }
4864a2e7687Sjeremylt 
4874a2e7687Sjeremylt   // Restore input arrays
4882b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, false, e_data_full, impl));
489e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4901d102b48SJeremy L Thompson }
4911d102b48SJeremy L Thompson 
492f10650afSjeremylt //------------------------------------------------------------------------------
49370a7ffb3SJeremy L Thompson // Core code for assembling linear QFunction
494f10650afSjeremylt //------------------------------------------------------------------------------
4952b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Blocked(CeedOperator op, bool build_objects, CeedVector *assembled,
496a0162de9SJeremy L Thompson                                                                   CeedElemRestriction *rstr, CeedRequest *request) {
497ad70ee2cSJeremy L Thompson   Ceed                  ceed;
498ff8551c5SJeremy L Thompson   CeedInt               qf_size_in, qf_size_out, Q, num_input_fields, num_output_fields, num_elem;
499ad70ee2cSJeremy L Thompson   const CeedInt         block_size = 8;
500ad70ee2cSJeremy L Thompson   CeedScalar           *l_vec_array;
501ad70ee2cSJeremy L Thompson   CeedScalar           *e_data_full[2 * CEED_FIELD_MAX] = {0};
502ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
503ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
504ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
505ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
506ad70ee2cSJeremy L Thompson 
507ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
508ff8551c5SJeremy L Thompson   qf_size_in                     = impl->qf_size_in;
509ff8551c5SJeremy L Thompson   qf_size_out                    = impl->qf_size_out;
510ad70ee2cSJeremy L Thompson   CeedVector          l_vec      = impl->qf_l_vec;
511ad70ee2cSJeremy L Thompson   CeedElemRestriction block_rstr = impl->qf_block_rstr;
512ad70ee2cSJeremy L Thompson 
5132b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
5142b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
5152b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
5162b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
5172b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
5182b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
519ad70ee2cSJeremy L Thompson   const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size);
5201d102b48SJeremy L Thompson 
5211d102b48SJeremy L Thompson   // Setup
5222b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Blocked(op));
5231d102b48SJeremy L Thompson 
524506b1a0cSSebastian Grimberg   // Check for restriction only operator
525506b1a0cSSebastian Grimberg   CeedCheck(!impl->is_identity_rstr_op, ceed, CEED_ERROR_BACKEND, "Assembling restriction only operators is not supported");
52616911fdaSjeremylt 
5271d102b48SJeremy L Thompson   // Input Evecs and Restriction
5282b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data_full, impl, request));
5291d102b48SJeremy L Thompson 
5301d102b48SJeremy L Thompson   // Count number of active input fields
531ff8551c5SJeremy L Thompson   if (qf_size_in == 0) {
532d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_input_fields; i++) {
533c7b67790SJeremy L Thompson       CeedInt    field_size;
534ad70ee2cSJeremy L Thompson       CeedVector vec;
535ad70ee2cSJeremy L Thompson 
5361d102b48SJeremy L Thompson       // Get input vector
5372b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
5381d102b48SJeremy L Thompson       // Check if active input
5391d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
540c7b67790SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &field_size));
5412b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
542ff8551c5SJeremy L Thompson         qf_size_in += field_size;
5431d102b48SJeremy L Thompson       }
5441d102b48SJeremy L Thompson     }
545ff8551c5SJeremy L Thompson     CeedCheck(qf_size_in > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
546ff8551c5SJeremy L Thompson     impl->qf_size_in = qf_size_in;
547bb219a0fSJeremy L Thompson   }
5481d102b48SJeremy L Thompson 
5491d102b48SJeremy L Thompson   // Count number of active output fields
550ff8551c5SJeremy L Thompson   if (qf_size_out == 0) {
551d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_output_fields; i++) {
552c7b67790SJeremy L Thompson       CeedInt    field_size;
553ad70ee2cSJeremy L Thompson       CeedVector vec;
554ad70ee2cSJeremy L Thompson 
5551d102b48SJeremy L Thompson       // Get output vector
5562b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5571d102b48SJeremy L Thompson       // Check if active output
5581d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
559c7b67790SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &field_size));
560ff8551c5SJeremy L Thompson         qf_size_out += field_size;
5611d102b48SJeremy L Thompson       }
5621d102b48SJeremy L Thompson     }
563ff8551c5SJeremy L Thompson     CeedCheck(qf_size_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
564ff8551c5SJeremy L Thompson     impl->qf_size_out = qf_size_out;
565bb219a0fSJeremy L Thompson   }
5661d102b48SJeremy L Thompson 
567d1d35e2fSjeremylt   // Setup Lvec
5684fc1f125SJeremy L Thompson   if (!l_vec) {
569ff8551c5SJeremy L Thompson     const CeedSize l_size = (CeedSize)num_blocks * block_size * Q * qf_size_in * qf_size_out;
570ad70ee2cSJeremy L Thompson 
5712b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, &l_vec));
5724fc1f125SJeremy L Thompson     impl->qf_l_vec = l_vec;
573bb219a0fSJeremy L Thompson   }
574ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(l_vec, CEED_MEM_HOST, &l_vec_array));
575ad70ee2cSJeremy L Thompson 
576ad70ee2cSJeremy L Thompson   // Setup block restriction
577ad70ee2cSJeremy L Thompson   if (!block_rstr) {
578ff8551c5SJeremy L Thompson     const CeedInt strides[3] = {1, Q, qf_size_in * qf_size_out * Q};
579ad70ee2cSJeremy L Thompson 
580ff8551c5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateBlockedStrided(ceed, num_elem, Q, block_size, qf_size_in * qf_size_out,
581ff8551c5SJeremy L Thompson                                                             qf_size_in * qf_size_out * num_elem * Q, strides, &block_rstr));
582ad70ee2cSJeremy L Thompson     impl->qf_block_rstr = block_rstr;
583ad70ee2cSJeremy L Thompson   }
5841d102b48SJeremy L Thompson 
58570a7ffb3SJeremy L Thompson   // Build objects if needed
58670a7ffb3SJeremy L Thompson   if (build_objects) {
587ff8551c5SJeremy L Thompson     const CeedSize l_size     = (CeedSize)num_elem * Q * qf_size_in * qf_size_out;
588ff8551c5SJeremy L Thompson     const CeedInt  strides[3] = {1, Q, qf_size_in * qf_size_out * Q};
589ad70ee2cSJeremy L Thompson 
59070a7ffb3SJeremy L Thompson     // Create output restriction
5910a5597ceSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed, num_elem, Q, qf_size_in * qf_size_out,
5920a5597ceSJeremy L Thompson                                                      (CeedSize)qf_size_in * (CeedSize)qf_size_out * (CeedSize)num_elem * (CeedSize)Q, strides, rstr));
5931d102b48SJeremy L Thompson     // Create assembled vector
5942b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, assembled));
59570a7ffb3SJeremy L Thompson   }
5961d102b48SJeremy L Thompson 
5971d102b48SJeremy L Thompson   // Loop through elements
598ad70ee2cSJeremy L Thompson   for (CeedInt e = 0; e < num_blocks * block_size; e += block_size) {
5991d102b48SJeremy L Thompson     // Input basis apply
600ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, true, e_data_full, impl));
6011d102b48SJeremy L Thompson 
6021d102b48SJeremy L Thompson     // Assemble QFunction
603c7b67790SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
604c7b67790SJeremy L Thompson       CeedInt    field_size;
605c7b67790SJeremy L Thompson       CeedVector vec;
606c7b67790SJeremy L Thompson 
607c7b67790SJeremy L Thompson       // Get input vector
608c7b67790SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
609c7b67790SJeremy L Thompson       // Check if active input
610c7b67790SJeremy L Thompson       if (vec != CEED_VECTOR_ACTIVE) continue;
611c7b67790SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &field_size));
612c7b67790SJeremy L Thompson       for (CeedInt field = 0; field < field_size; field++) {
613c7b67790SJeremy L Thompson         // Set current portion of input to 1.0
614c7b67790SJeremy L Thompson         {
615c7b67790SJeremy L Thompson           CeedScalar *array;
616c7b67790SJeremy L Thompson 
617c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_HOST, &array));
618c7b67790SJeremy L Thompson           for (CeedInt j = 0; j < Q * block_size; j++) array[field * Q * block_size + j] = 1.0;
619c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &array));
62042ea3801Sjeremylt         }
621c7b67790SJeremy L Thompson 
622506b1a0cSSebastian Grimberg         if (!impl->is_identity_qf) {
6231d102b48SJeremy L Thompson           // Set Outputs
624d1d35e2fSjeremylt           for (CeedInt out = 0; out < num_output_fields; out++) {
625c7b67790SJeremy L Thompson             CeedInt    field_size;
626ad70ee2cSJeremy L Thompson             CeedVector vec;
627ad70ee2cSJeremy L Thompson 
6281d102b48SJeremy L Thompson             // Get output vector
6292b730f8bSJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
6301d102b48SJeremy L Thompson             // Check if active output
6311d102b48SJeremy L Thompson             if (vec == CEED_VECTOR_ACTIVE) {
632ad70ee2cSJeremy L Thompson               CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_HOST, CEED_USE_POINTER, l_vec_array));
633c7b67790SJeremy L Thompson               CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &field_size));
634c7b67790SJeremy L Thompson               l_vec_array += field_size * Q * block_size;  // Advance the pointer by the size of the output
6351d102b48SJeremy L Thompson             }
6361d102b48SJeremy L Thompson           }
6371d102b48SJeremy L Thompson           // Apply QFunction
638ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
639506b1a0cSSebastian Grimberg         } else {
640c7b67790SJeremy L Thompson           CeedInt           field_size;
641c7b67790SJeremy L Thompson           const CeedScalar *array;
642506b1a0cSSebastian Grimberg 
643506b1a0cSSebastian Grimberg           // Copy Identity Outputs
644c7b67790SJeremy L Thompson           CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[0], &field_size));
645c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayRead(impl->q_vecs_out[0], CEED_MEM_HOST, &array));
646c7b67790SJeremy L Thompson           for (CeedInt j = 0; j < field_size * Q * block_size; j++) l_vec_array[j] = array[j];
647c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorRestoreArrayRead(impl->q_vecs_out[0], &array));
648c7b67790SJeremy L Thompson           l_vec_array += field_size * Q * block_size;
649c7b67790SJeremy L Thompson         }
650c7b67790SJeremy L Thompson         // Reset input to 0.0
651c7b67790SJeremy L Thompson         {
652c7b67790SJeremy L Thompson           CeedScalar *array;
653c7b67790SJeremy L Thompson 
654c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_HOST, &array));
655c7b67790SJeremy L Thompson           for (CeedInt j = 0; j < Q * block_size; j++) array[field * Q * block_size + j] = 0.0;
656c7b67790SJeremy L Thompson           CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &array));
657c7b67790SJeremy L Thompson         }
658506b1a0cSSebastian Grimberg       }
6594a2e7687Sjeremylt     }
6604a2e7687Sjeremylt   }
6614a2e7687Sjeremylt 
6621d102b48SJeremy L Thompson   // Un-set output Qvecs to prevent accidental overwrite of Assembled
663506b1a0cSSebastian Grimberg   if (!impl->is_identity_qf) {
664d1d35e2fSjeremylt     for (CeedInt out = 0; out < num_output_fields; out++) {
665ad70ee2cSJeremy L Thompson       CeedVector vec;
666ad70ee2cSJeremy L Thompson 
6671d102b48SJeremy L Thompson       // Get output vector
6682b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
6691d102b48SJeremy L Thompson       // Check if active output
6701d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
671506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_HOST, NULL));
672506b1a0cSSebastian Grimberg       }
6731d102b48SJeremy L Thompson     }
6741d102b48SJeremy L Thompson   }
6751d102b48SJeremy L Thompson 
6761d102b48SJeremy L Thompson   // Restore input arrays
6772b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, true, e_data_full, impl));
6781d102b48SJeremy L Thompson 
6791d102b48SJeremy L Thompson   // Output blocked restriction
680ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(l_vec, &l_vec_array));
6812b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
682ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(block_rstr, CEED_TRANSPOSE, l_vec, *assembled, request));
683e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6844a2e7687Sjeremylt }
6854a2e7687Sjeremylt 
686f10650afSjeremylt //------------------------------------------------------------------------------
68770a7ffb3SJeremy L Thompson // Assemble Linear QFunction
68870a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6892b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Blocked(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
6902b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, true, assembled, rstr, request);
69170a7ffb3SJeremy L Thompson }
69270a7ffb3SJeremy L Thompson 
69370a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
69470a7ffb3SJeremy L Thompson // Update Assembled Linear QFunction
69570a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6962b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Blocked(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
6972b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, false, &assembled, &rstr, request);
69870a7ffb3SJeremy L Thompson }
69970a7ffb3SJeremy L Thompson 
70070a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
701f10650afSjeremylt // Operator Destroy
702f10650afSjeremylt //------------------------------------------------------------------------------
703f10650afSjeremylt static int CeedOperatorDestroy_Blocked(CeedOperator op) {
704f10650afSjeremylt   CeedOperator_Blocked *impl;
705ad70ee2cSJeremy L Thompson 
7062b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
707f10650afSjeremylt 
7083aab95c0SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_in));
709*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_out));
710*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_data_out_indices));
711*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->apply_add_basis_out));
7124fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
713edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->block_rstr[i]));
7142b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_full[i]));
715f10650afSjeremylt   }
716edb2538eSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->block_rstr));
7172b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_full));
7182b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
719f10650afSjeremylt 
7204fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
7212b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_in[i]));
7222b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
723f10650afSjeremylt   }
7242b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_in));
7252b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
726f10650afSjeremylt 
7274fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
7282b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_out[i]));
7292b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
730f10650afSjeremylt   }
7312b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_out));
7322b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
733f10650afSjeremylt 
734bb219a0fSJeremy L Thompson   // QFunction assembly data
7352b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->qf_l_vec));
736ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&impl->qf_block_rstr));
737bb219a0fSJeremy L Thompson 
7382b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
739e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
740f10650afSjeremylt }
741f10650afSjeremylt 
742f10650afSjeremylt //------------------------------------------------------------------------------
743f10650afSjeremylt // Operator Create
744f10650afSjeremylt //------------------------------------------------------------------------------
7454a2e7687Sjeremylt int CeedOperatorCreate_Blocked(CeedOperator op) {
746fe2413ffSjeremylt   Ceed                  ceed;
7474ce2993fSjeremylt   CeedOperator_Blocked *impl;
7484a2e7687Sjeremylt 
749ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
7502b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
7512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
7522b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Blocked));
7532b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Blocked));
7542b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Blocked));
7552b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Blocked));
756e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7574a2e7687Sjeremylt }
7582a86cc9dSSebastian Grimberg 
759f10650afSjeremylt //------------------------------------------------------------------------------
760