xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-operator.c (revision 41655a2310ae869cbaf66471563d33865f525ec1)
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.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
849aac155SJeremy L Thompson #include <ceed.h>
90d0321e0SJeremy L Thompson #include <ceed/backend.h>
1007b31e0eSJeremy L Thompson #include <ceed/jit-tools.h>
11c85e8640SSebastian Grimberg #include <assert.h>
120d0321e0SJeremy L Thompson #include <stdbool.h>
130d0321e0SJeremy L Thompson #include <string.h>
14c85e8640SSebastian Grimberg #include <hip/hip_runtime.h>
152b730f8bSJeremy L Thompson 
1649aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
170d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
182b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
190d0321e0SJeremy L Thompson 
200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
210d0321e0SJeremy L Thompson // Destroy operator
220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
230d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Hip(CeedOperator op) {
240d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
25b7453713SJeremy L Thompson 
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
270d0321e0SJeremy L Thompson 
280d0321e0SJeremy L Thompson   // Apply data
293aab95c0SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_in));
30f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_out));
31f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->apply_add_basis_out));
32b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
33b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
340d0321e0SJeremy L Thompson   }
35b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
36c1222711SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
370d0321e0SJeremy L Thompson 
38b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
39b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
400d0321e0SJeremy L Thompson   }
41b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
420d0321e0SJeremy L Thompson 
43b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
44b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
450d0321e0SJeremy L Thompson   }
46b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
4767d9480aSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem));
480d0321e0SJeremy L Thompson 
49b2165e7aSSebastian Grimberg   // QFunction assembly data
50b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
51b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
520d0321e0SJeremy L Thompson   }
53b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
540d0321e0SJeremy L Thompson 
550d0321e0SJeremy L Thompson   // Diag data
560d0321e0SJeremy L Thompson   if (impl->diag) {
570d0321e0SJeremy L Thompson     Ceed ceed;
58b7453713SJeremy L Thompson 
592b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
60cbfe683aSSebastian Grimberg     if (impl->diag->module) {
612b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
62cbfe683aSSebastian Grimberg     }
63cbfe683aSSebastian Grimberg     if (impl->diag->module_point_block) {
64cbfe683aSSebastian Grimberg       CeedCallHip(ceed, hipModuleUnload(impl->diag->module_point_block));
65cbfe683aSSebastian Grimberg     }
66004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_in));
67004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_out));
682b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
69b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_in));
70b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_out));
71b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_in));
72b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_out));
73004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_in));
74004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_out));
75004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_in));
76004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_out));
77004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
78b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
79b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
80b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
810d0321e0SJeremy L Thompson   }
822b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
830d0321e0SJeremy L Thompson 
84a835093fSnbeams   if (impl->asmb) {
85a835093fSnbeams     Ceed ceed;
86b7453713SJeremy L Thompson 
872b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
882b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
892b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
902b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
91a835093fSnbeams   }
922b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
93a835093fSnbeams 
942b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
950d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
960d0321e0SJeremy L Thompson }
970d0321e0SJeremy L Thompson 
980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
990d0321e0SJeremy L Thompson // Setup infields or outfields
1000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
101f8a0df59SJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, bool *skip_rstr, bool *apply_add_basis,
102f8a0df59SJeremy L Thompson                                        CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
1030d0321e0SJeremy L Thompson   Ceed                ceed;
104b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
105b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
1060d0321e0SJeremy L Thompson 
107b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
108b7453713SJeremy L Thompson   if (is_input) {
109b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
110b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1110d0321e0SJeremy L Thompson   } else {
112b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
113b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1140d0321e0SJeremy L Thompson   }
1150d0321e0SJeremy L Thompson 
1160d0321e0SJeremy L Thompson   // Loop over fields
117b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
118004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
119b7453713SJeremy L Thompson     CeedSize     q_size;
120004e4986SSebastian Grimberg     CeedInt      size;
121004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
122b7453713SJeremy L Thompson     CeedBasis    basis;
1230d0321e0SJeremy L Thompson 
124004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
125004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
126004e4986SSebastian Grimberg       CeedElemRestriction elem_rstr;
1270d0321e0SJeremy L Thompson 
1280d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
129004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
130004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1310d0321e0SJeremy L Thompson 
1320d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
133b7453713SJeremy L Thompson       if (is_input) {
134004e4986SSebastian Grimberg         CeedVector vec;
135004e4986SSebastian Grimberg 
136004e4986SSebastian Grimberg         // Check for passive input
137b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
138b7453713SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
139004e4986SSebastian Grimberg           // Check eval_mode
140004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1410d0321e0SJeremy L Thompson             // Check for strided restriction
142b7453713SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
143b7453713SJeremy L Thompson             if (is_strided) {
1440d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
145b7453713SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1460d0321e0SJeremy L Thompson             }
1470d0321e0SJeremy L Thompson           }
1480d0321e0SJeremy L Thompson         }
1490d0321e0SJeremy L Thompson       }
150b7453713SJeremy L Thompson       if (skip_restriction) {
151ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
152b7453713SJeremy L Thompson         e_vecs[i + start_e] = NULL;
1530d0321e0SJeremy L Thompson       } else {
154b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1550d0321e0SJeremy L Thompson       }
1560d0321e0SJeremy L Thompson     }
1570d0321e0SJeremy L Thompson 
158004e4986SSebastian Grimberg     switch (eval_mode) {
1590d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
160b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
161b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
162b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1630d0321e0SJeremy L Thompson         break;
1640d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1650d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
166004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
167004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
168b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
169b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
170b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1710d0321e0SJeremy L Thompson         break;
1720d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
173b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
174b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
175b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
17667d9480aSJeremy L Thompson         if (is_at_points) {
17767d9480aSJeremy L Thompson           CeedInt num_points[num_elem];
17867d9480aSJeremy L Thompson 
17967d9480aSJeremy L Thompson           for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q;
18067d9480aSJeremy L Thompson           CeedCallBackend(
18167d9480aSJeremy L Thompson               CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i]));
18267d9480aSJeremy L Thompson         } else {
183b7453713SJeremy L Thompson           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
18467d9480aSJeremy L Thompson         }
1850d0321e0SJeremy L Thompson         break;
1860d0321e0SJeremy L Thompson     }
1870d0321e0SJeremy L Thompson   }
188f8a0df59SJeremy L Thompson   // Drop duplicate restrictions
1893aab95c0SJeremy L Thompson   if (is_input) {
1903aab95c0SJeremy L Thompson     for (CeedInt i = 0; i < num_fields; i++) {
1913aab95c0SJeremy L Thompson       CeedVector          vec_i;
1923aab95c0SJeremy L Thompson       CeedElemRestriction rstr_i;
1933aab95c0SJeremy L Thompson 
1943aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
1953aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
1963aab95c0SJeremy L Thompson       for (CeedInt j = i + 1; j < num_fields; j++) {
1973aab95c0SJeremy L Thompson         CeedVector          vec_j;
1983aab95c0SJeremy L Thompson         CeedElemRestriction rstr_j;
1993aab95c0SJeremy L Thompson 
2003aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
2013aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
2023aab95c0SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
203f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e]));
2043aab95c0SJeremy L Thompson           skip_rstr[j] = true;
2053aab95c0SJeremy L Thompson         }
2063aab95c0SJeremy L Thompson       }
2073aab95c0SJeremy L Thompson     }
208f8a0df59SJeremy L Thompson   } else {
209f8a0df59SJeremy L Thompson     for (CeedInt i = num_fields - 1; i >= 0; i--) {
210f8a0df59SJeremy L Thompson       CeedVector          vec_i;
211f8a0df59SJeremy L Thompson       CeedElemRestriction rstr_i;
212f8a0df59SJeremy L Thompson 
213f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
214f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
215f8a0df59SJeremy L Thompson       for (CeedInt j = i - 1; j >= 0; j--) {
216f8a0df59SJeremy L Thompson         CeedVector          vec_j;
217f8a0df59SJeremy L Thompson         CeedElemRestriction rstr_j;
218f8a0df59SJeremy L Thompson 
219f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
220f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
221f8a0df59SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
222f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e]));
223f8a0df59SJeremy L Thompson           skip_rstr[j]       = true;
224f8a0df59SJeremy L Thompson           apply_add_basis[i] = true;
225f8a0df59SJeremy L Thompson         }
226f8a0df59SJeremy L Thompson       }
227f8a0df59SJeremy L Thompson     }
2283aab95c0SJeremy L Thompson   }
2290d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2300d0321e0SJeremy L Thompson }
2310d0321e0SJeremy L Thompson 
2320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
233ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
2340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2350d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
2360d0321e0SJeremy L Thompson   Ceed                ceed;
237b7453713SJeremy L Thompson   bool                is_setup_done;
238b7453713SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
239b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
2400d0321e0SJeremy L Thompson   CeedQFunction       qf;
241b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
242b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
243b7453713SJeremy L Thompson 
244b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
245b7453713SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
246b7453713SJeremy L Thompson 
247b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
248b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
2492b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
2502b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
251b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
252b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
253b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2540d0321e0SJeremy L Thompson 
2550d0321e0SJeremy L Thompson   // Allocate
256b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
2573aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
258f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
259f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
260c1222711SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
261b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
262b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
263b7453713SJeremy L Thompson   impl->num_inputs  = num_input_fields;
264b7453713SJeremy L Thompson   impl->num_outputs = num_output_fields;
2650d0321e0SJeremy L Thompson 
266*41655a23SJeremy L Thompson   // Set up infield and outfield e-vecs and q-vecs
2670d0321e0SJeremy L Thompson   // Infields
2683aab95c0SJeremy L Thompson   CeedCallBackend(
269f8a0df59SJeremy L Thompson       CeedOperatorSetupFields_Hip(qf, op, true, false, impl->skip_rstr_in, NULL, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2700d0321e0SJeremy L Thompson   // Outfields
271f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, false, impl->skip_rstr_out, impl->apply_add_basis_out, impl->e_vecs, impl->q_vecs_out,
272f8a0df59SJeremy L Thompson                                               num_input_fields, num_output_fields, Q, num_elem));
2730d0321e0SJeremy L Thompson 
274*41655a23SJeremy L Thompson   // Reuse active e-vecs where able
275*41655a23SJeremy L Thompson   {
276*41655a23SJeremy L Thompson     CeedInt              num_used  = 0;
277*41655a23SJeremy L Thompson     CeedElemRestriction *rstr_used = NULL;
278*41655a23SJeremy L Thompson 
279*41655a23SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
280*41655a23SJeremy L Thompson       bool                is_used = false;
281*41655a23SJeremy L Thompson       CeedVector          vec_i;
282*41655a23SJeremy L Thompson       CeedElemRestriction rstr_i;
283*41655a23SJeremy L Thompson 
284*41655a23SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
285*41655a23SJeremy L Thompson       if (vec_i != CEED_VECTOR_ACTIVE) continue;
286*41655a23SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
287*41655a23SJeremy L Thompson       for (CeedInt j = 0; j < num_used; j++) {
288*41655a23SJeremy L Thompson         if (rstr_i == rstr_used[i]) is_used = true;
289*41655a23SJeremy L Thompson       }
290*41655a23SJeremy L Thompson       if (is_used) continue;
291*41655a23SJeremy L Thompson       num_used++;
292*41655a23SJeremy L Thompson       if (num_used == 1) CeedCallBackend(CeedCalloc(num_used, &rstr_used));
293*41655a23SJeremy L Thompson       else CeedCallBackend(CeedRealloc(num_used, &rstr_used));
294*41655a23SJeremy L Thompson       rstr_used[num_used - 1] = rstr_i;
295*41655a23SJeremy L Thompson       for (CeedInt j = num_output_fields - 1; j >= 0; j--) {
296*41655a23SJeremy L Thompson         CeedEvalMode        eval_mode;
297*41655a23SJeremy L Thompson         CeedVector          vec_j;
298*41655a23SJeremy L Thompson         CeedElemRestriction rstr_j;
299*41655a23SJeremy L Thompson 
300*41655a23SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec_j));
301*41655a23SJeremy L Thompson         if (vec_j != CEED_VECTOR_ACTIVE) continue;
302*41655a23SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
303*41655a23SJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) continue;
304*41655a23SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &rstr_j));
305*41655a23SJeremy L Thompson         if (rstr_i == rstr_j) {
306*41655a23SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(impl->e_vecs[i], &impl->e_vecs[j + impl->num_inputs]));
307*41655a23SJeremy L Thompson         }
308*41655a23SJeremy L Thompson       }
309*41655a23SJeremy L Thompson     }
310*41655a23SJeremy L Thompson     CeedCallBackend(CeedFree(&rstr_used));
311*41655a23SJeremy L Thompson   }
3122b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
3130d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3140d0321e0SJeremy L Thompson }
3150d0321e0SJeremy L Thompson 
3160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3170d0321e0SJeremy L Thompson // Setup Operator Inputs
3180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
319b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
320b7453713SJeremy L Thompson                                               CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
321b7453713SJeremy L Thompson                                               CeedOperator_Hip *impl, CeedRequest *request) {
322b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
323004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
3240d0321e0SJeremy L Thompson     CeedVector          vec;
325b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
3260d0321e0SJeremy L Thompson 
3270d0321e0SJeremy L Thompson     // Get input vector
328b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3290d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
330b7453713SJeremy L Thompson       if (skip_active) continue;
331b7453713SJeremy L Thompson       else vec = in_vec;
3320d0321e0SJeremy L Thompson     }
3330d0321e0SJeremy L Thompson 
334004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
335004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3360d0321e0SJeremy L Thompson     } else {
3370d0321e0SJeremy L Thompson       // Get input vector
338b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3390d0321e0SJeremy L Thompson       // Get input element restriction
340b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
341b7453713SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
3420d0321e0SJeremy L Thompson       // Restrict, if necessary
343b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {
3440d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
345b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3460d0321e0SJeremy L Thompson       } else {
347c1222711SJeremy L Thompson         uint64_t state;
348c1222711SJeremy L Thompson 
349c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
350*41655a23SJeremy L Thompson         if ((state != impl->input_states[i] || vec == in_vec) && !impl->skip_rstr_in[i]) {
351b7453713SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
352c1222711SJeremy L Thompson         }
3533aab95c0SJeremy L Thompson         impl->input_states[i] = state;
3540d0321e0SJeremy L Thompson         // Get evec
355b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3560d0321e0SJeremy L Thompson       }
3570d0321e0SJeremy L Thompson     }
3580d0321e0SJeremy L Thompson   }
3590d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3600d0321e0SJeremy L Thompson }
3610d0321e0SJeremy L Thompson 
3620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3630d0321e0SJeremy L Thompson // Input Basis Action
3640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
365b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
366b7453713SJeremy L Thompson                                              CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
3672b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
368b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
369b7453713SJeremy L Thompson     CeedInt             elem_size, size;
370004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
371b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
3720d0321e0SJeremy L Thompson     CeedBasis           basis;
3730d0321e0SJeremy L Thompson 
3740d0321e0SJeremy L Thompson     // Skip active input
375b7453713SJeremy L Thompson     if (skip_active) {
3760d0321e0SJeremy L Thompson       CeedVector vec;
377b7453713SJeremy L Thompson 
378b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3792b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3800d0321e0SJeremy L Thompson     }
381004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
382b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
383b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
384004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
385b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3860d0321e0SJeremy L Thompson     // Basis action
387004e4986SSebastian Grimberg     switch (eval_mode) {
3880d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
389b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
3900d0321e0SJeremy L Thompson         break;
3910d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3920d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
393004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
394004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
395b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
396004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3970d0321e0SJeremy L Thompson         break;
3980d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
3990d0321e0SJeremy L Thompson         break;  // No action
4000d0321e0SJeremy L Thompson     }
4010d0321e0SJeremy L Thompson   }
4020d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4030d0321e0SJeremy L Thompson }
4040d0321e0SJeremy L Thompson 
4050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4060d0321e0SJeremy L Thompson // Restore Input Vectors
4070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
408b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
409b7453713SJeremy L Thompson                                                 const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
410b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
411004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
4120d0321e0SJeremy L Thompson     CeedVector   vec;
413004e4986SSebastian Grimberg 
4140d0321e0SJeremy L Thompson     // Skip active input
415b7453713SJeremy L Thompson     if (skip_active) {
416b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4172b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
4180d0321e0SJeremy L Thompson     }
419004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
420004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
4210d0321e0SJeremy L Thompson     } else {
422b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
423b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
424b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
4250d0321e0SJeremy L Thompson       } else {
426b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
4270d0321e0SJeremy L Thompson       }
4280d0321e0SJeremy L Thompson     }
4290d0321e0SJeremy L Thompson   }
4300d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4310d0321e0SJeremy L Thompson }
4320d0321e0SJeremy L Thompson 
4330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4340d0321e0SJeremy L Thompson // Apply and add to output
4350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
436b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
437b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
438b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
439b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
4400d0321e0SJeremy L Thompson   CeedQFunction       qf;
441b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
442b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
443b7453713SJeremy L Thompson 
444b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4452b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4462b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
447b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
448b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
449b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4500d0321e0SJeremy L Thompson 
4510d0321e0SJeremy L Thompson   // Setup
4522b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4530d0321e0SJeremy L Thompson 
4540d0321e0SJeremy L Thompson   // Input Evecs and Restriction
455b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
4560d0321e0SJeremy L Thompson 
4570d0321e0SJeremy L Thompson   // Input basis apply if needed
458b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
4590d0321e0SJeremy L Thompson 
4600d0321e0SJeremy L Thompson   // Output pointers, as necessary
461b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
462004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
463b7453713SJeremy L Thompson 
464004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
465004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
4660d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
467b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
468b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
4690d0321e0SJeremy L Thompson     }
4700d0321e0SJeremy L Thompson   }
4710d0321e0SJeremy L Thompson 
4720d0321e0SJeremy L Thompson   // Q function
473b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
4740d0321e0SJeremy L Thompson 
475*41655a23SJeremy L Thompson   // Restore input arrays
476*41655a23SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
477*41655a23SJeremy L Thompson 
4780d0321e0SJeremy L Thompson   // Output basis apply if needed
479b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
480004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
481b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
482b7453713SJeremy L Thompson     CeedBasis           basis;
483b7453713SJeremy L Thompson 
484004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
485b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
486b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
487004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
488b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4890d0321e0SJeremy L Thompson     // Basis action
490004e4986SSebastian Grimberg     switch (eval_mode) {
4910d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
492004e4986SSebastian Grimberg         break;  // No action
4930d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4940d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
495004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
496004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
497b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
498f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
499f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAdd(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
500f8a0df59SJeremy L Thompson         } else {
501004e4986SSebastian Grimberg           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
502f8a0df59SJeremy L Thompson         }
5030d0321e0SJeremy L Thompson         break;
5040d0321e0SJeremy L Thompson       // LCOV_EXCL_START
5050d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
5066e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
5070d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
5080d0321e0SJeremy L Thompson       }
5090d0321e0SJeremy L Thompson     }
510004e4986SSebastian Grimberg   }
5110d0321e0SJeremy L Thompson 
5120d0321e0SJeremy L Thompson   // Output restriction
513b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
514004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
515b7453713SJeremy L Thompson     CeedVector          vec;
516b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
517b7453713SJeremy L Thompson 
5180d0321e0SJeremy L Thompson     // Restore evec
519004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
520004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
521b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
5220d0321e0SJeremy L Thompson     }
523f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
5240d0321e0SJeremy L Thompson     // Get output vector
525b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5260d0321e0SJeremy L Thompson     // Restrict
527b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
5280d0321e0SJeremy L Thompson     // Active
529b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
5300d0321e0SJeremy L Thompson 
531b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
5320d0321e0SJeremy L Thompson   }
5330d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5340d0321e0SJeremy L Thompson }
5350d0321e0SJeremy L Thompson 
5360d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
53767d9480aSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
53867d9480aSJeremy L Thompson //------------------------------------------------------------------------------
53967d9480aSJeremy L Thompson static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) {
54067d9480aSJeremy L Thompson   Ceed                ceed;
54167d9480aSJeremy L Thompson   bool                is_setup_done;
54267d9480aSJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
54367d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
54467d9480aSJeremy L Thompson   CeedQFunction       qf;
54567d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
54667d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
54767d9480aSJeremy L Thompson 
54867d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
54967d9480aSJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
55067d9480aSJeremy L Thompson 
55167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
55267d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
55367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
55467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
55567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
55667d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
55767d9480aSJeremy L Thompson   {
55867d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr = NULL;
55967d9480aSJeremy L Thompson 
56067d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL));
56167d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points));
56267d9480aSJeremy L Thompson   }
56367d9480aSJeremy L Thompson   impl->max_num_points = max_num_points;
56467d9480aSJeremy L Thompson 
56567d9480aSJeremy L Thompson   // Allocate
56667d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
5673aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
568f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
569f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
57067d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
57167d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
57267d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
57367d9480aSJeremy L Thompson   impl->num_inputs  = num_input_fields;
57467d9480aSJeremy L Thompson   impl->num_outputs = num_output_fields;
57567d9480aSJeremy L Thompson 
57667d9480aSJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
57767d9480aSJeremy L Thompson   // Infields
578f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, true, impl->skip_rstr_in, NULL, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields,
5793aab95c0SJeremy L Thompson                                               max_num_points, num_elem));
58067d9480aSJeremy L Thompson   // Outfields
581f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, true, impl->skip_rstr_out, impl->apply_add_basis_out, impl->e_vecs, impl->q_vecs_out,
582f8a0df59SJeremy L Thompson                                               num_input_fields, num_output_fields, max_num_points, num_elem));
58367d9480aSJeremy L Thompson 
58467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
58567d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
58667d9480aSJeremy L Thompson }
58767d9480aSJeremy L Thompson 
58867d9480aSJeremy L Thompson //------------------------------------------------------------------------------
58967d9480aSJeremy L Thompson // Input Basis Action AtPoints
59067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
59167d9480aSJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Hip(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields,
59267d9480aSJeremy L Thompson                                                      CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active,
59367d9480aSJeremy L Thompson                                                      CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
59467d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
59567d9480aSJeremy L Thompson     CeedInt             elem_size, size;
59667d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
59767d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
59867d9480aSJeremy L Thompson     CeedBasis           basis;
59967d9480aSJeremy L Thompson 
60067d9480aSJeremy L Thompson     // Skip active input
60167d9480aSJeremy L Thompson     if (skip_active) {
60267d9480aSJeremy L Thompson       CeedVector vec;
60367d9480aSJeremy L Thompson 
60467d9480aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
60567d9480aSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
60667d9480aSJeremy L Thompson     }
60767d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
60867d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
60967d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
61067d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
61167d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
61267d9480aSJeremy L Thompson     // Basis action
61367d9480aSJeremy L Thompson     switch (eval_mode) {
61467d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
61567d9480aSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
61667d9480aSJeremy L Thompson         break;
61767d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
61867d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
61967d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
62067d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
62167d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
62267d9480aSJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
62367d9480aSJeremy L Thompson                                                impl->q_vecs_in[i]));
62467d9480aSJeremy L Thompson         break;
62567d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT:
62667d9480aSJeremy L Thompson         break;  // No action
62767d9480aSJeremy L Thompson     }
62867d9480aSJeremy L Thompson   }
62967d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
63067d9480aSJeremy L Thompson }
63167d9480aSJeremy L Thompson 
63267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
63367d9480aSJeremy L Thompson // Apply and add to output AtPoints
63467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
63567d9480aSJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
63667d9480aSJeremy L Thompson   CeedInt             max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
63767d9480aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
63867d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
63967d9480aSJeremy L Thompson   CeedQFunction       qf;
64067d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
64167d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
64267d9480aSJeremy L Thompson 
64367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
64467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
64567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
64667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
64767d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
64867d9480aSJeremy L Thompson   CeedInt num_points[num_elem];
64967d9480aSJeremy L Thompson 
65067d9480aSJeremy L Thompson   // Setup
65167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
65267d9480aSJeremy L Thompson   max_num_points = impl->max_num_points;
65367d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
65467d9480aSJeremy L Thompson 
65567d9480aSJeremy L Thompson   // Input Evecs and Restriction
65667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
65767d9480aSJeremy L Thompson 
65867d9480aSJeremy L Thompson   // Get point coordinates
65967d9480aSJeremy L Thompson   if (!impl->point_coords_elem) {
66067d9480aSJeremy L Thompson     CeedVector          point_coords = NULL;
66167d9480aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
66267d9480aSJeremy L Thompson 
66367d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
66467d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
66567d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
66667d9480aSJeremy L Thompson   }
66767d9480aSJeremy L Thompson 
66867d9480aSJeremy L Thompson   // Input basis apply if needed
66967d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
67067d9480aSJeremy L Thompson 
67167d9480aSJeremy L Thompson   // Output pointers, as necessary
67267d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
67367d9480aSJeremy L Thompson     CeedEvalMode eval_mode;
67467d9480aSJeremy L Thompson 
67567d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
67667d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
67767d9480aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
67867d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
67967d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
68067d9480aSJeremy L Thompson     }
68167d9480aSJeremy L Thompson   }
68267d9480aSJeremy L Thompson 
68367d9480aSJeremy L Thompson   // Q function
68467d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
68567d9480aSJeremy L Thompson 
68667d9480aSJeremy L Thompson   // Output basis apply if needed
68767d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
68867d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
68967d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
69067d9480aSJeremy L Thompson     CeedBasis           basis;
69167d9480aSJeremy L Thompson 
69267d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
69367d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
69467d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
69567d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
69667d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
69767d9480aSJeremy L Thompson     // Basis action
69867d9480aSJeremy L Thompson     switch (eval_mode) {
69967d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
70067d9480aSJeremy L Thompson         break;  // No action
70167d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
70267d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
70367d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
70467d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
70567d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
706f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
707f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAddAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
708f8a0df59SJeremy L Thompson                                                     impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
709f8a0df59SJeremy L Thompson         } else {
71067d9480aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
71167d9480aSJeremy L Thompson                                                  impl->e_vecs[i + impl->num_inputs]));
712f8a0df59SJeremy L Thompson         }
71367d9480aSJeremy L Thompson         break;
71467d9480aSJeremy L Thompson       // LCOV_EXCL_START
71567d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT: {
71667d9480aSJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
71767d9480aSJeremy L Thompson         // LCOV_EXCL_STOP
71867d9480aSJeremy L Thompson       }
71967d9480aSJeremy L Thompson     }
72067d9480aSJeremy L Thompson   }
72167d9480aSJeremy L Thompson 
72267d9480aSJeremy L Thompson   // Output restriction
72367d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
72467d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
72567d9480aSJeremy L Thompson     CeedVector          vec;
72667d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
72767d9480aSJeremy L Thompson 
72867d9480aSJeremy L Thompson     // Restore evec
72967d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
73067d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
73167d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
73267d9480aSJeremy L Thompson     }
733f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
73467d9480aSJeremy L Thompson     // Get output vector
73567d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
73667d9480aSJeremy L Thompson     // Restrict
73767d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
73867d9480aSJeremy L Thompson     // Active
73967d9480aSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
74067d9480aSJeremy L Thompson 
74167d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
74267d9480aSJeremy L Thompson   }
74367d9480aSJeremy L Thompson 
74467d9480aSJeremy L Thompson   // Restore input arrays
74567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
74667d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
74767d9480aSJeremy L Thompson }
74867d9480aSJeremy L Thompson 
74967d9480aSJeremy L Thompson //------------------------------------------------------------------------------
750004e4986SSebastian Grimberg // Linear QFunction Assembly Core
7510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7522b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
7530d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
754b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
755b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
756b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
757004e4986SSebastian Grimberg   CeedVector         *active_inputs;
758b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
759b7453713SJeremy L Thompson   CeedQFunction       qf;
760b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
761b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
762b7453713SJeremy L Thompson 
7632b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
764b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
765e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
766e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
767b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
768004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
769b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
770b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
771004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
772004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
7730d0321e0SJeremy L Thompson 
7740d0321e0SJeremy L Thompson   // Setup
7752b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
7760d0321e0SJeremy L Thompson 
7770d0321e0SJeremy L Thompson   // Input Evecs and Restriction
778b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
7790d0321e0SJeremy L Thompson 
7800d0321e0SJeremy L Thompson   // Count number of active input fields
781b7453713SJeremy L Thompson   if (!num_active_in) {
782b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
783b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
784b7453713SJeremy L Thompson       CeedVector  vec;
785b7453713SJeremy L Thompson 
7860d0321e0SJeremy L Thompson       // Get input vector
787b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
7880d0321e0SJeremy L Thompson       // Check if active input
7890d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
790b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
791b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
792b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
793004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
7940d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
795004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
796004e4986SSebastian Grimberg 
797004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
798b7453713SJeremy L Thompson           CeedCallBackend(
799004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
8000d0321e0SJeremy L Thompson         }
801b7453713SJeremy L Thompson         num_active_in += size;
802b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
8030d0321e0SJeremy L Thompson       }
8040d0321e0SJeremy L Thompson     }
805b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
806004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
8070d0321e0SJeremy L Thompson   }
8080d0321e0SJeremy L Thompson 
8090d0321e0SJeremy L Thompson   // Count number of active output fields
810b7453713SJeremy L Thompson   if (!num_active_out) {
811b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
812b7453713SJeremy L Thompson       CeedVector vec;
813b7453713SJeremy L Thompson 
8140d0321e0SJeremy L Thompson       // Get output vector
815b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
8160d0321e0SJeremy L Thompson       // Check if active output
8170d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
818b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
819b7453713SJeremy L Thompson         num_active_out += size;
8200d0321e0SJeremy L Thompson       }
8210d0321e0SJeremy L Thompson     }
822b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
8230d0321e0SJeremy L Thompson   }
8240d0321e0SJeremy L Thompson 
8250d0321e0SJeremy L Thompson   // Check sizes
826b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
8270d0321e0SJeremy L Thompson 
8280d0321e0SJeremy L Thompson   // Build objects if needed
8290d0321e0SJeremy L Thompson   if (build_objects) {
830b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
831b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
832b7453713SJeremy L Thompson 
833004e4986SSebastian Grimberg     // Create output restriction
834b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
8350a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
8360a5597ceSJeremy L Thompson                                                      rstr));
8370d0321e0SJeremy L Thompson     // Create assembled vector
838b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
8390d0321e0SJeremy L Thompson   }
8402b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
841b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
8420d0321e0SJeremy L Thompson 
8430d0321e0SJeremy L Thompson   // Input basis apply
844b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
8450d0321e0SJeremy L Thompson 
8460d0321e0SJeremy L Thompson   // Assemble QFunction
847b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
8480d0321e0SJeremy L Thompson     // Set Inputs
849004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
850b7453713SJeremy L Thompson     if (num_active_in > 1) {
851004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
8520d0321e0SJeremy L Thompson     }
8530d0321e0SJeremy L Thompson     // Set Outputs
854b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
855b7453713SJeremy L Thompson       CeedVector vec;
856b7453713SJeremy L Thompson 
8570d0321e0SJeremy L Thompson       // Get output vector
858b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
8590d0321e0SJeremy L Thompson       // Check if active output
8600d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
861b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
862b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
863b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
8640d0321e0SJeremy L Thompson       }
8650d0321e0SJeremy L Thompson     }
8660d0321e0SJeremy L Thompson     // Apply QFunction
867b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
8680d0321e0SJeremy L Thompson   }
8690d0321e0SJeremy L Thompson 
870004e4986SSebastian Grimberg   // Un-set output q_vecs to prevent accidental overwrite of Assembled
871b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
872b7453713SJeremy L Thompson     CeedVector vec;
873b7453713SJeremy L Thompson 
8740d0321e0SJeremy L Thompson     // Get output vector
875b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
8760d0321e0SJeremy L Thompson     // Check if active output
8770d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
878b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
8790d0321e0SJeremy L Thompson     }
8800d0321e0SJeremy L Thompson   }
8810d0321e0SJeremy L Thompson 
8820d0321e0SJeremy L Thompson   // Restore input arrays
883b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
8840d0321e0SJeremy L Thompson 
8850d0321e0SJeremy L Thompson   // Restore output
886b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
8870d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8880d0321e0SJeremy L Thompson }
8890d0321e0SJeremy L Thompson 
8900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8910d0321e0SJeremy L Thompson // Assemble Linear QFunction
8920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8932b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
8942b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
8950d0321e0SJeremy L Thompson }
8960d0321e0SJeremy L Thompson 
8970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
898b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
8990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9002b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
9012b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
9020d0321e0SJeremy L Thompson }
9030d0321e0SJeremy L Thompson 
9040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
905004e4986SSebastian Grimberg // Assemble Diagonal Setup
9060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
907cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
9080d0321e0SJeremy L Thompson   Ceed                ceed;
909004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
910cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
911004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
912b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
913b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
9140d0321e0SJeremy L Thompson   CeedQFunction       qf;
915b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
916b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
917b7453713SJeremy L Thompson 
918b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
9192b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
920b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
9210d0321e0SJeremy L Thompson 
9220d0321e0SJeremy L Thompson   // Determine active input basis
923b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
924b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
925b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9260d0321e0SJeremy L Thompson     CeedVector vec;
927b7453713SJeremy L Thompson 
928b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
9290d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
930004e4986SSebastian Grimberg       CeedBasis    basis;
931004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
932b7453713SJeremy L Thompson 
933004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
934004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
935004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
936004e4986SSebastian Grimberg       basis_in = basis;
937004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
938004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
939004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
940004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
941004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
942004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
943004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
9440d0321e0SJeremy L Thompson       }
9450d0321e0SJeremy L Thompson     }
9460d0321e0SJeremy L Thompson   }
9470d0321e0SJeremy L Thompson 
9480d0321e0SJeremy L Thompson   // Determine active output basis
949b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
950b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
951b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9520d0321e0SJeremy L Thompson     CeedVector vec;
953b7453713SJeremy L Thompson 
954b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
9550d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
956004e4986SSebastian Grimberg       CeedBasis    basis;
957004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
958b7453713SJeremy L Thompson 
959004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
960004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
961004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
962004e4986SSebastian Grimberg       basis_out = basis;
963004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
964004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
965004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
966004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
967004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
968004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
969004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
9700d0321e0SJeremy L Thompson       }
9710d0321e0SJeremy L Thompson     }
9720d0321e0SJeremy L Thompson   }
9730d0321e0SJeremy L Thompson 
9740d0321e0SJeremy L Thompson   // Operator data struct
9752b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
9762b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
9770d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
978b7453713SJeremy L Thompson 
979cbfe683aSSebastian Grimberg   // Basis matrices
980004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
981004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
982004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
983004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
984004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
985004e4986SSebastian Grimberg   bool          has_eval_none    = false;
9860d0321e0SJeremy L Thompson 
9870d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
988004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
989004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
990004e4986SSebastian Grimberg   if (has_eval_none) {
9910d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
992b7453713SJeremy L Thompson 
993004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
994004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
995b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
996b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
997004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
9980d0321e0SJeremy L Thompson   }
9990d0321e0SJeremy L Thompson 
1000004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
1001004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
1002004e4986SSebastian Grimberg     CeedFESpace fespace;
1003004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
10040d0321e0SJeremy L Thompson 
1005004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
1006004e4986SSebastian Grimberg     switch (fespace) {
1007004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
1008004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
1009004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
1010004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
10110d0321e0SJeremy L Thompson 
1012004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1013004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
10140d0321e0SJeremy L Thompson 
1015004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1016004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1017004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1018004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
1019004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
1020004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
1021004e4986SSebastian Grimberg         if (in) {
1022004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1023004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
1024004e4986SSebastian Grimberg         } else {
1025004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1026004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
1027004e4986SSebastian Grimberg         }
1028004e4986SSebastian Grimberg       } break;
1029004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
1030004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
1031004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
1032004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
1033004e4986SSebastian Grimberg 
1034004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1035004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
1036004e4986SSebastian Grimberg 
1037004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1038004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1039004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1040004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
1041004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
1042004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
1043004e4986SSebastian Grimberg         if (in) {
1044004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1045004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
1046004e4986SSebastian Grimberg         } else {
1047004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1048004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
1049004e4986SSebastian Grimberg         }
1050004e4986SSebastian Grimberg       } break;
1051004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
1052004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
1053004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
1054004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
1055004e4986SSebastian Grimberg 
1056004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1057004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
1058004e4986SSebastian Grimberg 
1059004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1060004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1061004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1062004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
1063004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
1064004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
1065004e4986SSebastian Grimberg         if (in) {
1066004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1067004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
1068004e4986SSebastian Grimberg         } else {
1069004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1070004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
1071004e4986SSebastian Grimberg         }
1072004e4986SSebastian Grimberg       } break;
1073004e4986SSebastian Grimberg     }
1074004e4986SSebastian Grimberg   }
1075004e4986SSebastian Grimberg 
1076004e4986SSebastian Grimberg   // Arrays of eval_modes
1077004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
1078004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
1079004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
1080004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
1081004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
1082004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
10830d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10840d0321e0SJeremy L Thompson }
10850d0321e0SJeremy L Thompson 
10860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1087cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
1088cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1089cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
1090cbfe683aSSebastian Grimberg   Ceed                ceed;
109122070f95SJeremy L Thompson   char               *diagonal_kernel_source;
109222070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
1093cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
1094cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
1095cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
1096cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
1097cbfe683aSSebastian Grimberg   CeedQFunction       qf;
1098cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
1099cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
1100cbfe683aSSebastian Grimberg 
1101cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1102cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1103cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1104cbfe683aSSebastian Grimberg 
1105cbfe683aSSebastian Grimberg   // Determine active input basis
1106cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1107cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1108cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1109cbfe683aSSebastian Grimberg     CeedVector vec;
1110cbfe683aSSebastian Grimberg 
1111cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1112cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1113cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1114cbfe683aSSebastian Grimberg 
1115cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1116cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1117cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1118cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1119cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1120cbfe683aSSebastian Grimberg       }
1121cbfe683aSSebastian Grimberg     }
1122cbfe683aSSebastian Grimberg   }
1123cbfe683aSSebastian Grimberg 
1124cbfe683aSSebastian Grimberg   // Determine active output basis
1125cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1126cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1127cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1128cbfe683aSSebastian Grimberg     CeedVector vec;
1129cbfe683aSSebastian Grimberg 
1130cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1131cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1132cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1133cbfe683aSSebastian Grimberg 
1134cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1135cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1136cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1137cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1138cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1139cbfe683aSSebastian Grimberg       }
1140cbfe683aSSebastian Grimberg     }
1141cbfe683aSSebastian Grimberg   }
1142cbfe683aSSebastian Grimberg 
1143cbfe683aSSebastian Grimberg   // Operator data struct
1144cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1145cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1146cbfe683aSSebastian Grimberg 
1147cbfe683aSSebastian Grimberg   // Assemble kernel
1148cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
1149cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
1150cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1151cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1152cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1153cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1154cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1155cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1156cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1157cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1158cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1159cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1160cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1161cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1162cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1163cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1164cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1165cbfe683aSSebastian Grimberg }
1166cbfe683aSSebastian Grimberg 
1167cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1168004e4986SSebastian Grimberg // Assemble Diagonal Core
11690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1170b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
11710d0321e0SJeremy L Thompson   Ceed                ceed;
1172cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1173b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
1174b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1175004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1176004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
11770d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
1178b7453713SJeremy L Thompson 
1179b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11802b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
11810d0321e0SJeremy L Thompson 
11820d0321e0SJeremy L Thompson   // Assemble QFunction
1183004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1184004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1185004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
11860d0321e0SJeremy L Thompson 
1187cbfe683aSSebastian Grimberg   // Setup
1188cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
1189cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1190cbfe683aSSebastian Grimberg 
1191cbfe683aSSebastian Grimberg   assert(diag != NULL);
1192cbfe683aSSebastian Grimberg 
1193cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1194cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1195cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1196cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
11979330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1198b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1199b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
12009330daecSnbeams 
1201cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
1202cbfe683aSSebastian Grimberg   }
12030d0321e0SJeremy L Thompson 
1204004e4986SSebastian Grimberg   // Restriction and diagonal vector
1205004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1206004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1207004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1208004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1209004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1210004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1211004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1212004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1213004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
12140d0321e0SJeremy L Thompson   }
1215004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1216004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1217b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
12180d0321e0SJeremy L Thompson 
121991db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1220004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1221004e4986SSebastian Grimberg   if (num_nodes > 0) {
12220d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1223b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
1224b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
12250d0321e0SJeremy L Thompson 
12260d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1227004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1228004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1229004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1230004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1231004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1232b7453713SJeremy L Thompson 
1233b7453713SJeremy L Thompson     if (is_point_block) {
1234004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
12350d0321e0SJeremy L Thompson     } else {
1236004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
12370d0321e0SJeremy L Thompson     }
12380d0321e0SJeremy L Thompson 
12390d0321e0SJeremy L Thompson     // Restore arrays
1240b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1241b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
124291db28b6SZach Atkins   }
12430d0321e0SJeremy L Thompson 
12440d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1245b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
12460d0321e0SJeremy L Thompson 
12470d0321e0SJeremy L Thompson   // Cleanup
1248b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
12490d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12500d0321e0SJeremy L Thompson }
12510d0321e0SJeremy L Thompson 
12520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12530d0321e0SJeremy L Thompson // Assemble Linear Diagonal
12540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12552b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
12562b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
12576aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12580d0321e0SJeremy L Thompson }
12590d0321e0SJeremy L Thompson 
12600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12610d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
12620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12632b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
12642b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
12656aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12660d0321e0SJeremy L Thompson }
12670d0321e0SJeremy L Thompson 
1268a835093fSnbeams //------------------------------------------------------------------------------
1269004e4986SSebastian Grimberg // Single Operator Assembly Setup
1270a835093fSnbeams //------------------------------------------------------------------------------
12719330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
1272a835093fSnbeams   Ceed                ceed;
127322070f95SJeremy L Thompson   char               *assembly_kernel_source;
127422070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1275004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
12763b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1277004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1278b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1279b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1280b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
1281b7453713SJeremy L Thompson   CeedQFunction       qf;
1282b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1283a835093fSnbeams   CeedOperator_Hip   *impl;
1284b7453713SJeremy L Thompson 
1285b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12862b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1287a835093fSnbeams 
1288a835093fSnbeams   // Get intput and output fields
12892b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1290a835093fSnbeams 
1291a835093fSnbeams   // Determine active input basis eval mode
12922b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
12932b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1294a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1295a835093fSnbeams     CeedVector vec;
1296b7453713SJeremy L Thompson 
12972b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1298a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1299004e4986SSebastian Grimberg       CeedBasis    basis;
1300b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1301b7453713SJeremy L Thompson 
1302004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1303004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1304004e4986SSebastian Grimberg       basis_in = basis;
13052b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1306004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1307004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1308004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
13092b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1310004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1311004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1312004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1313004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1314004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1315004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1316a835093fSnbeams         }
1317004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1318a835093fSnbeams       }
1319a835093fSnbeams     }
1320a835093fSnbeams   }
1321a835093fSnbeams 
1322a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
13232b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1324a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1325a835093fSnbeams     CeedVector vec;
1326b7453713SJeremy L Thompson 
13272b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1328a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1329004e4986SSebastian Grimberg       CeedBasis    basis;
1330b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1331b7453713SJeremy L Thompson 
1332004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1333004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1334004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1335004e4986SSebastian Grimberg       basis_out = basis;
13362b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1337004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1338004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1339004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1340004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1341004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
13422b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1343004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1344004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1345004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1346004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1347004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1348004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1349004e4986SSebastian Grimberg         }
1350004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1351a835093fSnbeams       }
1352a835093fSnbeams     }
1353a835093fSnbeams   }
1354004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1355a835093fSnbeams 
13562b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1357a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1358004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1359004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1360004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1361004e4986SSebastian Grimberg 
1362004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1363004e4986SSebastian Grimberg 
1364004e4986SSebastian Grimberg   if (fallback) {
1365004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1366004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1367004e4986SSebastian Grimberg   }
1368a835093fSnbeams 
1369a835093fSnbeams   // Compile kernels
1370004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1371004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
13722b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
137323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
13742b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
137523d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1376004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1377004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1378004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1379cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
13809330daecSnbeams                                   use_ceedsize_idx));
1381004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
13822b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
13832b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1384a835093fSnbeams 
1385004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1386004e4986SSebastian Grimberg   {
1387004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1388004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1389004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1390004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1391004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1392a835093fSnbeams 
1393004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1394004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1395004e4986SSebastian Grimberg     }
1396004e4986SSebastian Grimberg     if (has_eval_none) {
1397004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1398004e4986SSebastian Grimberg       for (CeedInt i = 0; i < (elem_size_in < num_qpts_in ? elem_size_in : num_qpts_in); i++) identity[i * elem_size_in + i] = 1.0;
1399004e4986SSebastian Grimberg     }
1400b7453713SJeremy L Thompson 
1401b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1402004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1403004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1404004e4986SSebastian Grimberg 
1405004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1406004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1407004e4986SSebastian Grimberg       if (q_comp > 1) {
1408004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1409004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1410004e4986SSebastian Grimberg       }
1411004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1412004e4986SSebastian Grimberg 
1413004e4986SSebastian Grimberg       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_in[i * elem_size_in * num_qpts_in], h_B_in, elem_size_in * num_qpts_in * sizeof(CeedScalar),
1414004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1415004e4986SSebastian Grimberg     }
1416004e4986SSebastian Grimberg 
1417004e4986SSebastian Grimberg     if (identity) {
1418004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1419a835093fSnbeams     }
1420a835093fSnbeams   }
1421a835093fSnbeams 
1422004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1423004e4986SSebastian Grimberg   {
1424004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1425004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1426004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1427004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1428004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1429b7453713SJeremy L Thompson 
1430004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1431004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1432004e4986SSebastian Grimberg     }
1433004e4986SSebastian Grimberg     if (has_eval_none) {
1434004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1435004e4986SSebastian Grimberg       for (CeedInt i = 0; i < (elem_size_out < num_qpts_out ? elem_size_out : num_qpts_out); i++) identity[i * elem_size_out + i] = 1.0;
1436a835093fSnbeams     }
1437a835093fSnbeams 
1438b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1439004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1440004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1441004e4986SSebastian Grimberg 
1442004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1443004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1444004e4986SSebastian Grimberg       if (q_comp > 1) {
1445004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1446004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1447004e4986SSebastian Grimberg       }
1448004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1449004e4986SSebastian Grimberg 
1450004e4986SSebastian Grimberg       CeedCallHip(ceed, hipMemcpy(&asmb->d_B_out[i * elem_size_out * num_qpts_out], h_B_out, elem_size_out * num_qpts_out * sizeof(CeedScalar),
1451004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1452004e4986SSebastian Grimberg     }
1453004e4986SSebastian Grimberg 
1454004e4986SSebastian Grimberg     if (identity) {
1455004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1456a835093fSnbeams     }
1457a835093fSnbeams   }
1458a835093fSnbeams   return CEED_ERROR_SUCCESS;
1459a835093fSnbeams }
1460a835093fSnbeams 
1461a835093fSnbeams //------------------------------------------------------------------------------
1462cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1463cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1464cefa2673SJeremy L Thompson //
1465ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1466cefa2673SJeremy L Thompson // modes).
1467cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1468a835093fSnbeams //------------------------------------------------------------------------------
14692b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1470a835093fSnbeams   Ceed                ceed;
1471b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1472004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1473b7453713SJeremy L Thompson   CeedScalar         *values_array;
1474004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1475b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1476004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1477004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1478004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1479004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1480a835093fSnbeams   CeedOperator_Hip   *impl;
1481b7453713SJeremy L Thompson 
1482b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
14832b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1484a835093fSnbeams 
1485a835093fSnbeams   // Assemble QFunction
1486004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1487004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1488004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1489a835093fSnbeams 
14909330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
14919330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
14929330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1493004e4986SSebastian Grimberg 
14949330daecSnbeams   // Setup
1495004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1496004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1497004e4986SSebastian Grimberg 
1498004e4986SSebastian Grimberg   assert(asmb != NULL);
1499004e4986SSebastian Grimberg 
1500004e4986SSebastian Grimberg   // Assemble element operator
1501004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1502004e4986SSebastian Grimberg   values_array += offset;
1503004e4986SSebastian Grimberg 
1504004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1505004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1506004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1507004e4986SSebastian Grimberg 
1508004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1509004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1510004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1511004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1512004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1513004e4986SSebastian Grimberg   }
1514004e4986SSebastian Grimberg 
1515004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1516004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1517004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1518004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1519004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1520004e4986SSebastian Grimberg 
1521004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1522004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1523004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1524004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1525004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1526004e4986SSebastian Grimberg     }
1527004e4986SSebastian Grimberg   } else {
1528004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1529004e4986SSebastian Grimberg     orients_out      = orients_in;
1530004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
15319330daecSnbeams   }
15329330daecSnbeams 
1533a835093fSnbeams   // Compute B^T D B
1534004e4986SSebastian Grimberg   CeedInt shared_mem =
1535004e4986SSebastian Grimberg       ((curl_orients_in || curl_orients_out ? elem_size_in * elem_size_out : 0) + (curl_orients_in ? elem_size_in * asmb->block_size_y : 0)) *
1536004e4986SSebastian Grimberg       sizeof(CeedScalar);
1537004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1538004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1539004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1540b7453713SJeremy L Thompson 
15412b730f8bSJeremy L Thompson   CeedCallBackend(
1542004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1543a835093fSnbeams 
1544a835093fSnbeams   // Restore arrays
15452b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1546004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1547a835093fSnbeams 
1548a835093fSnbeams   // Cleanup
15492b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1550004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1551004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1552004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1553004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1554004e4986SSebastian Grimberg   }
1555004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1556004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1557004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1558004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1559004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1560004e4986SSebastian Grimberg     }
1561004e4986SSebastian Grimberg   }
1562a835093fSnbeams   return CEED_ERROR_SUCCESS;
1563a835093fSnbeams }
1564a835093fSnbeams 
1565a835093fSnbeams //------------------------------------------------------------------------------
156667d9480aSJeremy L Thompson // Assemble Linear QFunction AtPoints
156767d9480aSJeremy L Thompson //------------------------------------------------------------------------------
156867d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
156967d9480aSJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
157067d9480aSJeremy L Thompson }
157167d9480aSJeremy L Thompson 
157267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
157367d9480aSJeremy L Thompson // Assemble Linear Diagonal AtPoints
157467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
157567d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1576382e9c83SJeremy L Thompson   CeedInt             max_num_points, num_elem, num_input_fields, num_output_fields;
1577afe3bc8aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1578afe3bc8aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1579afe3bc8aSJeremy L Thompson   CeedQFunction       qf;
1580afe3bc8aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1581afe3bc8aSJeremy L Thompson   CeedOperator_Hip   *impl;
1582afe3bc8aSJeremy L Thompson 
1583afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1584afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1585afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1586afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1587afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1588afe3bc8aSJeremy L Thompson   CeedInt num_points[num_elem];
1589afe3bc8aSJeremy L Thompson 
1590afe3bc8aSJeremy L Thompson   // Setup
1591afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
1592afe3bc8aSJeremy L Thompson   max_num_points = impl->max_num_points;
1593afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
1594afe3bc8aSJeremy L Thompson 
1595afe3bc8aSJeremy L Thompson   // Input Evecs and Restriction
1596afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1597afe3bc8aSJeremy L Thompson 
1598afe3bc8aSJeremy L Thompson   // Get point coordinates
1599afe3bc8aSJeremy L Thompson   if (!impl->point_coords_elem) {
1600afe3bc8aSJeremy L Thompson     CeedVector          point_coords = NULL;
1601afe3bc8aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1602afe3bc8aSJeremy L Thompson 
1603afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1604afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1605afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1606afe3bc8aSJeremy L Thompson   }
1607afe3bc8aSJeremy L Thompson 
1608382e9c83SJeremy L Thompson   // Clear active input Qvecs
1609382e9c83SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1610382e9c83SJeremy L Thompson     CeedVector vec;
1611382e9c83SJeremy L Thompson 
1612382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1613382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1614382e9c83SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
1615382e9c83SJeremy L Thompson   }
1616382e9c83SJeremy L Thompson 
1617afe3bc8aSJeremy L Thompson   // Input basis apply if needed
1618afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1619afe3bc8aSJeremy L Thompson 
1620afe3bc8aSJeremy L Thompson   // Output pointers, as necessary
1621afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1622afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1623afe3bc8aSJeremy L Thompson 
1624afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1625afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1626afe3bc8aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1627afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1628afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1629afe3bc8aSJeremy L Thompson     }
1630afe3bc8aSJeremy L Thompson   }
1631afe3bc8aSJeremy L Thompson 
1632afe3bc8aSJeremy L Thompson   // Loop over active fields
1633afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1634382e9c83SJeremy L Thompson     bool                is_active_at_points = true;
1635382e9c83SJeremy L Thompson     CeedInt             elem_size = 1, num_comp_active = 1, e_vec_size = 0;
1636382e9c83SJeremy L Thompson     CeedRestrictionType rstr_type;
1637382e9c83SJeremy L Thompson     CeedVector          vec;
1638382e9c83SJeremy L Thompson     CeedElemRestriction elem_rstr;
1639382e9c83SJeremy L Thompson 
1640382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1641382e9c83SJeremy L Thompson     // -- Skip non-active input
1642382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1643382e9c83SJeremy L Thompson 
1644382e9c83SJeremy L Thompson     // -- Get active restriction type
1645382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1646382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1647382e9c83SJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1648382e9c83SJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1649382e9c83SJeremy L Thompson     else elem_size = max_num_points;
1650382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1651382e9c83SJeremy L Thompson 
1652382e9c83SJeremy L Thompson     e_vec_size = elem_size * num_comp_active;
1653382e9c83SJeremy L Thompson     for (CeedInt s = 0; s < e_vec_size; s++) {
1654afe3bc8aSJeremy L Thompson       bool         is_active_input = false;
1655afe3bc8aSJeremy L Thompson       CeedEvalMode eval_mode;
1656afe3bc8aSJeremy L Thompson       CeedVector   vec;
1657afe3bc8aSJeremy L Thompson       CeedBasis    basis;
1658afe3bc8aSJeremy L Thompson 
1659afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1660afe3bc8aSJeremy L Thompson       // Skip non-active input
1661afe3bc8aSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1662afe3bc8aSJeremy L Thompson       if (!is_active_input) continue;
1663afe3bc8aSJeremy L Thompson 
1664afe3bc8aSJeremy L Thompson       // Update unit vector
166513062808SJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1666afe3bc8aSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1667afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1668afe3bc8aSJeremy L Thompson 
1669afe3bc8aSJeremy L Thompson       // Basis action
1670afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1671afe3bc8aSJeremy L Thompson       switch (eval_mode) {
1672afe3bc8aSJeremy L Thompson         case CEED_EVAL_NONE:
1673afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1674afe3bc8aSJeremy L Thompson           break;
1675afe3bc8aSJeremy L Thompson         case CEED_EVAL_INTERP:
1676afe3bc8aSJeremy L Thompson         case CEED_EVAL_GRAD:
1677afe3bc8aSJeremy L Thompson         case CEED_EVAL_DIV:
1678afe3bc8aSJeremy L Thompson         case CEED_EVAL_CURL:
1679afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1680afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1681afe3bc8aSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1682afe3bc8aSJeremy L Thompson           break;
1683afe3bc8aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1684afe3bc8aSJeremy L Thompson           break;  // No action
1685afe3bc8aSJeremy L Thompson       }
1686afe3bc8aSJeremy L Thompson 
1687afe3bc8aSJeremy L Thompson       // Q function
1688afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1689afe3bc8aSJeremy L Thompson 
1690afe3bc8aSJeremy L Thompson       // Output basis apply if needed
1691382e9c83SJeremy L Thompson       for (CeedInt j = 0; j < num_output_fields; j++) {
1692afe3bc8aSJeremy L Thompson         bool                is_active_output = false;
1693382e9c83SJeremy L Thompson         CeedInt             elem_size        = 0;
1694382e9c83SJeremy L Thompson         CeedRestrictionType rstr_type;
1695afe3bc8aSJeremy L Thompson         CeedEvalMode        eval_mode;
1696afe3bc8aSJeremy L Thompson         CeedVector          vec;
1697afe3bc8aSJeremy L Thompson         CeedElemRestriction elem_rstr;
1698afe3bc8aSJeremy L Thompson         CeedBasis           basis;
1699afe3bc8aSJeremy L Thompson 
1700382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec));
1701382e9c83SJeremy L Thompson         // ---- Skip non-active output
1702afe3bc8aSJeremy L Thompson         is_active_output = vec == CEED_VECTOR_ACTIVE;
1703afe3bc8aSJeremy L Thompson         if (!is_active_output) continue;
1704afe3bc8aSJeremy L Thompson 
1705382e9c83SJeremy L Thompson         // ---- Check if elem size matches
1706382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1707382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1708382e9c83SJeremy L Thompson         if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue;
1709382e9c83SJeremy L Thompson         if (rstr_type == CEED_RESTRICTION_POINTS) {
1710382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size));
1711382e9c83SJeremy L Thompson         } else {
1712382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1713382e9c83SJeremy L Thompson         }
1714382e9c83SJeremy L Thompson         {
1715382e9c83SJeremy L Thompson           CeedInt num_comp = 0;
1716382e9c83SJeremy L Thompson 
1717382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1718382e9c83SJeremy L Thompson           if (e_vec_size != num_comp * elem_size) continue;
1719382e9c83SJeremy L Thompson         }
1720382e9c83SJeremy L Thompson 
1721afe3bc8aSJeremy L Thompson         // Basis action
1722382e9c83SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
1723afe3bc8aSJeremy L Thompson         switch (eval_mode) {
1724afe3bc8aSJeremy L Thompson           case CEED_EVAL_NONE:
1725382e9c83SJeremy L Thompson             CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields]));
1726afe3bc8aSJeremy L Thompson             break;
1727afe3bc8aSJeremy L Thompson           case CEED_EVAL_INTERP:
1728afe3bc8aSJeremy L Thompson           case CEED_EVAL_GRAD:
1729afe3bc8aSJeremy L Thompson           case CEED_EVAL_DIV:
1730afe3bc8aSJeremy L Thompson           case CEED_EVAL_CURL:
1731382e9c83SJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis));
1732382e9c83SJeremy L Thompson             CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
1733382e9c83SJeremy L Thompson                                                    impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs]));
1734afe3bc8aSJeremy L Thompson             break;
1735afe3bc8aSJeremy L Thompson           // LCOV_EXCL_START
1736afe3bc8aSJeremy L Thompson           case CEED_EVAL_WEIGHT: {
1737afe3bc8aSJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1738afe3bc8aSJeremy L Thompson             // LCOV_EXCL_STOP
1739afe3bc8aSJeremy L Thompson           }
1740afe3bc8aSJeremy L Thompson         }
1741afe3bc8aSJeremy L Thompson 
1742afe3bc8aSJeremy L Thompson         // Mask output e-vec
1743382e9c83SJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs]));
1744afe3bc8aSJeremy L Thompson 
1745afe3bc8aSJeremy L Thompson         // Restrict
1746382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1747382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request));
1748afe3bc8aSJeremy L Thompson 
1749afe3bc8aSJeremy L Thompson         // Reset q_vec for
1750afe3bc8aSJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) {
1751382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields]));
1752382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields]));
1753afe3bc8aSJeremy L Thompson         }
1754afe3bc8aSJeremy L Thompson       }
1755382e9c83SJeremy L Thompson 
1756382e9c83SJeremy L Thompson       // Reset vec
175713062808SJeremy L Thompson       if (s == e_vec_size - 1 && i != num_input_fields - 1) CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
175886e10729SJeremy L Thompson     }
1759afe3bc8aSJeremy L Thompson   }
1760afe3bc8aSJeremy L Thompson 
1761afe3bc8aSJeremy L Thompson   // Restore CEED_EVAL_NONE
1762afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1763afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1764afe3bc8aSJeremy L Thompson 
1765afe3bc8aSJeremy L Thompson     // Get eval_mode
1766afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1767afe3bc8aSJeremy L Thompson 
1768afe3bc8aSJeremy L Thompson     // Restore evec
1769afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1770afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1771afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1772afe3bc8aSJeremy L Thompson     }
1773afe3bc8aSJeremy L Thompson   }
1774afe3bc8aSJeremy L Thompson 
1775afe3bc8aSJeremy L Thompson   // Restore input arrays
1776afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1777afe3bc8aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
177867d9480aSJeremy L Thompson }
177967d9480aSJeremy L Thompson 
178067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
17810d0321e0SJeremy L Thompson // Create operator
17820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
17830d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
17840d0321e0SJeremy L Thompson   Ceed              ceed;
17850d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
17860d0321e0SJeremy L Thompson 
1787b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
17882b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
17892b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
17902b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
17912b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
17922b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
17932b730f8bSJeremy L Thompson   CeedCallBackend(
17942b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
17952b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
17962b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
17972b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
17980d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17990d0321e0SJeremy L Thompson }
18000d0321e0SJeremy L Thompson 
18010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
180267d9480aSJeremy L Thompson // Create operator AtPoints
180367d9480aSJeremy L Thompson //------------------------------------------------------------------------------
180467d9480aSJeremy L Thompson int CeedOperatorCreateAtPoints_Hip(CeedOperator op) {
180567d9480aSJeremy L Thompson   Ceed              ceed;
180667d9480aSJeremy L Thompson   CeedOperator_Hip *impl;
180767d9480aSJeremy L Thompson 
180867d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
180967d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
181067d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
181167d9480aSJeremy L Thompson 
181267d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Hip));
181367d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip));
181467d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Hip));
181567d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
181667d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
181767d9480aSJeremy L Thompson }
181867d9480aSJeremy L Thompson 
181967d9480aSJeremy L Thompson //------------------------------------------------------------------------------
1820