xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-operator.c (revision 3aab95c02208308af393c9014d0e46d9d208d4a6)
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*3aab95c0SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_in));
30b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
31b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
320d0321e0SJeremy L Thompson   }
33b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
34c1222711SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
350d0321e0SJeremy L Thompson 
36b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
37b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
380d0321e0SJeremy L Thompson   }
39b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
400d0321e0SJeremy L Thompson 
41b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
42b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
430d0321e0SJeremy L Thompson   }
44b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
4567d9480aSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem));
460d0321e0SJeremy L Thompson 
47b2165e7aSSebastian Grimberg   // QFunction assembly data
48b7453713SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
49b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
500d0321e0SJeremy L Thompson   }
51b7453713SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
520d0321e0SJeremy L Thompson 
530d0321e0SJeremy L Thompson   // Diag data
540d0321e0SJeremy L Thompson   if (impl->diag) {
550d0321e0SJeremy L Thompson     Ceed ceed;
56b7453713SJeremy L Thompson 
572b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
58cbfe683aSSebastian Grimberg     if (impl->diag->module) {
592b730f8bSJeremy L Thompson       CeedCallHip(ceed, hipModuleUnload(impl->diag->module));
60cbfe683aSSebastian Grimberg     }
61cbfe683aSSebastian Grimberg     if (impl->diag->module_point_block) {
62cbfe683aSSebastian Grimberg       CeedCallHip(ceed, hipModuleUnload(impl->diag->module_point_block));
63cbfe683aSSebastian Grimberg     }
64004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_in));
65004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_eval_modes_out));
662b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_identity));
67b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_in));
68b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_interp_out));
69b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_in));
70b7453713SJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->diag->d_grad_out));
71004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_in));
72004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_div_out));
73004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_in));
74004e4986SSebastian Grimberg     CeedCallHip(ceed, hipFree(impl->diag->d_curl_out));
75004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
76b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
77b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
78b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
790d0321e0SJeremy L Thompson   }
802b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
810d0321e0SJeremy L Thompson 
82a835093fSnbeams   if (impl->asmb) {
83a835093fSnbeams     Ceed ceed;
84b7453713SJeremy L Thompson 
852b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
862b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipModuleUnload(impl->asmb->module));
872b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_in));
882b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipFree(impl->asmb->d_B_out));
89a835093fSnbeams   }
902b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
91a835093fSnbeams 
922b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
930d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
940d0321e0SJeremy L Thompson }
950d0321e0SJeremy L Thompson 
960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
970d0321e0SJeremy L Thompson // Setup infields or outfields
980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
99*3aab95c0SJeremy L Thompson static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, bool *skip_rstr, CeedVector *e_vecs,
100*3aab95c0SJeremy L Thompson                                        CeedVector *q_vecs, CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
1010d0321e0SJeremy L Thompson   Ceed                ceed;
102b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
103b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
1040d0321e0SJeremy L Thompson 
105b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
106b7453713SJeremy L Thompson   if (is_input) {
107b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
108b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1090d0321e0SJeremy L Thompson   } else {
110b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
111b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1120d0321e0SJeremy L Thompson   }
1130d0321e0SJeremy L Thompson 
1140d0321e0SJeremy L Thompson   // Loop over fields
115b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
116004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
117b7453713SJeremy L Thompson     CeedSize     q_size;
118004e4986SSebastian Grimberg     CeedInt      size;
119004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
120b7453713SJeremy L Thompson     CeedBasis    basis;
1210d0321e0SJeremy L Thompson 
122004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
123004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
124004e4986SSebastian Grimberg       CeedElemRestriction elem_rstr;
1250d0321e0SJeremy L Thompson 
1260d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
127004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
128004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1290d0321e0SJeremy L Thompson 
1300d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
131b7453713SJeremy L Thompson       if (is_input) {
132004e4986SSebastian Grimberg         CeedVector vec;
133004e4986SSebastian Grimberg 
134004e4986SSebastian Grimberg         // Check for passive input
135b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
136b7453713SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
137004e4986SSebastian Grimberg           // Check eval_mode
138004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1390d0321e0SJeremy L Thompson             // Check for strided restriction
140b7453713SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
141b7453713SJeremy L Thompson             if (is_strided) {
1420d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
143b7453713SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1440d0321e0SJeremy L Thompson             }
1450d0321e0SJeremy L Thompson           }
1460d0321e0SJeremy L Thompson         }
1470d0321e0SJeremy L Thompson       }
148b7453713SJeremy L Thompson       if (skip_restriction) {
149ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
150b7453713SJeremy L Thompson         e_vecs[i + start_e] = NULL;
1510d0321e0SJeremy L Thompson       } else {
152b7453713SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1530d0321e0SJeremy L Thompson       }
1540d0321e0SJeremy L Thompson     }
1550d0321e0SJeremy L Thompson 
156004e4986SSebastian Grimberg     switch (eval_mode) {
1570d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
158b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
159b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
160b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1610d0321e0SJeremy L Thompson         break;
1620d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1630d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
164004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
165004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
166b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
167b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
168b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1690d0321e0SJeremy L Thompson         break;
1700d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
171b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
172b7453713SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
173b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
17467d9480aSJeremy L Thompson         if (is_at_points) {
17567d9480aSJeremy L Thompson           CeedInt num_points[num_elem];
17667d9480aSJeremy L Thompson 
17767d9480aSJeremy L Thompson           for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q;
17867d9480aSJeremy L Thompson           CeedCallBackend(
17967d9480aSJeremy L Thompson               CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i]));
18067d9480aSJeremy L Thompson         } else {
181b7453713SJeremy L Thompson           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
18267d9480aSJeremy L Thompson         }
1830d0321e0SJeremy L Thompson         break;
1840d0321e0SJeremy L Thompson     }
1850d0321e0SJeremy L Thompson   }
186*3aab95c0SJeremy L Thompson   // Drop duplicate input restrictions
187*3aab95c0SJeremy L Thompson   if (is_input) {
188*3aab95c0SJeremy L Thompson     for (CeedInt i = 0; i < num_fields; i++) {
189*3aab95c0SJeremy L Thompson       CeedVector          vec_i;
190*3aab95c0SJeremy L Thompson       CeedElemRestriction rstr_i;
191*3aab95c0SJeremy L Thompson 
192*3aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
193*3aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
194*3aab95c0SJeremy L Thompson       for (CeedInt j = i + 1; j < num_fields; j++) {
195*3aab95c0SJeremy L Thompson         CeedVector          vec_j;
196*3aab95c0SJeremy L Thompson         CeedElemRestriction rstr_j;
197*3aab95c0SJeremy L Thompson 
198*3aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
199*3aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
200*3aab95c0SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
201*3aab95c0SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i], &e_vecs[j]));
202*3aab95c0SJeremy L Thompson           skip_rstr[j] = true;
203*3aab95c0SJeremy L Thompson         }
204*3aab95c0SJeremy L Thompson       }
205*3aab95c0SJeremy L Thompson     }
206*3aab95c0SJeremy L Thompson   }
2070d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2080d0321e0SJeremy L Thompson }
2090d0321e0SJeremy L Thompson 
2100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
211ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
2120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2130d0321e0SJeremy L Thompson static int CeedOperatorSetup_Hip(CeedOperator op) {
2140d0321e0SJeremy L Thompson   Ceed                ceed;
215b7453713SJeremy L Thompson   bool                is_setup_done;
216b7453713SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
217b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
2180d0321e0SJeremy L Thompson   CeedQFunction       qf;
219b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
220b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
221b7453713SJeremy L Thompson 
222b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
223b7453713SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
224b7453713SJeremy L Thompson 
225b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
226b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
2272b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
2282b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
229b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
230b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
231b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2320d0321e0SJeremy L Thompson 
2330d0321e0SJeremy L Thompson   // Allocate
234b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
235*3aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
236c1222711SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
237b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
238b7453713SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
239b7453713SJeremy L Thompson   impl->num_inputs  = num_input_fields;
240b7453713SJeremy L Thompson   impl->num_outputs = num_output_fields;
2410d0321e0SJeremy L Thompson 
242b7453713SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
2430d0321e0SJeremy L Thompson   // Infields
244*3aab95c0SJeremy L Thompson   CeedCallBackend(
245*3aab95c0SJeremy L Thompson       CeedOperatorSetupFields_Hip(qf, op, true, false, impl->skip_rstr_in, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2460d0321e0SJeremy L Thompson   // Outfields
24767d9480aSJeremy L Thompson   CeedCallBackend(
248*3aab95c0SJeremy L Thompson       CeedOperatorSetupFields_Hip(qf, op, false, false, NULL, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem));
2490d0321e0SJeremy L Thompson 
2502b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2510d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2520d0321e0SJeremy L Thompson }
2530d0321e0SJeremy L Thompson 
2540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2550d0321e0SJeremy L Thompson // Setup Operator Inputs
2560d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
257b7453713SJeremy L Thompson static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
258b7453713SJeremy L Thompson                                               CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
259b7453713SJeremy L Thompson                                               CeedOperator_Hip *impl, CeedRequest *request) {
260b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
261004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
2620d0321e0SJeremy L Thompson     CeedVector          vec;
263b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
2640d0321e0SJeremy L Thompson 
2650d0321e0SJeremy L Thompson     // Get input vector
266b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2670d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
268b7453713SJeremy L Thompson       if (skip_active) continue;
269b7453713SJeremy L Thompson       else vec = in_vec;
2700d0321e0SJeremy L Thompson     }
2710d0321e0SJeremy L Thompson 
272004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
273004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2740d0321e0SJeremy L Thompson     } else {
2750d0321e0SJeremy L Thompson       // Get input vector
276b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2770d0321e0SJeremy L Thompson       // Get input element restriction
278b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
279b7453713SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
2800d0321e0SJeremy L Thompson       // Restrict, if necessary
281b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {
2820d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
283b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2840d0321e0SJeremy L Thompson       } else {
285c1222711SJeremy L Thompson         uint64_t state;
286c1222711SJeremy L Thompson 
287c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
288*3aab95c0SJeremy L Thompson         if (state != impl->input_states[i] && !impl->skip_rstr_in[i]) {
289b7453713SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
290c1222711SJeremy L Thompson         }
291*3aab95c0SJeremy L Thompson         impl->input_states[i] = state;
2920d0321e0SJeremy L Thompson         // Get evec
293b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2940d0321e0SJeremy L Thompson       }
2950d0321e0SJeremy L Thompson     }
2960d0321e0SJeremy L Thompson   }
2970d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2980d0321e0SJeremy L Thompson }
2990d0321e0SJeremy L Thompson 
3000d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3010d0321e0SJeremy L Thompson // Input Basis Action
3020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
303b7453713SJeremy L Thompson static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
304b7453713SJeremy L Thompson                                              CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
3052b730f8bSJeremy L Thompson                                              CeedOperator_Hip *impl) {
306b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
307b7453713SJeremy L Thompson     CeedInt             elem_size, size;
308004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
309b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
3100d0321e0SJeremy L Thompson     CeedBasis           basis;
3110d0321e0SJeremy L Thompson 
3120d0321e0SJeremy L Thompson     // Skip active input
313b7453713SJeremy L Thompson     if (skip_active) {
3140d0321e0SJeremy L Thompson       CeedVector vec;
315b7453713SJeremy L Thompson 
316b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3172b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3180d0321e0SJeremy L Thompson     }
319004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
320b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
321b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
322004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
323b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3240d0321e0SJeremy L Thompson     // Basis action
325004e4986SSebastian Grimberg     switch (eval_mode) {
3260d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
327b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
3280d0321e0SJeremy L Thompson         break;
3290d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3300d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
331004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
332004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
333b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
334004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3350d0321e0SJeremy L Thompson         break;
3360d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
3370d0321e0SJeremy L Thompson         break;  // No action
3380d0321e0SJeremy L Thompson     }
3390d0321e0SJeremy L Thompson   }
3400d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3410d0321e0SJeremy L Thompson }
3420d0321e0SJeremy L Thompson 
3430d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3440d0321e0SJeremy L Thompson // Restore Input Vectors
3450d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
346b7453713SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Hip(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
347b7453713SJeremy L Thompson                                                 const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
348b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
349004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
3500d0321e0SJeremy L Thompson     CeedVector   vec;
351004e4986SSebastian Grimberg 
3520d0321e0SJeremy L Thompson     // Skip active input
353b7453713SJeremy L Thompson     if (skip_active) {
354b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3552b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3560d0321e0SJeremy L Thompson     }
357004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
358004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3590d0321e0SJeremy L Thompson     } else {
360b7453713SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
361b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
362b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
3630d0321e0SJeremy L Thompson       } else {
364b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
3650d0321e0SJeremy L Thompson       }
3660d0321e0SJeremy L Thompson     }
3670d0321e0SJeremy L Thompson   }
3680d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3690d0321e0SJeremy L Thompson }
3700d0321e0SJeremy L Thompson 
3710d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3720d0321e0SJeremy L Thompson // Apply and add to output
3730d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
374b7453713SJeremy L Thompson static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
375b7453713SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
376b7453713SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
377b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
3780d0321e0SJeremy L Thompson   CeedQFunction       qf;
379b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
380b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
381b7453713SJeremy L Thompson 
382b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3832b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3842b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
385b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
386b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
387b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
3880d0321e0SJeremy L Thompson 
3890d0321e0SJeremy L Thompson   // Setup
3902b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
3910d0321e0SJeremy L Thompson 
3920d0321e0SJeremy L Thompson   // Input Evecs and Restriction
393b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
3940d0321e0SJeremy L Thompson 
3950d0321e0SJeremy L Thompson   // Input basis apply if needed
396b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
3970d0321e0SJeremy L Thompson 
3980d0321e0SJeremy L Thompson   // Output pointers, as necessary
399b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
400004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
401b7453713SJeremy L Thompson 
402004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
403004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
4040d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
405b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
406b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
4070d0321e0SJeremy L Thompson     }
4080d0321e0SJeremy L Thompson   }
4090d0321e0SJeremy L Thompson 
4100d0321e0SJeremy L Thompson   // Q function
411b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
4120d0321e0SJeremy L Thompson 
4130d0321e0SJeremy L Thompson   // Output basis apply if needed
414b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
415004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
416b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
417b7453713SJeremy L Thompson     CeedBasis           basis;
418b7453713SJeremy L Thompson 
419004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
420b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
421b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
422004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
423b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4240d0321e0SJeremy L Thompson     // Basis action
425004e4986SSebastian Grimberg     switch (eval_mode) {
4260d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
427004e4986SSebastian Grimberg         break;  // No action
4280d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4290d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
430004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
431004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
432b7453713SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
433004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
4340d0321e0SJeremy L Thompson         break;
4350d0321e0SJeremy L Thompson       // LCOV_EXCL_START
4360d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
4376e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
4380d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
4390d0321e0SJeremy L Thompson       }
4400d0321e0SJeremy L Thompson     }
441004e4986SSebastian Grimberg   }
4420d0321e0SJeremy L Thompson 
4430d0321e0SJeremy L Thompson   // Output restriction
444b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
445004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
446b7453713SJeremy L Thompson     CeedVector          vec;
447b7453713SJeremy L Thompson     CeedElemRestriction elem_rstr;
448b7453713SJeremy L Thompson 
4490d0321e0SJeremy L Thompson     // Restore evec
450004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
451004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
452b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4530d0321e0SJeremy L Thompson     }
4540d0321e0SJeremy L Thompson     // Get output vector
455b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4560d0321e0SJeremy L Thompson     // Restrict
457b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4580d0321e0SJeremy L Thompson     // Active
459b7453713SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4600d0321e0SJeremy L Thompson 
461b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4620d0321e0SJeremy L Thompson   }
4630d0321e0SJeremy L Thompson 
4640d0321e0SJeremy L Thompson   // Restore input arrays
465b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4660d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4670d0321e0SJeremy L Thompson }
4680d0321e0SJeremy L Thompson 
4690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
47067d9480aSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
47167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
47267d9480aSJeremy L Thompson static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) {
47367d9480aSJeremy L Thompson   Ceed                ceed;
47467d9480aSJeremy L Thompson   bool                is_setup_done;
47567d9480aSJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
47667d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
47767d9480aSJeremy L Thompson   CeedQFunction       qf;
47867d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
47967d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
48067d9480aSJeremy L Thompson 
48167d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
48267d9480aSJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
48367d9480aSJeremy L Thompson 
48467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
48567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
48667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
48767d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
48867d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
48967d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
49067d9480aSJeremy L Thompson   {
49167d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr = NULL;
49267d9480aSJeremy L Thompson 
49367d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL));
49467d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points));
49567d9480aSJeremy L Thompson   }
49667d9480aSJeremy L Thompson   impl->max_num_points = max_num_points;
49767d9480aSJeremy L Thompson 
49867d9480aSJeremy L Thompson   // Allocate
49967d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
500*3aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
50167d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
50267d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
50367d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
50467d9480aSJeremy L Thompson   impl->num_inputs  = num_input_fields;
50567d9480aSJeremy L Thompson   impl->num_outputs = num_output_fields;
50667d9480aSJeremy L Thompson 
50767d9480aSJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
50867d9480aSJeremy L Thompson   // Infields
509*3aab95c0SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, true, true, impl->skip_rstr_in, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields,
510*3aab95c0SJeremy L Thompson                                               max_num_points, num_elem));
51167d9480aSJeremy L Thompson   // Outfields
512*3aab95c0SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Hip(qf, op, false, true, NULL, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields,
51367d9480aSJeremy L Thompson                                               max_num_points, num_elem));
51467d9480aSJeremy L Thompson 
51567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
51667d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
51767d9480aSJeremy L Thompson }
51867d9480aSJeremy L Thompson 
51967d9480aSJeremy L Thompson //------------------------------------------------------------------------------
52067d9480aSJeremy L Thompson // Input Basis Action AtPoints
52167d9480aSJeremy L Thompson //------------------------------------------------------------------------------
52267d9480aSJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Hip(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields,
52367d9480aSJeremy L Thompson                                                      CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active,
52467d9480aSJeremy L Thompson                                                      CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Hip *impl) {
52567d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
52667d9480aSJeremy L Thompson     CeedInt             elem_size, size;
52767d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
52867d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
52967d9480aSJeremy L Thompson     CeedBasis           basis;
53067d9480aSJeremy L Thompson 
53167d9480aSJeremy L Thompson     // Skip active input
53267d9480aSJeremy L Thompson     if (skip_active) {
53367d9480aSJeremy L Thompson       CeedVector vec;
53467d9480aSJeremy L Thompson 
53567d9480aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
53667d9480aSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
53767d9480aSJeremy L Thompson     }
53867d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
53967d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
54067d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
54167d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
54267d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
54367d9480aSJeremy L Thompson     // Basis action
54467d9480aSJeremy L Thompson     switch (eval_mode) {
54567d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
54667d9480aSJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
54767d9480aSJeremy L Thompson         break;
54867d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
54967d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
55067d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
55167d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
55267d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
55367d9480aSJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
55467d9480aSJeremy L Thompson                                                impl->q_vecs_in[i]));
55567d9480aSJeremy L Thompson         break;
55667d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT:
55767d9480aSJeremy L Thompson         break;  // No action
55867d9480aSJeremy L Thompson     }
55967d9480aSJeremy L Thompson   }
56067d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
56167d9480aSJeremy L Thompson }
56267d9480aSJeremy L Thompson 
56367d9480aSJeremy L Thompson //------------------------------------------------------------------------------
56467d9480aSJeremy L Thompson // Apply and add to output AtPoints
56567d9480aSJeremy L Thompson //------------------------------------------------------------------------------
56667d9480aSJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
56767d9480aSJeremy L Thompson   CeedInt             max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
56867d9480aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
56967d9480aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
57067d9480aSJeremy L Thompson   CeedQFunction       qf;
57167d9480aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
57267d9480aSJeremy L Thompson   CeedOperator_Hip   *impl;
57367d9480aSJeremy L Thompson 
57467d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
57567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
57667d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
57767d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
57867d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
57967d9480aSJeremy L Thompson   CeedInt num_points[num_elem];
58067d9480aSJeremy L Thompson 
58167d9480aSJeremy L Thompson   // Setup
58267d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
58367d9480aSJeremy L Thompson   max_num_points = impl->max_num_points;
58467d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
58567d9480aSJeremy L Thompson 
58667d9480aSJeremy L Thompson   // Input Evecs and Restriction
58767d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
58867d9480aSJeremy L Thompson 
58967d9480aSJeremy L Thompson   // Get point coordinates
59067d9480aSJeremy L Thompson   if (!impl->point_coords_elem) {
59167d9480aSJeremy L Thompson     CeedVector          point_coords = NULL;
59267d9480aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
59367d9480aSJeremy L Thompson 
59467d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
59567d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
59667d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
59767d9480aSJeremy L Thompson   }
59867d9480aSJeremy L Thompson 
59967d9480aSJeremy L Thompson   // Input basis apply if needed
60067d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
60167d9480aSJeremy L Thompson 
60267d9480aSJeremy L Thompson   // Output pointers, as necessary
60367d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
60467d9480aSJeremy L Thompson     CeedEvalMode eval_mode;
60567d9480aSJeremy L Thompson 
60667d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
60767d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
60867d9480aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
60967d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
61067d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
61167d9480aSJeremy L Thompson     }
61267d9480aSJeremy L Thompson   }
61367d9480aSJeremy L Thompson 
61467d9480aSJeremy L Thompson   // Q function
61567d9480aSJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
61667d9480aSJeremy L Thompson 
61767d9480aSJeremy L Thompson   // Output basis apply if needed
61867d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
61967d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
62067d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
62167d9480aSJeremy L Thompson     CeedBasis           basis;
62267d9480aSJeremy L Thompson 
62367d9480aSJeremy L Thompson     // Get elem_size, eval_mode, size
62467d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
62567d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
62667d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
62767d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
62867d9480aSJeremy L Thompson     // Basis action
62967d9480aSJeremy L Thompson     switch (eval_mode) {
63067d9480aSJeremy L Thompson       case CEED_EVAL_NONE:
63167d9480aSJeremy L Thompson         break;  // No action
63267d9480aSJeremy L Thompson       case CEED_EVAL_INTERP:
63367d9480aSJeremy L Thompson       case CEED_EVAL_GRAD:
63467d9480aSJeremy L Thompson       case CEED_EVAL_DIV:
63567d9480aSJeremy L Thompson       case CEED_EVAL_CURL:
63667d9480aSJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
63767d9480aSJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
63867d9480aSJeremy L Thompson                                                impl->e_vecs[i + impl->num_inputs]));
63967d9480aSJeremy L Thompson         break;
64067d9480aSJeremy L Thompson       // LCOV_EXCL_START
64167d9480aSJeremy L Thompson       case CEED_EVAL_WEIGHT: {
64267d9480aSJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
64367d9480aSJeremy L Thompson         // LCOV_EXCL_STOP
64467d9480aSJeremy L Thompson       }
64567d9480aSJeremy L Thompson     }
64667d9480aSJeremy L Thompson   }
64767d9480aSJeremy L Thompson 
64867d9480aSJeremy L Thompson   // Output restriction
64967d9480aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
65067d9480aSJeremy L Thompson     CeedEvalMode        eval_mode;
65167d9480aSJeremy L Thompson     CeedVector          vec;
65267d9480aSJeremy L Thompson     CeedElemRestriction elem_rstr;
65367d9480aSJeremy L Thompson 
65467d9480aSJeremy L Thompson     // Restore evec
65567d9480aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
65667d9480aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
65767d9480aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
65867d9480aSJeremy L Thompson     }
65967d9480aSJeremy L Thompson     // Get output vector
66067d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
66167d9480aSJeremy L Thompson     // Restrict
66267d9480aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
66367d9480aSJeremy L Thompson     // Active
66467d9480aSJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
66567d9480aSJeremy L Thompson 
66667d9480aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
66767d9480aSJeremy L Thompson   }
66867d9480aSJeremy L Thompson 
66967d9480aSJeremy L Thompson   // Restore input arrays
67067d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
67167d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
67267d9480aSJeremy L Thompson }
67367d9480aSJeremy L Thompson 
67467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
675004e4986SSebastian Grimberg // Linear QFunction Assembly Core
6760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6772b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
6780d0321e0SJeremy L Thompson                                                               CeedRequest *request) {
679b7453713SJeremy L Thompson   Ceed                ceed, ceed_parent;
680b7453713SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
681b7453713SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
682004e4986SSebastian Grimberg   CeedVector         *active_inputs;
683b7453713SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
684b7453713SJeremy L Thompson   CeedQFunction       qf;
685b7453713SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
686b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
687b7453713SJeremy L Thompson 
6882b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
689b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
690e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
691e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
692b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
693004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
694b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
695b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
696004e4986SSebastian Grimberg   active_inputs = impl->qf_active_in;
697004e4986SSebastian Grimberg   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
6980d0321e0SJeremy L Thompson 
6990d0321e0SJeremy L Thompson   // Setup
7002b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Hip(op));
7010d0321e0SJeremy L Thompson 
7020d0321e0SJeremy L Thompson   // Input Evecs and Restriction
703b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
7040d0321e0SJeremy L Thompson 
7050d0321e0SJeremy L Thompson   // Count number of active input fields
706b7453713SJeremy L Thompson   if (!num_active_in) {
707b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
708b7453713SJeremy L Thompson       CeedScalar *q_vec_array;
709b7453713SJeremy L Thompson       CeedVector  vec;
710b7453713SJeremy L Thompson 
7110d0321e0SJeremy L Thompson       // Get input vector
712b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
7130d0321e0SJeremy L Thompson       // Check if active input
7140d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
715b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
716b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
717b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
718004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
7190d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
720004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
721004e4986SSebastian Grimberg 
722004e4986SSebastian Grimberg           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
723b7453713SJeremy L Thompson           CeedCallBackend(
724004e4986SSebastian Grimberg               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
7250d0321e0SJeremy L Thompson         }
726b7453713SJeremy L Thompson         num_active_in += size;
727b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
7280d0321e0SJeremy L Thompson       }
7290d0321e0SJeremy L Thompson     }
730b7453713SJeremy L Thompson     impl->num_active_in = num_active_in;
731004e4986SSebastian Grimberg     impl->qf_active_in  = active_inputs;
7320d0321e0SJeremy L Thompson   }
7330d0321e0SJeremy L Thompson 
7340d0321e0SJeremy L Thompson   // Count number of active output fields
735b7453713SJeremy L Thompson   if (!num_active_out) {
736b7453713SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
737b7453713SJeremy L Thompson       CeedVector vec;
738b7453713SJeremy L Thompson 
7390d0321e0SJeremy L Thompson       // Get output vector
740b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
7410d0321e0SJeremy L Thompson       // Check if active output
7420d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
743b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
744b7453713SJeremy L Thompson         num_active_out += size;
7450d0321e0SJeremy L Thompson       }
7460d0321e0SJeremy L Thompson     }
747b7453713SJeremy L Thompson     impl->num_active_out = num_active_out;
7480d0321e0SJeremy L Thompson   }
7490d0321e0SJeremy L Thompson 
7500d0321e0SJeremy L Thompson   // Check sizes
751b7453713SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
7520d0321e0SJeremy L Thompson 
7530d0321e0SJeremy L Thompson   // Build objects if needed
7540d0321e0SJeremy L Thompson   if (build_objects) {
755b7453713SJeremy L Thompson     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
756b7453713SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
757b7453713SJeremy L Thompson 
758004e4986SSebastian Grimberg     // Create output restriction
759b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
7600a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
7610a5597ceSJeremy L Thompson                                                      rstr));
7620d0321e0SJeremy L Thompson     // Create assembled vector
763b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
7640d0321e0SJeremy L Thompson   }
7652b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
766b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
7670d0321e0SJeremy L Thompson 
7680d0321e0SJeremy L Thompson   // Input basis apply
769b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Hip(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
7700d0321e0SJeremy L Thompson 
7710d0321e0SJeremy L Thompson   // Assemble QFunction
772b7453713SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
7730d0321e0SJeremy L Thompson     // Set Inputs
774004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
775b7453713SJeremy L Thompson     if (num_active_in > 1) {
776004e4986SSebastian Grimberg       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
7770d0321e0SJeremy L Thompson     }
7780d0321e0SJeremy L Thompson     // Set Outputs
779b7453713SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
780b7453713SJeremy L Thompson       CeedVector vec;
781b7453713SJeremy L Thompson 
7820d0321e0SJeremy L Thompson       // Get output vector
783b7453713SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
7840d0321e0SJeremy L Thompson       // Check if active output
7850d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
786b7453713SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
787b7453713SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
788b7453713SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
7890d0321e0SJeremy L Thompson       }
7900d0321e0SJeremy L Thompson     }
7910d0321e0SJeremy L Thompson     // Apply QFunction
792b7453713SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
7930d0321e0SJeremy L Thompson   }
7940d0321e0SJeremy L Thompson 
795004e4986SSebastian Grimberg   // Un-set output q_vecs to prevent accidental overwrite of Assembled
796b7453713SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
797b7453713SJeremy L Thompson     CeedVector vec;
798b7453713SJeremy L Thompson 
7990d0321e0SJeremy L Thompson     // Get output vector
800b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
8010d0321e0SJeremy L Thompson     // Check if active output
8020d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
803b7453713SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
8040d0321e0SJeremy L Thompson     }
8050d0321e0SJeremy L Thompson   }
8060d0321e0SJeremy L Thompson 
8070d0321e0SJeremy L Thompson   // Restore input arrays
808b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
8090d0321e0SJeremy L Thompson 
8100d0321e0SJeremy L Thompson   // Restore output
811b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
8120d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8130d0321e0SJeremy L Thompson }
8140d0321e0SJeremy L Thompson 
8150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8160d0321e0SJeremy L Thompson // Assemble Linear QFunction
8170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8182b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
8192b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, true, assembled, rstr, request);
8200d0321e0SJeremy L Thompson }
8210d0321e0SJeremy L Thompson 
8220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
823b2165e7aSSebastian Grimberg // Update Assembled Linear QFunction
8240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8252b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Hip(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
8262b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Hip(op, false, &assembled, &rstr, request);
8270d0321e0SJeremy L Thompson }
8280d0321e0SJeremy L Thompson 
8290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
830004e4986SSebastian Grimberg // Assemble Diagonal Setup
8310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
832cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) {
8330d0321e0SJeremy L Thompson   Ceed                ceed;
834004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
835cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
836004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
837b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
838b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
8390d0321e0SJeremy L Thompson   CeedQFunction       qf;
840b7453713SJeremy L Thompson   CeedOperatorField  *op_fields;
841b7453713SJeremy L Thompson   CeedOperator_Hip   *impl;
842b7453713SJeremy L Thompson 
843b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8442b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
845b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
8460d0321e0SJeremy L Thompson 
8470d0321e0SJeremy L Thompson   // Determine active input basis
848b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
849b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
850b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
8510d0321e0SJeremy L Thompson     CeedVector vec;
852b7453713SJeremy L Thompson 
853b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8540d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
855004e4986SSebastian Grimberg       CeedBasis    basis;
856004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
857b7453713SJeremy L Thompson 
858004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
859004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
860004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
861004e4986SSebastian Grimberg       basis_in = basis;
862004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
863004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
864004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
865004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
866004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
867004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
868004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
8690d0321e0SJeremy L Thompson       }
8700d0321e0SJeremy L Thompson     }
8710d0321e0SJeremy L Thompson   }
8720d0321e0SJeremy L Thompson 
8730d0321e0SJeremy L Thompson   // Determine active output basis
874b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
875b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
876b7453713SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
8770d0321e0SJeremy L Thompson     CeedVector vec;
878b7453713SJeremy L Thompson 
879b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8800d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
881004e4986SSebastian Grimberg       CeedBasis    basis;
882004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
883b7453713SJeremy L Thompson 
884004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
885004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
886004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
887004e4986SSebastian Grimberg       basis_out = basis;
888004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
889004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
890004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
891004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
892004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
893004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
894004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
8950d0321e0SJeremy L Thompson       }
8960d0321e0SJeremy L Thompson     }
8970d0321e0SJeremy L Thompson   }
8980d0321e0SJeremy L Thompson 
8990d0321e0SJeremy L Thompson   // Operator data struct
9002b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
9012b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
9020d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip *diag = impl->diag;
903b7453713SJeremy L Thompson 
904cbfe683aSSebastian Grimberg   // Basis matrices
905004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
906004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
907004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
908004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
909004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
910004e4986SSebastian Grimberg   bool          has_eval_none    = false;
9110d0321e0SJeremy L Thompson 
9120d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
913004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
914004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
915004e4986SSebastian Grimberg   if (has_eval_none) {
9160d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
917b7453713SJeremy L Thompson 
918004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
919004e4986SSebastian Grimberg     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
920b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&diag->d_identity, interp_bytes));
921b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(diag->d_identity, identity, interp_bytes, hipMemcpyHostToDevice));
922004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
9230d0321e0SJeremy L Thompson   }
9240d0321e0SJeremy L Thompson 
925004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
926004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
927004e4986SSebastian Grimberg     CeedFESpace fespace;
928004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
9290d0321e0SJeremy L Thompson 
930004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
931004e4986SSebastian Grimberg     switch (fespace) {
932004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
933004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
934004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
935004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
9360d0321e0SJeremy L Thompson 
937004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
938004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
9390d0321e0SJeremy L Thompson 
940004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
941004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
942004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
943004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
944004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
945004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_grad, grad, interp_bytes * q_comp_grad, hipMemcpyHostToDevice));
946004e4986SSebastian Grimberg         if (in) {
947004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
948004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
949004e4986SSebastian Grimberg         } else {
950004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
951004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
952004e4986SSebastian Grimberg         }
953004e4986SSebastian Grimberg       } break;
954004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
955004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
956004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
957004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
958004e4986SSebastian Grimberg 
959004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
960004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
961004e4986SSebastian Grimberg 
962004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
963004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
964004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
965004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
966004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_div, interp_bytes * q_comp_div));
967004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_div, div, interp_bytes * q_comp_div, hipMemcpyHostToDevice));
968004e4986SSebastian Grimberg         if (in) {
969004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
970004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
971004e4986SSebastian Grimberg         } else {
972004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
973004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
974004e4986SSebastian Grimberg         }
975004e4986SSebastian Grimberg       } break;
976004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
977004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
978004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
979004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
980004e4986SSebastian Grimberg 
981004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
982004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
983004e4986SSebastian Grimberg 
984004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
985004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
986004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_interp, interp, interp_bytes * q_comp_interp, hipMemcpyHostToDevice));
987004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
988004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
989004e4986SSebastian Grimberg         CeedCallHip(ceed, hipMemcpy(d_curl, curl, interp_bytes * q_comp_curl, hipMemcpyHostToDevice));
990004e4986SSebastian Grimberg         if (in) {
991004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
992004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
993004e4986SSebastian Grimberg         } else {
994004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
995004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
996004e4986SSebastian Grimberg         }
997004e4986SSebastian Grimberg       } break;
998004e4986SSebastian Grimberg     }
999004e4986SSebastian Grimberg   }
1000004e4986SSebastian Grimberg 
1001004e4986SSebastian Grimberg   // Arrays of eval_modes
1002004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
1003004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, hipMemcpyHostToDevice));
1004004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
1005004e4986SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice));
1006004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
1007004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
10080d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10090d0321e0SJeremy L Thompson }
10100d0321e0SJeremy L Thompson 
10110d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1012cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
1013cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1014cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
1015cbfe683aSSebastian Grimberg   Ceed                ceed;
101622070f95SJeremy L Thompson   char               *diagonal_kernel_source;
101722070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
1018cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
1019cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
1020cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
1021cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
1022cbfe683aSSebastian Grimberg   CeedQFunction       qf;
1023cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
1024cbfe683aSSebastian Grimberg   CeedOperator_Hip   *impl;
1025cbfe683aSSebastian Grimberg 
1026cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1027cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1028cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1029cbfe683aSSebastian Grimberg 
1030cbfe683aSSebastian Grimberg   // Determine active input basis
1031cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1032cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1033cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1034cbfe683aSSebastian Grimberg     CeedVector vec;
1035cbfe683aSSebastian Grimberg 
1036cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1037cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1038cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1039cbfe683aSSebastian Grimberg 
1040cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1041cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1042cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1043cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1044cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1045cbfe683aSSebastian Grimberg       }
1046cbfe683aSSebastian Grimberg     }
1047cbfe683aSSebastian Grimberg   }
1048cbfe683aSSebastian Grimberg 
1049cbfe683aSSebastian Grimberg   // Determine active output basis
1050cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1051cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1052cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1053cbfe683aSSebastian Grimberg     CeedVector vec;
1054cbfe683aSSebastian Grimberg 
1055cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1056cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1057cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1058cbfe683aSSebastian Grimberg 
1059cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1060cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1061cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1062cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1063cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1064cbfe683aSSebastian Grimberg       }
1065cbfe683aSSebastian Grimberg     }
1066cbfe683aSSebastian Grimberg   }
1067cbfe683aSSebastian Grimberg 
1068cbfe683aSSebastian Grimberg   // Operator data struct
1069cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1070cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1071cbfe683aSSebastian Grimberg 
1072cbfe683aSSebastian Grimberg   // Assemble kernel
1073cbfe683aSSebastian Grimberg   hipModule_t *module          = is_point_block ? &diag->module_point_block : &diag->module;
1074cbfe683aSSebastian Grimberg   CeedInt      elems_per_block = 1;
1075cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1076cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1077cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1078cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1079cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1080cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1081cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1082cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1083cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedCompile_Hip(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1084cbfe683aSSebastian Grimberg                                     num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1085cbfe683aSSebastian Grimberg                                     use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1086cbfe683aSSebastian Grimberg   CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1087cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1088cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1089cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1090cbfe683aSSebastian Grimberg }
1091cbfe683aSSebastian Grimberg 
1092cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1093004e4986SSebastian Grimberg // Assemble Diagonal Core
10940d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1095b7453713SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
10960d0321e0SJeremy L Thompson   Ceed                ceed;
1097cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1098b7453713SJeremy L Thompson   CeedScalar         *elem_diag_array;
1099b7453713SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1100004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1101004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
11020d0321e0SJeremy L Thompson   CeedOperator_Hip   *impl;
1103b7453713SJeremy L Thompson 
1104b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11052b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
11060d0321e0SJeremy L Thompson 
11070d0321e0SJeremy L Thompson   // Assemble QFunction
1108004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1109004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1110004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
11110d0321e0SJeremy L Thompson 
1112cbfe683aSSebastian Grimberg   // Setup
1113cf8cbdd6SSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Hip(op));
1114cbfe683aSSebastian Grimberg   CeedOperatorDiag_Hip *diag = impl->diag;
1115cbfe683aSSebastian Grimberg 
1116cbfe683aSSebastian Grimberg   assert(diag != NULL);
1117cbfe683aSSebastian Grimberg 
1118cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1119cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1120cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1121cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
11229330daecSnbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1123b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1124b7453713SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
11259330daecSnbeams 
1126cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Hip(op, use_ceedsize_idx, is_point_block));
1127cbfe683aSSebastian Grimberg   }
11280d0321e0SJeremy L Thompson 
1129004e4986SSebastian Grimberg   // Restriction and diagonal vector
1130004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1131004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1132004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1133004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1134004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1135004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1136004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1137004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1138004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
11390d0321e0SJeremy L Thompson   }
1140004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1141004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1142b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
11430d0321e0SJeremy L Thompson 
114491db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1145004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1146004e4986SSebastian Grimberg   if (num_nodes > 0) {
11470d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1148b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
1149b7453713SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
11500d0321e0SJeremy L Thompson 
11510d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1152004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1153004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1154004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1155004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1156004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1157b7453713SJeremy L Thompson 
1158b7453713SJeremy L Thompson     if (is_point_block) {
1159004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
11600d0321e0SJeremy L Thompson     } else {
1161004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
11620d0321e0SJeremy L Thompson     }
11630d0321e0SJeremy L Thompson 
11640d0321e0SJeremy L Thompson     // Restore arrays
1165b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1166b7453713SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
116791db28b6SZach Atkins   }
11680d0321e0SJeremy L Thompson 
11690d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1170b7453713SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
11710d0321e0SJeremy L Thompson 
11720d0321e0SJeremy L Thompson   // Cleanup
1173b7453713SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
11740d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11750d0321e0SJeremy L Thompson }
11760d0321e0SJeremy L Thompson 
11770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11780d0321e0SJeremy L Thompson // Assemble Linear Diagonal
11790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11802b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
11812b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, false));
11826aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11830d0321e0SJeremy L Thompson }
11840d0321e0SJeremy L Thompson 
11850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11860d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
11870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11882b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
11892b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Hip(op, assembled, request, true));
11906aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11910d0321e0SJeremy L Thompson }
11920d0321e0SJeremy L Thompson 
1193a835093fSnbeams //------------------------------------------------------------------------------
1194004e4986SSebastian Grimberg // Single Operator Assembly Setup
1195a835093fSnbeams //------------------------------------------------------------------------------
11969330daecSnbeams static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceedsize_idx) {
1197a835093fSnbeams   Ceed                ceed;
119822070f95SJeremy L Thompson   char               *assembly_kernel_source;
119922070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1200004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
12013b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1202004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1203b7453713SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1204b7453713SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1205b7453713SJeremy L Thompson   CeedQFunctionField *qf_fields;
1206b7453713SJeremy L Thompson   CeedQFunction       qf;
1207b7453713SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1208a835093fSnbeams   CeedOperator_Hip   *impl;
1209b7453713SJeremy L Thompson 
1210b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12112b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1212a835093fSnbeams 
1213a835093fSnbeams   // Get intput and output fields
12142b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1215a835093fSnbeams 
1216a835093fSnbeams   // Determine active input basis eval mode
12172b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
12182b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1219a835093fSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1220a835093fSnbeams     CeedVector vec;
1221b7453713SJeremy L Thompson 
12222b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1223a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1224004e4986SSebastian Grimberg       CeedBasis    basis;
1225b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1226b7453713SJeremy L Thompson 
1227004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1228004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1229004e4986SSebastian Grimberg       basis_in = basis;
12302b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1231004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1232004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1233004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
12342b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1235004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1236004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1237004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1238004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1239004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1240004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1241a835093fSnbeams         }
1242004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1243a835093fSnbeams       }
1244a835093fSnbeams     }
1245a835093fSnbeams   }
1246a835093fSnbeams 
1247a835093fSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
12482b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1249a835093fSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1250a835093fSnbeams     CeedVector vec;
1251b7453713SJeremy L Thompson 
12522b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1253a835093fSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1254004e4986SSebastian Grimberg       CeedBasis    basis;
1255b7453713SJeremy L Thompson       CeedEvalMode eval_mode;
1256b7453713SJeremy L Thompson 
1257004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1258004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1259004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1260004e4986SSebastian Grimberg       basis_out = basis;
12612b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1262004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1263004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1264004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1265004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1266004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
12672b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1268004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1269004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1270004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1271004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1272004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1273004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1274004e4986SSebastian Grimberg         }
1275004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1276a835093fSnbeams       }
1277a835093fSnbeams     }
1278a835093fSnbeams   }
1279004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1280a835093fSnbeams 
12812b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1282a835093fSnbeams   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1283004e4986SSebastian Grimberg   asmb->elems_per_block          = 1;
1284004e4986SSebastian Grimberg   asmb->block_size_x             = elem_size_in;
1285004e4986SSebastian Grimberg   asmb->block_size_y             = elem_size_out;
1286004e4986SSebastian Grimberg 
1287004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > 1024;
1288004e4986SSebastian Grimberg 
1289004e4986SSebastian Grimberg   if (fallback) {
1290004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1291004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1292004e4986SSebastian Grimberg   }
1293a835093fSnbeams 
1294a835093fSnbeams   // Compile kernels
1295004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1296004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
12972b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-operator-assemble.h", &assembly_kernel_path));
129823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
12992b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
130023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1301004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1302004e4986SSebastian Grimberg                                   num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1303004e4986SSebastian Grimberg                                   "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1304cbfe683aSSebastian Grimberg                                   asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y, "USE_CEEDSIZE",
13059330daecSnbeams                                   use_ceedsize_idx));
1306004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
13072b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
13082b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1309a835093fSnbeams 
1310004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1311004e4986SSebastian Grimberg   {
1312004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1313004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1314004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1315004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1316004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1317a835093fSnbeams 
1318004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1319004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1320004e4986SSebastian Grimberg     }
1321004e4986SSebastian Grimberg     if (has_eval_none) {
1322004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1323004e4986SSebastian 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;
1324004e4986SSebastian Grimberg     }
1325b7453713SJeremy L Thompson 
1326b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_in, in_bytes));
1327004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1328004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1329004e4986SSebastian Grimberg 
1330004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1331004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1332004e4986SSebastian Grimberg       if (q_comp > 1) {
1333004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1334004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1335004e4986SSebastian Grimberg       }
1336004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1337004e4986SSebastian Grimberg 
1338004e4986SSebastian 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),
1339004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1340004e4986SSebastian Grimberg     }
1341004e4986SSebastian Grimberg 
1342004e4986SSebastian Grimberg     if (identity) {
1343004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1344a835093fSnbeams     }
1345a835093fSnbeams   }
1346a835093fSnbeams 
1347004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1348004e4986SSebastian Grimberg   {
1349004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1350004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1351004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1352004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1353004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1354b7453713SJeremy L Thompson 
1355004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1356004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1357004e4986SSebastian Grimberg     }
1358004e4986SSebastian Grimberg     if (has_eval_none) {
1359004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1360004e4986SSebastian 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;
1361a835093fSnbeams     }
1362a835093fSnbeams 
1363b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&asmb->d_B_out, out_bytes));
1364004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1365004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1366004e4986SSebastian Grimberg 
1367004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1368004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1369004e4986SSebastian Grimberg       if (q_comp > 1) {
1370004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1371004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1372004e4986SSebastian Grimberg       }
1373004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1374004e4986SSebastian Grimberg 
1375004e4986SSebastian 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),
1376004e4986SSebastian Grimberg                                   hipMemcpyHostToDevice));
1377004e4986SSebastian Grimberg     }
1378004e4986SSebastian Grimberg 
1379004e4986SSebastian Grimberg     if (identity) {
1380004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1381a835093fSnbeams     }
1382a835093fSnbeams   }
1383a835093fSnbeams   return CEED_ERROR_SUCCESS;
1384a835093fSnbeams }
1385a835093fSnbeams 
1386a835093fSnbeams //------------------------------------------------------------------------------
1387cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1388cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1389cefa2673SJeremy L Thompson //
1390ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1391cefa2673SJeremy L Thompson // modes).
1392cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1393a835093fSnbeams //------------------------------------------------------------------------------
13942b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedVector values) {
1395a835093fSnbeams   Ceed                ceed;
1396b7453713SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1397004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1398b7453713SJeremy L Thompson   CeedScalar         *values_array;
1399004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1400b7453713SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1401004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1402004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1403004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1404004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1405a835093fSnbeams   CeedOperator_Hip   *impl;
1406b7453713SJeremy L Thompson 
1407b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
14082b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1409a835093fSnbeams 
1410a835093fSnbeams   // Assemble QFunction
1411004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1412004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1413004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1414a835093fSnbeams 
14159330daecSnbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
14169330daecSnbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
14179330daecSnbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1418004e4986SSebastian Grimberg 
14199330daecSnbeams   // Setup
1420004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Hip(op, use_ceedsize_idx));
1421004e4986SSebastian Grimberg   CeedOperatorAssemble_Hip *asmb = impl->asmb;
1422004e4986SSebastian Grimberg 
1423004e4986SSebastian Grimberg   assert(asmb != NULL);
1424004e4986SSebastian Grimberg 
1425004e4986SSebastian Grimberg   // Assemble element operator
1426004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1427004e4986SSebastian Grimberg   values_array += offset;
1428004e4986SSebastian Grimberg 
1429004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1430004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1431004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1432004e4986SSebastian Grimberg 
1433004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1434004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1435004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1436004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1437004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1438004e4986SSebastian Grimberg   }
1439004e4986SSebastian Grimberg 
1440004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1441004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1442004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1443004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1444004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1445004e4986SSebastian Grimberg 
1446004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1447004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1448004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1449004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1450004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1451004e4986SSebastian Grimberg     }
1452004e4986SSebastian Grimberg   } else {
1453004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1454004e4986SSebastian Grimberg     orients_out      = orients_in;
1455004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
14569330daecSnbeams   }
14579330daecSnbeams 
1458a835093fSnbeams   // Compute B^T D B
1459004e4986SSebastian Grimberg   CeedInt shared_mem =
1460004e4986SSebastian 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)) *
1461004e4986SSebastian Grimberg       sizeof(CeedScalar);
1462004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1463004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1464004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1465b7453713SJeremy L Thompson 
14662b730f8bSJeremy L Thompson   CeedCallBackend(
1467004e4986SSebastian Grimberg       CeedRunKernelDimShared_Hip(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1468a835093fSnbeams 
1469a835093fSnbeams   // Restore arrays
14702b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1471004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1472a835093fSnbeams 
1473a835093fSnbeams   // Cleanup
14742b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1475004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1476004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1477004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1478004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1479004e4986SSebastian Grimberg   }
1480004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1481004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1482004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1483004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1484004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1485004e4986SSebastian Grimberg     }
1486004e4986SSebastian Grimberg   }
1487a835093fSnbeams   return CEED_ERROR_SUCCESS;
1488a835093fSnbeams }
1489a835093fSnbeams 
1490a835093fSnbeams //------------------------------------------------------------------------------
149167d9480aSJeremy L Thompson // Assemble Linear QFunction AtPoints
149267d9480aSJeremy L Thompson //------------------------------------------------------------------------------
149367d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Hip(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
149467d9480aSJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
149567d9480aSJeremy L Thompson }
149667d9480aSJeremy L Thompson 
149767d9480aSJeremy L Thompson //------------------------------------------------------------------------------
149867d9480aSJeremy L Thompson // Assemble Linear Diagonal AtPoints
149967d9480aSJeremy L Thompson //------------------------------------------------------------------------------
150067d9480aSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1501382e9c83SJeremy L Thompson   CeedInt             max_num_points, num_elem, num_input_fields, num_output_fields;
1502afe3bc8aSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1503afe3bc8aSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1504afe3bc8aSJeremy L Thompson   CeedQFunction       qf;
1505afe3bc8aSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1506afe3bc8aSJeremy L Thompson   CeedOperator_Hip   *impl;
1507afe3bc8aSJeremy L Thompson 
1508afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1509afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1510afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1511afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1512afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1513afe3bc8aSJeremy L Thompson   CeedInt num_points[num_elem];
1514afe3bc8aSJeremy L Thompson 
1515afe3bc8aSJeremy L Thompson   // Setup
1516afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Hip(op));
1517afe3bc8aSJeremy L Thompson   max_num_points = impl->max_num_points;
1518afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
1519afe3bc8aSJeremy L Thompson 
1520afe3bc8aSJeremy L Thompson   // Input Evecs and Restriction
1521afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1522afe3bc8aSJeremy L Thompson 
1523afe3bc8aSJeremy L Thompson   // Get point coordinates
1524afe3bc8aSJeremy L Thompson   if (!impl->point_coords_elem) {
1525afe3bc8aSJeremy L Thompson     CeedVector          point_coords = NULL;
1526afe3bc8aSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1527afe3bc8aSJeremy L Thompson 
1528afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1529afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1530afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1531afe3bc8aSJeremy L Thompson   }
1532afe3bc8aSJeremy L Thompson 
1533382e9c83SJeremy L Thompson   // Clear active input Qvecs
1534382e9c83SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1535382e9c83SJeremy L Thompson     CeedVector vec;
1536382e9c83SJeremy L Thompson 
1537382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1538382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1539382e9c83SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
1540382e9c83SJeremy L Thompson   }
1541382e9c83SJeremy L Thompson 
1542afe3bc8aSJeremy L Thompson   // Input basis apply if needed
1543afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Hip(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1544afe3bc8aSJeremy L Thompson 
1545afe3bc8aSJeremy L Thompson   // Output pointers, as necessary
1546afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1547afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1548afe3bc8aSJeremy L Thompson 
1549afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1550afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1551afe3bc8aSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1552afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1553afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1554afe3bc8aSJeremy L Thompson     }
1555afe3bc8aSJeremy L Thompson   }
1556afe3bc8aSJeremy L Thompson 
1557afe3bc8aSJeremy L Thompson   // Loop over active fields
1558afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1559382e9c83SJeremy L Thompson     bool                is_active_at_points = true;
1560382e9c83SJeremy L Thompson     CeedInt             elem_size = 1, num_comp_active = 1, e_vec_size = 0;
1561382e9c83SJeremy L Thompson     CeedRestrictionType rstr_type;
1562382e9c83SJeremy L Thompson     CeedVector          vec;
1563382e9c83SJeremy L Thompson     CeedElemRestriction elem_rstr;
1564382e9c83SJeremy L Thompson 
1565382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1566382e9c83SJeremy L Thompson     // -- Skip non-active input
1567382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1568382e9c83SJeremy L Thompson 
1569382e9c83SJeremy L Thompson     // -- Get active restriction type
1570382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1571382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1572382e9c83SJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1573382e9c83SJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1574382e9c83SJeremy L Thompson     else elem_size = max_num_points;
1575382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1576382e9c83SJeremy L Thompson 
1577382e9c83SJeremy L Thompson     e_vec_size = elem_size * num_comp_active;
1578382e9c83SJeremy L Thompson     for (CeedInt s = 0; s < e_vec_size; s++) {
1579afe3bc8aSJeremy L Thompson       bool         is_active_input = false;
1580afe3bc8aSJeremy L Thompson       CeedEvalMode eval_mode;
1581afe3bc8aSJeremy L Thompson       CeedVector   vec;
1582afe3bc8aSJeremy L Thompson       CeedBasis    basis;
1583afe3bc8aSJeremy L Thompson 
1584afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1585afe3bc8aSJeremy L Thompson       // Skip non-active input
1586afe3bc8aSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1587afe3bc8aSJeremy L Thompson       if (!is_active_input) continue;
1588afe3bc8aSJeremy L Thompson 
1589afe3bc8aSJeremy L Thompson       // Update unit vector
159013062808SJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1591afe3bc8aSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1592afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1593afe3bc8aSJeremy L Thompson 
1594afe3bc8aSJeremy L Thompson       // Basis action
1595afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1596afe3bc8aSJeremy L Thompson       switch (eval_mode) {
1597afe3bc8aSJeremy L Thompson         case CEED_EVAL_NONE:
1598afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1599afe3bc8aSJeremy L Thompson           break;
1600afe3bc8aSJeremy L Thompson         case CEED_EVAL_INTERP:
1601afe3bc8aSJeremy L Thompson         case CEED_EVAL_GRAD:
1602afe3bc8aSJeremy L Thompson         case CEED_EVAL_DIV:
1603afe3bc8aSJeremy L Thompson         case CEED_EVAL_CURL:
1604afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1605afe3bc8aSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1606afe3bc8aSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1607afe3bc8aSJeremy L Thompson           break;
1608afe3bc8aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1609afe3bc8aSJeremy L Thompson           break;  // No action
1610afe3bc8aSJeremy L Thompson       }
1611afe3bc8aSJeremy L Thompson 
1612afe3bc8aSJeremy L Thompson       // Q function
1613afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1614afe3bc8aSJeremy L Thompson 
1615afe3bc8aSJeremy L Thompson       // Output basis apply if needed
1616382e9c83SJeremy L Thompson       for (CeedInt j = 0; j < num_output_fields; j++) {
1617afe3bc8aSJeremy L Thompson         bool                is_active_output = false;
1618382e9c83SJeremy L Thompson         CeedInt             elem_size        = 0;
1619382e9c83SJeremy L Thompson         CeedRestrictionType rstr_type;
1620afe3bc8aSJeremy L Thompson         CeedEvalMode        eval_mode;
1621afe3bc8aSJeremy L Thompson         CeedVector          vec;
1622afe3bc8aSJeremy L Thompson         CeedElemRestriction elem_rstr;
1623afe3bc8aSJeremy L Thompson         CeedBasis           basis;
1624afe3bc8aSJeremy L Thompson 
1625382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec));
1626382e9c83SJeremy L Thompson         // ---- Skip non-active output
1627afe3bc8aSJeremy L Thompson         is_active_output = vec == CEED_VECTOR_ACTIVE;
1628afe3bc8aSJeremy L Thompson         if (!is_active_output) continue;
1629afe3bc8aSJeremy L Thompson 
1630382e9c83SJeremy L Thompson         // ---- Check if elem size matches
1631382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1632382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1633382e9c83SJeremy L Thompson         if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue;
1634382e9c83SJeremy L Thompson         if (rstr_type == CEED_RESTRICTION_POINTS) {
1635382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size));
1636382e9c83SJeremy L Thompson         } else {
1637382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1638382e9c83SJeremy L Thompson         }
1639382e9c83SJeremy L Thompson         {
1640382e9c83SJeremy L Thompson           CeedInt num_comp = 0;
1641382e9c83SJeremy L Thompson 
1642382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1643382e9c83SJeremy L Thompson           if (e_vec_size != num_comp * elem_size) continue;
1644382e9c83SJeremy L Thompson         }
1645382e9c83SJeremy L Thompson 
1646afe3bc8aSJeremy L Thompson         // Basis action
1647382e9c83SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
1648afe3bc8aSJeremy L Thompson         switch (eval_mode) {
1649afe3bc8aSJeremy L Thompson           case CEED_EVAL_NONE:
1650382e9c83SJeremy L Thompson             CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields]));
1651afe3bc8aSJeremy L Thompson             break;
1652afe3bc8aSJeremy L Thompson           case CEED_EVAL_INTERP:
1653afe3bc8aSJeremy L Thompson           case CEED_EVAL_GRAD:
1654afe3bc8aSJeremy L Thompson           case CEED_EVAL_DIV:
1655afe3bc8aSJeremy L Thompson           case CEED_EVAL_CURL:
1656382e9c83SJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis));
1657382e9c83SJeremy L Thompson             CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
1658382e9c83SJeremy L Thompson                                                    impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs]));
1659afe3bc8aSJeremy L Thompson             break;
1660afe3bc8aSJeremy L Thompson           // LCOV_EXCL_START
1661afe3bc8aSJeremy L Thompson           case CEED_EVAL_WEIGHT: {
1662afe3bc8aSJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1663afe3bc8aSJeremy L Thompson             // LCOV_EXCL_STOP
1664afe3bc8aSJeremy L Thompson           }
1665afe3bc8aSJeremy L Thompson         }
1666afe3bc8aSJeremy L Thompson 
1667afe3bc8aSJeremy L Thompson         // Mask output e-vec
1668382e9c83SJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs]));
1669afe3bc8aSJeremy L Thompson 
1670afe3bc8aSJeremy L Thompson         // Restrict
1671382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1672382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request));
1673afe3bc8aSJeremy L Thompson 
1674afe3bc8aSJeremy L Thompson         // Reset q_vec for
1675afe3bc8aSJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) {
1676382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields]));
1677382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields]));
1678afe3bc8aSJeremy L Thompson         }
1679afe3bc8aSJeremy L Thompson       }
1680382e9c83SJeremy L Thompson 
1681382e9c83SJeremy L Thompson       // Reset vec
168213062808SJeremy L Thompson       if (s == e_vec_size - 1 && i != num_input_fields - 1) CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
168386e10729SJeremy L Thompson     }
1684afe3bc8aSJeremy L Thompson   }
1685afe3bc8aSJeremy L Thompson 
1686afe3bc8aSJeremy L Thompson   // Restore CEED_EVAL_NONE
1687afe3bc8aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1688afe3bc8aSJeremy L Thompson     CeedEvalMode eval_mode;
1689afe3bc8aSJeremy L Thompson 
1690afe3bc8aSJeremy L Thompson     // Get eval_mode
1691afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1692afe3bc8aSJeremy L Thompson 
1693afe3bc8aSJeremy L Thompson     // Restore evec
1694afe3bc8aSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1695afe3bc8aSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1696afe3bc8aSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1697afe3bc8aSJeremy L Thompson     }
1698afe3bc8aSJeremy L Thompson   }
1699afe3bc8aSJeremy L Thompson 
1700afe3bc8aSJeremy L Thompson   // Restore input arrays
1701afe3bc8aSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Hip(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1702afe3bc8aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
170367d9480aSJeremy L Thompson }
170467d9480aSJeremy L Thompson 
170567d9480aSJeremy L Thompson //------------------------------------------------------------------------------
17060d0321e0SJeremy L Thompson // Create operator
17070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
17080d0321e0SJeremy L Thompson int CeedOperatorCreate_Hip(CeedOperator op) {
17090d0321e0SJeremy L Thompson   Ceed              ceed;
17100d0321e0SJeremy L Thompson   CeedOperator_Hip *impl;
17110d0321e0SJeremy L Thompson 
1712b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
17132b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
17142b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
17152b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Hip));
17162b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Hip));
17172b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Hip));
17182b730f8bSJeremy L Thompson   CeedCallBackend(
17192b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Hip));
17202b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip));
17212b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip));
17222b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
17230d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17240d0321e0SJeremy L Thompson }
17250d0321e0SJeremy L Thompson 
17260d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
172767d9480aSJeremy L Thompson // Create operator AtPoints
172867d9480aSJeremy L Thompson //------------------------------------------------------------------------------
172967d9480aSJeremy L Thompson int CeedOperatorCreateAtPoints_Hip(CeedOperator op) {
173067d9480aSJeremy L Thompson   Ceed              ceed;
173167d9480aSJeremy L Thompson   CeedOperator_Hip *impl;
173267d9480aSJeremy L Thompson 
173367d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
173467d9480aSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
173567d9480aSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
173667d9480aSJeremy L Thompson 
173767d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Hip));
173867d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip));
173967d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Hip));
174067d9480aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip));
174167d9480aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
174267d9480aSJeremy L Thompson }
174367d9480aSJeremy L Thompson 
174467d9480aSJeremy L Thompson //------------------------------------------------------------------------------
1745