xref: /libCEED/rust/libceed-sys/c-src/backends/blocked/ceed-blocked-operator.c (revision 56c48462b0d39517237dc0770653f43cb7356d08)
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 
100*56c48462SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetStrides(rstr, strides));
101ad70ee2cSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionCreateBlockedStrided(ceed_rstr, num_elem, elem_size, block_size, num_comp, l_size, strides,
102edb2538eSJeremy L Thompson                                                                   &block_rstr[i + start_e]));
103fcbe8c06SSebastian Grimberg         } break;
1042c7e7413SJeremy L Thompson         case CEED_RESTRICTION_POINTS:
1052c7e7413SJeremy L Thompson           // Empty case - won't occur
1062c7e7413SJeremy L Thompson           break;
10777d1c127SSebastian Grimberg       }
108edb2538eSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionCreateVector(block_rstr[i + start_e], NULL, &e_vecs_full[i + start_e]));
1094a2e7687Sjeremylt     }
1104a2e7687Sjeremylt 
111d1d35e2fSjeremylt     switch (eval_mode) {
1124a2e7687Sjeremylt       case CEED_EVAL_NONE:
1132b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
114ad70ee2cSJeremy L Thompson         q_size = (CeedSize)Q * size * block_size;
1152b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
116aedaa0e5Sjeremylt         break;
117aedaa0e5Sjeremylt       case CEED_EVAL_INTERP:
1184a2e7687Sjeremylt       case CEED_EVAL_GRAD:
119a915a514Srezgarshakeri       case CEED_EVAL_DIV:
120c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
1212b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
1222b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
1232b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetNumNodes(basis, &P));
1242b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
125ad70ee2cSJeremy L Thompson         e_size = (CeedSize)P * num_comp * block_size;
1262b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, e_size, &e_vecs[i]));
127ad70ee2cSJeremy L Thompson         q_size = (CeedSize)Q * size * block_size;
1282b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1294a2e7687Sjeremylt         break;
1304a2e7687Sjeremylt       case CEED_EVAL_WEIGHT:  // Only on input fields
1312b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
132ad70ee2cSJeremy L Thompson         q_size = (CeedSize)Q * block_size;
1332b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
134ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, block_size, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
1354a2e7687Sjeremylt         break;
1364a2e7687Sjeremylt     }
1374a2e7687Sjeremylt   }
138e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1394a2e7687Sjeremylt }
1404a2e7687Sjeremylt 
141f10650afSjeremylt //------------------------------------------------------------------------------
142f10650afSjeremylt // Setup Operator
143f10650afSjeremylt //------------------------------------------------------------------------------
1444a2e7687Sjeremylt static int CeedOperatorSetup_Blocked(CeedOperator op) {
1458c1105f8SJeremy L Thompson   bool                  is_setup_done;
146ad70ee2cSJeremy L Thompson   Ceed                  ceed;
147ad70ee2cSJeremy L Thompson   CeedInt               Q, num_input_fields, num_output_fields;
1488130dc29SSebastian Grimberg   const CeedInt         block_size = 8;
149ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
150ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
151ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
152ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
153ad70ee2cSJeremy L Thompson 
1542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
1558c1105f8SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
156ad70ee2cSJeremy L Thompson 
1572b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1582b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1592b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
1612b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionIsIdentity(qf, &impl->is_identity_qf));
1622b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1632b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1644a2e7687Sjeremylt 
1654a2e7687Sjeremylt   // Allocate
166edb2538eSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->block_rstr));
1672b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs_full));
1684a2e7687Sjeremylt 
1692b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
1702b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->e_vecs_in));
1712b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->e_vecs_out));
1722b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
1732b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
1744a2e7687Sjeremylt 
1754fc1f125SJeremy L Thompson   impl->num_inputs  = num_input_fields;
1764fc1f125SJeremy L Thompson   impl->num_outputs = num_output_fields;
177aedaa0e5Sjeremylt 
1784a2e7687Sjeremylt   // Set up infield and outfield pointer arrays
1794a2e7687Sjeremylt   // Infields
1808130dc29SSebastian Grimberg   CeedCallBackend(CeedOperatorSetupFields_Blocked(qf, op, true, block_size, impl->block_rstr, impl->e_vecs_full, impl->e_vecs_in, impl->q_vecs_in, 0,
1818130dc29SSebastian Grimberg                                                   num_input_fields, Q));
1824a2e7687Sjeremylt   // Outfields
1838130dc29SSebastian Grimberg   CeedCallBackend(CeedOperatorSetupFields_Blocked(qf, op, false, block_size, impl->block_rstr, impl->e_vecs_full, impl->e_vecs_out, impl->q_vecs_out,
1842b730f8bSJeremy L Thompson                                                   num_input_fields, num_output_fields, Q));
185aedaa0e5Sjeremylt 
18616911fdaSjeremylt   // Identity QFunctions
1870b454692Sjeremylt   if (impl->is_identity_qf) {
188d1d35e2fSjeremylt     CeedEvalMode        in_mode, out_mode;
189d1d35e2fSjeremylt     CeedQFunctionField *in_fields, *out_fields;
190ad70ee2cSJeremy L Thompson 
1912b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &in_fields, NULL, &out_fields));
1922b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(in_fields[0], &in_mode));
1932b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(out_fields[0], &out_mode));
194d1d35e2fSjeremylt 
1950b454692Sjeremylt     if (in_mode == CEED_EVAL_NONE && out_mode == CEED_EVAL_NONE) {
196edb2538eSJeremy L Thompson       impl->is_identity_rstr_op = true;
1970b454692Sjeremylt     } else {
198db002c03SJeremy L Thompson       CeedCallBackend(CeedVectorReferenceCopy(impl->q_vecs_in[0], &impl->q_vecs_out[0]));
19916911fdaSjeremylt     }
20016911fdaSjeremylt   }
20116911fdaSjeremylt 
2022b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
203e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2044a2e7687Sjeremylt }
2054a2e7687Sjeremylt 
206f10650afSjeremylt //------------------------------------------------------------------------------
207f10650afSjeremylt // Setup Operator Inputs
208f10650afSjeremylt //------------------------------------------------------------------------------
2092b730f8bSJeremy L Thompson static inline int CeedOperatorSetupInputs_Blocked(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
2104fc1f125SJeremy L Thompson                                                   CeedVector in_vec, bool skip_active, CeedScalar *e_data_full[2 * CEED_FIELD_MAX],
211a0162de9SJeremy L Thompson                                                   CeedOperator_Blocked *impl, CeedRequest *request) {
212ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
213ad70ee2cSJeremy L Thompson     uint64_t     state;
214d1d35e2fSjeremylt     CeedEvalMode eval_mode;
215d1bcdac9Sjeremylt     CeedVector   vec;
2164a2e7687Sjeremylt 
2171d102b48SJeremy L Thompson     // Get input vector
2182b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2191d102b48SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
2202b730f8bSJeremy L Thompson       if (skip_active) continue;
2212b730f8bSJeremy L Thompson       else vec = in_vec;
2221d102b48SJeremy L Thompson     }
2231d102b48SJeremy L Thompson 
2242b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
225d1d35e2fSjeremylt     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2264a2e7687Sjeremylt     } else {
2274a2e7687Sjeremylt       // Restrict
2282b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetState(vec, &state));
2294fc1f125SJeremy L Thompson       if (state != impl->input_states[i] || vec == in_vec) {
230edb2538eSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[i], CEED_NOTRANSPOSE, vec, impl->e_vecs_full[i], request));
2314fc1f125SJeremy L Thompson         impl->input_states[i] = state;
23216c359e6Sjeremylt       }
2334a2e7687Sjeremylt       // Get evec
2342b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs_full[i], CEED_MEM_HOST, (const CeedScalar **)&e_data_full[i]));
2354a2e7687Sjeremylt     }
2364a2e7687Sjeremylt   }
237e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2384a2e7687Sjeremylt }
2394a2e7687Sjeremylt 
240f10650afSjeremylt //------------------------------------------------------------------------------
241f10650afSjeremylt // Input Basis Action
242f10650afSjeremylt //------------------------------------------------------------------------------
2432b730f8bSJeremy L Thompson static inline int CeedOperatorInputBasis_Blocked(CeedInt e, CeedInt Q, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
244ad70ee2cSJeremy L Thompson                                                  CeedInt num_input_fields, CeedInt block_size, bool skip_active,
2454fc1f125SJeremy L Thompson                                                  CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
246ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
247a915a514Srezgarshakeri     CeedInt             elem_size, size, num_comp;
248d1d35e2fSjeremylt     CeedEvalMode        eval_mode;
249edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2501d102b48SJeremy L Thompson     CeedBasis           basis;
2511d102b48SJeremy L Thompson 
2521d102b48SJeremy L Thompson     // Skip active input
253d1d35e2fSjeremylt     if (skip_active) {
2541d102b48SJeremy L Thompson       CeedVector vec;
255ad70ee2cSJeremy L Thompson 
2562b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2572b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2581d102b48SJeremy L Thompson     }
2591d102b48SJeremy L Thompson 
260d1d35e2fSjeremylt     // Get elem_size, eval_mode, size
261edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
262edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
2632b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
2642b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
2654a2e7687Sjeremylt     // Basis action
266d1d35e2fSjeremylt     switch (eval_mode) {
2674a2e7687Sjeremylt       case CEED_EVAL_NONE:
2682b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i][e * Q * size]));
2694a2e7687Sjeremylt         break;
2704a2e7687Sjeremylt       case CEED_EVAL_INTERP:
2714a2e7687Sjeremylt       case CEED_EVAL_GRAD:
272a915a514Srezgarshakeri       case CEED_EVAL_DIV:
273c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
274a915a514Srezgarshakeri         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
275a915a514Srezgarshakeri         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
276a915a514Srezgarshakeri         CeedCallBackend(CeedVectorSetArray(impl->e_vecs_in[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i][e * elem_size * num_comp]));
277ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, block_size, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs_in[i], impl->q_vecs_in[i]));
278a915a514Srezgarshakeri         break;
2794a2e7687Sjeremylt       case CEED_EVAL_WEIGHT:
2804a2e7687Sjeremylt         break;  // No action
2814a2e7687Sjeremylt     }
28289c6efa4Sjeremylt   }
283e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
28489c6efa4Sjeremylt }
2854a2e7687Sjeremylt 
286f10650afSjeremylt //------------------------------------------------------------------------------
287f10650afSjeremylt // Output Basis Action
288f10650afSjeremylt //------------------------------------------------------------------------------
2892b730f8bSJeremy L Thompson static inline int CeedOperatorOutputBasis_Blocked(CeedInt e, CeedInt Q, CeedQFunctionField *qf_output_fields, CeedOperatorField *op_output_fields,
290ad70ee2cSJeremy L Thompson                                                   CeedInt block_size, CeedInt num_input_fields, CeedInt num_output_fields, CeedOperator op,
2912b730f8bSJeremy L Thompson                                                   CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
292ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
293a915a514Srezgarshakeri     CeedInt             elem_size, num_comp;
294d1d35e2fSjeremylt     CeedEvalMode        eval_mode;
295edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2961d102b48SJeremy L Thompson     CeedBasis           basis;
2971d102b48SJeremy L Thompson 
298d1d35e2fSjeremylt     // Get elem_size, eval_mode, size
299edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
300edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
3012b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
3024a2e7687Sjeremylt     // Basis action
303d1d35e2fSjeremylt     switch (eval_mode) {
3044a2e7687Sjeremylt       case CEED_EVAL_NONE:
3054a2e7687Sjeremylt         break;  // No action
3064a2e7687Sjeremylt       case CEED_EVAL_INTERP:
3074a2e7687Sjeremylt       case CEED_EVAL_GRAD:
308a915a514Srezgarshakeri       case CEED_EVAL_DIV:
309c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL:
310a915a514Srezgarshakeri         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
311a915a514Srezgarshakeri         CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
312a915a514Srezgarshakeri         CeedCallBackend(
313a915a514Srezgarshakeri             CeedVectorSetArray(impl->e_vecs_out[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i + num_input_fields][e * elem_size * num_comp]));
314ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedBasisApply(basis, block_size, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs_out[i]));
315a915a514Srezgarshakeri         break;
316c042f62fSJeremy L Thompson       // LCOV_EXCL_START
317bbfacfcdSjeremylt       case CEED_EVAL_WEIGHT: {
3184ce2993fSjeremylt         Ceed ceed;
319bcbe1c99SJeremy L Thompson 
3202b730f8bSJeremy L Thompson         CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
3212b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
322bbfacfcdSjeremylt         // LCOV_EXCL_STOP
3234a2e7687Sjeremylt       }
32489c6efa4Sjeremylt     }
32589c6efa4Sjeremylt   }
326e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3271d102b48SJeremy L Thompson }
3281d102b48SJeremy L Thompson 
329f10650afSjeremylt //------------------------------------------------------------------------------
330f10650afSjeremylt // Restore Input Vectors
331f10650afSjeremylt //------------------------------------------------------------------------------
3322b730f8bSJeremy L Thompson static inline int CeedOperatorRestoreInputs_Blocked(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
3332b730f8bSJeremy L Thompson                                                     bool skip_active, CeedScalar *e_data_full[2 * CEED_FIELD_MAX], CeedOperator_Blocked *impl) {
334ad70ee2cSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
335d1d35e2fSjeremylt     CeedEvalMode eval_mode;
3361d102b48SJeremy L Thompson 
3371d102b48SJeremy L Thompson     // Skip active inputs
338d1d35e2fSjeremylt     if (skip_active) {
3391d102b48SJeremy L Thompson       CeedVector vec;
340ad70ee2cSJeremy L Thompson 
3412b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3422b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3431d102b48SJeremy L Thompson     }
3442b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
345d1d35e2fSjeremylt     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3461d102b48SJeremy L Thompson     } else {
3472b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs_full[i], (const CeedScalar **)&e_data_full[i]));
3481d102b48SJeremy L Thompson     }
3491d102b48SJeremy L Thompson   }
350e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3511d102b48SJeremy L Thompson }
3521d102b48SJeremy L Thompson 
353f10650afSjeremylt //------------------------------------------------------------------------------
354f10650afSjeremylt // Operator Apply
355f10650afSjeremylt //------------------------------------------------------------------------------
3562b730f8bSJeremy L Thompson static int CeedOperatorApplyAdd_Blocked(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
357d1d35e2fSjeremylt   CeedInt               Q, num_input_fields, num_output_fields, num_elem, size;
358ad70ee2cSJeremy L Thompson   const CeedInt         block_size = 8;
359ad70ee2cSJeremy L Thompson   CeedEvalMode          eval_mode;
360ad70ee2cSJeremy L Thompson   CeedScalar           *e_data_full[2 * CEED_FIELD_MAX] = {0};
361ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
362ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
363ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
364ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
365ad70ee2cSJeremy L Thompson 
366ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3672b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
3682b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
3692b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3702b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
3712b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
372ad70ee2cSJeremy L Thompson   const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size);
3731d102b48SJeremy L Thompson 
3741d102b48SJeremy L Thompson   // Setup
3752b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Blocked(op));
3761d102b48SJeremy L Thompson 
3770b454692Sjeremylt   // Restriction only operator
378edb2538eSJeremy L Thompson   if (impl->is_identity_rstr_op) {
379edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[0], CEED_NOTRANSPOSE, in_vec, impl->e_vecs_full[0], request));
380edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[1], CEED_TRANSPOSE, impl->e_vecs_full[0], out_vec, request));
3810b454692Sjeremylt     return CEED_ERROR_SUCCESS;
3820b454692Sjeremylt   }
3830b454692Sjeremylt 
3841d102b48SJeremy L Thompson   // Input Evecs and Restriction
3852b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data_full, impl, request));
3861d102b48SJeremy L Thompson 
3871d102b48SJeremy L Thompson   // Output Evecs
388d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_output_fields; i++) {
3892b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs_full[i + impl->num_inputs], CEED_MEM_HOST, &e_data_full[i + num_input_fields]));
3901d102b48SJeremy L Thompson   }
3911d102b48SJeremy L Thompson 
3921d102b48SJeremy L Thompson   // Loop through elements
393ad70ee2cSJeremy L Thompson   for (CeedInt e = 0; e < num_blocks * block_size; e += block_size) {
3941d102b48SJeremy L Thompson     // Output pointers
395d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_output_fields; i++) {
3962b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
397d1d35e2fSjeremylt       if (eval_mode == CEED_EVAL_NONE) {
3982b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
3992b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_HOST, CEED_USE_POINTER, &e_data_full[i + num_input_fields][e * Q * size]));
4001d102b48SJeremy L Thompson       }
4011d102b48SJeremy L Thompson     }
4021d102b48SJeremy L Thompson 
40316911fdaSjeremylt     // Input basis apply
404ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, false, e_data_full, impl));
40516911fdaSjeremylt 
4061d102b48SJeremy L Thompson     // Q function
4070b454692Sjeremylt     if (!impl->is_identity_qf) {
408ad70ee2cSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
40916911fdaSjeremylt     }
4101d102b48SJeremy L Thompson 
4111d102b48SJeremy L Thompson     // Output basis apply
412ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorOutputBasis_Blocked(e, Q, qf_output_fields, op_output_fields, block_size, num_input_fields, num_output_fields, op,
4132b730f8bSJeremy L Thompson                                                     e_data_full, impl));
4141d102b48SJeremy L Thompson   }
41589c6efa4Sjeremylt 
41689c6efa4Sjeremylt   // Output restriction
417d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_output_fields; i++) {
418ad70ee2cSJeremy L Thompson     CeedVector vec;
419ad70ee2cSJeremy L Thompson 
42089c6efa4Sjeremylt     // Restore evec
4212b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs_full[i + impl->num_inputs], &e_data_full[i + num_input_fields]));
422d1bcdac9Sjeremylt     // Get output vector
4232b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
42489c6efa4Sjeremylt     // Active
4252b730f8bSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4264a2e7687Sjeremylt     // Restrict
4272b730f8bSJeremy L Thompson     CeedCallBackend(
428edb2538eSJeremy L Thompson         CeedElemRestrictionApply(impl->block_rstr[i + impl->num_inputs], CEED_TRANSPOSE, impl->e_vecs_full[i + impl->num_inputs], vec, request));
4294a2e7687Sjeremylt   }
4304a2e7687Sjeremylt 
4314a2e7687Sjeremylt   // Restore input arrays
4322b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, false, e_data_full, impl));
433e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4341d102b48SJeremy L Thompson }
4351d102b48SJeremy L Thompson 
436f10650afSjeremylt //------------------------------------------------------------------------------
43770a7ffb3SJeremy L Thompson // Core code for assembling linear QFunction
438f10650afSjeremylt //------------------------------------------------------------------------------
4392b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Blocked(CeedOperator op, bool build_objects, CeedVector *assembled,
440a0162de9SJeremy L Thompson                                                                   CeedElemRestriction *rstr, CeedRequest *request) {
441ad70ee2cSJeremy L Thompson   Ceed                  ceed;
442ad70ee2cSJeremy L Thompson   CeedSize              q_size;
443d1d35e2fSjeremylt   CeedInt               Q, num_input_fields, num_output_fields, num_elem, size;
444ad70ee2cSJeremy L Thompson   const CeedInt         block_size = 8;
445ad70ee2cSJeremy L Thompson   CeedScalar           *l_vec_array;
446ad70ee2cSJeremy L Thompson   CeedScalar           *e_data_full[2 * CEED_FIELD_MAX] = {0};
447ad70ee2cSJeremy L Thompson   CeedQFunctionField   *qf_input_fields, *qf_output_fields;
448ad70ee2cSJeremy L Thompson   CeedQFunction         qf;
449ad70ee2cSJeremy L Thompson   CeedOperatorField    *op_input_fields, *op_output_fields;
450ad70ee2cSJeremy L Thompson   CeedOperator_Blocked *impl;
451ad70ee2cSJeremy L Thompson 
452ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
453ad70ee2cSJeremy L Thompson   CeedInt             num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
454ad70ee2cSJeremy L Thompson   CeedVector         *active_in  = impl->qf_active_in;
455ad70ee2cSJeremy L Thompson   CeedVector          l_vec      = impl->qf_l_vec;
456ad70ee2cSJeremy L Thompson   CeedElemRestriction block_rstr = impl->qf_block_rstr;
457ad70ee2cSJeremy L Thompson 
4582b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
4592b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
4602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4612b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
4622b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4632b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
464ad70ee2cSJeremy L Thompson   const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size);
4651d102b48SJeremy L Thompson 
4661d102b48SJeremy L Thompson   // Setup
4672b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Blocked(op));
4681d102b48SJeremy L Thompson 
469506b1a0cSSebastian Grimberg   // Check for restriction only operator
470506b1a0cSSebastian Grimberg   CeedCheck(!impl->is_identity_rstr_op, ceed, CEED_ERROR_BACKEND, "Assembling restriction only operators is not supported");
47116911fdaSjeremylt 
4721d102b48SJeremy L Thompson   // Input Evecs and Restriction
4732b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data_full, impl, request));
4741d102b48SJeremy L Thompson 
4751d102b48SJeremy L Thompson   // Count number of active input fields
476bb219a0fSJeremy L Thompson   if (!num_active_in) {
477d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_input_fields; i++) {
478ad70ee2cSJeremy L Thompson       CeedScalar *q_vec_array;
479ad70ee2cSJeremy L Thompson       CeedVector  vec;
480ad70ee2cSJeremy L Thompson 
4811d102b48SJeremy L Thompson       // Get input vector
4822b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4831d102b48SJeremy L Thompson       // Check if active input
4841d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
4852b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
4862b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
487ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_HOST, &q_vec_array));
4882b730f8bSJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_in));
4891d102b48SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
490ad70ee2cSJeremy L Thompson           q_size = (CeedSize)Q * block_size;
4912b730f8bSJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_in[num_active_in + field]));
492ad70ee2cSJeremy L Thompson           CeedCallBackend(
493ad70ee2cSJeremy L Thompson               CeedVectorSetArray(active_in[num_active_in + field], CEED_MEM_HOST, CEED_USE_POINTER, &q_vec_array[field * Q * block_size]));
4941d102b48SJeremy L Thompson         }
495d1d35e2fSjeremylt         num_active_in += size;
496ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
4971d102b48SJeremy L Thompson       }
4981d102b48SJeremy L Thompson     }
4994fc1f125SJeremy L Thompson     impl->num_active_in = num_active_in;
500bb219a0fSJeremy L Thompson     impl->qf_active_in  = active_in;
501bb219a0fSJeremy L Thompson   }
5021d102b48SJeremy L Thompson 
5031d102b48SJeremy L Thompson   // Count number of active output fields
504bb219a0fSJeremy L Thompson   if (!num_active_out) {
505d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_output_fields; i++) {
506ad70ee2cSJeremy L Thompson       CeedVector vec;
507ad70ee2cSJeremy L Thompson 
5081d102b48SJeremy L Thompson       // Get output vector
5092b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5101d102b48SJeremy L Thompson       // Check if active output
5111d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
5122b730f8bSJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
513d1d35e2fSjeremylt         num_active_out += size;
5141d102b48SJeremy L Thompson       }
5151d102b48SJeremy L Thompson     }
5164fc1f125SJeremy L Thompson     impl->num_active_out = num_active_out;
517bb219a0fSJeremy L Thompson   }
5181d102b48SJeremy L Thompson 
5191d102b48SJeremy L Thompson   // Check sizes
5206574a04fSJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
5211d102b48SJeremy L Thompson 
522d1d35e2fSjeremylt   // Setup Lvec
5234fc1f125SJeremy L Thompson   if (!l_vec) {
524ad70ee2cSJeremy L Thompson     const CeedSize l_size = (CeedSize)num_blocks * block_size * Q * num_active_in * num_active_out;
525ad70ee2cSJeremy L Thompson 
5262b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, &l_vec));
5274fc1f125SJeremy L Thompson     impl->qf_l_vec = l_vec;
528bb219a0fSJeremy L Thompson   }
529ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(l_vec, CEED_MEM_HOST, &l_vec_array));
530ad70ee2cSJeremy L Thompson 
531ad70ee2cSJeremy L Thompson   // Setup block restriction
532ad70ee2cSJeremy L Thompson   if (!block_rstr) {
533ad70ee2cSJeremy L Thompson     const CeedInt strides[3] = {1, Q, num_active_in * num_active_out * Q};
534ad70ee2cSJeremy L Thompson 
535ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateBlockedStrided(ceed, num_elem, Q, block_size, num_active_in * num_active_out,
536ad70ee2cSJeremy L Thompson                                                             num_active_in * num_active_out * num_elem * Q, strides, &block_rstr));
537ad70ee2cSJeremy L Thompson     impl->qf_block_rstr = block_rstr;
538ad70ee2cSJeremy L Thompson   }
5391d102b48SJeremy L Thompson 
54070a7ffb3SJeremy L Thompson   // Build objects if needed
54170a7ffb3SJeremy L Thompson   if (build_objects) {
542ad70ee2cSJeremy L Thompson     const CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
543ad70ee2cSJeremy L Thompson     const CeedInt  strides[3] = {1, Q, num_active_in * num_active_out * Q};
544ad70ee2cSJeremy L Thompson 
54570a7ffb3SJeremy L Thompson     // Create output restriction
5462b730f8bSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed, num_elem, Q, num_active_in * num_active_out, num_active_in * num_active_out * num_elem * Q,
5472b730f8bSJeremy L Thompson                                                      strides, rstr));
5481d102b48SJeremy L Thompson     // Create assembled vector
5492b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed, l_size, assembled));
55070a7ffb3SJeremy L Thompson   }
5511d102b48SJeremy L Thompson 
5521d102b48SJeremy L Thompson   // Loop through elements
553ad70ee2cSJeremy L Thompson   for (CeedInt e = 0; e < num_blocks * block_size; e += block_size) {
5541d102b48SJeremy L Thompson     // Input basis apply
555ad70ee2cSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Blocked(e, Q, qf_input_fields, op_input_fields, num_input_fields, block_size, true, e_data_full, impl));
5561d102b48SJeremy L Thompson 
5571d102b48SJeremy L Thompson     // Assemble QFunction
558d1d35e2fSjeremylt     for (CeedInt in = 0; in < num_active_in; in++) {
5591d102b48SJeremy L Thompson       // Set Inputs
5602b730f8bSJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_in[in], 1.0));
561d1d35e2fSjeremylt       if (num_active_in > 1) {
5622b730f8bSJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(active_in[(in + num_active_in - 1) % num_active_in], 0.0));
56342ea3801Sjeremylt       }
564506b1a0cSSebastian Grimberg       if (!impl->is_identity_qf) {
5651d102b48SJeremy L Thompson         // Set Outputs
566d1d35e2fSjeremylt         for (CeedInt out = 0; out < num_output_fields; out++) {
567ad70ee2cSJeremy L Thompson           CeedVector vec;
568ad70ee2cSJeremy L Thompson 
5691d102b48SJeremy L Thompson           // Get output vector
5702b730f8bSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
5711d102b48SJeremy L Thompson           // Check if active output
5721d102b48SJeremy L Thompson           if (vec == CEED_VECTOR_ACTIVE) {
573ad70ee2cSJeremy L Thompson             CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_HOST, CEED_USE_POINTER, l_vec_array));
5742b730f8bSJeremy L Thompson             CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
575ad70ee2cSJeremy L Thompson             l_vec_array += size * Q * block_size;  // Advance the pointer by the size of the output
5761d102b48SJeremy L Thompson           }
5771d102b48SJeremy L Thompson         }
5781d102b48SJeremy L Thompson         // Apply QFunction
579ad70ee2cSJeremy L Thompson         CeedCallBackend(CeedQFunctionApply(qf, Q * block_size, impl->q_vecs_in, impl->q_vecs_out));
580506b1a0cSSebastian Grimberg       } else {
581506b1a0cSSebastian Grimberg         const CeedScalar *q_vec_array;
582506b1a0cSSebastian Grimberg 
583506b1a0cSSebastian Grimberg         // Copy Identity Outputs
584506b1a0cSSebastian Grimberg         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[0], &size));
585506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorGetArrayRead(impl->q_vecs_out[0], CEED_MEM_HOST, &q_vec_array));
586506b1a0cSSebastian Grimberg         for (CeedInt i = 0; i < size * Q * block_size; i++) l_vec_array[i] = q_vec_array[i];
587506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorRestoreArrayRead(impl->q_vecs_out[0], &q_vec_array));
588506b1a0cSSebastian Grimberg         l_vec_array += size * Q * block_size;
589506b1a0cSSebastian Grimberg       }
5904a2e7687Sjeremylt     }
5914a2e7687Sjeremylt   }
5924a2e7687Sjeremylt 
5931d102b48SJeremy L Thompson   // Un-set output Qvecs to prevent accidental overwrite of Assembled
594506b1a0cSSebastian Grimberg   if (!impl->is_identity_qf) {
595d1d35e2fSjeremylt     for (CeedInt out = 0; out < num_output_fields; out++) {
596ad70ee2cSJeremy L Thompson       CeedVector vec;
597ad70ee2cSJeremy L Thompson 
5981d102b48SJeremy L Thompson       // Get output vector
5992b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
6001d102b48SJeremy L Thompson       // Check if active output
6011d102b48SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
602506b1a0cSSebastian Grimberg         CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_HOST, NULL));
603506b1a0cSSebastian Grimberg       }
6041d102b48SJeremy L Thompson     }
6051d102b48SJeremy L Thompson   }
6061d102b48SJeremy L Thompson 
6071d102b48SJeremy L Thompson   // Restore input arrays
6082b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, true, e_data_full, impl));
6091d102b48SJeremy L Thompson 
6101d102b48SJeremy L Thompson   // Output blocked restriction
611ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(l_vec, &l_vec_array));
6122b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
613ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(block_rstr, CEED_TRANSPOSE, l_vec, *assembled, request));
614e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6154a2e7687Sjeremylt }
6164a2e7687Sjeremylt 
617f10650afSjeremylt //------------------------------------------------------------------------------
61870a7ffb3SJeremy L Thompson // Assemble Linear QFunction
61970a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6202b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Blocked(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
6212b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, true, assembled, rstr, request);
62270a7ffb3SJeremy L Thompson }
62370a7ffb3SJeremy L Thompson 
62470a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
62570a7ffb3SJeremy L Thompson // Update Assembled Linear QFunction
62670a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
6272b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Blocked(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
6282b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Blocked(op, false, &assembled, &rstr, request);
62970a7ffb3SJeremy L Thompson }
63070a7ffb3SJeremy L Thompson 
63170a7ffb3SJeremy L Thompson //------------------------------------------------------------------------------
632f10650afSjeremylt // Operator Destroy
633f10650afSjeremylt //------------------------------------------------------------------------------
634f10650afSjeremylt static int CeedOperatorDestroy_Blocked(CeedOperator op) {
635f10650afSjeremylt   CeedOperator_Blocked *impl;
636ad70ee2cSJeremy L Thompson 
6372b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
638f10650afSjeremylt 
6394fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
640edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->block_rstr[i]));
6412b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_full[i]));
642f10650afSjeremylt   }
643edb2538eSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->block_rstr));
6442b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_full));
6452b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
646f10650afSjeremylt 
6474fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
6482b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_in[i]));
6492b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
650f10650afSjeremylt   }
6512b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_in));
6522b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
653f10650afSjeremylt 
6544fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
6552b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs_out[i]));
6562b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
657f10650afSjeremylt   }
6582b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs_out));
6592b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
660f10650afSjeremylt 
661bb219a0fSJeremy L Thompson   // QFunction assembly data
6624fc1f125SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
6632b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
664bb219a0fSJeremy L Thompson   }
6652b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
6662b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->qf_l_vec));
667ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&impl->qf_block_rstr));
668bb219a0fSJeremy L Thompson 
6692b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
670e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
671f10650afSjeremylt }
672f10650afSjeremylt 
673f10650afSjeremylt //------------------------------------------------------------------------------
674f10650afSjeremylt // Operator Create
675f10650afSjeremylt //------------------------------------------------------------------------------
6764a2e7687Sjeremylt int CeedOperatorCreate_Blocked(CeedOperator op) {
677fe2413ffSjeremylt   Ceed                  ceed;
6784ce2993fSjeremylt   CeedOperator_Blocked *impl;
6794a2e7687Sjeremylt 
680ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
6812b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
6822b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
6832b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Blocked));
6842b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Blocked));
6852b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Blocked));
6862b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Blocked));
687e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6884a2e7687Sjeremylt }
6892a86cc9dSSebastian Grimberg 
690f10650afSjeremylt //------------------------------------------------------------------------------
691