xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-operator.c (revision 43e13fee434cb91795700b4c22c1d8174b8fa174)
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
29111870feSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->num_points));
303aab95c0SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_in));
31f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_out));
32f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->apply_add_basis_out));
33b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
34b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
350d0321e0SJeremy L Thompson   }
36b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
37c1222711SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
380d0321e0SJeremy L Thompson 
39b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
40b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
410d0321e0SJeremy L Thompson   }
42b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
430d0321e0SJeremy L Thompson 
44b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
45b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
460d0321e0SJeremy L Thompson   }
47b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
4867d9480aSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem));
490d0321e0SJeremy L Thompson 
50b2165e7aSSebastian Grimberg   // QFunction assembly data
51b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
52b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
530d0321e0SJeremy L Thompson   }
54b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
550d0321e0SJeremy L Thompson 
560d0321e0SJeremy L Thompson   // Diag data
570d0321e0SJeremy L Thompson   if (impl->diag) {
580d0321e0SJeremy L Thompson     Ceed ceed;
59b7453713SJeremy L Thompson 
602b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
61cbfe683aSSebastian Grimberg     if (impl->diag->module) {
622b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
63cbfe683aSSebastian Grimberg     }
64cbfe683aSSebastian Grimberg     if (impl->diag->module_point_block) {
65cbfe683aSSebastian Grimberg       CeedCallHip(ceed, hipModuleUnload(impl->diag->module_point_block));
66cbfe683aSSebastian Grimberg     }
67004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_in));
68004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_out));
692b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
70b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_in));
71b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_out));
72b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_in));
73b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_out));
74004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_in));
75004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_out));
76004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_in));
77004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_out));
78004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
79b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
80b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
81b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
820d0321e0SJeremy L Thompson   }
832b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
840d0321e0SJeremy L Thompson 
85a835093fSnbeams   if (impl->asmb) {
86a835093fSnbeams     Ceed ceed;
87b7453713SJeremy L Thompson 
882b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
892b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
902b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
912b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
92a835093fSnbeams   }
932b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
94a835093fSnbeams 
952b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
960d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
970d0321e0SJeremy L Thompson }
980d0321e0SJeremy L Thompson 
990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1000d0321e0SJeremy L Thompson // Setup infields or outfields
1010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
102f8a0df59SJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, bool *skip_rstr, bool *apply_add_basis,
103f8a0df59SJeremy L Thompson                                        CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
1040d0321e0SJeremy L Thompson   Ceed                ceed;
105b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
106b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
1070d0321e0SJeremy L Thompson 
108b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
109b7453713SJeremy L Thompson   if (is_input) {
110b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
111b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1120d0321e0SJeremy L Thompson   } else {
113b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
114b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1150d0321e0SJeremy L Thompson   }
1160d0321e0SJeremy L Thompson 
1170d0321e0SJeremy L Thompson   // Loop over fields
118b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
119004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
120b7453713SJeremy L Thompson     CeedSize     q_size;
121004e4986SSebastian Grimberg     CeedInt      size;
122004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
123b7453713SJeremy L Thompson     CeedBasis    basis;
1240d0321e0SJeremy L Thompson 
125004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
126004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
127004e4986SSebastian Grimberg       CeedElemRestriction elem_rstr;
1280d0321e0SJeremy L Thompson 
1290d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
130004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
131004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1320d0321e0SJeremy L Thompson 
1330d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
134b7453713SJeremy L Thompson       if (is_input) {
135004e4986SSebastian Grimberg         CeedVector vec;
136004e4986SSebastian Grimberg 
137004e4986SSebastian Grimberg         // Check for passive input
138b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
139b7453713SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
140004e4986SSebastian Grimberg           // Check eval_mode
141004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1420d0321e0SJeremy L Thompson             // Check for strided restriction
143b7453713SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
144b7453713SJeremy L Thompson             if (is_strided) {
1450d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
146b7453713SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1470d0321e0SJeremy L Thompson             }
1480d0321e0SJeremy L Thompson           }
1490d0321e0SJeremy L Thompson         }
1500d0321e0SJeremy L Thompson       }
151b7453713SJeremy L Thompson       if (skip_restriction) {
152ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
153b7453713SJeremy L Thompson         e_vecs[i + start_e] = NULL;
1540d0321e0SJeremy L Thompson       } else {
155b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1560d0321e0SJeremy L Thompson       }
1570d0321e0SJeremy L Thompson     }
1580d0321e0SJeremy L Thompson 
159004e4986SSebastian Grimberg     switch (eval_mode) {
1600d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
161b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
162b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
163b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1640d0321e0SJeremy L Thompson         break;
1650d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1660d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
167004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
168004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
169b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
170b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
171b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1720d0321e0SJeremy L Thompson         break;
1730d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
174b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
175b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
176b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
17767d9480aSJeremy L Thompson         if (is_at_points) {
17867d9480aSJeremy L Thompson           CeedInt num_points[num_elem];
17967d9480aSJeremy L Thompson 
18067d9480aSJeremy L Thompson           for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q;
18167d9480aSJeremy L Thompson           CeedCallBackend(
18267d9480aSJeremy L Thompson               CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i]));
18367d9480aSJeremy L Thompson         } else {
184b7453713SJeremy L Thompson           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
18567d9480aSJeremy L Thompson         }
1860d0321e0SJeremy L Thompson         break;
1870d0321e0SJeremy L Thompson     }
1880d0321e0SJeremy L Thompson   }
189f8a0df59SJeremy L Thompson   // Drop duplicate restrictions
1903aab95c0SJeremy L Thompson   if (is_input) {
1913aab95c0SJeremy L Thompson     for (CeedInt i = 0; i < num_fields; i++) {
1923aab95c0SJeremy L Thompson       CeedVector          vec_i;
1933aab95c0SJeremy L Thompson       CeedElemRestriction rstr_i;
1943aab95c0SJeremy L Thompson 
1953aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
1963aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
1973aab95c0SJeremy L Thompson       for (CeedInt j = i + 1; j < num_fields; j++) {
1983aab95c0SJeremy L Thompson         CeedVector          vec_j;
1993aab95c0SJeremy L Thompson         CeedElemRestriction rstr_j;
2003aab95c0SJeremy L Thompson 
2013aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
2023aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
2033aab95c0SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
204f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e]));
2053aab95c0SJeremy L Thompson           skip_rstr[j] = true;
2063aab95c0SJeremy L Thompson         }
2073aab95c0SJeremy L Thompson       }
2083aab95c0SJeremy L Thompson     }
209f8a0df59SJeremy L Thompson   } else {
210f8a0df59SJeremy L Thompson     for (CeedInt i = num_fields - 1; i >= 0; i--) {
211f8a0df59SJeremy L Thompson       CeedVector          vec_i;
212f8a0df59SJeremy L Thompson       CeedElemRestriction rstr_i;
213f8a0df59SJeremy L Thompson 
214f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
215f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
216f8a0df59SJeremy L Thompson       for (CeedInt j = i - 1; j >= 0; j--) {
217f8a0df59SJeremy L Thompson         CeedVector          vec_j;
218f8a0df59SJeremy L Thompson         CeedElemRestriction rstr_j;
219f8a0df59SJeremy L Thompson 
220f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
221f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
222f8a0df59SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
223f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e]));
224f8a0df59SJeremy L Thompson           skip_rstr[j]       = true;
225f8a0df59SJeremy L Thompson           apply_add_basis[i] = true;
226f8a0df59SJeremy L Thompson         }
227f8a0df59SJeremy L Thompson       }
228f8a0df59SJeremy L Thompson     }
2293aab95c0SJeremy L Thompson   }
2300d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2310d0321e0SJeremy L Thompson }
2320d0321e0SJeremy L Thompson 
2330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
234ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
2350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2360d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
2370d0321e0SJeremy L Thompson   Ceed                ceed;
238b7453713SJeremy L Thompson   bool                is_setup_done;
239b7453713SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
240b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
2410d0321e0SJeremy L Thompson   CeedQFunction       qf;
242b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
243b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
244b7453713SJeremy L Thompson 
245b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
246b7453713SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
247b7453713SJeremy L Thompson 
248b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
249b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
2502b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
2512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
252b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
253b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
254b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2550d0321e0SJeremy L Thompson 
2560d0321e0SJeremy L Thompson   // Allocate
257b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
2583aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
259f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
260f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
261c1222711SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
262b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
263b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
264b7453713SJeremy L Thompson   impl->num_inputs  = num_input_fields;
265b7453713SJeremy L Thompson   impl->num_outputs = num_output_fields;
2660d0321e0SJeremy L Thompson 
26741655a23SJeremy L Thompson   // Set up infield and outfield e-vecs and q-vecs
2680d0321e0SJeremy L Thompson   // Infields
2693aab95c0SJeremy L Thompson   CeedCallBackend(
270f8a0df59SJeremy 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));
2710d0321e0SJeremy L Thompson   // Outfields
272f8a0df59SJeremy 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,
273f8a0df59SJeremy L Thompson                                               num_input_fields, num_output_fields, Q, num_elem));
2740d0321e0SJeremy L Thompson 
27541655a23SJeremy L Thompson   // Reuse active e-vecs where able
27641655a23SJeremy L Thompson   {
27741655a23SJeremy L Thompson     CeedInt              num_used  = 0;
27841655a23SJeremy L Thompson     CeedElemRestriction *rstr_used = NULL;
27941655a23SJeremy L Thompson 
28041655a23SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
28141655a23SJeremy L Thompson       bool                is_used = false;
28241655a23SJeremy L Thompson       CeedVector          vec_i;
28341655a23SJeremy L Thompson       CeedElemRestriction rstr_i;
28441655a23SJeremy L Thompson 
28541655a23SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
28641655a23SJeremy L Thompson       if (vec_i != CEED_VECTOR_ACTIVE) continue;
28741655a23SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
28841655a23SJeremy L Thompson       for (CeedInt j = 0; j < num_used; j++) {
28941655a23SJeremy L Thompson         if (rstr_i == rstr_used[i]) is_used = true;
29041655a23SJeremy L Thompson       }
29141655a23SJeremy L Thompson       if (is_used) continue;
29241655a23SJeremy L Thompson       num_used++;
29341655a23SJeremy L Thompson       if (num_used == 1) CeedCallBackend(CeedCalloc(num_used, &rstr_used));
29441655a23SJeremy L Thompson       else CeedCallBackend(CeedRealloc(num_used, &rstr_used));
29541655a23SJeremy L Thompson       rstr_used[num_used - 1] = rstr_i;
29641655a23SJeremy L Thompson       for (CeedInt j = num_output_fields - 1; j >= 0; j--) {
29741655a23SJeremy L Thompson         CeedEvalMode        eval_mode;
29841655a23SJeremy L Thompson         CeedVector          vec_j;
29941655a23SJeremy L Thompson         CeedElemRestriction rstr_j;
30041655a23SJeremy L Thompson 
30141655a23SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec_j));
30241655a23SJeremy L Thompson         if (vec_j != CEED_VECTOR_ACTIVE) continue;
30341655a23SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
30441655a23SJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) continue;
30541655a23SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &rstr_j));
30641655a23SJeremy L Thompson         if (rstr_i == rstr_j) {
30741655a23SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(impl->e_vecs[i], &impl->e_vecs[j + impl->num_inputs]));
30841655a23SJeremy L Thompson         }
30941655a23SJeremy L Thompson       }
31041655a23SJeremy L Thompson     }
31141655a23SJeremy L Thompson     CeedCallBackend(CeedFree(&rstr_used));
31241655a23SJeremy L Thompson   }
3138a213570SJeremy L Thompson   impl->has_shared_e_vecs = true;
3142b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
3150d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3160d0321e0SJeremy L Thompson }
3170d0321e0SJeremy L Thompson 
3180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
319*43e13feeSJeremy L Thompson // Restrict Operator Inputs
3200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
321*43e13feeSJeremy L Thompson static inline int CeedOperatorInputRestrict_Hip(CeedOperatorField op_input_field, CeedQFunctionField qf_input_field, CeedInt input_field,
322*43e13feeSJeremy L Thompson                                                 CeedVector in_vec, const bool skip_active, CeedScalar **e_data, CeedOperator_Hip *impl,
323*43e13feeSJeremy L Thompson                                                 CeedRequest *request) {
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
329*43e13feeSJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetVector(op_input_field, &vec));
3300d0321e0SJeremy L Thompson   if (vec == CEED_VECTOR_ACTIVE) {
331*43e13feeSJeremy L Thompson     if (skip_active) return CEED_ERROR_SUCCESS;
332b7453713SJeremy L Thompson     else vec = in_vec;
3330d0321e0SJeremy L Thompson   }
3340d0321e0SJeremy L Thompson 
335*43e13feeSJeremy L Thompson   // Restriction action
336*43e13feeSJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_field, &eval_mode));
337004e4986SSebastian Grimberg   if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3380d0321e0SJeremy L Thompson   } else {
3390d0321e0SJeremy L Thompson     // Get input vector
340*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_field, &vec));
3410d0321e0SJeremy L Thompson     // Get input element restriction
342*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_field, &elem_rstr));
343b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
3440d0321e0SJeremy L Thompson     // Restrict, if necessary
345*43e13feeSJeremy L Thompson     if (!impl->e_vecs[input_field]) {
3460d0321e0SJeremy L Thompson       // No restriction for this field; read data directly from vec.
347*43e13feeSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)e_data));
3480d0321e0SJeremy L Thompson     } else {
349c1222711SJeremy L Thompson       uint64_t state;
350c1222711SJeremy L Thompson 
351c1222711SJeremy L Thompson       CeedCallBackend(CeedVectorGetState(vec, &state));
352*43e13feeSJeremy L Thompson       if ((state != impl->input_states[input_field] || vec == in_vec) && !impl->skip_rstr_in[input_field]) {
353*43e13feeSJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[input_field], request));
354c1222711SJeremy L Thompson       }
355*43e13feeSJeremy L Thompson       impl->input_states[input_field] = state;
3560d0321e0SJeremy L Thompson       // Get evec
357*43e13feeSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[input_field], CEED_MEM_DEVICE, (const CeedScalar **)e_data));
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 //------------------------------------------------------------------------------
366*43e13feeSJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedOperatorField op_input_field, CeedQFunctionField qf_input_field, CeedInt input_field,
367*43e13feeSJeremy L Thompson                                              CeedInt num_elem, const bool skip_active, CeedScalar *e_data, CeedOperator_Hip *impl) {
368004e4986SSebastian Grimberg   CeedEvalMode eval_mode;
3690d0321e0SJeremy L Thompson 
3700d0321e0SJeremy L Thompson   // Skip active input
371b7453713SJeremy L Thompson   if (skip_active) {
3720d0321e0SJeremy L Thompson     CeedVector vec;
373b7453713SJeremy L Thompson 
374*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_field, &vec));
375*43e13feeSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) return CEED_ERROR_SUCCESS;
3760d0321e0SJeremy L Thompson   }
377*43e13feeSJeremy L Thompson 
3780d0321e0SJeremy L Thompson   // Basis action
379*43e13feeSJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_field, &eval_mode));
380004e4986SSebastian Grimberg   switch (eval_mode) {
3810d0321e0SJeremy L Thompson     case CEED_EVAL_NONE:
382*43e13feeSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[input_field], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data));
3830d0321e0SJeremy L Thompson       break;
3840d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP:
3850d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD:
386004e4986SSebastian Grimberg     case CEED_EVAL_DIV:
387*43e13feeSJeremy L Thompson     case CEED_EVAL_CURL: {
388*43e13feeSJeremy L Thompson       CeedBasis basis;
389*43e13feeSJeremy L Thompson 
390*43e13feeSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_field, &basis));
391*43e13feeSJeremy L Thompson       CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[input_field], impl->q_vecs_in[input_field]));
3920d0321e0SJeremy L Thompson       break;
393*43e13feeSJeremy L Thompson     }
3940d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
3950d0321e0SJeremy L Thompson       break;  // No action
3960d0321e0SJeremy L Thompson   }
3970d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3980d0321e0SJeremy L Thompson }
3990d0321e0SJeremy L Thompson 
4000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4010d0321e0SJeremy L Thompson // Restore Input Vectors
4020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
403*43e13feeSJeremy L Thompson static inline int CeedOperatorInputRestore_Hip(CeedOperatorField op_input_field, CeedQFunctionField qf_input_field, CeedInt input_field,
404*43e13feeSJeremy L Thompson                                                const bool skip_active, CeedScalar **e_data, CeedOperator_Hip *impl) {
405004e4986SSebastian Grimberg   CeedEvalMode eval_mode;
4060d0321e0SJeremy L Thompson   CeedVector   vec;
407004e4986SSebastian Grimberg 
4080d0321e0SJeremy L Thompson   // Skip active input
409b7453713SJeremy L Thompson   if (skip_active) {
410*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_field, &vec));
411*43e13feeSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) return CEED_ERROR_SUCCESS;
4120d0321e0SJeremy L Thompson   }
413*43e13feeSJeremy L Thompson 
414*43e13feeSJeremy L Thompson   // Restore e-vec
415*43e13feeSJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_field, &eval_mode));
416004e4986SSebastian Grimberg   if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
4170d0321e0SJeremy L Thompson   } else {
418*43e13feeSJeremy L Thompson     if (!impl->e_vecs[input_field]) {  // This was a skip_restriction case
419*43e13feeSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_field, &vec));
420*43e13feeSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)e_data));
4210d0321e0SJeremy L Thompson     } else {
422*43e13feeSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[input_field], (const CeedScalar **)e_data));
4230d0321e0SJeremy L Thompson     }
4240d0321e0SJeremy L Thompson   }
4250d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4260d0321e0SJeremy L Thompson }
4270d0321e0SJeremy L Thompson 
4280d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4290d0321e0SJeremy L Thompson // Apply and add to output
4300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
431b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
432b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
433b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
434b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
4350d0321e0SJeremy L Thompson   CeedQFunction       qf;
436b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
437b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
438b7453713SJeremy L Thompson 
439b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4402b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4412b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
442b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
443b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
444b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4450d0321e0SJeremy L Thompson 
4460d0321e0SJeremy L Thompson   // Setup
4472b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4480d0321e0SJeremy L Thompson 
449*43e13feeSJeremy L Thompson   // Process inputs
450*43e13feeSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
451*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputRestrict_Hip(op_input_fields[i], qf_input_fields[i], i, in_vec, false, &e_data[i], impl, request));
452*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Hip(op_input_fields[i], qf_input_fields[i], i, num_elem, false, e_data[i], impl));
453*43e13feeSJeremy L Thompson   }
4540d0321e0SJeremy L Thompson 
4550d0321e0SJeremy L Thompson   // Output pointers, as necessary
456b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
457004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
458b7453713SJeremy L Thompson 
459004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
460004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
4610d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
462b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
463b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
4640d0321e0SJeremy L Thompson     }
4650d0321e0SJeremy L Thompson   }
4660d0321e0SJeremy L Thompson 
4670d0321e0SJeremy L Thompson   // Q function
468b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
4690d0321e0SJeremy L Thompson 
47041655a23SJeremy L Thompson   // Restore input arrays
471*43e13feeSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
472*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputRestore_Hip(op_input_fields[i], qf_input_fields[i], i, false, &e_data[i], impl));
473*43e13feeSJeremy L Thompson   }
47441655a23SJeremy L Thompson 
4750d0321e0SJeremy L Thompson   // Output basis apply if needed
476b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
477004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
478b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
479b7453713SJeremy L Thompson     CeedBasis           basis;
480b7453713SJeremy L Thompson 
481004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
482b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
483b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
484004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
485b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4860d0321e0SJeremy L Thompson     // Basis action
487004e4986SSebastian Grimberg     switch (eval_mode) {
4880d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
489004e4986SSebastian Grimberg         break;  // No action
4900d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4910d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
492004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
493004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
494b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
495f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
496f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAdd(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
497f8a0df59SJeremy L Thompson         } else {
498004e4986SSebastian Grimberg           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
499f8a0df59SJeremy L Thompson         }
5000d0321e0SJeremy L Thompson         break;
5010d0321e0SJeremy L Thompson       // LCOV_EXCL_START
5020d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
5036e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
5040d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
5050d0321e0SJeremy L Thompson       }
5060d0321e0SJeremy L Thompson     }
507004e4986SSebastian Grimberg   }
5080d0321e0SJeremy L Thompson 
5090d0321e0SJeremy L Thompson   // Output restriction
510b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
511004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
512b7453713SJeremy L Thompson     CeedVector          vec;
513b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
514b7453713SJeremy L Thompson 
5150d0321e0SJeremy L Thompson     // Restore evec
516004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
517004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
518b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
5190d0321e0SJeremy L Thompson     }
520f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
5210d0321e0SJeremy L Thompson     // Get output vector
522b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5230d0321e0SJeremy L Thompson     // Restrict
524b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
5250d0321e0SJeremy L Thompson     // Active
526b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
5270d0321e0SJeremy L Thompson 
528b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
5290d0321e0SJeremy L Thompson   }
5300d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5310d0321e0SJeremy L Thompson }
5320d0321e0SJeremy L Thompson 
5330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
53467d9480aSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
53567d9480aSJeremy L Thompson //------------------------------------------------------------------------------
53667d9480aSJeremy L Thompson static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) {
53767d9480aSJeremy L Thompson   Ceed                ceed;
53867d9480aSJeremy L Thompson   bool                is_setup_done;
53967d9480aSJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
54067d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
54167d9480aSJeremy L Thompson   CeedQFunction       qf;
54267d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
54367d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
54467d9480aSJeremy L Thompson 
54567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
54667d9480aSJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
54767d9480aSJeremy L Thompson 
54867d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
54967d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
55067d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
55167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
55267d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
55367d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
55467d9480aSJeremy L Thompson   {
555111870feSJeremy L Thompson     CeedElemRestriction rstr_points = NULL;
55667d9480aSJeremy L Thompson 
557111870feSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
558111870feSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points));
559111870feSJeremy L Thompson     CeedCallBackend(CeedCalloc(num_elem, &impl->num_points));
560111870feSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
561111870feSJeremy L Thompson       CeedInt num_points_elem;
562111870feSJeremy L Thompson 
563111870feSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumPointsInElement(rstr_points, e, &num_points_elem));
564111870feSJeremy L Thompson       impl->num_points[e] = num_points_elem;
565111870feSJeremy L Thompson     }
56667d9480aSJeremy L Thompson   }
56767d9480aSJeremy L Thompson   impl->max_num_points = max_num_points;
56867d9480aSJeremy L Thompson 
56967d9480aSJeremy L Thompson   // Allocate
57067d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
5713aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
572f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
573f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
57467d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
57567d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
57667d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
57767d9480aSJeremy L Thompson   impl->num_inputs  = num_input_fields;
57867d9480aSJeremy L Thompson   impl->num_outputs = num_output_fields;
57967d9480aSJeremy L Thompson 
5808a213570SJeremy L Thompson   // Set up infield and outfield e-vecs and q-vecs
58167d9480aSJeremy L Thompson   // Infields
582f8a0df59SJeremy 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,
5833aab95c0SJeremy L Thompson                                               max_num_points, num_elem));
58467d9480aSJeremy L Thompson   // Outfields
585f8a0df59SJeremy 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,
586f8a0df59SJeremy L Thompson                                               num_input_fields, num_output_fields, max_num_points, num_elem));
58767d9480aSJeremy L Thompson 
5888a213570SJeremy L Thompson   // Reuse active e-vecs where able
5898a213570SJeremy L Thompson   {
5908a213570SJeremy L Thompson     CeedInt              num_used  = 0;
5918a213570SJeremy L Thompson     CeedElemRestriction *rstr_used = NULL;
5928a213570SJeremy L Thompson 
5938a213570SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
5948a213570SJeremy L Thompson       bool                is_used = false;
5958a213570SJeremy L Thompson       CeedVector          vec_i;
5968a213570SJeremy L Thompson       CeedElemRestriction rstr_i;
5978a213570SJeremy L Thompson 
5988a213570SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
5998a213570SJeremy L Thompson       if (vec_i != CEED_VECTOR_ACTIVE) continue;
6008a213570SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
6018a213570SJeremy L Thompson       for (CeedInt j = 0; j < num_used; j++) {
6028a213570SJeremy L Thompson         if (rstr_i == rstr_used[i]) is_used = true;
6038a213570SJeremy L Thompson       }
6048a213570SJeremy L Thompson       if (is_used) continue;
6058a213570SJeremy L Thompson       num_used++;
6068a213570SJeremy L Thompson       if (num_used == 1) CeedCallBackend(CeedCalloc(num_used, &rstr_used));
6078a213570SJeremy L Thompson       else CeedCallBackend(CeedRealloc(num_used, &rstr_used));
6088a213570SJeremy L Thompson       rstr_used[num_used - 1] = rstr_i;
6098a213570SJeremy L Thompson       for (CeedInt j = num_output_fields - 1; j >= 0; j--) {
6108a213570SJeremy L Thompson         CeedEvalMode        eval_mode;
6118a213570SJeremy L Thompson         CeedVector          vec_j;
6128a213570SJeremy L Thompson         CeedElemRestriction rstr_j;
6138a213570SJeremy L Thompson 
6148a213570SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec_j));
6158a213570SJeremy L Thompson         if (vec_j != CEED_VECTOR_ACTIVE) continue;
6168a213570SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
6178a213570SJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) continue;
6188a213570SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &rstr_j));
6198a213570SJeremy L Thompson         if (rstr_i == rstr_j) {
6208a213570SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(impl->e_vecs[i], &impl->e_vecs[j + impl->num_inputs]));
6218a213570SJeremy L Thompson         }
6228a213570SJeremy L Thompson       }
6238a213570SJeremy L Thompson     }
6248a213570SJeremy L Thompson     CeedCallBackend(CeedFree(&rstr_used));
6258a213570SJeremy L Thompson   }
6268a213570SJeremy L Thompson   impl->has_shared_e_vecs = true;
62767d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
62867d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
62967d9480aSJeremy L Thompson }
63067d9480aSJeremy L Thompson 
63167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
63267d9480aSJeremy L Thompson // Input Basis Action AtPoints
63367d9480aSJeremy L Thompson //------------------------------------------------------------------------------
634*43e13feeSJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Hip(CeedOperatorField op_input_field, CeedQFunctionField qf_input_field, CeedInt input_field,
635*43e13feeSJeremy L Thompson                                                      CeedInt num_elem, const CeedInt *num_points, const bool skip_active, CeedScalar *e_data,
636*43e13feeSJeremy L Thompson                                                      CeedOperator_Hip *impl) {
63767d9480aSJeremy L Thompson   CeedEvalMode eval_mode;
63867d9480aSJeremy L Thompson 
63967d9480aSJeremy L Thompson   // Skip active input
64067d9480aSJeremy L Thompson   if (skip_active) {
64167d9480aSJeremy L Thompson     CeedVector vec;
64267d9480aSJeremy L Thompson 
643*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_field, &vec));
644*43e13feeSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) return CEED_ERROR_SUCCESS;
64567d9480aSJeremy L Thompson   }
646*43e13feeSJeremy L Thompson 
64767d9480aSJeremy L Thompson   // Basis action
648*43e13feeSJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_field, &eval_mode));
64967d9480aSJeremy L Thompson   switch (eval_mode) {
65067d9480aSJeremy L Thompson     case CEED_EVAL_NONE:
651*43e13feeSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[input_field], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data));
65267d9480aSJeremy L Thompson       break;
65367d9480aSJeremy L Thompson     case CEED_EVAL_INTERP:
65467d9480aSJeremy L Thompson     case CEED_EVAL_GRAD:
65567d9480aSJeremy L Thompson     case CEED_EVAL_DIV:
656*43e13feeSJeremy L Thompson     case CEED_EVAL_CURL: {
657*43e13feeSJeremy L Thompson       CeedBasis basis;
658*43e13feeSJeremy L Thompson 
659*43e13feeSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_field, &basis));
660*43e13feeSJeremy L Thompson       CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem,
661*43e13feeSJeremy L Thompson                                              impl->e_vecs[input_field], impl->q_vecs_in[input_field]));
66267d9480aSJeremy L Thompson       break;
663*43e13feeSJeremy L Thompson     }
66467d9480aSJeremy L Thompson     case CEED_EVAL_WEIGHT:
66567d9480aSJeremy L Thompson       break;  // No action
66667d9480aSJeremy L Thompson   }
66767d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
66867d9480aSJeremy L Thompson }
66967d9480aSJeremy L Thompson 
67067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
67167d9480aSJeremy L Thompson // Apply and add to output AtPoints
67267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
67367d9480aSJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
674111870feSJeremy L Thompson   CeedInt             max_num_points, *num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
67567d9480aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
67667d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
67767d9480aSJeremy L Thompson   CeedQFunction       qf;
67867d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
67967d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
68067d9480aSJeremy L Thompson 
68167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
68267d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
68367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
68467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
68567d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
68667d9480aSJeremy L Thompson 
68767d9480aSJeremy L Thompson   // Setup
68867d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
689111870feSJeremy L Thompson   num_points     = impl->num_points;
69067d9480aSJeremy L Thompson   max_num_points = impl->max_num_points;
69167d9480aSJeremy L Thompson 
69267d9480aSJeremy L Thompson   // Get point coordinates
69367d9480aSJeremy L Thompson   if (!impl->point_coords_elem) {
69467d9480aSJeremy L Thompson     CeedVector          point_coords = NULL;
69567d9480aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
69667d9480aSJeremy L Thompson 
69767d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
69867d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
69967d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
70067d9480aSJeremy L Thompson   }
70167d9480aSJeremy L Thompson 
702*43e13feeSJeremy L Thompson   // Process inputs
703*43e13feeSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
704*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputRestrict_Hip(op_input_fields[i], qf_input_fields[i], i, in_vec, false, &e_data[i], impl, request));
705*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(op_input_fields[i], qf_input_fields[i], i, num_elem, num_points, false, e_data[i], impl));
706*43e13feeSJeremy L Thompson   }
70767d9480aSJeremy L Thompson 
70867d9480aSJeremy L Thompson   // Output pointers, as necessary
70967d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
71067d9480aSJeremy L Thompson     CeedEvalMode eval_mode;
71167d9480aSJeremy L Thompson 
71267d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
71367d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
71467d9480aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
71567d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
71667d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
71767d9480aSJeremy L Thompson     }
71867d9480aSJeremy L Thompson   }
71967d9480aSJeremy L Thompson 
72067d9480aSJeremy L Thompson   // Q function
72167d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
72267d9480aSJeremy L Thompson 
7238a213570SJeremy L Thompson   // Restore input arrays
724*43e13feeSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
725*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputRestore_Hip(op_input_fields[i], qf_input_fields[i], i, false, &e_data[i], impl));
726*43e13feeSJeremy L Thompson   }
7278a213570SJeremy L Thompson 
72867d9480aSJeremy L Thompson   // Output basis apply if needed
72967d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
73067d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
73167d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
73267d9480aSJeremy L Thompson     CeedBasis           basis;
73367d9480aSJeremy L Thompson 
73467d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
73567d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
73667d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
73767d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
73867d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
73967d9480aSJeremy L Thompson     // Basis action
74067d9480aSJeremy L Thompson     switch (eval_mode) {
74167d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
74267d9480aSJeremy L Thompson         break;  // No action
74367d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
74467d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
74567d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
74667d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
74767d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
748f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
749f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAddAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
750f8a0df59SJeremy L Thompson                                                     impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
751f8a0df59SJeremy L Thompson         } else {
75267d9480aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
75367d9480aSJeremy L Thompson                                                  impl->e_vecs[i + impl->num_inputs]));
754f8a0df59SJeremy L Thompson         }
75567d9480aSJeremy L Thompson         break;
75667d9480aSJeremy L Thompson       // LCOV_EXCL_START
75767d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT: {
75867d9480aSJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
75967d9480aSJeremy L Thompson         // LCOV_EXCL_STOP
76067d9480aSJeremy L Thompson       }
76167d9480aSJeremy L Thompson     }
76267d9480aSJeremy L Thompson   }
76367d9480aSJeremy L Thompson 
76467d9480aSJeremy L Thompson   // Output restriction
76567d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
76667d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
76767d9480aSJeremy L Thompson     CeedVector          vec;
76867d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
76967d9480aSJeremy L Thompson 
77067d9480aSJeremy L Thompson     // Restore evec
77167d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
77267d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
77367d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
77467d9480aSJeremy L Thompson     }
775f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
77667d9480aSJeremy L Thompson     // Get output vector
77767d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
77867d9480aSJeremy L Thompson     // Restrict
77967d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
78067d9480aSJeremy L Thompson     // Active
78167d9480aSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
78267d9480aSJeremy L Thompson 
78367d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
78467d9480aSJeremy L Thompson   }
78567d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
78667d9480aSJeremy L Thompson }
78767d9480aSJeremy L Thompson 
78867d9480aSJeremy L Thompson //------------------------------------------------------------------------------
789004e4986SSebastian Grimberg // Linear QFunction Assembly Core
7900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7912b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
7920d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
793b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
794b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
795b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
796004e4986SSebastian Grimberg   CeedVector         *active_inputs;
797b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
798b7453713SJeremy L Thompson   CeedQFunction       qf;
799b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
800b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
801b7453713SJeremy L Thompson 
8022b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
803b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
804e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
805e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
806b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
807004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
808b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
809b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
810004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
811004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
8120d0321e0SJeremy L Thompson 
8130d0321e0SJeremy L Thompson   // Setup
8142b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
8150d0321e0SJeremy L Thompson 
8160d0321e0SJeremy L Thompson   // Input Evecs and Restriction
817*43e13feeSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
818*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputRestrict_Hip(op_input_fields[i], qf_input_fields[i], i, NULL, true, &e_data[i], impl, request));
819*43e13feeSJeremy L Thompson   }
8200d0321e0SJeremy L Thompson 
8210d0321e0SJeremy L Thompson   // Count number of active input fields
822b7453713SJeremy L Thompson   if (!num_active_in) {
823b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
824b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
825b7453713SJeremy L Thompson       CeedVector  vec;
826b7453713SJeremy L Thompson 
8270d0321e0SJeremy L Thompson       // Get input vector
828b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
8290d0321e0SJeremy L Thompson       // Check if active input
8300d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
831b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
832b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
833b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
834004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
8350d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
836004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
837004e4986SSebastian Grimberg 
838004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
839b7453713SJeremy L Thompson           CeedCallBackend(
840004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
8410d0321e0SJeremy L Thompson         }
842b7453713SJeremy L Thompson         num_active_in += size;
843b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
8440d0321e0SJeremy L Thompson       }
8450d0321e0SJeremy L Thompson     }
846b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
847004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
8480d0321e0SJeremy L Thompson   }
8490d0321e0SJeremy L Thompson 
8500d0321e0SJeremy L Thompson   // Count number of active output fields
851b7453713SJeremy L Thompson   if (!num_active_out) {
852b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
853b7453713SJeremy L Thompson       CeedVector vec;
854b7453713SJeremy L Thompson 
8550d0321e0SJeremy L Thompson       // Get output vector
856b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
8570d0321e0SJeremy L Thompson       // Check if active output
8580d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
859b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
860b7453713SJeremy L Thompson         num_active_out += size;
8610d0321e0SJeremy L Thompson       }
8620d0321e0SJeremy L Thompson     }
863b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
8640d0321e0SJeremy L Thompson   }
8650d0321e0SJeremy L Thompson 
8660d0321e0SJeremy L Thompson   // Check sizes
867b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
8680d0321e0SJeremy L Thompson 
8690d0321e0SJeremy L Thompson   // Build objects if needed
8700d0321e0SJeremy L Thompson   if (build_objects) {
871b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
872b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
873b7453713SJeremy L Thompson 
874004e4986SSebastian Grimberg     // Create output restriction
875b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
8760a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
8770a5597ceSJeremy L Thompson                                                      rstr));
8780d0321e0SJeremy L Thompson     // Create assembled vector
879b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
8800d0321e0SJeremy L Thompson   }
8812b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
882b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
8830d0321e0SJeremy L Thompson 
8840d0321e0SJeremy L Thompson   // Input basis apply
885*43e13feeSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
886*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasis_Hip(op_input_fields[i], qf_input_fields[i], i, num_elem, true, e_data[i], impl));
887*43e13feeSJeremy L Thompson   }
8880d0321e0SJeremy L Thompson 
8890d0321e0SJeremy L Thompson   // Assemble QFunction
890b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
8910d0321e0SJeremy L Thompson     // Set Inputs
892004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
893b7453713SJeremy L Thompson     if (num_active_in > 1) {
894004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
8950d0321e0SJeremy L Thompson     }
8960d0321e0SJeremy L Thompson     // Set Outputs
897b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
898b7453713SJeremy L Thompson       CeedVector vec;
899b7453713SJeremy L Thompson 
9000d0321e0SJeremy L Thompson       // Get output vector
901b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
9020d0321e0SJeremy L Thompson       // Check if active output
9030d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
904b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
905b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
906b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
9070d0321e0SJeremy L Thompson       }
9080d0321e0SJeremy L Thompson     }
9090d0321e0SJeremy L Thompson     // Apply QFunction
910b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
9110d0321e0SJeremy L Thompson   }
9120d0321e0SJeremy L Thompson 
9138a213570SJeremy L Thompson   // Un-set output q-vecs to prevent accidental overwrite of Assembled
914b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
915b7453713SJeremy L Thompson     CeedVector vec;
916b7453713SJeremy L Thompson 
9170d0321e0SJeremy L Thompson     // Get output vector
918b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
9190d0321e0SJeremy L Thompson     // Check if active output
9200d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
921b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
9220d0321e0SJeremy L Thompson     }
9230d0321e0SJeremy L Thompson   }
9240d0321e0SJeremy L Thompson 
9250d0321e0SJeremy L Thompson   // Restore input arrays
926*43e13feeSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
927*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputRestore_Hip(op_input_fields[i], qf_input_fields[i], i, true, &e_data[i], impl));
928*43e13feeSJeremy L Thompson   }
9290d0321e0SJeremy L Thompson 
9300d0321e0SJeremy L Thompson   // Restore output
931b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
9320d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9330d0321e0SJeremy L Thompson }
9340d0321e0SJeremy L Thompson 
9350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9360d0321e0SJeremy L Thompson // Assemble Linear QFunction
9370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9382b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
9392b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
9400d0321e0SJeremy L Thompson }
9410d0321e0SJeremy L Thompson 
9420d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
943b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
9440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9452b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
9462b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
9470d0321e0SJeremy L Thompson }
9480d0321e0SJeremy L Thompson 
9490d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
950004e4986SSebastian Grimberg // Assemble Diagonal Setup
9510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
952cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
9530d0321e0SJeremy L Thompson   Ceed                ceed;
954004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
955cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
956004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
957b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
958b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
9590d0321e0SJeremy L Thompson   CeedQFunction       qf;
960b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
961b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
962b7453713SJeremy L Thompson 
963b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
9642b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
965b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
9660d0321e0SJeremy L Thompson 
9670d0321e0SJeremy L Thompson   // Determine active input basis
968b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
969b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
970b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9710d0321e0SJeremy L Thompson     CeedVector vec;
972b7453713SJeremy L Thompson 
973b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
9740d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
975004e4986SSebastian Grimberg       CeedBasis    basis;
976004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
977b7453713SJeremy L Thompson 
978004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
979004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
980004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
981004e4986SSebastian Grimberg       basis_in = basis;
982004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
983004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
984004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
985004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
986004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
987004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
988004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
9890d0321e0SJeremy L Thompson       }
9900d0321e0SJeremy L Thompson     }
9910d0321e0SJeremy L Thompson   }
9920d0321e0SJeremy L Thompson 
9930d0321e0SJeremy L Thompson   // Determine active output basis
994b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
995b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
996b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9970d0321e0SJeremy L Thompson     CeedVector vec;
998b7453713SJeremy L Thompson 
999b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
10000d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
1001004e4986SSebastian Grimberg       CeedBasis    basis;
1002004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
1003b7453713SJeremy L Thompson 
1004004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
1005004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1006004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
1007004e4986SSebastian Grimberg       basis_out = basis;
1008004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1009004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1010004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1011004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
1012004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1013004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
1014004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
10150d0321e0SJeremy L Thompson       }
10160d0321e0SJeremy L Thompson     }
10170d0321e0SJeremy L Thompson   }
10180d0321e0SJeremy L Thompson 
10190d0321e0SJeremy L Thompson   // Operator data struct
10202b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
10212b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
10220d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
1023b7453713SJeremy L Thompson 
1024cbfe683aSSebastian Grimberg   // Basis matrices
1025004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1026004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1027004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1028004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
1029004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
1030004e4986SSebastian Grimberg   bool          has_eval_none    = false;
10310d0321e0SJeremy L Thompson 
10320d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
1033004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1034004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1035004e4986SSebastian Grimberg   if (has_eval_none) {
10360d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
1037b7453713SJeremy L Thompson 
1038004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
1039004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
1040b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
1041b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
1042004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
10430d0321e0SJeremy L Thompson   }
10440d0321e0SJeremy L Thompson 
1045004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
1046004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
1047004e4986SSebastian Grimberg     CeedFESpace fespace;
1048004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
10490d0321e0SJeremy L Thompson 
1050004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
1051004e4986SSebastian Grimberg     switch (fespace) {
1052004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
1053004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
1054004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
1055004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
10560d0321e0SJeremy L Thompson 
1057004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1058004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
10590d0321e0SJeremy L Thompson 
1060004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1061004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1062004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1063004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
1064004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
1065004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
1066004e4986SSebastian Grimberg         if (in) {
1067004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1068004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
1069004e4986SSebastian Grimberg         } else {
1070004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1071004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
1072004e4986SSebastian Grimberg         }
1073004e4986SSebastian Grimberg       } break;
1074004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
1075004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
1076004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
1077004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
1078004e4986SSebastian Grimberg 
1079004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1080004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
1081004e4986SSebastian Grimberg 
1082004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1083004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1084004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1085004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
1086004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
1087004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
1088004e4986SSebastian Grimberg         if (in) {
1089004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1090004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
1091004e4986SSebastian Grimberg         } else {
1092004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1093004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
1094004e4986SSebastian Grimberg         }
1095004e4986SSebastian Grimberg       } break;
1096004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
1097004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
1098004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
1099004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
1100004e4986SSebastian Grimberg 
1101004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1102004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
1103004e4986SSebastian Grimberg 
1104004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1105004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1106004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1107004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
1108004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
1109004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
1110004e4986SSebastian Grimberg         if (in) {
1111004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1112004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
1113004e4986SSebastian Grimberg         } else {
1114004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1115004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
1116004e4986SSebastian Grimberg         }
1117004e4986SSebastian Grimberg       } break;
1118004e4986SSebastian Grimberg     }
1119004e4986SSebastian Grimberg   }
1120004e4986SSebastian Grimberg 
1121004e4986SSebastian Grimberg   // Arrays of eval_modes
1122004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
1123004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
1124004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
1125004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
1126004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
1127004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
11280d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11290d0321e0SJeremy L Thompson }
11300d0321e0SJeremy L Thompson 
11310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1132cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
1133cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1134cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
1135cbfe683aSSebastian Grimberg   Ceed                ceed;
113622070f95SJeremy L Thompson   char               *diagonal_kernel_source;
113722070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
1138cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
1139cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
1140cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
1141cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
1142cbfe683aSSebastian Grimberg   CeedQFunction       qf;
1143cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
1144cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
1145cbfe683aSSebastian Grimberg 
1146cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1147cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1148cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1149cbfe683aSSebastian Grimberg 
1150cbfe683aSSebastian Grimberg   // Determine active input basis
1151cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1152cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1153cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1154cbfe683aSSebastian Grimberg     CeedVector vec;
1155cbfe683aSSebastian Grimberg 
1156cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1157cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1158cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1159cbfe683aSSebastian Grimberg 
1160cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1161cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1162cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1163cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1164cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1165cbfe683aSSebastian Grimberg       }
1166cbfe683aSSebastian Grimberg     }
1167cbfe683aSSebastian Grimberg   }
1168cbfe683aSSebastian Grimberg 
1169cbfe683aSSebastian Grimberg   // Determine active output basis
1170cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1171cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1172cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1173cbfe683aSSebastian Grimberg     CeedVector vec;
1174cbfe683aSSebastian Grimberg 
1175cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1176cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1177cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1178cbfe683aSSebastian Grimberg 
1179cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1180cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1181cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1182cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1183cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1184cbfe683aSSebastian Grimberg       }
1185cbfe683aSSebastian Grimberg     }
1186cbfe683aSSebastian Grimberg   }
1187cbfe683aSSebastian Grimberg 
1188cbfe683aSSebastian Grimberg   // Operator data struct
1189cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1190cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1191cbfe683aSSebastian Grimberg 
1192cbfe683aSSebastian Grimberg   // Assemble kernel
1193cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
1194cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
1195cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1196cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1197cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1198cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1199cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1200cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1201cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1202cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1203cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1204cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1205cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1206cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1207cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1208cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1209cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1210cbfe683aSSebastian Grimberg }
1211cbfe683aSSebastian Grimberg 
1212cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1213004e4986SSebastian Grimberg // Assemble Diagonal Core
12140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1215b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
12160d0321e0SJeremy L Thompson   Ceed                ceed;
1217cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1218b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
1219b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1220004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1221004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
12220d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
1223b7453713SJeremy L Thompson 
1224b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12252b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
12260d0321e0SJeremy L Thompson 
12270d0321e0SJeremy L Thompson   // Assemble QFunction
1228004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1229004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1230004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
12310d0321e0SJeremy L Thompson 
1232cbfe683aSSebastian Grimberg   // Setup
1233cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
1234cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1235cbfe683aSSebastian Grimberg 
1236cbfe683aSSebastian Grimberg   assert(diag != NULL);
1237cbfe683aSSebastian Grimberg 
1238cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1239cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1240cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1241cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
12429330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1243b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1244b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
12459330daecSnbeams 
1246cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
1247cbfe683aSSebastian Grimberg   }
12480d0321e0SJeremy L Thompson 
1249004e4986SSebastian Grimberg   // Restriction and diagonal vector
1250004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1251004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1252004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1253004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1254004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1255004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1256004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1257004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1258004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
12590d0321e0SJeremy L Thompson   }
1260004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1261004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1262b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
12630d0321e0SJeremy L Thompson 
126491db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1265004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1266004e4986SSebastian Grimberg   if (num_nodes > 0) {
12670d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1268b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
1269b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
12700d0321e0SJeremy L Thompson 
12710d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1272004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1273004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1274004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1275004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1276004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1277b7453713SJeremy L Thompson 
1278b7453713SJeremy L Thompson     if (is_point_block) {
1279004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
12800d0321e0SJeremy L Thompson     } else {
1281004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
12820d0321e0SJeremy L Thompson     }
12830d0321e0SJeremy L Thompson 
12840d0321e0SJeremy L Thompson     // Restore arrays
1285b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1286b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
128791db28b6SZach Atkins   }
12880d0321e0SJeremy L Thompson 
12890d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1290b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
12910d0321e0SJeremy L Thompson 
12920d0321e0SJeremy L Thompson   // Cleanup
1293b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
12940d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12950d0321e0SJeremy L Thompson }
12960d0321e0SJeremy L Thompson 
12970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12980d0321e0SJeremy L Thompson // Assemble Linear Diagonal
12990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13002b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
13012b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
13026aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13030d0321e0SJeremy L Thompson }
13040d0321e0SJeremy L Thompson 
13050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13060d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
13070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13082b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
13092b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
13106aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13110d0321e0SJeremy L Thompson }
13120d0321e0SJeremy L Thompson 
1313a835093fSnbeams //------------------------------------------------------------------------------
1314004e4986SSebastian Grimberg // Single Operator Assembly Setup
1315a835093fSnbeams //------------------------------------------------------------------------------
13169330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
1317a835093fSnbeams   Ceed                ceed;
131822070f95SJeremy L Thompson   char               *assembly_kernel_source;
131922070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1320004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
13213b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1322004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1323b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1324b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1325b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
1326b7453713SJeremy L Thompson   CeedQFunction       qf;
1327b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1328a835093fSnbeams   CeedOperator_Hip   *impl;
1329b7453713SJeremy L Thompson 
1330b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
13312b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1332a835093fSnbeams 
1333a835093fSnbeams   // Get intput and output fields
13342b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1335a835093fSnbeams 
1336a835093fSnbeams   // Determine active input basis eval mode
13372b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
13382b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1339a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1340a835093fSnbeams     CeedVector vec;
1341b7453713SJeremy L Thompson 
13422b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1343a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1344004e4986SSebastian Grimberg       CeedBasis    basis;
1345b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1346b7453713SJeremy L Thompson 
1347004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1348004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1349004e4986SSebastian Grimberg       basis_in = basis;
13502b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1351004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1352004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1353004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
13542b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1355004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1356004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1357004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1358004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1359004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1360004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1361a835093fSnbeams         }
1362004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1363a835093fSnbeams       }
1364a835093fSnbeams     }
1365a835093fSnbeams   }
1366a835093fSnbeams 
1367a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
13682b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1369a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1370a835093fSnbeams     CeedVector vec;
1371b7453713SJeremy L Thompson 
13722b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1373a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1374004e4986SSebastian Grimberg       CeedBasis    basis;
1375b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1376b7453713SJeremy L Thompson 
1377004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1378004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1379004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1380004e4986SSebastian Grimberg       basis_out = basis;
13812b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1382004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1383004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1384004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1385004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1386004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
13872b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1388004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1389004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1390004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1391004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1392004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1393004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1394004e4986SSebastian Grimberg         }
1395004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1396a835093fSnbeams       }
1397a835093fSnbeams     }
1398a835093fSnbeams   }
1399004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1400a835093fSnbeams 
14012b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1402a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1403004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1404004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1405004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1406004e4986SSebastian Grimberg 
1407004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1408004e4986SSebastian Grimberg 
1409004e4986SSebastian Grimberg   if (fallback) {
1410004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1411004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1412004e4986SSebastian Grimberg   }
1413a835093fSnbeams 
1414a835093fSnbeams   // Compile kernels
1415004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1416004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
14172b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
141823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
14192b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
142023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1421004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1422004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1423004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1424cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
14259330daecSnbeams                                   use_ceedsize_idx));
1426004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
14272b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
14282b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1429a835093fSnbeams 
1430004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1431004e4986SSebastian Grimberg   {
1432004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1433004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1434004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1435004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1436004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1437a835093fSnbeams 
1438004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1439004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1440004e4986SSebastian Grimberg     }
1441004e4986SSebastian Grimberg     if (has_eval_none) {
1442004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1443004e4986SSebastian 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;
1444004e4986SSebastian Grimberg     }
1445b7453713SJeremy L Thompson 
1446b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1447004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1448004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1449004e4986SSebastian Grimberg 
1450004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1451004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1452004e4986SSebastian Grimberg       if (q_comp > 1) {
1453004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1454004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1455004e4986SSebastian Grimberg       }
1456004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1457004e4986SSebastian Grimberg 
1458004e4986SSebastian 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),
1459004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1460004e4986SSebastian Grimberg     }
1461004e4986SSebastian Grimberg 
1462004e4986SSebastian Grimberg     if (identity) {
1463004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1464a835093fSnbeams     }
1465a835093fSnbeams   }
1466a835093fSnbeams 
1467004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1468004e4986SSebastian Grimberg   {
1469004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1470004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1471004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1472004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1473004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1474b7453713SJeremy L Thompson 
1475004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1476004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1477004e4986SSebastian Grimberg     }
1478004e4986SSebastian Grimberg     if (has_eval_none) {
1479004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1480004e4986SSebastian 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;
1481a835093fSnbeams     }
1482a835093fSnbeams 
1483b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1484004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1485004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1486004e4986SSebastian Grimberg 
1487004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1488004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1489004e4986SSebastian Grimberg       if (q_comp > 1) {
1490004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1491004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1492004e4986SSebastian Grimberg       }
1493004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1494004e4986SSebastian Grimberg 
1495004e4986SSebastian 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),
1496004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1497004e4986SSebastian Grimberg     }
1498004e4986SSebastian Grimberg 
1499004e4986SSebastian Grimberg     if (identity) {
1500004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1501a835093fSnbeams     }
1502a835093fSnbeams   }
1503a835093fSnbeams   return CEED_ERROR_SUCCESS;
1504a835093fSnbeams }
1505a835093fSnbeams 
1506a835093fSnbeams //------------------------------------------------------------------------------
1507cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1508cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1509cefa2673SJeremy L Thompson //
1510ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1511cefa2673SJeremy L Thompson // modes).
1512cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1513a835093fSnbeams //------------------------------------------------------------------------------
15142b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1515a835093fSnbeams   Ceed                ceed;
1516b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1517004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1518b7453713SJeremy L Thompson   CeedScalar         *values_array;
1519004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1520b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1521004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1522004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1523004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1524004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1525a835093fSnbeams   CeedOperator_Hip   *impl;
1526b7453713SJeremy L Thompson 
1527b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
15282b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1529a835093fSnbeams 
1530a835093fSnbeams   // Assemble QFunction
1531004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1532004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1533004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1534a835093fSnbeams 
15359330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
15369330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
15379330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1538004e4986SSebastian Grimberg 
15399330daecSnbeams   // Setup
1540004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1541004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1542004e4986SSebastian Grimberg 
1543004e4986SSebastian Grimberg   assert(asmb != NULL);
1544004e4986SSebastian Grimberg 
1545004e4986SSebastian Grimberg   // Assemble element operator
1546004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1547004e4986SSebastian Grimberg   values_array += offset;
1548004e4986SSebastian Grimberg 
1549004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1550004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1551004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1552004e4986SSebastian Grimberg 
1553004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1554004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1555004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1556004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1557004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1558004e4986SSebastian Grimberg   }
1559004e4986SSebastian Grimberg 
1560004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1561004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1562004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1563004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1564004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1565004e4986SSebastian Grimberg 
1566004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1567004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1568004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1569004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1570004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1571004e4986SSebastian Grimberg     }
1572004e4986SSebastian Grimberg   } else {
1573004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1574004e4986SSebastian Grimberg     orients_out      = orients_in;
1575004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
15769330daecSnbeams   }
15779330daecSnbeams 
1578a835093fSnbeams   // Compute B^T D B
1579004e4986SSebastian Grimberg   CeedInt shared_mem =
1580004e4986SSebastian 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)) *
1581004e4986SSebastian Grimberg       sizeof(CeedScalar);
1582004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1583004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1584004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1585b7453713SJeremy L Thompson 
15862b730f8bSJeremy L Thompson   CeedCallBackend(
1587004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1588a835093fSnbeams 
1589a835093fSnbeams   // Restore arrays
15902b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1591004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1592a835093fSnbeams 
1593a835093fSnbeams   // Cleanup
15942b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1595004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1596004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1597004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1598004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1599004e4986SSebastian Grimberg   }
1600004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1601004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1602004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1603004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1604004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1605004e4986SSebastian Grimberg     }
1606004e4986SSebastian Grimberg   }
1607a835093fSnbeams   return CEED_ERROR_SUCCESS;
1608a835093fSnbeams }
1609a835093fSnbeams 
1610a835093fSnbeams //------------------------------------------------------------------------------
161167d9480aSJeremy L Thompson // Assemble Linear QFunction AtPoints
161267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
161367d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
161467d9480aSJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
161567d9480aSJeremy L Thompson }
161667d9480aSJeremy L Thompson 
161767d9480aSJeremy L Thompson //------------------------------------------------------------------------------
161867d9480aSJeremy L Thompson // Assemble Linear Diagonal AtPoints
161967d9480aSJeremy L Thompson //------------------------------------------------------------------------------
162067d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1621111870feSJeremy L Thompson   CeedInt             max_num_points, *num_points, num_elem, num_input_fields, num_output_fields;
1622afe3bc8aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1623afe3bc8aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1624afe3bc8aSJeremy L Thompson   CeedQFunction       qf;
1625afe3bc8aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1626afe3bc8aSJeremy L Thompson   CeedOperator_Hip   *impl;
1627afe3bc8aSJeremy L Thompson 
1628afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1629afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1630afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1631afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1632afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1633afe3bc8aSJeremy L Thompson 
1634afe3bc8aSJeremy L Thompson   // Setup
1635afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
1636111870feSJeremy L Thompson   num_points     = impl->num_points;
1637afe3bc8aSJeremy L Thompson   max_num_points = impl->max_num_points;
1638afe3bc8aSJeremy L Thompson 
16398a213570SJeremy L Thompson   // Create separate output e-vecs
16408a213570SJeremy L Thompson   if (impl->has_shared_e_vecs) {
16418a213570SJeremy L Thompson     for (CeedInt i = 0; i < impl->num_outputs; i++) {
16428a213570SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
16438a213570SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[impl->num_inputs + i]));
16448a213570SJeremy L Thompson     }
16458a213570SJeremy 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,
16468a213570SJeremy L Thompson                                                 num_input_fields, num_output_fields, max_num_points, num_elem));
16478a213570SJeremy L Thompson   }
16488a213570SJeremy L Thompson   impl->has_shared_e_vecs = false;
16498a213570SJeremy L Thompson 
1650afe3bc8aSJeremy L Thompson   // Get point coordinates
1651afe3bc8aSJeremy L Thompson   if (!impl->point_coords_elem) {
1652afe3bc8aSJeremy L Thompson     CeedVector          point_coords = NULL;
1653afe3bc8aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1654afe3bc8aSJeremy L Thompson 
1655afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1656afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1657afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1658afe3bc8aSJeremy L Thompson   }
1659afe3bc8aSJeremy L Thompson 
1660*43e13feeSJeremy L Thompson   // Input Evecs and Restriction
1661*43e13feeSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1662*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputRestrict_Hip(op_input_fields[i], qf_input_fields[i], i, NULL, true, &e_data[i], impl, request));
1663*43e13feeSJeremy L Thompson   }
1664*43e13feeSJeremy L Thompson 
1665382e9c83SJeremy L Thompson   // Clear active input Qvecs
1666382e9c83SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1667382e9c83SJeremy L Thompson     CeedVector vec;
1668382e9c83SJeremy L Thompson 
1669382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1670382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1671382e9c83SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
1672382e9c83SJeremy L Thompson   }
1673382e9c83SJeremy L Thompson 
1674afe3bc8aSJeremy L Thompson   // Input basis apply if needed
1675*43e13feeSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1676*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(op_input_fields[i], qf_input_fields[i], i, num_elem, num_points, true, e_data[i], impl));
1677*43e13feeSJeremy L Thompson   }
1678afe3bc8aSJeremy L Thompson 
1679afe3bc8aSJeremy L Thompson   // Output pointers, as necessary
1680afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1681afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1682afe3bc8aSJeremy L Thompson 
1683afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1684afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1685afe3bc8aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1686afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1687afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1688afe3bc8aSJeremy L Thompson     }
1689afe3bc8aSJeremy L Thompson   }
1690afe3bc8aSJeremy L Thompson 
1691afe3bc8aSJeremy L Thompson   // Loop over active fields
1692afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1693382e9c83SJeremy L Thompson     bool                is_active_at_points = true;
1694382e9c83SJeremy L Thompson     CeedInt             elem_size = 1, num_comp_active = 1, e_vec_size = 0;
1695382e9c83SJeremy L Thompson     CeedRestrictionType rstr_type;
1696382e9c83SJeremy L Thompson     CeedVector          vec;
1697382e9c83SJeremy L Thompson     CeedElemRestriction elem_rstr;
1698382e9c83SJeremy L Thompson 
1699382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1700382e9c83SJeremy L Thompson     // -- Skip non-active input
1701382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1702382e9c83SJeremy L Thompson 
1703382e9c83SJeremy L Thompson     // -- Get active restriction type
1704382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1705382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1706382e9c83SJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1707382e9c83SJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1708382e9c83SJeremy L Thompson     else elem_size = max_num_points;
1709382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1710382e9c83SJeremy L Thompson 
1711382e9c83SJeremy L Thompson     e_vec_size = elem_size * num_comp_active;
1712382e9c83SJeremy L Thompson     for (CeedInt s = 0; s < e_vec_size; s++) {
1713afe3bc8aSJeremy L Thompson       bool         is_active_input = false;
1714afe3bc8aSJeremy L Thompson       CeedEvalMode eval_mode;
1715afe3bc8aSJeremy L Thompson       CeedVector   vec;
1716afe3bc8aSJeremy L Thompson       CeedBasis    basis;
1717afe3bc8aSJeremy L Thompson 
1718afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1719afe3bc8aSJeremy L Thompson       // Skip non-active input
1720afe3bc8aSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1721afe3bc8aSJeremy L Thompson       if (!is_active_input) continue;
1722afe3bc8aSJeremy L Thompson 
1723afe3bc8aSJeremy L Thompson       // Update unit vector
172413062808SJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1725afe3bc8aSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1726afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1727afe3bc8aSJeremy L Thompson 
1728afe3bc8aSJeremy L Thompson       // Basis action
1729afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1730afe3bc8aSJeremy L Thompson       switch (eval_mode) {
1731afe3bc8aSJeremy L Thompson         case CEED_EVAL_NONE:
1732afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1733afe3bc8aSJeremy L Thompson           break;
1734afe3bc8aSJeremy L Thompson         case CEED_EVAL_INTERP:
1735afe3bc8aSJeremy L Thompson         case CEED_EVAL_GRAD:
1736afe3bc8aSJeremy L Thompson         case CEED_EVAL_DIV:
1737afe3bc8aSJeremy L Thompson         case CEED_EVAL_CURL:
1738afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1739afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1740afe3bc8aSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1741afe3bc8aSJeremy L Thompson           break;
1742afe3bc8aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1743afe3bc8aSJeremy L Thompson           break;  // No action
1744afe3bc8aSJeremy L Thompson       }
1745afe3bc8aSJeremy L Thompson 
1746afe3bc8aSJeremy L Thompson       // Q function
1747afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1748afe3bc8aSJeremy L Thompson 
1749afe3bc8aSJeremy L Thompson       // Output basis apply if needed
1750382e9c83SJeremy L Thompson       for (CeedInt j = 0; j < num_output_fields; j++) {
1751afe3bc8aSJeremy L Thompson         bool                is_active_output = false;
1752382e9c83SJeremy L Thompson         CeedInt             elem_size        = 0;
1753382e9c83SJeremy L Thompson         CeedRestrictionType rstr_type;
1754afe3bc8aSJeremy L Thompson         CeedEvalMode        eval_mode;
1755afe3bc8aSJeremy L Thompson         CeedVector          vec;
1756afe3bc8aSJeremy L Thompson         CeedElemRestriction elem_rstr;
1757afe3bc8aSJeremy L Thompson         CeedBasis           basis;
1758afe3bc8aSJeremy L Thompson 
1759382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec));
1760382e9c83SJeremy L Thompson         // ---- Skip non-active output
1761afe3bc8aSJeremy L Thompson         is_active_output = vec == CEED_VECTOR_ACTIVE;
1762afe3bc8aSJeremy L Thompson         if (!is_active_output) continue;
1763afe3bc8aSJeremy L Thompson 
1764382e9c83SJeremy L Thompson         // ---- Check if elem size matches
1765382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1766382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1767382e9c83SJeremy L Thompson         if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue;
1768382e9c83SJeremy L Thompson         if (rstr_type == CEED_RESTRICTION_POINTS) {
1769382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size));
1770382e9c83SJeremy L Thompson         } else {
1771382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1772382e9c83SJeremy L Thompson         }
1773382e9c83SJeremy L Thompson         {
1774382e9c83SJeremy L Thompson           CeedInt num_comp = 0;
1775382e9c83SJeremy L Thompson 
1776382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1777382e9c83SJeremy L Thompson           if (e_vec_size != num_comp * elem_size) continue;
1778382e9c83SJeremy L Thompson         }
1779382e9c83SJeremy L Thompson 
1780afe3bc8aSJeremy L Thompson         // Basis action
1781382e9c83SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
1782afe3bc8aSJeremy L Thompson         switch (eval_mode) {
1783afe3bc8aSJeremy L Thompson           case CEED_EVAL_NONE:
1784382e9c83SJeremy L Thompson             CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields]));
1785afe3bc8aSJeremy L Thompson             break;
1786afe3bc8aSJeremy L Thompson           case CEED_EVAL_INTERP:
1787afe3bc8aSJeremy L Thompson           case CEED_EVAL_GRAD:
1788afe3bc8aSJeremy L Thompson           case CEED_EVAL_DIV:
1789afe3bc8aSJeremy L Thompson           case CEED_EVAL_CURL:
1790382e9c83SJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis));
1791382e9c83SJeremy L Thompson             CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
1792382e9c83SJeremy L Thompson                                                    impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs]));
1793afe3bc8aSJeremy L Thompson             break;
1794afe3bc8aSJeremy L Thompson           // LCOV_EXCL_START
1795afe3bc8aSJeremy L Thompson           case CEED_EVAL_WEIGHT: {
1796afe3bc8aSJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1797afe3bc8aSJeremy L Thompson             // LCOV_EXCL_STOP
1798afe3bc8aSJeremy L Thompson           }
1799afe3bc8aSJeremy L Thompson         }
1800afe3bc8aSJeremy L Thompson 
1801afe3bc8aSJeremy L Thompson         // Mask output e-vec
1802382e9c83SJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs]));
1803afe3bc8aSJeremy L Thompson 
1804afe3bc8aSJeremy L Thompson         // Restrict
1805382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1806382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request));
1807afe3bc8aSJeremy L Thompson 
1808afe3bc8aSJeremy L Thompson         // Reset q_vec for
1809afe3bc8aSJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) {
1810382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields]));
1811382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields]));
1812afe3bc8aSJeremy L Thompson         }
1813afe3bc8aSJeremy L Thompson       }
1814382e9c83SJeremy L Thompson 
1815382e9c83SJeremy L Thompson       // Reset vec
181613062808SJeremy L Thompson       if (s == e_vec_size - 1 && i != num_input_fields - 1) CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
181786e10729SJeremy L Thompson     }
1818afe3bc8aSJeremy L Thompson   }
1819afe3bc8aSJeremy L Thompson 
1820afe3bc8aSJeremy L Thompson   // Restore CEED_EVAL_NONE
1821afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1822afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1823afe3bc8aSJeremy L Thompson 
1824afe3bc8aSJeremy L Thompson     // Get eval_mode
1825afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1826afe3bc8aSJeremy L Thompson 
1827afe3bc8aSJeremy L Thompson     // Restore evec
1828afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1829afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1830afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1831afe3bc8aSJeremy L Thompson     }
1832afe3bc8aSJeremy L Thompson   }
1833afe3bc8aSJeremy L Thompson 
1834afe3bc8aSJeremy L Thompson   // Restore input arrays
1835*43e13feeSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1836*43e13feeSJeremy L Thompson     CeedCallBackend(CeedOperatorInputRestore_Hip(op_input_fields[i], qf_input_fields[i], i, true, &e_data[i], impl));
1837*43e13feeSJeremy L Thompson   }
1838afe3bc8aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
183967d9480aSJeremy L Thompson }
184067d9480aSJeremy L Thompson 
184167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
18420d0321e0SJeremy L Thompson // Create operator
18430d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
18440d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
18450d0321e0SJeremy L Thompson   Ceed              ceed;
18460d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
18470d0321e0SJeremy L Thompson 
1848b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
18492b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
18502b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
18512b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
18522b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
18532b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
18542b730f8bSJeremy L Thompson   CeedCallBackend(
18552b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
18562b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
18572b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
18582b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
18590d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18600d0321e0SJeremy L Thompson }
18610d0321e0SJeremy L Thompson 
18620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
186367d9480aSJeremy L Thompson // Create operator AtPoints
186467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
186567d9480aSJeremy L Thompson int CeedOperatorCreateAtPoints_Hip(CeedOperator op) {
186667d9480aSJeremy L Thompson   Ceed              ceed;
186767d9480aSJeremy L Thompson   CeedOperator_Hip *impl;
186867d9480aSJeremy L Thompson 
186967d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
187067d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
187167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
187267d9480aSJeremy L Thompson 
187367d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Hip));
187467d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip));
187567d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Hip));
187667d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
187767d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
187867d9480aSJeremy L Thompson }
187967d9480aSJeremy L Thompson 
188067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
1881