xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-operator.c (revision 111870fe44216042e3619edd402b3642abc96e10)
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
29*111870feSJeremy 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 //------------------------------------------------------------------------------
3190d0321e0SJeremy L Thompson // Setup Operator Inputs
3200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
321b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
322b7453713SJeremy L Thompson                                               CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
323b7453713SJeremy L Thompson                                               CeedOperator_Hip *impl, CeedRequest *request) {
324b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
325004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
3260d0321e0SJeremy L Thompson     CeedVector          vec;
327b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
3280d0321e0SJeremy L Thompson 
3290d0321e0SJeremy L Thompson     // Get input vector
330b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3310d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
332b7453713SJeremy L Thompson       if (skip_active) continue;
333b7453713SJeremy L Thompson       else vec = in_vec;
3340d0321e0SJeremy L Thompson     }
3350d0321e0SJeremy L Thompson 
336004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
337004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3380d0321e0SJeremy L Thompson     } else {
3390d0321e0SJeremy L Thompson       // Get input vector
340b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3410d0321e0SJeremy L Thompson       // Get input element restriction
342b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
343b7453713SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
3440d0321e0SJeremy L Thompson       // Restrict, if necessary
345b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {
3460d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
347b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3480d0321e0SJeremy L Thompson       } else {
349c1222711SJeremy L Thompson         uint64_t state;
350c1222711SJeremy L Thompson 
351c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
35241655a23SJeremy L Thompson         if ((state != impl->input_states[i] || vec == in_vec) && !impl->skip_rstr_in[i]) {
353b7453713SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
354c1222711SJeremy L Thompson         }
3553aab95c0SJeremy L Thompson         impl->input_states[i] = state;
3560d0321e0SJeremy L Thompson         // Get evec
357b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3580d0321e0SJeremy L Thompson       }
3590d0321e0SJeremy L Thompson     }
3600d0321e0SJeremy L Thompson   }
3610d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3620d0321e0SJeremy L Thompson }
3630d0321e0SJeremy L Thompson 
3640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3650d0321e0SJeremy L Thompson // Input Basis Action
3660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
367b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
368b7453713SJeremy L Thompson                                              CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
3692b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
370b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
371b7453713SJeremy L Thompson     CeedInt             elem_size, size;
372004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
373b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
3740d0321e0SJeremy L Thompson     CeedBasis           basis;
3750d0321e0SJeremy L Thompson 
3760d0321e0SJeremy L Thompson     // Skip active input
377b7453713SJeremy L Thompson     if (skip_active) {
3780d0321e0SJeremy L Thompson       CeedVector vec;
379b7453713SJeremy L Thompson 
380b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3812b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3820d0321e0SJeremy L Thompson     }
383004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
384b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
385b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
386004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
387b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3880d0321e0SJeremy L Thompson     // Basis action
389004e4986SSebastian Grimberg     switch (eval_mode) {
3900d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
391b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
3920d0321e0SJeremy L Thompson         break;
3930d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3940d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
395004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
396004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
397b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
398004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3990d0321e0SJeremy L Thompson         break;
4000d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
4010d0321e0SJeremy L Thompson         break;  // No action
4020d0321e0SJeremy L Thompson     }
4030d0321e0SJeremy L Thompson   }
4040d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4050d0321e0SJeremy L Thompson }
4060d0321e0SJeremy L Thompson 
4070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4080d0321e0SJeremy L Thompson // Restore Input Vectors
4090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
410b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
411b7453713SJeremy L Thompson                                                 const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
412b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
413004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
4140d0321e0SJeremy L Thompson     CeedVector   vec;
415004e4986SSebastian Grimberg 
4160d0321e0SJeremy L Thompson     // Skip active input
417b7453713SJeremy L Thompson     if (skip_active) {
418b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4192b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
4200d0321e0SJeremy L Thompson     }
421004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
422004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
4230d0321e0SJeremy L Thompson     } else {
424b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
425b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
426b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
4270d0321e0SJeremy L Thompson       } else {
428b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
4290d0321e0SJeremy L Thompson       }
4300d0321e0SJeremy L Thompson     }
4310d0321e0SJeremy L Thompson   }
4320d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4330d0321e0SJeremy L Thompson }
4340d0321e0SJeremy L Thompson 
4350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4360d0321e0SJeremy L Thompson // Apply and add to output
4370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
438b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
439b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
440b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
441b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
4420d0321e0SJeremy L Thompson   CeedQFunction       qf;
443b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
444b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
445b7453713SJeremy L Thompson 
446b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4472b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4482b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
449b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
450b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
451b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4520d0321e0SJeremy L Thompson 
4530d0321e0SJeremy L Thompson   // Setup
4542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
4550d0321e0SJeremy L Thompson 
4560d0321e0SJeremy L Thompson   // Input Evecs and Restriction
457b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
4580d0321e0SJeremy L Thompson 
4590d0321e0SJeremy L Thompson   // Input basis apply if needed
460b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
4610d0321e0SJeremy L Thompson 
4620d0321e0SJeremy L Thompson   // Output pointers, as necessary
463b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
464004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
465b7453713SJeremy L Thompson 
466004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
467004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
4680d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
469b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
470b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
4710d0321e0SJeremy L Thompson     }
4720d0321e0SJeremy L Thompson   }
4730d0321e0SJeremy L Thompson 
4740d0321e0SJeremy L Thompson   // Q function
475b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
4760d0321e0SJeremy L Thompson 
47741655a23SJeremy L Thompson   // Restore input arrays
47841655a23SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
47941655a23SJeremy L Thompson 
4800d0321e0SJeremy L Thompson   // Output basis apply if needed
481b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
482004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
483b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
484b7453713SJeremy L Thompson     CeedBasis           basis;
485b7453713SJeremy L Thompson 
486004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
487b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
488b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
489004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
490b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4910d0321e0SJeremy L Thompson     // Basis action
492004e4986SSebastian Grimberg     switch (eval_mode) {
4930d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
494004e4986SSebastian Grimberg         break;  // No action
4950d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4960d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
497004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
498004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
499b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
500f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
501f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAdd(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
502f8a0df59SJeremy L Thompson         } else {
503004e4986SSebastian Grimberg           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
504f8a0df59SJeremy L Thompson         }
5050d0321e0SJeremy L Thompson         break;
5060d0321e0SJeremy L Thompson       // LCOV_EXCL_START
5070d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
5086e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
5090d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
5100d0321e0SJeremy L Thompson       }
5110d0321e0SJeremy L Thompson     }
512004e4986SSebastian Grimberg   }
5130d0321e0SJeremy L Thompson 
5140d0321e0SJeremy L Thompson   // Output restriction
515b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
516004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
517b7453713SJeremy L Thompson     CeedVector          vec;
518b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
519b7453713SJeremy L Thompson 
5200d0321e0SJeremy L Thompson     // Restore evec
521004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
522004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
523b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
5240d0321e0SJeremy L Thompson     }
525f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
5260d0321e0SJeremy L Thompson     // Get output vector
527b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5280d0321e0SJeremy L Thompson     // Restrict
529b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
5300d0321e0SJeremy L Thompson     // Active
531b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
5320d0321e0SJeremy L Thompson 
533b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
5340d0321e0SJeremy L Thompson   }
5350d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5360d0321e0SJeremy L Thompson }
5370d0321e0SJeremy L Thompson 
5380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
53967d9480aSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
54067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
54167d9480aSJeremy L Thompson static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) {
54267d9480aSJeremy L Thompson   Ceed                ceed;
54367d9480aSJeremy L Thompson   bool                is_setup_done;
54467d9480aSJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
54567d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
54667d9480aSJeremy L Thompson   CeedQFunction       qf;
54767d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
54867d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
54967d9480aSJeremy L Thompson 
55067d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
55167d9480aSJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
55267d9480aSJeremy L Thompson 
55367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
55467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
55567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
55667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
55767d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
55867d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
55967d9480aSJeremy L Thompson   {
560*111870feSJeremy L Thompson     CeedElemRestriction rstr_points = NULL;
56167d9480aSJeremy L Thompson 
562*111870feSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
563*111870feSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points));
564*111870feSJeremy L Thompson     CeedCallBackend(CeedCalloc(num_elem, &impl->num_points));
565*111870feSJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
566*111870feSJeremy L Thompson       CeedInt num_points_elem;
567*111870feSJeremy L Thompson 
568*111870feSJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumPointsInElement(rstr_points, e, &num_points_elem));
569*111870feSJeremy L Thompson       impl->num_points[e] = num_points_elem;
570*111870feSJeremy L Thompson     }
57167d9480aSJeremy L Thompson   }
57267d9480aSJeremy L Thompson   impl->max_num_points = max_num_points;
57367d9480aSJeremy L Thompson 
57467d9480aSJeremy L Thompson   // Allocate
57567d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
5763aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
577f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
578f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
57967d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
58067d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
58167d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
58267d9480aSJeremy L Thompson   impl->num_inputs  = num_input_fields;
58367d9480aSJeremy L Thompson   impl->num_outputs = num_output_fields;
58467d9480aSJeremy L Thompson 
5858a213570SJeremy L Thompson   // Set up infield and outfield e-vecs and q-vecs
58667d9480aSJeremy L Thompson   // Infields
587f8a0df59SJeremy 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,
5883aab95c0SJeremy L Thompson                                               max_num_points, num_elem));
58967d9480aSJeremy L Thompson   // Outfields
590f8a0df59SJeremy 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,
591f8a0df59SJeremy L Thompson                                               num_input_fields, num_output_fields, max_num_points, num_elem));
59267d9480aSJeremy L Thompson 
5938a213570SJeremy L Thompson   // Reuse active e-vecs where able
5948a213570SJeremy L Thompson   {
5958a213570SJeremy L Thompson     CeedInt              num_used  = 0;
5968a213570SJeremy L Thompson     CeedElemRestriction *rstr_used = NULL;
5978a213570SJeremy L Thompson 
5988a213570SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
5998a213570SJeremy L Thompson       bool                is_used = false;
6008a213570SJeremy L Thompson       CeedVector          vec_i;
6018a213570SJeremy L Thompson       CeedElemRestriction rstr_i;
6028a213570SJeremy L Thompson 
6038a213570SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
6048a213570SJeremy L Thompson       if (vec_i != CEED_VECTOR_ACTIVE) continue;
6058a213570SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
6068a213570SJeremy L Thompson       for (CeedInt j = 0; j < num_used; j++) {
6078a213570SJeremy L Thompson         if (rstr_i == rstr_used[i]) is_used = true;
6088a213570SJeremy L Thompson       }
6098a213570SJeremy L Thompson       if (is_used) continue;
6108a213570SJeremy L Thompson       num_used++;
6118a213570SJeremy L Thompson       if (num_used == 1) CeedCallBackend(CeedCalloc(num_used, &rstr_used));
6128a213570SJeremy L Thompson       else CeedCallBackend(CeedRealloc(num_used, &rstr_used));
6138a213570SJeremy L Thompson       rstr_used[num_used - 1] = rstr_i;
6148a213570SJeremy L Thompson       for (CeedInt j = num_output_fields - 1; j >= 0; j--) {
6158a213570SJeremy L Thompson         CeedEvalMode        eval_mode;
6168a213570SJeremy L Thompson         CeedVector          vec_j;
6178a213570SJeremy L Thompson         CeedElemRestriction rstr_j;
6188a213570SJeremy L Thompson 
6198a213570SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec_j));
6208a213570SJeremy L Thompson         if (vec_j != CEED_VECTOR_ACTIVE) continue;
6218a213570SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
6228a213570SJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) continue;
6238a213570SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &rstr_j));
6248a213570SJeremy L Thompson         if (rstr_i == rstr_j) {
6258a213570SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(impl->e_vecs[i], &impl->e_vecs[j + impl->num_inputs]));
6268a213570SJeremy L Thompson         }
6278a213570SJeremy L Thompson       }
6288a213570SJeremy L Thompson     }
6298a213570SJeremy L Thompson     CeedCallBackend(CeedFree(&rstr_used));
6308a213570SJeremy L Thompson   }
6318a213570SJeremy L Thompson   impl->has_shared_e_vecs = true;
63267d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
63367d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
63467d9480aSJeremy L Thompson }
63567d9480aSJeremy L Thompson 
63667d9480aSJeremy L Thompson //------------------------------------------------------------------------------
63767d9480aSJeremy L Thompson // Input Basis Action AtPoints
63867d9480aSJeremy L Thompson //------------------------------------------------------------------------------
63967d9480aSJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Hip(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields,
64067d9480aSJeremy L Thompson                                                      CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active,
64167d9480aSJeremy L Thompson                                                      CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
64267d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
64367d9480aSJeremy L Thompson     CeedInt             elem_size, size;
64467d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
64567d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
64667d9480aSJeremy L Thompson     CeedBasis           basis;
64767d9480aSJeremy L Thompson 
64867d9480aSJeremy L Thompson     // Skip active input
64967d9480aSJeremy L Thompson     if (skip_active) {
65067d9480aSJeremy L Thompson       CeedVector vec;
65167d9480aSJeremy L Thompson 
65267d9480aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
65367d9480aSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
65467d9480aSJeremy L Thompson     }
65567d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
65667d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
65767d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
65867d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
65967d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
66067d9480aSJeremy L Thompson     // Basis action
66167d9480aSJeremy L Thompson     switch (eval_mode) {
66267d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
66367d9480aSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
66467d9480aSJeremy L Thompson         break;
66567d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
66667d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
66767d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
66867d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
66967d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
67067d9480aSJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
67167d9480aSJeremy L Thompson                                                impl->q_vecs_in[i]));
67267d9480aSJeremy L Thompson         break;
67367d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT:
67467d9480aSJeremy L Thompson         break;  // No action
67567d9480aSJeremy L Thompson     }
67667d9480aSJeremy L Thompson   }
67767d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
67867d9480aSJeremy L Thompson }
67967d9480aSJeremy L Thompson 
68067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
68167d9480aSJeremy L Thompson // Apply and add to output AtPoints
68267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
68367d9480aSJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
684*111870feSJeremy L Thompson   CeedInt             max_num_points, *num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
68567d9480aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
68667d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
68767d9480aSJeremy L Thompson   CeedQFunction       qf;
68867d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
68967d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
69067d9480aSJeremy L Thompson 
69167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
69267d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
69367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
69467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
69567d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
69667d9480aSJeremy L Thompson 
69767d9480aSJeremy L Thompson   // Setup
69867d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
699*111870feSJeremy L Thompson   num_points     = impl->num_points;
70067d9480aSJeremy L Thompson   max_num_points = impl->max_num_points;
70167d9480aSJeremy L Thompson 
70267d9480aSJeremy L Thompson   // Input Evecs and Restriction
70367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
70467d9480aSJeremy L Thompson 
70567d9480aSJeremy L Thompson   // Get point coordinates
70667d9480aSJeremy L Thompson   if (!impl->point_coords_elem) {
70767d9480aSJeremy L Thompson     CeedVector          point_coords = NULL;
70867d9480aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
70967d9480aSJeremy L Thompson 
71067d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
71167d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
71267d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
71367d9480aSJeremy L Thompson   }
71467d9480aSJeremy L Thompson 
71567d9480aSJeremy L Thompson   // Input basis apply if needed
71667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
71767d9480aSJeremy L Thompson 
71867d9480aSJeremy L Thompson   // Output pointers, as necessary
71967d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
72067d9480aSJeremy L Thompson     CeedEvalMode eval_mode;
72167d9480aSJeremy L Thompson 
72267d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
72367d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
72467d9480aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
72567d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
72667d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
72767d9480aSJeremy L Thompson     }
72867d9480aSJeremy L Thompson   }
72967d9480aSJeremy L Thompson 
73067d9480aSJeremy L Thompson   // Q function
73167d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
73267d9480aSJeremy L Thompson 
7338a213570SJeremy L Thompson   // Restore input arrays
7348a213570SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
7358a213570SJeremy L Thompson 
73667d9480aSJeremy L Thompson   // Output basis apply if needed
73767d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
73867d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
73967d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
74067d9480aSJeremy L Thompson     CeedBasis           basis;
74167d9480aSJeremy L Thompson 
74267d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
74367d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
74467d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
74567d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
74667d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
74767d9480aSJeremy L Thompson     // Basis action
74867d9480aSJeremy L Thompson     switch (eval_mode) {
74967d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
75067d9480aSJeremy L Thompson         break;  // No action
75167d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
75267d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
75367d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
75467d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
75567d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
756f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
757f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAddAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
758f8a0df59SJeremy L Thompson                                                     impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
759f8a0df59SJeremy L Thompson         } else {
76067d9480aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
76167d9480aSJeremy L Thompson                                                  impl->e_vecs[i + impl->num_inputs]));
762f8a0df59SJeremy L Thompson         }
76367d9480aSJeremy L Thompson         break;
76467d9480aSJeremy L Thompson       // LCOV_EXCL_START
76567d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT: {
76667d9480aSJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
76767d9480aSJeremy L Thompson         // LCOV_EXCL_STOP
76867d9480aSJeremy L Thompson       }
76967d9480aSJeremy L Thompson     }
77067d9480aSJeremy L Thompson   }
77167d9480aSJeremy L Thompson 
77267d9480aSJeremy L Thompson   // Output restriction
77367d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
77467d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
77567d9480aSJeremy L Thompson     CeedVector          vec;
77667d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
77767d9480aSJeremy L Thompson 
77867d9480aSJeremy L Thompson     // Restore evec
77967d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
78067d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
78167d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
78267d9480aSJeremy L Thompson     }
783f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
78467d9480aSJeremy L Thompson     // Get output vector
78567d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
78667d9480aSJeremy L Thompson     // Restrict
78767d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
78867d9480aSJeremy L Thompson     // Active
78967d9480aSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
79067d9480aSJeremy L Thompson 
79167d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
79267d9480aSJeremy L Thompson   }
79367d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
79467d9480aSJeremy L Thompson }
79567d9480aSJeremy L Thompson 
79667d9480aSJeremy L Thompson //------------------------------------------------------------------------------
797004e4986SSebastian Grimberg // Linear QFunction Assembly Core
7980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7992b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
8000d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
801b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
802b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
803b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
804004e4986SSebastian Grimberg   CeedVector         *active_inputs;
805b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
806b7453713SJeremy L Thompson   CeedQFunction       qf;
807b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
808b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
809b7453713SJeremy L Thompson 
8102b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
811b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
812e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
813e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
814b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
815004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
816b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
817b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
818004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
819004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
8200d0321e0SJeremy L Thompson 
8210d0321e0SJeremy L Thompson   // Setup
8222b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
8230d0321e0SJeremy L Thompson 
8240d0321e0SJeremy L Thompson   // Input Evecs and Restriction
825b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
8260d0321e0SJeremy L Thompson 
8270d0321e0SJeremy L Thompson   // Count number of active input fields
828b7453713SJeremy L Thompson   if (!num_active_in) {
829b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
830b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
831b7453713SJeremy L Thompson       CeedVector  vec;
832b7453713SJeremy L Thompson 
8330d0321e0SJeremy L Thompson       // Get input vector
834b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
8350d0321e0SJeremy L Thompson       // Check if active input
8360d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
837b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
838b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
839b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
840004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
8410d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
842004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
843004e4986SSebastian Grimberg 
844004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
845b7453713SJeremy L Thompson           CeedCallBackend(
846004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
8470d0321e0SJeremy L Thompson         }
848b7453713SJeremy L Thompson         num_active_in += size;
849b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
8500d0321e0SJeremy L Thompson       }
8510d0321e0SJeremy L Thompson     }
852b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
853004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
8540d0321e0SJeremy L Thompson   }
8550d0321e0SJeremy L Thompson 
8560d0321e0SJeremy L Thompson   // Count number of active output fields
857b7453713SJeremy L Thompson   if (!num_active_out) {
858b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
859b7453713SJeremy L Thompson       CeedVector vec;
860b7453713SJeremy L Thompson 
8610d0321e0SJeremy L Thompson       // Get output vector
862b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
8630d0321e0SJeremy L Thompson       // Check if active output
8640d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
865b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
866b7453713SJeremy L Thompson         num_active_out += size;
8670d0321e0SJeremy L Thompson       }
8680d0321e0SJeremy L Thompson     }
869b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
8700d0321e0SJeremy L Thompson   }
8710d0321e0SJeremy L Thompson 
8720d0321e0SJeremy L Thompson   // Check sizes
873b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
8740d0321e0SJeremy L Thompson 
8750d0321e0SJeremy L Thompson   // Build objects if needed
8760d0321e0SJeremy L Thompson   if (build_objects) {
877b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
878b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
879b7453713SJeremy L Thompson 
880004e4986SSebastian Grimberg     // Create output restriction
881b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
8820a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
8830a5597ceSJeremy L Thompson                                                      rstr));
8840d0321e0SJeremy L Thompson     // Create assembled vector
885b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
8860d0321e0SJeremy L Thompson   }
8872b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
888b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
8890d0321e0SJeremy L Thompson 
8900d0321e0SJeremy L Thompson   // Input basis apply
891b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
8920d0321e0SJeremy L Thompson 
8930d0321e0SJeremy L Thompson   // Assemble QFunction
894b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
8950d0321e0SJeremy L Thompson     // Set Inputs
896004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
897b7453713SJeremy L Thompson     if (num_active_in > 1) {
898004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
8990d0321e0SJeremy L Thompson     }
9000d0321e0SJeremy L Thompson     // Set Outputs
901b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
902b7453713SJeremy L Thompson       CeedVector vec;
903b7453713SJeremy L Thompson 
9040d0321e0SJeremy L Thompson       // Get output vector
905b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
9060d0321e0SJeremy L Thompson       // Check if active output
9070d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
908b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
909b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
910b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
9110d0321e0SJeremy L Thompson       }
9120d0321e0SJeremy L Thompson     }
9130d0321e0SJeremy L Thompson     // Apply QFunction
914b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
9150d0321e0SJeremy L Thompson   }
9160d0321e0SJeremy L Thompson 
9178a213570SJeremy L Thompson   // Un-set output q-vecs to prevent accidental overwrite of Assembled
918b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
919b7453713SJeremy L Thompson     CeedVector vec;
920b7453713SJeremy L Thompson 
9210d0321e0SJeremy L Thompson     // Get output vector
922b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
9230d0321e0SJeremy L Thompson     // Check if active output
9240d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
925b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
9260d0321e0SJeremy L Thompson     }
9270d0321e0SJeremy L Thompson   }
9280d0321e0SJeremy L Thompson 
9290d0321e0SJeremy L Thompson   // Restore input arrays
930b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
9310d0321e0SJeremy L Thompson 
9320d0321e0SJeremy L Thompson   // Restore output
933b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
9340d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9350d0321e0SJeremy L Thompson }
9360d0321e0SJeremy L Thompson 
9370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9380d0321e0SJeremy L Thompson // Assemble Linear QFunction
9390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9402b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
9412b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
9420d0321e0SJeremy L Thompson }
9430d0321e0SJeremy L Thompson 
9440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
945b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
9460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9472b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
9482b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
9490d0321e0SJeremy L Thompson }
9500d0321e0SJeremy L Thompson 
9510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
952004e4986SSebastian Grimberg // Assemble Diagonal Setup
9530d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
954cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
9550d0321e0SJeremy L Thompson   Ceed                ceed;
956004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
957cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
958004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
959b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
960b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
9610d0321e0SJeremy L Thompson   CeedQFunction       qf;
962b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
963b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
964b7453713SJeremy L Thompson 
965b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
9662b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
967b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
9680d0321e0SJeremy L Thompson 
9690d0321e0SJeremy L Thompson   // Determine active input basis
970b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
971b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
972b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9730d0321e0SJeremy L Thompson     CeedVector vec;
974b7453713SJeremy L Thompson 
975b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
9760d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
977004e4986SSebastian Grimberg       CeedBasis    basis;
978004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
979b7453713SJeremy L Thompson 
980004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
981004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
982004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
983004e4986SSebastian Grimberg       basis_in = basis;
984004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
985004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
986004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
987004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
988004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
989004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
990004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
9910d0321e0SJeremy L Thompson       }
9920d0321e0SJeremy L Thompson     }
9930d0321e0SJeremy L Thompson   }
9940d0321e0SJeremy L Thompson 
9950d0321e0SJeremy L Thompson   // Determine active output basis
996b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
997b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
998b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9990d0321e0SJeremy L Thompson     CeedVector vec;
1000b7453713SJeremy L Thompson 
1001b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
10020d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
1003004e4986SSebastian Grimberg       CeedBasis    basis;
1004004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
1005b7453713SJeremy L Thompson 
1006004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
1007004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1008004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
1009004e4986SSebastian Grimberg       basis_out = basis;
1010004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1011004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1012004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1013004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
1014004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1015004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
1016004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
10170d0321e0SJeremy L Thompson       }
10180d0321e0SJeremy L Thompson     }
10190d0321e0SJeremy L Thompson   }
10200d0321e0SJeremy L Thompson 
10210d0321e0SJeremy L Thompson   // Operator data struct
10222b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
10232b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
10240d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
1025b7453713SJeremy L Thompson 
1026cbfe683aSSebastian Grimberg   // Basis matrices
1027004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1028004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1029004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1030004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
1031004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
1032004e4986SSebastian Grimberg   bool          has_eval_none    = false;
10330d0321e0SJeremy L Thompson 
10340d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
1035004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1036004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1037004e4986SSebastian Grimberg   if (has_eval_none) {
10380d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
1039b7453713SJeremy L Thompson 
1040004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
1041004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
1042b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
1043b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
1044004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
10450d0321e0SJeremy L Thompson   }
10460d0321e0SJeremy L Thompson 
1047004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
1048004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
1049004e4986SSebastian Grimberg     CeedFESpace fespace;
1050004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
10510d0321e0SJeremy L Thompson 
1052004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
1053004e4986SSebastian Grimberg     switch (fespace) {
1054004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
1055004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
1056004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
1057004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
10580d0321e0SJeremy L Thompson 
1059004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1060004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
10610d0321e0SJeremy L Thompson 
1062004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1063004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1064004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1065004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
1066004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
1067004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
1068004e4986SSebastian Grimberg         if (in) {
1069004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1070004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
1071004e4986SSebastian Grimberg         } else {
1072004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1073004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
1074004e4986SSebastian Grimberg         }
1075004e4986SSebastian Grimberg       } break;
1076004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
1077004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
1078004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
1079004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
1080004e4986SSebastian Grimberg 
1081004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1082004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
1083004e4986SSebastian Grimberg 
1084004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1085004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1086004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1087004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
1088004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
1089004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
1090004e4986SSebastian Grimberg         if (in) {
1091004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1092004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
1093004e4986SSebastian Grimberg         } else {
1094004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1095004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
1096004e4986SSebastian Grimberg         }
1097004e4986SSebastian Grimberg       } break;
1098004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
1099004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
1100004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
1101004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
1102004e4986SSebastian Grimberg 
1103004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1104004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
1105004e4986SSebastian Grimberg 
1106004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1107004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1108004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
1109004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
1110004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
1111004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
1112004e4986SSebastian Grimberg         if (in) {
1113004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1114004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
1115004e4986SSebastian Grimberg         } else {
1116004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1117004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
1118004e4986SSebastian Grimberg         }
1119004e4986SSebastian Grimberg       } break;
1120004e4986SSebastian Grimberg     }
1121004e4986SSebastian Grimberg   }
1122004e4986SSebastian Grimberg 
1123004e4986SSebastian Grimberg   // Arrays of eval_modes
1124004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
1125004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
1126004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
1127004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
1128004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
1129004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
11300d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11310d0321e0SJeremy L Thompson }
11320d0321e0SJeremy L Thompson 
11330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1134cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
1135cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1136cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
1137cbfe683aSSebastian Grimberg   Ceed                ceed;
113822070f95SJeremy L Thompson   char               *diagonal_kernel_source;
113922070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
1140cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
1141cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
1142cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
1143cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
1144cbfe683aSSebastian Grimberg   CeedQFunction       qf;
1145cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
1146cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
1147cbfe683aSSebastian Grimberg 
1148cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1149cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1150cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1151cbfe683aSSebastian Grimberg 
1152cbfe683aSSebastian Grimberg   // Determine active input basis
1153cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1154cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1155cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1156cbfe683aSSebastian Grimberg     CeedVector vec;
1157cbfe683aSSebastian Grimberg 
1158cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1159cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1160cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1161cbfe683aSSebastian Grimberg 
1162cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1163cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1164cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1165cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1166cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1167cbfe683aSSebastian Grimberg       }
1168cbfe683aSSebastian Grimberg     }
1169cbfe683aSSebastian Grimberg   }
1170cbfe683aSSebastian Grimberg 
1171cbfe683aSSebastian Grimberg   // Determine active output basis
1172cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1173cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1174cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1175cbfe683aSSebastian Grimberg     CeedVector vec;
1176cbfe683aSSebastian Grimberg 
1177cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1178cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1179cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1180cbfe683aSSebastian Grimberg 
1181cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1182cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1183cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1184cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1185cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1186cbfe683aSSebastian Grimberg       }
1187cbfe683aSSebastian Grimberg     }
1188cbfe683aSSebastian Grimberg   }
1189cbfe683aSSebastian Grimberg 
1190cbfe683aSSebastian Grimberg   // Operator data struct
1191cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1192cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1193cbfe683aSSebastian Grimberg 
1194cbfe683aSSebastian Grimberg   // Assemble kernel
1195cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
1196cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
1197cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1198cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1199cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1200cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1201cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1202cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1203cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1204cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1205cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1206cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1207cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1208cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1209cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1210cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1211cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1212cbfe683aSSebastian Grimberg }
1213cbfe683aSSebastian Grimberg 
1214cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1215004e4986SSebastian Grimberg // Assemble Diagonal Core
12160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1217b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
12180d0321e0SJeremy L Thompson   Ceed                ceed;
1219cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1220b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
1221b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1222004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1223004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
12240d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
1225b7453713SJeremy L Thompson 
1226b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12272b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
12280d0321e0SJeremy L Thompson 
12290d0321e0SJeremy L Thompson   // Assemble QFunction
1230004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1231004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1232004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
12330d0321e0SJeremy L Thompson 
1234cbfe683aSSebastian Grimberg   // Setup
1235cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
1236cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1237cbfe683aSSebastian Grimberg 
1238cbfe683aSSebastian Grimberg   assert(diag != NULL);
1239cbfe683aSSebastian Grimberg 
1240cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1241cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1242cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1243cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
12449330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1245b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1246b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
12479330daecSnbeams 
1248cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
1249cbfe683aSSebastian Grimberg   }
12500d0321e0SJeremy L Thompson 
1251004e4986SSebastian Grimberg   // Restriction and diagonal vector
1252004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1253004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1254004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1255004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1256004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1257004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1258004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1259004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1260004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
12610d0321e0SJeremy L Thompson   }
1262004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1263004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1264b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
12650d0321e0SJeremy L Thompson 
126691db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1267004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1268004e4986SSebastian Grimberg   if (num_nodes > 0) {
12690d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1270b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
1271b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
12720d0321e0SJeremy L Thompson 
12730d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1274004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1275004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1276004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1277004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1278004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1279b7453713SJeremy L Thompson 
1280b7453713SJeremy L Thompson     if (is_point_block) {
1281004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
12820d0321e0SJeremy L Thompson     } else {
1283004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
12840d0321e0SJeremy L Thompson     }
12850d0321e0SJeremy L Thompson 
12860d0321e0SJeremy L Thompson     // Restore arrays
1287b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1288b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
128991db28b6SZach Atkins   }
12900d0321e0SJeremy L Thompson 
12910d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1292b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
12930d0321e0SJeremy L Thompson 
12940d0321e0SJeremy L Thompson   // Cleanup
1295b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
12960d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12970d0321e0SJeremy L Thompson }
12980d0321e0SJeremy L Thompson 
12990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13000d0321e0SJeremy L Thompson // Assemble Linear Diagonal
13010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13022b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
13032b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
13046aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13050d0321e0SJeremy L Thompson }
13060d0321e0SJeremy L Thompson 
13070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13080d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
13090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13102b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
13112b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
13126aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13130d0321e0SJeremy L Thompson }
13140d0321e0SJeremy L Thompson 
1315a835093fSnbeams //------------------------------------------------------------------------------
1316004e4986SSebastian Grimberg // Single Operator Assembly Setup
1317a835093fSnbeams //------------------------------------------------------------------------------
13189330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
1319a835093fSnbeams   Ceed                ceed;
132022070f95SJeremy L Thompson   char               *assembly_kernel_source;
132122070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1322004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
13233b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1324004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1325b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1326b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1327b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
1328b7453713SJeremy L Thompson   CeedQFunction       qf;
1329b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1330a835093fSnbeams   CeedOperator_Hip   *impl;
1331b7453713SJeremy L Thompson 
1332b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
13332b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1334a835093fSnbeams 
1335a835093fSnbeams   // Get intput and output fields
13362b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1337a835093fSnbeams 
1338a835093fSnbeams   // Determine active input basis eval mode
13392b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
13402b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1341a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1342a835093fSnbeams     CeedVector vec;
1343b7453713SJeremy L Thompson 
13442b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1345a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1346004e4986SSebastian Grimberg       CeedBasis    basis;
1347b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1348b7453713SJeremy L Thompson 
1349004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1350004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1351004e4986SSebastian Grimberg       basis_in = basis;
13522b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1353004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1354004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1355004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
13562b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1357004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1358004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1359004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1360004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1361004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1362004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1363a835093fSnbeams         }
1364004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1365a835093fSnbeams       }
1366a835093fSnbeams     }
1367a835093fSnbeams   }
1368a835093fSnbeams 
1369a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
13702b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1371a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1372a835093fSnbeams     CeedVector vec;
1373b7453713SJeremy L Thompson 
13742b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1375a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1376004e4986SSebastian Grimberg       CeedBasis    basis;
1377b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1378b7453713SJeremy L Thompson 
1379004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1380004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1381004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1382004e4986SSebastian Grimberg       basis_out = basis;
13832b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1384004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1385004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1386004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1387004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1388004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
13892b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1390004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1391004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1392004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1393004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1394004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1395004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1396004e4986SSebastian Grimberg         }
1397004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1398a835093fSnbeams       }
1399a835093fSnbeams     }
1400a835093fSnbeams   }
1401004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1402a835093fSnbeams 
14032b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1404a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1405004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1406004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1407004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1408004e4986SSebastian Grimberg 
1409004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1410004e4986SSebastian Grimberg 
1411004e4986SSebastian Grimberg   if (fallback) {
1412004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1413004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1414004e4986SSebastian Grimberg   }
1415a835093fSnbeams 
1416a835093fSnbeams   // Compile kernels
1417004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1418004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
14192b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
142023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
14212b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
142223d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1423004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1424004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1425004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1426cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
14279330daecSnbeams                                   use_ceedsize_idx));
1428004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
14292b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
14302b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1431a835093fSnbeams 
1432004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1433004e4986SSebastian Grimberg   {
1434004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1435004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1436004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1437004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1438004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1439a835093fSnbeams 
1440004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1441004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1442004e4986SSebastian Grimberg     }
1443004e4986SSebastian Grimberg     if (has_eval_none) {
1444004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1445004e4986SSebastian 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;
1446004e4986SSebastian Grimberg     }
1447b7453713SJeremy L Thompson 
1448b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1449004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1450004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1451004e4986SSebastian Grimberg 
1452004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1453004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1454004e4986SSebastian Grimberg       if (q_comp > 1) {
1455004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1456004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1457004e4986SSebastian Grimberg       }
1458004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1459004e4986SSebastian Grimberg 
1460004e4986SSebastian 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),
1461004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1462004e4986SSebastian Grimberg     }
1463004e4986SSebastian Grimberg 
1464004e4986SSebastian Grimberg     if (identity) {
1465004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1466a835093fSnbeams     }
1467a835093fSnbeams   }
1468a835093fSnbeams 
1469004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1470004e4986SSebastian Grimberg   {
1471004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1472004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1473004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1474004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1475004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1476b7453713SJeremy L Thompson 
1477004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1478004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1479004e4986SSebastian Grimberg     }
1480004e4986SSebastian Grimberg     if (has_eval_none) {
1481004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1482004e4986SSebastian 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;
1483a835093fSnbeams     }
1484a835093fSnbeams 
1485b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1486004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1487004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1488004e4986SSebastian Grimberg 
1489004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1490004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1491004e4986SSebastian Grimberg       if (q_comp > 1) {
1492004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1493004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1494004e4986SSebastian Grimberg       }
1495004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1496004e4986SSebastian Grimberg 
1497004e4986SSebastian 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),
1498004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1499004e4986SSebastian Grimberg     }
1500004e4986SSebastian Grimberg 
1501004e4986SSebastian Grimberg     if (identity) {
1502004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1503a835093fSnbeams     }
1504a835093fSnbeams   }
1505a835093fSnbeams   return CEED_ERROR_SUCCESS;
1506a835093fSnbeams }
1507a835093fSnbeams 
1508a835093fSnbeams //------------------------------------------------------------------------------
1509cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1510cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1511cefa2673SJeremy L Thompson //
1512ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1513cefa2673SJeremy L Thompson // modes).
1514cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1515a835093fSnbeams //------------------------------------------------------------------------------
15162b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1517a835093fSnbeams   Ceed                ceed;
1518b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1519004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1520b7453713SJeremy L Thompson   CeedScalar         *values_array;
1521004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1522b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1523004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1524004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1525004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1526004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1527a835093fSnbeams   CeedOperator_Hip   *impl;
1528b7453713SJeremy L Thompson 
1529b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
15302b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1531a835093fSnbeams 
1532a835093fSnbeams   // Assemble QFunction
1533004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1534004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1535004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1536a835093fSnbeams 
15379330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
15389330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
15399330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1540004e4986SSebastian Grimberg 
15419330daecSnbeams   // Setup
1542004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1543004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1544004e4986SSebastian Grimberg 
1545004e4986SSebastian Grimberg   assert(asmb != NULL);
1546004e4986SSebastian Grimberg 
1547004e4986SSebastian Grimberg   // Assemble element operator
1548004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1549004e4986SSebastian Grimberg   values_array += offset;
1550004e4986SSebastian Grimberg 
1551004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1552004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1553004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1554004e4986SSebastian Grimberg 
1555004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1556004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1557004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1558004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1559004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1560004e4986SSebastian Grimberg   }
1561004e4986SSebastian Grimberg 
1562004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1563004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1564004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1565004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1566004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1567004e4986SSebastian Grimberg 
1568004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1569004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1570004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1571004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1572004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1573004e4986SSebastian Grimberg     }
1574004e4986SSebastian Grimberg   } else {
1575004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1576004e4986SSebastian Grimberg     orients_out      = orients_in;
1577004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
15789330daecSnbeams   }
15799330daecSnbeams 
1580a835093fSnbeams   // Compute B^T D B
1581004e4986SSebastian Grimberg   CeedInt shared_mem =
1582004e4986SSebastian 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)) *
1583004e4986SSebastian Grimberg       sizeof(CeedScalar);
1584004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1585004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1586004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1587b7453713SJeremy L Thompson 
15882b730f8bSJeremy L Thompson   CeedCallBackend(
1589004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1590a835093fSnbeams 
1591a835093fSnbeams   // Restore arrays
15922b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1593004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1594a835093fSnbeams 
1595a835093fSnbeams   // Cleanup
15962b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1597004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1598004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1599004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1600004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1601004e4986SSebastian Grimberg   }
1602004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1603004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1604004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1605004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1606004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1607004e4986SSebastian Grimberg     }
1608004e4986SSebastian Grimberg   }
1609a835093fSnbeams   return CEED_ERROR_SUCCESS;
1610a835093fSnbeams }
1611a835093fSnbeams 
1612a835093fSnbeams //------------------------------------------------------------------------------
161367d9480aSJeremy L Thompson // Assemble Linear QFunction AtPoints
161467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
161567d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
161667d9480aSJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
161767d9480aSJeremy L Thompson }
161867d9480aSJeremy L Thompson 
161967d9480aSJeremy L Thompson //------------------------------------------------------------------------------
162067d9480aSJeremy L Thompson // Assemble Linear Diagonal AtPoints
162167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
162267d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1623*111870feSJeremy L Thompson   CeedInt             max_num_points, *num_points, num_elem, num_input_fields, num_output_fields;
1624afe3bc8aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1625afe3bc8aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1626afe3bc8aSJeremy L Thompson   CeedQFunction       qf;
1627afe3bc8aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1628afe3bc8aSJeremy L Thompson   CeedOperator_Hip   *impl;
1629afe3bc8aSJeremy L Thompson 
1630afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1631afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1632afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1633afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1634afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1635afe3bc8aSJeremy L Thompson 
1636afe3bc8aSJeremy L Thompson   // Setup
1637afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
1638*111870feSJeremy L Thompson   num_points     = impl->num_points;
1639afe3bc8aSJeremy L Thompson   max_num_points = impl->max_num_points;
1640afe3bc8aSJeremy L Thompson 
16418a213570SJeremy L Thompson   // Create separate output e-vecs
16428a213570SJeremy L Thompson   if (impl->has_shared_e_vecs) {
16438a213570SJeremy L Thompson     for (CeedInt i = 0; i < impl->num_outputs; i++) {
16448a213570SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
16458a213570SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[impl->num_inputs + i]));
16468a213570SJeremy L Thompson     }
16478a213570SJeremy 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,
16488a213570SJeremy L Thompson                                                 num_input_fields, num_output_fields, max_num_points, num_elem));
16498a213570SJeremy L Thompson   }
16508a213570SJeremy L Thompson   impl->has_shared_e_vecs = false;
16518a213570SJeremy L Thompson 
1652afe3bc8aSJeremy L Thompson   // Input Evecs and Restriction
1653afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1654afe3bc8aSJeremy L Thompson 
1655afe3bc8aSJeremy L Thompson   // Get point coordinates
1656afe3bc8aSJeremy L Thompson   if (!impl->point_coords_elem) {
1657afe3bc8aSJeremy L Thompson     CeedVector          point_coords = NULL;
1658afe3bc8aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1659afe3bc8aSJeremy L Thompson 
1660afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1661afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1662afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1663afe3bc8aSJeremy L Thompson   }
1664afe3bc8aSJeremy 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
1675afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1676afe3bc8aSJeremy L Thompson 
1677afe3bc8aSJeremy L Thompson   // Output pointers, as necessary
1678afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1679afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1680afe3bc8aSJeremy L Thompson 
1681afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1682afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1683afe3bc8aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1684afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1685afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1686afe3bc8aSJeremy L Thompson     }
1687afe3bc8aSJeremy L Thompson   }
1688afe3bc8aSJeremy L Thompson 
1689afe3bc8aSJeremy L Thompson   // Loop over active fields
1690afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1691382e9c83SJeremy L Thompson     bool                is_active_at_points = true;
1692382e9c83SJeremy L Thompson     CeedInt             elem_size = 1, num_comp_active = 1, e_vec_size = 0;
1693382e9c83SJeremy L Thompson     CeedRestrictionType rstr_type;
1694382e9c83SJeremy L Thompson     CeedVector          vec;
1695382e9c83SJeremy L Thompson     CeedElemRestriction elem_rstr;
1696382e9c83SJeremy L Thompson 
1697382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1698382e9c83SJeremy L Thompson     // -- Skip non-active input
1699382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1700382e9c83SJeremy L Thompson 
1701382e9c83SJeremy L Thompson     // -- Get active restriction type
1702382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1703382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1704382e9c83SJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1705382e9c83SJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1706382e9c83SJeremy L Thompson     else elem_size = max_num_points;
1707382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1708382e9c83SJeremy L Thompson 
1709382e9c83SJeremy L Thompson     e_vec_size = elem_size * num_comp_active;
1710382e9c83SJeremy L Thompson     for (CeedInt s = 0; s < e_vec_size; s++) {
1711afe3bc8aSJeremy L Thompson       bool         is_active_input = false;
1712afe3bc8aSJeremy L Thompson       CeedEvalMode eval_mode;
1713afe3bc8aSJeremy L Thompson       CeedVector   vec;
1714afe3bc8aSJeremy L Thompson       CeedBasis    basis;
1715afe3bc8aSJeremy L Thompson 
1716afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1717afe3bc8aSJeremy L Thompson       // Skip non-active input
1718afe3bc8aSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1719afe3bc8aSJeremy L Thompson       if (!is_active_input) continue;
1720afe3bc8aSJeremy L Thompson 
1721afe3bc8aSJeremy L Thompson       // Update unit vector
172213062808SJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1723afe3bc8aSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1724afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1725afe3bc8aSJeremy L Thompson 
1726afe3bc8aSJeremy L Thompson       // Basis action
1727afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1728afe3bc8aSJeremy L Thompson       switch (eval_mode) {
1729afe3bc8aSJeremy L Thompson         case CEED_EVAL_NONE:
1730afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1731afe3bc8aSJeremy L Thompson           break;
1732afe3bc8aSJeremy L Thompson         case CEED_EVAL_INTERP:
1733afe3bc8aSJeremy L Thompson         case CEED_EVAL_GRAD:
1734afe3bc8aSJeremy L Thompson         case CEED_EVAL_DIV:
1735afe3bc8aSJeremy L Thompson         case CEED_EVAL_CURL:
1736afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1737afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1738afe3bc8aSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1739afe3bc8aSJeremy L Thompson           break;
1740afe3bc8aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1741afe3bc8aSJeremy L Thompson           break;  // No action
1742afe3bc8aSJeremy L Thompson       }
1743afe3bc8aSJeremy L Thompson 
1744afe3bc8aSJeremy L Thompson       // Q function
1745afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1746afe3bc8aSJeremy L Thompson 
1747afe3bc8aSJeremy L Thompson       // Output basis apply if needed
1748382e9c83SJeremy L Thompson       for (CeedInt j = 0; j < num_output_fields; j++) {
1749afe3bc8aSJeremy L Thompson         bool                is_active_output = false;
1750382e9c83SJeremy L Thompson         CeedInt             elem_size        = 0;
1751382e9c83SJeremy L Thompson         CeedRestrictionType rstr_type;
1752afe3bc8aSJeremy L Thompson         CeedEvalMode        eval_mode;
1753afe3bc8aSJeremy L Thompson         CeedVector          vec;
1754afe3bc8aSJeremy L Thompson         CeedElemRestriction elem_rstr;
1755afe3bc8aSJeremy L Thompson         CeedBasis           basis;
1756afe3bc8aSJeremy L Thompson 
1757382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec));
1758382e9c83SJeremy L Thompson         // ---- Skip non-active output
1759afe3bc8aSJeremy L Thompson         is_active_output = vec == CEED_VECTOR_ACTIVE;
1760afe3bc8aSJeremy L Thompson         if (!is_active_output) continue;
1761afe3bc8aSJeremy L Thompson 
1762382e9c83SJeremy L Thompson         // ---- Check if elem size matches
1763382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1764382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1765382e9c83SJeremy L Thompson         if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue;
1766382e9c83SJeremy L Thompson         if (rstr_type == CEED_RESTRICTION_POINTS) {
1767382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size));
1768382e9c83SJeremy L Thompson         } else {
1769382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1770382e9c83SJeremy L Thompson         }
1771382e9c83SJeremy L Thompson         {
1772382e9c83SJeremy L Thompson           CeedInt num_comp = 0;
1773382e9c83SJeremy L Thompson 
1774382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1775382e9c83SJeremy L Thompson           if (e_vec_size != num_comp * elem_size) continue;
1776382e9c83SJeremy L Thompson         }
1777382e9c83SJeremy L Thompson 
1778afe3bc8aSJeremy L Thompson         // Basis action
1779382e9c83SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
1780afe3bc8aSJeremy L Thompson         switch (eval_mode) {
1781afe3bc8aSJeremy L Thompson           case CEED_EVAL_NONE:
1782382e9c83SJeremy L Thompson             CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields]));
1783afe3bc8aSJeremy L Thompson             break;
1784afe3bc8aSJeremy L Thompson           case CEED_EVAL_INTERP:
1785afe3bc8aSJeremy L Thompson           case CEED_EVAL_GRAD:
1786afe3bc8aSJeremy L Thompson           case CEED_EVAL_DIV:
1787afe3bc8aSJeremy L Thompson           case CEED_EVAL_CURL:
1788382e9c83SJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis));
1789382e9c83SJeremy L Thompson             CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
1790382e9c83SJeremy L Thompson                                                    impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs]));
1791afe3bc8aSJeremy L Thompson             break;
1792afe3bc8aSJeremy L Thompson           // LCOV_EXCL_START
1793afe3bc8aSJeremy L Thompson           case CEED_EVAL_WEIGHT: {
1794afe3bc8aSJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1795afe3bc8aSJeremy L Thompson             // LCOV_EXCL_STOP
1796afe3bc8aSJeremy L Thompson           }
1797afe3bc8aSJeremy L Thompson         }
1798afe3bc8aSJeremy L Thompson 
1799afe3bc8aSJeremy L Thompson         // Mask output e-vec
1800382e9c83SJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs]));
1801afe3bc8aSJeremy L Thompson 
1802afe3bc8aSJeremy L Thompson         // Restrict
1803382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1804382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request));
1805afe3bc8aSJeremy L Thompson 
1806afe3bc8aSJeremy L Thompson         // Reset q_vec for
1807afe3bc8aSJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) {
1808382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields]));
1809382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields]));
1810afe3bc8aSJeremy L Thompson         }
1811afe3bc8aSJeremy L Thompson       }
1812382e9c83SJeremy L Thompson 
1813382e9c83SJeremy L Thompson       // Reset vec
181413062808SJeremy L Thompson       if (s == e_vec_size - 1 && i != num_input_fields - 1) CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
181586e10729SJeremy L Thompson     }
1816afe3bc8aSJeremy L Thompson   }
1817afe3bc8aSJeremy L Thompson 
1818afe3bc8aSJeremy L Thompson   // Restore CEED_EVAL_NONE
1819afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1820afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1821afe3bc8aSJeremy L Thompson 
1822afe3bc8aSJeremy L Thompson     // Get eval_mode
1823afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1824afe3bc8aSJeremy L Thompson 
1825afe3bc8aSJeremy L Thompson     // Restore evec
1826afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1827afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1828afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1829afe3bc8aSJeremy L Thompson     }
1830afe3bc8aSJeremy L Thompson   }
1831afe3bc8aSJeremy L Thompson 
1832afe3bc8aSJeremy L Thompson   // Restore input arrays
1833afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1834afe3bc8aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
183567d9480aSJeremy L Thompson }
183667d9480aSJeremy L Thompson 
183767d9480aSJeremy L Thompson //------------------------------------------------------------------------------
18380d0321e0SJeremy L Thompson // Create operator
18390d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
18400d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
18410d0321e0SJeremy L Thompson   Ceed              ceed;
18420d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
18430d0321e0SJeremy L Thompson 
1844b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
18452b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
18462b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
18472b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
18482b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
18492b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
18502b730f8bSJeremy L Thompson   CeedCallBackend(
18512b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
18522b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
18532b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
18542b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
18550d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18560d0321e0SJeremy L Thompson }
18570d0321e0SJeremy L Thompson 
18580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
185967d9480aSJeremy L Thompson // Create operator AtPoints
186067d9480aSJeremy L Thompson //------------------------------------------------------------------------------
186167d9480aSJeremy L Thompson int CeedOperatorCreateAtPoints_Hip(CeedOperator op) {
186267d9480aSJeremy L Thompson   Ceed              ceed;
186367d9480aSJeremy L Thompson   CeedOperator_Hip *impl;
186467d9480aSJeremy L Thompson 
186567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
186667d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
186767d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
186867d9480aSJeremy L Thompson 
186967d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Hip));
187067d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip));
187167d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Hip));
187267d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
187367d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
187467d9480aSJeremy L Thompson }
187567d9480aSJeremy L Thompson 
187667d9480aSJeremy L Thompson //------------------------------------------------------------------------------
1877