xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-operator.c (revision 8a21357021cf95ebb58183dc35a646c90195a1f2)
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 
26641655a23SJeremy 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 
27441655a23SJeremy L Thompson   // Reuse active e-vecs where able
27541655a23SJeremy L Thompson   {
27641655a23SJeremy L Thompson     CeedInt              num_used  = 0;
27741655a23SJeremy L Thompson     CeedElemRestriction *rstr_used = NULL;
27841655a23SJeremy L Thompson 
27941655a23SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
28041655a23SJeremy L Thompson       bool                is_used = false;
28141655a23SJeremy L Thompson       CeedVector          vec_i;
28241655a23SJeremy L Thompson       CeedElemRestriction rstr_i;
28341655a23SJeremy L Thompson 
28441655a23SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
28541655a23SJeremy L Thompson       if (vec_i != CEED_VECTOR_ACTIVE) continue;
28641655a23SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
28741655a23SJeremy L Thompson       for (CeedInt j = 0; j < num_used; j++) {
28841655a23SJeremy L Thompson         if (rstr_i == rstr_used[i]) is_used = true;
28941655a23SJeremy L Thompson       }
29041655a23SJeremy L Thompson       if (is_used) continue;
29141655a23SJeremy L Thompson       num_used++;
29241655a23SJeremy L Thompson       if (num_used == 1) CeedCallBackend(CeedCalloc(num_used, &rstr_used));
29341655a23SJeremy L Thompson       else CeedCallBackend(CeedRealloc(num_used, &rstr_used));
29441655a23SJeremy L Thompson       rstr_used[num_used - 1] = rstr_i;
29541655a23SJeremy L Thompson       for (CeedInt j = num_output_fields - 1; j >= 0; j--) {
29641655a23SJeremy L Thompson         CeedEvalMode        eval_mode;
29741655a23SJeremy L Thompson         CeedVector          vec_j;
29841655a23SJeremy L Thompson         CeedElemRestriction rstr_j;
29941655a23SJeremy L Thompson 
30041655a23SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec_j));
30141655a23SJeremy L Thompson         if (vec_j != CEED_VECTOR_ACTIVE) continue;
30241655a23SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
30341655a23SJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) continue;
30441655a23SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &rstr_j));
30541655a23SJeremy L Thompson         if (rstr_i == rstr_j) {
30641655a23SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(impl->e_vecs[i], &impl->e_vecs[j + impl->num_inputs]));
30741655a23SJeremy L Thompson         }
30841655a23SJeremy L Thompson       }
30941655a23SJeremy L Thompson     }
31041655a23SJeremy L Thompson     CeedCallBackend(CeedFree(&rstr_used));
31141655a23SJeremy L Thompson   }
312*8a213570SJeremy L Thompson   impl->has_shared_e_vecs = true;
3132b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
3140d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3150d0321e0SJeremy L Thompson }
3160d0321e0SJeremy L Thompson 
3170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3180d0321e0SJeremy L Thompson // Setup Operator Inputs
3190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
320b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
321b7453713SJeremy L Thompson                                               CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
322b7453713SJeremy L Thompson                                               CeedOperator_Hip *impl, CeedRequest *request) {
323b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
324004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
3250d0321e0SJeremy L Thompson     CeedVector          vec;
326b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
3270d0321e0SJeremy L Thompson 
3280d0321e0SJeremy L Thompson     // Get input vector
329b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3300d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
331b7453713SJeremy L Thompson       if (skip_active) continue;
332b7453713SJeremy L Thompson       else vec = in_vec;
3330d0321e0SJeremy L Thompson     }
3340d0321e0SJeremy L Thompson 
335004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
336004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3370d0321e0SJeremy L Thompson     } else {
3380d0321e0SJeremy L Thompson       // Get input vector
339b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3400d0321e0SJeremy L Thompson       // Get input element restriction
341b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
342b7453713SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
3430d0321e0SJeremy L Thompson       // Restrict, if necessary
344b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {
3450d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
346b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3470d0321e0SJeremy L Thompson       } else {
348c1222711SJeremy L Thompson         uint64_t state;
349c1222711SJeremy L Thompson 
350c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
35141655a23SJeremy L Thompson         if ((state != impl->input_states[i] || vec == in_vec) && !impl->skip_rstr_in[i]) {
352b7453713SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
353c1222711SJeremy L Thompson         }
3543aab95c0SJeremy L Thompson         impl->input_states[i] = state;
3550d0321e0SJeremy L Thompson         // Get evec
356b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3570d0321e0SJeremy L Thompson       }
3580d0321e0SJeremy L Thompson     }
3590d0321e0SJeremy L Thompson   }
3600d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3610d0321e0SJeremy L Thompson }
3620d0321e0SJeremy L Thompson 
3630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3640d0321e0SJeremy L Thompson // Input Basis Action
3650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
366b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
367b7453713SJeremy L Thompson                                              CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
3682b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
369b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
370b7453713SJeremy L Thompson     CeedInt             elem_size, size;
371004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
372b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
3730d0321e0SJeremy L Thompson     CeedBasis           basis;
3740d0321e0SJeremy L Thompson 
3750d0321e0SJeremy L Thompson     // Skip active input
376b7453713SJeremy L Thompson     if (skip_active) {
3770d0321e0SJeremy L Thompson       CeedVector vec;
378b7453713SJeremy L Thompson 
379b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3802b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3810d0321e0SJeremy L Thompson     }
382004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
383b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
384b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
385004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
386b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3870d0321e0SJeremy L Thompson     // Basis action
388004e4986SSebastian Grimberg     switch (eval_mode) {
3890d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
390b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
3910d0321e0SJeremy L Thompson         break;
3920d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3930d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
394004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
395004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
396b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
397004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3980d0321e0SJeremy L Thompson         break;
3990d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
4000d0321e0SJeremy L Thompson         break;  // No action
4010d0321e0SJeremy L Thompson     }
4020d0321e0SJeremy L Thompson   }
4030d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4040d0321e0SJeremy L Thompson }
4050d0321e0SJeremy L Thompson 
4060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4070d0321e0SJeremy L Thompson // Restore Input Vectors
4080d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
409b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
410b7453713SJeremy L Thompson                                                 const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
411b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
412004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
4130d0321e0SJeremy L Thompson     CeedVector   vec;
414004e4986SSebastian Grimberg 
4150d0321e0SJeremy L Thompson     // Skip active input
416b7453713SJeremy L Thompson     if (skip_active) {
417b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4182b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
4190d0321e0SJeremy L Thompson     }
420004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
421004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
4220d0321e0SJeremy L Thompson     } else {
423b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
424b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
425b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
4260d0321e0SJeremy L Thompson       } else {
427b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
4280d0321e0SJeremy L Thompson       }
4290d0321e0SJeremy L Thompson     }
4300d0321e0SJeremy L Thompson   }
4310d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4320d0321e0SJeremy L Thompson }
4330d0321e0SJeremy L Thompson 
4340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4350d0321e0SJeremy L Thompson // Apply and add to output
4360d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
437b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
438b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
439b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
440b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
4410d0321e0SJeremy L Thompson   CeedQFunction       qf;
442b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
443b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
444b7453713SJeremy L Thompson 
445b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4462b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4472b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
448b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
449b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
450b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4510d0321e0SJeremy L Thompson 
4520d0321e0SJeremy L Thompson   // Setup
4532b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4540d0321e0SJeremy L Thompson 
4550d0321e0SJeremy L Thompson   // Input Evecs and Restriction
456b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
4570d0321e0SJeremy L Thompson 
4580d0321e0SJeremy L Thompson   // Input basis apply if needed
459b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
4600d0321e0SJeremy L Thompson 
4610d0321e0SJeremy L Thompson   // Output pointers, as necessary
462b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
463004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
464b7453713SJeremy L Thompson 
465004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
466004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
4670d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
468b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
469b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
4700d0321e0SJeremy L Thompson     }
4710d0321e0SJeremy L Thompson   }
4720d0321e0SJeremy L Thompson 
4730d0321e0SJeremy L Thompson   // Q function
474b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
4750d0321e0SJeremy L Thompson 
47641655a23SJeremy L Thompson   // Restore input arrays
47741655a23SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
47841655a23SJeremy L Thompson 
4790d0321e0SJeremy L Thompson   // Output basis apply if needed
480b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
481004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
482b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
483b7453713SJeremy L Thompson     CeedBasis           basis;
484b7453713SJeremy L Thompson 
485004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
486b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
487b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
488004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
489b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4900d0321e0SJeremy L Thompson     // Basis action
491004e4986SSebastian Grimberg     switch (eval_mode) {
4920d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
493004e4986SSebastian Grimberg         break;  // No action
4940d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4950d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
496004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
497004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
498b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
499f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
500f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAdd(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
501f8a0df59SJeremy L Thompson         } else {
502004e4986SSebastian Grimberg           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
503f8a0df59SJeremy L Thompson         }
5040d0321e0SJeremy L Thompson         break;
5050d0321e0SJeremy L Thompson       // LCOV_EXCL_START
5060d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
5076e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
5080d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
5090d0321e0SJeremy L Thompson       }
5100d0321e0SJeremy L Thompson     }
511004e4986SSebastian Grimberg   }
5120d0321e0SJeremy L Thompson 
5130d0321e0SJeremy L Thompson   // Output restriction
514b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
515004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
516b7453713SJeremy L Thompson     CeedVector          vec;
517b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
518b7453713SJeremy L Thompson 
5190d0321e0SJeremy L Thompson     // Restore evec
520004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
521004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
522b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
5230d0321e0SJeremy L Thompson     }
524f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
5250d0321e0SJeremy L Thompson     // Get output vector
526b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5270d0321e0SJeremy L Thompson     // Restrict
528b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
5290d0321e0SJeremy L Thompson     // Active
530b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
5310d0321e0SJeremy L Thompson 
532b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
5330d0321e0SJeremy L Thompson   }
5340d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5350d0321e0SJeremy L Thompson }
5360d0321e0SJeremy L Thompson 
5370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
53867d9480aSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
53967d9480aSJeremy L Thompson //------------------------------------------------------------------------------
54067d9480aSJeremy L Thompson static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) {
54167d9480aSJeremy L Thompson   Ceed                ceed;
54267d9480aSJeremy L Thompson   bool                is_setup_done;
54367d9480aSJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
54467d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
54567d9480aSJeremy L Thompson   CeedQFunction       qf;
54667d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
54767d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
54867d9480aSJeremy L Thompson 
54967d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
55067d9480aSJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
55167d9480aSJeremy L Thompson 
55267d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
55367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
55467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
55567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
55667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
55767d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
55867d9480aSJeremy L Thompson   {
55967d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr = NULL;
56067d9480aSJeremy L Thompson 
56167d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL));
56267d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points));
56367d9480aSJeremy L Thompson   }
56467d9480aSJeremy L Thompson   impl->max_num_points = max_num_points;
56567d9480aSJeremy L Thompson 
56667d9480aSJeremy L Thompson   // Allocate
56767d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
5683aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
569f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
570f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
57167d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
57267d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
57367d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
57467d9480aSJeremy L Thompson   impl->num_inputs  = num_input_fields;
57567d9480aSJeremy L Thompson   impl->num_outputs = num_output_fields;
57667d9480aSJeremy L Thompson 
577*8a213570SJeremy L Thompson   // Set up infield and outfield e-vecs and q-vecs
57867d9480aSJeremy L Thompson   // Infields
579f8a0df59SJeremy 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,
5803aab95c0SJeremy L Thompson                                               max_num_points, num_elem));
58167d9480aSJeremy L Thompson   // Outfields
582f8a0df59SJeremy 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,
583f8a0df59SJeremy L Thompson                                               num_input_fields, num_output_fields, max_num_points, num_elem));
58467d9480aSJeremy L Thompson 
585*8a213570SJeremy L Thompson   // Reuse active e-vecs where able
586*8a213570SJeremy L Thompson   {
587*8a213570SJeremy L Thompson     CeedInt              num_used  = 0;
588*8a213570SJeremy L Thompson     CeedElemRestriction *rstr_used = NULL;
589*8a213570SJeremy L Thompson 
590*8a213570SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
591*8a213570SJeremy L Thompson       bool                is_used = false;
592*8a213570SJeremy L Thompson       CeedVector          vec_i;
593*8a213570SJeremy L Thompson       CeedElemRestriction rstr_i;
594*8a213570SJeremy L Thompson 
595*8a213570SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
596*8a213570SJeremy L Thompson       if (vec_i != CEED_VECTOR_ACTIVE) continue;
597*8a213570SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
598*8a213570SJeremy L Thompson       for (CeedInt j = 0; j < num_used; j++) {
599*8a213570SJeremy L Thompson         if (rstr_i == rstr_used[i]) is_used = true;
600*8a213570SJeremy L Thompson       }
601*8a213570SJeremy L Thompson       if (is_used) continue;
602*8a213570SJeremy L Thompson       num_used++;
603*8a213570SJeremy L Thompson       if (num_used == 1) CeedCallBackend(CeedCalloc(num_used, &rstr_used));
604*8a213570SJeremy L Thompson       else CeedCallBackend(CeedRealloc(num_used, &rstr_used));
605*8a213570SJeremy L Thompson       rstr_used[num_used - 1] = rstr_i;
606*8a213570SJeremy L Thompson       for (CeedInt j = num_output_fields - 1; j >= 0; j--) {
607*8a213570SJeremy L Thompson         CeedEvalMode        eval_mode;
608*8a213570SJeremy L Thompson         CeedVector          vec_j;
609*8a213570SJeremy L Thompson         CeedElemRestriction rstr_j;
610*8a213570SJeremy L Thompson 
611*8a213570SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec_j));
612*8a213570SJeremy L Thompson         if (vec_j != CEED_VECTOR_ACTIVE) continue;
613*8a213570SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
614*8a213570SJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) continue;
615*8a213570SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &rstr_j));
616*8a213570SJeremy L Thompson         if (rstr_i == rstr_j) {
617*8a213570SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(impl->e_vecs[i], &impl->e_vecs[j + impl->num_inputs]));
618*8a213570SJeremy L Thompson         }
619*8a213570SJeremy L Thompson       }
620*8a213570SJeremy L Thompson     }
621*8a213570SJeremy L Thompson     CeedCallBackend(CeedFree(&rstr_used));
622*8a213570SJeremy L Thompson   }
623*8a213570SJeremy L Thompson   impl->has_shared_e_vecs = true;
62467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
62567d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
62667d9480aSJeremy L Thompson }
62767d9480aSJeremy L Thompson 
62867d9480aSJeremy L Thompson //------------------------------------------------------------------------------
62967d9480aSJeremy L Thompson // Input Basis Action AtPoints
63067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
63167d9480aSJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Hip(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields,
63267d9480aSJeremy L Thompson                                                      CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active,
63367d9480aSJeremy L Thompson                                                      CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
63467d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
63567d9480aSJeremy L Thompson     CeedInt             elem_size, size;
63667d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
63767d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
63867d9480aSJeremy L Thompson     CeedBasis           basis;
63967d9480aSJeremy L Thompson 
64067d9480aSJeremy L Thompson     // Skip active input
64167d9480aSJeremy L Thompson     if (skip_active) {
64267d9480aSJeremy L Thompson       CeedVector vec;
64367d9480aSJeremy L Thompson 
64467d9480aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
64567d9480aSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
64667d9480aSJeremy L Thompson     }
64767d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
64867d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
64967d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
65067d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
65167d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
65267d9480aSJeremy L Thompson     // Basis action
65367d9480aSJeremy L Thompson     switch (eval_mode) {
65467d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
65567d9480aSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
65667d9480aSJeremy L Thompson         break;
65767d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
65867d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
65967d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
66067d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
66167d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
66267d9480aSJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
66367d9480aSJeremy L Thompson                                                impl->q_vecs_in[i]));
66467d9480aSJeremy L Thompson         break;
66567d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT:
66667d9480aSJeremy L Thompson         break;  // No action
66767d9480aSJeremy L Thompson     }
66867d9480aSJeremy L Thompson   }
66967d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
67067d9480aSJeremy L Thompson }
67167d9480aSJeremy L Thompson 
67267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
67367d9480aSJeremy L Thompson // Apply and add to output AtPoints
67467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
67567d9480aSJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
67667d9480aSJeremy L Thompson   CeedInt             max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
67767d9480aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
67867d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
67967d9480aSJeremy L Thompson   CeedQFunction       qf;
68067d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
68167d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
68267d9480aSJeremy L Thompson 
68367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
68467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
68567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
68667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
68767d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
68867d9480aSJeremy L Thompson   CeedInt num_points[num_elem];
68967d9480aSJeremy L Thompson 
69067d9480aSJeremy L Thompson   // Setup
69167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
69267d9480aSJeremy L Thompson   max_num_points = impl->max_num_points;
69367d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
69467d9480aSJeremy L Thompson 
69567d9480aSJeremy L Thompson   // Input Evecs and Restriction
69667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
69767d9480aSJeremy L Thompson 
69867d9480aSJeremy L Thompson   // Get point coordinates
69967d9480aSJeremy L Thompson   if (!impl->point_coords_elem) {
70067d9480aSJeremy L Thompson     CeedVector          point_coords = NULL;
70167d9480aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
70267d9480aSJeremy L Thompson 
70367d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
70467d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
70567d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
70667d9480aSJeremy L Thompson   }
70767d9480aSJeremy L Thompson 
70867d9480aSJeremy L Thompson   // Input basis apply if needed
70967d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
71067d9480aSJeremy L Thompson 
71167d9480aSJeremy L Thompson   // Output pointers, as necessary
71267d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
71367d9480aSJeremy L Thompson     CeedEvalMode eval_mode;
71467d9480aSJeremy L Thompson 
71567d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
71667d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
71767d9480aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
71867d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
71967d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
72067d9480aSJeremy L Thompson     }
72167d9480aSJeremy L Thompson   }
72267d9480aSJeremy L Thompson 
72367d9480aSJeremy L Thompson   // Q function
72467d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
72567d9480aSJeremy L Thompson 
726*8a213570SJeremy L Thompson   // Restore input arrays
727*8a213570SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
728*8a213570SJeremy L Thompson 
72967d9480aSJeremy L Thompson   // Output basis apply if needed
73067d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
73167d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
73267d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
73367d9480aSJeremy L Thompson     CeedBasis           basis;
73467d9480aSJeremy L Thompson 
73567d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
73667d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
73767d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
73867d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
73967d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
74067d9480aSJeremy L Thompson     // Basis action
74167d9480aSJeremy L Thompson     switch (eval_mode) {
74267d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
74367d9480aSJeremy L Thompson         break;  // No action
74467d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
74567d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
74667d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
74767d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
74867d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
749f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
750f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAddAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
751f8a0df59SJeremy L Thompson                                                     impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
752f8a0df59SJeremy L Thompson         } else {
75367d9480aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
75467d9480aSJeremy L Thompson                                                  impl->e_vecs[i + impl->num_inputs]));
755f8a0df59SJeremy L Thompson         }
75667d9480aSJeremy L Thompson         break;
75767d9480aSJeremy L Thompson       // LCOV_EXCL_START
75867d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT: {
75967d9480aSJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
76067d9480aSJeremy L Thompson         // LCOV_EXCL_STOP
76167d9480aSJeremy L Thompson       }
76267d9480aSJeremy L Thompson     }
76367d9480aSJeremy L Thompson   }
76467d9480aSJeremy L Thompson 
76567d9480aSJeremy L Thompson   // Output restriction
76667d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
76767d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
76867d9480aSJeremy L Thompson     CeedVector          vec;
76967d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
77067d9480aSJeremy L Thompson 
77167d9480aSJeremy L Thompson     // Restore evec
77267d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
77367d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
77467d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
77567d9480aSJeremy L Thompson     }
776f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
77767d9480aSJeremy L Thompson     // Get output vector
77867d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
77967d9480aSJeremy L Thompson     // Restrict
78067d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
78167d9480aSJeremy L Thompson     // Active
78267d9480aSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
78367d9480aSJeremy L Thompson 
78467d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
78567d9480aSJeremy L Thompson   }
78667d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
78767d9480aSJeremy L Thompson }
78867d9480aSJeremy L Thompson 
78967d9480aSJeremy L Thompson //------------------------------------------------------------------------------
790004e4986SSebastian Grimberg // Linear QFunction Assembly Core
7910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7922b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
7930d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
794b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
795b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
796b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
797004e4986SSebastian Grimberg   CeedVector         *active_inputs;
798b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
799b7453713SJeremy L Thompson   CeedQFunction       qf;
800b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
801b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
802b7453713SJeremy L Thompson 
8032b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
804b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
805e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
806e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
807b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
808004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
809b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
810b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
811004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
812004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
8130d0321e0SJeremy L Thompson 
8140d0321e0SJeremy L Thompson   // Setup
8152b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
8160d0321e0SJeremy L Thompson 
8170d0321e0SJeremy L Thompson   // Input Evecs and Restriction
818b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
8190d0321e0SJeremy L Thompson 
8200d0321e0SJeremy L Thompson   // Count number of active input fields
821b7453713SJeremy L Thompson   if (!num_active_in) {
822b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
823b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
824b7453713SJeremy L Thompson       CeedVector  vec;
825b7453713SJeremy L Thompson 
8260d0321e0SJeremy L Thompson       // Get input vector
827b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
8280d0321e0SJeremy L Thompson       // Check if active input
8290d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
830b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
831b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
832b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
833004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
8340d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
835004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
836004e4986SSebastian Grimberg 
837004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
838b7453713SJeremy L Thompson           CeedCallBackend(
839004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
8400d0321e0SJeremy L Thompson         }
841b7453713SJeremy L Thompson         num_active_in += size;
842b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
8430d0321e0SJeremy L Thompson       }
8440d0321e0SJeremy L Thompson     }
845b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
846004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
8470d0321e0SJeremy L Thompson   }
8480d0321e0SJeremy L Thompson 
8490d0321e0SJeremy L Thompson   // Count number of active output fields
850b7453713SJeremy L Thompson   if (!num_active_out) {
851b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
852b7453713SJeremy L Thompson       CeedVector vec;
853b7453713SJeremy L Thompson 
8540d0321e0SJeremy L Thompson       // Get output vector
855b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
8560d0321e0SJeremy L Thompson       // Check if active output
8570d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
858b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
859b7453713SJeremy L Thompson         num_active_out += size;
8600d0321e0SJeremy L Thompson       }
8610d0321e0SJeremy L Thompson     }
862b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
8630d0321e0SJeremy L Thompson   }
8640d0321e0SJeremy L Thompson 
8650d0321e0SJeremy L Thompson   // Check sizes
866b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
8670d0321e0SJeremy L Thompson 
8680d0321e0SJeremy L Thompson   // Build objects if needed
8690d0321e0SJeremy L Thompson   if (build_objects) {
870b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
871b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
872b7453713SJeremy L Thompson 
873004e4986SSebastian Grimberg     // Create output restriction
874b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
8750a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
8760a5597ceSJeremy L Thompson                                                      rstr));
8770d0321e0SJeremy L Thompson     // Create assembled vector
878b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
8790d0321e0SJeremy L Thompson   }
8802b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
881b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
8820d0321e0SJeremy L Thompson 
8830d0321e0SJeremy L Thompson   // Input basis apply
884b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
8850d0321e0SJeremy L Thompson 
8860d0321e0SJeremy L Thompson   // Assemble QFunction
887b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
8880d0321e0SJeremy L Thompson     // Set Inputs
889004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
890b7453713SJeremy L Thompson     if (num_active_in > 1) {
891004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
8920d0321e0SJeremy L Thompson     }
8930d0321e0SJeremy L Thompson     // Set Outputs
894b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
895b7453713SJeremy L Thompson       CeedVector vec;
896b7453713SJeremy L Thompson 
8970d0321e0SJeremy L Thompson       // Get output vector
898b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
8990d0321e0SJeremy L Thompson       // Check if active output
9000d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
901b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
902b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
903b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
9040d0321e0SJeremy L Thompson       }
9050d0321e0SJeremy L Thompson     }
9060d0321e0SJeremy L Thompson     // Apply QFunction
907b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
9080d0321e0SJeremy L Thompson   }
9090d0321e0SJeremy L Thompson 
910*8a213570SJeremy L Thompson   // Un-set output q-vecs to prevent accidental overwrite of Assembled
911b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
912b7453713SJeremy L Thompson     CeedVector vec;
913b7453713SJeremy L Thompson 
9140d0321e0SJeremy L Thompson     // Get output vector
915b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
9160d0321e0SJeremy L Thompson     // Check if active output
9170d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
918b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
9190d0321e0SJeremy L Thompson     }
9200d0321e0SJeremy L Thompson   }
9210d0321e0SJeremy L Thompson 
9220d0321e0SJeremy L Thompson   // Restore input arrays
923b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
9240d0321e0SJeremy L Thompson 
9250d0321e0SJeremy L Thompson   // Restore output
926b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
9270d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9280d0321e0SJeremy L Thompson }
9290d0321e0SJeremy L Thompson 
9300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9310d0321e0SJeremy L Thompson // Assemble Linear QFunction
9320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9332b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
9342b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
9350d0321e0SJeremy L Thompson }
9360d0321e0SJeremy L Thompson 
9370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
938b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
9390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9402b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
9412b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
9420d0321e0SJeremy L Thompson }
9430d0321e0SJeremy L Thompson 
9440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
945004e4986SSebastian Grimberg // Assemble Diagonal Setup
9460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
947cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
9480d0321e0SJeremy L Thompson   Ceed                ceed;
949004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
950cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
951004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
952b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
953b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
9540d0321e0SJeremy L Thompson   CeedQFunction       qf;
955b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
956b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
957b7453713SJeremy L Thompson 
958b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
9592b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
960b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
9610d0321e0SJeremy L Thompson 
9620d0321e0SJeremy L Thompson   // Determine active input basis
963b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
964b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
965b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9660d0321e0SJeremy L Thompson     CeedVector vec;
967b7453713SJeremy L Thompson 
968b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
9690d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
970004e4986SSebastian Grimberg       CeedBasis    basis;
971004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
972b7453713SJeremy L Thompson 
973004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
974004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
975004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
976004e4986SSebastian Grimberg       basis_in = basis;
977004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
978004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
979004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
980004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
981004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
982004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
983004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
9840d0321e0SJeremy L Thompson       }
9850d0321e0SJeremy L Thompson     }
9860d0321e0SJeremy L Thompson   }
9870d0321e0SJeremy L Thompson 
9880d0321e0SJeremy L Thompson   // Determine active output basis
989b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
990b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
991b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9920d0321e0SJeremy L Thompson     CeedVector vec;
993b7453713SJeremy L Thompson 
994b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
9950d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
996004e4986SSebastian Grimberg       CeedBasis    basis;
997004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
998b7453713SJeremy L Thompson 
999004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
1000004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1001004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
1002004e4986SSebastian Grimberg       basis_out = basis;
1003004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1004004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1005004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1006004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
1007004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1008004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
1009004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
10100d0321e0SJeremy L Thompson       }
10110d0321e0SJeremy L Thompson     }
10120d0321e0SJeremy L Thompson   }
10130d0321e0SJeremy L Thompson 
10140d0321e0SJeremy L Thompson   // Operator data struct
10152b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
10162b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
10170d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
1018b7453713SJeremy L Thompson 
1019cbfe683aSSebastian Grimberg   // Basis matrices
1020004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1021004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1022004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1023004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
1024004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
1025004e4986SSebastian Grimberg   bool          has_eval_none    = false;
10260d0321e0SJeremy L Thompson 
10270d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
1028004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1029004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1030004e4986SSebastian Grimberg   if (has_eval_none) {
10310d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
1032b7453713SJeremy L Thompson 
1033004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
1034004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
1035b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
1036b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
1037004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
10380d0321e0SJeremy L Thompson   }
10390d0321e0SJeremy L Thompson 
1040004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
1041004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
1042004e4986SSebastian Grimberg     CeedFESpace fespace;
1043004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
10440d0321e0SJeremy L Thompson 
1045004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
1046004e4986SSebastian Grimberg     switch (fespace) {
1047004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
1048004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
1049004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
1050004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
10510d0321e0SJeremy L Thompson 
1052004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1053004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
10540d0321e0SJeremy L Thompson 
1055004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1056004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1057004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1058004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
1059004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
1060004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
1061004e4986SSebastian Grimberg         if (in) {
1062004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1063004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
1064004e4986SSebastian Grimberg         } else {
1065004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1066004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
1067004e4986SSebastian Grimberg         }
1068004e4986SSebastian Grimberg       } break;
1069004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
1070004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
1071004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
1072004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
1073004e4986SSebastian Grimberg 
1074004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1075004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
1076004e4986SSebastian Grimberg 
1077004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1078004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1079004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1080004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
1081004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
1082004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
1083004e4986SSebastian Grimberg         if (in) {
1084004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1085004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
1086004e4986SSebastian Grimberg         } else {
1087004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1088004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
1089004e4986SSebastian Grimberg         }
1090004e4986SSebastian Grimberg       } break;
1091004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
1092004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
1093004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
1094004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
1095004e4986SSebastian Grimberg 
1096004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1097004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
1098004e4986SSebastian Grimberg 
1099004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1100004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1101004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1102004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
1103004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
1104004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
1105004e4986SSebastian Grimberg         if (in) {
1106004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1107004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
1108004e4986SSebastian Grimberg         } else {
1109004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1110004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
1111004e4986SSebastian Grimberg         }
1112004e4986SSebastian Grimberg       } break;
1113004e4986SSebastian Grimberg     }
1114004e4986SSebastian Grimberg   }
1115004e4986SSebastian Grimberg 
1116004e4986SSebastian Grimberg   // Arrays of eval_modes
1117004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
1118004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
1119004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
1120004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
1121004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
1122004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
11230d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11240d0321e0SJeremy L Thompson }
11250d0321e0SJeremy L Thompson 
11260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1127cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
1128cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1129cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
1130cbfe683aSSebastian Grimberg   Ceed                ceed;
113122070f95SJeremy L Thompson   char               *diagonal_kernel_source;
113222070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
1133cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
1134cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
1135cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
1136cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
1137cbfe683aSSebastian Grimberg   CeedQFunction       qf;
1138cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
1139cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
1140cbfe683aSSebastian Grimberg 
1141cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1142cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1143cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1144cbfe683aSSebastian Grimberg 
1145cbfe683aSSebastian Grimberg   // Determine active input basis
1146cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1147cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1148cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1149cbfe683aSSebastian Grimberg     CeedVector vec;
1150cbfe683aSSebastian Grimberg 
1151cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1152cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1153cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1154cbfe683aSSebastian Grimberg 
1155cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1156cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1157cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1158cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1159cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1160cbfe683aSSebastian Grimberg       }
1161cbfe683aSSebastian Grimberg     }
1162cbfe683aSSebastian Grimberg   }
1163cbfe683aSSebastian Grimberg 
1164cbfe683aSSebastian Grimberg   // Determine active output basis
1165cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1166cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1167cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1168cbfe683aSSebastian Grimberg     CeedVector vec;
1169cbfe683aSSebastian Grimberg 
1170cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1171cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1172cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1173cbfe683aSSebastian Grimberg 
1174cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1175cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1176cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1177cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1178cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1179cbfe683aSSebastian Grimberg       }
1180cbfe683aSSebastian Grimberg     }
1181cbfe683aSSebastian Grimberg   }
1182cbfe683aSSebastian Grimberg 
1183cbfe683aSSebastian Grimberg   // Operator data struct
1184cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1185cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1186cbfe683aSSebastian Grimberg 
1187cbfe683aSSebastian Grimberg   // Assemble kernel
1188cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
1189cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
1190cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1191cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1192cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1193cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1194cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1195cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1196cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1197cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1198cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1199cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1200cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1201cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1202cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1203cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1204cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1205cbfe683aSSebastian Grimberg }
1206cbfe683aSSebastian Grimberg 
1207cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1208004e4986SSebastian Grimberg // Assemble Diagonal Core
12090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1210b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
12110d0321e0SJeremy L Thompson   Ceed                ceed;
1212cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1213b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
1214b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1215004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1216004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
12170d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
1218b7453713SJeremy L Thompson 
1219b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12202b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
12210d0321e0SJeremy L Thompson 
12220d0321e0SJeremy L Thompson   // Assemble QFunction
1223004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1224004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1225004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
12260d0321e0SJeremy L Thompson 
1227cbfe683aSSebastian Grimberg   // Setup
1228cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
1229cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1230cbfe683aSSebastian Grimberg 
1231cbfe683aSSebastian Grimberg   assert(diag != NULL);
1232cbfe683aSSebastian Grimberg 
1233cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1234cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1235cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1236cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
12379330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1238b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1239b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
12409330daecSnbeams 
1241cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
1242cbfe683aSSebastian Grimberg   }
12430d0321e0SJeremy L Thompson 
1244004e4986SSebastian Grimberg   // Restriction and diagonal vector
1245004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1246004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1247004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1248004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1249004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1250004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1251004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1252004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1253004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
12540d0321e0SJeremy L Thompson   }
1255004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1256004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1257b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
12580d0321e0SJeremy L Thompson 
125991db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1260004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1261004e4986SSebastian Grimberg   if (num_nodes > 0) {
12620d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1263b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
1264b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
12650d0321e0SJeremy L Thompson 
12660d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1267004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1268004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1269004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1270004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1271004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1272b7453713SJeremy L Thompson 
1273b7453713SJeremy L Thompson     if (is_point_block) {
1274004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
12750d0321e0SJeremy L Thompson     } else {
1276004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
12770d0321e0SJeremy L Thompson     }
12780d0321e0SJeremy L Thompson 
12790d0321e0SJeremy L Thompson     // Restore arrays
1280b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1281b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
128291db28b6SZach Atkins   }
12830d0321e0SJeremy L Thompson 
12840d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1285b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
12860d0321e0SJeremy L Thompson 
12870d0321e0SJeremy L Thompson   // Cleanup
1288b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
12890d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12900d0321e0SJeremy L Thompson }
12910d0321e0SJeremy L Thompson 
12920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12930d0321e0SJeremy L Thompson // Assemble Linear Diagonal
12940d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12952b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
12962b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
12976aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12980d0321e0SJeremy L Thompson }
12990d0321e0SJeremy L Thompson 
13000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13010d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
13020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13032b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
13042b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
13056aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13060d0321e0SJeremy L Thompson }
13070d0321e0SJeremy L Thompson 
1308a835093fSnbeams //------------------------------------------------------------------------------
1309004e4986SSebastian Grimberg // Single Operator Assembly Setup
1310a835093fSnbeams //------------------------------------------------------------------------------
13119330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
1312a835093fSnbeams   Ceed                ceed;
131322070f95SJeremy L Thompson   char               *assembly_kernel_source;
131422070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1315004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
13163b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1317004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1318b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1319b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1320b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
1321b7453713SJeremy L Thompson   CeedQFunction       qf;
1322b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1323a835093fSnbeams   CeedOperator_Hip   *impl;
1324b7453713SJeremy L Thompson 
1325b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
13262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1327a835093fSnbeams 
1328a835093fSnbeams   // Get intput and output fields
13292b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1330a835093fSnbeams 
1331a835093fSnbeams   // Determine active input basis eval mode
13322b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
13332b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1334a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1335a835093fSnbeams     CeedVector vec;
1336b7453713SJeremy L Thompson 
13372b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1338a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1339004e4986SSebastian Grimberg       CeedBasis    basis;
1340b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1341b7453713SJeremy L Thompson 
1342004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1343004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1344004e4986SSebastian Grimberg       basis_in = basis;
13452b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1346004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1347004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1348004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
13492b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1350004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1351004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1352004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1353004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1354004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1355004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1356a835093fSnbeams         }
1357004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1358a835093fSnbeams       }
1359a835093fSnbeams     }
1360a835093fSnbeams   }
1361a835093fSnbeams 
1362a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
13632b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1364a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1365a835093fSnbeams     CeedVector vec;
1366b7453713SJeremy L Thompson 
13672b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1368a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1369004e4986SSebastian Grimberg       CeedBasis    basis;
1370b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1371b7453713SJeremy L Thompson 
1372004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1373004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1374004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1375004e4986SSebastian Grimberg       basis_out = basis;
13762b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1377004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1378004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1379004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1380004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1381004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
13822b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1383004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1384004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1385004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1386004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1387004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1388004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1389004e4986SSebastian Grimberg         }
1390004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1391a835093fSnbeams       }
1392a835093fSnbeams     }
1393a835093fSnbeams   }
1394004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1395a835093fSnbeams 
13962b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1397a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1398004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1399004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1400004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1401004e4986SSebastian Grimberg 
1402004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1403004e4986SSebastian Grimberg 
1404004e4986SSebastian Grimberg   if (fallback) {
1405004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1406004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1407004e4986SSebastian Grimberg   }
1408a835093fSnbeams 
1409a835093fSnbeams   // Compile kernels
1410004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1411004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
14122b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
141323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
14142b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
141523d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1416004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1417004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1418004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1419cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
14209330daecSnbeams                                   use_ceedsize_idx));
1421004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
14222b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
14232b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1424a835093fSnbeams 
1425004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1426004e4986SSebastian Grimberg   {
1427004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1428004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1429004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1430004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1431004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1432a835093fSnbeams 
1433004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1434004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1435004e4986SSebastian Grimberg     }
1436004e4986SSebastian Grimberg     if (has_eval_none) {
1437004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1438004e4986SSebastian 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;
1439004e4986SSebastian Grimberg     }
1440b7453713SJeremy L Thompson 
1441b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1442004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1443004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1444004e4986SSebastian Grimberg 
1445004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1446004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1447004e4986SSebastian Grimberg       if (q_comp > 1) {
1448004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1449004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1450004e4986SSebastian Grimberg       }
1451004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1452004e4986SSebastian Grimberg 
1453004e4986SSebastian 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),
1454004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1455004e4986SSebastian Grimberg     }
1456004e4986SSebastian Grimberg 
1457004e4986SSebastian Grimberg     if (identity) {
1458004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1459a835093fSnbeams     }
1460a835093fSnbeams   }
1461a835093fSnbeams 
1462004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1463004e4986SSebastian Grimberg   {
1464004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1465004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1466004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1467004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1468004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1469b7453713SJeremy L Thompson 
1470004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1471004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1472004e4986SSebastian Grimberg     }
1473004e4986SSebastian Grimberg     if (has_eval_none) {
1474004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1475004e4986SSebastian 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;
1476a835093fSnbeams     }
1477a835093fSnbeams 
1478b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1479004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1480004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1481004e4986SSebastian Grimberg 
1482004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1483004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1484004e4986SSebastian Grimberg       if (q_comp > 1) {
1485004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1486004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1487004e4986SSebastian Grimberg       }
1488004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1489004e4986SSebastian Grimberg 
1490004e4986SSebastian 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),
1491004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1492004e4986SSebastian Grimberg     }
1493004e4986SSebastian Grimberg 
1494004e4986SSebastian Grimberg     if (identity) {
1495004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1496a835093fSnbeams     }
1497a835093fSnbeams   }
1498a835093fSnbeams   return CEED_ERROR_SUCCESS;
1499a835093fSnbeams }
1500a835093fSnbeams 
1501a835093fSnbeams //------------------------------------------------------------------------------
1502cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1503cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1504cefa2673SJeremy L Thompson //
1505ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1506cefa2673SJeremy L Thompson // modes).
1507cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1508a835093fSnbeams //------------------------------------------------------------------------------
15092b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1510a835093fSnbeams   Ceed                ceed;
1511b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1512004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1513b7453713SJeremy L Thompson   CeedScalar         *values_array;
1514004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1515b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1516004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1517004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1518004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1519004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1520a835093fSnbeams   CeedOperator_Hip   *impl;
1521b7453713SJeremy L Thompson 
1522b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
15232b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1524a835093fSnbeams 
1525a835093fSnbeams   // Assemble QFunction
1526004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1527004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1528004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1529a835093fSnbeams 
15309330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
15319330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
15329330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1533004e4986SSebastian Grimberg 
15349330daecSnbeams   // Setup
1535004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1536004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1537004e4986SSebastian Grimberg 
1538004e4986SSebastian Grimberg   assert(asmb != NULL);
1539004e4986SSebastian Grimberg 
1540004e4986SSebastian Grimberg   // Assemble element operator
1541004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1542004e4986SSebastian Grimberg   values_array += offset;
1543004e4986SSebastian Grimberg 
1544004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1545004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1546004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1547004e4986SSebastian Grimberg 
1548004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1549004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1550004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1551004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1552004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1553004e4986SSebastian Grimberg   }
1554004e4986SSebastian Grimberg 
1555004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1556004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1557004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1558004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1559004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1560004e4986SSebastian Grimberg 
1561004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1562004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1563004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1564004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1565004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1566004e4986SSebastian Grimberg     }
1567004e4986SSebastian Grimberg   } else {
1568004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1569004e4986SSebastian Grimberg     orients_out      = orients_in;
1570004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
15719330daecSnbeams   }
15729330daecSnbeams 
1573a835093fSnbeams   // Compute B^T D B
1574004e4986SSebastian Grimberg   CeedInt shared_mem =
1575004e4986SSebastian 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)) *
1576004e4986SSebastian Grimberg       sizeof(CeedScalar);
1577004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1578004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1579004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1580b7453713SJeremy L Thompson 
15812b730f8bSJeremy L Thompson   CeedCallBackend(
1582004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1583a835093fSnbeams 
1584a835093fSnbeams   // Restore arrays
15852b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1586004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1587a835093fSnbeams 
1588a835093fSnbeams   // Cleanup
15892b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1590004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1591004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1592004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1593004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1594004e4986SSebastian Grimberg   }
1595004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1596004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1597004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1598004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1599004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1600004e4986SSebastian Grimberg     }
1601004e4986SSebastian Grimberg   }
1602a835093fSnbeams   return CEED_ERROR_SUCCESS;
1603a835093fSnbeams }
1604a835093fSnbeams 
1605a835093fSnbeams //------------------------------------------------------------------------------
160667d9480aSJeremy L Thompson // Assemble Linear QFunction AtPoints
160767d9480aSJeremy L Thompson //------------------------------------------------------------------------------
160867d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
160967d9480aSJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
161067d9480aSJeremy L Thompson }
161167d9480aSJeremy L Thompson 
161267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
161367d9480aSJeremy L Thompson // Assemble Linear Diagonal AtPoints
161467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
161567d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1616382e9c83SJeremy L Thompson   CeedInt             max_num_points, num_elem, num_input_fields, num_output_fields;
1617afe3bc8aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1618afe3bc8aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1619afe3bc8aSJeremy L Thompson   CeedQFunction       qf;
1620afe3bc8aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1621afe3bc8aSJeremy L Thompson   CeedOperator_Hip   *impl;
1622afe3bc8aSJeremy L Thompson 
1623afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1624afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1625afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1626afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1627afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1628afe3bc8aSJeremy L Thompson   CeedInt num_points[num_elem];
1629afe3bc8aSJeremy L Thompson 
1630afe3bc8aSJeremy L Thompson   // Setup
1631afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
1632afe3bc8aSJeremy L Thompson   max_num_points = impl->max_num_points;
1633afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
1634afe3bc8aSJeremy L Thompson 
1635*8a213570SJeremy L Thompson   // Create separate output e-vecs
1636*8a213570SJeremy L Thompson   if (impl->has_shared_e_vecs) {
1637*8a213570SJeremy L Thompson     for (CeedInt i = 0; i < impl->num_outputs; i++) {
1638*8a213570SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
1639*8a213570SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[impl->num_inputs + i]));
1640*8a213570SJeremy L Thompson     }
1641*8a213570SJeremy 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,
1642*8a213570SJeremy L Thompson                                                 num_input_fields, num_output_fields, max_num_points, num_elem));
1643*8a213570SJeremy L Thompson   }
1644*8a213570SJeremy L Thompson   impl->has_shared_e_vecs = false;
1645*8a213570SJeremy L Thompson 
1646afe3bc8aSJeremy L Thompson   // Input Evecs and Restriction
1647afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1648afe3bc8aSJeremy L Thompson 
1649afe3bc8aSJeremy L Thompson   // Get point coordinates
1650afe3bc8aSJeremy L Thompson   if (!impl->point_coords_elem) {
1651afe3bc8aSJeremy L Thompson     CeedVector          point_coords = NULL;
1652afe3bc8aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1653afe3bc8aSJeremy L Thompson 
1654afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1655afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1656afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1657afe3bc8aSJeremy L Thompson   }
1658afe3bc8aSJeremy L Thompson 
1659382e9c83SJeremy L Thompson   // Clear active input Qvecs
1660382e9c83SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1661382e9c83SJeremy L Thompson     CeedVector vec;
1662382e9c83SJeremy L Thompson 
1663382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1664382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1665382e9c83SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
1666382e9c83SJeremy L Thompson   }
1667382e9c83SJeremy L Thompson 
1668afe3bc8aSJeremy L Thompson   // Input basis apply if needed
1669afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1670afe3bc8aSJeremy L Thompson 
1671afe3bc8aSJeremy L Thompson   // Output pointers, as necessary
1672afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1673afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1674afe3bc8aSJeremy L Thompson 
1675afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1676afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1677afe3bc8aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1678afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1679afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1680afe3bc8aSJeremy L Thompson     }
1681afe3bc8aSJeremy L Thompson   }
1682afe3bc8aSJeremy L Thompson 
1683afe3bc8aSJeremy L Thompson   // Loop over active fields
1684afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1685382e9c83SJeremy L Thompson     bool                is_active_at_points = true;
1686382e9c83SJeremy L Thompson     CeedInt             elem_size = 1, num_comp_active = 1, e_vec_size = 0;
1687382e9c83SJeremy L Thompson     CeedRestrictionType rstr_type;
1688382e9c83SJeremy L Thompson     CeedVector          vec;
1689382e9c83SJeremy L Thompson     CeedElemRestriction elem_rstr;
1690382e9c83SJeremy L Thompson 
1691382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1692382e9c83SJeremy L Thompson     // -- Skip non-active input
1693382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1694382e9c83SJeremy L Thompson 
1695382e9c83SJeremy L Thompson     // -- Get active restriction type
1696382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1697382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1698382e9c83SJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1699382e9c83SJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1700382e9c83SJeremy L Thompson     else elem_size = max_num_points;
1701382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1702382e9c83SJeremy L Thompson 
1703382e9c83SJeremy L Thompson     e_vec_size = elem_size * num_comp_active;
1704382e9c83SJeremy L Thompson     for (CeedInt s = 0; s < e_vec_size; s++) {
1705afe3bc8aSJeremy L Thompson       bool         is_active_input = false;
1706afe3bc8aSJeremy L Thompson       CeedEvalMode eval_mode;
1707afe3bc8aSJeremy L Thompson       CeedVector   vec;
1708afe3bc8aSJeremy L Thompson       CeedBasis    basis;
1709afe3bc8aSJeremy L Thompson 
1710afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1711afe3bc8aSJeremy L Thompson       // Skip non-active input
1712afe3bc8aSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1713afe3bc8aSJeremy L Thompson       if (!is_active_input) continue;
1714afe3bc8aSJeremy L Thompson 
1715afe3bc8aSJeremy L Thompson       // Update unit vector
171613062808SJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1717afe3bc8aSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1718afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1719afe3bc8aSJeremy L Thompson 
1720afe3bc8aSJeremy L Thompson       // Basis action
1721afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1722afe3bc8aSJeremy L Thompson       switch (eval_mode) {
1723afe3bc8aSJeremy L Thompson         case CEED_EVAL_NONE:
1724afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1725afe3bc8aSJeremy L Thompson           break;
1726afe3bc8aSJeremy L Thompson         case CEED_EVAL_INTERP:
1727afe3bc8aSJeremy L Thompson         case CEED_EVAL_GRAD:
1728afe3bc8aSJeremy L Thompson         case CEED_EVAL_DIV:
1729afe3bc8aSJeremy L Thompson         case CEED_EVAL_CURL:
1730afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1731afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1732afe3bc8aSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1733afe3bc8aSJeremy L Thompson           break;
1734afe3bc8aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1735afe3bc8aSJeremy L Thompson           break;  // No action
1736afe3bc8aSJeremy L Thompson       }
1737afe3bc8aSJeremy L Thompson 
1738afe3bc8aSJeremy L Thompson       // Q function
1739afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1740afe3bc8aSJeremy L Thompson 
1741afe3bc8aSJeremy L Thompson       // Output basis apply if needed
1742382e9c83SJeremy L Thompson       for (CeedInt j = 0; j < num_output_fields; j++) {
1743afe3bc8aSJeremy L Thompson         bool                is_active_output = false;
1744382e9c83SJeremy L Thompson         CeedInt             elem_size        = 0;
1745382e9c83SJeremy L Thompson         CeedRestrictionType rstr_type;
1746afe3bc8aSJeremy L Thompson         CeedEvalMode        eval_mode;
1747afe3bc8aSJeremy L Thompson         CeedVector          vec;
1748afe3bc8aSJeremy L Thompson         CeedElemRestriction elem_rstr;
1749afe3bc8aSJeremy L Thompson         CeedBasis           basis;
1750afe3bc8aSJeremy L Thompson 
1751382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec));
1752382e9c83SJeremy L Thompson         // ---- Skip non-active output
1753afe3bc8aSJeremy L Thompson         is_active_output = vec == CEED_VECTOR_ACTIVE;
1754afe3bc8aSJeremy L Thompson         if (!is_active_output) continue;
1755afe3bc8aSJeremy L Thompson 
1756382e9c83SJeremy L Thompson         // ---- Check if elem size matches
1757382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1758382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1759382e9c83SJeremy L Thompson         if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue;
1760382e9c83SJeremy L Thompson         if (rstr_type == CEED_RESTRICTION_POINTS) {
1761382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size));
1762382e9c83SJeremy L Thompson         } else {
1763382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1764382e9c83SJeremy L Thompson         }
1765382e9c83SJeremy L Thompson         {
1766382e9c83SJeremy L Thompson           CeedInt num_comp = 0;
1767382e9c83SJeremy L Thompson 
1768382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1769382e9c83SJeremy L Thompson           if (e_vec_size != num_comp * elem_size) continue;
1770382e9c83SJeremy L Thompson         }
1771382e9c83SJeremy L Thompson 
1772afe3bc8aSJeremy L Thompson         // Basis action
1773382e9c83SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
1774afe3bc8aSJeremy L Thompson         switch (eval_mode) {
1775afe3bc8aSJeremy L Thompson           case CEED_EVAL_NONE:
1776382e9c83SJeremy L Thompson             CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields]));
1777afe3bc8aSJeremy L Thompson             break;
1778afe3bc8aSJeremy L Thompson           case CEED_EVAL_INTERP:
1779afe3bc8aSJeremy L Thompson           case CEED_EVAL_GRAD:
1780afe3bc8aSJeremy L Thompson           case CEED_EVAL_DIV:
1781afe3bc8aSJeremy L Thompson           case CEED_EVAL_CURL:
1782382e9c83SJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis));
1783382e9c83SJeremy L Thompson             CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
1784382e9c83SJeremy L Thompson                                                    impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs]));
1785afe3bc8aSJeremy L Thompson             break;
1786afe3bc8aSJeremy L Thompson           // LCOV_EXCL_START
1787afe3bc8aSJeremy L Thompson           case CEED_EVAL_WEIGHT: {
1788afe3bc8aSJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1789afe3bc8aSJeremy L Thompson             // LCOV_EXCL_STOP
1790afe3bc8aSJeremy L Thompson           }
1791afe3bc8aSJeremy L Thompson         }
1792afe3bc8aSJeremy L Thompson 
1793afe3bc8aSJeremy L Thompson         // Mask output e-vec
1794382e9c83SJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs]));
1795afe3bc8aSJeremy L Thompson 
1796afe3bc8aSJeremy L Thompson         // Restrict
1797382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1798382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request));
1799afe3bc8aSJeremy L Thompson 
1800afe3bc8aSJeremy L Thompson         // Reset q_vec for
1801afe3bc8aSJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) {
1802382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields]));
1803382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields]));
1804afe3bc8aSJeremy L Thompson         }
1805afe3bc8aSJeremy L Thompson       }
1806382e9c83SJeremy L Thompson 
1807382e9c83SJeremy L Thompson       // Reset vec
180813062808SJeremy L Thompson       if (s == e_vec_size - 1 && i != num_input_fields - 1) CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
180986e10729SJeremy L Thompson     }
1810afe3bc8aSJeremy L Thompson   }
1811afe3bc8aSJeremy L Thompson 
1812afe3bc8aSJeremy L Thompson   // Restore CEED_EVAL_NONE
1813afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1814afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1815afe3bc8aSJeremy L Thompson 
1816afe3bc8aSJeremy L Thompson     // Get eval_mode
1817afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1818afe3bc8aSJeremy L Thompson 
1819afe3bc8aSJeremy L Thompson     // Restore evec
1820afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1821afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1822afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1823afe3bc8aSJeremy L Thompson     }
1824afe3bc8aSJeremy L Thompson   }
1825afe3bc8aSJeremy L Thompson 
1826afe3bc8aSJeremy L Thompson   // Restore input arrays
1827afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1828afe3bc8aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
182967d9480aSJeremy L Thompson }
183067d9480aSJeremy L Thompson 
183167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
18320d0321e0SJeremy L Thompson // Create operator
18330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
18340d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
18350d0321e0SJeremy L Thompson   Ceed              ceed;
18360d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
18370d0321e0SJeremy L Thompson 
1838b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
18392b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
18402b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
18412b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
18422b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
18432b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
18442b730f8bSJeremy L Thompson   CeedCallBackend(
18452b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
18462b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
18472b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
18482b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
18490d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18500d0321e0SJeremy L Thompson }
18510d0321e0SJeremy L Thompson 
18520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
185367d9480aSJeremy L Thompson // Create operator AtPoints
185467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
185567d9480aSJeremy L Thompson int CeedOperatorCreateAtPoints_Hip(CeedOperator op) {
185667d9480aSJeremy L Thompson   Ceed              ceed;
185767d9480aSJeremy L Thompson   CeedOperator_Hip *impl;
185867d9480aSJeremy L Thompson 
185967d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
186067d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
186167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
186267d9480aSJeremy L Thompson 
186367d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Hip));
186467d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip));
186567d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Hip));
186667d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
186767d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
186867d9480aSJeremy L Thompson }
186967d9480aSJeremy L Thompson 
187067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
1871